test/testspriteminimal.c
changeset 3417 64a60c5d502e
child 3536 0267b8b1595c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testspriteminimal.c	Wed Oct 28 06:04:07 2009 +0000
     1.3 @@ -0,0 +1,165 @@
     1.4 +/* Simple program:  Move N sprites around on the screen as fast as possible */
     1.5 +
     1.6 +#include <stdlib.h>
     1.7 +#include <stdio.h>
     1.8 +#include <time.h>
     1.9 +
    1.10 +#include "SDL_events.h"
    1.11 +#include "SDL_video.h"
    1.12 +
    1.13 +#define WINDOW_WIDTH    640
    1.14 +#define WINDOW_HEIGHT   480
    1.15 +#define NUM_SPRITES     100
    1.16 +#define MAX_SPEED       1
    1.17 +
    1.18 +static SDL_TextureID sprite;
    1.19 +static SDL_Rect positions[NUM_SPRITES];
    1.20 +static SDL_Rect velocities[NUM_SPRITES];
    1.21 +static int sprite_w, sprite_h;
    1.22 +
    1.23 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    1.24 +static void
    1.25 +quit(int rc)
    1.26 +{
    1.27 +    exit(rc);
    1.28 +}
    1.29 +
    1.30 +int
    1.31 +LoadSprite(char *file)
    1.32 +{
    1.33 +    SDL_Surface *temp;
    1.34 +
    1.35 +    /* Load the sprite image */
    1.36 +    temp = SDL_LoadBMP(file);
    1.37 +    if (temp == NULL) {
    1.38 +        fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
    1.39 +        return (-1);
    1.40 +    }
    1.41 +    sprite_w = temp->w;
    1.42 +    sprite_h = temp->h;
    1.43 +
    1.44 +    /* Set transparent pixel as the pixel at (0,0) */
    1.45 +    if (temp->format->palette) {
    1.46 +        SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
    1.47 +    } else {
    1.48 +        switch (temp->format->BitsPerPixel) {
    1.49 +        case 15:
    1.50 +            SDL_SetColorKey(temp, SDL_TRUE,
    1.51 +                            (*(Uint16 *) temp->pixels) & 0x00007FFF);
    1.52 +            break;
    1.53 +        case 16:
    1.54 +            SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
    1.55 +            break;
    1.56 +        case 24:
    1.57 +            SDL_SetColorKey(temp, SDL_TRUE,
    1.58 +                            (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
    1.59 +            break;
    1.60 +        case 32:
    1.61 +            SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
    1.62 +            break;
    1.63 +        }
    1.64 +    }
    1.65 +
    1.66 +    /* Create textures from the image */
    1.67 +    sprite = SDL_CreateTextureFromSurface(0, temp);
    1.68 +    if (!sprite) {
    1.69 +        SDL_SetColorKey(temp, 0, 0);
    1.70 +        sprite = SDL_CreateTextureFromSurface(0, temp);
    1.71 +    }
    1.72 +    if (!sprite) {
    1.73 +        fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
    1.74 +        SDL_FreeSurface(temp);
    1.75 +        return (-1);
    1.76 +    }
    1.77 +    SDL_FreeSurface(temp);
    1.78 +
    1.79 +    /* We're ready to roll. :) */
    1.80 +    return (0);
    1.81 +}
    1.82 +
    1.83 +void
    1.84 +MoveSprites(SDL_WindowID window, SDL_TextureID sprite)
    1.85 +{
    1.86 +    int i;
    1.87 +    int window_w = WINDOW_WIDTH;
    1.88 +    int window_h = WINDOW_HEIGHT;
    1.89 +    SDL_Rect *position, *velocity;
    1.90 +
    1.91 +    /* Draw a gray background */
    1.92 +    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
    1.93 +    SDL_RenderFill(NULL);
    1.94 +
    1.95 +    /* Move the sprite, bounce at the wall, and draw */
    1.96 +    for (i = 0; i < NUM_SPRITES; ++i) {
    1.97 +        position = &positions[i];
    1.98 +        velocity = &velocities[i];
    1.99 +        position->x += velocity->x;
   1.100 +        if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
   1.101 +            velocity->x = -velocity->x;
   1.102 +            position->x += velocity->x;
   1.103 +        }
   1.104 +        position->y += velocity->y;
   1.105 +        if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
   1.106 +            velocity->y = -velocity->y;
   1.107 +            position->y += velocity->y;
   1.108 +        }
   1.109 +
   1.110 +        /* Blit the sprite onto the screen */
   1.111 +        SDL_RenderCopy(sprite, NULL, position);
   1.112 +    }
   1.113 +
   1.114 +    /* Update the screen! */
   1.115 +    SDL_RenderPresent();
   1.116 +}
   1.117 +
   1.118 +int
   1.119 +main(int argc, char *argv[])
   1.120 +{
   1.121 +    SDL_WindowID window;
   1.122 +    int i, done;
   1.123 +    SDL_Event event;
   1.124 +
   1.125 +    window = SDL_CreateWindow("Happy Smileys",
   1.126 +                              SDL_WINDOWPOS_UNDEFINED,
   1.127 +                              SDL_WINDOWPOS_UNDEFINED,
   1.128 +                              WINDOW_WIDTH, WINDOW_HEIGHT,
   1.129 +                              SDL_WINDOW_SHOWN);
   1.130 +    if (!window) {
   1.131 +        quit(2);
   1.132 +    }
   1.133 +
   1.134 +    if (LoadSprite("icon.bmp") < 0) {
   1.135 +        quit(2);
   1.136 +    }
   1.137 +
   1.138 +    /* Initialize the sprite positions */
   1.139 +    srand(time(NULL));
   1.140 +    for (i = 0; i < NUM_SPRITES; ++i) {
   1.141 +        positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
   1.142 +        positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
   1.143 +        positions[i].w = sprite_w;
   1.144 +        positions[i].h = sprite_h;
   1.145 +        velocities[i].x = 0;
   1.146 +        velocities[i].y = 0;
   1.147 +        while (!velocities[i].x && !velocities[i].y) {
   1.148 +            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   1.149 +            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   1.150 +        }
   1.151 +    }
   1.152 +
   1.153 +    /* Main render loop */
   1.154 +    done = 0;
   1.155 +    while (!done) {
   1.156 +        /* Check for events */
   1.157 +        while (SDL_PollEvent(&event)) {
   1.158 +            if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
   1.159 +                done = 1;
   1.160 +            }
   1.161 +        }
   1.162 +        MoveSprites(window, sprite);
   1.163 +    }
   1.164 +
   1.165 +    quit(0);
   1.166 +}
   1.167 +
   1.168 +/* vi: set ts=4 sw=4 expandtab: */