test/testspriteminimal.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 22 Jan 2012 19:47:33 -0500
changeset 6259 4aef5b792087
parent 5535 96594ac5fd1a
child 7011 82bbfbbc720e
permissions -rw-r--r--
Have testspriteminimal use the new simple API
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5517
diff changeset
     1
/*
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5517
diff changeset
     2
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5517
diff changeset
     3
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5517
diff changeset
     4
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5517
diff changeset
     5
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5517
diff changeset
     6
  arising from the use of this software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5517
diff changeset
     7
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5517
diff changeset
     8
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5517
diff changeset
     9
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5517
diff changeset
    10
  freely.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5517
diff changeset
    11
*/
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
/* Simple program:  Move N sprites around on the screen as fast as possible */
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    13
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    14
#include <stdlib.h>
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    15
#include <stdio.h>
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    16
#include <time.h>
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    17
4969
d2247eb39fab Need to include SDL_main.h ... eh, just include everything. :)
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    18
#include "SDL.h"
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    19
5517
aa339c0dca2d NDS update
Sam Lantinga <slouken@libsdl.org>
parents: 5158
diff changeset
    20
#ifdef __NDS__
aa339c0dca2d NDS update
Sam Lantinga <slouken@libsdl.org>
parents: 5158
diff changeset
    21
#define WINDOW_WIDTH    256
aa339c0dca2d NDS update
Sam Lantinga <slouken@libsdl.org>
parents: 5158
diff changeset
    22
#define WINDOW_HEIGHT   (2*192)
aa339c0dca2d NDS update
Sam Lantinga <slouken@libsdl.org>
parents: 5158
diff changeset
    23
#else
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
#define WINDOW_WIDTH    640
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
#define WINDOW_HEIGHT   480
5517
aa339c0dca2d NDS update
Sam Lantinga <slouken@libsdl.org>
parents: 5158
diff changeset
    26
#endif
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
#define NUM_SPRITES     100
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
#define MAX_SPEED       1
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3596
diff changeset
    30
static SDL_Texture *sprite;
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
static SDL_Rect positions[NUM_SPRITES];
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
static SDL_Rect velocities[NUM_SPRITES];
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
static int sprite_w, sprite_h;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
static void
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
quit(int rc)
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
{
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
    exit(rc);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
}
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
int
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 4969
diff changeset
    43
LoadSprite(char *file, SDL_Renderer *renderer)
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    44
{
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    45
    SDL_Surface *temp;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    46
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
    /* Load the sprite image */
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
    temp = SDL_LoadBMP(file);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    49
    if (temp == NULL) {
6259
4aef5b792087 Have testspriteminimal use the new simple API
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    50
        fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
        return (-1);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
    }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
    sprite_w = temp->w;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
    sprite_h = temp->h;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    56
    /* Set transparent pixel as the pixel at (0,0) */
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    57
    if (temp->format->palette) {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    58
        SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    59
    } else {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
        switch (temp->format->BitsPerPixel) {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    61
        case 15:
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    62
            SDL_SetColorKey(temp, SDL_TRUE,
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    63
                            (*(Uint16 *) temp->pixels) & 0x00007FFF);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    64
            break;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    65
        case 16:
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    66
            SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
            break;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
        case 24:
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
            SDL_SetColorKey(temp, SDL_TRUE,
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
                            (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
            break;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
        case 32:
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
            SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    74
            break;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    75
        }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
    }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    77
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
    /* Create textures from the image */
5158
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5147
diff changeset
    79
    sprite = SDL_CreateTextureFromSurface(renderer, temp);
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
    if (!sprite) {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    81
        fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
        SDL_FreeSurface(temp);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    83
        return (-1);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    84
    }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    85
    SDL_FreeSurface(temp);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    86
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    87
    /* We're ready to roll. :) */
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    88
    return (0);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    89
}
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    90
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    91
void
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 4969
diff changeset
    92
MoveSprites(SDL_Window * window, SDL_Renderer * renderer, SDL_Texture * sprite)
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    93
{
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    94
    int i;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    95
    int window_w = WINDOW_WIDTH;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    96
    int window_h = WINDOW_HEIGHT;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    97
    SDL_Rect *position, *velocity;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    98
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    99
    /* Draw a gray background */
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 4969
diff changeset
   100
    SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 4969
diff changeset
   101
    SDL_RenderClear(renderer);
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   102
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   103
    /* Move the sprite, bounce at the wall, and draw */
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   104
    for (i = 0; i < NUM_SPRITES; ++i) {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   105
        position = &positions[i];
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   106
        velocity = &velocities[i];
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   107
        position->x += velocity->x;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   108
        if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   109
            velocity->x = -velocity->x;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   110
            position->x += velocity->x;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   111
        }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
        position->y += velocity->y;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   113
        if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
            velocity->y = -velocity->y;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   115
            position->y += velocity->y;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   116
        }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   117
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   118
        /* Blit the sprite onto the screen */
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 4969
diff changeset
   119
        SDL_RenderCopy(renderer, sprite, NULL, position);
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   120
    }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   121
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   122
    /* Update the screen! */
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 4969
diff changeset
   123
    SDL_RenderPresent(renderer);
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   124
}
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   125
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   126
int
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   127
main(int argc, char *argv[])
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   128
{
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3596
diff changeset
   129
    SDL_Window *window;
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 4969
diff changeset
   130
    SDL_Renderer *renderer;
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   131
    int i, done;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   132
    SDL_Event event;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   133
6259
4aef5b792087 Have testspriteminimal use the new simple API
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   134
    if (SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer) < 0) {
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 4969
diff changeset
   135
        quit(2);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 4969
diff changeset
   136
    }
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 4969
diff changeset
   137
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 4969
diff changeset
   138
    if (LoadSprite("icon.bmp", renderer) < 0) {
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   139
        quit(2);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   140
    }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   141
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   142
    /* Initialize the sprite positions */
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   143
    srand(time(NULL));
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   144
    for (i = 0; i < NUM_SPRITES; ++i) {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   145
        positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   146
        positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   147
        positions[i].w = sprite_w;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   148
        positions[i].h = sprite_h;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   149
        velocities[i].x = 0;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   150
        velocities[i].y = 0;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   151
        while (!velocities[i].x && !velocities[i].y) {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   152
            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   153
            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   154
        }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   155
    }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   156
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   157
    /* Main render loop */
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   158
    done = 0;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   159
    while (!done) {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   160
        /* Check for events */
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   161
        while (SDL_PollEvent(&event)) {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   162
            if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   163
                done = 1;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   164
            }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   165
        }
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 4969
diff changeset
   166
        MoveSprites(window, renderer, sprite);
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   167
    }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   168
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   169
    quit(0);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   170
}
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   171
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   172
/* vi: set ts=4 sw=4 expandtab: */