#include <cstdio>
#include "CDisplay.hpp"

CDisplay Display;

PALETTE palette;
int CDisplay::CreateWindow(word w, word h, byte bpp, bool fullscreen)
{
        allegro_init();
        install_keyboard();
        install_timer();
        install_mouse();
        install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL);
		
		set_color_depth(bpp);

		
        if (fullscreen == false)
        {
                set_gfx_mode(GFX_DIRECTX_WIN, w, h, 0, 0);
              // TODO: Apparently this is for the WIP only... fix this
              //  set_close_button_callback(exit_callback);
        }

        else
        {
                set_gfx_mode(GFX_AUTODETECT, w, h, 0, 0);
        }

//        SetTimers();
        set_window_title(APP_TITLE);
		
		
		cur_page = 0;
        vscreen[0] = create_bitmap(w, h);
        vscreen[1] = create_bitmap(w, h);
        vscreen[2] = vscreen[cur_page];
		

		if(bpp == 8)
		{ 
			
			//BITMAP *pal = load_bitmap("./data/pal.bmp", palette);
			//set_palette(palette);
		}
		
        return 0;
}

void CDisplay::Shutdown(void)
{
  //TODO: Remember to destroy all bitmaps that were created!
        destroy_bitmap(vscreen[0]);

        allegro_exit();
        exit(0);
}

BITMAP *CDisplay::GetSurface()
{
        return vscreen[2];
}

// *****USE THIS ALL THE TIME ******
BITMAP *safe_load_bitmap(const char *filename, RGB *pal)
{
	
	if(filename)
	{
		return load_bitmap(filename, pal);
	}
	else 
	{
		allegro_message("Error: %s could not be found.", filename);
		//return (BITMAP *);
	}
	
}

