#include "graphics.h"
#include "SDL.h"


static SDL_Surface* screen;


/*****************************************************************************
* Create a screen of type "type".  Return 0 if the screen was created,
* 1 if the screen could not be created.
*/
int create_screen(int type)
{
	const static int widths[] = { 320, 640, 800, 1024 };
	const static int heights[] = { 240, 480, 600, 768 };

	int bpp = type & 0xff;
	int width = widths[type >> 8];
	int height = heights[type >> 8];
	int flags = SDL_SWSURFACE | SDL_DOUBLEBUF;
	int err = 0;

	err = SDL_Init(SDL_INIT_VIDEO);
	if(!err)
	{
		screen = SDL_SetVideoMode(width, height, bpp, flags);
		if(!screen) SDL_Quit();
	}

	if(!err && screen) return 0;
	else return 1;
}


/*****************************************************************************
* Destroy the previously created screen.
*/
int destroy_screen()
{
	if(screen)
	{
		SDL_FreeSurface(screen);
		screen = NULL;
	}

	SDL_Quit();

	return 0;
}


/*****************************************************************************
* Draw a filled box in specified region and color.
*/
int draw_box(int x, int y, int w, int h, int color)
{
	SDL_Rect rect = {x, y, w, h};
	int r = (color >> 16) & 0xff;
	int g = (color >> 8) & 0xff;
	int b = (color >> 0) & 0xff;

	Uint32 col;
	int err = 0;

	if(!screen) return 1;
	col = SDL_MapRGB(screen->format, r, g, b);
	err = SDL_FillRect(screen, &rect, col);
	err |= SDL_Flip(screen);

	if(err) return 1;
	else return 0;
}


/*****************************************************************************
* Freezes the graphics library for s seconds.
*/
int g_sleep(int s)
{
	SDL_Delay(s * 1000);

	return 0;
}


/* vim:ts=3
*/
