Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. // emcc source.cpp -s USE_SDL=2 -s FULL_ES2=1 --preload-file res/img -o publish\emsripten\index.html -O2 -s ALLOW_MEMORY_GROWTH=1
  2.  
  3. #include <SDL.h>
  4. #include <stdio.h>
  5. #include <string>
  6. #ifdef __EMSCRIPTEN__
  7. #include <emscripten.h>
  8. #endif
  9.  
  10. const int SCREEN_WIDTH = 640;
  11. const int SCREEN_HEIGHT = 480;
  12.  
  13. enum KeyPressSurfaces
  14. {
  15. KEY_PRESS_SURFACE_DEFAULT,
  16. KEY_PRESS_SURFACE_UP,
  17. KEY_PRESS_SURFACE_DOWN,
  18. KEY_PRESS_SURFACE_LEFT,
  19. KEY_PRESS_SURFACE_RIGHT,
  20. KEY_PRESS_SURFACE_TOTAL
  21. };
  22.  
  23. bool init();
  24. bool loadMedia();
  25. void close();
  26. SDL_Surface* loadSurface(std::string path);
  27.  
  28. SDL_Window* gWindow = NULL;
  29. SDL_Surface* gScreenSurface = NULL;
  30. //SDL_Surface* gXOut = NULL;
  31. SDL_Surface* gKeyPressSurfaces[KEY_PRESS_SURFACE_TOTAL];
  32. SDL_Surface* gCurrentSurface = NULL;
  33. SDL_Event e;
  34. bool quit = false;
  35.  
  36. SDL_Surface* loadSurface(std::string path)
  37. {
  38. //Load image at specified path
  39. SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str());
  40. if (loadedSurface == NULL)
  41. {
  42. printf("Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
  43. }
  44.  
  45. return loadedSurface;
  46. }
  47.  
  48. bool init()
  49. {
  50. //Initialization flag
  51. bool success = true;
  52.  
  53. //Initialize SDL
  54. if (SDL_Init(SDL_INIT_VIDEO) < 0)
  55. {
  56. printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
  57. success = false;
  58. }
  59. else
  60. {
  61. //Create window
  62. gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  63. if (gWindow == NULL)
  64. {
  65. printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
  66. success = false;
  67. }
  68. else
  69. {
  70. //Get window surface
  71. gScreenSurface = SDL_GetWindowSurface(gWindow);
  72. }
  73. }
  74.  
  75. return success;
  76. }
  77.  
  78.  
  79. bool loadMedia()
  80. {
  81. //Loading success flag
  82. bool success = true;
  83.  
  84. //Load default surface
  85. gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT] = loadSurface("./res/img/press.bmp");
  86. if (gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT] == NULL)
  87. {
  88. printf("Failed to load default image!\n");
  89. success = false;
  90. }
  91.  
  92. //Load up surface
  93. gKeyPressSurfaces[KEY_PRESS_SURFACE_UP] = loadSurface("./res/img/up.bmp");
  94. if (gKeyPressSurfaces[KEY_PRESS_SURFACE_UP] == NULL)
  95. {
  96. printf("Failed to load up image!\n");
  97. success = false;
  98. }
  99.  
  100. //Load down surface
  101. gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN] = loadSurface("./res/img/down.bmp");
  102. if (gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN] == NULL)
  103. {
  104. printf("Failed to load down image!\n");
  105. success = false;
  106. }
  107.  
  108. //Load left surface
  109. gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT] = loadSurface("./res/img/left.bmp");
  110. if (gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT] == NULL)
  111. {
  112. printf("Failed to load left image!\n");
  113. success = false;
  114. }
  115.  
  116. //Load right surface
  117. gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT] = loadSurface("./res/img/right.bmp");
  118. if (gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT] == NULL)
  119. {
  120. printf("Failed to load right image!\n");
  121. success = false;
  122. }
  123.  
  124. gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT];
  125.  
  126. return success;
  127. }
  128.  
  129. void close()
  130. {
  131. //Deallocate surfaces
  132. for (int i = 0; i < KEY_PRESS_SURFACE_TOTAL; ++i)
  133. {
  134. SDL_FreeSurface(gKeyPressSurfaces[i]);
  135. gKeyPressSurfaces[i] = NULL;
  136. }
  137.  
  138. //Destroy window
  139. SDL_DestroyWindow(gWindow);
  140. gWindow = NULL;
  141.  
  142. //Quit SDL subsystems
  143. SDL_Quit();
  144. }
  145.  
  146.  
  147. void main_loop()
  148. {
  149. //Handle events on queue
  150. while (SDL_PollEvent(&e) != 0)
  151. {
  152. //User requests quit
  153. if (e.type == SDL_QUIT)
  154. {
  155. quit = true;
  156. }
  157. //User presses a key
  158. else if (e.type == SDL_KEYDOWN)
  159. {
  160. //Select surfaces based on key press
  161. switch (e.key.keysym.sym)
  162. {
  163. case SDLK_UP:
  164. gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_UP];
  165. break;
  166.  
  167. case SDLK_DOWN:
  168. gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN];
  169. break;
  170.  
  171. case SDLK_LEFT:
  172. gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT];
  173. break;
  174.  
  175. case SDLK_RIGHT:
  176. gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT];
  177. break;
  178.  
  179. default:
  180. gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT];
  181. break;
  182. }
  183. }
  184. }
  185.  
  186. //Apply the image
  187. SDL_BlitSurface(gCurrentSurface, NULL, gScreenSurface, NULL);
  188.  
  189. //Update the surface
  190. SDL_UpdateWindowSurface(gWindow);
  191.  
  192. }
  193.  
  194. int main(int argc, char* args[])
  195. {
  196. //Start up SDL and create window
  197. if (!init())
  198. {
  199. printf("Failed to initialize!\n");
  200. }
  201. else
  202. {
  203. //Load media
  204. if (!loadMedia())
  205. {
  206. printf("Failed to load media!\n");
  207. }
  208. else
  209. {
  210. #ifdef __EMSCRIPTEN__
  211. emscripten_set_main_loop(main_loop, 0, 1);
  212. #else
  213.  
  214. //While application is running
  215. while (!quit)
  216. {
  217. main_loop();
  218. }
  219. SDL_Delay(100);
  220. #endif
  221. }
  222. }
  223.  
  224. //Free resources and close SDL
  225. close();
  226.  
  227. return 0;
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement