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