
#ifndef INC_DISPLAYENGINE
#include "DisplayEngine.h"
#endif

#include <allegro.h>

//----------------------------------------------------------------------------

DisplayEngine::DisplayEngine()
        : back_buffer1_(0)
        , back_buffer2_(0)
{}

DisplayEngine::~DisplayEngine()
{
    if( back_buffer1_ )
        destroy_bitmap( back_buffer1_ );
    back_buffer1_ = 0;
    if( back_buffer2_ )
        destroy_bitmap( back_buffer2_ );
    back_buffer2_ = 0;
}

//----------------------------------------------------------------------------

void DisplayEngine::init( const char*ini_fn )
{
    assertCondition( ini_fn );

    push_config_state();
    set_config_file( ini_fn );

    int w = 0;
    int h = 0;
    int bpp = 16;

    int count = 0;
    const char*const *const data = get_config_argv("graphics", "mode", &count);
    if( count == 3 )
    {
        w = atoi(data[0]);
        h = atoi(data[1]);
        bpp = atoi(data[2]);
    }
    else if( count == 2 )
    {
        w = atoi(data[0]);
        h = atoi(data[1]);
    }
    else
    {
        allegro_message( "Found %i parameters in graphics.mode instead of 3 expected: w x h x bpp.\n", count );
        fatalError("can't get screen resolution");
    }

    int gfx_mode = GFX_AUTODETECT_FULLSCREEN;
    if( get_config_int( "graphics", "windowed", 0 ) != 0 )
        gfx_mode = GFX_AUTODETECT_WINDOWED;
    bool dbl_buffer = false;
    if( get_config_int( "graphics", "dbl_buffer", 0 ) != 0 )
        dbl_buffer = true;

    assertCondition( bpp>=16 );
    set_color_depth( bpp );
    if( set_gfx_mode( gfx_mode, w, h, 0, 0 ) < 0 )
    {
        if( gfx_mode == GFX_AUTODETECT_WINDOWED )
            gfx_mode = GFX_AUTODETECT_FULLSCREEN;
        if( set_gfx_mode( gfx_mode, w, h, 0, 0 ) < 0 )
            fatalError("Unable to set a graphics mode");
    }
    set_display_switch_mode( SWITCH_BACKAMNESIA );

    // first try to get two video_bitmaps and use page flipping
    if( dbl_buffer && gfx_mode == GFX_AUTODETECT_FULLSCREEN && (gfx_capabilities & GFX_HW_SYS_TO_VRAM_BLIT) )
    {
        back_buffer1_ = create_video_bitmap( SCREEN_W, SCREEN_H );
        if( back_buffer1_ )
        {
            back_buffer2_ = create_video_bitmap( SCREEN_W, SCREEN_H );
        }
        if( !back_buffer2_ )
        {
            destroy_bitmap( back_buffer1_ );
            back_buffer1_ = 0;
        }
    }
    // if that didn't work use normal blit
    if( !back_buffer2_ )
    {
        /* Create the double_buffer */
        if( !back_buffer1_ )
        {
            back_buffer1_ = create_system_bitmap( SCREEN_W, SCREEN_H );
        }
    }

    if( !back_buffer1_ )
    {
        fatalError("Unable to create back_buffer1_");
    }

    pop_config_state();
    enable_hardware_cursor();
    show_mouse(screen);
}

void DisplayEngine::display_draw_buffer()
{
    scare_mouse();
    if( back_buffer2_ ) // use page flipping
    {
        show_video_bitmap( back_buffer1_ );
        //show_mouse(screen);
        BITMAP*const tmp_bmp = back_buffer1_;
        back_buffer1_ = back_buffer2_;
        back_buffer2_ = tmp_bmp;
    }
    else // normal blit
    {
        blit( back_buffer1_, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H );
    }
    unscare_mouse();
}

//----------------------------------------------------------------------------

ViewPortBase::ViewPortBase( const ScreenRect&asr )
        : area(asr)
{}

ViewPortBase::~ViewPortBase()
{}

//----------------------------------------------------------------------------

