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