test/testdrawchessboard.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sat, 27 Jul 2013 21:11:12 +0200
changeset 7541 913786dec9d5
parent 7540 b1c08daea9cd
child 7639 9406b7dd2f2d
permissions -rw-r--r--
Changed test program to be more compatible C.
     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_Window *window;
    54 	SDL_Surface *surface;
    55 	SDL_Renderer *renderer;
    56 
    57 	/* Initialize SDL */
    58 	if(SDL_Init(SDL_INIT_VIDEO) != 0)
    59 	{
    60 		fprintf(stderr,"SDL_Init fail : %s\n", SDL_GetError());
    61 		return 1;
    62 	}
    63 
    64 
    65 	/* Create window and renderer for given surface */
    66 	window = SDL_CreateWindow("Chess Board", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
    67 	if(!window)
    68 	{
    69 		fprintf(stderr,"Window creation fail : %s\n",SDL_GetError());
    70 		return 1;
    71 	}	
    72 	surface = SDL_GetWindowSurface(window);
    73 	renderer = SDL_CreateSoftwareRenderer(surface);
    74 	if(!renderer)
    75 	{
    76 		fprintf(stderr,"Render creation for surface fail : %s\n",SDL_GetError());
    77 		return 1;
    78 	}
    79 
    80 	/* Clear the rendering surface with the specified color */
    81 	SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0);
    82 	SDL_RenderClear(renderer);
    83 
    84 
    85 	/* Draw the Image on rendering surface */
    86 	while(1)
    87 	{
    88 		SDL_Event e;
    89 		if (SDL_PollEvent(&e)) {
    90 			if (e.type == SDL_QUIT) 
    91 				break;
    92 
    93 			if(e.key.keysym.sym == SDLK_ESCAPE)
    94 				break;
    95 		}
    96 		
    97 		DrawChessBoard(renderer);
    98 		
    99 		/* Got everything on rendering surface,
   100  		   now Update the drawing image on window screen */
   101 		SDL_UpdateWindowSurface(window);
   102 
   103 	}
   104 
   105 	return 0;
   106 }
   107