Debian Codegraph @

test/testdrawchessboard.c

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