comparison test/testnative.c @ 3057:089a77aebb7d

Added test program for SDL_CreateWindowFrom() Make sure OpenGL library is loaded before working with OpenGL windows, even those created with SDL_CreateWindowFrom()
author Sam Lantinga <slouken@libsdl.org>
date Mon, 09 Feb 2009 05:32:12 +0000
parents
children 4cf533f434d8
comparison
equal deleted inserted replaced
3056:a434fe6360df 3057:089a77aebb7d
1 /* Simple program: Create a native window and attach an SDL renderer */
2
3 #include "testnative.h"
4
5 #define WINDOW_W 640
6 #define WINDOW_H 480
7 #define NUM_SPRITES 100
8 #define MAX_SPEED 1
9
10 static NativeWindowFactory *factories[] = {
11 #ifdef TEST_NATIVE_WIN32
12 &Win32WindowFactory,
13 #endif
14 #ifdef TEST_NATIVE_X11
15 &X11WindowFactory,
16 #endif
17 #ifdef TEST_NATIVE_COCOA
18 &CocoaWindowFactory,
19 #endif
20 NULL
21 };
22 static NativeWindowFactory *factory = NULL;
23 static void *native_window;
24 static SDL_Rect *positions, *velocities;
25
26 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
27 static void
28 quit(int rc)
29 {
30 SDL_VideoQuit();
31 if (native_window) {
32 factory->DestroyWindow(native_window);
33 }
34 exit(rc);
35 }
36
37 SDL_TextureID
38 LoadSprite(SDL_WindowID window, char *file)
39 {
40 SDL_Surface *temp;
41 SDL_TextureID sprite;
42
43 /* Load the sprite image */
44 temp = SDL_LoadBMP(file);
45 if (temp == NULL) {
46 fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
47 return 0;
48 }
49
50 /* Set transparent pixel as the pixel at (0,0) */
51 if (temp->format->palette) {
52 SDL_SetColorKey(temp, SDL_SRCCOLORKEY, *(Uint8 *) temp->pixels);
53 }
54
55 /* Create textures from the image */
56 SDL_SelectRenderer(window);
57 sprite = SDL_CreateTextureFromSurface(0, temp);
58 if (!sprite) {
59 fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
60 SDL_FreeSurface(temp);
61 return 0;
62 }
63 SDL_FreeSurface(temp);
64
65 /* We're ready to roll. :) */
66 return sprite;
67 }
68
69 void
70 MoveSprites(SDL_WindowID window, SDL_TextureID sprite)
71 {
72 int i, n;
73 int window_w, window_h;
74 int sprite_w, sprite_h;
75 SDL_Rect *position, *velocity;
76
77 SDL_SelectRenderer(window);
78
79 /* Query the sizes */
80 SDL_GetWindowSize(window, &window_w, &window_h);
81 SDL_QueryTexture(sprite, NULL, NULL, &sprite_w, &sprite_h);
82
83 /* Move the sprite, bounce at the wall, and draw */
84 n = 0;
85 SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
86 SDL_RenderFill(NULL);
87 for (i = 0; i < NUM_SPRITES; ++i) {
88 position = &positions[i];
89 velocity = &velocities[i];
90 position->x += velocity->x;
91 if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
92 velocity->x = -velocity->x;
93 position->x += velocity->x;
94 }
95 position->y += velocity->y;
96 if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
97 velocity->y = -velocity->y;
98 position->y += velocity->y;
99 }
100
101 /* Blit the sprite onto the screen */
102 SDL_RenderCopy(sprite, NULL, position);
103 }
104
105 /* Update the screen! */
106 SDL_RenderPresent();
107 }
108
109 int
110 main(int argc, char *argv[])
111 {
112 int i, done;
113 const char *driver;
114 SDL_WindowID window;
115 SDL_TextureID sprite;
116 int window_w, window_h;
117 int sprite_w, sprite_h;
118 SDL_Event event;
119
120 if (SDL_VideoInit(NULL, 0) < 0) {
121 fprintf(stderr, "Couldn't initialize SDL video: %s\n",
122 SDL_GetError());
123 exit(1);
124 }
125 driver = SDL_GetCurrentVideoDriver();
126
127 /* Find a native window driver and create a native window */
128 for (i = 0; factories[i]; ++i) {
129 if (SDL_strcmp(driver, factories[i]->tag) == 0) {
130 factory = factories[i];
131 break;
132 }
133 }
134 if (!factory) {
135 fprintf(stderr, "Couldn't find native window code for %s driver\n",
136 driver);
137 quit(2);
138 }
139 printf("Creating native window for %s driver\n", driver);
140 native_window = factory->CreateWindow(WINDOW_W, WINDOW_H);
141 if (!native_window) {
142 fprintf(stderr, "Couldn't create native window\n");
143 quit(3);
144 }
145 window = SDL_CreateWindowFrom(native_window);
146 if (!window) {
147 fprintf(stderr, "Couldn't create SDL window: %s\n", SDL_GetError());
148 quit(4);
149 }
150 SDL_SetWindowTitle(window, "SDL Native Window Test");
151
152 /* Create the renderer */
153 if (SDL_CreateRenderer(window, -1, 0) < 0) {
154 fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
155 quit(5);
156 }
157
158 /* Clear the window, load the sprite and go! */
159 SDL_SelectRenderer(window);
160 SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
161 SDL_RenderFill(NULL);
162
163 sprite = LoadSprite(window, "icon.bmp");
164 if (!sprite) {
165 quit(6);
166 }
167
168 /* Allocate memory for the sprite info */
169 SDL_GetWindowSize(window, &window_w, &window_h);
170 SDL_QueryTexture(sprite, NULL, NULL, &sprite_w, &sprite_h);
171 positions = (SDL_Rect *) SDL_malloc(NUM_SPRITES * sizeof(SDL_Rect));
172 velocities = (SDL_Rect *) SDL_malloc(NUM_SPRITES * sizeof(SDL_Rect));
173 if (!positions || !velocities) {
174 fprintf(stderr, "Out of memory!\n");
175 quit(2);
176 }
177 srand(time(NULL));
178 for (i = 0; i < NUM_SPRITES; ++i) {
179 positions[i].x = rand() % (window_w - sprite_w);
180 positions[i].y = rand() % (window_h - sprite_h);
181 positions[i].w = sprite_w;
182 positions[i].h = sprite_h;
183 velocities[i].x = 0;
184 velocities[i].y = 0;
185 while (!velocities[i].x && !velocities[i].y) {
186 velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
187 velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
188 }
189 }
190
191 /* Main render loop */
192 done = 0;
193 while (!done) {
194 /* Check for events */
195 while (SDL_PollEvent(&event)) {
196 switch (event.type) {
197 case SDL_WINDOWEVENT:
198 switch (event.window.event) {
199 case SDL_WINDOWEVENT_EXPOSED:
200 SDL_SelectRenderer(event.window.windowID);
201 SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
202 SDL_RenderFill(NULL);
203 break;
204 }
205 break;
206 case SDL_QUIT:
207 done = 1;
208 break;
209 default:
210 break;
211 }
212 }
213 MoveSprites(window, sprite);
214 }
215
216 quit(0);
217 }
218
219 /* vi: set ts=4 sw=4 expandtab: */