testdrawchessboard.c 3.6 KB

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