test/testspriteminimal.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Jan 2011 11:22:07 -0800
changeset 4969 d2247eb39fab
parent 3685 64ce267332c6
child 5147 ad50b3db78bd
permissions -rw-r--r--
Need to include SDL_main.h ... eh, just include everything. :)
slouken@3417
     1
/* Simple program:  Move N sprites around on the screen as fast as possible */
slouken@3417
     2
slouken@3417
     3
#include <stdlib.h>
slouken@3417
     4
#include <stdio.h>
slouken@3417
     5
#include <time.h>
slouken@3417
     6
slouken@4969
     7
#include "SDL.h"
slouken@3417
     8
slouken@3417
     9
#define WINDOW_WIDTH    640
slouken@3417
    10
#define WINDOW_HEIGHT   480
slouken@3417
    11
#define NUM_SPRITES     100
slouken@3417
    12
#define MAX_SPEED       1
slouken@3417
    13
slouken@3685
    14
static SDL_Texture *sprite;
slouken@3417
    15
static SDL_Rect positions[NUM_SPRITES];
slouken@3417
    16
static SDL_Rect velocities[NUM_SPRITES];
slouken@3417
    17
static int sprite_w, sprite_h;
slouken@3417
    18
slouken@3417
    19
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@3417
    20
static void
slouken@3417
    21
quit(int rc)
slouken@3417
    22
{
slouken@3417
    23
    exit(rc);
slouken@3417
    24
}
slouken@3417
    25
slouken@3417
    26
int
slouken@3417
    27
LoadSprite(char *file)
slouken@3417
    28
{
slouken@3417
    29
    SDL_Surface *temp;
slouken@3417
    30
slouken@3417
    31
    /* Load the sprite image */
slouken@3417
    32
    temp = SDL_LoadBMP(file);
slouken@3417
    33
    if (temp == NULL) {
slouken@3417
    34
        fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
slouken@3417
    35
        return (-1);
slouken@3417
    36
    }
slouken@3417
    37
    sprite_w = temp->w;
slouken@3417
    38
    sprite_h = temp->h;
slouken@3417
    39
slouken@3417
    40
    /* Set transparent pixel as the pixel at (0,0) */
slouken@3417
    41
    if (temp->format->palette) {
slouken@3417
    42
        SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
slouken@3417
    43
    } else {
slouken@3417
    44
        switch (temp->format->BitsPerPixel) {
slouken@3417
    45
        case 15:
slouken@3417
    46
            SDL_SetColorKey(temp, SDL_TRUE,
slouken@3417
    47
                            (*(Uint16 *) temp->pixels) & 0x00007FFF);
slouken@3417
    48
            break;
slouken@3417
    49
        case 16:
slouken@3417
    50
            SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
slouken@3417
    51
            break;
slouken@3417
    52
        case 24:
slouken@3417
    53
            SDL_SetColorKey(temp, SDL_TRUE,
slouken@3417
    54
                            (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
slouken@3417
    55
            break;
slouken@3417
    56
        case 32:
slouken@3417
    57
            SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
slouken@3417
    58
            break;
slouken@3417
    59
        }
slouken@3417
    60
    }
slouken@3417
    61
slouken@3417
    62
    /* Create textures from the image */
slouken@3417
    63
    sprite = SDL_CreateTextureFromSurface(0, temp);
slouken@3417
    64
    if (!sprite) {
slouken@3417
    65
        SDL_SetColorKey(temp, 0, 0);
slouken@3417
    66
        sprite = SDL_CreateTextureFromSurface(0, temp);
slouken@3417
    67
    }
slouken@3417
    68
    if (!sprite) {
slouken@3417
    69
        fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
slouken@3417
    70
        SDL_FreeSurface(temp);
slouken@3417
    71
        return (-1);
slouken@3417
    72
    }
slouken@3417
    73
    SDL_FreeSurface(temp);
slouken@3417
    74
slouken@3417
    75
    /* We're ready to roll. :) */
slouken@3417
    76
    return (0);
slouken@3417
    77
}
slouken@3417
    78
slouken@3417
    79
void
slouken@3685
    80
MoveSprites(SDL_Window * window, SDL_Texture * sprite)
slouken@3417
    81
{
slouken@3417
    82
    int i;
slouken@3417
    83
    int window_w = WINDOW_WIDTH;
slouken@3417
    84
    int window_h = WINDOW_HEIGHT;
slouken@3417
    85
    SDL_Rect *position, *velocity;
slouken@3417
    86
slouken@3417
    87
    /* Draw a gray background */
slouken@3417
    88
    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
slouken@3596
    89
    SDL_RenderClear();
slouken@3417
    90
slouken@3417
    91
    /* Move the sprite, bounce at the wall, and draw */
slouken@3417
    92
    for (i = 0; i < NUM_SPRITES; ++i) {
slouken@3417
    93
        position = &positions[i];
slouken@3417
    94
        velocity = &velocities[i];
slouken@3417
    95
        position->x += velocity->x;
slouken@3417
    96
        if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
slouken@3417
    97
            velocity->x = -velocity->x;
slouken@3417
    98
            position->x += velocity->x;
slouken@3417
    99
        }
slouken@3417
   100
        position->y += velocity->y;
slouken@3417
   101
        if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
slouken@3417
   102
            velocity->y = -velocity->y;
slouken@3417
   103
            position->y += velocity->y;
slouken@3417
   104
        }
slouken@3417
   105
slouken@3417
   106
        /* Blit the sprite onto the screen */
slouken@3417
   107
        SDL_RenderCopy(sprite, NULL, position);
slouken@3417
   108
    }
slouken@3417
   109
slouken@3417
   110
    /* Update the screen! */
slouken@3417
   111
    SDL_RenderPresent();
slouken@3417
   112
}
slouken@3417
   113
slouken@3417
   114
int
slouken@3417
   115
main(int argc, char *argv[])
slouken@3417
   116
{
slouken@3685
   117
    SDL_Window *window;
slouken@3417
   118
    int i, done;
slouken@3417
   119
    SDL_Event event;
slouken@3417
   120
slouken@3417
   121
    window = SDL_CreateWindow("Happy Smileys",
slouken@3417
   122
                              SDL_WINDOWPOS_UNDEFINED,
slouken@3417
   123
                              SDL_WINDOWPOS_UNDEFINED,
slouken@3417
   124
                              WINDOW_WIDTH, WINDOW_HEIGHT,
slouken@3417
   125
                              SDL_WINDOW_SHOWN);
slouken@3417
   126
    if (!window) {
slouken@3417
   127
        quit(2);
slouken@3417
   128
    }
slouken@3417
   129
slouken@3417
   130
    if (LoadSprite("icon.bmp") < 0) {
slouken@3417
   131
        quit(2);
slouken@3417
   132
    }
slouken@3417
   133
slouken@3417
   134
    /* Initialize the sprite positions */
slouken@3417
   135
    srand(time(NULL));
slouken@3417
   136
    for (i = 0; i < NUM_SPRITES; ++i) {
slouken@3417
   137
        positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
slouken@3417
   138
        positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
slouken@3417
   139
        positions[i].w = sprite_w;
slouken@3417
   140
        positions[i].h = sprite_h;
slouken@3417
   141
        velocities[i].x = 0;
slouken@3417
   142
        velocities[i].y = 0;
slouken@3417
   143
        while (!velocities[i].x && !velocities[i].y) {
slouken@3417
   144
            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@3417
   145
            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@3417
   146
        }
slouken@3417
   147
    }
slouken@3417
   148
slouken@3417
   149
    /* Main render loop */
slouken@3417
   150
    done = 0;
slouken@3417
   151
    while (!done) {
slouken@3417
   152
        /* Check for events */
slouken@3417
   153
        while (SDL_PollEvent(&event)) {
slouken@3417
   154
            if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
slouken@3417
   155
                done = 1;
slouken@3417
   156
            }
slouken@3417
   157
        }
slouken@3417
   158
        MoveSprites(window, sprite);
slouken@3417
   159
    }
slouken@3417
   160
slouken@3417
   161
    quit(0);
slouken@3417
   162
}
slouken@3417
   163
slouken@3417
   164
/* vi: set ts=4 sw=4 expandtab: */