test/testspriteminimal.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 03 Feb 2011 00:54:29 -0800
changeset 5158 b3ccd1947786
parent 5147 ad50b3db78bd
child 5517 aa339c0dca2d
permissions -rw-r--r--
Simplified and improved the process of creating a texture from a surface.
     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, SDL_Renderer *renderer)
    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(renderer, temp);
    64     if (!sprite) {
    65         fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
    66         SDL_FreeSurface(temp);
    67         return (-1);
    68     }
    69     SDL_FreeSurface(temp);
    70 
    71     /* We're ready to roll. :) */
    72     return (0);
    73 }
    74 
    75 void
    76 MoveSprites(SDL_Window * window, SDL_Renderer * renderer, SDL_Texture * sprite)
    77 {
    78     int i;
    79     int window_w = WINDOW_WIDTH;
    80     int window_h = WINDOW_HEIGHT;
    81     SDL_Rect *position, *velocity;
    82 
    83     /* Draw a gray background */
    84     SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
    85     SDL_RenderClear(renderer);
    86 
    87     /* Move the sprite, bounce at the wall, and draw */
    88     for (i = 0; i < NUM_SPRITES; ++i) {
    89         position = &positions[i];
    90         velocity = &velocities[i];
    91         position->x += velocity->x;
    92         if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
    93             velocity->x = -velocity->x;
    94             position->x += velocity->x;
    95         }
    96         position->y += velocity->y;
    97         if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
    98             velocity->y = -velocity->y;
    99             position->y += velocity->y;
   100         }
   101 
   102         /* Blit the sprite onto the screen */
   103         SDL_RenderCopy(renderer, sprite, NULL, position);
   104     }
   105 
   106     /* Update the screen! */
   107     SDL_RenderPresent(renderer);
   108 }
   109 
   110 int
   111 main(int argc, char *argv[])
   112 {
   113     SDL_Window *window;
   114     SDL_Renderer *renderer;
   115     int i, done;
   116     SDL_Event event;
   117 
   118     window = SDL_CreateWindow("Happy Smileys",
   119                               SDL_WINDOWPOS_UNDEFINED,
   120                               SDL_WINDOWPOS_UNDEFINED,
   121                               WINDOW_WIDTH, WINDOW_HEIGHT,
   122                               SDL_WINDOW_SHOWN);
   123     if (!window) {
   124         quit(2);
   125     }
   126 
   127     renderer = SDL_CreateRenderer(window, -1, 0);
   128     if (!renderer) {
   129         quit(2);
   130     }
   131 
   132     if (LoadSprite("icon.bmp", renderer) < 0) {
   133         quit(2);
   134     }
   135 
   136     /* Initialize the sprite positions */
   137     srand(time(NULL));
   138     for (i = 0; i < NUM_SPRITES; ++i) {
   139         positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
   140         positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
   141         positions[i].w = sprite_w;
   142         positions[i].h = sprite_h;
   143         velocities[i].x = 0;
   144         velocities[i].y = 0;
   145         while (!velocities[i].x && !velocities[i].y) {
   146             velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   147             velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   148         }
   149     }
   150 
   151     /* Main render loop */
   152     done = 0;
   153     while (!done) {
   154         /* Check for events */
   155         while (SDL_PollEvent(&event)) {
   156             if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
   157                 done = 1;
   158             }
   159         }
   160         MoveSprites(window, renderer, sprite);
   161     }
   162 
   163     quit(0);
   164 }
   165 
   166 /* vi: set ts=4 sw=4 expandtab: */