testdrawchessboard.c 3.9 KB

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