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