/*
	This is my very first attemt at using the pvr, so it most certainly
	is rather aweful. Yeah, you did not misread, all the stuff that could
	be seen of Saviour on our website and facebook was realized with help of
	a software renderer and therefore slightly slow(however I still managed
	to get about 30 fps most of the time(and 20 at worst), so for what it
	was it was quite alright). Now, after almost a year of development i
	am finally forced to accept that this simply wont be fast enough once some
	magic effects are added and my fear of using the pvr was actually rather
	unjustified, so i am currently rewriting my rendering code. This also
	implies I can only use the pvrs ram for textures and due to the wonderful
	graphics done by Jana, this is not very much to work with and i consider
	vq compression. However the vqenc program shipped with KallistiOS does
	not seem to be able to create vq encoded ARGB1555 textures. Is this even
	possible. If you happen to know i would be extraordinarily grateful if you
	could tell me. My email is fillius@2d-rp.de. Thank you.
	Well, I digress again, sorry. This just wrappes the pvr functionality
	in some form i feel more compfortable to work with. I actually do all the
	drawing to the translucent list due to excessive laziness, so i never
	tested the list switching code, sorry.
*/

#define VERTBUF_SIZE 1024*512
#define PVR_LIST_NONE -1

namespace kos_gfx
{
	namespace pvr
	{
		typedef uint32 color;
		
		int z_val=1;
		int active_list_type=PVR_LIST_NONE;
		int SCREEN_WIDTH=640, SCREEN_HEIGHT=480;
		bool is_50_hz=false;
		int TITLE_SAFE_LEFT=640/10, TITLE_SAFE_RIGHT=640-640/10, TITLE_SAFE_TOP=480/10, TITLE_SAFE_BOTTOM=480-480/10;
		
		pvr_init_params_t pvr_params =
		{//Opague, modifier_op,trans, modifier_trans, pt ;-)
          { PVR_BINSIZE_16, PVR_BINSIZE_0, PVR_BINSIZE_16, PVR_BINSIZE_0, PVR_BINSIZE_0, },
          VERTBUF_SIZE, 0, 0,
		};
		
		void init(Uint16 x, Uint16 y, bool hz60)
		{
			is_50_hz=!hz60;
			//detailed handling to be added later on ;-);
			//the size arguments are wonderfully ignored, shame on me ;-);
			vid_set_mode(vid_check_cable()==CT_VGA?DM_640x480:
				hz60?DM_640x480_NTSC_IL:DM_640x480_PAL_IL,PM_RGB565);
			pvr_init(&pvr_params);
			pvr_set_bg_color(0,0,0);
			
		};
		
		void shutdown()
		{
			pvr_shutdown();
		};
		
		void change_active_list(int new_list_type)
		{
			if(active_list_type!=PVR_LIST_NONE)
				pvr_list_finish();
			active_list_type=new_list_type;
			if(new_list_type!=PVR_LIST_NONE)
				pvr_list_begin(new_list_type);
		};
		
		void close_active_list()
		{
			if(active_list_type!=PVR_LIST_NONE)
			{
				pvr_list_finish();
				active_list_type=PVR_LIST_NONE;
			};
		};
		
		void begin_drawing()
		{
			kos_gfx::pvr::z_val=1;
			pvr_wait_ready();
			pvr_scene_begin();
			kos_gfx::pvr::change_active_list(PVR_LIST_TR_POLY);
		};
		
		void end_drawing()
		{
			kos_gfx::pvr::close_active_list();
			pvr_scene_finish();
		};
	};
};

#include "pvr_primitives.h"
#include "pvr_image.h"
