annotate test/testsprite2.c @ 1725:98a3207ddde8 SDL-1.3

Implemented Win32 video mode support
author Sam Lantinga <slouken@libsdl.org>
date Fri, 07 Jul 2006 08:05:39 +0000
parents 6c63fc2bd986
children 0b1070f2f94d
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;
1725
98a3207ddde8 Implemented Win32 video mode support
Sam Lantinga <slouken@libsdl.org>
parents: 1724
diff changeset
125 Uint32 window_flags = SDL_WINDOW_SHOWN;
98a3207ddde8 Implemented Win32 video mode support
Sam Lantinga <slouken@libsdl.org>
parents: 1724
diff changeset
126 SDL_DisplayMode *mode, fullscreen_mode;
1706
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 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
143 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
144 --argc;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
145 } 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
146 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
147 --argc;
1725
98a3207ddde8 Implemented Win32 video mode support
Sam Lantinga <slouken@libsdl.org>
parents: 1724
diff changeset
148 } else if (strcmp(argv[argc - 1], "-fullscreen") == 0) {
98a3207ddde8 Implemented Win32 video mode support
Sam Lantinga <slouken@libsdl.org>
parents: 1724
diff changeset
149 num_windows = 1;
98a3207ddde8 Implemented Win32 video mode support
Sam Lantinga <slouken@libsdl.org>
parents: 1724
diff changeset
150 window_flags |= SDL_WINDOW_FULLSCREEN;
98a3207ddde8 Implemented Win32 video mode support
Sam Lantinga <slouken@libsdl.org>
parents: 1724
diff changeset
151 --argc;
1706
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
152 } 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
153 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
154 } else {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
155 fprintf(stderr,
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
156 "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
157 quit(1);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
158 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
159 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
160
1725
98a3207ddde8 Implemented Win32 video mode support
Sam Lantinga <slouken@libsdl.org>
parents: 1724
diff changeset
161 if (window_flags & SDL_WINDOW_FULLSCREEN) {
98a3207ddde8 Implemented Win32 video mode support
Sam Lantinga <slouken@libsdl.org>
parents: 1724
diff changeset
162 SDL_zero(fullscreen_mode);
98a3207ddde8 Implemented Win32 video mode support
Sam Lantinga <slouken@libsdl.org>
parents: 1724
diff changeset
163 fullscreen_mode.w = window_w;
98a3207ddde8 Implemented Win32 video mode support
Sam Lantinga <slouken@libsdl.org>
parents: 1724
diff changeset
164 fullscreen_mode.h = window_h;
98a3207ddde8 Implemented Win32 video mode support
Sam Lantinga <slouken@libsdl.org>
parents: 1724
diff changeset
165 mode = &fullscreen_mode;
98a3207ddde8 Implemented Win32 video mode support
Sam Lantinga <slouken@libsdl.org>
parents: 1724
diff changeset
166 } else {
98a3207ddde8 Implemented Win32 video mode support
Sam Lantinga <slouken@libsdl.org>
parents: 1724
diff changeset
167 /* Set the desktop mode, we don't care what it is */
98a3207ddde8 Implemented Win32 video mode support
Sam Lantinga <slouken@libsdl.org>
parents: 1724
diff changeset
168 mode = NULL;
98a3207ddde8 Implemented Win32 video mode support
Sam Lantinga <slouken@libsdl.org>
parents: 1724
diff changeset
169 }
98a3207ddde8 Implemented Win32 video mode support
Sam Lantinga <slouken@libsdl.org>
parents: 1724
diff changeset
170 if (SDL_SetDisplayMode(mode) < 0) {
1707
57ce47f033a5 Passing NULL to SDL_SetDisplayMode() will set the desktop mode.
Sam Lantinga <slouken@libsdl.org>
parents: 1706
diff changeset
171 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
172 quit(2);
1706
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
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
175 /* 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
176 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
177 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
178 if (!windows || !sprites) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
179 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
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 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
183 char title[32];
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
184
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
185 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
186 windows[i] =
1724
6c63fc2bd986 Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents: 1712
diff changeset
187 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
188 SDL_WINDOWPOS_UNDEFINED, window_w, window_h,
1725
98a3207ddde8 Implemented Win32 video mode support
Sam Lantinga <slouken@libsdl.org>
parents: 1724
diff changeset
189 window_flags);
1706
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
190 if (!windows[i]) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
191 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
192 quit(2);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
193 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
194
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
195 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
196 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
197 quit(2);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
198 }
1724
6c63fc2bd986 Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents: 1712
diff changeset
199 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
200 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
201 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
202 quit(2);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
203 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
204
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
205 /* 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
206 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
207 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
208 if (!positions || !velocities) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
209 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
210 quit(2);
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 srand(time(NULL));
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
213 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
214 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
215 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
216 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
217 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
218 velocities[i].x = 0;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
219 velocities[i].y = 0;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
220 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
221 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
222 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
223 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
224 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
225
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
226 /* 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
227 frames = 0;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
228 then = SDL_GetTicks();
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
229 done = 0;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
230 while (!done) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
231 /* Check for events */
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
232 ++frames;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
233 while (SDL_PollEvent(&event)) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
234 switch (event.type) {
1724
6c63fc2bd986 Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents: 1712
diff changeset
235 case SDL_WINDOWEVENT:
6c63fc2bd986 Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents: 1712
diff changeset
236 switch (event.window.event) {
6c63fc2bd986 Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents: 1712
diff changeset
237 case SDL_WINDOWEVENT_EXPOSED:
6c63fc2bd986 Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents: 1712
diff changeset
238 SDL_SelectRenderer(event.window.windowID);
6c63fc2bd986 Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents: 1712
diff changeset
239 SDL_RenderFill(NULL, BACKGROUND);
6c63fc2bd986 Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents: 1712
diff changeset
240 break;
6c63fc2bd986 Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents: 1712
diff changeset
241 case SDL_WINDOWEVENT_CLOSE:
6c63fc2bd986 Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents: 1712
diff changeset
242 done = 1;
6c63fc2bd986 Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents: 1712
diff changeset
243 break;
6c63fc2bd986 Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents: 1712
diff changeset
244 }
6c63fc2bd986 Proof of concept done - Win32 GDI implementation mostly complete.
Sam Lantinga <slouken@libsdl.org>
parents: 1712
diff changeset
245 break;
1706
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
246 case SDL_KEYDOWN:
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
247 /* 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
248 case SDL_QUIT:
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
249 done = 1;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
250 break;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
251 default:
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
252 break;
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
253 }
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 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
256 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
257 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
258 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
259
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
260 /* 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
261 now = SDL_GetTicks();
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
262 if (now > then) {
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
263 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
264 ((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
265 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
266 quit(0);
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
267 }
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
268
1577404809f0 Added a multi-window version of testsprite using the new API.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
269 /* vi: set ts=4 sw=4 expandtab: */