test/testspriteminimal.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 22 Jan 2012 19:47:33 -0500
changeset 6259 4aef5b792087
parent 5535 96594ac5fd1a
child 7011 82bbfbbc720e
permissions -rw-r--r--
Have testspriteminimal use the new simple API
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 /* Simple program:  Move N sprites around on the screen as fast as possible */
    13 
    14 #include <stdlib.h>
    15 #include <stdio.h>
    16 #include <time.h>
    17 
    18 #include "SDL.h"
    19 
    20 #ifdef __NDS__
    21 #define WINDOW_WIDTH    256
    22 #define WINDOW_HEIGHT   (2*192)
    23 #else
    24 #define WINDOW_WIDTH    640
    25 #define WINDOW_HEIGHT   480
    26 #endif
    27 #define NUM_SPRITES     100
    28 #define MAX_SPEED       1
    29 
    30 static SDL_Texture *sprite;
    31 static SDL_Rect positions[NUM_SPRITES];
    32 static SDL_Rect velocities[NUM_SPRITES];
    33 static int sprite_w, sprite_h;
    34 
    35 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    36 static void
    37 quit(int rc)
    38 {
    39     exit(rc);
    40 }
    41 
    42 int
    43 LoadSprite(char *file, SDL_Renderer *renderer)
    44 {
    45     SDL_Surface *temp;
    46 
    47     /* Load the sprite image */
    48     temp = SDL_LoadBMP(file);
    49     if (temp == NULL) {
    50         fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
    51         return (-1);
    52     }
    53     sprite_w = temp->w;
    54     sprite_h = temp->h;
    55 
    56     /* Set transparent pixel as the pixel at (0,0) */
    57     if (temp->format->palette) {
    58         SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
    59     } else {
    60         switch (temp->format->BitsPerPixel) {
    61         case 15:
    62             SDL_SetColorKey(temp, SDL_TRUE,
    63                             (*(Uint16 *) temp->pixels) & 0x00007FFF);
    64             break;
    65         case 16:
    66             SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
    67             break;
    68         case 24:
    69             SDL_SetColorKey(temp, SDL_TRUE,
    70                             (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
    71             break;
    72         case 32:
    73             SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
    74             break;
    75         }
    76     }
    77 
    78     /* Create textures from the image */
    79     sprite = SDL_CreateTextureFromSurface(renderer, temp);
    80     if (!sprite) {
    81         fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
    82         SDL_FreeSurface(temp);
    83         return (-1);
    84     }
    85     SDL_FreeSurface(temp);
    86 
    87     /* We're ready to roll. :) */
    88     return (0);
    89 }
    90 
    91 void
    92 MoveSprites(SDL_Window * window, SDL_Renderer * renderer, SDL_Texture * sprite)
    93 {
    94     int i;
    95     int window_w = WINDOW_WIDTH;
    96     int window_h = WINDOW_HEIGHT;
    97     SDL_Rect *position, *velocity;
    98 
    99     /* Draw a gray background */
   100     SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
   101     SDL_RenderClear(renderer);
   102 
   103     /* Move the sprite, bounce at the wall, and draw */
   104     for (i = 0; i < NUM_SPRITES; ++i) {
   105         position = &positions[i];
   106         velocity = &velocities[i];
   107         position->x += velocity->x;
   108         if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
   109             velocity->x = -velocity->x;
   110             position->x += velocity->x;
   111         }
   112         position->y += velocity->y;
   113         if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
   114             velocity->y = -velocity->y;
   115             position->y += velocity->y;
   116         }
   117 
   118         /* Blit the sprite onto the screen */
   119         SDL_RenderCopy(renderer, sprite, NULL, position);
   120     }
   121 
   122     /* Update the screen! */
   123     SDL_RenderPresent(renderer);
   124 }
   125 
   126 int
   127 main(int argc, char *argv[])
   128 {
   129     SDL_Window *window;
   130     SDL_Renderer *renderer;
   131     int i, done;
   132     SDL_Event event;
   133 
   134     if (SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer) < 0) {
   135         quit(2);
   136     }
   137 
   138     if (LoadSprite("icon.bmp", renderer) < 0) {
   139         quit(2);
   140     }
   141 
   142     /* Initialize the sprite positions */
   143     srand(time(NULL));
   144     for (i = 0; i < NUM_SPRITES; ++i) {
   145         positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
   146         positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
   147         positions[i].w = sprite_w;
   148         positions[i].h = sprite_h;
   149         velocities[i].x = 0;
   150         velocities[i].y = 0;
   151         while (!velocities[i].x && !velocities[i].y) {
   152             velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   153             velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   154         }
   155     }
   156 
   157     /* Main render loop */
   158     done = 0;
   159     while (!done) {
   160         /* Check for events */
   161         while (SDL_PollEvent(&event)) {
   162             if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
   163                 done = 1;
   164             }
   165         }
   166         MoveSprites(window, renderer, sprite);
   167     }
   168 
   169     quit(0);
   170 }
   171 
   172 /* vi: set ts=4 sw=4 expandtab: */