test/testdrawchessboard.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Thu, 21 May 2015 21:25:14 +0200
changeset 9609 897b25f560d0
parent 9607 7746ab058d12
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Fixed undefined key access in test program.
     1 /*
     2    Copyright (C) 1997-2014 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 #ifdef __EMSCRIPTEN__
    21 #include <emscripten/emscripten.h>
    22 #endif
    23 
    24 #include "SDL.h"
    25 
    26 SDL_Window *window;
    27 SDL_Renderer *renderer;
    28 int done;
    29 
    30 void
    31 DrawChessBoard(SDL_Renderer * renderer)
    32 {
    33 	int row = 0,column = 0,x = 0;
    34 	SDL_Rect rect, darea;
    35 
    36 	/* Get the Size of drawing surface */
    37 	SDL_RenderGetViewport(renderer, &darea);
    38 
    39 	for( ; row < 8; row++)
    40 	{
    41 		column = row%2;
    42 		x = x + column;
    43 		for( ; column < 4+(row%2); column++)
    44 		{
    45 			SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF);
    46 
    47 			rect.w = darea.w/8;
    48 			rect.h = darea.h/8;
    49 			rect.x = x * rect.w;
    50 			rect.y = row * rect.h;
    51 			x = x + 2;
    52 			SDL_RenderFillRect(renderer, &rect);
    53 		}
    54 		x=0;
    55 	}
    56 }
    57 
    58 void
    59 loop()
    60 {
    61     SDL_Event e;
    62     while (SDL_PollEvent(&e)) {
    63 		if (e.type == SDL_QUIT) {
    64 			done = 1;
    65 #ifdef __EMSCRIPTEN__
    66 			emscripten_cancel_main_loop();
    67 #endif
    68 			return;
    69 		}
    70 
    71 		if ((e.type == SDL_KEYDOWN) && (e.key.keysym.sym == SDLK_ESCAPE)) {
    72 			done = 1;
    73 #ifdef __EMSCRIPTEN__
    74 			emscripten_cancel_main_loop();
    75 #endif
    76 			return;
    77 		}
    78 	}
    79 	
    80 	DrawChessBoard(renderer);
    81 	
    82 	/* Got everything on rendering surface,
    83 	   now Update the drawing image on window screen */
    84 	SDL_UpdateWindowSurface(window);
    85 }
    86 
    87 int
    88 main(int argc, char *argv[])
    89 {
    90 	SDL_Surface *surface;
    91 
    92     /* Enable standard application logging */
    93     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
    94 
    95 	/* Initialize SDL */
    96 	if(SDL_Init(SDL_INIT_VIDEO) != 0)
    97 	{
    98 		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init fail : %s\n", SDL_GetError());
    99 		return 1;
   100 	}
   101 
   102 
   103 	/* Create window and renderer for given surface */
   104 	window = SDL_CreateWindow("Chess Board", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
   105 	if(!window)
   106 	{
   107 		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Window creation fail : %s\n",SDL_GetError());
   108 		return 1;
   109 	}	
   110 	surface = SDL_GetWindowSurface(window);
   111 	renderer = SDL_CreateSoftwareRenderer(surface);
   112 	if(!renderer)
   113 	{
   114 		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Render creation for surface fail : %s\n",SDL_GetError());
   115 		return 1;
   116 	}
   117 
   118 	/* Clear the rendering surface with the specified color */
   119 	SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
   120 	SDL_RenderClear(renderer);
   121 
   122 
   123 	/* Draw the Image on rendering surface */
   124 	done = 0;
   125 #ifdef __EMSCRIPTEN__
   126     emscripten_set_main_loop(loop, 0, 1);
   127 #else
   128     while (!done) {
   129         loop();
   130 	}
   131 #endif
   132 
   133 	return 0;
   134 }
   135