test/testspriteminimal.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Jan 2011 11:22:07 -0800
changeset 4969 d2247eb39fab
parent 3685 64ce267332c6
child 5147 ad50b3db78bd
permissions -rw-r--r--
Need to include SDL_main.h ... eh, just include everything. :)
     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 #define WINDOW_WIDTH    640
    10 #define WINDOW_HEIGHT   480
    11 #define NUM_SPRITES     100
    12 #define MAX_SPEED       1
    13 
    14 static SDL_Texture *sprite;
    15 static SDL_Rect positions[NUM_SPRITES];
    16 static SDL_Rect velocities[NUM_SPRITES];
    17 static int sprite_w, sprite_h;
    18 
    19 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    20 static void
    21 quit(int rc)
    22 {
    23     exit(rc);
    24 }
    25 
    26 int
    27 LoadSprite(char *file)
    28 {
    29     SDL_Surface *temp;
    30 
    31     /* Load the sprite image */
    32     temp = SDL_LoadBMP(file);
    33     if (temp == NULL) {
    34         fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
    35         return (-1);
    36     }
    37     sprite_w = temp->w;
    38     sprite_h = temp->h;
    39 
    40     /* Set transparent pixel as the pixel at (0,0) */
    41     if (temp->format->palette) {
    42         SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
    43     } else {
    44         switch (temp->format->BitsPerPixel) {
    45         case 15:
    46             SDL_SetColorKey(temp, SDL_TRUE,
    47                             (*(Uint16 *) temp->pixels) & 0x00007FFF);
    48             break;
    49         case 16:
    50             SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
    51             break;
    52         case 24:
    53             SDL_SetColorKey(temp, SDL_TRUE,
    54                             (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
    55             break;
    56         case 32:
    57             SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
    58             break;
    59         }
    60     }
    61 
    62     /* Create textures from the image */
    63     sprite = SDL_CreateTextureFromSurface(0, temp);
    64     if (!sprite) {
    65         SDL_SetColorKey(temp, 0, 0);
    66         sprite = SDL_CreateTextureFromSurface(0, temp);
    67     }
    68     if (!sprite) {
    69         fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
    70         SDL_FreeSurface(temp);
    71         return (-1);
    72     }
    73     SDL_FreeSurface(temp);
    74 
    75     /* We're ready to roll. :) */
    76     return (0);
    77 }
    78 
    79 void
    80 MoveSprites(SDL_Window * window, SDL_Texture * sprite)
    81 {
    82     int i;
    83     int window_w = WINDOW_WIDTH;
    84     int window_h = WINDOW_HEIGHT;
    85     SDL_Rect *position, *velocity;
    86 
    87     /* Draw a gray background */
    88     SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
    89     SDL_RenderClear();
    90 
    91     /* Move the sprite, bounce at the wall, and draw */
    92     for (i = 0; i < NUM_SPRITES; ++i) {
    93         position = &positions[i];
    94         velocity = &velocities[i];
    95         position->x += velocity->x;
    96         if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
    97             velocity->x = -velocity->x;
    98             position->x += velocity->x;
    99         }
   100         position->y += velocity->y;
   101         if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
   102             velocity->y = -velocity->y;
   103             position->y += velocity->y;
   104         }
   105 
   106         /* Blit the sprite onto the screen */
   107         SDL_RenderCopy(sprite, NULL, position);
   108     }
   109 
   110     /* Update the screen! */
   111     SDL_RenderPresent();
   112 }
   113 
   114 int
   115 main(int argc, char *argv[])
   116 {
   117     SDL_Window *window;
   118     int i, done;
   119     SDL_Event event;
   120 
   121     window = SDL_CreateWindow("Happy Smileys",
   122                               SDL_WINDOWPOS_UNDEFINED,
   123                               SDL_WINDOWPOS_UNDEFINED,
   124                               WINDOW_WIDTH, WINDOW_HEIGHT,
   125                               SDL_WINDOW_SHOWN);
   126     if (!window) {
   127         quit(2);
   128     }
   129 
   130     if (LoadSprite("icon.bmp") < 0) {
   131         quit(2);
   132     }
   133 
   134     /* Initialize the sprite positions */
   135     srand(time(NULL));
   136     for (i = 0; i < NUM_SPRITES; ++i) {
   137         positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
   138         positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
   139         positions[i].w = sprite_w;
   140         positions[i].h = sprite_h;
   141         velocities[i].x = 0;
   142         velocities[i].y = 0;
   143         while (!velocities[i].x && !velocities[i].y) {
   144             velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   145             velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   146         }
   147     }
   148 
   149     /* Main render loop */
   150     done = 0;
   151     while (!done) {
   152         /* Check for events */
   153         while (SDL_PollEvent(&event)) {
   154             if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
   155                 done = 1;
   156             }
   157         }
   158         MoveSprites(window, sprite);
   159     }
   160 
   161     quit(0);
   162 }
   163 
   164 /* vi: set ts=4 sw=4 expandtab: */