test/testspriteminimal.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 21 Jan 2010 06:21:52 +0000
changeset 3685 64ce267332c6
parent 3596 f638ded38b8a
child 4969 d2247eb39fab
permissions -rw-r--r--
Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
     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_events.h"
     8 #include "SDL_video.h"
     9 
    10 #define WINDOW_WIDTH    640
    11 #define WINDOW_HEIGHT   480
    12 #define NUM_SPRITES     100
    13 #define MAX_SPEED       1
    14 
    15 static SDL_Texture *sprite;
    16 static SDL_Rect positions[NUM_SPRITES];
    17 static SDL_Rect velocities[NUM_SPRITES];
    18 static int sprite_w, sprite_h;
    19 
    20 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    21 static void
    22 quit(int rc)
    23 {
    24     exit(rc);
    25 }
    26 
    27 int
    28 LoadSprite(char *file)
    29 {
    30     SDL_Surface *temp;
    31 
    32     /* Load the sprite image */
    33     temp = SDL_LoadBMP(file);
    34     if (temp == NULL) {
    35         fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
    36         return (-1);
    37     }
    38     sprite_w = temp->w;
    39     sprite_h = temp->h;
    40 
    41     /* Set transparent pixel as the pixel at (0,0) */
    42     if (temp->format->palette) {
    43         SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
    44     } else {
    45         switch (temp->format->BitsPerPixel) {
    46         case 15:
    47             SDL_SetColorKey(temp, SDL_TRUE,
    48                             (*(Uint16 *) temp->pixels) & 0x00007FFF);
    49             break;
    50         case 16:
    51             SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
    52             break;
    53         case 24:
    54             SDL_SetColorKey(temp, SDL_TRUE,
    55                             (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
    56             break;
    57         case 32:
    58             SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
    59             break;
    60         }
    61     }
    62 
    63     /* Create textures from the image */
    64     sprite = SDL_CreateTextureFromSurface(0, temp);
    65     if (!sprite) {
    66         SDL_SetColorKey(temp, 0, 0);
    67         sprite = SDL_CreateTextureFromSurface(0, temp);
    68     }
    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_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(0xA0, 0xA0, 0xA0, 0xFF);
    90     SDL_RenderClear();
    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(sprite, NULL, position);
   109     }
   110 
   111     /* Update the screen! */
   112     SDL_RenderPresent();
   113 }
   114 
   115 int
   116 main(int argc, char *argv[])
   117 {
   118     SDL_Window *window;
   119     int i, done;
   120     SDL_Event event;
   121 
   122     window = SDL_CreateWindow("Happy Smileys",
   123                               SDL_WINDOWPOS_UNDEFINED,
   124                               SDL_WINDOWPOS_UNDEFINED,
   125                               WINDOW_WIDTH, WINDOW_HEIGHT,
   126                               SDL_WINDOW_SHOWN);
   127     if (!window) {
   128         quit(2);
   129     }
   130 
   131     if (LoadSprite("icon.bmp") < 0) {
   132         quit(2);
   133     }
   134 
   135     /* Initialize the sprite positions */
   136     srand(time(NULL));
   137     for (i = 0; i < NUM_SPRITES; ++i) {
   138         positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
   139         positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
   140         positions[i].w = sprite_w;
   141         positions[i].h = sprite_h;
   142         velocities[i].x = 0;
   143         velocities[i].y = 0;
   144         while (!velocities[i].x && !velocities[i].y) {
   145             velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   146             velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   147         }
   148     }
   149 
   150     /* Main render loop */
   151     done = 0;
   152     while (!done) {
   153         /* Check for events */
   154         while (SDL_PollEvent(&event)) {
   155             if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
   156                 done = 1;
   157             }
   158         }
   159         MoveSprites(window, sprite);
   160     }
   161 
   162     quit(0);
   163 }
   164 
   165 /* vi: set ts=4 sw=4 expandtab: */