| 1 |
/* |
| 2 |
Copyright (C) 1997-2026 Sam Lantinga <[email protected]> |
| 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 <stdio.h> |
| 15 |
#include <stdlib.h> /* for srand() */ |
| 16 |
#include <time.h> /* for time() */ |
| 17 |
|
| 18 |
#include "testnative.h" |
| 19 |
#include "testutils.h" |
| 20 |
|
| 21 |
#define WINDOW_W 640 |
| 22 |
#define WINDOW_H 480 |
| 23 |
#define NUM_SPRITES 100 |
| 24 |
#define MAX_SPEED 1 |
| 25 |
|
| 26 |
static NativeWindowFactory *factories[] = { |
| 27 |
#ifdef TEST_NATIVE_WINDOWS |
| 28 |
&WindowsWindowFactory, |
| 29 |
#endif |
| 30 |
#ifdef TEST_NATIVE_X11 |
| 31 |
&X11WindowFactory, |
| 32 |
#endif |
| 33 |
#ifdef TEST_NATIVE_COCOA |
| 34 |
&CocoaWindowFactory, |
| 35 |
#endif |
| 36 |
#ifdef TEST_NATIVE_OS2 |
| 37 |
&OS2WindowFactory, |
| 38 |
#endif |
| 39 |
NULL |
| 40 |
}; |
| 41 |
static NativeWindowFactory *factory = NULL; |
| 42 |
static void *native_window; |
| 43 |
static SDL_Rect *positions, *velocities; |
| 44 |
|
| 45 |
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ |
| 46 |
static void |
| 47 |
quit(int rc) |
| 48 |
{ |
| 49 |
SDL_VideoQuit(); |
| 50 |
if (native_window && factory) { |
| 51 |
factory->DestroyNativeWindow(native_window); |
| 52 |
} |
| 53 |
exit(rc); |
| 54 |
} |
| 55 |
|
| 56 |
void MoveSprites(SDL_Renderer *renderer, SDL_Texture *sprite) |
| 57 |
{ |
| 58 |
int sprite_w, sprite_h; |
| 59 |
int i; |
| 60 |
SDL_Rect viewport; |
| 61 |
SDL_Rect *position, *velocity; |
| 62 |
|
| 63 |
/* Query the sizes */ |
| 64 |
SDL_RenderGetViewport(renderer, &viewport); |
| 65 |
SDL_QueryTexture(sprite, NULL, NULL, &sprite_w, &sprite_h); |
| 66 |
|
| 67 |
/* Draw a gray background */ |
| 68 |
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); |
| 69 |
SDL_RenderClear(renderer); |
| 70 |
|
| 71 |
/* Move the sprite, bounce at the wall, and draw */ |
| 72 |
for (i = 0; i < NUM_SPRITES; ++i) { |
| 73 |
position = &positions[i]; |
| 74 |
velocity = &velocities[i]; |
| 75 |
position->x += velocity->x; |
| 76 |
if ((position->x < 0) || (position->x >= (viewport.w - sprite_w))) { |
| 77 |
velocity->x = -velocity->x; |
| 78 |
position->x += velocity->x; |
| 79 |
} |
| 80 |
position->y += velocity->y; |
| 81 |
if ((position->y < 0) || (position->y >= (viewport.h - sprite_h))) { |
| 82 |
velocity->y = -velocity->y; |
| 83 |
position->y += velocity->y; |
| 84 |
} |
| 85 |
|
| 86 |
/* Blit the sprite onto the screen */ |
| 87 |
SDL_RenderCopy(renderer, sprite, NULL, position); |
| 88 |
} |
| 89 |
|
| 90 |
/* Update the screen! */ |
| 91 |
SDL_RenderPresent(renderer); |
| 92 |
} |
| 93 |
|
| 94 |
int main(int argc, char *argv[]) |
| 95 |
{ |
| 96 |
int i, done; |
| 97 |
const char *driver; |
| 98 |
SDL_Window *window; |
| 99 |
SDL_Renderer *renderer; |
| 100 |
SDL_Texture *sprite; |
| 101 |
int window_w, window_h; |
| 102 |
int sprite_w, sprite_h; |
| 103 |
SDL_Event event; |
| 104 |
|
| 105 |
/* Enable standard application logging */ |
| 106 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
| 107 |
|
| 108 |
if (SDL_VideoInit(NULL) < 0) { |
| 109 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL video: %s\n", |
| 110 |
SDL_GetError()); |
| 111 |
exit(1); |
| 112 |
} |
| 113 |
driver = SDL_GetCurrentVideoDriver(); |
| 114 |
|
| 115 |
/* Find a native window driver and create a native window */ |
| 116 |
for (i = 0; factories[i]; ++i) { |
| 117 |
if (SDL_strcmp(driver, factories[i]->tag) == 0) { |
| 118 |
factory = factories[i]; |
| 119 |
break; |
| 120 |
} |
| 121 |
} |
| 122 |
if (!factory) { |
| 123 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find native window code for %s driver\n", |
| 124 |
driver); |
| 125 |
quit(2); |
| 126 |
} |
| 127 |
SDL_Log("Creating native window for %s driver\n", driver); |
| 128 |
native_window = factory->CreateNativeWindow(WINDOW_W, WINDOW_H); |
| 129 |
if (!native_window) { |
| 130 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create native window\n"); |
| 131 |
quit(3); |
| 132 |
} |
| 133 |
window = SDL_CreateWindowFrom(native_window); |
| 134 |
if (!window) { |
| 135 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create SDL window: %s\n", SDL_GetError()); |
| 136 |
quit(4); |
| 137 |
} |
| 138 |
SDL_SetWindowTitle(window, "SDL Native Window Test"); |
| 139 |
|
| 140 |
/* Create the renderer */ |
| 141 |
renderer = SDL_CreateRenderer(window, -1, 0); |
| 142 |
if (!renderer) { |
| 143 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError()); |
| 144 |
quit(5); |
| 145 |
} |
| 146 |
|
| 147 |
/* Clear the window, load the sprite and go! */ |
| 148 |
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); |
| 149 |
SDL_RenderClear(renderer); |
| 150 |
|
| 151 |
sprite = LoadTexture(renderer, "icon.bmp", SDL_TRUE, NULL, NULL); |
| 152 |
if (!sprite) { |
| 153 |
quit(6); |
| 154 |
} |
| 155 |
|
| 156 |
/* Allocate memory for the sprite info */ |
| 157 |
SDL_GetWindowSize(window, &window_w, &window_h); |
| 158 |
SDL_QueryTexture(sprite, NULL, NULL, &sprite_w, &sprite_h); |
| 159 |
positions = (SDL_Rect *)SDL_malloc(NUM_SPRITES * sizeof(SDL_Rect)); |
| 160 |
velocities = (SDL_Rect *)SDL_malloc(NUM_SPRITES * sizeof(SDL_Rect)); |
| 161 |
if (!positions || !velocities) { |
| 162 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n"); |
| 163 |
quit(2); |
| 164 |
} |
| 165 |
srand((unsigned int)time(NULL)); |
| 166 |
for (i = 0; i < NUM_SPRITES; ++i) { |
| 167 |
positions[i].x = rand() % (window_w - sprite_w); |
| 168 |
positions[i].y = rand() % (window_h - sprite_h); |
| 169 |
positions[i].w = sprite_w; |
| 170 |
positions[i].h = sprite_h; |
| 171 |
velocities[i].x = 0; |
| 172 |
velocities[i].y = 0; |
| 173 |
while (!velocities[i].x && !velocities[i].y) { |
| 174 |
velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED; |
| 175 |
velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
/* Main render loop */ |
| 180 |
done = 0; |
| 181 |
while (!done) { |
| 182 |
/* Check for events */ |
| 183 |
while (SDL_PollEvent(&event)) { |
| 184 |
switch (event.type) { |
| 185 |
case SDL_WINDOWEVENT: |
| 186 |
switch (event.window.event) { |
| 187 |
case SDL_WINDOWEVENT_EXPOSED: |
| 188 |
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); |
| 189 |
SDL_RenderClear(renderer); |
| 190 |
break; |
| 191 |
} |
| 192 |
break; |
| 193 |
case SDL_QUIT: |
| 194 |
done = 1; |
| 195 |
break; |
| 196 |
default: |
| 197 |
break; |
| 198 |
} |
| 199 |
} |
| 200 |
MoveSprites(renderer, sprite); |
| 201 |
} |
| 202 |
|
| 203 |
quit(0); |
| 204 |
|
| 205 |
return 0; /* to prevent compiler warning */ |
| 206 |
} |
| 207 |
|
| 208 |
/* vi: set ts=4 sw=4 expandtab: */ |