# define DEBUGMODE
# include <allegro.h>

volatile long int coreClock = 0 ;


void increment_coreClock () {++ coreClock ;} END_OF_FUNCTION ()


int main ()
{
    int screenResX = 800 ;
    int screenResY = 600 ;

    const float fullFrameRate = 60 ;       // Maximum frame rate
    float frameRatePercent = 100 ;         // Percentage of the maximum frame rate that the game is run on
    float frameRate = fullFrameRate / 100 * frameRatePercent ;        // Actual frame rate that the game is run on

    // Allegro initialization
    allegro_init () ;
    install_keyboard () ;
    set_window_title ("") ;
    set_color_depth (desktop_color_depth ()) ;
    set_gfx_mode (GFX_AUTODETECT_FULLSCREEN, screenResX, screenResY, 0, 0) ;
    install_timer () ;
    LOCK_VARIABLE (coreClock) ;
    LOCK_FUNCTION (increment_coreClock) ;
    install_int_ex (increment_coreClock, BPS_TO_TIMER (frameRate)) ;



    const int debugColor = makecol (0, 0, 255) ;           // Color that the debugging information is written in

    BITMAP* buffer = create_bitmap (screenResX, screenResY) ;

    BITMAP* starBuffer = create_bitmap (1600, 1200) ;
    BITMAP* starLayer = load_bitmap ("StarLayer2.bmp", NULL) ;
    BITMAP* landscapeLayer = load_bitmap ("Starscape.bmp", NULL) ;

    int rotateCounter [2] = {3, 3} ;
    float rotateInc = 0.1 ;
    float angle = 0 ;



// ***************************** Begin game loop *******************************
    while (! key [KEY_ESC])
    {
        while (! coreClock) {rest (100 / frameRate) ;}
        while (coreClock > 0)
        {
            int oldTicks = coreClock ;

            -- rotateCounter [0] ;
            if (! rotateCounter [0])
            {
                rotateCounter [0] = rotateCounter [1] ;
                angle += rotateInc ;
            }



            -- coreClock ;
            if (oldTicks <= coreClock) {break ;}
        }
        rotate_sprite (starBuffer, starLayer, 0, 0, ftofix (angle)) ;
        blit (starBuffer, buffer, 400, 300, 0, 0, buffer->w, buffer->h) ;
        draw_sprite (buffer, landscapeLayer, 0, 0) ;
        vsync () ;
        blit (buffer, screen, 0, 0, 0, 0, screenResX, screenResY) ;
    }
// ***************************** End game loop *********************************

// ***************************** Begin cleanup *********************************
    destroy_bitmap (buffer) ;
    destroy_bitmap (starBuffer) ;
    destroy_bitmap (starLayer) ;
    destroy_bitmap (landscapeLayer) ;
// ***************************** End cleanup ***********************************
    return 0 ;
}

END_OF_MAIN ()
