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