| 1 |
/* |
| 2 |
Copyright (C) 1997-2026 Sam Lantinga <[email protected]> |
| 3 |
|
| 4 |
This software is provided 'as-is', without any express or implied |
| 5 |
warranty. In no event will the authors be held liable for any damages |
| 6 |
arising from the use of this software. |
| 7 |
|
| 8 |
Permission is granted to anyone to use this software for any purpose, |
| 9 |
including commercial applications, and to alter it and redistribute it |
| 10 |
freely. |
| 11 |
*/ |
| 12 |
/* Simple program: Move N sprites around on the screen as fast as possible */ |
| 13 |
|
| 14 |
#include <stdlib.h> |
| 15 |
#include <stdio.h> |
| 16 |
#include <time.h> |
| 17 |
|
| 18 |
#ifdef __EMSCRIPTEN__ |
| 19 |
#include <emscripten/emscripten.h> |
| 20 |
#endif |
| 21 |
|
| 22 |
#include "SDL_test_common.h" |
| 23 |
#include "testutils.h" |
| 24 |
|
| 25 |
#define WINDOW_WIDTH 640 |
| 26 |
#define WINDOW_HEIGHT 480 |
| 27 |
|
| 28 |
static SDLTest_CommonState *state; |
| 29 |
|
| 30 |
typedef struct |
| 31 |
{ |
| 32 |
SDL_Window *window; |
| 33 |
SDL_Renderer *renderer; |
| 34 |
SDL_Texture *background; |
| 35 |
SDL_Texture *sprite; |
| 36 |
SDL_Rect sprite_rect; |
| 37 |
int scale_direction; |
| 38 |
} DrawState; |
| 39 |
|
| 40 |
DrawState *drawstates; |
| 41 |
int done; |
| 42 |
|
| 43 |
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ |
| 44 |
static void |
| 45 |
quit(int rc) |
| 46 |
{ |
| 47 |
SDLTest_CommonQuit(state); |
| 48 |
exit(rc); |
| 49 |
} |
| 50 |
|
| 51 |
void Draw(DrawState *s) |
| 52 |
{ |
| 53 |
SDL_Rect viewport; |
| 54 |
|
| 55 |
SDL_RenderGetViewport(s->renderer, &viewport); |
| 56 |
|
| 57 |
/* Draw the background */ |
| 58 |
SDL_RenderCopy(s->renderer, s->background, NULL, NULL); |
| 59 |
|
| 60 |
/* Scale and draw the sprite */ |
| 61 |
s->sprite_rect.w += s->scale_direction; |
| 62 |
s->sprite_rect.h += s->scale_direction; |
| 63 |
if (s->scale_direction > 0) { |
| 64 |
if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) { |
| 65 |
s->scale_direction = -1; |
| 66 |
} |
| 67 |
} else { |
| 68 |
if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) { |
| 69 |
s->scale_direction = 1; |
| 70 |
} |
| 71 |
} |
| 72 |
s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2; |
| 73 |
s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2; |
| 74 |
|
| 75 |
SDL_RenderCopy(s->renderer, s->sprite, NULL, &s->sprite_rect); |
| 76 |
|
| 77 |
/* Update the screen! */ |
| 78 |
SDL_RenderPresent(s->renderer); |
| 79 |
} |
| 80 |
|
| 81 |
void loop(void) |
| 82 |
{ |
| 83 |
int i; |
| 84 |
SDL_Event event; |
| 85 |
|
| 86 |
/* Check for events */ |
| 87 |
while (SDL_PollEvent(&event)) { |
| 88 |
SDLTest_CommonEvent(state, &event, &done); |
| 89 |
} |
| 90 |
for (i = 0; i < state->num_windows; ++i) { |
| 91 |
if (state->windows[i] == NULL) { |
| 92 |
continue; |
| 93 |
} |
| 94 |
Draw(&drawstates[i]); |
| 95 |
} |
| 96 |
#ifdef __EMSCRIPTEN__ |
| 97 |
if (done) { |
| 98 |
emscripten_cancel_main_loop(); |
| 99 |
} |
| 100 |
#endif |
| 101 |
} |
| 102 |
|
| 103 |
int main(int argc, char *argv[]) |
| 104 |
{ |
| 105 |
int i; |
| 106 |
int frames; |
| 107 |
Uint32 then, now; |
| 108 |
|
| 109 |
/* Enable standard application logging */ |
| 110 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
| 111 |
|
| 112 |
/* Initialize test framework */ |
| 113 |
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); |
| 114 |
if (!state) { |
| 115 |
return 1; |
| 116 |
} |
| 117 |
|
| 118 |
if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) { |
| 119 |
SDLTest_CommonQuit(state); |
| 120 |
return 1; |
| 121 |
} |
| 122 |
|
| 123 |
drawstates = SDL_stack_alloc(DrawState, state->num_windows); |
| 124 |
for (i = 0; i < state->num_windows; ++i) { |
| 125 |
DrawState *drawstate = &drawstates[i]; |
| 126 |
|
| 127 |
drawstate->window = state->windows[i]; |
| 128 |
drawstate->renderer = state->renderers[i]; |
| 129 |
drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE, NULL, NULL); |
| 130 |
drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE, NULL, NULL); |
| 131 |
if (!drawstate->sprite || !drawstate->background) { |
| 132 |
quit(2); |
| 133 |
} |
| 134 |
SDL_QueryTexture(drawstate->sprite, NULL, NULL, |
| 135 |
&drawstate->sprite_rect.w, &drawstate->sprite_rect.h); |
| 136 |
drawstate->scale_direction = 1; |
| 137 |
} |
| 138 |
|
| 139 |
/* Main render loop */ |
| 140 |
frames = 0; |
| 141 |
then = SDL_GetTicks(); |
| 142 |
done = 0; |
| 143 |
|
| 144 |
#ifdef __EMSCRIPTEN__ |
| 145 |
emscripten_set_main_loop(loop, 0, 1); |
| 146 |
#else |
| 147 |
while (!done) { |
| 148 |
++frames; |
| 149 |
loop(); |
| 150 |
} |
| 151 |
#endif |
| 152 |
|
| 153 |
/* Print out some timing information */ |
| 154 |
now = SDL_GetTicks(); |
| 155 |
if (now > then) { |
| 156 |
double fps = ((double)frames * 1000) / (now - then); |
| 157 |
SDL_Log("%2.2f frames per second\n", fps); |
| 158 |
} |
| 159 |
|
| 160 |
SDL_stack_free(drawstates); |
| 161 |
|
| 162 |
quit(0); |
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
/* vi: set ts=4 sw=4 expandtab: */ |