SDL  2.0
testscale.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "SDL_test_common.h"
+ Include dependency graph for testscale.c:

Go to the source code of this file.

Data Structures

struct  DrawState

Macros

#define WINDOW_WIDTH   640
#define WINDOW_HEIGHT   480

Functions

static void quit (int rc)
SDL_TextureLoadTexture (SDL_Renderer *renderer, char *file, SDL_bool transparent)
void Draw (DrawState *s)
void loop ()
int main (int argc, char *argv[])

Variables

static SDLTest_CommonStatestate
DrawStatedrawstates
int done

Macro Definition Documentation

#define WINDOW_HEIGHT   480

Definition at line 25 of file testscale.c.

#define WINDOW_WIDTH   640

Definition at line 24 of file testscale.c.

Function Documentation

void Draw ( DrawState s)

Definition at line 100 of file testscale.c.

References DrawState::background, SDL_Rect::h, NULL, DrawState::renderer, DrawState::scale_direction, SDL_RenderCopy, SDL_RenderGetViewport, SDL_RenderPresent, DrawState::sprite, DrawState::sprite_rect, viewport, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

{
/* Draw the background */
/* Scale and draw the sprite */
if (s->scale_direction > 0) {
if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
s->scale_direction = -1;
}
} else {
if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
}
}
s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
/* Update the screen! */
}
SDL_Texture* LoadTexture ( SDL_Renderer renderer,
char *  file,
SDL_bool  transparent 
)

Definition at line 50 of file testscale.c.

References SDL_PixelFormat::BitsPerPixel, SDL_Surface::format, NULL, SDL_PixelFormat::palette, SDL_Surface::pixels, SDL_CreateTextureFromSurface, SDL_FreeSurface, SDL_GetError, SDL_LoadBMP, SDL_LOG_CATEGORY_APPLICATION, SDL_LogError, SDL_SetColorKey, and SDL_TRUE.

{
SDL_Surface *temp;
/* Load the sprite image */
temp = SDL_LoadBMP(file);
if (temp == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
return NULL;
}
/* Set transparent pixel as the pixel at (0,0) */
if (transparent) {
if (temp->format->palette) {
SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
} else {
switch (temp->format->BitsPerPixel) {
case 15:
(*(Uint16 *) temp->pixels) & 0x00007FFF);
break;
case 16:
SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
break;
case 24:
(*(Uint32 *) temp->pixels) & 0x00FFFFFF);
break;
case 32:
SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
break;
}
}
}
/* Create textures from the image */
texture = SDL_CreateTextureFromSurface(renderer, temp);
if (!texture) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
return NULL;
}
/* We're ready to roll. :) */
return texture;
}
void loop ( )

Definition at line 131 of file testscale.c.

References done, Draw(), i, NULL, SDLTest_CommonState::num_windows, SDL_PollEvent, SDLTest_CommonEvent(), and SDLTest_CommonState::windows.

{
int i;
/* Check for events */
while (SDL_PollEvent(&event)) {
}
for (i = 0; i < state->num_windows; ++i) {
if (state->windows[i] == NULL)
continue;
}
#ifdef __EMSCRIPTEN__
if (done) {
emscripten_cancel_main_loop();
}
#endif
}
int main ( int  argc,
char *  argv[] 
)

Definition at line 153 of file testscale.c.

References DrawState::background, done, SDL_Rect::h, i, LoadTexture(), loop(), NULL, SDLTest_CommonState::num_windows, quit(), DrawState::renderer, SDLTest_CommonState::renderers, DrawState::scale_direction, SDL_FALSE, SDL_GetTicks(), SDL_INIT_VIDEO, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogSetPriority, SDL_QueryTexture, SDL_stack_alloc, SDL_stack_free, SDL_TRUE, SDLTest_CommonArg(), SDLTest_CommonCreateState(), SDLTest_CommonInit(), SDLTest_CommonUsage(), DrawState::sprite, DrawState::sprite_rect, SDL_Rect::w, DrawState::window, and SDLTest_CommonState::windows.

{
int i;
int frames;
Uint32 then, now;
/* Enable standard application logging */
/* Initialize test framework */
if (!state) {
return 1;
}
for (i = 1; i < argc;) {
int consumed;
consumed = SDLTest_CommonArg(state, i);
if (consumed == 0) {
SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
return 1;
}
i += consumed;
}
quit(2);
}
for (i = 0; i < state->num_windows; ++i) {
DrawState *drawstate = &drawstates[i];
drawstate->window = state->windows[i];
drawstate->renderer = state->renderers[i];
drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
if (!drawstate->sprite || !drawstate->background) {
quit(2);
}
&drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
drawstate->scale_direction = 1;
}
/* Main render loop */
frames = 0;
then = SDL_GetTicks();
done = 0;
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(loop, 0, 1);
#else
while (!done) {
++frames;
loop();
}
#endif
/* Print out some timing information */
now = SDL_GetTicks();
if (now > then) {
double fps = ((double) frames * 1000) / (now - then);
SDL_Log("%2.2f frames per second\n", fps);
}
quit(0);
return 0;
}
static void quit ( int  rc)
static

Definition at line 43 of file testscale.c.

References SDLTest_CommonQuit().

{
exit(rc);
}

Variable Documentation

int done

Definition at line 39 of file testscale.c.

DrawState* drawstates

Definition at line 38 of file testscale.c.

SDLTest_CommonState* state
static

Definition at line 27 of file testscale.c.