testdrawchessboard.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright (C) 1997-2014 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. #include "SDL.h"
  15. void
  16. DrawChessBoard(SDL_Renderer * renderer)
  17. {
  18. int row = 0,coloum = 0,x = 0;
  19. SDL_Rect rect, darea;
  20. /* Get the Size of drawing surface */
  21. SDL_RenderGetViewport(renderer, &darea);
  22. for(row; row < 8; row++)
  23. {
  24. coloum = row%2;
  25. x = x + coloum;
  26. for(coloum; coloum < 4+(row%2); coloum++)
  27. {
  28. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF);
  29. rect.w = darea.w/8;
  30. rect.h = darea.h/8;
  31. rect.x = x * rect.w;
  32. rect.y = row * rect.h;
  33. x = x + 2;
  34. SDL_RenderFillRect(renderer, &rect);
  35. }
  36. x=0;
  37. }
  38. }
  39. int
  40. main(int argc, char *argv[])
  41. {
  42. SDL_Window *window;
  43. SDL_Surface *surface;
  44. SDL_Renderer *renderer;
  45. /* Enable standard application logging */
  46. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  47. /* Initialize SDL */
  48. if(SDL_Init(SDL_INIT_VIDEO) != 0)
  49. {
  50. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init fail : %s\n", SDL_GetError());
  51. return 1;
  52. }
  53. /* Create window and renderer for given surface */
  54. window = SDL_CreateWindow("Chess Board", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
  55. if(!window)
  56. {
  57. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Window creation fail : %s\n",SDL_GetError());
  58. return 1;
  59. }
  60. surface = SDL_GetWindowSurface(window);
  61. renderer = SDL_CreateSoftwareRenderer(surface);
  62. if(!renderer)
  63. {
  64. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Render creation for surface fail : %s\n",SDL_GetError());
  65. return 1;
  66. }
  67. /* Clear the rendering surface with the specified color */
  68. SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
  69. SDL_RenderClear(renderer);
  70. /* Draw the Image on rendering surface */
  71. while(1)
  72. {
  73. SDL_Event e;
  74. if (SDL_PollEvent(&e)) {
  75. if (e.type == SDL_QUIT)
  76. break;
  77. if(e.key.keysym.sym == SDLK_ESCAPE)
  78. break;
  79. }
  80. DrawChessBoard(renderer);
  81. /* Got everything on rendering surface,
  82. now Update the drawing image on window screen */
  83. SDL_UpdateWindowSurface(window);
  84. }
  85. return 0;
  86. }