test/testspriteminimal.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 28 Oct 2009 06:04:07 +0000
changeset 3417 64a60c5d502e
child 3536 0267b8b1595c
permissions -rw-r--r--
Automatically initialize the video system and create a renderer to simplify use.
slouken@3417
     1
/* Simple program:  Move N sprites around on the screen as fast as possible */
slouken@3417
     2
slouken@3417
     3
#include <stdlib.h>
slouken@3417
     4
#include <stdio.h>
slouken@3417
     5
#include <time.h>
slouken@3417
     6
slouken@3417
     7
#include "SDL_events.h"
slouken@3417
     8
#include "SDL_video.h"
slouken@3417
     9
slouken@3417
    10
#define WINDOW_WIDTH    640
slouken@3417
    11
#define WINDOW_HEIGHT   480
slouken@3417
    12
#define NUM_SPRITES     100
slouken@3417
    13
#define MAX_SPEED       1
slouken@3417
    14
slouken@3417
    15
static SDL_TextureID sprite;
slouken@3417
    16
static SDL_Rect positions[NUM_SPRITES];
slouken@3417
    17
static SDL_Rect velocities[NUM_SPRITES];
slouken@3417
    18
static int sprite_w, sprite_h;
slouken@3417
    19
slouken@3417
    20
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@3417
    21
static void
slouken@3417
    22
quit(int rc)
slouken@3417
    23
{
slouken@3417
    24
    exit(rc);
slouken@3417
    25
}
slouken@3417
    26
slouken@3417
    27
int
slouken@3417
    28
LoadSprite(char *file)
slouken@3417
    29
{
slouken@3417
    30
    SDL_Surface *temp;
slouken@3417
    31
slouken@3417
    32
    /* Load the sprite image */
slouken@3417
    33
    temp = SDL_LoadBMP(file);
slouken@3417
    34
    if (temp == NULL) {
slouken@3417
    35
        fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
slouken@3417
    36
        return (-1);
slouken@3417
    37
    }
slouken@3417
    38
    sprite_w = temp->w;
slouken@3417
    39
    sprite_h = temp->h;
slouken@3417
    40
slouken@3417
    41
    /* Set transparent pixel as the pixel at (0,0) */
slouken@3417
    42
    if (temp->format->palette) {
slouken@3417
    43
        SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
slouken@3417
    44
    } else {
slouken@3417
    45
        switch (temp->format->BitsPerPixel) {
slouken@3417
    46
        case 15:
slouken@3417
    47
            SDL_SetColorKey(temp, SDL_TRUE,
slouken@3417
    48
                            (*(Uint16 *) temp->pixels) & 0x00007FFF);
slouken@3417
    49
            break;
slouken@3417
    50
        case 16:
slouken@3417
    51
            SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
slouken@3417
    52
            break;
slouken@3417
    53
        case 24:
slouken@3417
    54
            SDL_SetColorKey(temp, SDL_TRUE,
slouken@3417
    55
                            (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
slouken@3417
    56
            break;
slouken@3417
    57
        case 32:
slouken@3417
    58
            SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
slouken@3417
    59
            break;
slouken@3417
    60
        }
slouken@3417
    61
    }
slouken@3417
    62
slouken@3417
    63
    /* Create textures from the image */
slouken@3417
    64
    sprite = SDL_CreateTextureFromSurface(0, temp);
slouken@3417
    65
    if (!sprite) {
slouken@3417
    66
        SDL_SetColorKey(temp, 0, 0);
slouken@3417
    67
        sprite = SDL_CreateTextureFromSurface(0, temp);
slouken@3417
    68
    }
slouken@3417
    69
    if (!sprite) {
slouken@3417
    70
        fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
slouken@3417
    71
        SDL_FreeSurface(temp);
slouken@3417
    72
        return (-1);
slouken@3417
    73
    }
slouken@3417
    74
    SDL_FreeSurface(temp);
slouken@3417
    75
slouken@3417
    76
    /* We're ready to roll. :) */
slouken@3417
    77
    return (0);
slouken@3417
    78
}
slouken@3417
    79
slouken@3417
    80
void
slouken@3417
    81
MoveSprites(SDL_WindowID window, SDL_TextureID sprite)
slouken@3417
    82
{
slouken@3417
    83
    int i;
slouken@3417
    84
    int window_w = WINDOW_WIDTH;
slouken@3417
    85
    int window_h = WINDOW_HEIGHT;
slouken@3417
    86
    SDL_Rect *position, *velocity;
slouken@3417
    87
slouken@3417
    88
    /* Draw a gray background */
slouken@3417
    89
    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
slouken@3417
    90
    SDL_RenderFill(NULL);
slouken@3417
    91
slouken@3417
    92
    /* Move the sprite, bounce at the wall, and draw */
slouken@3417
    93
    for (i = 0; i < NUM_SPRITES; ++i) {
slouken@3417
    94
        position = &positions[i];
slouken@3417
    95
        velocity = &velocities[i];
slouken@3417
    96
        position->x += velocity->x;
slouken@3417
    97
        if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
slouken@3417
    98
            velocity->x = -velocity->x;
slouken@3417
    99
            position->x += velocity->x;
slouken@3417
   100
        }
slouken@3417
   101
        position->y += velocity->y;
slouken@3417
   102
        if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
slouken@3417
   103
            velocity->y = -velocity->y;
slouken@3417
   104
            position->y += velocity->y;
slouken@3417
   105
        }
slouken@3417
   106
slouken@3417
   107
        /* Blit the sprite onto the screen */
slouken@3417
   108
        SDL_RenderCopy(sprite, NULL, position);
slouken@3417
   109
    }
slouken@3417
   110
slouken@3417
   111
    /* Update the screen! */
slouken@3417
   112
    SDL_RenderPresent();
slouken@3417
   113
}
slouken@3417
   114
slouken@3417
   115
int
slouken@3417
   116
main(int argc, char *argv[])
slouken@3417
   117
{
slouken@3417
   118
    SDL_WindowID window;
slouken@3417
   119
    int i, done;
slouken@3417
   120
    SDL_Event event;
slouken@3417
   121
slouken@3417
   122
    window = SDL_CreateWindow("Happy Smileys",
slouken@3417
   123
                              SDL_WINDOWPOS_UNDEFINED,
slouken@3417
   124
                              SDL_WINDOWPOS_UNDEFINED,
slouken@3417
   125
                              WINDOW_WIDTH, WINDOW_HEIGHT,
slouken@3417
   126
                              SDL_WINDOW_SHOWN);
slouken@3417
   127
    if (!window) {
slouken@3417
   128
        quit(2);
slouken@3417
   129
    }
slouken@3417
   130
slouken@3417
   131
    if (LoadSprite("icon.bmp") < 0) {
slouken@3417
   132
        quit(2);
slouken@3417
   133
    }
slouken@3417
   134
slouken@3417
   135
    /* Initialize the sprite positions */
slouken@3417
   136
    srand(time(NULL));
slouken@3417
   137
    for (i = 0; i < NUM_SPRITES; ++i) {
slouken@3417
   138
        positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
slouken@3417
   139
        positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
slouken@3417
   140
        positions[i].w = sprite_w;
slouken@3417
   141
        positions[i].h = sprite_h;
slouken@3417
   142
        velocities[i].x = 0;
slouken@3417
   143
        velocities[i].y = 0;
slouken@3417
   144
        while (!velocities[i].x && !velocities[i].y) {
slouken@3417
   145
            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@3417
   146
            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@3417
   147
        }
slouken@3417
   148
    }
slouken@3417
   149
slouken@3417
   150
    /* Main render loop */
slouken@3417
   151
    done = 0;
slouken@3417
   152
    while (!done) {
slouken@3417
   153
        /* Check for events */
slouken@3417
   154
        while (SDL_PollEvent(&event)) {
slouken@3417
   155
            if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
slouken@3417
   156
                done = 1;
slouken@3417
   157
            }
slouken@3417
   158
        }
slouken@3417
   159
        MoveSprites(window, sprite);
slouken@3417
   160
    }
slouken@3417
   161
slouken@3417
   162
    quit(0);
slouken@3417
   163
}
slouken@3417
   164
slouken@3417
   165
/* vi: set ts=4 sw=4 expandtab: */