test/testsprite2.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 18 Jul 2006 07:49:51 +0000
changeset 1914 051df511279c
parent 1907 06c27a737b7a
child 1915 a228436a2404
permissions -rw-r--r--
Added a test program framework for easy initialization.
Started work on multi-window OpenGL demo
slouken@1895
     1
/* Simple program:  Move N sprites around on the screen as fast as possible */
slouken@1895
     2
slouken@1895
     3
#include <stdlib.h>
slouken@1907
     4
#include <stdio.h>
slouken@1895
     5
#include <time.h>
slouken@1895
     6
slouken@1914
     7
#include "common.h"
slouken@1895
     8
slouken@1895
     9
#define NUM_SPRITES	100
slouken@1895
    10
#define MAX_SPEED 	1
slouken@1895
    11
#define BACKGROUND  0x00FFFFFF
slouken@1895
    12
slouken@1914
    13
static CommonState *state;
slouken@1895
    14
static int num_sprites;
slouken@1895
    15
static SDL_TextureID *sprites;
slouken@1895
    16
static SDL_Rect *positions;
slouken@1895
    17
static SDL_Rect *velocities;
slouken@1895
    18
static int sprite_w, sprite_h;
slouken@1895
    19
slouken@1895
    20
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1895
    21
static void
slouken@1895
    22
quit(int rc)
slouken@1895
    23
{
slouken@1895
    24
    if (sprites) {
slouken@1895
    25
        SDL_free(sprites);
slouken@1895
    26
    }
slouken@1895
    27
    if (positions) {
slouken@1895
    28
        SDL_free(positions);
slouken@1895
    29
    }
slouken@1895
    30
    if (velocities) {
slouken@1895
    31
        SDL_free(velocities);
slouken@1895
    32
    }
slouken@1914
    33
    CommonQuit(state);
slouken@1895
    34
    exit(rc);
slouken@1895
    35
}
slouken@1895
    36
slouken@1895
    37
int
slouken@1895
    38
LoadSprite(char *file)
slouken@1895
    39
{
slouken@1895
    40
    int i;
slouken@1895
    41
    SDL_Surface *temp;
slouken@1895
    42
slouken@1895
    43
    /* Load the sprite image */
slouken@1895
    44
    temp = SDL_LoadBMP(file);
slouken@1895
    45
    if (temp == NULL) {
slouken@1895
    46
        fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
slouken@1895
    47
        return (-1);
slouken@1895
    48
    }
slouken@1895
    49
    sprite_w = temp->w;
slouken@1895
    50
    sprite_h = temp->h;
slouken@1895
    51
slouken@1895
    52
    /* Set transparent pixel as the pixel at (0,0) */
slouken@1895
    53
    if (temp->format->palette) {
slouken@1895
    54
        SDL_SetColorKey(temp, SDL_SRCCOLORKEY, *(Uint8 *) temp->pixels);
slouken@1895
    55
    }
slouken@1895
    56
slouken@1895
    57
    /* Create textures from the image */
slouken@1914
    58
    for (i = 0; i < state->num_windows; ++i) {
slouken@1914
    59
        SDL_SelectRenderer(state->windows[i]);
slouken@1895
    60
        sprites[i] =
slouken@1895
    61
            SDL_CreateTextureFromSurface(0, SDL_TextureAccess_Remote, temp);
slouken@1895
    62
        if (!sprites[i]) {
slouken@1895
    63
            fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
slouken@1895
    64
            SDL_FreeSurface(temp);
slouken@1895
    65
            return (-1);
slouken@1895
    66
        }
slouken@1895
    67
    }
slouken@1895
    68
    SDL_FreeSurface(temp);
slouken@1895
    69
slouken@1895
    70
    /* We're ready to roll. :) */
slouken@1895
    71
    return (0);
slouken@1895
    72
}
slouken@1895
    73
slouken@1895
    74
void
slouken@1895
    75
MoveSprites(SDL_WindowID window, SDL_TextureID sprite)
slouken@1895
    76
{
slouken@1895
    77
    int i, n;
slouken@1895
    78
    int window_w, window_h;
slouken@1895
    79
    SDL_Rect area, *position, *velocity;
slouken@1895
    80
slouken@1895
    81
    SDL_SelectRenderer(window);
slouken@1895
    82
slouken@1895
    83
    /* Query the sizes */
slouken@1895
    84
    SDL_GetWindowSize(window, &window_w, &window_h);
slouken@1895
    85
slouken@1895
    86
    /* Move the sprite, bounce at the wall, and draw */
slouken@1895
    87
    n = 0;
slouken@1903
    88
    SDL_RenderFill(NULL, BACKGROUND);
slouken@1903
    89
    /*
slouken@1903
    90
       for (i = 0; i < num_sprites; ++i) {
slouken@1903
    91
       position = &positions[i];
slouken@1903
    92
       SDL_RenderFill(position, BACKGROUND);
slouken@1903
    93
       }
slouken@1903
    94
     */
slouken@1895
    95
    for (i = 0; i < num_sprites; ++i) {
slouken@1895
    96
        position = &positions[i];
slouken@1895
    97
        velocity = &velocities[i];
slouken@1895
    98
        position->x += velocity->x;
slouken@1895
    99
        if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
slouken@1895
   100
            velocity->x = -velocity->x;
slouken@1895
   101
            position->x += velocity->x;
slouken@1895
   102
        }
slouken@1895
   103
        position->y += velocity->y;
slouken@1895
   104
        if ((position->y < 0) || (position->y >= (window_h - sprite_w))) {
slouken@1895
   105
            velocity->y = -velocity->y;
slouken@1895
   106
            position->y += velocity->y;
slouken@1895
   107
        }
slouken@1895
   108
slouken@1895
   109
        /* Blit the sprite onto the screen */
slouken@1895
   110
        SDL_RenderCopy(sprite, NULL, position, SDL_TextureBlendMode_Mask,
slouken@1895
   111
                       SDL_TextureScaleMode_None);
slouken@1895
   112
    }
slouken@1895
   113
slouken@1895
   114
    /* Update the screen! */
slouken@1895
   115
    SDL_RenderPresent();
slouken@1895
   116
}
slouken@1895
   117
slouken@1895
   118
int
slouken@1895
   119
main(int argc, char *argv[])
slouken@1895
   120
{
slouken@1895
   121
    int i, done;
slouken@1895
   122
    SDL_Event event;
slouken@1895
   123
    Uint32 then, now, frames;
slouken@1895
   124
slouken@1914
   125
    /* Initialize parameters */
slouken@1914
   126
    num_sprites = NUM_SPRITES;
slouken@1914
   127
slouken@1914
   128
    /* Initialize test framework */
slouken@1914
   129
    state = CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@1914
   130
    if (!state) {
slouken@1914
   131
        return 1;
slouken@1895
   132
    }
slouken@1914
   133
    for (i = 1; i < argc;) {
slouken@1914
   134
        int consumed;
slouken@1895
   135
slouken@1914
   136
        consumed = CommonArg(state, i);
slouken@1914
   137
        if (consumed < 0) {
slouken@1914
   138
            fprintf(stderr, "Usage: %s %s", argv[0], CommonUsage(state));
slouken@1895
   139
            quit(1);
slouken@1895
   140
        }
slouken@1914
   141
        if (consumed == 0) {
slouken@1914
   142
            num_sprites = SDL_atoi(argv[i]);
slouken@1914
   143
            consumed = 1;
slouken@1914
   144
        }
slouken@1914
   145
        i += consumed;
slouken@1895
   146
    }
slouken@1914
   147
    if (!CommonInit(state)) {
slouken@1914
   148
        quit(2);
slouken@1895
   149
    }
slouken@1895
   150
slouken@1895
   151
    /* Create the windows, initialize the renderers, and load the textures */
slouken@1914
   152
    sprites =
slouken@1914
   153
        (SDL_TextureID *) SDL_malloc(state->num_windows * sizeof(*sprites));
slouken@1914
   154
    if (!sprites) {
slouken@1895
   155
        fprintf(stderr, "Out of memory!\n");
slouken@1895
   156
        quit(2);
slouken@1895
   157
    }
slouken@1914
   158
    for (i = 0; i < state->num_windows; ++i) {
slouken@1914
   159
        SDL_SelectRenderer(state->windows[i]);
slouken@1895
   160
        SDL_RenderFill(NULL, BACKGROUND);
slouken@1895
   161
    }
slouken@1895
   162
    if (LoadSprite("icon.bmp") < 0) {
slouken@1895
   163
        quit(2);
slouken@1895
   164
    }
slouken@1895
   165
slouken@1895
   166
    /* Allocate memory for the sprite info */
slouken@1895
   167
    positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
slouken@1895
   168
    velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
slouken@1895
   169
    if (!positions || !velocities) {
slouken@1895
   170
        fprintf(stderr, "Out of memory!\n");
slouken@1895
   171
        quit(2);
slouken@1895
   172
    }
slouken@1895
   173
    srand(time(NULL));
slouken@1895
   174
    for (i = 0; i < num_sprites; ++i) {
slouken@1914
   175
        positions[i].x = rand() % (state->window_w - sprite_w);
slouken@1914
   176
        positions[i].y = rand() % (state->window_h - sprite_h);
slouken@1895
   177
        positions[i].w = sprite_w;
slouken@1895
   178
        positions[i].h = sprite_h;
slouken@1895
   179
        velocities[i].x = 0;
slouken@1895
   180
        velocities[i].y = 0;
slouken@1895
   181
        while (!velocities[i].x && !velocities[i].y) {
slouken@1895
   182
            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@1895
   183
            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@1895
   184
        }
slouken@1895
   185
    }
slouken@1895
   186
slouken@1895
   187
    /* Loop, blitting sprites and waiting for a keystroke */
slouken@1895
   188
    frames = 0;
slouken@1895
   189
    then = SDL_GetTicks();
slouken@1895
   190
    done = 0;
slouken@1895
   191
    while (!done) {
slouken@1895
   192
        /* Check for events */
slouken@1895
   193
        ++frames;
slouken@1895
   194
        while (SDL_PollEvent(&event)) {
slouken@1914
   195
            CommonEvent(state, &event, &done);
slouken@1895
   196
            switch (event.type) {
slouken@1895
   197
            case SDL_WINDOWEVENT:
slouken@1895
   198
                switch (event.window.event) {
slouken@1895
   199
                case SDL_WINDOWEVENT_EXPOSED:
slouken@1895
   200
                    SDL_SelectRenderer(event.window.windowID);
slouken@1895
   201
                    SDL_RenderFill(NULL, BACKGROUND);
slouken@1895
   202
                    break;
slouken@1895
   203
                }
slouken@1895
   204
                break;
slouken@1895
   205
            default:
slouken@1895
   206
                break;
slouken@1895
   207
            }
slouken@1895
   208
        }
slouken@1914
   209
        for (i = 0; i < state->num_windows; ++i) {
slouken@1914
   210
            MoveSprites(state->windows[i], sprites[i]);
slouken@1895
   211
        }
slouken@1895
   212
    }
slouken@1895
   213
slouken@1895
   214
    /* Print out some timing information */
slouken@1895
   215
    now = SDL_GetTicks();
slouken@1895
   216
    if (now > then) {
slouken@1895
   217
        printf("%2.2f frames per second\n",
slouken@1895
   218
               ((double) frames * 1000) / (now - then));
slouken@1895
   219
    }
slouken@1895
   220
    quit(0);
slouken@1895
   221
}
slouken@1895
   222
slouken@1895
   223
/* vi: set ts=4 sw=4 expandtab: */