cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

testdrawchessboard.c (2532B)


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