test/testspriteminimal.c
author Alex Szpakowski <slime73@gmail.com>
Wed, 06 May 2015 12:54:51 -0300
changeset 9589 a583c42c51d7
parent 9278 8900afb78a19
child 9607 7746ab058d12
permissions -rw-r--r--
Fixed SDL_GL_GetAttribute queries for framebuffer component sizes in Core Profile OpenGL contexts.

Fixes bugzilla #2060.
slouken@5535
     1
/*
slouken@8149
     2
  Copyright (C) 1997-2014 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
icculus@9278
    18
#ifdef __EMSCRIPTEN__
icculus@9278
    19
#include <emscripten/emscripten.h>
icculus@9278
    20
#endif
icculus@9278
    21
slouken@4969
    22
#include "SDL.h"
slouken@3417
    23
slouken@3417
    24
#define WINDOW_WIDTH    640
slouken@3417
    25
#define WINDOW_HEIGHT   480
slouken@3417
    26
#define NUM_SPRITES     100
slouken@3417
    27
#define MAX_SPEED       1
slouken@3417
    28
slouken@3685
    29
static SDL_Texture *sprite;
slouken@3417
    30
static SDL_Rect positions[NUM_SPRITES];
slouken@3417
    31
static SDL_Rect velocities[NUM_SPRITES];
slouken@3417
    32
static int sprite_w, sprite_h;
slouken@3417
    33
icculus@9278
    34
SDL_Renderer *renderer;
icculus@9278
    35
int done;
icculus@9278
    36
slouken@3417
    37
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@3417
    38
static void
slouken@3417
    39
quit(int rc)
slouken@3417
    40
{
slouken@3417
    41
    exit(rc);
slouken@3417
    42
}
slouken@3417
    43
slouken@3417
    44
int
slouken@5147
    45
LoadSprite(char *file, SDL_Renderer *renderer)
slouken@3417
    46
{
slouken@3417
    47
    SDL_Surface *temp;
slouken@3417
    48
slouken@3417
    49
    /* Load the sprite image */
slouken@3417
    50
    temp = SDL_LoadBMP(file);
slouken@3417
    51
    if (temp == NULL) {
aschiffler@7639
    52
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", file, SDL_GetError());
slouken@3417
    53
        return (-1);
slouken@3417
    54
    }
slouken@3417
    55
    sprite_w = temp->w;
slouken@3417
    56
    sprite_h = temp->h;
slouken@3417
    57
slouken@3417
    58
    /* Set transparent pixel as the pixel at (0,0) */
slouken@3417
    59
    if (temp->format->palette) {
slouken@3417
    60
        SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
slouken@3417
    61
    } else {
slouken@3417
    62
        switch (temp->format->BitsPerPixel) {
slouken@3417
    63
        case 15:
slouken@3417
    64
            SDL_SetColorKey(temp, SDL_TRUE,
slouken@3417
    65
                            (*(Uint16 *) temp->pixels) & 0x00007FFF);
slouken@3417
    66
            break;
slouken@3417
    67
        case 16:
slouken@3417
    68
            SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
slouken@3417
    69
            break;
slouken@3417
    70
        case 24:
slouken@3417
    71
            SDL_SetColorKey(temp, SDL_TRUE,
slouken@3417
    72
                            (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
slouken@3417
    73
            break;
slouken@3417
    74
        case 32:
slouken@3417
    75
            SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
slouken@3417
    76
            break;
slouken@3417
    77
        }
slouken@3417
    78
    }
slouken@3417
    79
slouken@3417
    80
    /* Create textures from the image */
slouken@5158
    81
    sprite = SDL_CreateTextureFromSurface(renderer, temp);
slouken@3417
    82
    if (!sprite) {
aschiffler@7639
    83
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
slouken@3417
    84
        SDL_FreeSurface(temp);
slouken@3417
    85
        return (-1);
slouken@3417
    86
    }
slouken@3417
    87
    SDL_FreeSurface(temp);
slouken@3417
    88
slouken@3417
    89
    /* We're ready to roll. :) */
slouken@3417
    90
    return (0);
slouken@3417
    91
}
slouken@3417
    92
slouken@3417
    93
void
philipp@7539
    94
MoveSprites(SDL_Renderer * renderer, SDL_Texture * sprite)
slouken@3417
    95
{
slouken@3417
    96
    int i;
slouken@3417
    97
    int window_w = WINDOW_WIDTH;
slouken@3417
    98
    int window_h = WINDOW_HEIGHT;
slouken@3417
    99
    SDL_Rect *position, *velocity;
slouken@3417
   100
slouken@3417
   101
    /* Draw a gray background */
slouken@5147
   102
    SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@5147
   103
    SDL_RenderClear(renderer);
slouken@3417
   104
slouken@3417
   105
    /* Move the sprite, bounce at the wall, and draw */
slouken@3417
   106
    for (i = 0; i < NUM_SPRITES; ++i) {
slouken@3417
   107
        position = &positions[i];
slouken@3417
   108
        velocity = &velocities[i];
slouken@3417
   109
        position->x += velocity->x;
slouken@3417
   110
        if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
slouken@3417
   111
            velocity->x = -velocity->x;
slouken@3417
   112
            position->x += velocity->x;
slouken@3417
   113
        }
slouken@3417
   114
        position->y += velocity->y;
slouken@3417
   115
        if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
slouken@3417
   116
            velocity->y = -velocity->y;
slouken@3417
   117
            position->y += velocity->y;
slouken@3417
   118
        }
slouken@3417
   119
slouken@3417
   120
        /* Blit the sprite onto the screen */
slouken@5147
   121
        SDL_RenderCopy(renderer, sprite, NULL, position);
slouken@3417
   122
    }
slouken@3417
   123
slouken@3417
   124
    /* Update the screen! */
slouken@5147
   125
    SDL_RenderPresent(renderer);
slouken@3417
   126
}
slouken@3417
   127
icculus@9278
   128
void loop()
icculus@9278
   129
{
icculus@9278
   130
    SDL_Event event;
icculus@9278
   131
icculus@9278
   132
    /* Check for events */
icculus@9278
   133
    while (SDL_PollEvent(&event)) {
icculus@9278
   134
        if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
icculus@9278
   135
            done = 1;
icculus@9278
   136
        }
icculus@9278
   137
    }
icculus@9278
   138
    MoveSprites(renderer, sprite);
icculus@9278
   139
}
icculus@9278
   140
slouken@3417
   141
int
slouken@3417
   142
main(int argc, char *argv[])
slouken@3417
   143
{
slouken@3685
   144
    SDL_Window *window;
icculus@9278
   145
    int i;
icculus@9278
   146
slouken@3417
   147
aschiffler@7639
   148
	/* Enable standard application logging */
aschiffler@7639
   149
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
   150
slouken@6259
   151
    if (SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer) < 0) {
slouken@5147
   152
        quit(2);
slouken@5147
   153
    }
slouken@5147
   154
slouken@5147
   155
    if (LoadSprite("icon.bmp", renderer) < 0) {
slouken@3417
   156
        quit(2);
slouken@3417
   157
    }
slouken@3417
   158
slouken@3417
   159
    /* Initialize the sprite positions */
slouken@3417
   160
    srand(time(NULL));
slouken@3417
   161
    for (i = 0; i < NUM_SPRITES; ++i) {
slouken@3417
   162
        positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
slouken@3417
   163
        positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
slouken@3417
   164
        positions[i].w = sprite_w;
slouken@3417
   165
        positions[i].h = sprite_h;
slouken@3417
   166
        velocities[i].x = 0;
slouken@3417
   167
        velocities[i].y = 0;
slouken@3417
   168
        while (!velocities[i].x && !velocities[i].y) {
slouken@3417
   169
            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@3417
   170
            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@3417
   171
        }
slouken@3417
   172
    }
slouken@3417
   173
slouken@3417
   174
    /* Main render loop */
slouken@3417
   175
    done = 0;
icculus@9278
   176
icculus@9278
   177
#ifdef __EMSCRIPTEN__
icculus@9278
   178
    emscripten_set_main_loop(loop, 0, 1);
icculus@9278
   179
#else
slouken@3417
   180
    while (!done) {
icculus@9278
   181
        loop();
slouken@3417
   182
        }
icculus@9278
   183
#endif
slouken@3417
   184
    quit(0);
philipp@7443
   185
philipp@7443
   186
    return 0; /* to prevent compiler warning */
slouken@3417
   187
}
slouken@3417
   188
slouken@3417
   189
/* vi: set ts=4 sw=4 expandtab: */