test/testspriteminimal.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 26 Mar 2011 21:28:17 -0700
changeset 5517 aa339c0dca2d
parent 5158 b3ccd1947786
child 5535 96594ac5fd1a
permissions -rw-r--r--
NDS update

Frank Zago to SDL

There's also a patch to adapt one test for the nds, and that adds several
makefiles to compile these tests. Whenever you apply it, could you completely
remove the test/nds-test-progs/sprite and test/nds-test-progs/sprite2
directories ?
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@5517
     9
#ifdef __NDS__
slouken@5517
    10
#define WINDOW_WIDTH    256
slouken@5517
    11
#define WINDOW_HEIGHT   (2*192)
slouken@5517
    12
#else
slouken@3417
    13
#define WINDOW_WIDTH    640
slouken@3417
    14
#define WINDOW_HEIGHT   480
slouken@5517
    15
#endif
slouken@3417
    16
#define NUM_SPRITES     100
slouken@3417
    17
#define MAX_SPEED       1
slouken@3417
    18
slouken@3685
    19
static SDL_Texture *sprite;
slouken@3417
    20
static SDL_Rect positions[NUM_SPRITES];
slouken@3417
    21
static SDL_Rect velocities[NUM_SPRITES];
slouken@3417
    22
static int sprite_w, sprite_h;
slouken@3417
    23
slouken@3417
    24
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@3417
    25
static void
slouken@3417
    26
quit(int rc)
slouken@3417
    27
{
slouken@3417
    28
    exit(rc);
slouken@3417
    29
}
slouken@3417
    30
slouken@3417
    31
int
slouken@5147
    32
LoadSprite(char *file, SDL_Renderer *renderer)
slouken@3417
    33
{
slouken@3417
    34
    SDL_Surface *temp;
slouken@3417
    35
slouken@3417
    36
    /* Load the sprite image */
slouken@3417
    37
    temp = SDL_LoadBMP(file);
slouken@3417
    38
    if (temp == NULL) {
slouken@3417
    39
        fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
slouken@3417
    40
        return (-1);
slouken@3417
    41
    }
slouken@3417
    42
    sprite_w = temp->w;
slouken@3417
    43
    sprite_h = temp->h;
slouken@3417
    44
slouken@3417
    45
    /* Set transparent pixel as the pixel at (0,0) */
slouken@3417
    46
    if (temp->format->palette) {
slouken@3417
    47
        SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
slouken@3417
    48
    } else {
slouken@3417
    49
        switch (temp->format->BitsPerPixel) {
slouken@3417
    50
        case 15:
slouken@3417
    51
            SDL_SetColorKey(temp, SDL_TRUE,
slouken@3417
    52
                            (*(Uint16 *) temp->pixels) & 0x00007FFF);
slouken@3417
    53
            break;
slouken@3417
    54
        case 16:
slouken@3417
    55
            SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
slouken@3417
    56
            break;
slouken@3417
    57
        case 24:
slouken@3417
    58
            SDL_SetColorKey(temp, SDL_TRUE,
slouken@3417
    59
                            (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
slouken@3417
    60
            break;
slouken@3417
    61
        case 32:
slouken@3417
    62
            SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
slouken@3417
    63
            break;
slouken@3417
    64
        }
slouken@3417
    65
    }
slouken@3417
    66
slouken@3417
    67
    /* Create textures from the image */
slouken@5158
    68
    sprite = SDL_CreateTextureFromSurface(renderer, temp);
slouken@3417
    69
    if (!sprite) {
slouken@3417
    70
        fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
slouken@3417
    71
        SDL_FreeSurface(temp);
slouken@3417
    72
        return (-1);
slouken@3417
    73
    }
slouken@3417
    74
    SDL_FreeSurface(temp);
slouken@3417
    75
slouken@3417
    76
    /* We're ready to roll. :) */
slouken@3417
    77
    return (0);
slouken@3417
    78
}
slouken@3417
    79
slouken@3417
    80
void
slouken@5147
    81
MoveSprites(SDL_Window * window, SDL_Renderer * renderer, SDL_Texture * sprite)
slouken@3417
    82
{
slouken@3417
    83
    int i;
slouken@3417
    84
    int window_w = WINDOW_WIDTH;
slouken@3417
    85
    int window_h = WINDOW_HEIGHT;
slouken@3417
    86
    SDL_Rect *position, *velocity;
slouken@3417
    87
slouken@3417
    88
    /* Draw a gray background */
slouken@5147
    89
    SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@5147
    90
    SDL_RenderClear(renderer);
slouken@3417
    91
slouken@3417
    92
    /* Move the sprite, bounce at the wall, and draw */
slouken@3417
    93
    for (i = 0; i < NUM_SPRITES; ++i) {
slouken@3417
    94
        position = &positions[i];
slouken@3417
    95
        velocity = &velocities[i];
slouken@3417
    96
        position->x += velocity->x;
slouken@3417
    97
        if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
slouken@3417
    98
            velocity->x = -velocity->x;
slouken@3417
    99
            position->x += velocity->x;
slouken@3417
   100
        }
slouken@3417
   101
        position->y += velocity->y;
slouken@3417
   102
        if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
slouken@3417
   103
            velocity->y = -velocity->y;
slouken@3417
   104
            position->y += velocity->y;
slouken@3417
   105
        }
slouken@3417
   106
slouken@3417
   107
        /* Blit the sprite onto the screen */
slouken@5147
   108
        SDL_RenderCopy(renderer, sprite, NULL, position);
slouken@3417
   109
    }
slouken@3417
   110
slouken@3417
   111
    /* Update the screen! */
slouken@5147
   112
    SDL_RenderPresent(renderer);
slouken@3417
   113
}
slouken@3417
   114
slouken@3417
   115
int
slouken@3417
   116
main(int argc, char *argv[])
slouken@3417
   117
{
slouken@3685
   118
    SDL_Window *window;
slouken@5147
   119
    SDL_Renderer *renderer;
slouken@3417
   120
    int i, done;
slouken@3417
   121
    SDL_Event event;
slouken@3417
   122
slouken@3417
   123
    window = SDL_CreateWindow("Happy Smileys",
slouken@3417
   124
                              SDL_WINDOWPOS_UNDEFINED,
slouken@3417
   125
                              SDL_WINDOWPOS_UNDEFINED,
slouken@3417
   126
                              WINDOW_WIDTH, WINDOW_HEIGHT,
slouken@3417
   127
                              SDL_WINDOW_SHOWN);
slouken@3417
   128
    if (!window) {
slouken@3417
   129
        quit(2);
slouken@3417
   130
    }
slouken@3417
   131
slouken@5147
   132
    renderer = SDL_CreateRenderer(window, -1, 0);
slouken@5147
   133
    if (!renderer) {
slouken@5147
   134
        quit(2);
slouken@5147
   135
    }
slouken@5147
   136
slouken@5147
   137
    if (LoadSprite("icon.bmp", renderer) < 0) {
slouken@3417
   138
        quit(2);
slouken@3417
   139
    }
slouken@3417
   140
slouken@3417
   141
    /* Initialize the sprite positions */
slouken@3417
   142
    srand(time(NULL));
slouken@3417
   143
    for (i = 0; i < NUM_SPRITES; ++i) {
slouken@3417
   144
        positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
slouken@3417
   145
        positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
slouken@3417
   146
        positions[i].w = sprite_w;
slouken@3417
   147
        positions[i].h = sprite_h;
slouken@3417
   148
        velocities[i].x = 0;
slouken@3417
   149
        velocities[i].y = 0;
slouken@3417
   150
        while (!velocities[i].x && !velocities[i].y) {
slouken@3417
   151
            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@3417
   152
            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@3417
   153
        }
slouken@3417
   154
    }
slouken@3417
   155
slouken@3417
   156
    /* Main render loop */
slouken@3417
   157
    done = 0;
slouken@3417
   158
    while (!done) {
slouken@3417
   159
        /* Check for events */
slouken@3417
   160
        while (SDL_PollEvent(&event)) {
slouken@3417
   161
            if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
slouken@3417
   162
                done = 1;
slouken@3417
   163
            }
slouken@3417
   164
        }
slouken@5147
   165
        MoveSprites(window, renderer, sprite);
slouken@3417
   166
    }
slouken@3417
   167
slouken@3417
   168
    quit(0);
slouken@3417
   169
}
slouken@3417
   170
slouken@3417
   171
/* vi: set ts=4 sw=4 expandtab: */