testdrawchessboard.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. Copyright (C) 1997-2016 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. This file is created by : Nitin Jain (nitin.j4@samsung.com)
  10. */
  11. /* Sample program: Draw a Chess Board by using SDL_CreateSoftwareRenderer API */
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #ifdef __EMSCRIPTEN__
  15. #include <emscripten/emscripten.h>
  16. #endif
  17. #include "SDL.h"
  18. SDL_Window *window;
  19. SDL_Renderer *renderer;
  20. int done;
  21. void
  22. DrawChessBoard(SDL_Renderer * renderer)
  23. {
  24. int row = 0,column = 0,x = 0;
  25. SDL_Rect rect, darea;
  26. /* Get the Size of drawing surface */
  27. SDL_RenderGetViewport(renderer, &darea);
  28. for( ; row < 8; row++)
  29. {
  30. column = row%2;
  31. x = column;
  32. for( ; column < 4+(row%2); column++)
  33. {
  34. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF);
  35. rect.w = darea.w/8;
  36. rect.h = darea.h/8;
  37. rect.x = x * rect.w;
  38. rect.y = row * rect.h;
  39. x = x + 2;
  40. SDL_RenderFillRect(renderer, &rect);
  41. }
  42. }
  43. }
  44. void
  45. loop()
  46. {
  47. SDL_Event e;
  48. while (SDL_PollEvent(&e)) {
  49. if (e.type == SDL_QUIT) {
  50. done = 1;
  51. #ifdef __EMSCRIPTEN__
  52. emscripten_cancel_main_loop();
  53. #endif
  54. return;
  55. }
  56. if ((e.type == SDL_KEYDOWN) && (e.key.keysym.sym == SDLK_ESCAPE)) {
  57. done = 1;
  58. #ifdef __EMSCRIPTEN__
  59. emscripten_cancel_main_loop();
  60. #endif
  61. return;
  62. }
  63. }
  64. DrawChessBoard(renderer);
  65. /* Got everything on rendering surface,
  66. now Update the drawing image on window screen */
  67. SDL_UpdateWindowSurface(window);
  68. }
  69. int
  70. main(int argc, char *argv[])
  71. {
  72. SDL_Surface *surface;
  73. /* Enable standard application logging */
  74. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  75. /* Initialize SDL */
  76. if(SDL_Init(SDL_INIT_VIDEO) != 0)
  77. {
  78. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init fail : %s\n", SDL_GetError());
  79. return 1;
  80. }
  81. /* Create window and renderer for given surface */
  82. window = SDL_CreateWindow("Chess Board", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
  83. if(!window)
  84. {
  85. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Window creation fail : %s\n",SDL_GetError());
  86. return 1;
  87. }
  88. surface = SDL_GetWindowSurface(window);
  89. renderer = SDL_CreateSoftwareRenderer(surface);
  90. if(!renderer)
  91. {
  92. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Render creation for surface fail : %s\n",SDL_GetError());
  93. return 1;
  94. }
  95. /* Clear the rendering surface with the specified color */
  96. SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
  97. SDL_RenderClear(renderer);
  98. /* Draw the Image on rendering surface */
  99. done = 0;
  100. #ifdef __EMSCRIPTEN__
  101. emscripten_set_main_loop(loop, 0, 1);
  102. #else
  103. while (!done) {
  104. loop();
  105. }
  106. #endif
  107. SDL_Quit();
  108. return 0;
  109. }