summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/test/testnative.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
committer3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
commit6aaedb813fa11ba0679c3051bc2eb28646b9506c (patch)
tree34acbfc9840e02cb4753e6306ea7ce978bf8b58e /src/contrib/SDL-3.2.20/test/testnative.c
parent8f228ade99dd3d4c8da9b78ade1815c9adf85c8f (diff)
Update to SDL3
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/testnative.c')
-rw-r--r--src/contrib/SDL-3.2.20/test/testnative.c234
1 files changed, 234 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/testnative.c b/src/contrib/SDL-3.2.20/test/testnative.c
new file mode 100644
index 0000000..a4b58fa
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/test/testnative.c
@@ -0,0 +1,234 @@
1/*
2 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely.
11*/
12/* Simple program: Create a native window and attach an SDL renderer */
13
14#include "testnative.h"
15
16#include <SDL3/SDL.h>
17#include <SDL3/SDL_main.h>
18#include <SDL3/SDL_test.h>
19
20#include "testutils.h"
21
22#include <stdlib.h>
23
24#define WINDOW_W 640
25#define WINDOW_H 480
26#define NUM_SPRITES 100
27#define MAX_SPEED 1
28
29static NativeWindowFactory *factories[] = {
30#ifdef TEST_NATIVE_WINDOWS
31 &WindowsWindowFactory,
32#endif
33#ifdef TEST_NATIVE_WAYLAND
34 &WaylandWindowFactory,
35#endif
36#ifdef TEST_NATIVE_X11
37 &X11WindowFactory,
38#endif
39#ifdef TEST_NATIVE_COCOA
40 &CocoaWindowFactory,
41#endif
42 NULL
43};
44static NativeWindowFactory *factory = NULL;
45static void *native_window;
46static SDL_FRect *positions, *velocities;
47static SDLTest_CommonState *state;
48
49/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
50static void
51quit(int rc)
52{
53 if (native_window && factory) {
54 factory->DestroyNativeWindow(native_window);
55 }
56 SDL_Quit();
57 SDLTest_CommonDestroyState(state);
58 /* Let 'main()' return normally */
59 if (rc != 0) {
60 exit(rc);
61 }
62}
63
64static void MoveSprites(SDL_Renderer *renderer, SDL_Texture *sprite)
65{
66 int i;
67 SDL_Rect viewport;
68 SDL_FRect *position, *velocity;
69
70 /* Query the sizes */
71 SDL_GetRenderViewport(renderer, &viewport);
72
73 /* Draw a gray background */
74 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
75 SDL_RenderClear(renderer);
76
77 /* Move the sprite, bounce at the wall, and draw */
78 for (i = 0; i < NUM_SPRITES; ++i) {
79 position = &positions[i];
80 velocity = &velocities[i];
81 position->x += velocity->x;
82 if ((position->x < 0) || (position->x >= (viewport.w - sprite->w))) {
83 velocity->x = -velocity->x;
84 position->x += velocity->x;
85 }
86 position->y += velocity->y;
87 if ((position->y < 0) || (position->y >= (viewport.h - sprite->h))) {
88 velocity->y = -velocity->y;
89 position->y += velocity->y;
90 }
91
92 /* Blit the sprite onto the screen */
93 SDL_RenderTexture(renderer, sprite, NULL, position);
94 }
95
96 /* Update the screen! */
97 SDL_RenderPresent(renderer);
98}
99
100int main(int argc, char *argv[])
101{
102 int i, done;
103 const char *driver;
104 SDL_PropertiesID props;
105 SDL_Window *window;
106 SDL_Renderer *renderer;
107 SDL_Texture *sprite;
108 int window_w, window_h;
109 SDL_Event event;
110
111 /* Initialize test framework */
112 state = SDLTest_CommonCreateState(argv, 0);
113 if (!state) {
114 return 1;
115 }
116
117 /* Parse commandline */
118 if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
119 return 1;
120 }
121
122 if (!SDL_Init(SDL_INIT_VIDEO)) {
123 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL video: %s",
124 SDL_GetError());
125 exit(1);
126 }
127 driver = SDL_GetCurrentVideoDriver();
128
129 /* Find a native window driver and create a native window */
130 for (i = 0; factories[i]; ++i) {
131 if (SDL_strcmp(driver, factories[i]->tag) == 0) {
132 factory = factories[i];
133 break;
134 }
135 }
136 if (!factory) {
137 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find native window code for %s driver",
138 driver);
139 quit(2);
140 }
141 SDL_Log("Creating native window for %s driver", driver);
142 native_window = factory->CreateNativeWindow(WINDOW_W, WINDOW_H);
143 if (!native_window) {
144 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create native window");
145 quit(3);
146 }
147 props = SDL_CreateProperties();
148 SDL_SetPointerProperty(props, "sdl2-compat.external_window", native_window);
149 SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_OPENGL_BOOLEAN, true);
150 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, WINDOW_W);
151 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, WINDOW_H);
152 window = SDL_CreateWindowWithProperties(props);
153 SDL_DestroyProperties(props);
154 if (!window) {
155 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create SDL window: %s", SDL_GetError());
156 quit(4);
157 }
158 SDL_SetWindowTitle(window, "SDL Native Window Test");
159
160 /* Create the renderer */
161 renderer = SDL_CreateRenderer(window, NULL);
162 if (!renderer) {
163 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s", SDL_GetError());
164 quit(5);
165 }
166
167 /* Clear the window, load the sprite and go! */
168 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
169 SDL_RenderClear(renderer);
170
171 sprite = LoadTexture(renderer, "icon.bmp", true, NULL, NULL);
172 if (!sprite) {
173 quit(6);
174 }
175
176 /* Allocate memory for the sprite info */
177 SDL_GetWindowSize(window, &window_w, &window_h);
178 positions = (SDL_FRect *)SDL_malloc(NUM_SPRITES * sizeof(*positions));
179 velocities = (SDL_FRect *)SDL_malloc(NUM_SPRITES * sizeof(*velocities));
180 if (!positions || !velocities) {
181 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!");
182 quit(2);
183 }
184 for (i = 0; i < NUM_SPRITES; ++i) {
185 positions[i].x = (float)SDL_rand(window_w - sprite->w);
186 positions[i].y = (float)SDL_rand(window_h - sprite->h);
187 positions[i].w = (float)sprite->w;
188 positions[i].h = (float)sprite->h;
189 velocities[i].x = 0.0f;
190 velocities[i].y = 0.0f;
191 while (velocities[i].x == 0.f && velocities[i].y == 0.f) {
192 velocities[i].x = (float)(SDL_rand(MAX_SPEED * 2 + 1) - MAX_SPEED);
193 velocities[i].y = (float)(SDL_rand(MAX_SPEED * 2 + 1) - MAX_SPEED);
194 }
195 }
196
197 /* Main render loop */
198 done = 0;
199 while (!done) {
200 /* Check for events */
201 while (SDL_PollEvent(&event)) {
202 if (state->verbose & VERBOSE_EVENT) {
203 if (((event.type != SDL_EVENT_MOUSE_MOTION) &&
204 (event.type != SDL_EVENT_FINGER_MOTION)) ||
205 (state->verbose & VERBOSE_MOTION)) {
206 SDLTest_PrintEvent(&event);
207 }
208 }
209
210 switch (event.type) {
211 case SDL_EVENT_WINDOW_EXPOSED:
212 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
213 SDL_RenderClear(renderer);
214 break;
215 case SDL_EVENT_QUIT:
216 done = 1;
217 break;
218 default:
219 break;
220 }
221 }
222 MoveSprites(renderer, sprite);
223 }
224
225 SDL_DestroyTexture(sprite);
226 SDL_DestroyRenderer(renderer);
227 SDL_DestroyWindow(window);
228 SDL_free(positions);
229 SDL_free(velocities);
230
231 quit(0);
232
233 return 0; /* to prevent compiler warning */
234}