test/testspriteminimal.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 01 Feb 2011 19:19:43 -0800
changeset 5147 ad50b3db78bd
parent 4969 d2247eb39fab
child 5158 b3ccd1947786
permissions -rw-r--r--
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
     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, 0, temp);
    64     if (!sprite) {
    65         SDL_SetColorKey(temp, 0, 0);
    66         sprite = SDL_CreateTextureFromSurface(renderer, 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_Renderer * renderer, 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(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
    89     SDL_RenderClear(renderer);
    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(renderer, sprite, NULL, position);
   108     }
   109 
   110     /* Update the screen! */
   111     SDL_RenderPresent(renderer);
   112 }
   113 
   114 int
   115 main(int argc, char *argv[])
   116 {
   117     SDL_Window *window;
   118     SDL_Renderer *renderer;
   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     renderer = SDL_CreateRenderer(window, -1, 0);
   132     if (!renderer) {
   133         quit(2);
   134     }
   135 
   136     if (LoadSprite("icon.bmp", renderer) < 0) {
   137         quit(2);
   138     }
   139 
   140     /* Initialize the sprite positions */
   141     srand(time(NULL));
   142     for (i = 0; i < NUM_SPRITES; ++i) {
   143         positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
   144         positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
   145         positions[i].w = sprite_w;
   146         positions[i].h = sprite_h;
   147         velocities[i].x = 0;
   148         velocities[i].y = 0;
   149         while (!velocities[i].x && !velocities[i].y) {
   150             velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   151             velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   152         }
   153     }
   154 
   155     /* Main render loop */
   156     done = 0;
   157     while (!done) {
   158         /* Check for events */
   159         while (SDL_PollEvent(&event)) {
   160             if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
   161                 done = 1;
   162             }
   163         }
   164         MoveSprites(window, renderer, sprite);
   165     }
   166 
   167     quit(0);
   168 }
   169 
   170 /* vi: set ts=4 sw=4 expandtab: */