2015-06-21 18:33:46 +03:00
|
|
|
/*
|
2024-01-02 00:15:26 +03:00
|
|
|
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 18:33:46 +03:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely.
|
|
|
|
*/
|
|
|
|
/* Simple program: Move N sprites around on the screen as fast as possible */
|
|
|
|
|
2024-01-24 04:40:51 +03:00
|
|
|
#include <SDL3/SDL_test_common.h>
|
|
|
|
#include <SDL3/SDL_main.h>
|
|
|
|
#include "testutils.h"
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2024-01-24 04:40:51 +03:00
|
|
|
#ifdef SDL_PLATFORM_EMSCRIPTEN
|
2015-06-21 18:33:46 +03:00
|
|
|
#include <emscripten/emscripten.h>
|
|
|
|
#endif
|
|
|
|
|
2024-01-24 04:40:51 +03:00
|
|
|
#include <stdlib.h>
|
2015-06-21 18:33:46 +03:00
|
|
|
|
|
|
|
static SDLTest_CommonState *state;
|
|
|
|
|
2022-11-30 23:51:59 +03:00
|
|
|
typedef struct
|
|
|
|
{
|
2015-06-21 18:33:46 +03:00
|
|
|
SDL_Window *window;
|
|
|
|
SDL_Renderer *renderer;
|
|
|
|
SDL_Texture *background;
|
|
|
|
SDL_Texture *sprite;
|
2022-12-31 22:19:32 +03:00
|
|
|
SDL_FRect sprite_rect;
|
2015-06-21 18:33:46 +03:00
|
|
|
int scale_direction;
|
|
|
|
} DrawState;
|
|
|
|
|
2023-03-08 13:40:07 +03:00
|
|
|
static DrawState *drawstates;
|
|
|
|
static int done;
|
2015-06-21 18:33:46 +03:00
|
|
|
|
|
|
|
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
|
|
|
static void
|
|
|
|
quit(int rc)
|
|
|
|
{
|
|
|
|
SDLTest_CommonQuit(state);
|
2023-04-12 12:07:28 +03:00
|
|
|
/* Let 'main()' return normally */
|
|
|
|
if (rc != 0) {
|
|
|
|
exit(rc);
|
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
|
|
|
|
2023-03-08 13:40:07 +03:00
|
|
|
static void Draw(DrawState *s)
|
2015-06-21 18:33:46 +03:00
|
|
|
{
|
2024-06-13 05:08:06 +03:00
|
|
|
SDL_Rect viewport;
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2022-12-27 17:21:13 +03:00
|
|
|
SDL_GetRenderViewport(s->renderer, &viewport);
|
2015-06-21 18:33:46 +03:00
|
|
|
|
|
|
|
/* Draw the background */
|
2022-12-27 17:21:13 +03:00
|
|
|
SDL_RenderTexture(s->renderer, s->background, NULL, NULL);
|
2015-06-21 18:33:46 +03:00
|
|
|
|
|
|
|
/* Scale and draw the sprite */
|
|
|
|
s->sprite_rect.w += s->scale_direction;
|
|
|
|
s->sprite_rect.h += s->scale_direction;
|
|
|
|
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->scale_direction = 1;
|
|
|
|
}
|
|
|
|
}
|
2024-06-13 05:08:06 +03:00
|
|
|
s->sprite_rect.x = (float)((viewport.w - s->sprite_rect.w) / 2);
|
|
|
|
s->sprite_rect.y = (float)((viewport.h - s->sprite_rect.h) / 2);
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2022-12-27 17:21:13 +03:00
|
|
|
SDL_RenderTexture(s->renderer, s->sprite, NULL, &s->sprite_rect);
|
2015-06-21 18:33:46 +03:00
|
|
|
|
|
|
|
/* Update the screen! */
|
|
|
|
SDL_RenderPresent(s->renderer);
|
|
|
|
}
|
|
|
|
|
2023-03-08 13:40:07 +03:00
|
|
|
static void loop(void)
|
2015-06-21 18:33:46 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
SDL_Event event;
|
|
|
|
|
|
|
|
/* Check for events */
|
|
|
|
while (SDL_PollEvent(&event)) {
|
|
|
|
SDLTest_CommonEvent(state, &event, &done);
|
|
|
|
}
|
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
2022-11-27 19:38:43 +03:00
|
|
|
if (state->windows[i] == NULL) {
|
2015-06-21 18:33:46 +03:00
|
|
|
continue;
|
2022-11-27 19:38:43 +03:00
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
Draw(&drawstates[i]);
|
|
|
|
}
|
2024-01-24 04:40:51 +03:00
|
|
|
#ifdef SDL_PLATFORM_EMSCRIPTEN
|
2015-06-21 18:33:46 +03:00
|
|
|
if (done) {
|
|
|
|
emscripten_cancel_main_loop();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-11-30 23:51:59 +03:00
|
|
|
int main(int argc, char *argv[])
|
2015-06-21 18:33:46 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int frames;
|
2022-12-02 12:17:17 +03:00
|
|
|
Uint64 then, now;
|
2015-06-21 18:33:46 +03:00
|
|
|
|
|
|
|
/* Initialize test framework */
|
|
|
|
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
|
2023-11-10 00:29:15 +03:00
|
|
|
if (!state) {
|
2015-06-21 18:33:46 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-03-17 02:25:39 +03:00
|
|
|
/* Enable standard application logging */
|
2024-05-16 20:00:50 +03:00
|
|
|
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
2023-03-17 02:25:39 +03:00
|
|
|
|
|
|
|
/* Parse commandline */
|
|
|
|
if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
|
2019-05-19 08:45:15 +03:00
|
|
|
return 1;
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
|
|
|
|
2023-03-17 02:25:39 +03:00
|
|
|
if (!SDLTest_CommonInit(state)) {
|
|
|
|
quit(1);
|
|
|
|
}
|
|
|
|
|
2015-06-21 18:33:46 +03:00
|
|
|
drawstates = SDL_stack_alloc(DrawState, state->num_windows);
|
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
|
|
|
DrawState *drawstate = &drawstates[i];
|
|
|
|
|
|
|
|
drawstate->window = state->windows[i];
|
|
|
|
drawstate->renderer = state->renderers[i];
|
2022-04-12 15:07:18 +03:00
|
|
|
drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE, NULL, NULL);
|
|
|
|
drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE, NULL, NULL);
|
2015-06-21 18:33:46 +03:00
|
|
|
if (!drawstate->sprite || !drawstate->background) {
|
|
|
|
quit(2);
|
|
|
|
}
|
2024-06-13 05:08:06 +03:00
|
|
|
SDL_GetTextureSize(drawstate->sprite, &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
|
2015-06-21 18:33:46 +03:00
|
|
|
drawstate->scale_direction = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Main render loop */
|
|
|
|
frames = 0;
|
|
|
|
then = SDL_GetTicks();
|
|
|
|
done = 0;
|
|
|
|
|
2024-01-24 04:40:51 +03:00
|
|
|
#ifdef SDL_PLATFORM_EMSCRIPTEN
|
2015-06-21 18:33:46 +03:00
|
|
|
emscripten_set_main_loop(loop, 0, 1);
|
|
|
|
#else
|
|
|
|
while (!done) {
|
|
|
|
++frames;
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Print out some timing information */
|
|
|
|
now = SDL_GetTicks();
|
|
|
|
if (now > then) {
|
2022-11-30 23:51:59 +03:00
|
|
|
double fps = ((double)frames * 1000) / (now - then);
|
2015-06-21 18:33:46 +03:00
|
|
|
SDL_Log("%2.2f frames per second\n", fps);
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_stack_free(drawstates);
|
|
|
|
|
|
|
|
quit(0);
|
|
|
|
return 0;
|
|
|
|
}
|