test/testspriteminimal.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5517 aa339c0dca2d
child 6259 4aef5b792087
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
     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", 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     window = SDL_CreateWindow("Happy Smileys",
   135                               SDL_WINDOWPOS_UNDEFINED,
   136                               SDL_WINDOWPOS_UNDEFINED,
   137                               WINDOW_WIDTH, WINDOW_HEIGHT,
   138                               SDL_WINDOW_SHOWN);
   139     if (!window) {
   140         quit(2);
   141     }
   142 
   143     renderer = SDL_CreateRenderer(window, -1, 0);
   144     if (!renderer) {
   145         quit(2);
   146     }
   147 
   148     if (LoadSprite("icon.bmp", renderer) < 0) {
   149         quit(2);
   150     }
   151 
   152     /* Initialize the sprite positions */
   153     srand(time(NULL));
   154     for (i = 0; i < NUM_SPRITES; ++i) {
   155         positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
   156         positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
   157         positions[i].w = sprite_w;
   158         positions[i].h = sprite_h;
   159         velocities[i].x = 0;
   160         velocities[i].y = 0;
   161         while (!velocities[i].x && !velocities[i].y) {
   162             velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   163             velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   164         }
   165     }
   166 
   167     /* Main render loop */
   168     done = 0;
   169     while (!done) {
   170         /* Check for events */
   171         while (SDL_PollEvent(&event)) {
   172             if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
   173                 done = 1;
   174             }
   175         }
   176         MoveSprites(window, renderer, sprite);
   177     }
   178 
   179     quit(0);
   180 }
   181 
   182 /* vi: set ts=4 sw=4 expandtab: */