test/testdrawchessboard.c
changeset 7529 3f571ec09593
child 7538 1d187795e103
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testdrawchessboard.c	Sat Jul 27 03:48:23 2013 -0700
     1.3 @@ -0,0 +1,105 @@
     1.4 +/*
     1.5 +   Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     1.6 +
     1.7 +   This software is provided 'as-is', without any express or implied
     1.8 +   warranty.  In no event will the authors be held liable for any damages
     1.9 +   arising from the use of this software.
    1.10 +
    1.11 +   Permission is granted to anyone to use this software for any purpose,
    1.12 +   including commercial applications, and to alter it and redistribute it
    1.13 +   freely.
    1.14 +
    1.15 +   This file is created by : Nitin Jain (nitin.j4@samsung.com)
    1.16 +*/
    1.17 +
    1.18 +/* Sample program:  Draw a Chess Board  by using SDL_CreateSoftwareRenderer API */
    1.19 +
    1.20 +#include <stdlib.h>
    1.21 +#include <stdio.h>
    1.22 +
    1.23 +#include "SDL.h"
    1.24 +
    1.25 +void
    1.26 +DrawChessBoard(SDL_Renderer * renderer)
    1.27 +{
    1.28 +	int row = 0,coloum = 0,x = 0;
    1.29 +	SDL_Rect rect, darea;
    1.30 +
    1.31 +	/* Get the Size of drawing surface */
    1.32 +	SDL_RenderGetViewport(renderer, &darea);
    1.33 +
    1.34 +	for(row; row < 8; row++)
    1.35 +	{
    1.36 +		coloum = row%2;
    1.37 +		x = x + coloum;
    1.38 +		for(coloum; coloum < 4+(row%2); coloum++)
    1.39 +		{
    1.40 +			SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
    1.41 +
    1.42 +			rect.w = darea.w/8;
    1.43 +			rect.h = darea.h/8;
    1.44 +			rect.x = x * rect.w;
    1.45 +			rect.y = row * rect.h;
    1.46 +			x = x + 2;
    1.47 +			SDL_RenderFillRect(renderer, &rect);
    1.48 +		}
    1.49 +		x=0;
    1.50 +	}
    1.51 +}
    1.52 +
    1.53 +int
    1.54 +main(int argc, char *argv[])
    1.55 +{
    1.56 +	SDL_Event event;
    1.57 +
    1.58 +	/* Initialize SDL */
    1.59 +	if(SDL_Init(SDL_INIT_VIDEO) != 0)
    1.60 +	{
    1.61 +		fprintf(stderr,"SDL_Init fail : %s\n", SDL_GetError());
    1.62 +		return 1;
    1.63 +	}
    1.64 +
    1.65 +
    1.66 +	/* Create window and renderer for given surface */
    1.67 +	SDL_Window *window = SDL_CreateWindow("Chess Board",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,640,480,SDL_WINDOW_SHOWN);
    1.68 +	if(!window)
    1.69 +	{
    1.70 +		fprintf(stderr,"Window creation fail : %s\n",SDL_GetError());
    1.71 +		return 1;
    1.72 +	}	
    1.73 +	SDL_Surface *surface = SDL_GetWindowSurface(window);
    1.74 +	SDL_Renderer *renderer = SDL_CreateSoftwareRenderer(surface);
    1.75 +	if(!renderer)
    1.76 +	{
    1.77 +		fprintf(stderr,"Render creation for surface fail : %s\n",SDL_GetError());
    1.78 +		return 1;
    1.79 +	}
    1.80 +
    1.81 +	/* Clear the rendering surface with the specidfied colour */
    1.82 +	SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0);
    1.83 +	SDL_RenderClear(renderer);
    1.84 +
    1.85 +
    1.86 +	/* Draw the Image on rendering surface */
    1.87 +	while(1)
    1.88 +	{
    1.89 +		SDL_Event e;
    1.90 +		if (SDL_PollEvent(&e)) {
    1.91 +			if (e.type == SDL_QUIT) 
    1.92 +				break;
    1.93 +
    1.94 +			if(e.key.keysym.sym == SDLK_ESCAPE)
    1.95 +				break;
    1.96 +		}
    1.97 +		
    1.98 +		DrawChessBoard(renderer);
    1.99 +		
   1.100 +		/* Got everything on redering surface,
   1.101 + 		   now Update the drawing image on window screen */
   1.102 +		SDL_UpdateWindowSurface(window);
   1.103 +
   1.104 +	}
   1.105 +
   1.106 +	return 0;
   1.107 +}
   1.108 +