Code:
// INCLUDES FOR WINDOW //////////////
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <math.h>
// INCLUDES FOR GAME ////////////////
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
#include <time.h>
// DEFINES //////////////////////////
#define WINDOW_CLASS_NAME "WINCLASS1"
#define MAX_X 246 // Max X Pos for player
// Max Y is 247
#define SCROLL_POS 249 // 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 //////////////////////////
// save the window handle
HWND main_window_handle = NULL;
CONSOLE_SCREEN_BUFFER_INFO con_info; // Holds screen info
HANDLE hconsole; // Handle to Console
int game_running = 1; // State of game 1=run 0=done
// FUNCTIONS ////////////////////////
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT ps; // used in WM_PAINT
HDC hdc; // handle to a device context
// find out what the message is
switch(msg)
{
case WM_CREATE: // called when window created
{
// do initialization stuff here
return(0);
} break;
case WM_PAINT: // called when window needs painting
{
// simply validate the window
hdc = BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return(0);
} break;
case WM_DESTROY: // called when window is killed
{
// kill the application
PostQuitMessage(0);
return(0);
} break;
default:break;
} // end switch
// process all messages that you didn't take care of
return (DefWindowProc(hwnd, msg, wparam, lparam));
} // end WinProc
void Init_Graphics(void)
{
// This initializes the console graphics engine
COORD console_size = {250,250}; // Size of Console
// Seed the random number generator with time
srand((unsigned)time(NULL));
// Open i/o channel to console screen
hconsole=CreateFile("CONOUT$",GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
// Make sure we are in 80x25
SetConsoleScreenBufferSize(hconsole,console_size);
// Get details for console screen
GetConsoleScreenBufferInfo(hconsole,&con_info);
} // end Init_Graphics
inline void Set_Color(int fcolor, int bcolor=0)
{
// This function sets the color of the console output
SetConsoleTextAttribute(hconsole,(WORD)((bcolor << 4) | fcolor));
} // End Set_Color
inline void Draw_String(int x, int y, char *string)
{
// This function draws a string at the give x,y
COORD cursor_pos; // used to pass coords
// set printing position
cursor_pos.X = x;
cursor_pos.Y = y;
SetConsoleCursorPosition(hconsole,cursor_pos);
// Print the string in current color
printf("%s",string);
} // End Draw_String
inline void Clear_Screen(void)
{
// This function clears the screen
// Set color to white on black
Set_Color(15,0);
// clear the screen
for (int index=0; index<=50; index++)
Draw_String(0, SCROLL_POS,"\n");
} // End Clear_Screen
// WINMAIN //////////////////////////
int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
WNDCLASS winclass; // this will hold the class you create
HWND hwnd; // generic window handle
MSG msg; // generic message
// first, fill in the window class structure
winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
// register the window class
if (!RegisterClass(&winclass))
return(0);
// create the window
if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
"Title", // Title
WS_OVERLAPPEDWINDOW | WS_VISIBLE, // Flags
500,500, // x,y
250,250, // width, height
NULL, // Handle to parent
NULL, // Handle to Menu
hinstance, // instance
NULL))) // creation parameters
return(0);
// save the window handle in a global
main_window_handle = hwnd;
// Main Event LOOP
while(1)
{
if (PeekMessage(&msg, NULL,0,0,PM_REMOVE))
{
// test whether this is a quit
if (msg.message == WM_QUIT) break;
// translate any accelerator keys
TranslateMessage(&msg);
// send the message to the window proc
} // end if
int main(void)
{
int xpos = 100;
int ypos = 100;
// SECTION: initialization
// Set up the console text graphics system
Init_Graphics();
// Clear the screen
Clear_Screen();
// SECTION: main event loop, this is where all the action
// takes place, (the general loop is erase-move-draw)
while(game_running)
{
char key; // Player input Data
int player_x = xpos; // Player's X position
int player_y = ypos; // Player's Y position
// SECTION: erase all 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());
// Check if player is trying to exit, if so, exit
if (key=='Q' || key==27)
game_running=0;
// Check if player is moving left
if (key=='A')
xpos--;
// Check if player is moving right
if (key=='D')
xpos++;
// Check if player is moving up
if (key=='W')
ypos--;
// Check if player is moving down
if (key=='S')
ypos++;
} // end if
// SECTION: game logic and futhur processing
// make sure player stays on screen
if (++xpos > MAX_X)
xpos=0;
if (--xpos < 0)
xpos=MAX_X;
if (++ypos > 50)
ypos=50;
if (--ypos < 0)
ypos=0;
// SECTION: draw everything
// Draw Player
Set_Color(rand()%15,0);
Draw_String(xpos,ypos,"<-*->");
Draw_String(0,999,"");
// SECTION: synchronize to a constant frame rate
Sleep(40);
} // End while
// SECTION: shutdown and bail
Clear_Screen();
} // End main
} // end while
// return to windows like this
return(msg.wParam);
} // end WinMain
The errors are: