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