// A simple console based game, illustrates game loop
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
#include <windows.h>
#include <time.h>

#define MAX_X        77  // maximum x position for player
#define SCROLL_POS   24  // the point that scrolling occurs

// PROTOTYPES
void Init_Graphics(void);
inline void Set_Color(int fcolor, int bcolor);
inline void Draw_String(int x,int y, char *string);

// GLOBALS
CONSOLE_SCREEN_BUFFER_INFO con_info;   // holds screen info
HANDLE hconsole;         // handle to console
int    game_running = 1; // state of game, 0=done, 1=run

// ******************* FUNCTIONS ************************
void Init_Graphics(void)
{  // this function initializes the console graphics engine
    COORD console_size = {80,25};  // size of console
    srand((unsigned)time(NULL));       // seed the random generator

    // open I/O channel to console screen
    hconsole=CreateFile("CONOUT$",GENERIC_WRITE|GENERIC_READ,
                  
                    FILE_SHARE_READ | FILE_SHARE_WRITE,0L,
                     
                 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0L);
    
SetConsoleScreenBufferSize(hconsole,console_size);// make sure we are in 80x25
    GetConsoleScreenBufferInfo(hconsole,&con_info);   // get details for console screen 
}

inline void Draw_String(int x,int y, char *string)
{   // this function draws a string at the given x,y
     COORD cursor_pos;   // used to pass coords

     cursor_pos.X = x;        // set printing position
   
  cursor_pos.Y = y;  SetConsoleCursorPosition(hconsole,cursor_pos);
    
printf("%s",string);     // print the string in current color
}

inline void Clear_Screen(void)
{  // this function clears the screen
    Set_Color(15,0);  // set color to white on black
    for (int index=0; index<=25; index++)    // clear the screen
          Draw_String(0, SCROLL_POS,"\n");
}

inline void Set_Color(int fcolor, int bcolor=0)
{  // this function sets the color of the console output
    SetConsoleTextAttribute(hconsole,(WORD)((bcolor << 4) | fcolor));
}

// ***************  MAIN GAME LOOP *******************
void main(void)
{  char key;            // player input data
    int  player_x = 40;  // player's x position

    // SECTION: initialization
    Init_Graphics();  // set up the console text graphics system
    Clear_Screen();   // clear the screen

    // SECTION: main event loop, erase-move-draw
    while(game_running)
    { // SECTION: erase all the objects or clear screen
       // nothing to erase in our case  

       // SECTION: get player input
       if (kbhit())
       {  // get keyboard data, and filter it
           key = toupper(getch());
           if (key=='Q' || key==27)  game_running = 0; // exit
           else if (key=='A') player_x--;                       // moving left  
           else if (key=='S')  player_x++;                     // moving right
        }  

        // SECTION: game logic and further processing
        // make sure player stays on screen
        if (++player_x > MAX_X)   player_x=MAX_X;    
        if (--player_x < 0)                player_x=0;    

        // SECTION: draw everything
       
        Set_Color(15,0);                 // draw next star at random position
        Draw_String(rand()%80, SCROLL_POS,".\n");    
       
        Set_Color(rand()%15,0);    // draw player
        Draw_String(player_x,0,"<-*->");
        Draw_String(0,0,"");  

         // SECTION: synchronize to a constant frame rate
         Sleep(40);  
      } // end while

      // SECTION: shutdown and bail
     Clear_Screen();
     printf("\nG A M E  O V E R \n\n");
}