Sign in to follow this  
polpoint

strings and arrays in structures/classes

Recommended Posts

hey! I have: ------in object.h: class object { public: MAPCOORD mapCoord; //leads to the coordinates on the map INV i; //right now this just tells if an actor is in an object //bunch of other variables }; ------in mapcoord.h class MAPCOORD { public: int x; int y; }; ------in inv.h class INV { public: PLAYER* invnt[1]; //if an object has the player, this will point to it //(i was going to change it to less ambiguous // ACTOR class later) }; --------in player.h class PLAYER { public: OBJECT obj; int facing; //direction it is facing int topX; //used for screen position int topY; //screen position! }; ----------in tile.h class TILE { public: OBJECT obj; bool update; }; --------in control.h //then I have a control class that does everything class CONTROL { public: UNI u; DISPLAY disp; PLAYER curPlay[1]; TILE mapA[MAPWIDTH][MAPHEIGHT]; //all the functions go under here- move player around call on display class etc. }; If I add any string past a certain length to the OBJECT class, or I tried to add : OBJECT* inventory[30]; to INV class and it compiles fine but when I run it i get a stack overflow when the program starts to access main() I was able to add OBJECT* inventory[5]; and it worked fine. why does it do this? yeah lists seem a lot better right now.... but why does it do this???

Share this post


Link to post
Share on other sites
I'd love to help you but it still seems a little confusing. If you posted your full commented source as well as the main method and a simple explaination I may be able to help.

-sevans

Share this post


Link to post
Share on other sites
Data structures themselves can't cause a runtime problem. (Although WTF is up with these arrays of one element?) How you use them can. It could be any number of things. How big is your project right now? If only a couple hundred lines, feel free to drop it in a source box and I'll look at it later. If it's big, try to trim it down to the minimum that causes your problem.

Share this post


Link to post
Share on other sites
You need to learn pointers.

Quote:
Original post by polpoint
------in inv.h

PLAYER* invnt[1]; //if an object has the player, this will point to it
//(i was going to change it to less ambiguous
// ACTOR class later)
--------in control.h //then I have a control class that does everything

PLAYER curPlay[1];



You are allocating a single spot in memory to hold a pointer. What you want to do here is:


// setup a single pointer
PLAYER* invnt;


With what you have written you would have a pointer to a pointer. This will lead to unnecessary dereferencing. (Most likely the compiler will optimize it out to a single pointer but you sacrifice readability.)

Quote:
Original post by polpoint
OBJECT* inventory[30];


With that OBJECT variable definition, you have created an array of pointers to OBJECTS not an OBJECT itself. You have to allocate one somewhere else and setup this array prior to use.


// allocate an array for pointers
OBJECT* inv[30];
// create all or objects
for(int i = 0; i < 30; i++)
{
inv[i] = new OBJECT;
}

// accessing the object
inv[5]->SomeMethodOfObject();
inv[5]->m_SomePublicVariable;


PS: Pointers are a bit confusing at first but they allow you to do some fancy tricks. :P

Happy coding!

Share this post


Link to post
Share on other sites
thanks for the replies!
to lordshade:
thanks! that is a good explanation, and one I could have used two days ago

but I figured out about *'s &'s and ->'s once I did, it was amazing, let me tell you









ok, I hope I am using the forum correctly:

(I could not find how to post my code correctly if it is not in a little box, looked in the FAQ...)



here goes my source code:


this runs correctly and compiles correctly on my machine
I use vc++ 7.1


this allows the character to run around a map and scrolls the screen when he gets close to the edge.


I was trying to make everything encapsulated nicely, then I gave up and tried to figure out how everything works and rewrote the program from the start pretty much. this is the result



it is a little bit long and probably hoorrrible form, but I tried to comment everything



anybody can use my code if they want to. whether to use it in their own program or to show how people should not use it in their programs.











>>>START CONTROL_CPP<<<




#include "control.h"
#include "display.h"

#include <time.h>

#include <stdlib.h>
#include <iostream>

#define MAPWIDTH 120
#define MAPHEIGHT 120
#define OUTPUTWIDTH 60
#define OUTPUTHEIGHT 40

class TILE;

DISPLAY D;








void CONTROL::reset(int x, int y, int type)
{
switch(type)
{
case 0:{


// mapA[x][y].obj.name="floor";
// mapA[x][y].obj.desc="Looks like the floor.";
mapA[x][y].obj.beenSeen=false;
mapA[x][y].obj.color=60;
mapA[x][y].obj.mapCoord.x=x;
mapA[x][y].obj.mapCoord.y=y;
mapA[x][y].obj.opaque=false;
mapA[x][y].obj.output='.';
mapA[x][y].obj.seen=true;
mapA[x][y].obj.type=0;
mapA[x][y].obj.location=0;
mapA[x][y].obj.walkable=true;
// mapA[x][y].obj.IDnum=9999;
break;
}
case 1:{

// mapA[x][y].obj.name="ext";
// mapA[x][y].obj.desc="Looks like an exit.";

mapA[x][y].obj.beenSeen=false;
mapA[x][y].obj.color=49;
mapA[x][y].obj.mapCoord.x=x;
mapA[x][y].obj.mapCoord.y=y;
mapA[x][y].obj.opaque=false;
mapA[x][y].obj.output='X';
mapA[x][y].obj.seen=true;
mapA[x][y].obj.type=9;
mapA[x][y].obj.location=0;
mapA[x][y].obj.walkable=true;
//mapA[x][y].obj.IDnum=9999;

break;
}
case 2:{

// mapA[x][y].obj.name="obstacle";
// mapA[x][y].obj.desc="Looks like an obstacle.";
mapA[x][y].obj.beenSeen=false;
mapA[x][y].obj.color=0;
mapA[x][y].obj.mapCoord.x=x;
mapA[x][y].obj.mapCoord.y=y;
mapA[x][y].obj.opaque=true;
mapA[x][y].obj.output='#';
mapA[x][y].obj.seen=true;
mapA[x][y].obj.type=0;
mapA[x][y].obj.location=0;
mapA[x][y].obj.walkable=false;
//mapA[x][y].obj.IDnum=9999;
break;
}
case 3:{
// mapA[x][y].obj.name="grass";
// mapA[x][y].obj.desc="Looks like some grass.";
mapA[x][y].obj.beenSeen=false;
mapA[x][y].obj.color=30;
mapA[x][y].obj.mapCoord.x=x;
mapA[x][y].obj.mapCoord.y=y;
mapA[x][y].obj.opaque=true;
mapA[x][y].obj.output='"';
mapA[x][y].obj.seen=true;
mapA[x][y].obj.type=0;
mapA[x][y].obj.location=0;
mapA[x][y].obj.walkable=true;
//mapA[x][y].obj.IDnum=9999;
break;
}
}

}


void CONTROL::generatePlayer(int x, int y)
{
//curPlay[0].obj.name="Ian";
//curPlay[0].obj.desc="Looks like Ian.";
curPlay[0].obj.color=5;
curPlay[0].obj.output='@';
curPlay[0].obj.location=0;
curPlay[0].obj.mapCoord.x=x;
curPlay[0].obj.mapCoord.y=y;
curPlay[0].facing=9;
curPlay[0].topX=0;
curPlay[0].topY=0;
//curPlay[0].obj.IDnum=999;
mapA[x][y].obj.i.invnt[0]=&curPlay[0];
mapA[x][y].obj.hasAnActor=true;
mapA[x][y].update=true;




}










//yeah! I had this working at one point... now when I try to add the inventory
//back into the INV class, I get the stack overflow


/*
void CONTROL::generateItems(int number, int topX, int topY, int botX, int botY)
{
srand ( time(NULL) );
int x;
int y;
int color;
for(int counteR = 1; counteR < number; counteR++)
{

color=(rand()%14);
items[counteR].color=color;
//items[counteR].desc="An item.";
//items[counteR].name="item";
items[counteR].output='o';
items[counteR].location=0;
items[counteR].type=2;

x=(rand()%25);
y=(rand()%25);
items[counteR].mapCoord.x=x;
items[counteR].mapCoord.y=y;
mapA[x][y].obj.hasAnItem=true;

//items[counteR].IDnum=counteR;
for(int counteD=0;mapA[x][y].obj.i.inventory[counteD]!=0;counteD++){}
mapA[x][y].obj.i.inventory[counteD]=&items[counteR];
//mapA[x][y].obj.i.invIndex[counteD]=counteR;

}



}
*/





//this moves the player to a certain square- also if the square is within 8 tiles
//of the windows edge, it recenters the screen with the player at the middle


void CONTROL::moveP(int curX, int curY, int destX, int destY, int topX, int topY)
{
//check if curX & curY has a character and destX & destY does not and is walkable
if(mapA[curX][curY].obj.i.invnt[0]!=0 && mapA[destX][destY].obj.i.invnt[0]==0 && mapA[destX][destY].obj.walkable)
{

//this moves the player and unmoves them from the previous square
mapA[curX][curY].obj.i.invnt[0]=0;
mapA[curX][curY].obj.hasAnActor=false;
mapA[curX][curY].update=true;
mapA[destX][destY].obj.i.invnt[0]=&curPlay[0];
mapA[destX][destY].obj.hasAnActor=true;
mapA[destX][destY].update=true;
curPlay[0].obj.mapCoord.x=destX;
curPlay[0].obj.mapCoord.y=destY;

//all this checks if the player is close to the edge of the window
//and if he is, it moves recenters
if(destX > curPlay[0].topX + (OUTPUTWIDTH - 8) || destX < curPlay[0].topX + 8)
{
int varX;

if(destX > 8)
{

if(destX > (OUTPUTWIDTH / 2))
{
varX = (destX - (OUTPUTWIDTH / 2));
}
if(destX <= (OUTPUTWIDTH / 2))
{
varX = ((OUTPUTWIDTH / 2) - destX);
}

if(varX <= 7)
{
varX = 0;
}
D.Ater(3,45,(unsigned int) varX);




curPlay[0].topX = varX;

dsp(varX,curPlay[0].topY);
}
else
{
curPlay[0].topX = 0;
}


}

if(destY > curPlay[0].topY + (OUTPUTHEIGHT - 8) || destY < curPlay[0].topY + 8 )
{
int varY;
if(destY >= 8)
{
if(destY > (OUTPUTHEIGHT / 2))
{
varY = (destY - (OUTPUTHEIGHT / 2));
}
if(destY <= (OUTPUTHEIGHT / 2))
{
varY = (destY - (OUTPUTHEIGHT / 2));
}

if(varY <= 7)
{
varY = 0;
}

D.Ater(3,46,(unsigned int) varY);
curPlay[0].topY = varY;
dsp(curPlay[0].topX,varY);
}
else
{
curPlay[0].topY = 0;
}

}

}

}







//this generates a blank map MAPWIDTH X MAPHEIGHT and puts a bunch of exit squares around the edges

void CONTROL::generateBlankMap()
{
int xVal;
int yVal;
for(int counteY = 0; counteY < MAPHEIGHT; counteY++)
{
for(int counteX = 0; counteX < MAPWIDTH; counteX++)
{
reset(counteX,counteY,0);
}



}
for(xVal = 0;xVal < MAPWIDTH; xVal++)
{
reset(xVal,0,1);
}
for(yVal = 0;yVal < MAPHEIGHT;yVal++)
{
reset(0,yVal,1);
}
for(xVal = 0;xVal <MAPWIDTH; xVal++)
{
reset(xVal,MAPHEIGHT-1,1);
}
for(yVal = 0;yVal < MAPHEIGHT;yVal++)
{
reset((MAPWIDTH-1),yVal,1);
}





}



//this tells DISPLAY how large to make the window


void CONTROL::scrInt()
{
D.scrSize(OUTPUTWIDTH,OUTPUTHEIGHT+10);
//D.scrSize(MAPWIDTH+10,MAPHEIGHT+10);

}



//this was my old display function - this is also in case I need to display the entire map, for some reason

void CONTROL::display()
{
//display map:
int xVal = 0;
int yVal = 0;
int x;
int y;

for(int y = 0; y<MAPHEIGHT; y++)
{
for(int x = 0; x<MAPWIDTH; x++)
{
// part of if statement below - C.mapA[x][y].obj.seen &&
if( x < MAPWIDTH && y < MAPHEIGHT)
{

D.color(mapA[x][y].obj.color);


D.At(x,y,(unsigned char) mapA[x][y].obj.output);

}
else if(mapA[x][y].obj.beenSeen)
{
D.color(7);//SetConsoleTextAttribute(hOutput, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN);
D.At(x,y,(unsigned char) mapA[x][y].obj.output);
}

else
{
D.color(0);//SetConsoleTextAttribute(hOutput, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
D.At(x,y,(unsigned char) ' ');
}
mapA[x][y].obj.seen=false;
}
}

}





//this is my current display function. this need to be run every time the screen is refreshed

void CONTROL::dsp(int topX,int topY)
{


int x = 0;
int y = 0;
int varX = topX;
int varY = topY;


if(topX > MAPWIDTH - OUTPUTWIDTH)
{
topX = (MAPWIDTH - OUTPUTWIDTH);
}
if(topY > MAPHEIGHT - OUTPUTHEIGHT)
{
topY = (MAPHEIGHT - OUTPUTHEIGHT);
}




for(int y = 0, varY=topY; y<OUTPUTHEIGHT; y++,varY++)
{
for(int x = 0, varX=topX; x<OUTPUTWIDTH; x++,varX++)
{

if(mapA[varX][varY].obj.seen && varX < MAPWIDTH && varY < MAPHEIGHT)
{

//if mapsquare has an actor in it, print the actors character instead
if(mapA[varX][varY].obj.hasAnActor)
{

D.color(mapA[varX][varY].obj.i.invnt[0]->obj.color);
D.At(x,y,(unsigned char) mapA[varX][varY].obj.i.invnt[0]->obj.output);
}



//this used to be my checks_for_an_item function



/*
else if(mapA[varX][varY].obj.hasAnItem)
{
for(int counteR=0;counteR<30;counteR++)
{
if(mapA[varX][varY].obj.i.inventory[counteR] != 0)
{
D.color(mapA[varX][varY].obj.i.inventory[counteR]->color);
D.At(x,y, (unsigned char) mapA[varX][varY].obj.i.inventory[counteR]->output);
}
}
}
*/




//if the mapsquare is empty, print the mapsquare character

else
{

D.color(mapA[varX][varY].obj.color);
D.At(x,y,(unsigned char) mapA[varX][varY].obj.output);
}

}


//this if for FOV, this is the memory, if you have seen a square,
// but cant see it anymore it has beenSeen

//i dont have the FOV in this at the moment

else if(mapA[varX][varY].obj.beenSeen)
{
D.color(1);
D.At(x,y,(unsigned char) mapA[varX][varY].obj.output);
}

//if you cant see anything, you cant see anything

else
{
D.color(0);
D.At(x,y,(unsigned char) ' ');
}


//this is for the FOV, but I dont have it right now
//mapA[varX][varY].obj.seen=false;
}
}




}



//this is to update all of the mapsquares that have been flagged.
//so I do not have to redraw the whole screen every move


void CONTROL::upd(int topX,int topY)
{


int x = 0;
int y = 0;
int varX = topX;
int varY = topY;


if(topX > MAPWIDTH - OUTPUTWIDTH)
{
topX = (MAPWIDTH - OUTPUTWIDTH);
}
if(topY > MAPHEIGHT - OUTPUTHEIGHT)
{
topY = (MAPHEIGHT - OUTPUTHEIGHT);
}




for(int y = 0, varY=topY; y<OUTPUTHEIGHT; y++,varY++)
{
for(int x = 0, varX=topX; x<OUTPUTWIDTH; x++,varX++)
{
if(mapA[varX][varY].update)
{
if(mapA[varX][varY].obj.seen && varX < MAPWIDTH && varY < MAPHEIGHT )
{


if(mapA[varX][varY].obj.hasAnActor)
{

D.color(mapA[varX][varY].obj.i.invnt[0]->obj.color);
D.At(x,y,(unsigned char) mapA[varX][varY].obj.i.invnt[0]->obj.output);
}



//yay! my old Item display function again!


/*
else if(mapA[varX][varY].obj.hasAnItem)
{
for(int counteR=0;counteR<30;counteR++)
{
if(mapA[varX][varY].obj.i.inventory[counteR] != 0)
{
D.color(mapA[varX][varY].obj.i.inventory[counteR]->color);
D.At(x,y, (unsigned char) mapA[varX][varY].obj.i.inventory[counteR]->output);
}
}
}
*/




else
{

D.color(mapA[varX][varY].obj.color);
D.At(x,y,(unsigned char) mapA[varX][varY].obj.output);
}

}
else if(mapA[varX][varY].obj.beenSeen)
{
D.color(1);
D.At(x,y,(unsigned char) mapA[varX][varY].obj.output);
}

else
{
D.color(0);
D.At(x,y,(unsigned char) ' ');
}
mapA[varX][varY].update=false;

//mapA[varX][varY].obj.seen=false;
}
}
}


}






//this function is used to capture the input from the player and call on another function to move the player.
//or just change its output to the direction it is facing

//yeah yeah, i am going to clean this up eventually

int CONTROL::captureInput(int key)
{











if(key==56) // forward key (number 8) // key 2 is 50
{
if(curPlay[0].facing == 2)
{
if((curPlay[0].facing == 2))
{ //&& (mapA[(curPlay[0].obj.mapCoord.x)][(curPlay[0].obj.mapCoord.y+1)].obj.walkable)) // 2



moveP(curPlay[0].obj.mapCoord.x,curPlay[0].obj.mapCoord.y, curPlay[0].obj.mapCoord.x,(curPlay[0].obj.mapCoord.y+1), curPlay[0].topX, curPlay[0].topY);


key = 0;
}

}
else if(curPlay[0].facing == 1)
{
if(curPlay[0].facing == 1 && (mapA[curPlay[0].obj.mapCoord.x-1][curPlay[0].obj.mapCoord.y+1].obj.walkable)) // 2
{


moveP(curPlay[0].obj.mapCoord.x,curPlay[0].obj.mapCoord.y, curPlay[0].obj.mapCoord.x-1, curPlay[0].obj.mapCoord.y+1, curPlay[0].topX, curPlay[0].topY);



key = 0;
}

}
else if(curPlay[0].facing == 3)
{
if(curPlay[0].facing == 3 && (mapA[curPlay[0].obj.mapCoord.x+1][curPlay[0].obj.mapCoord.y+1].obj.walkable)) // 2
{

moveP(curPlay[0].obj.mapCoord.x,curPlay[0].obj.mapCoord.y, curPlay[0].obj.mapCoord.x+1, curPlay[0].obj.mapCoord.y+1, curPlay[0].topX, curPlay[0].topY);




key = 0;
}

}
else if(curPlay[0].facing == 4)
{
if(curPlay[0].facing == 4 && (mapA[curPlay[0].obj.mapCoord.x-1][curPlay[0].obj.mapCoord.y].obj.walkable)) // 2
{

moveP(curPlay[0].obj.mapCoord.x,curPlay[0].obj.mapCoord.y, curPlay[0].obj.mapCoord.x-1, curPlay[0].obj.mapCoord.y, curPlay[0].topX, curPlay[0].topY);





key = 0;
}

}
else if(curPlay[0].facing == 6)
{
if(curPlay[0].facing == 6 && (mapA[curPlay[0].obj.mapCoord.x+1][curPlay[0].obj.mapCoord.y].obj.walkable)) // 2
{

moveP(curPlay[0].obj.mapCoord.x,curPlay[0].obj.mapCoord.y, curPlay[0].obj.mapCoord.x+1, curPlay[0].obj.mapCoord.y, curPlay[0].topX, curPlay[0].topY);




key = 0;
}

}

else if(curPlay[0].facing == 7)
{
if(curPlay[0].facing == 7 && mapA[curPlay[0].obj.mapCoord.x-1][curPlay[0].obj.mapCoord.y-1].obj.walkable) // 2
{

moveP(curPlay[0].obj.mapCoord.x,curPlay[0].obj.mapCoord.y, curPlay[0].obj.mapCoord.x-1, curPlay[0].obj.mapCoord.y-1,curPlay[0].topX, curPlay[0].topY);




key = 0;
}

}

else if(curPlay[0].facing == 8)
{
if(curPlay[0].facing == 8 && mapA[curPlay[0].obj.mapCoord.x][curPlay[0].obj.mapCoord.y-1].obj.walkable) // 2
{

moveP(curPlay[0].obj.mapCoord.x,curPlay[0].obj.mapCoord.y, curPlay[0].obj.mapCoord.x, curPlay[0].obj.mapCoord.y-1, curPlay[0].topX, curPlay[0].topY);



key = 0;
}

}

else if(curPlay[0].facing == 9)
{
if(curPlay[0].facing == 9 && (mapA[curPlay[0].obj.mapCoord.x+1][curPlay[0].obj.mapCoord.y-1].obj.walkable)) // 2
{

moveP(curPlay[0].obj.mapCoord.x,curPlay[0].obj.mapCoord.y, curPlay[0].obj.mapCoord.x+1, curPlay[0].obj.mapCoord.y-1, curPlay[0].topX, curPlay[0].topY);



key = 0;
}

}

}
else if(key==52)
{
if(curPlay[0].facing == 1)
{
curPlay[0].facing = 2;


}
else if(curPlay[0].facing == 2)
{
curPlay[0].facing = 3;
}
else if(curPlay[0].facing == 9)
{
curPlay[0].facing = 8;
}
else if(curPlay[0].facing == 8)
{
curPlay[0].facing = 7;
}
else if(curPlay[0].facing == 4)
{
curPlay[0].facing = 1;
}
else if(curPlay[0].facing == 3)
{
curPlay[0].facing = 6;
}
else if(curPlay[0].facing == 7)
{
curPlay[0].facing = 4;
}
else if(curPlay[0].facing == 6)
{
curPlay[0].facing = 9;
}


}
else if(key==54)
{


if(curPlay[0].facing == 3)
{
curPlay[0].facing = 2;
}
else if(curPlay[0].facing == 2)
{
curPlay[0].facing = 1;
}
else if(curPlay[0].facing == 7)
{
curPlay[0].facing = 8;
}
else if(curPlay[0].facing == 8)
{
curPlay[0].facing = 9;
}
else if(curPlay[0].facing == 4)
{
curPlay[0].facing = 7;
}
else if(curPlay[0].facing == 9)
{
curPlay[0].facing = 6;
}
else if(curPlay[0].facing == 1)
{
curPlay[0].facing = 4;
}
else if(curPlay[0].facing == 6)
{
curPlay[0].facing = 3;
}

}
else if(key==49)
{



switch(curPlay[0].facing)
{

case 1:curPlay[0].facing = 9;
break;
case 2:curPlay[0].facing = 8;
break;
case 3:curPlay[0].facing = 7;
break;
case 4:curPlay[0].facing = 6;
break;
case 6:curPlay[0].facing = 4;
break;
case 7:curPlay[0].facing = 3;
break;
case 8:curPlay[0].facing = 2;
break;
case 9:curPlay[0].facing = 1;
break;
}

}




//this was the pick up key ','
//since Items are no longer implemented, this does not work

/*
else if(key==44)
{
if(mapA[curPlay[0].obj.mapCoord.x][curPlay[0].obj.mapCoord.y].obj.hasAnItem)
{
int tempKey = 0;
//
for(int counteR=0;counteR<30;counteR++)
{
if(mapA[curPlay[0].obj.mapCoord.x][curPlay[0].obj.mapCoord.y].obj.i.inventory[counteR] != 0)
{

D.At(OUTPUTHEIGHT+4,3,(unsigned char) "~");
std::cout << "pick up "<<mapA[curPlay[0].obj.mapCoord.x][curPlay[0].obj.mapCoord.y].obj.i.inventory[counteR]->output<<"? y/n";
tempKey = std::cin.get();
if(tempKey == 121)
{
//pick up
mapA[curPlay[0].obj.mapCoord.x][curPlay[0].obj.mapCoord.y].obj.i.inventory[counteR]->location=1;

//place in inventory
for(int tCount = 0; curPlay[0].obj.i.inventory[tCount] == 0; tCount++)
{
int itemIDX= mapA[curPlay[0].obj.mapCoord.x][curPlay[0].obj.mapCoord.y].obj.i.invIndex[tCount];

curPlay[0].obj.i.inventory[tCount]=&items[itemIDX];
//delete all traces of object from tile
mapA[curPlay[0].obj.mapCoord.x][curPlay[0].obj.mapCoord.y].obj.i.inventory[counteR]=0;
mapA[curPlay[0].obj.mapCoord.x][curPlay[0].obj.mapCoord.y].obj.i.invIndex[counteR]=999;

}

}

}


}
}
}
*/





else if(key==81)
{

return(9);

}
char outputVal;
//change character to direction facing (1235, etc)
switch(curPlay[0].facing)
{

case 1:outputVal = '1';
break;
case 2:outputVal = '2';
break;
case 3:outputVal = '3';
break;
case 4:outputVal = '4';
break;
case 6:outputVal = '6';
break;
case 7:outputVal = '7';
break;
case 8:outputVal = '8';
break;
case 9:outputVal = '9';
break;
}



//this changes the output character of the player to whatever direction they are facing
curPlay[0].obj.output= outputVal;




return 0;
}


>>>START DISPLAY_CPP<<<

//this is all the display stuff



#include <windows.h> // for the fullscreen
#include <iostream>

#include "display.h"
//#include "universe.h"


using namespace std;

#define MAPWIDTH 120
#define MAPHEIGHT 120
#define OUTPUTWIDTH 60
#define OUTPUTHEIGHT 40

//UNI U;

//CONTROL C;



#define FOVRADIUS 15












//I did not make this function... I forget where I got it from, cplusplus.com or something.
//thanks guy who made this!!

void DISPLAY::At(int x, int y, unsigned char What)
{

static bool First = TRUE;
COORD Position;
static HANDLE hOutput;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
if(First)
{

First = FALSE;
}

Position.X = x;
Position.Y = y;

SetConsoleCursorPosition(hOutput,
Position);

cout << What << flush;

return;
}

//this is the same as above, but displays ints instead!! yay!

void DISPLAY::Ater(int x, int y, unsigned int What)
{

static bool First = TRUE;
COORD Position;
static HANDLE hOutput;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
if(First)
{

First = FALSE;
}

Position.X = x;
Position.Y = y;

SetConsoleCursorPosition(hOutput,
Position);

cout << What << flush;

return;
}


//this sets the console to fullscreen mode! yay!

void DISPLAY::SetFullScreen()
{

keybd_event(VK_MENU,0x38,0,0);
keybd_event(VK_RETURN,0x1c,0,0);
keybd_event(VK_RETURN,0x1c,KEYEVENTF_KEYUP,0);
keybd_event(VK_MENU,0x38,KEYEVENTF_KEYUP,0);


}



//this changed the screen size to 0,0 z,d
//i did not make this function either, it was from some tutorial

void DISPLAY::scrSize(int z,int d)
{


HANDLE hInput, hOutput;
int x = z;
int y = d;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
hInput = GetStdHandle(STD_INPUT_HANDLE);

HANDLE hin, hout;
// Get handles to standard input and output
hin = GetStdHandle(DWORD STD_INPUT_HANDLE);
hout = GetStdHandle(STD_OUTPUT_HANDLE);

// Set the size of the console window
SMALL_RECT scrwindow;
scrwindow.Top = 0;
scrwindow.Left = 0;
scrwindow.Bottom = y;
scrwindow.Right = x;
SetConsoleWindowInfo(hout, TRUE, &scrwindow);
//set buffer size
COORD ScreenBufferSize;
ScreenBufferSize.X = x+1;
ScreenBufferSize.Y = y+1;
SetConsoleScreenBufferSize(hout,ScreenBufferSize);
//set window size again in case it was larger than buffer size the first time
scrwindow.Top = 0;
scrwindow.Left = 0;
scrwindow.Bottom = y;
scrwindow.Right = x;
SetConsoleWindowInfo(hout, TRUE, &scrwindow);
return;
}



//this function changes the display color! yay!
//yeeah I could have made it 1 color for foreground and 1 color for background
//instead of switch to 15 X 15 or however many combinations there are
//but I already did it this way

void DISPLAY::color(int colorNumber)
{


static HANDLE hOutput;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);





switch(colorNumber)
{
//SetConsoleTextAttribute(hOutput, map.cells[x][y].color);
//white
case 0:SetConsoleTextAttribute(hOutput, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
break;
//gray
case 1:SetConsoleTextAttribute(hOutput,FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN);
break;
//dark gray
case 2:SetConsoleTextAttribute(hOutput,FOREGROUND_INTENSITY);
break;
//Magenta
case 3:SetConsoleTextAttribute(hOutput, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
break;
//Dark Magenta
case 4:SetConsoleTextAttribute(hOutput, FOREGROUND_RED | FOREGROUND_GREEN);
break;
//Red
case 5:SetConsoleTextAttribute(hOutput, FOREGROUND_RED | FOREGROUND_INTENSITY);
break;
//DarkRed
case 6:SetConsoleTextAttribute(hOutput, FOREGROUND_RED);
break;
//Cyan
case 7:SetConsoleTextAttribute(hOutput, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
break;
//DarkCyan
case 8:SetConsoleTextAttribute(hOutput, FOREGROUND_GREEN | FOREGROUND_BLUE);
break;
//Green
case 9:SetConsoleTextAttribute(hOutput, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
break;
//DarkGreen
case 10:SetConsoleTextAttribute(hOutput, FOREGROUND_GREEN);
break;
//Blue
case 11:SetConsoleTextAttribute(hOutput,FOREGROUND_BLUE | FOREGROUND_INTENSITY);
break;
//DarkBlue
case 12:SetConsoleTextAttribute(hOutput,FOREGROUND_BLUE);
break;
//Yellow
case 13:SetConsoleTextAttribute(hOutput, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
break;
//Dark Yellow
case 14:SetConsoleTextAttribute(hOutput, FOREGROUND_RED | FOREGROUND_GREEN);
break;
//backgrounds-
//white on black
case 15:SetConsoleTextAttribute(hOutput, BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY);
break;
//gray
case 16:SetConsoleTextAttribute(hOutput,BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN);
break;
//Magenta
case 17:SetConsoleTextAttribute(hOutput, BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_INTENSITY);
break;
//Red
case 18:SetConsoleTextAttribute(hOutput, BACKGROUND_RED | BACKGROUND_INTENSITY);
break;
//Cyan
case 19:SetConsoleTextAttribute(hOutput, BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY);
break;
//Green
case 20:SetConsoleTextAttribute(hOutput, BACKGROUND_GREEN | BACKGROUND_INTENSITY);
break;
//Blue
case 21:SetConsoleTextAttribute(hOutput,BACKGROUND_BLUE | BACKGROUND_INTENSITY);
break;
//Yellow
case 22:SetConsoleTextAttribute(hOutput, BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_INTENSITY);
break;
//Dark Yellow
case 23:SetConsoleTextAttribute(hOutput, BACKGROUND_RED | BACKGROUND_GREEN);
break;
//Dark Magenta
case 24:SetConsoleTextAttribute(hOutput, BACKGROUND_RED | BACKGROUND_BLUE);
break;

//DarkRed
case 25:SetConsoleTextAttribute(hOutput, BACKGROUND_RED);
break;
//DarkCyan
case 26:SetConsoleTextAttribute(hOutput, BACKGROUND_GREEN | BACKGROUND_BLUE);
break;
//DarkGreen
case 27:SetConsoleTextAttribute(hOutput, BACKGROUND_GREEN);
break;
//DarkBlue
case 28:SetConsoleTextAttribute(hOutput,BACKGROUND_BLUE);
break;
//darkgreen back_lightgreen front
case 29:SetConsoleTextAttribute(hOutput,BACKGROUND_GREEN|FOREGROUND_GREEN|FOREGROUND_INTENSITY);
break;
//darkgreen front_lightgreen back
case 30:SetConsoleTextAttribute(hOutput,BACKGROUND_GREEN|FOREGROUND_GREEN|BACKGROUND_INTENSITY);
break;








//dark green background:
//foreground white
case 40:SetConsoleTextAttribute(hOutput,BACKGROUND_GREEN|FOREGROUND_GREEN| FOREGROUND_BLUE |FOREGROUND_RED | FOREGROUND_INTENSITY );
break;
//foreground gray
case 41:SetConsoleTextAttribute(hOutput,BACKGROUND_GREEN|FOREGROUND_GREEN| FOREGROUND_BLUE |FOREGROUND_RED);
break;

//Magenta
case 42:SetConsoleTextAttribute(hOutput, BACKGROUND_GREEN|FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
break;
//dark Magenta
case 43:SetConsoleTextAttribute(hOutput, BACKGROUND_GREEN|FOREGROUND_RED | FOREGROUND_BLUE);
break;
//Red
case 44:SetConsoleTextAttribute(hOutput, BACKGROUND_GREEN|FOREGROUND_RED | FOREGROUND_INTENSITY);
break;
//dark Red
case 45:SetConsoleTextAttribute(hOutput, BACKGROUND_GREEN|FOREGROUND_RED);
break;
//Cyan
case 46:SetConsoleTextAttribute(hOutput, BACKGROUND_GREEN|FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
break;
//dark Cyan
case 47:SetConsoleTextAttribute(hOutput, BACKGROUND_GREEN|FOREGROUND_GREEN | FOREGROUND_BLUE);
break;
//black
case 48:SetConsoleTextAttribute(hOutput, BACKGROUND_GREEN);
break;
//dark gray?
case 49:SetConsoleTextAttribute(hOutput, BACKGROUND_GREEN | FOREGROUND_INTENSITY);
break;
//Blue
case 50:SetConsoleTextAttribute(hOutput,BACKGROUND_GREEN|FOREGROUND_BLUE | FOREGROUND_INTENSITY);
break;
//dark Blue
case 51:SetConsoleTextAttribute(hOutput,BACKGROUND_GREEN|FOREGROUND_BLUE);
break;
//Yellow
case 52:SetConsoleTextAttribute(hOutput, BACKGROUND_GREEN|FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
break;
//dark Yellow
case 53:SetConsoleTextAttribute(hOutput, BACKGROUND_GREEN|FOREGROUND_RED | FOREGROUND_GREEN);
break;

//white background:
//white background black words
case 60:SetConsoleTextAttribute(hOutput, BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY);
break;
//dark gray words
case 61:SetConsoleTextAttribute(hOutput, BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY| FOREGROUND_INTENSITY);
break;
//gray words
case 62:SetConsoleTextAttribute(hOutput, BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY| FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN);
break;
//Magenta
case 63:SetConsoleTextAttribute(hOutput, BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY|FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
break;
//dark Magenta
case 64:SetConsoleTextAttribute(hOutput, BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY|FOREGROUND_RED | FOREGROUND_BLUE);
break;
//Red
case 65:SetConsoleTextAttribute(hOutput, BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY|FOREGROUND_RED | FOREGROUND_INTENSITY);
break;
//dark Red
case 66:SetConsoleTextAttribute(hOutput, BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY|FOREGROUND_RED);
break;
//Cyan
case 67:SetConsoleTextAttribute(hOutput, BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY|FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
break;
//dark Cyan
case 68:SetConsoleTextAttribute(hOutput, BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY|FOREGROUND_GREEN | FOREGROUND_BLUE);
break;
//Blue
case 69:SetConsoleTextAttribute(hOutput,BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY|FOREGROUND_BLUE | FOREGROUND_INTENSITY);
break;
//dark Blue
case 70:SetConsoleTextAttribute(hOutput,BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY|FOREGROUND_BLUE);
break;
//Yellow
case 71:SetConsoleTextAttribute(hOutput, BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY|FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
break;
//dark Yellow
case 72:SetConsoleTextAttribute(hOutput, BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY|FOREGROUND_RED | FOREGROUND_GREEN);
break;





}
return;
}




>>>START MAIN_CPP<<<



#include <iostream>

#include <windows.h>
#include "control.h"





int main()
{

HANDLE hInp;
hInp = GetStdHandle(STD_INPUT_HANDLE);

SetConsoleMode(hInp, ENABLE_PROCESSED_INPUT);


//this is the player input capture
int key=0;

//this decides if we should quit or not
int quit=0;

//this _really_ decides if we should quit or not
char close;



//control class controls and initializes everything else
CONTROL C;


//generate a blank map
C.generateBlankMap();

//create a player at 5,5
C.generatePlayer(5,5);

//this used to generate 11 items
//C.generateItems(11,0,0,0,0);

//this initializes the screen
C.scrInt();

//this displays the current screen, starting at 0,0 most likely

C.dsp(C.curPlay[0].topX,C.curPlay[0].topY);



while(quit != 9)
{


//this updates the screen every turn, every tile that needs to be updated.

C.upd(C.curPlay[0].topX,C.curPlay[0].topY);


key=std::cin.get();
if(C.captureInput(key) == 9)
{
quit = 9;
}
}



std::cin >> close;
return 0;
}




>>>START CONTROL_H<<<


#ifndef CONTROL_H
#define CONTROL_H
#include "universe.h"
#include "display.h"

class DISPLAY;
class UNI;
class TILE;
#define MAPWIDTH 120
#define MAPHEIGHT 120


class CONTROL
{
public:

//this initializes everything
UNI u;
DISPLAY disp;
PLAYER curPlay[1];

//i only used to have 11 measly items :(
//OBJECT items[11];

TILE mapA[MAPWIDTH][MAPHEIGHT];






//all the functions for everything! yay!

//generates a blank map
void generateBlankMap();


//the old display function. also displays the entire map at once
void display();

//intializes the screen to outputwidth X outputheight + 10
void scrInt();

//initializes a maptile to a certain type
void reset(int x, int y, int type);

//this is the new display function - works correctly
void dsp(int topX,int topY);

//generates a player, at x, y coordinates
void generatePlayer(int x, int y);

//this moves a player from curX, curY to destX, destY - I am going to remove topX & topY
//I solved that problem in another manner

void moveP(int curX, int curY, int destX, int destY, int topX, int topY);

//changes which direction the player is facing
void pFace(int direction); // 1 sw, 2 s, 3 se, 6 e, 9 ne, 8 n, 7 nw, 4 w

//this captures whatever key the player inputs - make sure to change to:

// HANDLE hInp;
// hInp = GetStdHandle(STD_INPUT_HANDLE);
// SetConsoleMode(hInp, ENABLE_PROCESSED_INPUT);


//in main()
//or else they will have to press enter after each key
int captureInput(int key);

//this used to generate number of items in between topX - botX and topY - botY
void generateItems(int number, int topX, int topY, int botX, int botY);

//this updates the screen, with the top left corner at topX, topY
void upd(int topX,int topY);

};

#endif




>>>START DISPLAY_H<<<


//this is from my old display class
//I do not use any functions except for
//scrSize(int z, int d);
//At(int x, int y,unsigned char What);
//Ater(int x, int y,unsigned int What);
//color(int colorNumber);


#ifndef DISPLAY_H
#define DISPLAY_H

#define MAPWIDTH 120
#define MAPHEIGHT 120

class DISPLAY
{




public:
//sets to full screen
void SetFullScreen();
//sets screen size to z by d
void scrSize(int z, int d);

//outputs What(char) at x, y coordinates on screen
void At(int x, int y,unsigned char What);
//outputs What(int) at x, y coordinates on screen
void Ater(int x, int y,unsigned int What);


//changes screen output to color #
void color(int colorNumber);// 0-14fore##15-30back##greenBG:40-53##whiteBG:60-72



};


#endif





>>>START INV_H<<<



#ifndef INV_H
#define INV_H


class OBJECT;
class PLAYER;


class INV
{

public:



//this works and compiles fine as:
//OBJECT* inventory[5];
//but if you put it to
//OBJECT* inventory[30];
//it has a stack overflow when the program starts to compile the first lines of the main() function

//OBJECT* inventory[5];


PLAYER* invnt[1];



};


#endif


>>>START MAPCOORD_H<<<

#ifndef MAPCOORD_H
#define MAPCOORD_H


class MAPCOORD
{
//these are the coordinates that everything is at.
public:
int x;
int y;

};

#endif




>>>START OBJECT_H<<<



#ifndef OBJECT_H
#define OBJECT_H
#include "mapcoord.h"
#include "inv.h"
#include <string>


//objects!
class OBJECT
{

public:
MAPCOORD mapCoord;
INV i;

//std::string name;
//std::string desc;

char output; // output character 'o' or whatever
int color; // 0 white
int type; // type of object - 0 for terrain - 1 for player 2 for item - 9 for automatic exit square
int location; // 0 for mapcell, 1 for players inventory
bool opaque; // true and you cant see through this sucka(even though it always sounds to me
// like something that is opaque you can see through)
bool seen; // if this is seen or not
bool beenSeen; // if this thing has been seen before then it is true! it is gray and not updated until seen again
bool hasAnItem; // if this has something inside of it
bool hasAnActor; // if this has an actor in it
bool walkable; //you can walk on erything!! hahahah!!! muahaha he
int item_count;


//bool update;
//int IDnum;




};

#endif



>>>START PLAYER_H<<<



#ifndef PLAYER_H
#define PLAYER_H
#include "object.h"


class PLAYER
{

public:
OBJECT obj;

int facing;
int topX;
int topY;

};


#endif


>>>START TILE_H<<<



#ifndef TILE_H
#define TILE_H
#include "object.h"

class TILE
{
public:

OBJECT obj;


//this is what determines if a tile should be redrawn or not
bool update;
//



};


>>>START UNIVERSE_H<<<


#ifndef UNIVERSE_H
#define UNIVERSE_H
#include "player.h"
#include "tile.h"


class UNI
{

public:

};






]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this