testdrawchessboard.c 3.7 KB

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