testdrawchessboard.c 4.3 KB

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