| 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 |
|
| 13 |
/* Simple program: draw a RGB triangle, with texture */ |
| 14 |
|
| 15 |
#include <stdlib.h> |
| 16 |
#include <stdio.h> |
| 17 |
#include <time.h> |
| 18 |
|
| 19 |
#ifdef __EMSCRIPTEN__ |
| 20 |
#include <emscripten/emscripten.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
#include "SDL_test_common.h" |
| 24 |
#include "testutils.h" |
| 25 |
|
| 26 |
static SDLTest_CommonState *state; |
| 27 |
static SDL_bool use_texture = SDL_FALSE; |
| 28 |
static SDL_Texture **sprites; |
| 29 |
static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE; |
| 30 |
static float angle = 0.0f; |
| 31 |
static int sprite_w, sprite_h; |
| 32 |
|
| 33 |
int done; |
| 34 |
|
| 35 |
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ |
| 36 |
static void |
| 37 |
quit(int rc) |
| 38 |
{ |
| 39 |
SDL_free(sprites); |
| 40 |
SDLTest_CommonQuit(state); |
| 41 |
exit(rc); |
| 42 |
} |
| 43 |
|
| 44 |
int LoadSprite(const char *file) |
| 45 |
{ |
| 46 |
int i; |
| 47 |
|
| 48 |
for (i = 0; i < state->num_windows; ++i) { |
| 49 |
/* This does the SDL_LoadBMP step repeatedly, but that's OK for test code. */ |
| 50 |
sprites[i] = LoadTexture(state->renderers[i], file, SDL_TRUE, &sprite_w, &sprite_h); |
| 51 |
if (!sprites[i]) { |
| 52 |
return -1; |
| 53 |
} |
| 54 |
if (SDL_SetTextureBlendMode(sprites[i], blendMode) < 0) { |
| 55 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set blend mode: %s\n", SDL_GetError()); |
| 56 |
SDL_DestroyTexture(sprites[i]); |
| 57 |
return -1; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/* We're ready to roll. :) */ |
| 62 |
return 0; |
| 63 |
} |
| 64 |
|
| 65 |
void loop(void) |
| 66 |
{ |
| 67 |
int i; |
| 68 |
SDL_Event event; |
| 69 |
|
| 70 |
/* Check for events */ |
| 71 |
while (SDL_PollEvent(&event)) { |
| 72 |
|
| 73 |
if (event.type == SDL_MOUSEMOTION) { |
| 74 |
if (event.motion.state) { |
| 75 |
int xrel, yrel; |
| 76 |
int window_w, window_h; |
| 77 |
SDL_Window *window = SDL_GetWindowFromID(event.motion.windowID); |
| 78 |
SDL_GetWindowSize(window, &window_w, &window_h); |
| 79 |
xrel = event.motion.xrel; |
| 80 |
yrel = event.motion.yrel; |
| 81 |
if (event.motion.y < window_h / 2) { |
| 82 |
angle += xrel; |
| 83 |
} else { |
| 84 |
angle -= xrel; |
| 85 |
} |
| 86 |
if (event.motion.x < window_w / 2) { |
| 87 |
angle -= yrel; |
| 88 |
} else { |
| 89 |
angle += yrel; |
| 90 |
} |
| 91 |
} |
| 92 |
} else { |
| 93 |
SDLTest_CommonEvent(state, &event, &done); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
for (i = 0; i < state->num_windows; ++i) { |
| 98 |
SDL_Renderer *renderer = state->renderers[i]; |
| 99 |
if (state->windows[i] == NULL) { |
| 100 |
continue; |
| 101 |
} |
| 102 |
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); |
| 103 |
SDL_RenderClear(renderer); |
| 104 |
|
| 105 |
{ |
| 106 |
SDL_Rect viewport; |
| 107 |
SDL_Vertex verts[3]; |
| 108 |
float a; |
| 109 |
float d; |
| 110 |
int cx, cy; |
| 111 |
|
| 112 |
/* Query the sizes */ |
| 113 |
SDL_RenderGetViewport(renderer, &viewport); |
| 114 |
SDL_zeroa(verts); |
| 115 |
cx = viewport.x + viewport.w / 2; |
| 116 |
cy = viewport.y + viewport.h / 2; |
| 117 |
d = (viewport.w + viewport.h) / 5.f; |
| 118 |
|
| 119 |
a = (angle * 3.1415f) / 180.0f; |
| 120 |
verts[0].position.x = cx + d * SDL_cosf(a); |
| 121 |
verts[0].position.y = cy + d * SDL_sinf(a); |
| 122 |
verts[0].color.r = 0xFF; |
| 123 |
verts[0].color.g = 0; |
| 124 |
verts[0].color.b = 0; |
| 125 |
verts[0].color.a = 0xFF; |
| 126 |
|
| 127 |
a = ((angle + 120) * 3.1415f) / 180.0f; |
| 128 |
verts[1].position.x = cx + d * SDL_cosf(a); |
| 129 |
verts[1].position.y = cy + d * SDL_sinf(a); |
| 130 |
verts[1].color.r = 0; |
| 131 |
verts[1].color.g = 0xFF; |
| 132 |
verts[1].color.b = 0; |
| 133 |
verts[1].color.a = 0xFF; |
| 134 |
|
| 135 |
a = ((angle + 240) * 3.1415f) / 180.0f; |
| 136 |
verts[2].position.x = cx + d * SDL_cosf(a); |
| 137 |
verts[2].position.y = cy + d * SDL_sinf(a); |
| 138 |
verts[2].color.r = 0; |
| 139 |
verts[2].color.g = 0; |
| 140 |
verts[2].color.b = 0xFF; |
| 141 |
verts[2].color.a = 0xFF; |
| 142 |
|
| 143 |
if (use_texture) { |
| 144 |
verts[0].tex_coord.x = 0.5f; |
| 145 |
verts[0].tex_coord.y = 0.0f; |
| 146 |
verts[1].tex_coord.x = 1.0f; |
| 147 |
verts[1].tex_coord.y = 1.0f; |
| 148 |
verts[2].tex_coord.x = 0.0f; |
| 149 |
verts[2].tex_coord.y = 1.0f; |
| 150 |
} |
| 151 |
|
| 152 |
SDL_RenderGeometry(renderer, sprites[i], verts, 3, NULL, 0); |
| 153 |
} |
| 154 |
|
| 155 |
SDL_RenderPresent(renderer); |
| 156 |
} |
| 157 |
#ifdef __EMSCRIPTEN__ |
| 158 |
if (done) { |
| 159 |
emscripten_cancel_main_loop(); |
| 160 |
} |
| 161 |
#endif |
| 162 |
} |
| 163 |
|
| 164 |
int main(int argc, char *argv[]) |
| 165 |
{ |
| 166 |
int i; |
| 167 |
const char *icon = "icon.bmp"; |
| 168 |
Uint32 then, now, frames; |
| 169 |
|
| 170 |
/* Enable standard application logging */ |
| 171 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
| 172 |
|
| 173 |
/* Initialize test framework */ |
| 174 |
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); |
| 175 |
if (!state) { |
| 176 |
return 1; |
| 177 |
} |
| 178 |
for (i = 1; i < argc;) { |
| 179 |
int consumed; |
| 180 |
|
| 181 |
consumed = SDLTest_CommonArg(state, i); |
| 182 |
if (consumed == 0) { |
| 183 |
consumed = -1; |
| 184 |
if (SDL_strcasecmp(argv[i], "--blend") == 0) { |
| 185 |
if (argv[i + 1]) { |
| 186 |
if (SDL_strcasecmp(argv[i + 1], "none") == 0) { |
| 187 |
blendMode = SDL_BLENDMODE_NONE; |
| 188 |
consumed = 2; |
| 189 |
} else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) { |
| 190 |
blendMode = SDL_BLENDMODE_BLEND; |
| 191 |
consumed = 2; |
| 192 |
} else if (SDL_strcasecmp(argv[i + 1], "add") == 0) { |
| 193 |
blendMode = SDL_BLENDMODE_ADD; |
| 194 |
consumed = 2; |
| 195 |
} else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) { |
| 196 |
blendMode = SDL_BLENDMODE_MOD; |
| 197 |
consumed = 2; |
| 198 |
} |
| 199 |
} |
| 200 |
} else if (SDL_strcasecmp(argv[i], "--use-texture") == 0) { |
| 201 |
use_texture = SDL_TRUE; |
| 202 |
consumed = 1; |
| 203 |
} |
| 204 |
} |
| 205 |
if (consumed < 0) { |
| 206 |
static const char *options[] = { "[--blend none|blend|add|mod]", "[--use-texture]", NULL }; |
| 207 |
SDLTest_CommonLogUsage(state, argv[0], options); |
| 208 |
return 1; |
| 209 |
} |
| 210 |
i += consumed; |
| 211 |
} |
| 212 |
if (!SDLTest_CommonInit(state)) { |
| 213 |
return 2; |
| 214 |
} |
| 215 |
|
| 216 |
/* Create the windows, initialize the renderers, and load the textures */ |
| 217 |
sprites = |
| 218 |
(SDL_Texture **)SDL_malloc(state->num_windows * sizeof(*sprites)); |
| 219 |
if (!sprites) { |
| 220 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n"); |
| 221 |
quit(2); |
| 222 |
} |
| 223 |
/* Create the windows and initialize the renderers */ |
| 224 |
for (i = 0; i < state->num_windows; ++i) { |
| 225 |
SDL_Renderer *renderer = state->renderers[i]; |
| 226 |
SDL_SetRenderDrawBlendMode(renderer, blendMode); |
| 227 |
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); |
| 228 |
SDL_RenderClear(renderer); |
| 229 |
sprites[i] = NULL; |
| 230 |
} |
| 231 |
if (use_texture) { |
| 232 |
if (LoadSprite(icon) < 0) { |
| 233 |
quit(2); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
srand((unsigned int)time(NULL)); |
| 238 |
|
| 239 |
/* Main render loop */ |
| 240 |
frames = 0; |
| 241 |
then = SDL_GetTicks(); |
| 242 |
done = 0; |
| 243 |
|
| 244 |
#ifdef __EMSCRIPTEN__ |
| 245 |
emscripten_set_main_loop(loop, 0, 1); |
| 246 |
#else |
| 247 |
while (!done) { |
| 248 |
++frames; |
| 249 |
loop(); |
| 250 |
} |
| 251 |
#endif |
| 252 |
|
| 253 |
/* Print out some timing information */ |
| 254 |
now = SDL_GetTicks(); |
| 255 |
if (now > then) { |
| 256 |
double fps = ((double)frames * 1000) / (now - then); |
| 257 |
SDL_Log("%2.2f frames per second\n", fps); |
| 258 |
} |
| 259 |
|
| 260 |
quit(0); |
| 261 |
|
| 262 |
return 0; |
| 263 |
} |
| 264 |
|
| 265 |
/* vi: set ts=4 sw=4 expandtab: */ |