14
#include "testnative.h"
17
#include <SDL3/SDL_main.h>
18
#include <SDL3/SDL_test.h>
26
#define NUM_SPRITES 100
29
static NativeWindowFactory *factories[] = {
30
#ifdef TEST_NATIVE_WINDOWS
31
&WindowsWindowFactory,
33
#ifdef TEST_NATIVE_WAYLAND
34
&WaylandWindowFactory,
39
#ifdef TEST_NATIVE_COCOA
44
static NativeWindowFactory *factory = NULL;
45
static void *native_window;
46
static SDL_FRect *positions, *velocities;
47
static SDLTest_CommonState *state;
53
if (native_window && factory) {
54
factory->DestroyNativeWindow(native_window);
57
SDLTest_CommonDestroyState(state);
64
static void MoveSprites(SDL_Renderer *renderer, SDL_Texture *sprite)
66
float sprite_w, sprite_h;
69
SDL_FRect *position, *velocity;
72
SDL_GetRenderViewport(renderer, &viewport);
73
SDL_GetTextureSize(sprite, &sprite_w, &sprite_h);
76
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
77
SDL_RenderClear(renderer);
80
for (i = 0; i < NUM_SPRITES; ++i) {
81
position = &positions[i];
82
velocity = &velocities[i];
83
position->x += velocity->x;
84
if ((position->x < 0) || (position->x >= (viewport.w - sprite_w))) {
85
velocity->x = -velocity->x;
86
position->x += velocity->x;
88
position->y += velocity->y;
89
if ((position->y < 0) || (position->y >= (viewport.h - sprite_h))) {
90
velocity->y = -velocity->y;
91
position->y += velocity->y;
95
SDL_RenderTexture(renderer, sprite, NULL, position);
99
SDL_RenderPresent(renderer);
102
int main(int argc, char *argv[])
106
SDL_PropertiesID props;
108
SDL_Renderer *renderer;
110
int window_w, window_h;
111
float sprite_w, sprite_h;
115
state = SDLTest_CommonCreateState(argv, 0);
121
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
124
if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
128
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
129
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL video: %s\n",
133
driver = SDL_GetCurrentVideoDriver();
136
for (i = 0; factories[i]; ++i) {
137
if (SDL_strcmp(driver, factories[i]->tag) == 0) {
138
factory = factories[i];
143
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find native window code for %s driver\n",
147
SDL_Log("Creating native window for %s driver\n", driver);
148
native_window = factory->CreateNativeWindow(WINDOW_W, WINDOW_H);
149
if (!native_window) {
150
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create native window\n");
153
props = SDL_CreateProperties();
154
SDL_SetPointerProperty(props, "sdl2-compat.external_window", native_window);
155
SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_OPENGL_BOOLEAN, SDL_TRUE);
156
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, WINDOW_W);
157
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, WINDOW_H);
158
window = SDL_CreateWindowWithProperties(props);
159
SDL_DestroyProperties(props);
161
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create SDL window: %s\n", SDL_GetError());
164
SDL_SetWindowTitle(window, "SDL Native Window Test");
167
renderer = SDL_CreateRenderer(window, NULL);
169
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
174
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
175
SDL_RenderClear(renderer);
177
sprite = LoadTexture(renderer, "icon.bmp", SDL_TRUE, NULL, NULL);
183
SDL_GetWindowSize(window, &window_w, &window_h);
184
SDL_GetTextureSize(sprite, &sprite_w, &sprite_h);
185
positions = (SDL_FRect *)SDL_malloc(NUM_SPRITES * sizeof(*positions));
186
velocities = (SDL_FRect *)SDL_malloc(NUM_SPRITES * sizeof(*velocities));
187
if (!positions || !velocities) {
188
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n");
191
for (i = 0; i < NUM_SPRITES; ++i) {
192
positions[i].x = (float)(SDL_rand(window_w - (int)sprite_w));
193
positions[i].y = (float)(SDL_rand(window_h - (int)sprite_h));
194
positions[i].w = sprite_w;
195
positions[i].h = sprite_h;
196
velocities[i].x = 0.0f;
197
velocities[i].y = 0.0f;
198
while (velocities[i].x == 0.f && velocities[i].y == 0.f) {
199
velocities[i].x = (float)(SDL_rand(MAX_SPEED * 2 + 1) - MAX_SPEED);
200
velocities[i].y = (float)(SDL_rand(MAX_SPEED * 2 + 1) - MAX_SPEED);
208
while (SDL_PollEvent(&event)) {
209
if (state->verbose & VERBOSE_EVENT) {
210
if (((event.type != SDL_EVENT_MOUSE_MOTION) &&
211
(event.type != SDL_EVENT_FINGER_MOTION)) ||
212
(state->verbose & VERBOSE_MOTION)) {
213
SDLTest_PrintEvent(&event);
217
switch (event.type) {
218
case SDL_EVENT_WINDOW_EXPOSED:
219
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
220
SDL_RenderClear(renderer);
229
MoveSprites(renderer, sprite);
232
SDL_DestroyTexture(sprite);
233
SDL_DestroyRenderer(renderer);
234
SDL_DestroyWindow(window);
236
SDL_free(velocities);