teststreaming.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. Copyright (C) 1997-2014 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. #include "SDL.h"
  18. #define MOOSEPIC_W 64
  19. #define MOOSEPIC_H 88
  20. #define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
  21. #define MOOSEFRAMES_COUNT 10
  22. SDL_Color MooseColors[84] = {
  23. {49, 49, 49}, {66, 24, 0}, {66, 33, 0}, {66, 66, 66},
  24. {66, 115, 49}, {74, 33, 0}, {74, 41, 16}, {82, 33, 8},
  25. {82, 41, 8}, {82, 49, 16}, {82, 82, 82}, {90, 41, 8},
  26. {90, 41, 16}, {90, 57, 24}, {99, 49, 16}, {99, 66, 24},
  27. {99, 66, 33}, {99, 74, 33}, {107, 57, 24}, {107, 82, 41},
  28. {115, 57, 33}, {115, 66, 33}, {115, 66, 41}, {115, 74, 0},
  29. {115, 90, 49}, {115, 115, 115}, {123, 82, 0}, {123, 99, 57},
  30. {132, 66, 41}, {132, 74, 41}, {132, 90, 8}, {132, 99, 33},
  31. {132, 99, 66}, {132, 107, 66}, {140, 74, 49}, {140, 99, 16},
  32. {140, 107, 74}, {140, 115, 74}, {148, 107, 24}, {148, 115, 82},
  33. {148, 123, 74}, {148, 123, 90}, {156, 115, 33}, {156, 115, 90},
  34. {156, 123, 82}, {156, 132, 82}, {156, 132, 99}, {156, 156, 156},
  35. {165, 123, 49}, {165, 123, 90}, {165, 132, 82}, {165, 132, 90},
  36. {165, 132, 99}, {165, 140, 90}, {173, 132, 57}, {173, 132, 99},
  37. {173, 140, 107}, {173, 140, 115}, {173, 148, 99}, {173, 173, 173},
  38. {181, 140, 74}, {181, 148, 115}, {181, 148, 123}, {181, 156, 107},
  39. {189, 148, 123}, {189, 156, 82}, {189, 156, 123}, {189, 156, 132},
  40. {189, 189, 189}, {198, 156, 123}, {198, 165, 132}, {206, 165, 99},
  41. {206, 165, 132}, {206, 173, 140}, {206, 206, 206}, {214, 173, 115},
  42. {214, 173, 140}, {222, 181, 148}, {222, 189, 132}, {222, 189, 156},
  43. {222, 222, 222}, {231, 198, 165}, {231, 231, 231}, {239, 206, 173}
  44. };
  45. Uint8 MooseFrames[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE];
  46. void quit(int rc)
  47. {
  48. SDL_Quit();
  49. exit(rc);
  50. }
  51. void UpdateTexture(SDL_Texture *texture, int frame)
  52. {
  53. SDL_Color *color;
  54. Uint8 *src;
  55. Uint32 *dst;
  56. int row, col;
  57. void *pixels;
  58. int pitch;
  59. if (SDL_LockTexture(texture, NULL, &pixels, &pitch) < 0) {
  60. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't lock texture: %s\n", SDL_GetError());
  61. quit(5);
  62. }
  63. src = MooseFrames[frame];
  64. for (row = 0; row < MOOSEPIC_H; ++row) {
  65. dst = (Uint32*)((Uint8*)pixels + row * pitch);
  66. for (col = 0; col < MOOSEPIC_W; ++col) {
  67. color = &MooseColors[*src++];
  68. *dst++ = (0xFF000000|(color->r<<16)|(color->g<<8)|color->b);
  69. }
  70. }
  71. SDL_UnlockTexture(texture);
  72. }
  73. int
  74. main(int argc, char **argv)
  75. {
  76. SDL_Window *window;
  77. SDL_Renderer *renderer;
  78. SDL_RWops *handle;
  79. SDL_Texture *MooseTexture;
  80. SDL_Event event;
  81. SDL_bool done = SDL_FALSE;
  82. int frame;
  83. /* Enable standard application logging */
  84. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  85. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  86. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  87. return 1;
  88. }
  89. /* load the moose images */
  90. handle = SDL_RWFromFile("moose.dat", "rb");
  91. if (handle == NULL) {
  92. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't find the file moose.dat !\n");
  93. quit(2);
  94. }
  95. SDL_RWread(handle, MooseFrames, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
  96. SDL_RWclose(handle);
  97. /* Create the window and renderer */
  98. window = SDL_CreateWindow("Happy Moose",
  99. SDL_WINDOWPOS_UNDEFINED,
  100. SDL_WINDOWPOS_UNDEFINED,
  101. MOOSEPIC_W*4, MOOSEPIC_H*4,
  102. SDL_WINDOW_RESIZABLE);
  103. if (!window) {
  104. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create window: %s\n", SDL_GetError());
  105. quit(3);
  106. }
  107. renderer = SDL_CreateRenderer(window, -1, 0);
  108. if (!renderer) {
  109. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s\n", SDL_GetError());
  110. quit(4);
  111. }
  112. MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
  113. if (!MooseTexture) {
  114. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s\n", SDL_GetError());
  115. quit(5);
  116. }
  117. /* Loop, waiting for QUIT or the escape key */
  118. frame = 0;
  119. while (!done) {
  120. while (SDL_PollEvent(&event)) {
  121. switch (event.type) {
  122. case SDL_KEYDOWN:
  123. if (event.key.keysym.sym == SDLK_ESCAPE) {
  124. done = SDL_TRUE;
  125. }
  126. break;
  127. case SDL_QUIT:
  128. done = SDL_TRUE;
  129. break;
  130. }
  131. }
  132. frame = (frame + 1) % MOOSEFRAMES_COUNT;
  133. UpdateTexture(MooseTexture, frame);
  134. SDL_RenderClear(renderer);
  135. SDL_RenderCopy(renderer, MooseTexture, NULL, NULL);
  136. SDL_RenderPresent(renderer);
  137. }
  138. SDL_DestroyRenderer(renderer);
  139. quit(0);
  140. return 0;
  141. }
  142. /* vi: set ts=4 sw=4 expandtab: */