testdrawchessboard.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 ([email protected])
  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. SDL_Window *window;
  18. SDL_Renderer *renderer;
  19. SDL_Surface *surface;
  20. int done;
  21. void DrawChessBoard()
  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. void loop()
  48. {
  49. SDL_Event e;
  50. while (SDL_PollEvent(&e)) {
  51. /* Re-create when window has been resized */
  52. if (e.type == SDL_EVENT_WINDOW_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. /* Enable highdpi scaling on Windows */
  85. SDL_SetHint(SDL_HINT_WINDOWS_DPI_SCALING, "1");
  86. /* Initialize SDL */
  87. if (SDL_Init(SDL_INIT_VIDEO) != 0) {
  88. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init fail : %s\n", SDL_GetError());
  89. return 1;
  90. }
  91. /* Create window and renderer for given surface */
  92. window = SDL_CreateWindow("Chess Board", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
  93. if (window == NULL) {
  94. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Window creation fail : %s\n", SDL_GetError());
  95. return 1;
  96. }
  97. surface = SDL_GetWindowSurface(window);
  98. renderer = SDL_CreateSoftwareRenderer(surface);
  99. if (renderer == NULL) {
  100. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Render creation for surface fail : %s\n", SDL_GetError());
  101. return 1;
  102. }
  103. /* Clear the rendering surface with the specified color */
  104. SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
  105. SDL_RenderClear(renderer);
  106. /* Draw the Image on rendering surface */
  107. done = 0;
  108. #ifdef __EMSCRIPTEN__
  109. emscripten_set_main_loop(loop, 0, 1);
  110. #else
  111. while (!done) {
  112. loop();
  113. }
  114. #endif
  115. SDL_Quit();
  116. return 0;
  117. }