19
#include <SDL3/SDL_main.h>
20
#include <SDL3/SDL_test.h>
23
#ifdef SDL_PLATFORM_EMSCRIPTEN
24
#include <emscripten/emscripten.h>
32
#define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
33
#define MOOSEFRAMES_COUNT 10
36
static 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}
61
static Uint8 MooseFrames[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE];
63
static SDL_Renderer *renderer;
65
static SDL_Texture *MooseTexture;
66
static SDL_bool done = SDL_FALSE;
67
static SDLTest_CommonState *state;
69
static void quit(int rc)
72
SDLTest_CommonDestroyState(state);
79
static void UpdateTexture(SDL_Texture *texture)
88
if (SDL_LockTexture(texture, NULL, &pixels, &pitch) < 0) {
89
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't lock texture: %s\n", SDL_GetError());
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);
100
SDL_UnlockTexture(texture);
103
static void loop(void)
107
while (SDL_PollEvent(&event)) {
108
switch (event.type) {
109
case SDL_EVENT_KEY_DOWN:
110
if (event.key.key == SDLK_ESCAPE) {
122
frame = (frame + 1) % MOOSEFRAMES_COUNT;
123
UpdateTexture(MooseTexture);
125
SDL_RenderClear(renderer);
126
SDL_RenderTexture(renderer, MooseTexture, NULL, NULL);
127
SDL_RenderPresent(renderer);
129
#ifdef SDL_PLATFORM_EMSCRIPTEN
131
emscripten_cancel_main_loop();
136
int main(int argc, char **argv)
139
SDL_IOStream *handle;
140
char *filename = NULL;
143
state = SDLTest_CommonCreateState(argv, 0);
149
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
151
if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
153
SDLTest_CommonDestroyState(state);
157
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
158
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
163
filename = GetResourceFilename(NULL, "moose.dat");
165
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory\n");
168
handle = SDL_IOFromFile(filename, "rb");
171
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't find the file moose.dat !\n");
174
SDL_ReadIO(handle, MooseFrames, MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
178
window = SDL_CreateWindow("Happy Moose", MOOSEPIC_W * 4, MOOSEPIC_H * 4, SDL_WINDOW_RESIZABLE);
180
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create window: %s\n", SDL_GetError());
184
renderer = SDL_CreateRenderer(window, NULL);
186
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s\n", SDL_GetError());
190
MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
192
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s\n", SDL_GetError());
199
#ifdef SDL_PLATFORM_EMSCRIPTEN
200
emscripten_set_main_loop(loop, 0, 1);
207
SDL_DestroyRenderer(renderer);