SDL  2.0
testdrawchessboard.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
3 
4  This software is provided 'as-is', without any express or implied
5  warranty. In no event will the authors be held liable for any damages
6  arising from the use of this software.
7 
8  Permission is granted to anyone to use this software for any purpose,
9  including commercial applications, and to alter it and redistribute it
10  freely.
11 
12  This file is created by : Nitin Jain (nitin.j4@samsung.com)
13 */
14 
15 /* Sample program: Draw a Chess Board by using SDL_CreateSoftwareRenderer API */
16 
17 #include <stdlib.h>
18 #include <stdio.h>
19 
20 #ifdef __EMSCRIPTEN__
21 #include <emscripten/emscripten.h>
22 #endif
23 
24 #include "SDL.h"
25 
28 int done;
29 
30 void
32 {
33  int row = 0,column = 0,x = 0;
34  SDL_Rect rect, darea;
35 
36  /* Get the Size of drawing surface */
37  SDL_RenderGetViewport(renderer, &darea);
38 
39  for( ; row < 8; row++)
40  {
41  column = row%2;
42  x = column;
43  for( ; column < 4+(row%2); column++)
44  {
45  SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF);
46 
47  rect.w = darea.w/8;
48  rect.h = darea.h/8;
49  rect.x = x * rect.w;
50  rect.y = row * rect.h;
51  x = x + 2;
52  SDL_RenderFillRect(renderer, &rect);
53  }
54  }
55 }
56 
57 void
59 {
60  SDL_Event e;
61  while (SDL_PollEvent(&e)) {
62  if (e.type == SDL_QUIT) {
63  done = 1;
64 #ifdef __EMSCRIPTEN__
65  emscripten_cancel_main_loop();
66 #endif
67  return;
68  }
69 
70  if ((e.type == SDL_KEYDOWN) && (e.key.keysym.sym == SDLK_ESCAPE)) {
71  done = 1;
72 #ifdef __EMSCRIPTEN__
73  emscripten_cancel_main_loop();
74 #endif
75  return;
76  }
77  }
78 
79  DrawChessBoard(renderer);
80 
81  /* Got everything on rendering surface,
82  now Update the drawing image on window screen */
84 }
85 
86 int
87 main(int argc, char *argv[])
88 {
89  SDL_Surface *surface;
90 
91  /* Enable standard application logging */
93 
94  /* Initialize SDL */
95  if(SDL_Init(SDL_INIT_VIDEO) != 0)
96  {
97  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init fail : %s\n", SDL_GetError());
98  return 1;
99  }
100 
101 
102  /* Create window and renderer for given surface */
104  if(!window)
105  {
106  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Window creation fail : %s\n",SDL_GetError());
107  return 1;
108  }
109  surface = SDL_GetWindowSurface(window);
110  renderer = SDL_CreateSoftwareRenderer(surface);
111  if(!renderer)
112  {
113  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Render creation for surface fail : %s\n",SDL_GetError());
114  return 1;
115  }
116 
117  /* Clear the rendering surface with the specified color */
118  SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
119  SDL_RenderClear(renderer);
120 
121 
122  /* Draw the Image on rendering surface */
123  done = 0;
124 #ifdef __EMSCRIPTEN__
125  emscripten_set_main_loop(loop, 0, 1);
126 #else
127  while (!done) {
128  loop();
129  }
130 #endif
131 
132  SDL_Quit();
133  return 0;
134 }
135 
#define SDL_PollEvent
#define SDL_GetError
int main(int argc, char *argv[])
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 Uint32 * e
GLenum GLenum void void * column
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1567
SDL_Rect rect
Definition: testrelative.c:27
#define SDL_RenderFillRect
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
#define SDL_CreateWindow
#define SDL_WINDOWPOS_UNDEFINED
Definition: SDL_video.h:120
#define SDL_LogError
void DrawChessBoard(SDL_Renderer *renderer)
#define SDL_UpdateWindowSurface
void loop()
#define SDL_Quit
SDL_Window * window
#define SDL_RenderGetViewport
#define SDL_GetWindowSurface
int x
Definition: SDL_rect.h:66
SDL_Keysym keysym
Definition: SDL_events.h:196
int w
Definition: SDL_rect.h:67
SDL_Renderer * renderer
#define SDL_CreateSoftwareRenderer
#define SDL_LogSetPriority
#define SDL_RenderClear
SDL_KeyboardEvent key
Definition: SDL_events.h:526
int h
Definition: SDL_rect.h:67
The type used to identify a window.
Definition: SDL_sysvideo.h:71
SDL_Keycode sym
Definition: SDL_keyboard.h:50
#define SDL_Init
General event structure.
Definition: SDL_events.h:521
#define SDL_SetRenderDrawColor
int done
GLenum GLenum void * row
int y
Definition: SDL_rect.h:66
#define SDL_INIT_VIDEO
Definition: SDL.h:77
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
Uint32 type
Definition: SDL_events.h:523