I am a simple newbie at C++ but im eager to learn. I tried to use the sample code from a C++ book (windows game programming for dummies) and I compiled it on my Dev C++ compiler. After fixing some simple errors (usually spelling mistakes) I found 2 errors I can't figure out. Here are the errors:
[Warning] In function 'void Init_Graphics()";
'OL' undeclared (first use this function)
(Each undeclared identifier is reported only once for each)
And another error:
At global scope:
'main' must return 'int'
[Build Error] [main.o] Error 1
And here is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
// DEFINES /////////////////
#define MAX_X 77 // Max X Pos 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 1=run 0=done
// FUNCTIONS ///////////////
void Init_Graphics(void)
{
// This initializes the console graphics engine
COORD console_size = {80,25}; // 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, OL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, OL);
// 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<=25; index++)
Draw_String(0, SCROLL_POS,"\n");
} // End Clear_Screen
// MAIN GAME LOOP /////////
void main(void)
{
char key; // Player input Data
int player_x = 40; // Players X position
// 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)
{
// 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')
player_x--;
// Check if player is moving right
if (key=='S')
player_x++;
} // end if
// SECTION: game logic and futhur 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
// draw next star at random position
Set_Color(15,0);
Draw_String(rand()%80, SCROLL_POS,".\n");
// Draw Player
Set_Color(rand()%15,0);
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");
} // End main
Yes, I know its a simple game, but you gotta start somewhere.