diff options
Diffstat (limited to 'contrib/SDL-3.2.8/test/testspriteminimal.c')
| -rw-r--r-- | contrib/SDL-3.2.8/test/testspriteminimal.c | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/test/testspriteminimal.c b/contrib/SDL-3.2.8/test/testspriteminimal.c new file mode 100644 index 0000000..bf2a834 --- /dev/null +++ b/contrib/SDL-3.2.8/test/testspriteminimal.c | |||
| @@ -0,0 +1,166 @@ | |||
| 1 | /* | ||
| 2 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 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 <SDL3/SDL.h> | ||
| 15 | #include <SDL3/SDL_main.h> | ||
| 16 | |||
| 17 | #ifdef SDL_PLATFORM_EMSCRIPTEN | ||
| 18 | #include <emscripten/emscripten.h> | ||
| 19 | #endif | ||
| 20 | |||
| 21 | #include "icon.h" | ||
| 22 | |||
| 23 | #define WINDOW_WIDTH 640 | ||
| 24 | #define WINDOW_HEIGHT 480 | ||
| 25 | #define NUM_SPRITES 100 | ||
| 26 | #define MAX_SPEED 1 | ||
| 27 | |||
| 28 | static SDL_Texture *sprite; | ||
| 29 | static SDL_FRect positions[NUM_SPRITES]; | ||
| 30 | static SDL_FRect velocities[NUM_SPRITES]; | ||
| 31 | static int sprite_w, sprite_h; | ||
| 32 | |||
| 33 | static SDL_Renderer *renderer; | ||
| 34 | static int done; | ||
| 35 | |||
| 36 | static SDL_Texture *CreateTexture(SDL_Renderer *r, unsigned char *data, unsigned int len, int *w, int *h) | ||
| 37 | { | ||
| 38 | SDL_Texture *texture = NULL; | ||
| 39 | SDL_Surface *surface; | ||
| 40 | SDL_IOStream *src = SDL_IOFromConstMem(data, len); | ||
| 41 | if (src) { | ||
| 42 | surface = SDL_LoadBMP_IO(src, true); | ||
| 43 | if (surface) { | ||
| 44 | /* Treat white as transparent */ | ||
| 45 | SDL_SetSurfaceColorKey(surface, true, SDL_MapSurfaceRGB(surface, 255, 255, 255)); | ||
| 46 | |||
| 47 | texture = SDL_CreateTextureFromSurface(r, surface); | ||
| 48 | *w = surface->w; | ||
| 49 | *h = surface->h; | ||
| 50 | SDL_DestroySurface(surface); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | return texture; | ||
| 54 | } | ||
| 55 | |||
| 56 | static void MoveSprites(void) | ||
| 57 | { | ||
| 58 | int i; | ||
| 59 | int window_w = WINDOW_WIDTH; | ||
| 60 | int window_h = WINDOW_HEIGHT; | ||
| 61 | SDL_FRect *position, *velocity; | ||
| 62 | |||
| 63 | /* Draw a gray background */ | ||
| 64 | SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); | ||
| 65 | SDL_RenderClear(renderer); | ||
| 66 | |||
| 67 | /* Move the sprite, bounce at the wall, and draw */ | ||
| 68 | for (i = 0; i < NUM_SPRITES; ++i) { | ||
| 69 | position = &positions[i]; | ||
| 70 | velocity = &velocities[i]; | ||
| 71 | position->x += velocity->x; | ||
| 72 | if ((position->x < 0) || (position->x >= (window_w - sprite_w))) { | ||
| 73 | velocity->x = -velocity->x; | ||
| 74 | position->x += velocity->x; | ||
| 75 | } | ||
| 76 | position->y += velocity->y; | ||
| 77 | if ((position->y < 0) || (position->y >= (window_h - sprite_h))) { | ||
| 78 | velocity->y = -velocity->y; | ||
| 79 | position->y += velocity->y; | ||
| 80 | } | ||
| 81 | |||
| 82 | /* Blit the sprite onto the screen */ | ||
| 83 | SDL_RenderTexture(renderer, sprite, NULL, position); | ||
| 84 | } | ||
| 85 | |||
| 86 | /* Update the screen! */ | ||
| 87 | SDL_RenderPresent(renderer); | ||
| 88 | } | ||
| 89 | |||
| 90 | static void loop(void) | ||
| 91 | { | ||
| 92 | SDL_Event event; | ||
| 93 | |||
| 94 | /* Check for events */ | ||
| 95 | while (SDL_PollEvent(&event)) { | ||
| 96 | if (event.type == SDL_EVENT_QUIT || | ||
| 97 | (event.type == SDL_EVENT_KEY_DOWN && event.key.key == SDLK_ESCAPE)) { | ||
| 98 | done = 1; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | MoveSprites(); | ||
| 102 | #ifdef SDL_PLATFORM_EMSCRIPTEN | ||
| 103 | if (done) { | ||
| 104 | emscripten_cancel_main_loop(); | ||
| 105 | } | ||
| 106 | #endif | ||
| 107 | } | ||
| 108 | |||
| 109 | int main(int argc, char *argv[]) | ||
| 110 | { | ||
| 111 | SDL_Window *window = NULL; | ||
| 112 | int return_code = -1; | ||
| 113 | int i; | ||
| 114 | |||
| 115 | if (argc > 1) { | ||
| 116 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "USAGE: %s", argv[0]); | ||
| 117 | return_code = 1; | ||
| 118 | goto quit; | ||
| 119 | } | ||
| 120 | |||
| 121 | if (!SDL_CreateWindowAndRenderer("testspriteminimal", WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_RESIZABLE, &window, &renderer)) { | ||
| 122 | return_code = 2; | ||
| 123 | goto quit; | ||
| 124 | } | ||
| 125 | |||
| 126 | SDL_SetRenderLogicalPresentation(renderer, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_LOGICAL_PRESENTATION_LETTERBOX); | ||
| 127 | |||
| 128 | sprite = CreateTexture(renderer, icon_bmp, icon_bmp_len, &sprite_w, &sprite_h); | ||
| 129 | |||
| 130 | if (!sprite) { | ||
| 131 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture (%s)", SDL_GetError()); | ||
| 132 | return_code = 3; | ||
| 133 | goto quit; | ||
| 134 | } | ||
| 135 | |||
| 136 | /* Initialize the sprite positions */ | ||
| 137 | for (i = 0; i < NUM_SPRITES; ++i) { | ||
| 138 | positions[i].x = (float)SDL_rand(WINDOW_WIDTH - sprite_w); | ||
| 139 | positions[i].y = (float)SDL_rand(WINDOW_HEIGHT - sprite_h); | ||
| 140 | positions[i].w = (float)sprite_w; | ||
| 141 | positions[i].h = (float)sprite_h; | ||
| 142 | velocities[i].x = 0.0f; | ||
| 143 | velocities[i].y = 0.0f; | ||
| 144 | while (velocities[i].x == 0.f && velocities[i].y == 0.f) { | ||
| 145 | velocities[i].x = (float)(SDL_rand(MAX_SPEED * 2 + 1) - MAX_SPEED); | ||
| 146 | velocities[i].y = (float)(SDL_rand(MAX_SPEED * 2 + 1) - MAX_SPEED); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | /* Main render loop */ | ||
| 151 | done = 0; | ||
| 152 | |||
| 153 | #ifdef SDL_PLATFORM_EMSCRIPTEN | ||
| 154 | emscripten_set_main_loop(loop, 0, 1); | ||
| 155 | #else | ||
| 156 | while (!done) { | ||
| 157 | loop(); | ||
| 158 | } | ||
| 159 | #endif | ||
| 160 | return_code = 0; | ||
| 161 | quit: | ||
| 162 | SDL_DestroyRenderer(renderer); | ||
| 163 | SDL_DestroyWindow(window); | ||
| 164 | SDL_Quit(); | ||
| 165 | return return_code; | ||
| 166 | } | ||
