test/testdrawchessboard.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Mon, 18 May 2015 21:12:16 +0200
changeset 9605 be71aef6c347
parent 9278 8900afb78a19
child 9607 7746ab058d12
permissions -rw-r--r--
Fixed handling only one event per frame 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 			return;
    66 		}
    67 
    68 		if(e.key.keysym.sym == SDLK_ESCAPE) {
    69 			done = 1;
    70 			return;
    71 		}
    72 	}
    73 	
    74 	DrawChessBoard(renderer);
    75 	
    76 	/* Got everything on rendering surface,
    77 	   now Update the drawing image on window screen */
    78 	SDL_UpdateWindowSurface(window);
    79 }
    80 
    81 int
    82 main(int argc, char *argv[])
    83 {
    84 	SDL_Surface *surface;
    85 
    86     /* Enable standard application logging */
    87     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
    88 
    89 	/* Initialize SDL */
    90 	if(SDL_Init(SDL_INIT_VIDEO) != 0)
    91 	{
    92 		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init fail : %s\n", SDL_GetError());
    93 		return 1;
    94 	}
    95 
    96 
    97 	/* Create window and renderer for given surface */
    98 	window = SDL_CreateWindow("Chess Board", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
    99 	if(!window)
   100 	{
   101 		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Window creation fail : %s\n",SDL_GetError());
   102 		return 1;
   103 	}	
   104 	surface = SDL_GetWindowSurface(window);
   105 	renderer = SDL_CreateSoftwareRenderer(surface);
   106 	if(!renderer)
   107 	{
   108 		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Render creation for surface fail : %s\n",SDL_GetError());
   109 		return 1;
   110 	}
   111 
   112 	/* Clear the rendering surface with the specified color */
   113 	SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
   114 	SDL_RenderClear(renderer);
   115 
   116 
   117 	/* Draw the Image on rendering surface */
   118 	done = 0;
   119 #ifdef __EMSCRIPTEN__
   120     emscripten_set_main_loop(loop, 0, 1);
   121 #else
   122     while (!done) {
   123         loop();
   124 	}
   125 #endif
   126 
   127 	return 0;
   128 }
   129