🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
3.7 KiB

  1. /*
  2. Copyright (C) 1997-2019 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. #include <stdlib.h>
  13. #include <stdio.h>
  14. #ifdef __EMSCRIPTEN__
  15. #include <emscripten/emscripten.h>
  16. #endif
  17. #include "SDL.h"
  18. SDL_Window *window;
  19. SDL_Renderer *renderer;
  20. SDL_Surface *surface;
  21. int done;
  22. void
  23. DrawChessBoard(SDL_Renderer * renderer)
  24. {
  25. int row = 0,column = 0,x = 0;
  26. SDL_Rect rect, darea;
  27. /* Get the Size of drawing surface */
  28. SDL_RenderGetViewport(renderer, &darea);
  29. for( ; row < 8; row++)
  30. {
  31. column = row%2;
  32. x = column;
  33. for( ; column < 4+(row%2); column++)
  34. {
  35. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF);
  36. rect.w = darea.w/8;
  37. rect.h = darea.h/8;
  38. rect.x = x * rect.w;
  39. rect.y = row * rect.h;
  40. x = x + 2;
  41. SDL_RenderFillRect(renderer, &rect);
  42. }
  43. }
  44. }
  45. void
  46. loop()
  47. {
  48. SDL_Event e;
  49. while (SDL_PollEvent(&e)) {
  50. /* Re-create when window has been resized */
  51. if ((e.type == SDL_WINDOWEVENT) && (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)) {
  52. SDL_DestroyRenderer(renderer);
  53. surface = SDL_GetWindowSurface(window);
  54. renderer = SDL_CreateSoftwareRenderer(surface);
  55. /* Clear the rendering surface with the specified color */
  56. SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
  57. SDL_RenderClear(renderer);
  58. }
  59. if (e.type == SDL_QUIT) {
  60. done = 1;
  61. #ifdef __EMSCRIPTEN__
  62. emscripten_cancel_main_loop();
  63. #endif
  64. return;
  65. }
  66. if ((e.type == SDL_KEYDOWN) && (e.key.keysym.sym == SDLK_ESCAPE)) {
  67. done = 1;
  68. #ifdef __EMSCRIPTEN__
  69. emscripten_cancel_main_loop();
  70. #endif
  71. return;
  72. }
  73. }
  74. DrawChessBoard(renderer);
  75. /* Got everything on rendering surface,
  76. now Update the drawing image on window screen */
  77. SDL_UpdateWindowSurface(window);
  78. }
  79. int
  80. 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. {
  87. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init fail : %s\n", SDL_GetError());
  88. return 1;
  89. }
  90. /* Create window and renderer for given surface */
  91. window = SDL_CreateWindow("Chess Board", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE);
  92. if(!window)
  93. {
  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)
  100. {
  101. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Render creation for surface fail : %s\n",SDL_GetError());
  102. return 1;
  103. }
  104. /* Clear the rendering surface with the specified color */
  105. SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
  106. SDL_RenderClear(renderer);
  107. /* Draw the Image on rendering surface */
  108. done = 0;
  109. #ifdef __EMSCRIPTEN__
  110. emscripten_set_main_loop(loop, 0, 1);
  111. #else
  112. while (!done) {
  113. loop();
  114. }
  115. #endif
  116. SDL_Quit();
  117. return 0;
  118. }