| 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.h" |
| 23 |
#include "testutils.h" |
| 24 |
|
| 25 |
#define WINDOW_WIDTH 640 |
| 26 |
#define WINDOW_HEIGHT 480 |
| 27 |
#define NUM_SPRITES 100 |
| 28 |
#define MAX_SPEED 1 |
| 29 |
|
| 30 |
static SDL_Texture *sprite; |
| 31 |
static SDL_Rect positions[NUM_SPRITES]; |
| 32 |
static SDL_Rect velocities[NUM_SPRITES]; |
| 33 |
static int sprite_w, sprite_h; |
| 34 |
|
| 35 |
SDL_Renderer *renderer; |
| 36 |
int done; |
| 37 |
|
| 38 |
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ |
| 39 |
static void |
| 40 |
quit(int rc) |
| 41 |
{ |
| 42 |
SDL_Quit(); |
| 43 |
exit(rc); |
| 44 |
} |
| 45 |
|
| 46 |
void MoveSprites(void) |
| 47 |
{ |
| 48 |
int i; |
| 49 |
int window_w = WINDOW_WIDTH; |
| 50 |
int window_h = WINDOW_HEIGHT; |
| 51 |
SDL_Rect *position, *velocity; |
| 52 |
|
| 53 |
/* Draw a gray background */ |
| 54 |
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); |
| 55 |
SDL_RenderClear(renderer); |
| 56 |
|
| 57 |
/* Move the sprite, bounce at the wall, and draw */ |
| 58 |
for (i = 0; i < NUM_SPRITES; ++i) { |
| 59 |
position = &positions[i]; |
| 60 |
velocity = &velocities[i]; |
| 61 |
position->x += velocity->x; |
| 62 |
if ((position->x < 0) || (position->x >= (window_w - sprite_w))) { |
| 63 |
velocity->x = -velocity->x; |
| 64 |
position->x += velocity->x; |
| 65 |
} |
| 66 |
position->y += velocity->y; |
| 67 |
if ((position->y < 0) || (position->y >= (window_h - sprite_h))) { |
| 68 |
velocity->y = -velocity->y; |
| 69 |
position->y += velocity->y; |
| 70 |
} |
| 71 |
|
| 72 |
/* Blit the sprite onto the screen */ |
| 73 |
SDL_RenderCopy(renderer, sprite, NULL, position); |
| 74 |
} |
| 75 |
|
| 76 |
/* Update the screen! */ |
| 77 |
SDL_RenderPresent(renderer); |
| 78 |
} |
| 79 |
|
| 80 |
void loop(void) |
| 81 |
{ |
| 82 |
SDL_Event event; |
| 83 |
|
| 84 |
/* Check for events */ |
| 85 |
while (SDL_PollEvent(&event)) { |
| 86 |
if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) { |
| 87 |
done = 1; |
| 88 |
} |
| 89 |
} |
| 90 |
MoveSprites(); |
| 91 |
#ifdef __EMSCRIPTEN__ |
| 92 |
if (done) { |
| 93 |
emscripten_cancel_main_loop(); |
| 94 |
} |
| 95 |
#endif |
| 96 |
} |
| 97 |
|
| 98 |
int main(int argc, char *argv[]) |
| 99 |
{ |
| 100 |
SDL_Window *window; |
| 101 |
int i; |
| 102 |
|
| 103 |
/* Enable standard application logging */ |
| 104 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
| 105 |
|
| 106 |
if (SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer) < 0) { |
| 107 |
quit(2); |
| 108 |
} |
| 109 |
|
| 110 |
sprite = LoadTexture(renderer, "icon.bmp", SDL_TRUE, &sprite_w, &sprite_h); |
| 111 |
|
| 112 |
if (!sprite) { |
| 113 |
quit(2); |
| 114 |
} |
| 115 |
|
| 116 |
/* Initialize the sprite positions */ |
| 117 |
srand((unsigned int)time(NULL)); |
| 118 |
for (i = 0; i < NUM_SPRITES; ++i) { |
| 119 |
positions[i].x = rand() % (WINDOW_WIDTH - sprite_w); |
| 120 |
positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h); |
| 121 |
positions[i].w = sprite_w; |
| 122 |
positions[i].h = sprite_h; |
| 123 |
velocities[i].x = 0; |
| 124 |
velocities[i].y = 0; |
| 125 |
while (!velocities[i].x && !velocities[i].y) { |
| 126 |
velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED; |
| 127 |
velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/* Main render loop */ |
| 132 |
done = 0; |
| 133 |
|
| 134 |
#ifdef __EMSCRIPTEN__ |
| 135 |
emscripten_set_main_loop(loop, 0, 1); |
| 136 |
#else |
| 137 |
while (!done) { |
| 138 |
loop(); |
| 139 |
} |
| 140 |
#endif |
| 141 |
quit(0); |
| 142 |
|
| 143 |
return 0; /* to prevent compiler warning */ |
| 144 |
} |
| 145 |
|
| 146 |
/* vi: set ts=4 sw=4 expandtab: */ |