test/testspriteminimal.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 25 Apr 2013 00:26:17 -0700
changeset 7110 2a98852fd58d
parent 7011 82bbfbbc720e
child 7443 58c5fcba814d
permissions -rw-r--r--
Fixed bug 1582 - Allow disabling SDL_VIDEO_DRIVER_WINDOWS

Marcus von Appen

Trying to build SDL 2.x (HG) on Win32 platforms with either VS.NET or MinGW requires one to have the video subsystem and SDL_VIDEO_DRIVER_WINDOWS flag enabled due to the helper window creation routines.

The attached patch changes the helper window creation behaviour, so that one can build SDL2 without the video subsystem or Windows video drivers on Win32 platforms.
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@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) {
slouken@6259
    45
        fprintf(stderr, "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) {
slouken@3417
    76
        fprintf(stderr, "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
slouken@5147
    87
MoveSprites(SDL_Window * window, 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
slouken@6259
   129
    if (SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer) < 0) {
slouken@5147
   130
        quit(2);
slouken@5147
   131
    }
slouken@5147
   132
slouken@5147
   133
    if (LoadSprite("icon.bmp", renderer) < 0) {
slouken@3417
   134
        quit(2);
slouken@3417
   135
    }
slouken@3417
   136
slouken@3417
   137
    /* Initialize the sprite positions */
slouken@3417
   138
    srand(time(NULL));
slouken@3417
   139
    for (i = 0; i < NUM_SPRITES; ++i) {
slouken@3417
   140
        positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
slouken@3417
   141
        positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
slouken@3417
   142
        positions[i].w = sprite_w;
slouken@3417
   143
        positions[i].h = sprite_h;
slouken@3417
   144
        velocities[i].x = 0;
slouken@3417
   145
        velocities[i].y = 0;
slouken@3417
   146
        while (!velocities[i].x && !velocities[i].y) {
slouken@3417
   147
            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@3417
   148
            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@3417
   149
        }
slouken@3417
   150
    }
slouken@3417
   151
slouken@3417
   152
    /* Main render loop */
slouken@3417
   153
    done = 0;
slouken@3417
   154
    while (!done) {
slouken@3417
   155
        /* Check for events */
slouken@3417
   156
        while (SDL_PollEvent(&event)) {
slouken@3417
   157
            if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
slouken@3417
   158
                done = 1;
slouken@3417
   159
            }
slouken@3417
   160
        }
slouken@5147
   161
        MoveSprites(window, renderer, sprite);
slouken@3417
   162
    }
slouken@3417
   163
slouken@3417
   164
    quit(0);
slouken@3417
   165
}
slouken@3417
   166
slouken@3417
   167
/* vi: set ts=4 sw=4 expandtab: */