test/testsprite2.c
author David Ludwig <dludwig@pobox.com>
Tue, 12 Feb 2013 12:57:06 -0500
changeset 8424 3cf9501008f1
parent 5535 96594ac5fd1a
child 6785 c094c438c0a6
permissions -rw-r--r--
WinRT: fixed bug: SDL_CreateWindow wouldn't work after an initial window was created + destroyed
slouken@5535
     1
/*
slouken@5535
     2
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
slouken@1895
    12
/* Simple program:  Move N sprites around on the screen as fast as possible */
slouken@1895
    13
slouken@1895
    14
#include <stdlib.h>
slouken@1907
    15
#include <stdio.h>
slouken@1895
    16
#include <time.h>
slouken@1895
    17
slouken@1914
    18
#include "common.h"
slouken@1895
    19
slouken@5147
    20
#define NUM_SPRITES    100
slouken@5147
    21
#define MAX_SPEED     1
slouken@1895
    22
slouken@1914
    23
static CommonState *state;
slouken@1895
    24
static int num_sprites;
slouken@3685
    25
static SDL_Texture **sprites;
slouken@1985
    26
static SDL_bool cycle_color;
slouken@1985
    27
static SDL_bool cycle_alpha;
slouken@1985
    28
static int cycle_direction = 1;
slouken@1985
    29
static int current_alpha = 0;
slouken@1985
    30
static int current_color = 0;
slouken@1895
    31
static SDL_Rect *positions;
slouken@1895
    32
static SDL_Rect *velocities;
slouken@1895
    33
static int sprite_w, sprite_h;
slouken@5140
    34
static SDL_BlendMode blendMode = SDL_BLENDMODE_BLEND;
slouken@1895
    35
slouken@1895
    36
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1895
    37
static void
slouken@1895
    38
quit(int rc)
slouken@1895
    39
{
slouken@1895
    40
    if (sprites) {
slouken@1895
    41
        SDL_free(sprites);
slouken@1895
    42
    }
slouken@1895
    43
    if (positions) {
slouken@1895
    44
        SDL_free(positions);
slouken@1895
    45
    }
slouken@1895
    46
    if (velocities) {
slouken@1895
    47
        SDL_free(velocities);
slouken@1895
    48
    }
slouken@1914
    49
    CommonQuit(state);
slouken@1895
    50
    exit(rc);
slouken@1895
    51
}
slouken@1895
    52
slouken@1895
    53
int
slouken@1895
    54
LoadSprite(char *file)
slouken@1895
    55
{
slouken@1895
    56
    int i;
slouken@1895
    57
    SDL_Surface *temp;
slouken@1895
    58
slouken@1895
    59
    /* Load the sprite image */
slouken@1895
    60
    temp = SDL_LoadBMP(file);
slouken@1895
    61
    if (temp == NULL) {
slouken@1895
    62
        fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
slouken@1895
    63
        return (-1);
slouken@1895
    64
    }
slouken@1895
    65
    sprite_w = temp->w;
slouken@1895
    66
    sprite_h = temp->h;
slouken@1895
    67
slouken@1895
    68
    /* Set transparent pixel as the pixel at (0,0) */
slouken@1895
    69
    if (temp->format->palette) {
slouken@3560
    70
        SDL_SetColorKey(temp, 1, *(Uint8 *) temp->pixels);
lestat@3183
    71
    } else {
lestat@3183
    72
        switch (temp->format->BitsPerPixel) {
lestat@3183
    73
        case 15:
slouken@3560
    74
            SDL_SetColorKey(temp, 1, (*(Uint16 *) temp->pixels) & 0x00007FFF);
lestat@3183
    75
            break;
lestat@3183
    76
        case 16:
slouken@3560
    77
            SDL_SetColorKey(temp, 1, *(Uint16 *) temp->pixels);
lestat@3183
    78
            break;
lestat@3183
    79
        case 24:
slouken@3560
    80
            SDL_SetColorKey(temp, 1, (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
lestat@3183
    81
            break;
lestat@3183
    82
        case 32:
slouken@3560
    83
            SDL_SetColorKey(temp, 1, *(Uint32 *) temp->pixels);
lestat@3183
    84
            break;
lestat@3183
    85
        }
slouken@1895
    86
    }
slouken@1895
    87
slouken@1895
    88
    /* Create textures from the image */
slouken@1914
    89
    for (i = 0; i < state->num_windows; ++i) {
slouken@5147
    90
        SDL_Renderer *renderer = state->renderers[i];
slouken@5158
    91
        sprites[i] = SDL_CreateTextureFromSurface(renderer, temp);
slouken@3285
    92
        if (!sprites[i]) {
slouken@1895
    93
            fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
slouken@1895
    94
            SDL_FreeSurface(temp);
slouken@1895
    95
            return (-1);
slouken@1895
    96
        }
slouken@1985
    97
        SDL_SetTextureBlendMode(sprites[i], blendMode);
slouken@1895
    98
    }
slouken@1895
    99
    SDL_FreeSurface(temp);
slouken@1895
   100
slouken@1895
   101
    /* We're ready to roll. :) */
slouken@1895
   102
    return (0);
slouken@1895
   103
}
slouken@1895
   104
slouken@1895
   105
void
slouken@5297
   106
MoveSprites(SDL_Renderer * renderer, SDL_Texture * sprite)
slouken@1895
   107
{
slouken@1895
   108
    int i, n;
slouken@5297
   109
    SDL_Rect viewport, temp;
slouken@2783
   110
    SDL_Rect *position, *velocity;
slouken@1895
   111
slouken@1895
   112
    /* Query the sizes */
slouken@5297
   113
    SDL_RenderGetViewport(renderer, &viewport);
slouken@1895
   114
slouken@1985
   115
    /* Cycle the color and alpha, if desired */
slouken@1985
   116
    if (cycle_color) {
slouken@1985
   117
        current_color += cycle_direction;
slouken@1985
   118
        if (current_color < 0) {
slouken@1985
   119
            current_color = 0;
slouken@1985
   120
            cycle_direction = -cycle_direction;
slouken@1985
   121
        }
slouken@1985
   122
        if (current_color > 255) {
slouken@1985
   123
            current_color = 255;
slouken@1985
   124
            cycle_direction = -cycle_direction;
slouken@1985
   125
        }
slouken@1985
   126
        SDL_SetTextureColorMod(sprite, 255, (Uint8) current_color,
slouken@1985
   127
                               (Uint8) current_color);
slouken@1985
   128
    }
slouken@1985
   129
    if (cycle_alpha) {
slouken@1985
   130
        current_alpha += cycle_direction;
slouken@1985
   131
        if (current_alpha < 0) {
slouken@1985
   132
            current_alpha = 0;
slouken@1985
   133
            cycle_direction = -cycle_direction;
slouken@1985
   134
        }
slouken@1985
   135
        if (current_alpha > 255) {
slouken@1985
   136
            current_alpha = 255;
slouken@1985
   137
            cycle_direction = -cycle_direction;
slouken@1985
   138
        }
slouken@1985
   139
        SDL_SetTextureAlphaMod(sprite, (Uint8) current_alpha);
slouken@1985
   140
    }
slouken@1985
   141
slouken@3264
   142
    /* Draw a gray background */
slouken@5147
   143
    SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@5147
   144
    SDL_RenderClear(renderer);
slouken@3264
   145
slouken@3265
   146
    /* Test points */
slouken@5147
   147
    SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
slouken@5147
   148
    SDL_RenderDrawPoint(renderer, 0, 0);
slouken@5297
   149
    SDL_RenderDrawPoint(renderer, viewport.w-1, 0);
slouken@5297
   150
    SDL_RenderDrawPoint(renderer, 0, viewport.h-1);
slouken@5297
   151
    SDL_RenderDrawPoint(renderer, viewport.w-1, viewport.h-1);
slouken@3264
   152
slouken@3265
   153
    /* Test horizontal and vertical lines */
slouken@5147
   154
    SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
slouken@5297
   155
    SDL_RenderDrawLine(renderer, 1, 0, viewport.w-2, 0);
slouken@5297
   156
    SDL_RenderDrawLine(renderer, 1, viewport.h-1, viewport.w-2, viewport.h-1);
slouken@5297
   157
    SDL_RenderDrawLine(renderer, 0, 1, 0, viewport.h-2);
slouken@5297
   158
    SDL_RenderDrawLine(renderer, viewport.w-1, 1, viewport.w-1, viewport.h-2);
slouken@3265
   159
slouken@3266
   160
    /* Test fill and copy */
slouken@5147
   161
    SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
slouken@3266
   162
    temp.x = 1;
slouken@3266
   163
    temp.y = 1;
slouken@3266
   164
    temp.w = sprite_w;
slouken@3266
   165
    temp.h = sprite_h;
slouken@5147
   166
    SDL_RenderFillRect(renderer, &temp);
slouken@5147
   167
    SDL_RenderCopy(renderer, sprite, NULL, &temp);
slouken@5297
   168
    temp.x = viewport.w-sprite_w-1;
slouken@3265
   169
    temp.y = 1;
slouken@3265
   170
    temp.w = sprite_w;
slouken@3265
   171
    temp.h = sprite_h;
slouken@5147
   172
    SDL_RenderFillRect(renderer, &temp);
slouken@5147
   173
    SDL_RenderCopy(renderer, sprite, NULL, &temp);
slouken@3265
   174
    temp.x = 1;
slouken@5297
   175
    temp.y = viewport.h-sprite_h-1;
slouken@3265
   176
    temp.w = sprite_w;
slouken@3265
   177
    temp.h = sprite_h;
slouken@5147
   178
    SDL_RenderFillRect(renderer, &temp);
slouken@5147
   179
    SDL_RenderCopy(renderer, sprite, NULL, &temp);
slouken@5297
   180
    temp.x = viewport.w-sprite_w-1;
slouken@5297
   181
    temp.y = viewport.h-sprite_h-1;
slouken@3265
   182
    temp.w = sprite_w;
slouken@3265
   183
    temp.h = sprite_h;
slouken@5147
   184
    SDL_RenderFillRect(renderer, &temp);
slouken@5147
   185
    SDL_RenderCopy(renderer, sprite, NULL, &temp);
slouken@3265
   186
slouken@3265
   187
    /* Test diagonal lines */
slouken@5147
   188
    SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
slouken@5147
   189
    SDL_RenderDrawLine(renderer, sprite_w, sprite_h,
slouken@5297
   190
                       viewport.w-sprite_w-2, viewport.h-sprite_h-2);
slouken@5297
   191
    SDL_RenderDrawLine(renderer, viewport.w-sprite_w-2, sprite_h,
slouken@5297
   192
                       sprite_w, viewport.h-sprite_h-2);
slouken@3265
   193
slouken@1895
   194
    /* Move the sprite, bounce at the wall, and draw */
slouken@1895
   195
    n = 0;
slouken@1895
   196
    for (i = 0; i < num_sprites; ++i) {
slouken@1895
   197
        position = &positions[i];
slouken@1895
   198
        velocity = &velocities[i];
slouken@1895
   199
        position->x += velocity->x;
slouken@5297
   200
        if ((position->x < 0) || (position->x >= (viewport.w - sprite_w))) {
slouken@1895
   201
            velocity->x = -velocity->x;
slouken@1895
   202
            position->x += velocity->x;
slouken@1895
   203
        }
slouken@1895
   204
        position->y += velocity->y;
slouken@5297
   205
        if ((position->y < 0) || (position->y >= (viewport.h - sprite_h))) {
slouken@1895
   206
            velocity->y = -velocity->y;
slouken@1895
   207
            position->y += velocity->y;
slouken@1895
   208
        }
slouken@1895
   209
slouken@1895
   210
        /* Blit the sprite onto the screen */
slouken@5147
   211
        SDL_RenderCopy(renderer, sprite, NULL, position);
slouken@1895
   212
    }
slouken@1895
   213
slouken@1895
   214
    /* Update the screen! */
slouken@5147
   215
    SDL_RenderPresent(renderer);
slouken@1895
   216
}
slouken@1895
   217
slouken@1895
   218
int
slouken@1895
   219
main(int argc, char *argv[])
slouken@1895
   220
{
slouken@1895
   221
    int i, done;
slouken@1895
   222
    SDL_Event event;
slouken@1895
   223
    Uint32 then, now, frames;
slouken@1895
   224
slouken@1914
   225
    /* Initialize parameters */
slouken@1914
   226
    num_sprites = NUM_SPRITES;
slouken@1914
   227
slouken@1914
   228
    /* Initialize test framework */
slouken@1914
   229
    state = CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@1914
   230
    if (!state) {
slouken@1914
   231
        return 1;
slouken@1895
   232
    }
icculus@5520
   233
icculus@5520
   234
    state->window_flags |= SDL_WINDOW_RESIZABLE;
icculus@5520
   235
slouken@1914
   236
    for (i = 1; i < argc;) {
slouken@1914
   237
        int consumed;
slouken@1895
   238
slouken@1914
   239
        consumed = CommonArg(state, i);
slouken@1915
   240
        if (consumed == 0) {
slouken@1916
   241
            consumed = -1;
slouken@1916
   242
            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
slouken@1916
   243
                if (argv[i + 1]) {
slouken@1916
   244
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
slouken@2884
   245
                        blendMode = SDL_BLENDMODE_NONE;
slouken@1916
   246
                        consumed = 2;
slouken@1916
   247
                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
slouken@2884
   248
                        blendMode = SDL_BLENDMODE_BLEND;
slouken@1916
   249
                        consumed = 2;
slouken@1916
   250
                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
slouken@2884
   251
                        blendMode = SDL_BLENDMODE_ADD;
slouken@1916
   252
                        consumed = 2;
slouken@5184
   253
                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
slouken@5184
   254
                        blendMode = SDL_BLENDMODE_MOD;
slouken@5184
   255
                        consumed = 2;
slouken@1916
   256
                    }
slouken@1916
   257
                }
slouken@1985
   258
            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
slouken@1985
   259
                cycle_color = SDL_TRUE;
slouken@1985
   260
                consumed = 1;
slouken@1985
   261
            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
slouken@1985
   262
                cycle_alpha = SDL_TRUE;
slouken@1985
   263
                consumed = 1;
slouken@1916
   264
            } else if (SDL_isdigit(*argv[i])) {
slouken@1916
   265
                num_sprites = SDL_atoi(argv[i]);
slouken@1916
   266
                consumed = 1;
slouken@1916
   267
            }
slouken@1915
   268
        }
slouken@1914
   269
        if (consumed < 0) {
slouken@1917
   270
            fprintf(stderr,
slouken@5184
   271
                    "Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
slouken@1916
   272
                    argv[0], CommonUsage(state));
slouken@1895
   273
            quit(1);
slouken@1895
   274
        }
slouken@1914
   275
        i += consumed;
slouken@1895
   276
    }
slouken@1914
   277
    if (!CommonInit(state)) {
slouken@1914
   278
        quit(2);
slouken@1895
   279
    }
slouken@1895
   280
slouken@1895
   281
    /* Create the windows, initialize the renderers, and load the textures */
slouken@1914
   282
    sprites =
slouken@3685
   283
        (SDL_Texture **) SDL_malloc(state->num_windows * sizeof(*sprites));
slouken@1914
   284
    if (!sprites) {
slouken@1895
   285
        fprintf(stderr, "Out of memory!\n");
slouken@1895
   286
        quit(2);
slouken@1895
   287
    }
slouken@1914
   288
    for (i = 0; i < state->num_windows; ++i) {
slouken@5147
   289
        SDL_Renderer *renderer = state->renderers[i];
slouken@5147
   290
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@5147
   291
        SDL_RenderClear(renderer);
slouken@1895
   292
    }
slouken@1895
   293
    if (LoadSprite("icon.bmp") < 0) {
slouken@1895
   294
        quit(2);
slouken@1895
   295
    }
slouken@1895
   296
slouken@1895
   297
    /* Allocate memory for the sprite info */
slouken@1895
   298
    positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
slouken@1895
   299
    velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
slouken@1895
   300
    if (!positions || !velocities) {
slouken@1895
   301
        fprintf(stderr, "Out of memory!\n");
slouken@1895
   302
        quit(2);
slouken@1895
   303
    }
aschiffler@4884
   304
    srand((unsigned int)time(NULL));
slouken@1895
   305
    for (i = 0; i < num_sprites; ++i) {
slouken@1914
   306
        positions[i].x = rand() % (state->window_w - sprite_w);
slouken@1914
   307
        positions[i].y = rand() % (state->window_h - sprite_h);
slouken@1895
   308
        positions[i].w = sprite_w;
slouken@1895
   309
        positions[i].h = sprite_h;
slouken@1895
   310
        velocities[i].x = 0;
slouken@1895
   311
        velocities[i].y = 0;
slouken@1895
   312
        while (!velocities[i].x && !velocities[i].y) {
slouken@1895
   313
            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@1895
   314
            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@1895
   315
        }
slouken@1895
   316
    }
slouken@1895
   317
slouken@1915
   318
    /* Main render loop */
slouken@1895
   319
    frames = 0;
slouken@1895
   320
    then = SDL_GetTicks();
slouken@1895
   321
    done = 0;
slouken@1895
   322
    while (!done) {
slouken@1895
   323
        /* Check for events */
slouken@1895
   324
        ++frames;
slouken@1895
   325
        while (SDL_PollEvent(&event)) {
slouken@1914
   326
            CommonEvent(state, &event, &done);
slouken@1895
   327
        }
slouken@1914
   328
        for (i = 0; i < state->num_windows; ++i) {
slouken@5297
   329
            MoveSprites(state->renderers[i], sprites[i]);
slouken@1895
   330
        }
slouken@1895
   331
    }
slouken@1895
   332
slouken@1895
   333
    /* Print out some timing information */
slouken@1895
   334
    now = SDL_GetTicks();
slouken@1895
   335
    if (now > then) {
slouken@2786
   336
        double fps = ((double) frames * 1000) / (now - then);
slouken@2783
   337
        printf("%2.2f frames per second\n", fps);
slouken@1895
   338
    }
slouken@1895
   339
    quit(0);
slouken@5147
   340
    return 0;
slouken@1895
   341
}
slouken@1895
   342
slouken@1895
   343
/* vi: set ts=4 sw=4 expandtab: */