test/testsprite2.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 19 Jul 2006 05:45:42 +0000
changeset 1917 3f54b3ec5a07
parent 1916 c773b0c0ac89
child 1965 a788656ca29a
permissions -rw-r--r--
Implemented scaling in the D3D 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@1914
     7
#include "common.h"
slouken@1895
     8
slouken@1895
     9
#define NUM_SPRITES	100
slouken@1895
    10
#define MAX_SPEED 	1
slouken@1916
    11
#define BACKGROUND  0x00A0A0A0
slouken@1895
    12
slouken@1914
    13
static CommonState *state;
slouken@1895
    14
static int num_sprites;
slouken@1895
    15
static SDL_TextureID *sprites;
slouken@1895
    16
static SDL_Rect *positions;
slouken@1895
    17
static SDL_Rect *velocities;
slouken@1895
    18
static int sprite_w, sprite_h;
slouken@1916
    19
static SDL_TextureBlendMode blendMode = SDL_TextureBlendMode_Mask;
slouken@1917
    20
static SDL_TextureScaleMode scaleMode = SDL_TextureScaleMode_None;
slouken@1895
    21
slouken@1895
    22
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1895
    23
static void
slouken@1895
    24
quit(int rc)
slouken@1895
    25
{
slouken@1895
    26
    if (sprites) {
slouken@1895
    27
        SDL_free(sprites);
slouken@1895
    28
    }
slouken@1895
    29
    if (positions) {
slouken@1895
    30
        SDL_free(positions);
slouken@1895
    31
    }
slouken@1895
    32
    if (velocities) {
slouken@1895
    33
        SDL_free(velocities);
slouken@1895
    34
    }
slouken@1914
    35
    CommonQuit(state);
slouken@1895
    36
    exit(rc);
slouken@1895
    37
}
slouken@1895
    38
slouken@1895
    39
int
slouken@1895
    40
LoadSprite(char *file)
slouken@1895
    41
{
slouken@1895
    42
    int i;
slouken@1895
    43
    SDL_Surface *temp;
slouken@1895
    44
slouken@1895
    45
    /* Load the sprite image */
slouken@1895
    46
    temp = SDL_LoadBMP(file);
slouken@1895
    47
    if (temp == NULL) {
slouken@1895
    48
        fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
slouken@1895
    49
        return (-1);
slouken@1895
    50
    }
slouken@1895
    51
    sprite_w = temp->w;
slouken@1895
    52
    sprite_h = temp->h;
slouken@1895
    53
slouken@1895
    54
    /* Set transparent pixel as the pixel at (0,0) */
slouken@1895
    55
    if (temp->format->palette) {
slouken@1895
    56
        SDL_SetColorKey(temp, SDL_SRCCOLORKEY, *(Uint8 *) temp->pixels);
slouken@1895
    57
    }
slouken@1895
    58
slouken@1895
    59
    /* Create textures from the image */
slouken@1914
    60
    for (i = 0; i < state->num_windows; ++i) {
slouken@1914
    61
        SDL_SelectRenderer(state->windows[i]);
slouken@1895
    62
        sprites[i] =
slouken@1895
    63
            SDL_CreateTextureFromSurface(0, SDL_TextureAccess_Remote, temp);
slouken@1895
    64
        if (!sprites[i]) {
slouken@1895
    65
            fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
slouken@1895
    66
            SDL_FreeSurface(temp);
slouken@1895
    67
            return (-1);
slouken@1895
    68
        }
slouken@1895
    69
    }
slouken@1895
    70
    SDL_FreeSurface(temp);
slouken@1895
    71
slouken@1895
    72
    /* We're ready to roll. :) */
slouken@1895
    73
    return (0);
slouken@1895
    74
}
slouken@1895
    75
slouken@1895
    76
void
slouken@1895
    77
MoveSprites(SDL_WindowID window, SDL_TextureID sprite)
slouken@1895
    78
{
slouken@1895
    79
    int i, n;
slouken@1895
    80
    int window_w, window_h;
slouken@1895
    81
    SDL_Rect area, *position, *velocity;
slouken@1895
    82
slouken@1895
    83
    SDL_SelectRenderer(window);
slouken@1895
    84
slouken@1895
    85
    /* Query the sizes */
slouken@1895
    86
    SDL_GetWindowSize(window, &window_w, &window_h);
slouken@1895
    87
slouken@1895
    88
    /* Move the sprite, bounce at the wall, and draw */
slouken@1895
    89
    n = 0;
slouken@1903
    90
    SDL_RenderFill(NULL, BACKGROUND);
slouken@1895
    91
    for (i = 0; i < num_sprites; ++i) {
slouken@1895
    92
        position = &positions[i];
slouken@1895
    93
        velocity = &velocities[i];
slouken@1895
    94
        position->x += velocity->x;
slouken@1895
    95
        if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
slouken@1895
    96
            velocity->x = -velocity->x;
slouken@1895
    97
            position->x += velocity->x;
slouken@1895
    98
        }
slouken@1895
    99
        position->y += velocity->y;
slouken@1895
   100
        if ((position->y < 0) || (position->y >= (window_h - sprite_w))) {
slouken@1895
   101
            velocity->y = -velocity->y;
slouken@1895
   102
            position->y += velocity->y;
slouken@1895
   103
        }
slouken@1895
   104
slouken@1895
   105
        /* Blit the sprite onto the screen */
slouken@1917
   106
        SDL_RenderCopy(sprite, NULL, position, blendMode, scaleMode);
slouken@1895
   107
    }
slouken@1895
   108
slouken@1895
   109
    /* Update the screen! */
slouken@1895
   110
    SDL_RenderPresent();
slouken@1895
   111
}
slouken@1895
   112
slouken@1895
   113
int
slouken@1895
   114
main(int argc, char *argv[])
slouken@1895
   115
{
slouken@1895
   116
    int i, done;
slouken@1895
   117
    SDL_Event event;
slouken@1895
   118
    Uint32 then, now, frames;
slouken@1895
   119
slouken@1914
   120
    /* Initialize parameters */
slouken@1914
   121
    num_sprites = NUM_SPRITES;
slouken@1914
   122
slouken@1914
   123
    /* Initialize test framework */
slouken@1914
   124
    state = CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@1914
   125
    if (!state) {
slouken@1914
   126
        return 1;
slouken@1895
   127
    }
slouken@1914
   128
    for (i = 1; i < argc;) {
slouken@1914
   129
        int consumed;
slouken@1895
   130
slouken@1914
   131
        consumed = CommonArg(state, i);
slouken@1915
   132
        if (consumed == 0) {
slouken@1916
   133
            consumed = -1;
slouken@1916
   134
            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
slouken@1916
   135
                if (argv[i + 1]) {
slouken@1916
   136
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
slouken@1916
   137
                        blendMode = SDL_TextureBlendMode_None;
slouken@1916
   138
                        consumed = 2;
slouken@1917
   139
                    } else if (SDL_strcasecmp(argv[i + 1], "mask") == 0) {
slouken@1917
   140
                        blendMode = SDL_TextureBlendMode_Mask;
slouken@1917
   141
                        consumed = 2;
slouken@1916
   142
                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
slouken@1916
   143
                        blendMode = SDL_TextureBlendMode_Blend;
slouken@1916
   144
                        consumed = 2;
slouken@1916
   145
                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
slouken@1916
   146
                        blendMode = SDL_TextureBlendMode_Add;
slouken@1916
   147
                        consumed = 2;
slouken@1916
   148
                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
slouken@1916
   149
                        blendMode = SDL_TextureBlendMode_Mod;
slouken@1916
   150
                        consumed = 2;
slouken@1916
   151
                    }
slouken@1916
   152
                }
slouken@1917
   153
            } else if (SDL_strcasecmp(argv[i], "--scale") == 0) {
slouken@1917
   154
                if (argv[i + 1]) {
slouken@1917
   155
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
slouken@1917
   156
                        scaleMode = SDL_TextureScaleMode_None;
slouken@1917
   157
                        consumed = 2;
slouken@1917
   158
                    } else if (SDL_strcasecmp(argv[i + 1], "fast") == 0) {
slouken@1917
   159
                        scaleMode = SDL_TextureScaleMode_Fast;
slouken@1917
   160
                        consumed = 2;
slouken@1917
   161
                    } else if (SDL_strcasecmp(argv[i + 1], "slow") == 0) {
slouken@1917
   162
                        scaleMode = SDL_TextureScaleMode_Slow;
slouken@1917
   163
                        consumed = 2;
slouken@1917
   164
                    } else if (SDL_strcasecmp(argv[i + 1], "best") == 0) {
slouken@1917
   165
                        scaleMode = SDL_TextureScaleMode_Best;
slouken@1917
   166
                        consumed = 2;
slouken@1917
   167
                    }
slouken@1917
   168
                }
slouken@1916
   169
            } else if (SDL_isdigit(*argv[i])) {
slouken@1916
   170
                num_sprites = SDL_atoi(argv[i]);
slouken@1916
   171
                consumed = 1;
slouken@1916
   172
            }
slouken@1915
   173
        }
slouken@1914
   174
        if (consumed < 0) {
slouken@1917
   175
            fprintf(stderr,
slouken@1917
   176
                    "Usage: %s %s [--blend none|mask|blend|add|mod] [--scale none|fast|slow|best]",
slouken@1916
   177
                    argv[0], CommonUsage(state));
slouken@1895
   178
            quit(1);
slouken@1895
   179
        }
slouken@1914
   180
        i += consumed;
slouken@1895
   181
    }
slouken@1914
   182
    if (!CommonInit(state)) {
slouken@1914
   183
        quit(2);
slouken@1895
   184
    }
slouken@1895
   185
slouken@1895
   186
    /* Create the windows, initialize the renderers, and load the textures */
slouken@1914
   187
    sprites =
slouken@1914
   188
        (SDL_TextureID *) SDL_malloc(state->num_windows * sizeof(*sprites));
slouken@1914
   189
    if (!sprites) {
slouken@1895
   190
        fprintf(stderr, "Out of memory!\n");
slouken@1895
   191
        quit(2);
slouken@1895
   192
    }
slouken@1914
   193
    for (i = 0; i < state->num_windows; ++i) {
slouken@1914
   194
        SDL_SelectRenderer(state->windows[i]);
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@1917
   209
    if (scaleMode != SDL_TextureScaleMode_None) {
slouken@1917
   210
        sprite_w += sprite_w / 2;
slouken@1917
   211
        sprite_h += sprite_h / 2;
slouken@1917
   212
    }
slouken@1895
   213
    for (i = 0; i < num_sprites; ++i) {
slouken@1914
   214
        positions[i].x = rand() % (state->window_w - sprite_w);
slouken@1914
   215
        positions[i].y = rand() % (state->window_h - sprite_h);
slouken@1895
   216
        positions[i].w = sprite_w;
slouken@1895
   217
        positions[i].h = sprite_h;
slouken@1895
   218
        velocities[i].x = 0;
slouken@1895
   219
        velocities[i].y = 0;
slouken@1895
   220
        while (!velocities[i].x && !velocities[i].y) {
slouken@1895
   221
            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@1895
   222
            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@1895
   223
        }
slouken@1895
   224
    }
slouken@1895
   225
slouken@1915
   226
    /* Main render loop */
slouken@1895
   227
    frames = 0;
slouken@1895
   228
    then = SDL_GetTicks();
slouken@1895
   229
    done = 0;
slouken@1895
   230
    while (!done) {
slouken@1895
   231
        /* Check for events */
slouken@1895
   232
        ++frames;
slouken@1895
   233
        while (SDL_PollEvent(&event)) {
slouken@1914
   234
            CommonEvent(state, &event, &done);
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
                }
slouken@1895
   243
                break;
slouken@1895
   244
            default:
slouken@1895
   245
                break;
slouken@1895
   246
            }
slouken@1895
   247
        }
slouken@1914
   248
        for (i = 0; i < state->num_windows; ++i) {
slouken@1914
   249
            MoveSprites(state->windows[i], sprites[i]);
slouken@1895
   250
        }
slouken@1895
   251
    }
slouken@1895
   252
slouken@1895
   253
    /* Print out some timing information */
slouken@1895
   254
    now = SDL_GetTicks();
slouken@1895
   255
    if (now > then) {
slouken@1895
   256
        printf("%2.2f frames per second\n",
slouken@1895
   257
               ((double) frames * 1000) / (now - then));
slouken@1895
   258
    }
slouken@1895
   259
    quit(0);
slouken@1895
   260
}
slouken@1895
   261
slouken@1895
   262
/* vi: set ts=4 sw=4 expandtab: */