19
#include <SDL3/SDL_test.h>
20
#include <SDL3/SDL_test_common.h>
21
#include <SDL3/SDL_main.h>
24
#ifdef SDL_PLATFORM_EMSCRIPTEN
25
#include <emscripten/emscripten.h>
33
#define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
34
#define MOOSEFRAMES_COUNT 10
37
static SDL_Color MooseColors[84] = {
38
{49, 49, 49, SDL_ALPHA_OPAQUE}
39
, {66, 24, 0, SDL_ALPHA_OPAQUE}
40
, {66, 33, 0, SDL_ALPHA_OPAQUE}
41
, {66, 66, 66, SDL_ALPHA_OPAQUE}
43
{66, 115, 49, SDL_ALPHA_OPAQUE}
44
, {74, 33, 0, SDL_ALPHA_OPAQUE}
45
, {74, 41, 16, SDL_ALPHA_OPAQUE}
46
, {82, 33, 8, SDL_ALPHA_OPAQUE}
48
{82, 41, 8, SDL_ALPHA_OPAQUE}
49
, {82, 49, 16, SDL_ALPHA_OPAQUE}
50
, {82, 82, 82, SDL_ALPHA_OPAQUE}
51
, {90, 41, 8, SDL_ALPHA_OPAQUE}
53
{90, 41, 16, SDL_ALPHA_OPAQUE}
54
, {90, 57, 24, SDL_ALPHA_OPAQUE}
55
, {99, 49, 16, SDL_ALPHA_OPAQUE}
56
, {99, 66, 24, SDL_ALPHA_OPAQUE}
58
{99, 66, 33, SDL_ALPHA_OPAQUE}
59
, {99, 74, 33, SDL_ALPHA_OPAQUE}
60
, {107, 57, 24, SDL_ALPHA_OPAQUE}
61
, {107, 82, 41, SDL_ALPHA_OPAQUE}
63
{115, 57, 33, SDL_ALPHA_OPAQUE}
64
, {115, 66, 33, SDL_ALPHA_OPAQUE}
65
, {115, 66, 41, SDL_ALPHA_OPAQUE}
66
, {115, 74, 0, SDL_ALPHA_OPAQUE}
68
{115, 90, 49, SDL_ALPHA_OPAQUE}
69
, {115, 115, 115, SDL_ALPHA_OPAQUE}
70
, {123, 82, 0, SDL_ALPHA_OPAQUE}
71
, {123, 99, 57, SDL_ALPHA_OPAQUE}
73
{132, 66, 41, SDL_ALPHA_OPAQUE}
74
, {132, 74, 41, SDL_ALPHA_OPAQUE}
75
, {132, 90, 8, SDL_ALPHA_OPAQUE}
76
, {132, 99, 33, SDL_ALPHA_OPAQUE}
78
{132, 99, 66, SDL_ALPHA_OPAQUE}
79
, {132, 107, 66, SDL_ALPHA_OPAQUE}
80
, {140, 74, 49, SDL_ALPHA_OPAQUE}
81
, {140, 99, 16, SDL_ALPHA_OPAQUE}
83
{140, 107, 74, SDL_ALPHA_OPAQUE}
84
, {140, 115, 74, SDL_ALPHA_OPAQUE}
85
, {148, 107, 24, SDL_ALPHA_OPAQUE}
86
, {148, 115, 82, SDL_ALPHA_OPAQUE}
88
{148, 123, 74, SDL_ALPHA_OPAQUE}
89
, {148, 123, 90, SDL_ALPHA_OPAQUE}
90
, {156, 115, 33, SDL_ALPHA_OPAQUE}
91
, {156, 115, 90, SDL_ALPHA_OPAQUE}
93
{156, 123, 82, SDL_ALPHA_OPAQUE}
94
, {156, 132, 82, SDL_ALPHA_OPAQUE}
95
, {156, 132, 99, SDL_ALPHA_OPAQUE}
96
, {156, 156, 156, SDL_ALPHA_OPAQUE}
98
{165, 123, 49, SDL_ALPHA_OPAQUE}
99
, {165, 123, 90, SDL_ALPHA_OPAQUE}
100
, {165, 132, 82, SDL_ALPHA_OPAQUE}
101
, {165, 132, 90, SDL_ALPHA_OPAQUE}
103
{165, 132, 99, SDL_ALPHA_OPAQUE}
104
, {165, 140, 90, SDL_ALPHA_OPAQUE}
105
, {173, 132, 57, SDL_ALPHA_OPAQUE}
106
, {173, 132, 99, SDL_ALPHA_OPAQUE}
108
{173, 140, 107, SDL_ALPHA_OPAQUE}
109
, {173, 140, 115, SDL_ALPHA_OPAQUE}
110
, {173, 148, 99, SDL_ALPHA_OPAQUE}
111
, {173, 173, 173, SDL_ALPHA_OPAQUE}
113
{181, 140, 74, SDL_ALPHA_OPAQUE}
114
, {181, 148, 115, SDL_ALPHA_OPAQUE}
115
, {181, 148, 123, SDL_ALPHA_OPAQUE}
116
, {181, 156, 107, SDL_ALPHA_OPAQUE}
118
{189, 148, 123, SDL_ALPHA_OPAQUE}
119
, {189, 156, 82, SDL_ALPHA_OPAQUE}
120
, {189, 156, 123, SDL_ALPHA_OPAQUE}
121
, {189, 156, 132, SDL_ALPHA_OPAQUE}
123
{189, 189, 189, SDL_ALPHA_OPAQUE}
124
, {198, 156, 123, SDL_ALPHA_OPAQUE}
125
, {198, 165, 132, SDL_ALPHA_OPAQUE}
126
, {206, 165, 99, SDL_ALPHA_OPAQUE}
128
{206, 165, 132, SDL_ALPHA_OPAQUE}
129
, {206, 173, 140, SDL_ALPHA_OPAQUE}
130
, {206, 206, 206, SDL_ALPHA_OPAQUE}
131
, {214, 173, 115, SDL_ALPHA_OPAQUE}
133
{214, 173, 140, SDL_ALPHA_OPAQUE}
134
, {222, 181, 148, SDL_ALPHA_OPAQUE}
135
, {222, 189, 132, SDL_ALPHA_OPAQUE}
136
, {222, 189, 156, SDL_ALPHA_OPAQUE}
138
{222, 222, 222, SDL_ALPHA_OPAQUE}
139
, {231, 198, 165, SDL_ALPHA_OPAQUE}
140
, {231, 231, 231, SDL_ALPHA_OPAQUE}
141
, {239, 206, 173, SDL_ALPHA_OPAQUE}
145
static SDLTest_CommonState *state;
146
static Uint64 next_fps_check;
148
static const Uint32 fps_check_delay = 5000;
150
static Uint32 yuv_format = SDL_PIXELFORMAT_YV12;
151
static SDL_Surface *MooseYUVSurfaces[MOOSEFRAMES_COUNT];
152
static SDL_Texture *MooseTexture = NULL;
153
static SDL_FRect displayrect;
156
static int paused = 0;
159
static SDL_bool streaming = SDL_TRUE;
160
static Uint8 *RawMooseData = NULL;
172
SDL_free(RawMooseData);
174
for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
175
SDL_DestroySurface(MooseYUVSurfaces[i]);
178
SDLTest_CommonQuit(state);
186
static void MoveSprites(SDL_Renderer *renderer)
192
i = (i + 1) % MOOSEFRAMES_COUNT;
194
if ((yuv_format == SDL_PIXELFORMAT_NV12 || yuv_format == SDL_PIXELFORMAT_NV21) &&
196
#ifdef TEST_RECT_UPDATE
205
rect.x = MOOSEPIC_W / 4;
206
rect.y = MOOSEPIC_H / 4;
207
rect.w = MOOSEPIC_W / 2;
208
rect.h = MOOSEPIC_H / 2;
210
SDL_UpdateNVTexture(MooseTexture, &rect,
211
(Uint8 *)MooseYUVSurfaces[i]->pixels + rect.y * MooseYUVSurfaces[i]->pitch + rect.x, MooseYUVSurfaces[i]->pitch,
212
(Uint8 *)MooseYUVSurfaces[i]->pixels + MOOSEFRAME_SIZE + (rect.y + 1) / 2 * MooseYUVSurfaces[i]->pitch + (rect.x + 1) / 2, MooseYUVSurfaces[i]->pitch);
214
SDL_UpdateNVTexture(MooseTexture, NULL,
215
MooseYUVSurfaces[i]->pixels, MooseYUVSurfaces[i]->pitch,
216
(Uint8 *)MooseYUVSurfaces[i]->pixels + MOOSEFRAME_SIZE, MooseYUVSurfaces[i]->pitch);
219
SDL_UpdateTexture(MooseTexture, NULL, MooseYUVSurfaces[i]->pixels, MooseYUVSurfaces[i]->pitch);
222
SDL_RenderClear(renderer);
223
SDL_RenderTexture(renderer, MooseTexture, NULL, &displayrect);
224
SDL_RenderPresent(renderer);
230
i = (i + 1) % MOOSEFRAMES_COUNT;
233
tmp = SDL_CreateTextureFromSurface(renderer, MooseYUVSurfaces[i]);
235
SDL_Log("Error %s", SDL_GetError());
239
SDL_RenderClear(renderer);
240
SDL_RenderTexture(renderer, tmp, NULL, &displayrect);
241
SDL_RenderPresent(renderer);
242
SDL_DestroyTexture(tmp);
247
static void loop(void)
253
SDL_Renderer *renderer = state->renderers[0];
256
while (SDL_PollEvent(&event)) {
257
SDLTest_CommonEvent(state, &event, &done);
259
switch (event.type) {
260
case SDL_EVENT_WINDOW_RESIZED:
261
SDL_SetRenderViewport(renderer, NULL);
262
window_w = event.window.data1;
263
window_h = event.window.data2;
264
displayrect.w = (float)window_w;
265
displayrect.h = (float)window_h;
267
case SDL_EVENT_MOUSE_BUTTON_DOWN:
268
displayrect.x = event.button.x - window_w / 2;
269
displayrect.y = event.button.y - window_h / 2;
271
case SDL_EVENT_MOUSE_MOTION:
272
if (event.motion.state) {
273
displayrect.x = event.motion.x - window_w / 2;
274
displayrect.y = event.motion.y - window_h / 2;
277
case SDL_EVENT_KEY_DOWN:
278
if (event.key.key == SDLK_SPACE) {
282
if (event.key.key != SDLK_ESCAPE) {
291
#ifndef SDL_PLATFORM_EMSCRIPTEN
295
for (i = 0; i < state->num_windows; ++i) {
296
if (state->windows[i] == NULL) {
299
MoveSprites(state->renderers[i]);
301
#ifdef SDL_PLATFORM_EMSCRIPTEN
303
emscripten_cancel_main_loop();
308
now = SDL_GetTicks();
309
if (now >= next_fps_check) {
311
const Uint64 then = next_fps_check - fps_check_delay;
312
const double fps = ((double)frames * 1000) / (now - then);
313
SDL_Log("%2.2f frames per second\n", fps);
314
next_fps_check = now + fps_check_delay;
320
int main(int argc, char **argv)
322
SDL_IOStream *handle;
328
char *filename = NULL;
331
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
336
SDL_zeroa(MooseYUVSurfaces);
338
for (i = 1; i < argc;) {
341
consumed = SDLTest_CommonArg(state, i);
344
if (SDL_strcmp(argv[i], "--fps") == 0) {
347
fps = SDL_atoi(argv[i + 1]);
349
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "The --fps option requires an argument [from 1 to 1000], default is 12.\n");
352
if ((fps < 0) || (fps > 1000)) {
353
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "The --fps option must be in range from 1 to 1000, default is 12.\n");
357
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "The --fps option requires an argument [from 1 to 1000], default is 12.\n");
360
} else if (SDL_strcmp(argv[i], "--nodelay") == 0) {
363
} else if (SDL_strcmp(argv[i], "--nostreaming") == 0) {
365
streaming = SDL_FALSE;
366
} else if (SDL_strcmp(argv[i], "--scale") == 0) {
369
scale = SDL_atoi(argv[i + 1]);
371
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "The --scale option requires an argument [from 1 to 50], default is 5.\n");
374
if ((scale < 0) || (scale > 50)) {
375
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "The --scale option must be in range from 1 to 50, default is 5.\n");
379
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "The --fps option requires an argument [from 1 to 1000], default is 12.\n");
382
} else if (SDL_strcmp(argv[i], "--yuvformat") == 0) {
385
char *fmt = argv[i + 1];
387
if (SDL_strcmp(fmt, "YV12") == 0) {
388
yuv_format = SDL_PIXELFORMAT_YV12;
389
} else if (SDL_strcmp(fmt, "IYUV") == 0) {
390
yuv_format = SDL_PIXELFORMAT_IYUV;
391
} else if (SDL_strcmp(fmt, "YUY2") == 0) {
392
yuv_format = SDL_PIXELFORMAT_YUY2;
393
} else if (SDL_strcmp(fmt, "UYVY") == 0) {
394
yuv_format = SDL_PIXELFORMAT_UYVY;
395
} else if (SDL_strcmp(fmt, "YVYU") == 0) {
396
yuv_format = SDL_PIXELFORMAT_YVYU;
397
} else if (SDL_strcmp(fmt, "NV12") == 0) {
398
yuv_format = SDL_PIXELFORMAT_NV12;
399
} else if (SDL_strcmp(fmt, "NV21") == 0) {
400
yuv_format = SDL_PIXELFORMAT_NV21;
402
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "The --yuvformat option requires one of the: YV12 (default), IYUV, YUY2, UYVY, YVYU, NV12, NV21)\n");
406
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "The --yuvformat option requires one of the: YV12 (default), IYUV, YUY2, UYVY, YVYU, NV12, NV21)\n");
413
static const char *options[] = {
414
"[--fps <frames per second>]",
416
"[--yuvformat <fmt>] (one of the: YV12 (default), IYUV, YUY2, UYVY, YVYU, NV12, NV21)",
417
"[--scale <scale factor>] (initial scale of the overlay)",
418
"[--nostreaming] path that use SDL_CreateTextureFromSurface() not STREAMING texture",
421
SDLTest_CommonLogUsage(state, argv[0], options);
428
state->window_w = MOOSEPIC_W * scale;
429
state->window_h = MOOSEPIC_H * scale;
431
if (!SDLTest_CommonInit(state)) {
435
RawMooseData = (Uint8 *)SDL_malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
437
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't allocate memory for movie !\n");
442
filename = GetResourceFilename(NULL, "moose.dat");
444
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory\n");
447
handle = SDL_IOFromFile(filename, "rb");
450
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't find the file moose.dat !\n");
454
SDL_ReadIO(handle, RawMooseData, MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
459
window_w = MOOSEPIC_W * scale;
460
window_h = MOOSEPIC_H * scale;
462
if (state->num_windows != 1) {
463
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Only one window allowed\n");
467
for (i = 0; i < state->num_windows; ++i) {
468
SDL_Renderer *renderer = state->renderers[i];
471
MooseTexture = SDL_CreateTexture(renderer, yuv_format, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
473
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s\n", SDL_GetError());
482
for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
484
SDL_Surface *mooseRGBSurface = SDL_CreateSurface(MOOSEPIC_W, MOOSEPIC_H, SDL_PIXELFORMAT_RGB24);
485
if (!mooseRGBSurface) {
491
Uint8 *rgb = mooseRGBSurface->pixels;
492
Uint8 *frame = RawMooseData + i * MOOSEFRAME_SIZE;
493
for (j = 0; j < MOOSEFRAME_SIZE; ++j) {
494
rgb[0] = MooseColors[frame[j]].r;
495
rgb[1] = MooseColors[frame[j]].g;
496
rgb[2] = MooseColors[frame[j]].b;
502
MooseYUVSurfaces[i] = SDL_ConvertSurface(mooseRGBSurface, yuv_format);
503
if (MooseYUVSurfaces[i] == NULL) {
507
SDL_DestroySurface(mooseRGBSurface);
510
SDL_free(RawMooseData);
518
fpsdelay = 1000 / fps;
523
displayrect.w = (float)window_w;
524
displayrect.h = (float)window_h;
527
SDL_SetEventEnabled(SDL_EVENT_KEY_UP, SDL_FALSE);
531
next_fps_check = SDL_GetTicks() + fps_check_delay;
535
#ifdef SDL_PLATFORM_EMSCRIPTEN
536
emscripten_set_main_loop(loop, nodelay ? 0 : fps, 1);