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