test/testdrawchessboard.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Mon, 18 May 2015 21:21:14 +0200
changeset 9607 7746ab058d12
parent 9605 be71aef6c347
child 9609 897b25f560d0
permissions -rw-r--r--
Added missing loop cancel for Emscripten in test programs.
slouken@7905
     1
/*
slouken@8149
     2
   Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
slouken@7905
     3
slouken@7905
     4
   This software is provided 'as-is', without any express or implied
slouken@7905
     5
   warranty.  In no event will the authors be held liable for any damages
slouken@7905
     6
   arising from the use of this software.
slouken@7905
     7
slouken@7905
     8
   Permission is granted to anyone to use this software for any purpose,
slouken@7905
     9
   including commercial applications, and to alter it and redistribute it
slouken@7905
    10
   freely.
slouken@7905
    11
slouken@7905
    12
   This file is created by : Nitin Jain (nitin.j4@samsung.com)
slouken@7905
    13
*/
slouken@7905
    14
slouken@7905
    15
/* Sample program:  Draw a Chess Board  by using SDL_CreateSoftwareRenderer API */
slouken@7905
    16
icculus@9278
    17
#include <stdlib.h>
icculus@9278
    18
#include <stdio.h>
icculus@9278
    19
icculus@9278
    20
#ifdef __EMSCRIPTEN__
icculus@9278
    21
#include <emscripten/emscripten.h>
icculus@9278
    22
#endif
icculus@9278
    23
slouken@7905
    24
#include "SDL.h"
slouken@7905
    25
icculus@9278
    26
SDL_Window *window;
icculus@9278
    27
SDL_Renderer *renderer;
icculus@9278
    28
int done;
icculus@9278
    29
slouken@7905
    30
void
slouken@7905
    31
DrawChessBoard(SDL_Renderer * renderer)
slouken@7905
    32
{
philipp@9105
    33
	int row = 0,column = 0,x = 0;
slouken@7905
    34
	SDL_Rect rect, darea;
slouken@7905
    35
slouken@7905
    36
	/* Get the Size of drawing surface */
slouken@7905
    37
	SDL_RenderGetViewport(renderer, &darea);
slouken@7905
    38
philipp@9103
    39
	for( ; row < 8; row++)
slouken@7905
    40
	{
philipp@9105
    41
		column = row%2;
philipp@9105
    42
		x = x + column;
philipp@9105
    43
		for( ; column < 4+(row%2); column++)
slouken@7905
    44
		{
slouken@8058
    45
			SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF);
slouken@7905
    46
slouken@7905
    47
			rect.w = darea.w/8;
slouken@7905
    48
			rect.h = darea.h/8;
slouken@7905
    49
			rect.x = x * rect.w;
slouken@7905
    50
			rect.y = row * rect.h;
slouken@7905
    51
			x = x + 2;
slouken@7905
    52
			SDL_RenderFillRect(renderer, &rect);
slouken@7905
    53
		}
slouken@7905
    54
		x=0;
slouken@7905
    55
	}
slouken@7905
    56
}
slouken@7905
    57
icculus@9278
    58
void
icculus@9278
    59
loop()
icculus@9278
    60
{
icculus@9278
    61
    SDL_Event e;
philipp@9605
    62
    while (SDL_PollEvent(&e)) {
icculus@9278
    63
		if (e.type == SDL_QUIT) {
icculus@9278
    64
			done = 1;
philipp@9607
    65
#ifdef __EMSCRIPTEN__
philipp@9607
    66
			emscripten_cancel_main_loop();
philipp@9607
    67
#endif
icculus@9278
    68
			return;
icculus@9278
    69
		}
icculus@9278
    70
icculus@9278
    71
		if(e.key.keysym.sym == SDLK_ESCAPE) {
icculus@9278
    72
			done = 1;
philipp@9607
    73
#ifdef __EMSCRIPTEN__
philipp@9607
    74
			emscripten_cancel_main_loop();
philipp@9607
    75
#endif
icculus@9278
    76
			return;
icculus@9278
    77
		}
icculus@9278
    78
	}
icculus@9278
    79
	
icculus@9278
    80
	DrawChessBoard(renderer);
icculus@9278
    81
	
icculus@9278
    82
	/* Got everything on rendering surface,
icculus@9278
    83
	   now Update the drawing image on window screen */
icculus@9278
    84
	SDL_UpdateWindowSurface(window);
icculus@9278
    85
}
icculus@9278
    86
slouken@7905
    87
int
slouken@7905
    88
main(int argc, char *argv[])
slouken@7905
    89
{
slouken@7905
    90
	SDL_Surface *surface;
slouken@7905
    91
aschiffler@7639
    92
    /* Enable standard application logging */
slouken@7905
    93
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
slouken@7905
    94
slouken@7905
    95
	/* Initialize SDL */
slouken@7905
    96
	if(SDL_Init(SDL_INIT_VIDEO) != 0)
slouken@7905
    97
	{
slouken@7905
    98
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init fail : %s\n", SDL_GetError());
slouken@7905
    99
		return 1;
slouken@7905
   100
	}
slouken@7905
   101
slouken@7905
   102
slouken@7905
   103
	/* Create window and renderer for given surface */
slouken@7905
   104
	window = SDL_CreateWindow("Chess Board", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
slouken@7905
   105
	if(!window)
slouken@7905
   106
	{
slouken@7905
   107
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Window creation fail : %s\n",SDL_GetError());
slouken@7905
   108
		return 1;
slouken@7905
   109
	}	
slouken@7905
   110
	surface = SDL_GetWindowSurface(window);
slouken@7905
   111
	renderer = SDL_CreateSoftwareRenderer(surface);
slouken@7905
   112
	if(!renderer)
slouken@7905
   113
	{
slouken@7905
   114
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Render creation for surface fail : %s\n",SDL_GetError());
slouken@7905
   115
		return 1;
slouken@7905
   116
	}
slouken@7905
   117
slouken@7905
   118
	/* Clear the rendering surface with the specified color */
slouken@8058
   119
	SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
slouken@7905
   120
	SDL_RenderClear(renderer);
slouken@7905
   121
slouken@7905
   122
slouken@7905
   123
	/* Draw the Image on rendering surface */
icculus@9278
   124
	done = 0;
icculus@9278
   125
#ifdef __EMSCRIPTEN__
icculus@9278
   126
    emscripten_set_main_loop(loop, 0, 1);
icculus@9278
   127
#else
icculus@9278
   128
    while (!done) {
icculus@9278
   129
        loop();
slouken@7905
   130
	}
icculus@9278
   131
#endif
slouken@7905
   132
slouken@7905
   133
	return 0;
slouken@7905
   134
}
slouken@7905
   135