🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
6.4 KiB

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