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