Atlas - testspritesurface.c
Home / ext / SDL / test Lines: 1 | Size: 4778 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Copyright (C) 1997-2025 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: Test surface blitting. */ 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 28static SDL_Surface *sprite; 29static SDL_Rect positions[NUM_SPRITES]; 30static SDL_Rect velocities[NUM_SPRITES]; 31static int sprite_w, sprite_h; 32 33SDL_Window *window; 34SDL_Surface *window_surf; 35static int done; 36 37static SDL_Surface *CreateSurface(unsigned char *data, unsigned int len, int *w, int *h) { 38 SDL_Surface *surface = NULL; 39 SDL_IOStream *src = SDL_IOFromConstMem(data, len); 40 if (src) { 41 surface = SDL_LoadPNG_IO(src, true); 42 if (surface) { 43 /* Treat white as transparent */ 44 SDL_SetSurfaceColorKey(surface, true, SDL_MapSurfaceRGB(surface, 255, 255, 255)); 45 46 *w = surface->w; 47 *h = surface->h; 48 } 49 } 50 return surface; 51} 52 53static void MoveSprites(void) 54{ 55 int i; 56 int window_w = WINDOW_WIDTH; 57 int window_h = WINDOW_HEIGHT; 58 SDL_Rect *position, *velocity; 59 Uint32 background = SDL_MapSurfaceRGB(window_surf, 0xA0, 0xA0, 0xA0); 60 SDL_FillSurfaceRect(window_surf, NULL, background); 61 62 /* Move the sprite, bounce at the wall, and draw */ 63 for (i = 0; i < NUM_SPRITES; ++i) { 64 position = &positions[i]; 65 velocity = &velocities[i]; 66 position->x += velocity->x; 67 if ((position->x < 0) || (position->x >= (window_w - sprite_w))) { 68 velocity->x = -velocity->x; 69 position->x += velocity->x; 70 } 71 position->y += velocity->y; 72 if ((position->y < 0) || (position->y >= (window_h - sprite_h))) { 73 velocity->y = -velocity->y; 74 position->y += velocity->y; 75 } 76 77 /* Blit the sprite onto the screen */ 78 SDL_BlitSurface(sprite, NULL, window_surf, position); 79 } 80 81 /* Update the screen! */ 82 SDL_UpdateWindowSurface(window); 83} 84 85static void loop(void) 86{ 87 SDL_Event event; 88 89 /* Check for events */ 90 while (SDL_PollEvent(&event)) { 91 if (event.type == SDL_EVENT_QUIT || event.type == SDL_EVENT_KEY_DOWN) { 92 done = 1; 93 } 94 } 95 MoveSprites(); 96#ifdef SDL_PLATFORM_EMSCRIPTEN 97 if (done) { 98 emscripten_cancel_main_loop(); 99 } 100#endif 101} 102 103int main(int argc, char *argv[]) 104{ 105 int return_code = -1; 106 int i; 107 108 if (argc > 1) { 109 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "USAGE: %s", argv[0]); 110 return_code = 1; 111 goto quit; 112 } 113 114 if ((window = SDL_CreateWindow("testspritesurface", WINDOW_WIDTH, WINDOW_HEIGHT, 0)) == NULL) { 115 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window (%s)", SDL_GetError()); 116 return_code = 2; 117 goto quit; 118 } 119 120 if ((window_surf = SDL_GetWindowSurface(window)) == NULL) { 121 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't acquire window surface (%s)", SDL_GetError()); 122 return_code = 3; 123 goto quit; 124 } 125 126 sprite = CreateSurface(icon_png, icon_png_len, &sprite_w, &sprite_h); 127 128 if (!sprite) { 129 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create surface (%s)", SDL_GetError()); 130 return_code = 4; 131 goto quit; 132 } 133 134 /* Initialize the sprite positions */ 135 for (i = 0; i < NUM_SPRITES; ++i) { 136 positions[i].x = SDL_rand(WINDOW_WIDTH - sprite_w); 137 positions[i].y = SDL_rand(WINDOW_HEIGHT - sprite_h); 138 positions[i].w = sprite_w; 139 positions[i].h = sprite_h; 140 velocities[i].x = 0; 141 velocities[i].y = 0; 142 while (velocities[i].x == 0.f && velocities[i].y == 0.f) { 143 velocities[i].x = (SDL_rand(MAX_SPEED * 2 + 1)) - MAX_SPEED; 144 velocities[i].y = (SDL_rand(MAX_SPEED * 2 + 1)) - MAX_SPEED; 145 } 146 } 147 148 /* Main render loop */ 149 done = 0; 150 151#ifdef SDL_PLATFORM_EMSCRIPTEN 152 emscripten_set_main_loop(loop, 0, 1); 153#else 154 while (!done) { 155 loop(); 156 } 157#endif 158 return_code = 0; 159quit: 160 if (sprite) { 161 SDL_DestroySurface(sprite); 162 sprite = NULL; 163 } 164 if (window_surf) { 165 SDL_DestroyWindowSurface(window); 166 window_surf = NULL; 167 } 168 if (window) { 169 SDL_DestroyWindow(window); 170 window = NULL; 171 } 172 SDL_Quit(); 173 return return_code; 174} 175[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.