test/testspriteminimal.c
author David Ludwig <dludwig@pobox.com>
Wed, 25 Dec 2013 21:39:48 -0500
changeset 8563 c0e68f3b6bbb
parent 7639 9406b7dd2f2d
child 8149 681eb46b8ac4
permissions -rw-r--r--
WinRT: compiled the d3d11 renderer's shaders into SDL itself

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