summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/test/teststreaming.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/teststreaming.c
parent8f228ade99dd3d4c8da9b78ade1815c9adf85c8f (diff)
Update to SDL3
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/teststreaming.c')
-rw-r--r--src/contrib/SDL-3.2.20/test/teststreaming.c208
1 files changed, 208 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/teststreaming.c b/src/contrib/SDL-3.2.20/test/teststreaming.c
new file mode 100644
index 0000000..ea61b0e
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/test/teststreaming.c
@@ -0,0 +1,208 @@
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/********************************************************************************
13 * *
14 * Running moose :) Coded by Mike Gorchak. *
15 * *
16 ********************************************************************************/
17
18#include <SDL3/SDL.h>
19#include <SDL3/SDL_main.h>
20#include <SDL3/SDL_test.h>
21#include "testutils.h"
22
23#ifdef SDL_PLATFORM_EMSCRIPTEN
24#include <emscripten/emscripten.h>
25#endif
26
27#include <stdlib.h>
28
29#define MOOSEPIC_W 64
30#define MOOSEPIC_H 88
31
32#define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
33#define MOOSEFRAMES_COUNT 10
34
35/* *INDENT-OFF* */ /* clang-format off */
36static SDL_Color MooseColors[84] = {
37 {49, 49, 49, 255}, {66, 24, 0, 255}, {66, 33, 0, 255}, {66, 66, 66, 255},
38 {66, 115, 49, 255}, {74, 33, 0, 255}, {74, 41, 16, 255}, {82, 33, 8, 255},
39 {82, 41, 8, 255}, {82, 49, 16, 255}, {82, 82, 82, 255}, {90, 41, 8, 255},
40 {90, 41, 16, 255}, {90, 57, 24, 255}, {99, 49, 16, 255}, {99, 66, 24, 255},
41 {99, 66, 33, 255}, {99, 74, 33, 255}, {107, 57, 24, 255}, {107, 82, 41, 255},
42 {115, 57, 33, 255}, {115, 66, 33, 255}, {115, 66, 41, 255}, {115, 74, 0, 255},
43 {115, 90, 49, 255}, {115, 115, 115, 255}, {123, 82, 0, 255}, {123, 99, 57, 255},
44 {132, 66, 41, 255}, {132, 74, 41, 255}, {132, 90, 8, 255}, {132, 99, 33, 255},
45 {132, 99, 66, 255}, {132, 107, 66, 255}, {140, 74, 49, 255}, {140, 99, 16, 255},
46 {140, 107, 74, 255}, {140, 115, 74, 255}, {148, 107, 24, 255}, {148, 115, 82, 255},
47 {148, 123, 74, 255}, {148, 123, 90, 255}, {156, 115, 33, 255}, {156, 115, 90, 255},
48 {156, 123, 82, 255}, {156, 132, 82, 255}, {156, 132, 99, 255}, {156, 156, 156, 255},
49 {165, 123, 49, 255}, {165, 123, 90, 255}, {165, 132, 82, 255}, {165, 132, 90, 255},
50 {165, 132, 99, 255}, {165, 140, 90, 255}, {173, 132, 57, 255}, {173, 132, 99, 255},
51 {173, 140, 107, 255}, {173, 140, 115, 255}, {173, 148, 99, 255}, {173, 173, 173, 255},
52 {181, 140, 74, 255}, {181, 148, 115, 255}, {181, 148, 123, 255}, {181, 156, 107, 255},
53 {189, 148, 123, 255}, {189, 156, 82, 255}, {189, 156, 123, 255}, {189, 156, 132, 255},
54 {189, 189, 189, 255}, {198, 156, 123, 255}, {198, 165, 132, 255}, {206, 165, 99, 255},
55 {206, 165, 132, 255}, {206, 173, 140, 255}, {206, 206, 206, 255}, {214, 173, 115, 255},
56 {214, 173, 140, 255}, {222, 181, 148, 255}, {222, 189, 132, 255}, {222, 189, 156, 255},
57 {222, 222, 222, 255}, {231, 198, 165, 255}, {231, 231, 231, 255}, {239, 206, 173, 255}
58};
59/* *INDENT-ON* */ /* clang-format on */
60
61static Uint8 MooseFrames[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE];
62
63static SDL_Renderer *renderer;
64static int frame;
65static SDL_Texture *MooseTexture;
66static bool done = false;
67static SDLTest_CommonState *state;
68
69static void quit(int rc)
70{
71 SDL_Quit();
72 SDLTest_CommonDestroyState(state);
73 /* Let 'main()' return normally */
74 if (rc != 0) {
75 exit(rc);
76 }
77}
78
79static void UpdateTexture(SDL_Texture *texture)
80{
81 SDL_Color *color;
82 Uint8 *src;
83 Uint32 *dst;
84 int row, col;
85 void *pixels;
86 int pitch;
87
88 if (!SDL_LockTexture(texture, NULL, &pixels, &pitch)) {
89 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't lock texture: %s", SDL_GetError());
90 quit(5);
91 }
92 src = MooseFrames[frame];
93 for (row = 0; row < MOOSEPIC_H; ++row) {
94 dst = (Uint32 *)((Uint8 *)pixels + row * pitch);
95 for (col = 0; col < MOOSEPIC_W; ++col) {
96 color = &MooseColors[*src++];
97 *dst++ = (0xFF000000 | (color->r << 16) | (color->g << 8) | color->b);
98 }
99 }
100 SDL_UnlockTexture(texture);
101}
102
103static void loop(void)
104{
105 SDL_Event event;
106
107 while (SDL_PollEvent(&event)) {
108 switch (event.type) {
109 case SDL_EVENT_KEY_DOWN:
110 if (event.key.key == SDLK_ESCAPE) {
111 done = true;
112 }
113 break;
114 case SDL_EVENT_QUIT:
115 done = true;
116 break;
117 default:
118 break;
119 }
120 }
121
122 frame = (frame + 1) % MOOSEFRAMES_COUNT;
123 UpdateTexture(MooseTexture);
124
125 SDL_RenderClear(renderer);
126 SDL_RenderTexture(renderer, MooseTexture, NULL, NULL);
127 SDL_RenderPresent(renderer);
128
129#ifdef SDL_PLATFORM_EMSCRIPTEN
130 if (done) {
131 emscripten_cancel_main_loop();
132 }
133#endif
134}
135
136int main(int argc, char **argv)
137{
138 SDL_Window *window;
139 SDL_IOStream *handle;
140 char *filename = NULL;
141
142 /* Initialize test framework */
143 state = SDLTest_CommonCreateState(argv, 0);
144 if (!state) {
145 return 1;
146 }
147
148 if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
149 SDL_Quit();
150 SDLTest_CommonDestroyState(state);
151 return 1;
152 }
153
154 if (!SDL_Init(SDL_INIT_VIDEO)) {
155 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError());
156 return 1;
157 }
158
159 /* load the moose images */
160 filename = GetResourceFilename(NULL, "moose.dat");
161 if (!filename) {
162 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory");
163 return -1;
164 }
165 handle = SDL_IOFromFile(filename, "rb");
166 SDL_free(filename);
167 if (!handle) {
168 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't find the file moose.dat !");
169 quit(2);
170 }
171 SDL_ReadIO(handle, MooseFrames, MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
172 SDL_CloseIO(handle);
173
174 /* Create the window and renderer */
175 window = SDL_CreateWindow("Happy Moose", MOOSEPIC_W * 4, MOOSEPIC_H * 4, SDL_WINDOW_RESIZABLE);
176 if (!window) {
177 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create window: %s", SDL_GetError());
178 quit(3);
179 }
180
181 renderer = SDL_CreateRenderer(window, NULL);
182 if (!renderer) {
183 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s", SDL_GetError());
184 quit(4);
185 }
186
187 MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
188 if (!MooseTexture) {
189 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s", SDL_GetError());
190 quit(5);
191 }
192
193 /* Loop, waiting for QUIT or the escape key */
194 frame = 0;
195
196#ifdef SDL_PLATFORM_EMSCRIPTEN
197 emscripten_set_main_loop(loop, 0, 1);
198#else
199 while (!done) {
200 loop();
201 }
202#endif
203
204 SDL_DestroyRenderer(renderer);
205
206 quit(0);
207 return 0;
208}