annotate test/testspriteminimal.c @ 4769:83f9b95da263

Added the standard headers and footers that make SDL_shape.h and SDL_shape.c integrate properly into the build.
author Eli Gottlieb <eligottlieb@gmail.com>
date Thu, 27 May 2010 16:44:35 -0400
parents 64ce267332c6
children d2247eb39fab
rev   line source
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1 /* 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
2
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
3 #include <stdlib.h>
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
4 #include <stdio.h>
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
5 #include <time.h>
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
6
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
7 #include "SDL_events.h"
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
8 #include "SDL_video.h"
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
9
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
10 #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
11 #define WINDOW_HEIGHT 480
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
12 #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
13 #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
14
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
15 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
16 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
17 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
18 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
19
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
20 /* 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
21 static void
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
22 quit(int rc)
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
23 {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
24 exit(rc);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
25 }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
26
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
27 int
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
28 LoadSprite(char *file)
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
29 {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
30 SDL_Surface *temp;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
31
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
32 /* 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
33 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
34 if (temp == NULL) {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
35 fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
36 return (-1);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
37 }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
38 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
39 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
40
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
41 /* 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
42 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
43 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
44 } else {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
45 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
46 case 15:
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
47 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
48 (*(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
49 break;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
50 case 16:
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
51 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
52 break;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
53 case 24:
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
54 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
55 (*(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
56 break;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
57 case 32:
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, *(Uint32 *) temp->pixels);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
59 break;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
60 }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
61 }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
62
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
63 /* Create textures from the image */
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
64 sprite = SDL_CreateTextureFromSurface(0, temp);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
65 if (!sprite) {
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, 0, 0);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
67 sprite = SDL_CreateTextureFromSurface(0, temp);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
68 }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
69 if (!sprite) {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
70 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
71 SDL_FreeSurface(temp);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
72 return (-1);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
73 }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
74 SDL_FreeSurface(temp);
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 /* 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
77 return (0);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
78 }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
79
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
80 void
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
81 MoveSprites(SDL_Window * window, 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
82 {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
83 int i;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
84 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
85 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
86 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
87
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
88 /* Draw a gray background */
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
89 SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
3596
f638ded38b8a Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
90 SDL_RenderClear();
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
91
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
92 /* 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
93 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
94 position = &positions[i];
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
95 velocity = &velocities[i];
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
96 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
97 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
98 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
99 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
100 }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
101 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
102 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
103 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
104 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
105 }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
106
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
107 /* Blit the sprite onto the screen */
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
108 SDL_RenderCopy(sprite, NULL, position);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
109 }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
110
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
111 /* Update the screen! */
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
112 SDL_RenderPresent();
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
113 }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
114
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
115 int
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
116 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
117 {
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
118 SDL_Window *window;
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
119 int i, done;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
120 SDL_Event event;
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 window = SDL_CreateWindow("Happy Smileys",
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
123 SDL_WINDOWPOS_UNDEFINED,
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
124 SDL_WINDOWPOS_UNDEFINED,
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
125 WINDOW_WIDTH, WINDOW_HEIGHT,
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
126 SDL_WINDOW_SHOWN);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
127 if (!window) {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
128 quit(2);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
129 }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
130
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
131 if (LoadSprite("icon.bmp") < 0) {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
132 quit(2);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
133 }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
134
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
135 /* 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
136 srand(time(NULL));
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
137 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
138 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
139 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
140 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
141 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
142 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
143 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
144 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
145 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
146 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
147 }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
148 }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
149
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
150 /* Main render loop */
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
151 done = 0;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
152 while (!done) {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
153 /* Check for events */
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
154 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
155 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
156 done = 1;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
157 }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
158 }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
159 MoveSprites(window, sprite);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
160 }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
161
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
162 quit(0);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
163 }
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 /* vi: set ts=4 sw=4 expandtab: */