| 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 |
* * |
| 14 |
* Running moose :) Coded by Mike Gorchak. * |
| 15 |
* * |
| 16 |
********************************************************************************/ |
| 17 |
|
| 18 |
#include <stdlib.h> |
| 19 |
#include <stdio.h> |
| 20 |
|
| 21 |
#ifdef __EMSCRIPTEN__ |
| 22 |
#include <emscripten/emscripten.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
#include "SDL.h" |
| 26 |
#include "testutils.h" |
| 27 |
|
| 28 |
#define MOOSEPIC_W 64 |
| 29 |
#define MOOSEPIC_H 88 |
| 30 |
|
| 31 |
#define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H) |
| 32 |
#define MOOSEFRAMES_COUNT 10 |
| 33 |
|
| 34 |
/* *INDENT-OFF* */ /* clang-format off */ |
| 35 |
SDL_Color MooseColors[84] = { |
| 36 |
{49, 49, 49, 255}, {66, 24, 0, 255}, {66, 33, 0, 255}, {66, 66, 66, 255}, |
| 37 |
{66, 115, 49, 255}, {74, 33, 0, 255}, {74, 41, 16, 255}, {82, 33, 8, 255}, |
| 38 |
{82, 41, 8, 255}, {82, 49, 16, 255}, {82, 82, 82, 255}, {90, 41, 8, 255}, |
| 39 |
{90, 41, 16, 255}, {90, 57, 24, 255}, {99, 49, 16, 255}, {99, 66, 24, 255}, |
| 40 |
{99, 66, 33, 255}, {99, 74, 33, 255}, {107, 57, 24, 255}, {107, 82, 41, 255}, |
| 41 |
{115, 57, 33, 255}, {115, 66, 33, 255}, {115, 66, 41, 255}, {115, 74, 0, 255}, |
| 42 |
{115, 90, 49, 255}, {115, 115, 115, 255}, {123, 82, 0, 255}, {123, 99, 57, 255}, |
| 43 |
{132, 66, 41, 255}, {132, 74, 41, 255}, {132, 90, 8, 255}, {132, 99, 33, 255}, |
| 44 |
{132, 99, 66, 255}, {132, 107, 66, 255}, {140, 74, 49, 255}, {140, 99, 16, 255}, |
| 45 |
{140, 107, 74, 255}, {140, 115, 74, 255}, {148, 107, 24, 255}, {148, 115, 82, 255}, |
| 46 |
{148, 123, 74, 255}, {148, 123, 90, 255}, {156, 115, 33, 255}, {156, 115, 90, 255}, |
| 47 |
{156, 123, 82, 255}, {156, 132, 82, 255}, {156, 132, 99, 255}, {156, 156, 156, 255}, |
| 48 |
{165, 123, 49, 255}, {165, 123, 90, 255}, {165, 132, 82, 255}, {165, 132, 90, 255}, |
| 49 |
{165, 132, 99, 255}, {165, 140, 90, 255}, {173, 132, 57, 255}, {173, 132, 99, 255}, |
| 50 |
{173, 140, 107, 255}, {173, 140, 115, 255}, {173, 148, 99, 255}, {173, 173, 173, 255}, |
| 51 |
{181, 140, 74, 255}, {181, 148, 115, 255}, {181, 148, 123, 255}, {181, 156, 107, 255}, |
| 52 |
{189, 148, 123, 255}, {189, 156, 82, 255}, {189, 156, 123, 255}, {189, 156, 132, 255}, |
| 53 |
{189, 189, 189, 255}, {198, 156, 123, 255}, {198, 165, 132, 255}, {206, 165, 99, 255}, |
| 54 |
{206, 165, 132, 255}, {206, 173, 140, 255}, {206, 206, 206, 255}, {214, 173, 115, 255}, |
| 55 |
{214, 173, 140, 255}, {222, 181, 148, 255}, {222, 189, 132, 255}, {222, 189, 156, 255}, |
| 56 |
{222, 222, 222, 255}, {231, 198, 165, 255}, {231, 231, 231, 255}, {239, 206, 173, 255} |
| 57 |
}; |
| 58 |
/* *INDENT-ON* */ /* clang-format on */ |
| 59 |
|
| 60 |
Uint8 MooseFrames[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE]; |
| 61 |
|
| 62 |
SDL_Renderer *renderer; |
| 63 |
int frame; |
| 64 |
SDL_Texture *MooseTexture; |
| 65 |
SDL_bool done = SDL_FALSE; |
| 66 |
|
| 67 |
void quit(int rc) |
| 68 |
{ |
| 69 |
SDL_Quit(); |
| 70 |
exit(rc); |
| 71 |
} |
| 72 |
|
| 73 |
void UpdateTexture(SDL_Texture *texture) |
| 74 |
{ |
| 75 |
SDL_Color *color; |
| 76 |
Uint8 *src; |
| 77 |
Uint32 *dst; |
| 78 |
int row, col; |
| 79 |
void *pixels; |
| 80 |
int pitch; |
| 81 |
|
| 82 |
if (SDL_LockTexture(texture, NULL, &pixels, &pitch) < 0) { |
| 83 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't lock texture: %s\n", SDL_GetError()); |
| 84 |
quit(5); |
| 85 |
} |
| 86 |
src = MooseFrames[frame]; |
| 87 |
for (row = 0; row < MOOSEPIC_H; ++row) { |
| 88 |
dst = (Uint32 *)((Uint8 *)pixels + row * pitch); |
| 89 |
for (col = 0; col < MOOSEPIC_W; ++col) { |
| 90 |
color = &MooseColors[*src++]; |
| 91 |
*dst++ = (0xFF000000 | (color->r << 16) | (color->g << 8) | color->b); |
| 92 |
} |
| 93 |
} |
| 94 |
SDL_UnlockTexture(texture); |
| 95 |
} |
| 96 |
|
| 97 |
void loop(void) |
| 98 |
{ |
| 99 |
SDL_Event event; |
| 100 |
|
| 101 |
while (SDL_PollEvent(&event)) { |
| 102 |
switch (event.type) { |
| 103 |
case SDL_KEYDOWN: |
| 104 |
if (event.key.keysym.sym == SDLK_ESCAPE) { |
| 105 |
done = SDL_TRUE; |
| 106 |
} |
| 107 |
break; |
| 108 |
case SDL_QUIT: |
| 109 |
done = SDL_TRUE; |
| 110 |
break; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
frame = (frame + 1) % MOOSEFRAMES_COUNT; |
| 115 |
UpdateTexture(MooseTexture); |
| 116 |
|
| 117 |
SDL_RenderClear(renderer); |
| 118 |
SDL_RenderCopy(renderer, MooseTexture, NULL, NULL); |
| 119 |
SDL_RenderPresent(renderer); |
| 120 |
|
| 121 |
#ifdef __EMSCRIPTEN__ |
| 122 |
if (done) { |
| 123 |
emscripten_cancel_main_loop(); |
| 124 |
} |
| 125 |
#endif |
| 126 |
} |
| 127 |
|
| 128 |
int main(int argc, char **argv) |
| 129 |
{ |
| 130 |
SDL_Window *window; |
| 131 |
SDL_RWops *handle; |
| 132 |
char *filename = NULL; |
| 133 |
|
| 134 |
/* Enable standard application logging */ |
| 135 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
| 136 |
|
| 137 |
if (SDL_Init(SDL_INIT_VIDEO) < 0) { |
| 138 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
| 139 |
return 1; |
| 140 |
} |
| 141 |
|
| 142 |
/* load the moose images */ |
| 143 |
filename = GetResourceFilename(NULL, "moose.dat"); |
| 144 |
if (!filename) { |
| 145 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory\n"); |
| 146 |
return -1; |
| 147 |
} |
| 148 |
handle = SDL_RWFromFile(filename, "rb"); |
| 149 |
SDL_free(filename); |
| 150 |
if (!handle) { |
| 151 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't find the file moose.dat !\n"); |
| 152 |
quit(2); |
| 153 |
} |
| 154 |
SDL_RWread(handle, MooseFrames, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT); |
| 155 |
SDL_RWclose(handle); |
| 156 |
|
| 157 |
/* Create the window and renderer */ |
| 158 |
window = SDL_CreateWindow("Happy Moose", |
| 159 |
SDL_WINDOWPOS_UNDEFINED, |
| 160 |
SDL_WINDOWPOS_UNDEFINED, |
| 161 |
MOOSEPIC_W * 4, MOOSEPIC_H * 4, |
| 162 |
SDL_WINDOW_RESIZABLE); |
| 163 |
if (!window) { |
| 164 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create window: %s\n", SDL_GetError()); |
| 165 |
quit(3); |
| 166 |
} |
| 167 |
|
| 168 |
renderer = SDL_CreateRenderer(window, -1, 0); |
| 169 |
if (!renderer) { |
| 170 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s\n", SDL_GetError()); |
| 171 |
quit(4); |
| 172 |
} |
| 173 |
|
| 174 |
MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H); |
| 175 |
if (!MooseTexture) { |
| 176 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s\n", SDL_GetError()); |
| 177 |
quit(5); |
| 178 |
} |
| 179 |
|
| 180 |
/* Loop, waiting for QUIT or the escape key */ |
| 181 |
frame = 0; |
| 182 |
|
| 183 |
#ifdef __EMSCRIPTEN__ |
| 184 |
emscripten_set_main_loop(loop, 0, 1); |
| 185 |
#else |
| 186 |
while (!done) { |
| 187 |
loop(); |
| 188 |
} |
| 189 |
#endif |
| 190 |
|
| 191 |
SDL_DestroyRenderer(renderer); |
| 192 |
|
| 193 |
quit(0); |
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
/* vi: set ts=4 sw=4 expandtab: */ |