Atlas - testdrawchessboard.c

Home / ext / SDL / test Lines: 1 | Size: 4692 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Copyright (C) 1997-2025 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 (nitin.j4\samsung.com) 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 <SDL3/SDL.h> 21#include <SDL3/SDL_main.h> 22#include <SDL3/SDL_test.h> 23 24#ifdef SDL_PLATFORM_EMSCRIPTEN 25#include <emscripten/emscripten.h> 26#endif 27 28static SDL_Window *window; 29static SDL_Renderer *renderer; 30static int done; 31 32#ifdef USE_SOFTWARE_RENDERER 33static SDL_Surface *surface; 34#endif 35 36static void DrawChessBoard(void) 37{ 38 int row = 0, column = 0, x = 0; 39 SDL_FRect rect; 40 SDL_Rect darea; 41 42 /* Get the Size of drawing surface */ 43 SDL_GetRenderViewport(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 = (float)(darea.w / 8); 52 rect.h = (float)(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 /* Draw a red diagonal line through the upper left rectangle */ 59 if (column == 0 && row == 0) { 60 SDL_SetRenderDrawColor(renderer, 0xFF, 0, 0, 0xFF); 61 SDL_RenderLine(renderer, 0, 0, rect.w, rect.h); 62 } 63 } 64 } 65} 66 67static void loop(void) 68{ 69 SDL_Event e; 70 while (SDL_PollEvent(&e)) { 71 72#ifdef USE_SOFTWARE_RENDERER 73 /* Re-create when window surface has been resized */ 74 if (e.type == SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED) { 75 76 SDL_DestroyRenderer(renderer); 77 78 surface = SDL_GetWindowSurface(window); 79 renderer = SDL_CreateSoftwareRenderer(surface); 80 /* Clear the rendering surface with the specified color */ 81 SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); 82 SDL_RenderClear(renderer); 83 } 84#endif 85 86 if (e.type == SDL_EVENT_QUIT) { 87 done = 1; 88#ifdef SDL_PLATFORM_EMSCRIPTEN 89 emscripten_cancel_main_loop(); 90#endif 91 return; 92 } 93 94 if ((e.type == SDL_EVENT_KEY_DOWN) && (e.key.key == SDLK_ESCAPE)) { 95 done = 1; 96#ifdef SDL_PLATFORM_EMSCRIPTEN 97 emscripten_cancel_main_loop(); 98#endif 99 return; 100 } 101 } 102 103 /* Clear the rendering surface with the specified color */ 104 SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); 105 SDL_RenderClear(renderer); 106 107 DrawChessBoard(); 108 109 SDL_RenderPresent(renderer); 110 111#ifdef USE_SOFTWARE_RENDERER 112 /* Got everything on rendering surface, 113 now Update the drawing image on window screen */ 114 SDL_UpdateWindowSurface(window); 115#endif 116} 117 118int main(int argc, char *argv[]) 119{ 120 SDLTest_CommonState *state; 121 122 /* Initialize test framework */ 123 state = SDLTest_CommonCreateState(argv, 0); 124 if (!state) { 125 return 1; 126 } 127 128 /* Parse commandline */ 129 if (!SDLTest_CommonDefaultArgs(state, argc, argv)) { 130 return 1; 131 } 132 133 /* Initialize SDL */ 134 if (!SDL_Init(SDL_INIT_VIDEO)) { 135 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init fail : %s", SDL_GetError()); 136 return 1; 137 } 138 139 /* Create window and renderer for given surface */ 140 window = SDL_CreateWindow("Chess Board", 640, 480, SDL_WINDOW_RESIZABLE); 141 if (!window) { 142 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Window creation fail : %s", SDL_GetError()); 143 return 1; 144 } 145#ifdef USE_SOFTWARE_RENDERER 146 surface = SDL_GetWindowSurface(window); 147 renderer = SDL_CreateSoftwareRenderer(surface); 148#else 149 renderer = SDL_CreateRenderer(window, NULL); 150#endif 151 if (!renderer) { 152 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Render creation for surface fail : %s", SDL_GetError()); 153 return 1; 154 } 155 156 /* Draw the Image on rendering surface */ 157 done = 0; 158#ifdef SDL_PLATFORM_EMSCRIPTEN 159 emscripten_set_main_loop(loop, 0, 1); 160#else 161 while (!done) { 162 loop(); 163 } 164#endif 165 166 SDL_DestroyRenderer(renderer); 167 SDL_DestroyWindow(window); 168 169 SDL_Quit(); 170 SDLTest_CommonDestroyState(state); 171 return 0; 172} 173
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.