test/testdrawchessboard.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sat, 27 Jul 2013 21:02:50 +0200
changeset 7538 1d187795e103
parent 7529 3f571ec09593
child 7540 b1c08daea9cd
permissions -rw-r--r--
Corrected words in comments of test programs.
slouken@7529
     1
/*
slouken@7529
     2
   Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@7529
     3
slouken@7529
     4
   This software is provided 'as-is', without any express or implied
slouken@7529
     5
   warranty.  In no event will the authors be held liable for any damages
slouken@7529
     6
   arising from the use of this software.
slouken@7529
     7
slouken@7529
     8
   Permission is granted to anyone to use this software for any purpose,
slouken@7529
     9
   including commercial applications, and to alter it and redistribute it
slouken@7529
    10
   freely.
slouken@7529
    11
slouken@7529
    12
   This file is created by : Nitin Jain (nitin.j4@samsung.com)
slouken@7529
    13
*/
slouken@7529
    14
slouken@7529
    15
/* Sample program:  Draw a Chess Board  by using SDL_CreateSoftwareRenderer API */
slouken@7529
    16
slouken@7529
    17
#include <stdlib.h>
slouken@7529
    18
#include <stdio.h>
slouken@7529
    19
slouken@7529
    20
#include "SDL.h"
slouken@7529
    21
slouken@7529
    22
void
slouken@7529
    23
DrawChessBoard(SDL_Renderer * renderer)
slouken@7529
    24
{
slouken@7529
    25
	int row = 0,coloum = 0,x = 0;
slouken@7529
    26
	SDL_Rect rect, darea;
slouken@7529
    27
slouken@7529
    28
	/* Get the Size of drawing surface */
slouken@7529
    29
	SDL_RenderGetViewport(renderer, &darea);
slouken@7529
    30
slouken@7529
    31
	for(row; row < 8; row++)
slouken@7529
    32
	{
slouken@7529
    33
		coloum = row%2;
slouken@7529
    34
		x = x + coloum;
slouken@7529
    35
		for(coloum; coloum < 4+(row%2); coloum++)
slouken@7529
    36
		{
slouken@7529
    37
			SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
slouken@7529
    38
slouken@7529
    39
			rect.w = darea.w/8;
slouken@7529
    40
			rect.h = darea.h/8;
slouken@7529
    41
			rect.x = x * rect.w;
slouken@7529
    42
			rect.y = row * rect.h;
slouken@7529
    43
			x = x + 2;
slouken@7529
    44
			SDL_RenderFillRect(renderer, &rect);
slouken@7529
    45
		}
slouken@7529
    46
		x=0;
slouken@7529
    47
	}
slouken@7529
    48
}
slouken@7529
    49
slouken@7529
    50
int
slouken@7529
    51
main(int argc, char *argv[])
slouken@7529
    52
{
slouken@7529
    53
	SDL_Event event;
slouken@7529
    54
slouken@7529
    55
	/* Initialize SDL */
slouken@7529
    56
	if(SDL_Init(SDL_INIT_VIDEO) != 0)
slouken@7529
    57
	{
slouken@7529
    58
		fprintf(stderr,"SDL_Init fail : %s\n", SDL_GetError());
slouken@7529
    59
		return 1;
slouken@7529
    60
	}
slouken@7529
    61
slouken@7529
    62
slouken@7529
    63
	/* Create window and renderer for given surface */
slouken@7529
    64
	SDL_Window *window = SDL_CreateWindow("Chess Board",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,640,480,SDL_WINDOW_SHOWN);
slouken@7529
    65
	if(!window)
slouken@7529
    66
	{
slouken@7529
    67
		fprintf(stderr,"Window creation fail : %s\n",SDL_GetError());
slouken@7529
    68
		return 1;
slouken@7529
    69
	}	
slouken@7529
    70
	SDL_Surface *surface = SDL_GetWindowSurface(window);
slouken@7529
    71
	SDL_Renderer *renderer = SDL_CreateSoftwareRenderer(surface);
slouken@7529
    72
	if(!renderer)
slouken@7529
    73
	{
slouken@7529
    74
		fprintf(stderr,"Render creation for surface fail : %s\n",SDL_GetError());
slouken@7529
    75
		return 1;
slouken@7529
    76
	}
slouken@7529
    77
philipp@7538
    78
	/* Clear the rendering surface with the specified color */
slouken@7529
    79
	SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0);
slouken@7529
    80
	SDL_RenderClear(renderer);
slouken@7529
    81
slouken@7529
    82
slouken@7529
    83
	/* Draw the Image on rendering surface */
slouken@7529
    84
	while(1)
slouken@7529
    85
	{
slouken@7529
    86
		SDL_Event e;
slouken@7529
    87
		if (SDL_PollEvent(&e)) {
slouken@7529
    88
			if (e.type == SDL_QUIT) 
slouken@7529
    89
				break;
slouken@7529
    90
slouken@7529
    91
			if(e.key.keysym.sym == SDLK_ESCAPE)
slouken@7529
    92
				break;
slouken@7529
    93
		}
slouken@7529
    94
		
slouken@7529
    95
		DrawChessBoard(renderer);
slouken@7529
    96
		
philipp@7538
    97
		/* Got everything on rendering surface,
slouken@7529
    98
 		   now Update the drawing image on window screen */
slouken@7529
    99
		SDL_UpdateWindowSurface(window);
slouken@7529
   100
slouken@7529
   101
	}
slouken@7529
   102
slouken@7529
   103
	return 0;
slouken@7529
   104
}
slouken@7529
   105