test/testspriteminimal.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 26 Mar 2011 21:28:17 -0700
changeset 5517 aa339c0dca2d
parent 5158 b3ccd1947786
child 5535 96594ac5fd1a
permissions -rw-r--r--
NDS update

Frank Zago to SDL

There's also a patch to adapt one test for the nds, and that adds several
makefiles to compile these tests. Whenever you apply it, could you completely
remove the test/nds-test-progs/sprite and test/nds-test-progs/sprite2
directories ?
     1 /* Simple program:  Move N sprites around on the screen as fast as possible */
     2 
     3 #include <stdlib.h>
     4 #include <stdio.h>
     5 #include <time.h>
     6 
     7 #include "SDL.h"
     8 
     9 #ifdef __NDS__
    10 #define WINDOW_WIDTH    256
    11 #define WINDOW_HEIGHT   (2*192)
    12 #else
    13 #define WINDOW_WIDTH    640
    14 #define WINDOW_HEIGHT   480
    15 #endif
    16 #define NUM_SPRITES     100
    17 #define MAX_SPEED       1
    18 
    19 static SDL_Texture *sprite;
    20 static SDL_Rect positions[NUM_SPRITES];
    21 static SDL_Rect velocities[NUM_SPRITES];
    22 static int sprite_w, sprite_h;
    23 
    24 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    25 static void
    26 quit(int rc)
    27 {
    28     exit(rc);
    29 }
    30 
    31 int
    32 LoadSprite(char *file, SDL_Renderer *renderer)
    33 {
    34     SDL_Surface *temp;
    35 
    36     /* Load the sprite image */
    37     temp = SDL_LoadBMP(file);
    38     if (temp == NULL) {
    39         fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
    40         return (-1);
    41     }
    42     sprite_w = temp->w;
    43     sprite_h = temp->h;
    44 
    45     /* Set transparent pixel as the pixel at (0,0) */
    46     if (temp->format->palette) {
    47         SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
    48     } else {
    49         switch (temp->format->BitsPerPixel) {
    50         case 15:
    51             SDL_SetColorKey(temp, SDL_TRUE,
    52                             (*(Uint16 *) temp->pixels) & 0x00007FFF);
    53             break;
    54         case 16:
    55             SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
    56             break;
    57         case 24:
    58             SDL_SetColorKey(temp, SDL_TRUE,
    59                             (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
    60             break;
    61         case 32:
    62             SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
    63             break;
    64         }
    65     }
    66 
    67     /* Create textures from the image */
    68     sprite = SDL_CreateTextureFromSurface(renderer, temp);
    69     if (!sprite) {
    70         fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
    71         SDL_FreeSurface(temp);
    72         return (-1);
    73     }
    74     SDL_FreeSurface(temp);
    75 
    76     /* We're ready to roll. :) */
    77     return (0);
    78 }
    79 
    80 void
    81 MoveSprites(SDL_Window * window, SDL_Renderer * renderer, SDL_Texture * sprite)
    82 {
    83     int i;
    84     int window_w = WINDOW_WIDTH;
    85     int window_h = WINDOW_HEIGHT;
    86     SDL_Rect *position, *velocity;
    87 
    88     /* Draw a gray background */
    89     SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
    90     SDL_RenderClear(renderer);
    91 
    92     /* Move the sprite, bounce at the wall, and draw */
    93     for (i = 0; i < NUM_SPRITES; ++i) {
    94         position = &positions[i];
    95         velocity = &velocities[i];
    96         position->x += velocity->x;
    97         if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
    98             velocity->x = -velocity->x;
    99             position->x += velocity->x;
   100         }
   101         position->y += velocity->y;
   102         if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
   103             velocity->y = -velocity->y;
   104             position->y += velocity->y;
   105         }
   106 
   107         /* Blit the sprite onto the screen */
   108         SDL_RenderCopy(renderer, sprite, NULL, position);
   109     }
   110 
   111     /* Update the screen! */
   112     SDL_RenderPresent(renderer);
   113 }
   114 
   115 int
   116 main(int argc, char *argv[])
   117 {
   118     SDL_Window *window;
   119     SDL_Renderer *renderer;
   120     int i, done;
   121     SDL_Event event;
   122 
   123     window = SDL_CreateWindow("Happy Smileys",
   124                               SDL_WINDOWPOS_UNDEFINED,
   125                               SDL_WINDOWPOS_UNDEFINED,
   126                               WINDOW_WIDTH, WINDOW_HEIGHT,
   127                               SDL_WINDOW_SHOWN);
   128     if (!window) {
   129         quit(2);
   130     }
   131 
   132     renderer = SDL_CreateRenderer(window, -1, 0);
   133     if (!renderer) {
   134         quit(2);
   135     }
   136 
   137     if (LoadSprite("icon.bmp", renderer) < 0) {
   138         quit(2);
   139     }
   140 
   141     /* Initialize the sprite positions */
   142     srand(time(NULL));
   143     for (i = 0; i < NUM_SPRITES; ++i) {
   144         positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
   145         positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
   146         positions[i].w = sprite_w;
   147         positions[i].h = sprite_h;
   148         velocities[i].x = 0;
   149         velocities[i].y = 0;
   150         while (!velocities[i].x && !velocities[i].y) {
   151             velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   152             velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   153         }
   154     }
   155 
   156     /* Main render loop */
   157     done = 0;
   158     while (!done) {
   159         /* Check for events */
   160         while (SDL_PollEvent(&event)) {
   161             if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
   162                 done = 1;
   163             }
   164         }
   165         MoveSprites(window, renderer, sprite);
   166     }
   167 
   168     quit(0);
   169 }
   170 
   171 /* vi: set ts=4 sw=4 expandtab: */