testdrawchessboard.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 <SDL3/SDL.h>
  15. #include <SDL3/SDL_main.h>
  16. #include <SDL3/SDL_test.h>
  17. #ifdef SDL_PLATFORM_EMSCRIPTEN
  18. #include <emscripten/emscripten.h>
  19. #endif
  20. static SDL_Window *window;
  21. static SDL_Renderer *renderer;
  22. static int done;
  23. #ifdef USE_SOFTWARE_RENDERER
  24. static SDL_Surface *surface;
  25. #endif
  26. static void DrawChessBoard(void)
  27. {
  28. int row = 0, column = 0, x = 0;
  29. SDL_FRect rect;
  30. SDL_Rect darea;
  31. /* Get the Size of drawing surface */
  32. SDL_GetRenderViewport(renderer, &darea);
  33. for (; row < 8; row++) {
  34. column = row % 2;
  35. x = column;
  36. for (; column < 4 + (row % 2); column++) {
  37. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF);
  38. rect.w = (float)(darea.w / 8);
  39. rect.h = (float)(darea.h / 8);
  40. rect.x = (float)(x * rect.w);
  41. rect.y = (float)(row * rect.h);
  42. x = x + 2;
  43. SDL_RenderFillRect(renderer, &rect);
  44. /* Draw a red diagonal line through the upper left rectangle */
  45. if (column == 0 && row == 0) {
  46. SDL_SetRenderDrawColor(renderer, 0xFF, 0, 0, 0xFF);
  47. SDL_RenderLine(renderer, 0, 0, rect.w, rect.h);
  48. }
  49. }
  50. }
  51. }
  52. static void loop(void)
  53. {
  54. SDL_Event e;
  55. while (SDL_PollEvent(&e)) {
  56. #ifdef USE_SOFTWARE_RENDERER
  57. /* Re-create when window surface has been resized */
  58. if (e.type == SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED) {
  59. SDL_DestroyRenderer(renderer);
  60. surface = SDL_GetWindowSurface(window);
  61. renderer = SDL_CreateSoftwareRenderer(surface);
  62. /* Clear the rendering surface with the specified color */
  63. SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
  64. SDL_RenderClear(renderer);
  65. }
  66. #endif
  67. if (e.type == SDL_EVENT_QUIT) {
  68. done = 1;
  69. #ifdef SDL_PLATFORM_EMSCRIPTEN
  70. emscripten_cancel_main_loop();
  71. #endif
  72. return;
  73. }
  74. if ((e.type == SDL_EVENT_KEY_DOWN) && (e.key.key == SDLK_ESCAPE)) {
  75. done = 1;
  76. #ifdef SDL_PLATFORM_EMSCRIPTEN
  77. emscripten_cancel_main_loop();
  78. #endif
  79. return;
  80. }
  81. }
  82. /* Clear the rendering surface with the specified color */
  83. SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
  84. SDL_RenderClear(renderer);
  85. DrawChessBoard();
  86. SDL_RenderPresent(renderer);
  87. #ifdef USE_SOFTWARE_RENDERER
  88. /* Got everything on rendering surface,
  89. now Update the drawing image on window screen */
  90. SDL_UpdateWindowSurface(window);
  91. #endif
  92. }
  93. int main(int argc, char *argv[])
  94. {
  95. SDLTest_CommonState *state;
  96. /* Initialize test framework */
  97. state = SDLTest_CommonCreateState(argv, 0);
  98. if (!state) {
  99. return 1;
  100. }
  101. /* Parse commandline */
  102. if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
  103. return 1;
  104. }
  105. /* Initialize SDL */
  106. if (!SDL_Init(SDL_INIT_VIDEO)) {
  107. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init fail : %s", SDL_GetError());
  108. return 1;
  109. }
  110. /* Create window and renderer for given surface */
  111. window = SDL_CreateWindow("Chess Board", 640, 480, SDL_WINDOW_RESIZABLE);
  112. if (!window) {
  113. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Window creation fail : %s", SDL_GetError());
  114. return 1;
  115. }
  116. #ifdef USE_SOFTWARE_RENDERER
  117. surface = SDL_GetWindowSurface(window);
  118. renderer = SDL_CreateSoftwareRenderer(surface);
  119. #else
  120. renderer = SDL_CreateRenderer(window, NULL);
  121. #endif
  122. if (!renderer) {
  123. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Render creation for surface fail : %s", SDL_GetError());
  124. return 1;
  125. }
  126. /* Draw the Image on rendering surface */
  127. done = 0;
  128. #ifdef SDL_PLATFORM_EMSCRIPTEN
  129. emscripten_set_main_loop(loop, 0, 1);
  130. #else
  131. while (!done) {
  132. loop();
  133. }
  134. #endif
  135. SDL_DestroyRenderer(renderer);
  136. SDL_DestroyWindow(window);
  137. SDL_Quit();
  138. SDLTest_CommonDestroyState(state);
  139. return 0;
  140. }