1
0

testdrawchessboard.c 4.0 KB

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