2 Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
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.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
12 /* Simple program: Move N sprites around on the screen as fast as possible */
19 #include <emscripten/emscripten.h>
24 #define WINDOW_WIDTH 720
25 #define WINDOW_HEIGHT 1280
26 #define NUM_SPRITES 100
29 static SDL_Texture *sprite;
30 static SDL_Rect positions[NUM_SPRITES];
31 static SDL_Rect velocities[NUM_SPRITES];
32 static int sprite_w, sprite_h;
34 SDL_Renderer *renderer;
37 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
45 LoadSprite(char *file, SDL_Renderer *renderer)
49 /* Load the sprite image */
50 temp = SDL_LoadBMP(file);
52 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", file, SDL_GetError());
58 /* Set transparent pixel as the pixel at (0,0) */
59 if (temp->format->palette) {
60 SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
62 switch (temp->format->BitsPerPixel) {
64 SDL_SetColorKey(temp, SDL_TRUE,
65 (*(Uint16 *) temp->pixels) & 0x00007FFF);
68 SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
71 SDL_SetColorKey(temp, SDL_TRUE,
72 (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
75 SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
80 /* Create textures from the image */
81 sprite = SDL_CreateTextureFromSurface(renderer, temp);
83 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
84 SDL_FreeSurface(temp);
87 SDL_FreeSurface(temp);
89 /* We're ready to roll. :) */
94 MoveSprites(SDL_Renderer * renderer, SDL_Texture * sprite)
97 int window_w = WINDOW_WIDTH;
98 int window_h = WINDOW_HEIGHT;
99 SDL_Rect *position, *velocity;
101 /* Draw a gray background */
102 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
103 SDL_RenderClear(renderer);
105 /* Move the sprite, bounce at the wall, and draw */
106 for (i = 0; i < NUM_SPRITES; ++i) {
107 position = &positions[i];
108 velocity = &velocities[i];
109 position->x += velocity->x;
110 if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
111 velocity->x = -velocity->x;
112 position->x += velocity->x;
114 position->y += velocity->y;
115 if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
116 velocity->y = -velocity->y;
117 position->y += velocity->y;
120 /* Blit the sprite onto the screen */
121 SDL_RenderCopy(renderer, sprite, NULL, position);
124 /* Update the screen! */
125 SDL_RenderPresent(renderer);
132 /* Check for events */
133 while (SDL_PollEvent(&event)) {
134 if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
138 MoveSprites(renderer, sprite);
139 #ifdef __EMSCRIPTEN__
141 emscripten_cancel_main_loop();
146 main(int argc, char *argv[])
152 /* Enable standard application logging */
153 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
155 if (SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer) < 0) {
159 if (LoadSprite("res/icon.bmp", renderer) < 0) {
163 /* Initialize the sprite positions */
165 for (i = 0; i < NUM_SPRITES; ++i) {
166 positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
167 positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
168 positions[i].w = sprite_w;
169 positions[i].h = sprite_h;
172 while (!velocities[i].x && !velocities[i].y) {
173 velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
174 velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
178 /* Main render loop */
181 #ifdef __EMSCRIPTEN__
182 emscripten_set_main_loop(loop, 0, 1);
190 return 0; /* to prevent compiler warning */
193 /* vi: set ts=4 sw=4 expandtab: */