Added example of using the software renderer and window surface API, contributed by Nitin Jain.
authorSam Lantinga <slouken@libsdl.org>
Sat, 27 Jul 2013 03:48:23 -0700
changeset 75293f571ec09593
parent 7528 1890a896802f
child 7530 7f26fd1df927
Added example of using the software renderer and window surface API, contributed by Nitin Jain.
test/Makefile.in
test/testdrawchessboard.c
     1.1 --- a/test/Makefile.in	Sat Jul 27 03:44:03 2013 -0700
     1.2 +++ b/test/Makefile.in	Sat Jul 27 03:48:23 2013 -0700
     1.3 @@ -13,6 +13,7 @@
     1.4  	testaudioinfo$(EXE) \
     1.5  	testautomation$(EXE) \
     1.6  	testdraw2$(EXE) \
     1.7 +	testdrawchessboard$(EXE) \
     1.8  	testerror$(EXE) \
     1.9  	testfile$(EXE) \
    1.10  	testgamecontroller$(EXE) \
    1.11 @@ -103,6 +104,9 @@
    1.12  testdraw2$(EXE): $(srcdir)/testdraw2.c
    1.13  	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
    1.14  
    1.15 +testdrawchessboard$(EXE): $(srcdir)/testdrawchessboard.c
    1.16 +	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
    1.17 +
    1.18  testerror$(EXE): $(srcdir)/testerror.c
    1.19  	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
    1.20  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/testdrawchessboard.c	Sat Jul 27 03:48:23 2013 -0700
     2.3 @@ -0,0 +1,105 @@
     2.4 +/*
     2.5 +   Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     2.6 +
     2.7 +   This software is provided 'as-is', without any express or implied
     2.8 +   warranty.  In no event will the authors be held liable for any damages
     2.9 +   arising from the use of this software.
    2.10 +
    2.11 +   Permission is granted to anyone to use this software for any purpose,
    2.12 +   including commercial applications, and to alter it and redistribute it
    2.13 +   freely.
    2.14 +
    2.15 +   This file is created by : Nitin Jain (nitin.j4@samsung.com)
    2.16 +*/
    2.17 +
    2.18 +/* Sample program:  Draw a Chess Board  by using SDL_CreateSoftwareRenderer API */
    2.19 +
    2.20 +#include <stdlib.h>
    2.21 +#include <stdio.h>
    2.22 +
    2.23 +#include "SDL.h"
    2.24 +
    2.25 +void
    2.26 +DrawChessBoard(SDL_Renderer * renderer)
    2.27 +{
    2.28 +	int row = 0,coloum = 0,x = 0;
    2.29 +	SDL_Rect rect, darea;
    2.30 +
    2.31 +	/* Get the Size of drawing surface */
    2.32 +	SDL_RenderGetViewport(renderer, &darea);
    2.33 +
    2.34 +	for(row; row < 8; row++)
    2.35 +	{
    2.36 +		coloum = row%2;
    2.37 +		x = x + coloum;
    2.38 +		for(coloum; coloum < 4+(row%2); coloum++)
    2.39 +		{
    2.40 +			SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
    2.41 +
    2.42 +			rect.w = darea.w/8;
    2.43 +			rect.h = darea.h/8;
    2.44 +			rect.x = x * rect.w;
    2.45 +			rect.y = row * rect.h;
    2.46 +			x = x + 2;
    2.47 +			SDL_RenderFillRect(renderer, &rect);
    2.48 +		}
    2.49 +		x=0;
    2.50 +	}
    2.51 +}
    2.52 +
    2.53 +int
    2.54 +main(int argc, char *argv[])
    2.55 +{
    2.56 +	SDL_Event event;
    2.57 +
    2.58 +	/* Initialize SDL */
    2.59 +	if(SDL_Init(SDL_INIT_VIDEO) != 0)
    2.60 +	{
    2.61 +		fprintf(stderr,"SDL_Init fail : %s\n", SDL_GetError());
    2.62 +		return 1;
    2.63 +	}
    2.64 +
    2.65 +
    2.66 +	/* Create window and renderer for given surface */
    2.67 +	SDL_Window *window = SDL_CreateWindow("Chess Board",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,640,480,SDL_WINDOW_SHOWN);
    2.68 +	if(!window)
    2.69 +	{
    2.70 +		fprintf(stderr,"Window creation fail : %s\n",SDL_GetError());
    2.71 +		return 1;
    2.72 +	}	
    2.73 +	SDL_Surface *surface = SDL_GetWindowSurface(window);
    2.74 +	SDL_Renderer *renderer = SDL_CreateSoftwareRenderer(surface);
    2.75 +	if(!renderer)
    2.76 +	{
    2.77 +		fprintf(stderr,"Render creation for surface fail : %s\n",SDL_GetError());
    2.78 +		return 1;
    2.79 +	}
    2.80 +
    2.81 +	/* Clear the rendering surface with the specidfied colour */
    2.82 +	SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0);
    2.83 +	SDL_RenderClear(renderer);
    2.84 +
    2.85 +
    2.86 +	/* Draw the Image on rendering surface */
    2.87 +	while(1)
    2.88 +	{
    2.89 +		SDL_Event e;
    2.90 +		if (SDL_PollEvent(&e)) {
    2.91 +			if (e.type == SDL_QUIT) 
    2.92 +				break;
    2.93 +
    2.94 +			if(e.key.keysym.sym == SDLK_ESCAPE)
    2.95 +				break;
    2.96 +		}
    2.97 +		
    2.98 +		DrawChessBoard(renderer);
    2.99 +		
   2.100 +		/* Got everything on redering surface,
   2.101 + 		   now Update the drawing image on window screen */
   2.102 +		SDL_UpdateWindowSurface(window);
   2.103 +
   2.104 +	}
   2.105 +
   2.106 +	return 0;
   2.107 +}
   2.108 +