there are repeats? i could have SWORN i checked every line of code there 2 - 3 times.. ill get rid of the extras.. then ill post most of my code if it doesnt work.. thank you for the speedy replies.
EDITk. the repeats are only in the forum because of my own mistake.. and im not able to edit my first post for.. whatever reason. so ill add it all to here.
first. the contents of main:
Code:
#include "headers.h"
room MyRoom;
int main()
{
MyRoom.Init(10, 10, 8, 4);
Character ASCIIMan = MyRoom.getPlayer();
int PlayerX;
int PlayerY;
while(1)
{
MyRoom.draw();
PlayerX = ASCIIMan.GetX();
PlayerY = ASCIIMan.GetY();
char PressedKey = console.ReadKey();
switch(PressedKey){
case Down:
if (MyRoom.isWalkable(console.GetASCIITile(PlayerX, PlayerY+1)))
{
ASCIIMan.Walk(PlayerX, PlayerY+1);
};
};
console.UpdateScreen();
};
return 0;
};
and the contents of character.h
Code:
#ifndef CHARACTER_
#define CHARACTER_
#include "headers.h"
class Character
{
public:
//methods:
void Init(int NewX, int NewY, char NewChar);
void Draw();
int Walk(int newX, int newY);
void Attack(Character);
int GetHealth();
int GetX();
int GetY();
char GetName();
private:
//attributes
char Name;
int Health;
int X;
int Y;
char ASCIICharacter;
};
#endif
and the code for character.cpp
Code:
#include "headers.h"
void Character::Init(int NewX, int NewY, char NewChar)
{
X = NewX;
Y = NewY;
ASCIICharacter = NewChar;
};
void Character::Draw()
{
console.Draw(X, Y, ASCIICharacter);
};
int Character::Walk(int newX, int newY)
{
X = newX;
Y = newY;
};
int Character::GetHealth()
{
return Health;
};
int Character::GetX()
{
return X;
};
int Character::GetY()
{
return Y;
};
char Character::GetName()
{
return Name;
};
console.h
Code:
class ConsoleWindow
{
public:
// constructor
ConsoleWindow();
// Draws an ASCII character to given location on backbuffer.
void Draw(int nX, int nY, char chrAscii);
// Draws text at given location on backbuffer.
void DrawText(int nX, int nY, const char* szText);
// Gets the character at a given location on screen.
char GetASCIITile(int nX, int nY);
// Flips buffers. Actually draws the entire screen
void UpdateScreen();
// Reads a key from keyboard.
char ReadKey();
private:
HANDLE GetActiveBuffer();
// Hides cursor on-screen.
void HideCursor();
HANDLE hBackBuffer;
HANDLE hFrontBuffer;
int nActiveBuffer;
};
extern ConsoleWindow console;
and console.cpp
Code:
#include "headers.h"
ConsoleWindow::ConsoleWindow()
{
hBackBuffer = CreateConsoleScreenBuffer(
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
CONSOLE_TEXTMODE_BUFFER,
NULL
);
hFrontBuffer = CreateConsoleScreenBuffer(
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
CONSOLE_TEXTMODE_BUFFER,
NULL
);
HideCursor();
UpdateScreen();
}
void ConsoleWindow::HideCursor()
{
CONSOLE_CURSOR_INFO ci = {10,FALSE}; /* bVisible = FALSE */
HANDLE hStdout = hBackBuffer;
SetConsoleCursorInfo(hStdout, &ci);
hStdout = hFrontBuffer;
SetConsoleCursorInfo(hStdout, &ci);
}
void ConsoleWindow::Draw(int nX, int nY, char chrAscii)
{
if(nX<0 || nX > 79)
return;
if(nY<0 || nY > 23)
return;
COORD coordScreen;
DWORD dwWritten;
HANDLE hStdout = GetActiveBuffer();
coordScreen.X=nX;
coordScreen.Y=nY;
SetConsoleCursorPosition(hStdout, coordScreen);
DWORD dw;
WriteConsole(hStdout, &chrAscii, 1, &dw, NULL);
}
void ConsoleWindow::DrawText(int nX, int nY, const char* szText)
{
COORD coordScreen;
HANDLE hStdout = GetActiveBuffer();
coordScreen.X=nX;
coordScreen.Y=nY;
SetConsoleCursorPosition(hStdout, coordScreen);
DWORD dw;
WriteConsole(hStdout, szText, strlen(szText), &dw, NULL);
coordScreen.X=79;
coordScreen.Y=24;
SetConsoleCursorPosition(hStdout, coordScreen);
}
char ConsoleWindow::GetASCIITile(int nX, int nY)
{
char chr;
COORD coordScreen;
HANDLE hStdout = GetActiveBuffer();
coordScreen.X=nX;
coordScreen.Y=nY;
DWORD dw;
ReadConsoleOutputCharacter(
hStdout,
&chr,
1,
coordScreen,
&dw
);
return chr;
}
HANDLE ConsoleWindow::GetActiveBuffer()
{
if(nActiveBuffer==0)
{
return hFrontBuffer;
}
else
{
return hBackBuffer;
}
}
void ConsoleWindow::UpdateScreen()
{
SetConsoleActiveScreenBuffer(GetActiveBuffer());
nActiveBuffer = !nActiveBuffer;
}
char ConsoleWindow::ReadKey()
{
COORD coordScreen;
coordScreen.X=79;
coordScreen.Y=24;
HANDLE hStdout = hBackBuffer;
if(nActiveBuffer==0)
{
// nActiveBuffer = 1;
hStdout = hFrontBuffer;
}
SetConsoleCursorPosition(hStdout, coordScreen);
return _getch();
}
ConsoleWindow console;
room.h
Code:
#ifndef ROOM_
#define ROOM_
#include "headers.h"
#include "character.h"
#include "define.h"
class room
{
public:
void Init(int newX, int newY, int newWidth, int newBreadth);
int getBreadth();
int getWidth();
int getX();
int getY();
bool isWalkable(char newASCIIChar);
Character getPlayer();
void draw();
private:
int Width;
int Breadth;
int X;
int Y;
Character ASCIIMan;
};
void room::Init(int newX, int newY, int newWidth, int newBreadth){
X = newX;
Y = newY;
Width = newWidth + 1; // This is so the room is as wide as we told it to be
Breadth = newBreadth + 1; // This is so the room is as tall as we told it to be
ASCIIMan.Init(X + Width - 2, Y + Breadth - 2, PlayerASCII);
}
int room::getBreadth()
{
return Breadth;
}
int room::getWidth()
{
return Width;
}
int room::getX()
{
return X;
}
int room::getY()
{
return Y;
};
bool room::isWalkable(char newASCIIChar)
{
int tile = newASCIIChar;
if (tile == FloorTileASCII ||
tile == PlayerASCII)
{
return true;
}
else
{
return false;
}
}
Character room::getPlayer()
{
return ASCIIMan;
}
void room::draw()
{
int drawX = X;
int drawY = Y;
// Draw the floor tiles
while(drawX < X + Width)
{
while(drawY < Y + Breadth)
{
// Draw tile drawX, drawY
console.Draw(drawX, drawY, FloorTileASCII);
drawY++;
}
drawX++;
drawY = Y;
}
// Draw the walls
drawX = X;
while(drawX < X + Width)
{
console.Draw(drawX, Y, HorizontalWallASCII);
console.Draw(drawX, Y + Breadth, HorizontalWallASCII);
drawX++;
}
drawY = Y;
while(drawY < Y + Breadth)
{
console.Draw(X, drawY, VerticalWallASCII);
console.Draw(X + Width, drawY, VerticalWallASCII);
drawY++;
}
// Draw the corners
console.Draw(X, Y, TopLeftCornerASCII);
console.Draw(X + Width, Y, TopRightCornerASCII);
console.Draw(X + Width, Y + Breadth, BotRightCornerASCII);
console.Draw(X, Y + Breadth, BotLeftCornerASCII);
}
#endif
earlier i bundled my room.h and room.cpp to make it easier to debug instead of going through multiple files.
and lastly, the code for "headers.h"
Code:
#ifndef _HEADERS_
#define _HEADERS_
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <wincon.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include "console.h"
#include "room.h"
#define PlayerASCII = 0x01; // ASCII Man
#define DuckASCII = 0x44; // ASCII Duck
#define VerticalWallASCII = 0xBA; // Vertical wall piece
#define HorizontalWallASCII = 0xCD; // Horizontal wall piece
#define TopRightCornerASCII = 0xBB; // Top right wall piece
#define TopLeftCornerASCII = 0xC9; // Top left wall piece
#define BotLeftCornerASCII = 0xC8; // Bottom right wall piece
#define BotRightCornerASCII = 0xBC; // Bottom left wall piece
#define FloorTileASCII = 0x2E; // Floor tile
#endif
headers.cpp
and defines.h
Code:
#define PlayerASCII = 0x01; // ASCII Man
#define DuckASCII = 0x44; // ASCII Duck
#define VerticalWallASCII = 0xBA; // Vertical wall piece
#define HorizontalWallASCII = 0xCD; // Horizontal wall piece
#define TopRightCornerASCII = 0xBB; // Top right wall piece
#define TopLeftCornerASCII = 0xC9; // Top left wall piece
#define BotLeftCornerASCII = 0xC8; // Bottom right wall piece
#define BotRightCornerASCII = 0xBC; // Bottom left wall piece
#define FloorTileASCII = 0x2E; // Floor tile
if its of any concelation. im currently using code::blocks but the code was originally from from dev-cpp with the same errors.
and the errors
Project : Console application
Compiler : GNU GCC Compiler (called directly)
Directory : C:\Documents and Settings\john\My Documents\
--------------------------------------------------------------------------------
Switching to target: default
Compiling: console.cpp
In file included from headers.h:12,
from console.cpp:1:
room.h: In member function `void room::Init(int, int, int, int)':
room.h:31: error: `PlayerASCII' undeclared (first use this function)
room.h:31: error: (Each undeclared identifier is reported only once for each function it appears in.)
room.h: In member function `bool room::isWalkable(char)':
room.h:58: error: `FloorTileASCII' undeclared (first use this function)
room.h:59: error: `PlayerASCII' undeclared (first use this function)
room.h: In member function `void room::draw()':
room.h:85: error: `FloorTileASCII' undeclared (first use this function)
room.h:96: error: `HorizontalWallASCII' undeclared (first use this function)
room.h:103: error: `VerticalWallASCII' undeclared (first use this function)
room.h:109: error: `TopLeftCornerASCII' undeclared (first use this function)
room.h:110: error: `TopRightCornerASCII' undeclared (first use this function)
room.h:111: error: `BotRightCornerASCII' undeclared (first use this function)
room.h:112: error: `BotLeftCornerASCII' undeclared (first use this function)
Process terminated with status 1 (0 minutes, 4 seconds)
11 errors, 0 warnings