test/testdrawchessboard.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sun, 09 Aug 2015 20:01:01 +0200
changeset 9836 3d43bb56c5d6
parent 9703 215d1dcd8e90
child 9922 b066d5fd5fa7
permissions -rw-r--r--
Added missing SDL_Quit() in test program.
     1 /*
     2    Copyright (C) 1997-2015 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 = 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 	}
    55 }
    56 
    57 void
    58 loop()
    59 {
    60     SDL_Event e;
    61     while (SDL_PollEvent(&e)) {
    62 		if (e.type == SDL_QUIT) {
    63 			done = 1;
    64 #ifdef __EMSCRIPTEN__
    65 			emscripten_cancel_main_loop();
    66 #endif
    67 			return;
    68 		}
    69 
    70 		if ((e.type == SDL_KEYDOWN) && (e.key.keysym.sym == SDLK_ESCAPE)) {
    71 			done = 1;
    72 #ifdef __EMSCRIPTEN__
    73 			emscripten_cancel_main_loop();
    74 #endif
    75 			return;
    76 		}
    77 	}
    78 	
    79 	DrawChessBoard(renderer);
    80 	
    81 	/* Got everything on rendering surface,
    82 	   now Update the drawing image on window screen */
    83 	SDL_UpdateWindowSurface(window);
    84 }
    85 
    86 int
    87 main(int argc, char *argv[])
    88 {
    89 	SDL_Surface *surface;
    90 
    91     /* Enable standard application logging */
    92     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
    93 
    94 	/* Initialize SDL */
    95 	if(SDL_Init(SDL_INIT_VIDEO) != 0)
    96 	{
    97 		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init fail : %s\n", SDL_GetError());
    98 		return 1;
    99 	}
   100 
   101 
   102 	/* Create window and renderer for given surface */
   103 	window = SDL_CreateWindow("Chess Board", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
   104 	if(!window)
   105 	{
   106 		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Window creation fail : %s\n",SDL_GetError());
   107 		return 1;
   108 	}	
   109 	surface = SDL_GetWindowSurface(window);
   110 	renderer = SDL_CreateSoftwareRenderer(surface);
   111 	if(!renderer)
   112 	{
   113 		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Render creation for surface fail : %s\n",SDL_GetError());
   114 		return 1;
   115 	}
   116 
   117 	/* Clear the rendering surface with the specified color */
   118 	SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
   119 	SDL_RenderClear(renderer);
   120 
   121 
   122 	/* Draw the Image on rendering surface */
   123 	done = 0;
   124 #ifdef __EMSCRIPTEN__
   125     emscripten_set_main_loop(loop, 0, 1);
   126 #else
   127     while (!done) {
   128         loop();
   129 	}
   130 #endif
   131 
   132     SDL_Quit();
   133 	return 0;
   134 }
   135