test/testsprite2.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 25 Nov 2008 00:29:44 +0000
changeset 2783 e33ad7ebb7eb
parent 2222 926294b2bb4e
child 2786 6bacfecbf27e
permissions -rw-r--r--
Fixed Direct3D rendering
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
slouken@1914
    12
static CommonState *state;
slouken@1895
    13
static int num_sprites;
slouken@1895
    14
static SDL_TextureID *sprites;
slouken@1985
    15
static SDL_bool cycle_color;
slouken@1985
    16
static SDL_bool cycle_alpha;
slouken@1985
    17
static int cycle_direction = 1;
slouken@1985
    18
static int current_alpha = 0;
slouken@1985
    19
static int current_color = 0;
slouken@1895
    20
static SDL_Rect *positions;
slouken@1895
    21
static SDL_Rect *velocities;
slouken@1895
    22
static int sprite_w, sprite_h;
slouken@1965
    23
static SDL_TextureBlendMode blendMode = SDL_TEXTUREBLENDMODE_MASK;
slouken@1965
    24
static SDL_TextureScaleMode scaleMode = SDL_TEXTURESCALEMODE_NONE;
slouken@1895
    25
slouken@1895
    26
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1895
    27
static void
slouken@1895
    28
quit(int rc)
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@1914
    39
    CommonQuit(state);
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@1914
    64
    for (i = 0; i < state->num_windows; ++i) {
slouken@1914
    65
        SDL_SelectRenderer(state->windows[i]);
slouken@2222
    66
        sprites[i] = SDL_CreateTextureFromSurface(0, temp);
slouken@1895
    67
        if (!sprites[i]) {
slouken@1895
    68
            fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
slouken@1895
    69
            SDL_FreeSurface(temp);
slouken@1895
    70
            return (-1);
slouken@1895
    71
        }
slouken@1985
    72
        SDL_SetTextureBlendMode(sprites[i], blendMode);
slouken@1985
    73
        SDL_SetTextureScaleMode(sprites[i], scaleMode);
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@2783
    86
    SDL_Rect *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@1985
    93
    /* Cycle the color and alpha, if desired */
slouken@1985
    94
    if (cycle_color) {
slouken@1985
    95
        current_color += cycle_direction;
slouken@1985
    96
        if (current_color < 0) {
slouken@1985
    97
            current_color = 0;
slouken@1985
    98
            cycle_direction = -cycle_direction;
slouken@1985
    99
        }
slouken@1985
   100
        if (current_color > 255) {
slouken@1985
   101
            current_color = 255;
slouken@1985
   102
            cycle_direction = -cycle_direction;
slouken@1985
   103
        }
slouken@1985
   104
        SDL_SetTextureColorMod(sprite, 255, (Uint8) current_color,
slouken@1985
   105
                               (Uint8) current_color);
slouken@1985
   106
    }
slouken@1985
   107
    if (cycle_alpha) {
slouken@1985
   108
        current_alpha += cycle_direction;
slouken@1985
   109
        if (current_alpha < 0) {
slouken@1985
   110
            current_alpha = 0;
slouken@1985
   111
            cycle_direction = -cycle_direction;
slouken@1985
   112
        }
slouken@1985
   113
        if (current_alpha > 255) {
slouken@1985
   114
            current_alpha = 255;
slouken@1985
   115
            cycle_direction = -cycle_direction;
slouken@1985
   116
        }
slouken@1985
   117
        SDL_SetTextureAlphaMod(sprite, (Uint8) current_alpha);
slouken@1985
   118
    }
slouken@1985
   119
slouken@1895
   120
    /* Move the sprite, bounce at the wall, and draw */
slouken@1895
   121
    n = 0;
slouken@1985
   122
    SDL_RenderFill(0xA0, 0xA0, 0xA0, 0xFF, NULL);
slouken@1895
   123
    for (i = 0; i < num_sprites; ++i) {
slouken@1895
   124
        position = &positions[i];
slouken@1895
   125
        velocity = &velocities[i];
slouken@1895
   126
        position->x += velocity->x;
slouken@1895
   127
        if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
slouken@1895
   128
            velocity->x = -velocity->x;
slouken@1895
   129
            position->x += velocity->x;
slouken@1895
   130
        }
slouken@1895
   131
        position->y += velocity->y;
slouken@1895
   132
        if ((position->y < 0) || (position->y >= (window_h - sprite_w))) {
slouken@1895
   133
            velocity->y = -velocity->y;
slouken@1895
   134
            position->y += velocity->y;
slouken@1895
   135
        }
slouken@1895
   136
slouken@1895
   137
        /* Blit the sprite onto the screen */
slouken@1985
   138
        SDL_RenderCopy(sprite, NULL, position);
slouken@1895
   139
    }
slouken@1895
   140
slouken@1895
   141
    /* Update the screen! */
slouken@1895
   142
    SDL_RenderPresent();
slouken@1895
   143
}
slouken@1895
   144
slouken@1895
   145
int
slouken@1895
   146
main(int argc, char *argv[])
slouken@1895
   147
{
slouken@1895
   148
    int i, done;
slouken@1895
   149
    SDL_Event event;
slouken@1895
   150
    Uint32 then, now, frames;
slouken@1895
   151
slouken@1914
   152
    /* Initialize parameters */
slouken@1914
   153
    num_sprites = NUM_SPRITES;
slouken@1914
   154
slouken@1914
   155
    /* Initialize test framework */
slouken@1914
   156
    state = CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@1914
   157
    if (!state) {
slouken@1914
   158
        return 1;
slouken@1895
   159
    }
slouken@1914
   160
    for (i = 1; i < argc;) {
slouken@1914
   161
        int consumed;
slouken@1895
   162
slouken@1914
   163
        consumed = CommonArg(state, i);
slouken@1915
   164
        if (consumed == 0) {
slouken@1916
   165
            consumed = -1;
slouken@1916
   166
            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
slouken@1916
   167
                if (argv[i + 1]) {
slouken@1916
   168
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
slouken@1965
   169
                        blendMode = SDL_TEXTUREBLENDMODE_NONE;
slouken@1916
   170
                        consumed = 2;
slouken@1917
   171
                    } else if (SDL_strcasecmp(argv[i + 1], "mask") == 0) {
slouken@1965
   172
                        blendMode = SDL_TEXTUREBLENDMODE_MASK;
slouken@1917
   173
                        consumed = 2;
slouken@1916
   174
                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
slouken@1965
   175
                        blendMode = SDL_TEXTUREBLENDMODE_BLEND;
slouken@1916
   176
                        consumed = 2;
slouken@1916
   177
                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
slouken@1965
   178
                        blendMode = SDL_TEXTUREBLENDMODE_ADD;
slouken@1916
   179
                        consumed = 2;
slouken@1916
   180
                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
slouken@1965
   181
                        blendMode = SDL_TEXTUREBLENDMODE_MOD;
slouken@1916
   182
                        consumed = 2;
slouken@1916
   183
                    }
slouken@1916
   184
                }
slouken@1917
   185
            } else if (SDL_strcasecmp(argv[i], "--scale") == 0) {
slouken@1917
   186
                if (argv[i + 1]) {
slouken@1917
   187
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
slouken@1965
   188
                        scaleMode = SDL_TEXTURESCALEMODE_NONE;
slouken@1917
   189
                        consumed = 2;
slouken@1917
   190
                    } else if (SDL_strcasecmp(argv[i + 1], "fast") == 0) {
slouken@1965
   191
                        scaleMode = SDL_TEXTURESCALEMODE_FAST;
slouken@1917
   192
                        consumed = 2;
slouken@1917
   193
                    } else if (SDL_strcasecmp(argv[i + 1], "slow") == 0) {
slouken@1965
   194
                        scaleMode = SDL_TEXTURESCALEMODE_SLOW;
slouken@1917
   195
                        consumed = 2;
slouken@1917
   196
                    } else if (SDL_strcasecmp(argv[i + 1], "best") == 0) {
slouken@1965
   197
                        scaleMode = SDL_TEXTURESCALEMODE_BEST;
slouken@1917
   198
                        consumed = 2;
slouken@1917
   199
                    }
slouken@1917
   200
                }
slouken@1985
   201
            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
slouken@1985
   202
                cycle_color = SDL_TRUE;
slouken@1985
   203
                consumed = 1;
slouken@1985
   204
            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
slouken@1985
   205
                cycle_alpha = SDL_TRUE;
slouken@1985
   206
                consumed = 1;
slouken@1916
   207
            } else if (SDL_isdigit(*argv[i])) {
slouken@1916
   208
                num_sprites = SDL_atoi(argv[i]);
slouken@1916
   209
                consumed = 1;
slouken@1916
   210
            }
slouken@1915
   211
        }
slouken@1914
   212
        if (consumed < 0) {
slouken@1917
   213
            fprintf(stderr,
slouken@1985
   214
                    "Usage: %s %s [--blend none|mask|blend|add|mod] [--scale none|fast|slow|best] [--cyclecolor] [--cyclealpha]\n",
slouken@1916
   215
                    argv[0], CommonUsage(state));
slouken@1895
   216
            quit(1);
slouken@1895
   217
        }
slouken@1914
   218
        i += consumed;
slouken@1895
   219
    }
slouken@1914
   220
    if (!CommonInit(state)) {
slouken@1914
   221
        quit(2);
slouken@1895
   222
    }
slouken@1895
   223
slouken@1895
   224
    /* Create the windows, initialize the renderers, and load the textures */
slouken@1914
   225
    sprites =
slouken@1914
   226
        (SDL_TextureID *) SDL_malloc(state->num_windows * sizeof(*sprites));
slouken@1914
   227
    if (!sprites) {
slouken@1895
   228
        fprintf(stderr, "Out of memory!\n");
slouken@1895
   229
        quit(2);
slouken@1895
   230
    }
slouken@1914
   231
    for (i = 0; i < state->num_windows; ++i) {
slouken@1914
   232
        SDL_SelectRenderer(state->windows[i]);
slouken@1985
   233
        SDL_RenderFill(0xA0, 0xA0, 0xA0, 0xFF, NULL);
slouken@1895
   234
    }
slouken@1895
   235
    if (LoadSprite("icon.bmp") < 0) {
slouken@1895
   236
        quit(2);
slouken@1895
   237
    }
slouken@1895
   238
slouken@1895
   239
    /* Allocate memory for the sprite info */
slouken@1895
   240
    positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
slouken@1895
   241
    velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
slouken@1895
   242
    if (!positions || !velocities) {
slouken@1895
   243
        fprintf(stderr, "Out of memory!\n");
slouken@1895
   244
        quit(2);
slouken@1895
   245
    }
slouken@1895
   246
    srand(time(NULL));
slouken@1965
   247
    if (scaleMode != SDL_TEXTURESCALEMODE_NONE) {
slouken@1917
   248
        sprite_w += sprite_w / 2;
slouken@1917
   249
        sprite_h += sprite_h / 2;
slouken@1917
   250
    }
slouken@1895
   251
    for (i = 0; i < num_sprites; ++i) {
slouken@1914
   252
        positions[i].x = rand() % (state->window_w - sprite_w);
slouken@1914
   253
        positions[i].y = rand() % (state->window_h - sprite_h);
slouken@1895
   254
        positions[i].w = sprite_w;
slouken@1895
   255
        positions[i].h = sprite_h;
slouken@1895
   256
        velocities[i].x = 0;
slouken@1895
   257
        velocities[i].y = 0;
slouken@1895
   258
        while (!velocities[i].x && !velocities[i].y) {
slouken@1895
   259
            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@1895
   260
            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@1895
   261
        }
slouken@1895
   262
    }
slouken@1895
   263
slouken@1915
   264
    /* Main render loop */
slouken@1895
   265
    frames = 0;
slouken@1895
   266
    then = SDL_GetTicks();
slouken@1895
   267
    done = 0;
slouken@1895
   268
    while (!done) {
slouken@1895
   269
        /* Check for events */
slouken@1895
   270
        ++frames;
slouken@1895
   271
        while (SDL_PollEvent(&event)) {
slouken@1914
   272
            CommonEvent(state, &event, &done);
slouken@1895
   273
            switch (event.type) {
slouken@1895
   274
            case SDL_WINDOWEVENT:
slouken@1895
   275
                switch (event.window.event) {
slouken@1895
   276
                case SDL_WINDOWEVENT_EXPOSED:
slouken@1895
   277
                    SDL_SelectRenderer(event.window.windowID);
slouken@1985
   278
                    SDL_RenderFill(0xA0, 0xA0, 0xA0, 0xFF, NULL);
slouken@1895
   279
                    break;
slouken@1895
   280
                }
slouken@1895
   281
                break;
slouken@1895
   282
            default:
slouken@1895
   283
                break;
slouken@1895
   284
            }
slouken@1895
   285
        }
slouken@1914
   286
        for (i = 0; i < state->num_windows; ++i) {
slouken@1914
   287
            MoveSprites(state->windows[i], sprites[i]);
slouken@1895
   288
        }
slouken@1895
   289
    }
slouken@1895
   290
slouken@1895
   291
    /* Print out some timing information */
slouken@1895
   292
    now = SDL_GetTicks();
slouken@1895
   293
    if (now > then) {
slouken@2783
   294
		double fps = ((double) frames * 1000) / (now - then);
slouken@2783
   295
        printf("%2.2f frames per second\n", fps);
slouken@1895
   296
    }
slouken@1895
   297
    quit(0);
slouken@1895
   298
}
slouken@1895
   299
slouken@1895
   300
/* vi: set ts=4 sw=4 expandtab: */