annotate test/testsprite2.c @ 1707:57ce47f033a5 SDL-1.3

Passing NULL to SDL_SetDisplayMode() will set the desktop mode.
author Sam Lantinga <slouken@libsdl.org>
date Sat, 24 Jun 2006 17:01:29 +0000
parents 1577404809f0
children 931d111e737a
rev   line source
1706
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1 /* Simple program: Move N sprites around on the screen as fast as possible */
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
2
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
3 #include <stdio.h>
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
4 #include <stdlib.h>
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
5 #include <string.h>
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
6 #include <ctype.h>
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
7 #include <math.h>
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
8 #include <time.h>
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
9
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
10 #include "SDL.h"
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
11
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
12 #define NUM_WINDOWS 2
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
13 #define WINDOW_W 640
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
14 #define WINDOW_H 480
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
15 #define NUM_SPRITES 100
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
16 #define MAX_SPEED 1
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
17
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
18 static int num_windows;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
19 static int num_sprites;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
20 static SDL_WindowID *windows;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
21 static SDL_TextureID *sprites;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
22 static SDL_Rect *positions;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
23 static SDL_Rect *velocities;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
24 static int sprite_w, sprite_h;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
25
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
26 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
27 static void
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
28 quit(int rc)
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
29 {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
30 if (windows) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
31 SDL_free(windows);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
32 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
33 if (sprites) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
34 SDL_free(sprites);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
35 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
36 if (positions) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
37 SDL_free(positions);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
38 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
39 if (velocities) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
40 SDL_free(velocities);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
41 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
42 SDL_Quit();
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
43 exit(rc);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
44 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
45
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
46 int
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
47 LoadSprite(char *file)
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
48 {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
49 int i;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
50 SDL_Surface *temp;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
51
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
52 /* Load the sprite image */
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
53 temp = SDL_LoadBMP(file);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
54 if (temp == NULL) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
55 fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
56 return (-1);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
57 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
58 sprite_w = temp->w;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
59 sprite_h = temp->h;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
60
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
61 /* Set transparent pixel as the pixel at (0,0) */
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
62 if (temp->format->palette) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
63 SDL_SetColorKey(temp, SDL_SRCCOLORKEY, *(Uint8 *) temp->pixels);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
64 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
65
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
66 /* Create textures from the image */
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
67 for (i = 0; i < num_windows; ++i) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
68 SDL_SelectRenderer(windows[i]);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
69 sprites[i] =
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
70 SDL_CreateTextureFromSurface(0, SDL_TextureAccess_Remote, temp);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
71 if (!sprites[i]) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
72 fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
73 SDL_FreeSurface(temp);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
74 return (-1);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
75 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
76 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
77 SDL_FreeSurface(temp);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
78
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
79 /* We're ready to roll. :) */
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
80 return (0);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
81 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
82
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
83 void
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
84 MoveSprites(SDL_WindowID window, SDL_TextureID sprite)
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
85 {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
86 int i, n;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
87 int window_w, window_h;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
88 SDL_Rect area, *position, *velocity;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
89
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
90 SDL_SelectRenderer(window);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
91
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
92 SDL_RenderFill(NULL, 0);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
93
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
94 /* Query the sizes */
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
95 SDL_GetWindowSize(window, &window_w, &window_h);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
96
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
97 /* Move the sprite, bounce at the wall, and draw */
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
98 n = 0;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
99 for (i = 0; i < num_sprites; ++i) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
100 position = &positions[i];
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
101 velocity = &velocities[i];
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
102 position->x += velocity->x;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
103 if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
104 velocity->x = -velocity->x;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
105 position->x += velocity->x;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
106 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
107 position->y += velocity->y;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
108 if ((position->y < 0) || (position->y >= (window_h - sprite_w))) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
109 velocity->y = -velocity->y;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
110 position->y += velocity->y;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
111 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
112
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
113 /* Blit the sprite onto the screen */
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
114 SDL_RenderCopy(sprite, NULL, position, SDL_TextureBlendMode_Mask,
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
115 SDL_TextureScaleMode_None);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
116 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
117
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
118 /* Update the screen! */
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
119 SDL_RenderPresent();
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
120 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
121
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
122 int
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
123 main(int argc, char *argv[])
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
124 {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
125 const SDL_DisplayMode *current_mode;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
126 int window_w, window_h;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
127 int i, done;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
128 SDL_Event event;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
129 Uint32 then, now, frames;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
130
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
131 /* Initialize SDL */
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
132 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
133 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
134 return (1);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
135 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
136
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
137 num_windows = NUM_WINDOWS;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
138 num_sprites = NUM_SPRITES;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
139 window_w = WINDOW_W;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
140 window_h = WINDOW_H;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
141 while (argc > 1) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
142 --argc;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
143 if (strcmp(argv[argc - 1], "-width") == 0) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
144 window_w = atoi(argv[argc]);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
145 --argc;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
146 } else if (strcmp(argv[argc - 1], "-height") == 0) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
147 window_h = atoi(argv[argc]);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
148 --argc;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
149 } else if (isdigit(argv[argc][0])) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
150 num_sprites = atoi(argv[argc]);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
151 } else {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
152 fprintf(stderr,
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
153 "Usage: %s [-width] [-height] [numsprites]\n", argv[0]);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
154 quit(1);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
155 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
156 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
157
1707
57ce47f033a5 Passing NULL to SDL_SetDisplayMode() will set the desktop mode.
Sam Lantinga <slouken@libsdl.org>
parents: 1706
diff changeset
158 /* Set the desktop mode, we don't care what it is */
57ce47f033a5 Passing NULL to SDL_SetDisplayMode() will set the desktop mode.
Sam Lantinga <slouken@libsdl.org>
parents: 1706
diff changeset
159 if (SDL_SetDisplayMode(NULL) < 0) {
57ce47f033a5 Passing NULL to SDL_SetDisplayMode() will set the desktop mode.
Sam Lantinga <slouken@libsdl.org>
parents: 1706
diff changeset
160 fprintf(stderr, "Couldn't set display mode: %s\n", SDL_GetError());
57ce47f033a5 Passing NULL to SDL_SetDisplayMode() will set the desktop mode.
Sam Lantinga <slouken@libsdl.org>
parents: 1706
diff changeset
161 quit(2);
1706
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
162 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
163
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
164 /* Create the windows, initialize the renderers, and load the textures */
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
165 windows = (SDL_WindowID *) SDL_malloc(num_windows * sizeof(*windows));
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
166 sprites = (SDL_TextureID *) SDL_malloc(num_windows * sizeof(*sprites));
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
167 if (!windows || !sprites) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
168 fprintf(stderr, "Out of memory!\n");
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
169 quit(2);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
170 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
171 for (i = 0; i < num_windows; ++i) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
172 char title[32];
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
173
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
174 SDL_snprintf(title, sizeof(title), "testsprite %d", i + 1);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
175 windows[i] =
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
176 SDL_CreateWindow(title, -1, -1, window_w, window_h,
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
177 SDL_WINDOW_SHOWN);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
178 if (!windows[i]) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
179 fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
180 quit(2);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
181 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
182
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
183 if (SDL_CreateRenderer(windows[i], -1, 0) < 0) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
184 fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
185 quit(2);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
186 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
187 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
188 if (LoadSprite("icon.bmp") < 0) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
189 quit(2);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
190 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
191
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
192 /* Allocate memory for the sprite info */
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
193 positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
194 velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
195 if (!positions || !velocities) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
196 fprintf(stderr, "Out of memory!\n");
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
197 quit(2);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
198 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
199 srand(time(NULL));
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
200 for (i = 0; i < num_sprites; ++i) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
201 positions[i].x = rand() % (window_w - sprite_w);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
202 positions[i].y = rand() % (window_h - sprite_h);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
203 positions[i].w = sprite_w;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
204 positions[i].h = sprite_h;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
205 velocities[i].x = 0;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
206 velocities[i].y = 0;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
207 while (!velocities[i].x && !velocities[i].y) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
208 velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
209 velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
210 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
211 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
212
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
213 /* Loop, blitting sprites and waiting for a keystroke */
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
214 frames = 0;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
215 then = SDL_GetTicks();
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
216 done = 0;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
217 while (!done) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
218 /* Check for events */
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
219 ++frames;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
220 while (SDL_PollEvent(&event)) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
221 switch (event.type) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
222 case SDL_KEYDOWN:
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
223 /* Any keypress quits the app... */
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
224 case SDL_QUIT:
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
225 done = 1;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
226 break;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
227 default:
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
228 break;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
229 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
230 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
231 for (i = 0; i < num_windows; ++i) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
232 MoveSprites(windows[i], sprites[i]);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
233 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
234 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
235
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
236 /* Print out some timing information */
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
237 now = SDL_GetTicks();
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
238 if (now > then) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
239 printf("%2.2f frames per second\n",
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
240 ((double) frames * 1000) / (now - then));
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
241 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
242 quit(0);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
243 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
244
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
245 /* vi: set ts=4 sw=4 expandtab: */