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