teststreaming.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. Copyright (C) 1997-2023 Sam Lantinga <[email protected]>
  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. #ifdef __EMSCRIPTEN__
  17. #include <emscripten/emscripten.h>
  18. #endif
  19. #include <SDL3/SDL.h>
  20. #include <SDL3/SDL_main.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. /* *INDENT-OFF* */ /* clang-format off */
  27. static SDL_Color MooseColors[84] = {
  28. {49, 49, 49, 255}, {66, 24, 0, 255}, {66, 33, 0, 255}, {66, 66, 66, 255},
  29. {66, 115, 49, 255}, {74, 33, 0, 255}, {74, 41, 16, 255}, {82, 33, 8, 255},
  30. {82, 41, 8, 255}, {82, 49, 16, 255}, {82, 82, 82, 255}, {90, 41, 8, 255},
  31. {90, 41, 16, 255}, {90, 57, 24, 255}, {99, 49, 16, 255}, {99, 66, 24, 255},
  32. {99, 66, 33, 255}, {99, 74, 33, 255}, {107, 57, 24, 255}, {107, 82, 41, 255},
  33. {115, 57, 33, 255}, {115, 66, 33, 255}, {115, 66, 41, 255}, {115, 74, 0, 255},
  34. {115, 90, 49, 255}, {115, 115, 115, 255}, {123, 82, 0, 255}, {123, 99, 57, 255},
  35. {132, 66, 41, 255}, {132, 74, 41, 255}, {132, 90, 8, 255}, {132, 99, 33, 255},
  36. {132, 99, 66, 255}, {132, 107, 66, 255}, {140, 74, 49, 255}, {140, 99, 16, 255},
  37. {140, 107, 74, 255}, {140, 115, 74, 255}, {148, 107, 24, 255}, {148, 115, 82, 255},
  38. {148, 123, 74, 255}, {148, 123, 90, 255}, {156, 115, 33, 255}, {156, 115, 90, 255},
  39. {156, 123, 82, 255}, {156, 132, 82, 255}, {156, 132, 99, 255}, {156, 156, 156, 255},
  40. {165, 123, 49, 255}, {165, 123, 90, 255}, {165, 132, 82, 255}, {165, 132, 90, 255},
  41. {165, 132, 99, 255}, {165, 140, 90, 255}, {173, 132, 57, 255}, {173, 132, 99, 255},
  42. {173, 140, 107, 255}, {173, 140, 115, 255}, {173, 148, 99, 255}, {173, 173, 173, 255},
  43. {181, 140, 74, 255}, {181, 148, 115, 255}, {181, 148, 123, 255}, {181, 156, 107, 255},
  44. {189, 148, 123, 255}, {189, 156, 82, 255}, {189, 156, 123, 255}, {189, 156, 132, 255},
  45. {189, 189, 189, 255}, {198, 156, 123, 255}, {198, 165, 132, 255}, {206, 165, 99, 255},
  46. {206, 165, 132, 255}, {206, 173, 140, 255}, {206, 206, 206, 255}, {214, 173, 115, 255},
  47. {214, 173, 140, 255}, {222, 181, 148, 255}, {222, 189, 132, 255}, {222, 189, 156, 255},
  48. {222, 222, 222, 255}, {231, 198, 165, 255}, {231, 231, 231, 255}, {239, 206, 173, 255}
  49. };
  50. /* *INDENT-ON* */ /* clang-format on */
  51. static Uint8 MooseFrames[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE];
  52. static SDL_Renderer *renderer;
  53. static int frame;
  54. static SDL_Texture *MooseTexture;
  55. static SDL_bool done = SDL_FALSE;
  56. static void quit(int rc)
  57. {
  58. SDL_Quit();
  59. exit(rc);
  60. }
  61. static void UpdateTexture(SDL_Texture *texture)
  62. {
  63. SDL_Color *color;
  64. Uint8 *src;
  65. Uint32 *dst;
  66. int row, col;
  67. void *pixels;
  68. int pitch;
  69. if (SDL_LockTexture(texture, NULL, &pixels, &pitch) < 0) {
  70. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't lock texture: %s\n", SDL_GetError());
  71. quit(5);
  72. }
  73. src = MooseFrames[frame];
  74. for (row = 0; row < MOOSEPIC_H; ++row) {
  75. dst = (Uint32 *)((Uint8 *)pixels + row * pitch);
  76. for (col = 0; col < MOOSEPIC_W; ++col) {
  77. color = &MooseColors[*src++];
  78. *dst++ = (0xFF000000 | (color->r << 16) | (color->g << 8) | color->b);
  79. }
  80. }
  81. SDL_UnlockTexture(texture);
  82. }
  83. static void loop(void)
  84. {
  85. SDL_Event event;
  86. while (SDL_PollEvent(&event)) {
  87. switch (event.type) {
  88. case SDL_EVENT_KEY_DOWN:
  89. if (event.key.keysym.sym == SDLK_ESCAPE) {
  90. done = SDL_TRUE;
  91. }
  92. break;
  93. case SDL_EVENT_QUIT:
  94. done = SDL_TRUE;
  95. break;
  96. }
  97. }
  98. frame = (frame + 1) % MOOSEFRAMES_COUNT;
  99. UpdateTexture(MooseTexture);
  100. SDL_RenderClear(renderer);
  101. SDL_RenderTexture(renderer, MooseTexture, NULL, NULL);
  102. SDL_RenderPresent(renderer);
  103. #ifdef __EMSCRIPTEN__
  104. if (done) {
  105. emscripten_cancel_main_loop();
  106. }
  107. #endif
  108. }
  109. int 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", MOOSEPIC_W * 4, MOOSEPIC_H * 4, SDL_WINDOW_RESIZABLE);
  136. if (window == NULL) {
  137. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create window: %s\n", SDL_GetError());
  138. quit(3);
  139. }
  140. renderer = SDL_CreateRenderer(window, NULL, 0);
  141. if (renderer == NULL) {
  142. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s\n", SDL_GetError());
  143. quit(4);
  144. }
  145. MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
  146. if (MooseTexture == NULL) {
  147. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s\n", SDL_GetError());
  148. quit(5);
  149. }
  150. /* Loop, waiting for QUIT or the escape key */
  151. frame = 0;
  152. #ifdef __EMSCRIPTEN__
  153. emscripten_set_main_loop(loop, 0, 1);
  154. #else
  155. while (!done) {
  156. loop();
  157. }
  158. #endif
  159. SDL_DestroyRenderer(renderer);
  160. quit(0);
  161. return 0;
  162. }