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