Mercurial > sdl-ios-xcode
annotate test/testsprite2.c @ 1724:6c63fc2bd986 SDL-1.3
Proof of concept done - Win32 GDI implementation mostly complete.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Thu, 06 Jul 2006 07:17:11 +0000 |
parents | 931d111e737a |
children | 98a3207ddde8 |
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 <stdlib.h> |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
4 #include <time.h> |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
5 |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
6 #include "SDL.h" |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
7 |
1724
6c63fc2bd986
Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents:
1712
diff
changeset
|
8 #define NUM_WINDOWS 4 |
1706
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
9 #define WINDOW_W 640 |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
10 #define WINDOW_H 480 |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
11 #define NUM_SPRITES 100 |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
12 #define MAX_SPEED 1 |
1724
6c63fc2bd986
Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents:
1712
diff
changeset
|
13 #define BACKGROUND 0x00FFFFFF |
1706
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
14 |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
15 static int num_windows; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
16 static int num_sprites; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
17 static SDL_WindowID *windows; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
18 static SDL_TextureID *sprites; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
19 static SDL_Rect *positions; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
20 static SDL_Rect *velocities; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
21 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
|
22 |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
23 /* 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
|
24 static void |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
25 quit(int rc) |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
26 { |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
27 if (windows) { |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
28 SDL_free(windows); |
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 (sprites) { |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
31 SDL_free(sprites); |
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 (positions) { |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
34 SDL_free(positions); |
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 (velocities) { |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
37 SDL_free(velocities); |
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 SDL_Quit(); |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
40 exit(rc); |
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 |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
43 int |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
44 LoadSprite(char *file) |
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 i; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
47 SDL_Surface *temp; |
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 /* Load the sprite image */ |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
50 temp = SDL_LoadBMP(file); |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
51 if (temp == NULL) { |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
52 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
|
53 return (-1); |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
54 } |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
55 sprite_w = temp->w; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
56 sprite_h = temp->h; |
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 /* 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
|
59 if (temp->format->palette) { |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
60 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
|
61 } |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
62 |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
63 /* 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
|
64 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
|
65 SDL_SelectRenderer(windows[i]); |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
66 sprites[i] = |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
67 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
|
68 if (!sprites[i]) { |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
69 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
|
70 SDL_FreeSurface(temp); |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
71 return (-1); |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
72 } |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
73 } |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
74 SDL_FreeSurface(temp); |
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 /* 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
|
77 return (0); |
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 |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
80 void |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
81 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
|
82 { |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
83 int i, n; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
84 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
|
85 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
|
86 |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
87 SDL_SelectRenderer(window); |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
88 |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
89 /* Query the sizes */ |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
90 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
|
91 |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
92 /* 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
|
93 n = 0; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
94 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
|
95 position = &positions[i]; |
1724
6c63fc2bd986
Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents:
1712
diff
changeset
|
96 SDL_RenderFill(position, BACKGROUND); |
6c63fc2bd986
Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents:
1712
diff
changeset
|
97 } |
6c63fc2bd986
Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents:
1712
diff
changeset
|
98 for (i = 0; i < num_sprites; ++i) { |
6c63fc2bd986
Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents:
1712
diff
changeset
|
99 position = &positions[i]; |
1706
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
100 velocity = &velocities[i]; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
101 position->x += velocity->x; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
102 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
|
103 velocity->x = -velocity->x; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
104 position->x += velocity->x; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
105 } |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
106 position->y += velocity->y; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
107 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
|
108 velocity->y = -velocity->y; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
109 position->y += velocity->y; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
110 } |
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 /* 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
|
113 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
|
114 SDL_TextureScaleMode_None); |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
115 } |
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 /* Update the screen! */ |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
118 SDL_RenderPresent(); |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
119 } |
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 int |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
122 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
|
123 { |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
124 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
|
125 int i, done; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
126 SDL_Event event; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
127 Uint32 then, now, frames; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
128 |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
129 /* Initialize SDL */ |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
130 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
|
131 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
|
132 return (1); |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
133 } |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
134 |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
135 num_windows = NUM_WINDOWS; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
136 num_sprites = NUM_SPRITES; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
137 window_w = WINDOW_W; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
138 window_h = WINDOW_H; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
139 while (argc > 1) { |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
140 --argc; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
141 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
|
142 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
|
143 --argc; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
144 } 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
|
145 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
|
146 --argc; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
147 } 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
|
148 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
|
149 } else { |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
150 fprintf(stderr, |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
151 "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
|
152 quit(1); |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
153 } |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
154 } |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
155 |
1707
57ce47f033a5
Passing NULL to SDL_SetDisplayMode() will set the desktop mode.
Sam Lantinga <slouken@libsdl.org>
parents:
1706
diff
changeset
|
156 /* 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
|
157 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
|
158 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
|
159 quit(2); |
1706
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
160 } |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
161 |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
162 /* 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
|
163 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
|
164 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
|
165 if (!windows || !sprites) { |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
166 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
|
167 quit(2); |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
168 } |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
169 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
|
170 char title[32]; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
171 |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
172 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
|
173 windows[i] = |
1724
6c63fc2bd986
Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents:
1712
diff
changeset
|
174 SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, |
6c63fc2bd986
Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents:
1712
diff
changeset
|
175 SDL_WINDOWPOS_UNDEFINED, window_w, window_h, |
1706
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
176 SDL_WINDOW_SHOWN); |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
177 if (!windows[i]) { |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
178 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
|
179 quit(2); |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
180 } |
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 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
|
183 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
|
184 quit(2); |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
185 } |
1724
6c63fc2bd986
Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents:
1712
diff
changeset
|
186 SDL_RenderFill(NULL, BACKGROUND); |
1706
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) { |
1724
6c63fc2bd986
Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents:
1712
diff
changeset
|
222 case SDL_WINDOWEVENT: |
6c63fc2bd986
Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents:
1712
diff
changeset
|
223 switch (event.window.event) { |
6c63fc2bd986
Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents:
1712
diff
changeset
|
224 case SDL_WINDOWEVENT_EXPOSED: |
6c63fc2bd986
Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents:
1712
diff
changeset
|
225 SDL_SelectRenderer(event.window.windowID); |
6c63fc2bd986
Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents:
1712
diff
changeset
|
226 SDL_RenderFill(NULL, BACKGROUND); |
6c63fc2bd986
Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents:
1712
diff
changeset
|
227 break; |
6c63fc2bd986
Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents:
1712
diff
changeset
|
228 case SDL_WINDOWEVENT_CLOSE: |
6c63fc2bd986
Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents:
1712
diff
changeset
|
229 done = 1; |
6c63fc2bd986
Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents:
1712
diff
changeset
|
230 break; |
6c63fc2bd986
Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents:
1712
diff
changeset
|
231 } |
6c63fc2bd986
Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents:
1712
diff
changeset
|
232 break; |
1706
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
233 case SDL_KEYDOWN: |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
234 /* 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
|
235 case SDL_QUIT: |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
236 done = 1; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
237 break; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
238 default: |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
239 break; |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
240 } |
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 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
|
243 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
|
244 } |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
245 } |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
246 |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
247 /* 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
|
248 now = SDL_GetTicks(); |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
249 if (now > then) { |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
250 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
|
251 ((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
|
252 } |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
253 quit(0); |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
254 } |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
255 |
1577404809f0
Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
256 /* vi: set ts=4 sw=4 expandtab: */ |