test/testsprite2.c
author Mike Gorchak <lestat@i.com.ua>
Wed, 10 Jun 2009 05:54:19 +0000
changeset 3183 d1436442215f
parent 2884 9dde605c7540
child 3186 51750b7a966f
permissions -rw-r--r--
Support for 15/16/24/32 bpps of icon.bmp has been added, in case if not an original icon.bmp (8bpp with palette) is used for tests.
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@2884
    23
static SDL_BlendMode blendMode = SDL_BLENDMODE_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);
lestat@3183
    61
    } else {
lestat@3183
    62
        switch (temp->format->BitsPerPixel) {
lestat@3183
    63
        case 15:
lestat@3183
    64
            SDL_SetColorKey(temp, SDL_SRCCOLORKEY, (*(Uint16 *) temp->pixels) & 0x00007FFF);
lestat@3183
    65
            break;
lestat@3183
    66
        case 16:
lestat@3183
    67
            SDL_SetColorKey(temp, SDL_SRCCOLORKEY, *(Uint16 *) temp->pixels);
lestat@3183
    68
            break;
lestat@3183
    69
        case 24:
lestat@3183
    70
            SDL_SetColorKey(temp, SDL_SRCCOLORKEY, (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
lestat@3183
    71
            break;
lestat@3183
    72
        case 32:
lestat@3183
    73
            SDL_SetColorKey(temp, SDL_SRCCOLORKEY, *(Uint32 *) temp->pixels);
lestat@3183
    74
            break;
lestat@3183
    75
        }
slouken@1895
    76
    }
slouken@1895
    77
slouken@1895
    78
    /* Create textures from the image */
slouken@1914
    79
    for (i = 0; i < state->num_windows; ++i) {
slouken@1914
    80
        SDL_SelectRenderer(state->windows[i]);
slouken@2222
    81
        sprites[i] = SDL_CreateTextureFromSurface(0, temp);
slouken@1895
    82
        if (!sprites[i]) {
slouken@1895
    83
            fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
slouken@1895
    84
            SDL_FreeSurface(temp);
slouken@1895
    85
            return (-1);
slouken@1895
    86
        }
slouken@1985
    87
        SDL_SetTextureBlendMode(sprites[i], blendMode);
slouken@1985
    88
        SDL_SetTextureScaleMode(sprites[i], scaleMode);
slouken@1895
    89
    }
slouken@1895
    90
    SDL_FreeSurface(temp);
slouken@1895
    91
slouken@1895
    92
    /* We're ready to roll. :) */
slouken@1895
    93
    return (0);
slouken@1895
    94
}
slouken@1895
    95
slouken@1895
    96
void
slouken@1895
    97
MoveSprites(SDL_WindowID window, SDL_TextureID sprite)
slouken@1895
    98
{
slouken@1895
    99
    int i, n;
slouken@1895
   100
    int window_w, window_h;
slouken@2783
   101
    SDL_Rect *position, *velocity;
slouken@1895
   102
slouken@1895
   103
    SDL_SelectRenderer(window);
slouken@1895
   104
slouken@1895
   105
    /* Query the sizes */
slouken@1895
   106
    SDL_GetWindowSize(window, &window_w, &window_h);
slouken@1895
   107
slouken@1985
   108
    /* Cycle the color and alpha, if desired */
slouken@1985
   109
    if (cycle_color) {
slouken@1985
   110
        current_color += cycle_direction;
slouken@1985
   111
        if (current_color < 0) {
slouken@1985
   112
            current_color = 0;
slouken@1985
   113
            cycle_direction = -cycle_direction;
slouken@1985
   114
        }
slouken@1985
   115
        if (current_color > 255) {
slouken@1985
   116
            current_color = 255;
slouken@1985
   117
            cycle_direction = -cycle_direction;
slouken@1985
   118
        }
slouken@1985
   119
        SDL_SetTextureColorMod(sprite, 255, (Uint8) current_color,
slouken@1985
   120
                               (Uint8) current_color);
slouken@1985
   121
    }
slouken@1985
   122
    if (cycle_alpha) {
slouken@1985
   123
        current_alpha += cycle_direction;
slouken@1985
   124
        if (current_alpha < 0) {
slouken@1985
   125
            current_alpha = 0;
slouken@1985
   126
            cycle_direction = -cycle_direction;
slouken@1985
   127
        }
slouken@1985
   128
        if (current_alpha > 255) {
slouken@1985
   129
            current_alpha = 255;
slouken@1985
   130
            cycle_direction = -cycle_direction;
slouken@1985
   131
        }
slouken@1985
   132
        SDL_SetTextureAlphaMod(sprite, (Uint8) current_alpha);
slouken@1985
   133
    }
slouken@1985
   134
slouken@1895
   135
    /* Move the sprite, bounce at the wall, and draw */
slouken@1895
   136
    n = 0;
slouken@2884
   137
    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
slouken@2884
   138
    SDL_RenderFill(NULL);
slouken@1895
   139
    for (i = 0; i < num_sprites; ++i) {
slouken@1895
   140
        position = &positions[i];
slouken@1895
   141
        velocity = &velocities[i];
slouken@1895
   142
        position->x += velocity->x;
slouken@1895
   143
        if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
slouken@1895
   144
            velocity->x = -velocity->x;
slouken@1895
   145
            position->x += velocity->x;
slouken@1895
   146
        }
slouken@1895
   147
        position->y += velocity->y;
slouken@2834
   148
        if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
slouken@1895
   149
            velocity->y = -velocity->y;
slouken@1895
   150
            position->y += velocity->y;
slouken@1895
   151
        }
slouken@1895
   152
slouken@1895
   153
        /* Blit the sprite onto the screen */
slouken@1985
   154
        SDL_RenderCopy(sprite, NULL, position);
slouken@1895
   155
    }
slouken@1895
   156
slouken@1895
   157
    /* Update the screen! */
slouken@1895
   158
    SDL_RenderPresent();
slouken@1895
   159
}
slouken@1895
   160
slouken@1895
   161
int
slouken@1895
   162
main(int argc, char *argv[])
slouken@1895
   163
{
slouken@1895
   164
    int i, done;
slouken@1895
   165
    SDL_Event event;
slouken@1895
   166
    Uint32 then, now, frames;
slouken@1895
   167
slouken@1914
   168
    /* Initialize parameters */
slouken@1914
   169
    num_sprites = NUM_SPRITES;
slouken@1914
   170
slouken@1914
   171
    /* Initialize test framework */
slouken@1914
   172
    state = CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@1914
   173
    if (!state) {
slouken@1914
   174
        return 1;
slouken@1895
   175
    }
slouken@1914
   176
    for (i = 1; i < argc;) {
slouken@1914
   177
        int consumed;
slouken@1895
   178
slouken@1914
   179
        consumed = CommonArg(state, i);
slouken@1915
   180
        if (consumed == 0) {
slouken@1916
   181
            consumed = -1;
slouken@1916
   182
            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
slouken@1916
   183
                if (argv[i + 1]) {
slouken@1916
   184
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
slouken@2884
   185
                        blendMode = SDL_BLENDMODE_NONE;
slouken@1916
   186
                        consumed = 2;
slouken@1917
   187
                    } else if (SDL_strcasecmp(argv[i + 1], "mask") == 0) {
slouken@2884
   188
                        blendMode = SDL_BLENDMODE_MASK;
slouken@1917
   189
                        consumed = 2;
slouken@1916
   190
                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
slouken@2884
   191
                        blendMode = SDL_BLENDMODE_BLEND;
slouken@1916
   192
                        consumed = 2;
slouken@1916
   193
                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
slouken@2884
   194
                        blendMode = SDL_BLENDMODE_ADD;
slouken@1916
   195
                        consumed = 2;
slouken@1916
   196
                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
slouken@2884
   197
                        blendMode = SDL_BLENDMODE_MOD;
slouken@1916
   198
                        consumed = 2;
slouken@1916
   199
                    }
slouken@1916
   200
                }
slouken@1917
   201
            } else if (SDL_strcasecmp(argv[i], "--scale") == 0) {
slouken@1917
   202
                if (argv[i + 1]) {
slouken@1917
   203
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
slouken@1965
   204
                        scaleMode = SDL_TEXTURESCALEMODE_NONE;
slouken@1917
   205
                        consumed = 2;
slouken@1917
   206
                    } else if (SDL_strcasecmp(argv[i + 1], "fast") == 0) {
slouken@1965
   207
                        scaleMode = SDL_TEXTURESCALEMODE_FAST;
slouken@1917
   208
                        consumed = 2;
slouken@1917
   209
                    } else if (SDL_strcasecmp(argv[i + 1], "slow") == 0) {
slouken@1965
   210
                        scaleMode = SDL_TEXTURESCALEMODE_SLOW;
slouken@1917
   211
                        consumed = 2;
slouken@1917
   212
                    } else if (SDL_strcasecmp(argv[i + 1], "best") == 0) {
slouken@1965
   213
                        scaleMode = SDL_TEXTURESCALEMODE_BEST;
slouken@1917
   214
                        consumed = 2;
slouken@1917
   215
                    }
slouken@1917
   216
                }
slouken@1985
   217
            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
slouken@1985
   218
                cycle_color = SDL_TRUE;
slouken@1985
   219
                consumed = 1;
slouken@1985
   220
            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
slouken@1985
   221
                cycle_alpha = SDL_TRUE;
slouken@1985
   222
                consumed = 1;
slouken@1916
   223
            } else if (SDL_isdigit(*argv[i])) {
slouken@1916
   224
                num_sprites = SDL_atoi(argv[i]);
slouken@1916
   225
                consumed = 1;
slouken@1916
   226
            }
slouken@1915
   227
        }
slouken@1914
   228
        if (consumed < 0) {
slouken@1917
   229
            fprintf(stderr,
slouken@1985
   230
                    "Usage: %s %s [--blend none|mask|blend|add|mod] [--scale none|fast|slow|best] [--cyclecolor] [--cyclealpha]\n",
slouken@1916
   231
                    argv[0], CommonUsage(state));
slouken@1895
   232
            quit(1);
slouken@1895
   233
        }
slouken@1914
   234
        i += consumed;
slouken@1895
   235
    }
slouken@1914
   236
    if (!CommonInit(state)) {
slouken@1914
   237
        quit(2);
slouken@1895
   238
    }
slouken@1895
   239
slouken@1895
   240
    /* Create the windows, initialize the renderers, and load the textures */
slouken@1914
   241
    sprites =
slouken@1914
   242
        (SDL_TextureID *) SDL_malloc(state->num_windows * sizeof(*sprites));
slouken@1914
   243
    if (!sprites) {
slouken@1895
   244
        fprintf(stderr, "Out of memory!\n");
slouken@1895
   245
        quit(2);
slouken@1895
   246
    }
slouken@1914
   247
    for (i = 0; i < state->num_windows; ++i) {
slouken@1914
   248
        SDL_SelectRenderer(state->windows[i]);
slouken@2884
   249
        SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
slouken@2884
   250
        SDL_RenderFill(NULL);
slouken@1895
   251
    }
slouken@1895
   252
    if (LoadSprite("icon.bmp") < 0) {
slouken@1895
   253
        quit(2);
slouken@1895
   254
    }
slouken@1895
   255
slouken@1895
   256
    /* Allocate memory for the sprite info */
slouken@1895
   257
    positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
slouken@1895
   258
    velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
slouken@1895
   259
    if (!positions || !velocities) {
slouken@1895
   260
        fprintf(stderr, "Out of memory!\n");
slouken@1895
   261
        quit(2);
slouken@1895
   262
    }
slouken@1895
   263
    srand(time(NULL));
slouken@1965
   264
    if (scaleMode != SDL_TEXTURESCALEMODE_NONE) {
slouken@1917
   265
        sprite_w += sprite_w / 2;
slouken@1917
   266
        sprite_h += sprite_h / 2;
slouken@1917
   267
    }
slouken@1895
   268
    for (i = 0; i < num_sprites; ++i) {
slouken@1914
   269
        positions[i].x = rand() % (state->window_w - sprite_w);
slouken@1914
   270
        positions[i].y = rand() % (state->window_h - sprite_h);
slouken@1895
   271
        positions[i].w = sprite_w;
slouken@1895
   272
        positions[i].h = sprite_h;
slouken@1895
   273
        velocities[i].x = 0;
slouken@1895
   274
        velocities[i].y = 0;
slouken@1895
   275
        while (!velocities[i].x && !velocities[i].y) {
slouken@1895
   276
            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@1895
   277
            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@1895
   278
        }
slouken@1895
   279
    }
slouken@1895
   280
slouken@1915
   281
    /* Main render loop */
slouken@1895
   282
    frames = 0;
slouken@1895
   283
    then = SDL_GetTicks();
slouken@1895
   284
    done = 0;
slouken@1895
   285
    while (!done) {
slouken@1895
   286
        /* Check for events */
slouken@1895
   287
        ++frames;
slouken@1895
   288
        while (SDL_PollEvent(&event)) {
slouken@1914
   289
            CommonEvent(state, &event, &done);
slouken@1895
   290
            switch (event.type) {
slouken@1895
   291
            case SDL_WINDOWEVENT:
slouken@1895
   292
                switch (event.window.event) {
slouken@1895
   293
                case SDL_WINDOWEVENT_EXPOSED:
slouken@1895
   294
                    SDL_SelectRenderer(event.window.windowID);
slouken@2884
   295
                    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
slouken@2884
   296
                    SDL_RenderFill(NULL);
slouken@1895
   297
                    break;
slouken@1895
   298
                }
slouken@1895
   299
                break;
slouken@1895
   300
            default:
slouken@1895
   301
                break;
slouken@1895
   302
            }
slouken@1895
   303
        }
slouken@1914
   304
        for (i = 0; i < state->num_windows; ++i) {
slouken@1914
   305
            MoveSprites(state->windows[i], sprites[i]);
slouken@1895
   306
        }
slouken@1895
   307
    }
slouken@1895
   308
slouken@1895
   309
    /* Print out some timing information */
slouken@1895
   310
    now = SDL_GetTicks();
slouken@1895
   311
    if (now > then) {
slouken@2786
   312
        double fps = ((double) frames * 1000) / (now - then);
slouken@2783
   313
        printf("%2.2f frames per second\n", fps);
slouken@1895
   314
    }
slouken@1895
   315
    quit(0);
slouken@1895
   316
}
slouken@1895
   317
slouken@1895
   318
/* vi: set ts=4 sw=4 expandtab: */