test/testsprite2.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 15 Jul 2006 09:46:36 +0000
changeset 1907 06c27a737b7a
parent 1904 1a713f9d1f71
child 1914 051df511279c
permissions -rw-r--r--
Streamlined the API a bit and optimized the software renderer.
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@1895
     7
#include "SDL.h"
slouken@1895
     8
slouken@1904
     9
#define NUM_WINDOWS 4
slouken@1895
    10
#define WINDOW_W    640
slouken@1895
    11
#define WINDOW_H    480
slouken@1895
    12
#define NUM_SPRITES	100
slouken@1895
    13
#define MAX_SPEED 	1
slouken@1895
    14
#define BACKGROUND  0x00FFFFFF
slouken@1895
    15
slouken@1895
    16
static int num_windows;
slouken@1895
    17
static int num_sprites;
slouken@1895
    18
static SDL_WindowID *windows;
slouken@1895
    19
static SDL_TextureID *sprites;
slouken@1895
    20
static SDL_Rect *positions;
slouken@1895
    21
static SDL_Rect *velocities;
slouken@1895
    22
static int sprite_w, sprite_h;
slouken@1895
    23
slouken@1895
    24
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1895
    25
static void
slouken@1895
    26
quit(int rc)
slouken@1895
    27
{
slouken@1895
    28
    if (windows) {
slouken@1895
    29
        SDL_free(windows);
slouken@1895
    30
    }
slouken@1895
    31
    if (sprites) {
slouken@1895
    32
        SDL_free(sprites);
slouken@1895
    33
    }
slouken@1895
    34
    if (positions) {
slouken@1895
    35
        SDL_free(positions);
slouken@1895
    36
    }
slouken@1895
    37
    if (velocities) {
slouken@1895
    38
        SDL_free(velocities);
slouken@1895
    39
    }
slouken@1895
    40
    SDL_Quit();
slouken@1895
    41
    exit(rc);
slouken@1895
    42
}
slouken@1895
    43
slouken@1895
    44
int
slouken@1895
    45
LoadSprite(char *file)
slouken@1895
    46
{
slouken@1895
    47
    int i;
slouken@1895
    48
    SDL_Surface *temp;
slouken@1895
    49
slouken@1895
    50
    /* Load the sprite image */
slouken@1895
    51
    temp = SDL_LoadBMP(file);
slouken@1895
    52
    if (temp == NULL) {
slouken@1895
    53
        fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
slouken@1895
    54
        return (-1);
slouken@1895
    55
    }
slouken@1895
    56
    sprite_w = temp->w;
slouken@1895
    57
    sprite_h = temp->h;
slouken@1895
    58
slouken@1895
    59
    /* Set transparent pixel as the pixel at (0,0) */
slouken@1895
    60
    if (temp->format->palette) {
slouken@1895
    61
        SDL_SetColorKey(temp, SDL_SRCCOLORKEY, *(Uint8 *) temp->pixels);
slouken@1895
    62
    }
slouken@1895
    63
slouken@1895
    64
    /* Create textures from the image */
slouken@1895
    65
    for (i = 0; i < num_windows; ++i) {
slouken@1895
    66
        SDL_SelectRenderer(windows[i]);
slouken@1895
    67
        sprites[i] =
slouken@1895
    68
            SDL_CreateTextureFromSurface(0, SDL_TextureAccess_Remote, temp);
slouken@1895
    69
        if (!sprites[i]) {
slouken@1895
    70
            fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
slouken@1895
    71
            SDL_FreeSurface(temp);
slouken@1895
    72
            return (-1);
slouken@1895
    73
        }
slouken@1895
    74
    }
slouken@1895
    75
    SDL_FreeSurface(temp);
slouken@1895
    76
slouken@1895
    77
    /* We're ready to roll. :) */
slouken@1895
    78
    return (0);
slouken@1895
    79
}
slouken@1895
    80
slouken@1895
    81
void
slouken@1895
    82
MoveSprites(SDL_WindowID window, SDL_TextureID sprite)
slouken@1895
    83
{
slouken@1895
    84
    int i, n;
slouken@1895
    85
    int window_w, window_h;
slouken@1895
    86
    SDL_Rect area, *position, *velocity;
slouken@1895
    87
slouken@1895
    88
    SDL_SelectRenderer(window);
slouken@1895
    89
slouken@1895
    90
    /* Query the sizes */
slouken@1895
    91
    SDL_GetWindowSize(window, &window_w, &window_h);
slouken@1895
    92
slouken@1895
    93
    /* Move the sprite, bounce at the wall, and draw */
slouken@1895
    94
    n = 0;
slouken@1903
    95
    SDL_RenderFill(NULL, BACKGROUND);
slouken@1903
    96
    /*
slouken@1903
    97
       for (i = 0; i < num_sprites; ++i) {
slouken@1903
    98
       position = &positions[i];
slouken@1903
    99
       SDL_RenderFill(position, BACKGROUND);
slouken@1903
   100
       }
slouken@1903
   101
     */
slouken@1895
   102
    for (i = 0; i < num_sprites; ++i) {
slouken@1895
   103
        position = &positions[i];
slouken@1895
   104
        velocity = &velocities[i];
slouken@1895
   105
        position->x += velocity->x;
slouken@1895
   106
        if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
slouken@1895
   107
            velocity->x = -velocity->x;
slouken@1895
   108
            position->x += velocity->x;
slouken@1895
   109
        }
slouken@1895
   110
        position->y += velocity->y;
slouken@1895
   111
        if ((position->y < 0) || (position->y >= (window_h - sprite_w))) {
slouken@1895
   112
            velocity->y = -velocity->y;
slouken@1895
   113
            position->y += velocity->y;
slouken@1895
   114
        }
slouken@1895
   115
slouken@1895
   116
        /* Blit the sprite onto the screen */
slouken@1895
   117
        SDL_RenderCopy(sprite, NULL, position, SDL_TextureBlendMode_Mask,
slouken@1895
   118
                       SDL_TextureScaleMode_None);
slouken@1895
   119
    }
slouken@1895
   120
slouken@1895
   121
    /* Update the screen! */
slouken@1895
   122
    SDL_RenderPresent();
slouken@1895
   123
}
slouken@1895
   124
slouken@1895
   125
int
slouken@1895
   126
main(int argc, char *argv[])
slouken@1895
   127
{
slouken@1895
   128
    int window_w, window_h;
slouken@1895
   129
    Uint32 window_flags = SDL_WINDOW_SHOWN;
slouken@1907
   130
    Uint32 render_flags = 0;
slouken@1895
   131
    SDL_DisplayMode *mode, fullscreen_mode;
slouken@1895
   132
    int i, done;
slouken@1895
   133
    SDL_Event event;
slouken@1895
   134
    Uint32 then, now, frames;
slouken@1895
   135
slouken@1895
   136
    /* Initialize SDL */
slouken@1895
   137
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@1895
   138
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
   139
        return (1);
slouken@1895
   140
    }
slouken@1895
   141
slouken@1895
   142
    num_windows = NUM_WINDOWS;
slouken@1895
   143
    num_sprites = NUM_SPRITES;
slouken@1895
   144
    window_w = WINDOW_W;
slouken@1895
   145
    window_h = WINDOW_H;
slouken@1904
   146
    for (i = 1; i < argc; ++i) {
slouken@1904
   147
        if (strcmp(argv[i], "-width") == 0 && (i + 1 < argc)) {
slouken@1904
   148
            window_w = atoi(argv[++i]);
slouken@1904
   149
        } else if (strcmp(argv[i], "-height") == 0 && (i + 1 < argc)) {
slouken@1904
   150
            window_h = atoi(argv[++i]);
slouken@1904
   151
        } else if (strcmp(argv[i], "-windows") == 0 && (i + 1 < argc)) {
slouken@1904
   152
            num_windows = atoi(argv[++i]);
slouken@1904
   153
            window_flags &= ~SDL_WINDOW_FULLSCREEN;
slouken@1904
   154
        } else if (strcmp(argv[i], "-fullscreen") == 0) {
slouken@1895
   155
            num_windows = 1;
slouken@1895
   156
            window_flags |= SDL_WINDOW_FULLSCREEN;
slouken@1907
   157
        } else if (strcmp(argv[i], "-sync") == 0) {
slouken@1907
   158
            render_flags |= SDL_Renderer_PresentVSync;
slouken@1904
   159
        } else if (isdigit(argv[i][0])) {
slouken@1904
   160
            num_sprites = atoi(argv[i]);
slouken@1895
   161
        } else {
slouken@1895
   162
            fprintf(stderr,
slouken@1907
   163
                    "Usage: %s [-width N] [-height N] [-windows N] [-fullscreen] [-sync] [numsprites]\n",
slouken@1904
   164
                    argv[0]);
slouken@1895
   165
            quit(1);
slouken@1895
   166
        }
slouken@1895
   167
    }
slouken@1895
   168
slouken@1895
   169
    if (window_flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   170
        SDL_zero(fullscreen_mode);
slouken@1895
   171
        fullscreen_mode.w = window_w;
slouken@1895
   172
        fullscreen_mode.h = window_h;
slouken@1895
   173
        SDL_SetFullscreenDisplayMode(&fullscreen_mode);
slouken@1895
   174
    }
slouken@1895
   175
slouken@1895
   176
    /* Create the windows, initialize the renderers, and load the textures */
slouken@1895
   177
    windows = (SDL_WindowID *) SDL_malloc(num_windows * sizeof(*windows));
slouken@1895
   178
    sprites = (SDL_TextureID *) SDL_malloc(num_windows * sizeof(*sprites));
slouken@1895
   179
    if (!windows || !sprites) {
slouken@1895
   180
        fprintf(stderr, "Out of memory!\n");
slouken@1895
   181
        quit(2);
slouken@1895
   182
    }
slouken@1895
   183
    for (i = 0; i < num_windows; ++i) {
slouken@1895
   184
        char title[32];
slouken@1895
   185
slouken@1895
   186
        SDL_snprintf(title, sizeof(title), "testsprite %d", i + 1);
slouken@1895
   187
        windows[i] =
slouken@1895
   188
            SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED,
slouken@1895
   189
                             SDL_WINDOWPOS_UNDEFINED, window_w, window_h,
slouken@1895
   190
                             window_flags);
slouken@1895
   191
        if (!windows[i]) {
slouken@1895
   192
            fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
slouken@1895
   193
            quit(2);
slouken@1895
   194
        }
slouken@1895
   195
slouken@1907
   196
        if (SDL_CreateRenderer(windows[i], -1, render_flags) < 0) {
slouken@1895
   197
            fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
slouken@1895
   198
            quit(2);
slouken@1895
   199
        }
slouken@1895
   200
        SDL_RenderFill(NULL, BACKGROUND);
slouken@1895
   201
    }
slouken@1895
   202
    if (LoadSprite("icon.bmp") < 0) {
slouken@1895
   203
        quit(2);
slouken@1895
   204
    }
slouken@1895
   205
slouken@1895
   206
    /* Allocate memory for the sprite info */
slouken@1895
   207
    positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
slouken@1895
   208
    velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
slouken@1895
   209
    if (!positions || !velocities) {
slouken@1895
   210
        fprintf(stderr, "Out of memory!\n");
slouken@1895
   211
        quit(2);
slouken@1895
   212
    }
slouken@1895
   213
    srand(time(NULL));
slouken@1895
   214
    for (i = 0; i < num_sprites; ++i) {
slouken@1895
   215
        positions[i].x = rand() % (window_w - sprite_w);
slouken@1895
   216
        positions[i].y = rand() % (window_h - sprite_h);
slouken@1895
   217
        positions[i].w = sprite_w;
slouken@1895
   218
        positions[i].h = sprite_h;
slouken@1895
   219
        velocities[i].x = 0;
slouken@1895
   220
        velocities[i].y = 0;
slouken@1895
   221
        while (!velocities[i].x && !velocities[i].y) {
slouken@1895
   222
            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@1895
   223
            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@1895
   224
        }
slouken@1895
   225
    }
slouken@1895
   226
slouken@1895
   227
    /* Loop, blitting sprites and waiting for a keystroke */
slouken@1895
   228
    frames = 0;
slouken@1895
   229
    then = SDL_GetTicks();
slouken@1895
   230
    done = 0;
slouken@1895
   231
    while (!done) {
slouken@1895
   232
        /* Check for events */
slouken@1895
   233
        ++frames;
slouken@1895
   234
        while (SDL_PollEvent(&event)) {
slouken@1895
   235
            switch (event.type) {
slouken@1895
   236
            case SDL_WINDOWEVENT:
slouken@1895
   237
                switch (event.window.event) {
slouken@1895
   238
                case SDL_WINDOWEVENT_EXPOSED:
slouken@1895
   239
                    SDL_SelectRenderer(event.window.windowID);
slouken@1895
   240
                    SDL_RenderFill(NULL, BACKGROUND);
slouken@1895
   241
                    break;
slouken@1895
   242
                case SDL_WINDOWEVENT_CLOSE:
slouken@1895
   243
                    done = 1;
slouken@1895
   244
                    break;
slouken@1895
   245
                }
slouken@1895
   246
                break;
slouken@1895
   247
            case SDL_KEYDOWN:
slouken@1904
   248
                /* Any keypress quits the app... */
slouken@1895
   249
            case SDL_QUIT:
slouken@1895
   250
                done = 1;
slouken@1895
   251
                break;
slouken@1895
   252
            default:
slouken@1895
   253
                break;
slouken@1895
   254
            }
slouken@1895
   255
        }
slouken@1895
   256
        for (i = 0; i < num_windows; ++i) {
slouken@1895
   257
            MoveSprites(windows[i], sprites[i]);
slouken@1895
   258
        }
slouken@1895
   259
    }
slouken@1895
   260
slouken@1895
   261
    /* Print out some timing information */
slouken@1895
   262
    now = SDL_GetTicks();
slouken@1895
   263
    if (now > then) {
slouken@1895
   264
        printf("%2.2f frames per second\n",
slouken@1895
   265
               ((double) frames * 1000) / (now - then));
slouken@1895
   266
    }
slouken@1895
   267
    quit(0);
slouken@1895
   268
}
slouken@1895
   269
slouken@1895
   270
/* vi: set ts=4 sw=4 expandtab: */