1 | /* |
2 | Copyright (C) 1997-2021 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 | |
26 | SDL_Window *window; |
27 | SDL_Renderer *renderer; |
28 | SDL_Surface *surface; |
29 | int done; |
30 | |
31 | void |
32 | DrawChessBoard() |
33 | { |
34 | int row = 0,column = 0,x = 0; |
35 | SDL_Rect rect, darea; |
36 | |
37 | /* Get the Size of drawing surface */ |
38 | SDL_RenderGetViewport(renderer, &darea); |
39 | |
40 | for( ; row < 8; row++) |
41 | { |
42 | column = row%2; |
43 | x = column; |
44 | for( ; column < 4+(row%2); column++) |
45 | { |
46 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF); |
47 | |
48 | rect.w = darea.w/8; |
49 | rect.h = darea.h/8; |
50 | rect.x = x * rect.w; |
51 | rect.y = row * rect.h; |
52 | x = x + 2; |
53 | SDL_RenderFillRect(renderer, &rect); |
54 | } |
55 | } |
56 | } |
57 | |
58 | void |
59 | loop() |
60 | { |
61 | SDL_Event e; |
62 | while (SDL_PollEvent(&e)) { |
63 | |
64 | /* Re-create when window has been resized */ |
65 | if ((e.type == SDL_WINDOWEVENT) && (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)) { |
66 | |
67 | SDL_DestroyRenderer(renderer); |
68 | |
69 | surface = SDL_GetWindowSurface(window); |
70 | renderer = SDL_CreateSoftwareRenderer(surface); |
71 | /* Clear the rendering surface with the specified color */ |
72 | SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); |
73 | SDL_RenderClear(renderer); |
74 | } |
75 | |
76 | if (e.type == SDL_QUIT) { |
77 | done = 1; |
78 | #ifdef __EMSCRIPTEN__ |
79 | emscripten_cancel_main_loop(); |
80 | #endif |
81 | return; |
82 | } |
83 | |
84 | if ((e.type == SDL_KEYDOWN) && (e.key.keysym.sym == SDLK_ESCAPE)) { |
85 | done = 1; |
86 | #ifdef __EMSCRIPTEN__ |
87 | emscripten_cancel_main_loop(); |
88 | #endif |
89 | return; |
90 | } |
91 | } |
92 | |
93 | DrawChessBoard(); |
94 | |
95 | /* Got everything on rendering surface, |
96 | now Update the drawing image on window screen */ |
97 | SDL_UpdateWindowSurface(window); |
98 | } |
99 | |
100 | int |
101 | main(int argc, char *argv[]) |
102 | { |
103 | /* Enable standard application logging */ |
104 | SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
105 | |
106 | /* Initialize SDL */ |
107 | if(SDL_Init(SDL_INIT_VIDEO) != 0) |
108 | { |
109 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init fail : %s\n" , SDL_GetError()); |
110 | return 1; |
111 | } |
112 | |
113 | |
114 | /* Create window and renderer for given surface */ |
115 | window = SDL_CreateWindow("Chess Board" , SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE); |
116 | if(!window) |
117 | { |
118 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Window creation fail : %s\n" ,SDL_GetError()); |
119 | return 1; |
120 | } |
121 | surface = SDL_GetWindowSurface(window); |
122 | renderer = SDL_CreateSoftwareRenderer(surface); |
123 | if(!renderer) |
124 | { |
125 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Render creation for surface fail : %s\n" ,SDL_GetError()); |
126 | return 1; |
127 | } |
128 | |
129 | /* Clear the rendering surface with the specified color */ |
130 | SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); |
131 | SDL_RenderClear(renderer); |
132 | |
133 | |
134 | /* Draw the Image on rendering surface */ |
135 | done = 0; |
136 | #ifdef __EMSCRIPTEN__ |
137 | emscripten_set_main_loop(loop, 0, 1); |
138 | #else |
139 | while (!done) { |
140 | loop(); |
141 | } |
142 | #endif |
143 | |
144 | SDL_Quit(); |
145 | return 0; |
146 | } |
147 | |
148 | |