test/testspriteminimal.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 07 Jan 2017 22:24:45 -0500
changeset 10782 1de6d4633ee6
parent 10737 3406a0f8b041
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Fixed a bunch of compiler warnings in the test code.
slouken@5535
     1
/*
slouken@10737
     2
  Copyright (C) 1997-2017 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);
philipp@9607
   139
#ifdef __EMSCRIPTEN__
philipp@9607
   140
    if (done) {
philipp@9607
   141
        emscripten_cancel_main_loop();
philipp@9607
   142
    }
philipp@9607
   143
#endif
icculus@9278
   144
}
icculus@9278
   145
slouken@3417
   146
int
slouken@3417
   147
main(int argc, char *argv[])
slouken@3417
   148
{
slouken@3685
   149
    SDL_Window *window;
icculus@9278
   150
    int i;
icculus@9278
   151
slouken@3417
   152
philipp@9922
   153
    /* Enable standard application logging */
aschiffler@7639
   154
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
   155
slouken@6259
   156
    if (SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer) < 0) {
slouken@5147
   157
        quit(2);
slouken@5147
   158
    }
slouken@5147
   159
slouken@5147
   160
    if (LoadSprite("icon.bmp", renderer) < 0) {
slouken@3417
   161
        quit(2);
slouken@3417
   162
    }
slouken@3417
   163
slouken@3417
   164
    /* Initialize the sprite positions */
slouken@3417
   165
    srand(time(NULL));
slouken@3417
   166
    for (i = 0; i < NUM_SPRITES; ++i) {
slouken@3417
   167
        positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
slouken@3417
   168
        positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
slouken@3417
   169
        positions[i].w = sprite_w;
slouken@3417
   170
        positions[i].h = sprite_h;
slouken@3417
   171
        velocities[i].x = 0;
slouken@3417
   172
        velocities[i].y = 0;
slouken@3417
   173
        while (!velocities[i].x && !velocities[i].y) {
slouken@3417
   174
            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@3417
   175
            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@3417
   176
        }
slouken@3417
   177
    }
slouken@3417
   178
slouken@3417
   179
    /* Main render loop */
slouken@3417
   180
    done = 0;
icculus@9278
   181
icculus@9278
   182
#ifdef __EMSCRIPTEN__
icculus@9278
   183
    emscripten_set_main_loop(loop, 0, 1);
icculus@9278
   184
#else
slouken@3417
   185
    while (!done) {
icculus@9278
   186
        loop();
slouken@9965
   187
    }
icculus@9278
   188
#endif
slouken@3417
   189
    quit(0);
philipp@7443
   190
philipp@7443
   191
    return 0; /* to prevent compiler warning */
slouken@3417
   192
}
slouken@3417
   193
slouken@3417
   194
/* vi: set ts=4 sw=4 expandtab: */