Giant Char or multiple Chars for console game map? - c++

I'm a pretty novice c++ coder and I am starting to make a console adventure game.
My adventure game currently consists of a player character that walks around inside a console application window with an 80 character width x 40 lines.
I am not sure how to approach storing the maps for my game. Each map will consist of 80 x 40 ASCII characters with colour attributes.
Should I store each 80 x 40 map in its own char? so a single map would look something like...
int cHeight = 5; // Reduced size for this example
int cHeight = 10; // Reduced size for this example
// Set up the characters:
char map[cHeight][cWidth+1] = {
"1234567890",
"1234567890",
"1234567890",
"1234567890",
"1234567890",
};
CHAR_INFO mapA[cWidth * cHeight];
for (int y = 0; y < cHeight; ++y) {
for (int x = 0; x < cWidth; ++x) {
mapA[x + cWidth * y].Char.AsciiChar = map[y][x];
mapA[x + cWidth * y].Attributes = FOREGROUND_BLUE | Black; //I have an enum setup with background colours.
}
}
// Set up the positions:
COORD charBufSize = {cWidth,cHeight};
COORD characterPos = {0,0};
SMALL_RECT writeArea = {0,0,cWidth-1,cHeight-1};
// Write the characters:
WriteConsoleOutputA(wHnd, mapA, charBufSize, characterPos, &writeArea);
Im not sure if this is entirely the correct way to display the characters but I didn't think it was a good idea to just cout every character in the for loop.
So.. lets say my console window (in the above code) is 10 characters wide and 5 lines high.
In the above code I have a single map in the Char, so when loading each map I would put each one in their own array.
I was thinking of putting the entire map into a single Char, but then only displaying what I needed by offsetting the x and y in the for loop.
mapA[x + cWidth * y].Char.AsciiChar = map[y+offset][x+offset];
So the map would look more like this;
char map[cHeight][cWidth+1] = {
"1234567890ABCDEFGHIJ",
"1234567890ABCDEFGHIJ",
"1234567890ABCDEFGHIJ",
"1234567890ABCDEFGHIJ",
"1234567890ABCDEFGHIJ",
};
with the offset I could display '1234567890' on 5 rows separately from 'ABCDEFGHIJ' on 5 rows.
So in short I would like to know the most effective way to do this, should I have multiple Chars? Should I create a class? then I could store the characters an colours? (class' are still new to me in c++).
Should I draw the terrain only in the map and then add objects (houses, trees)?
Or just draw it all in the map manually?
I think I've just thought about this too long and need a bit of direction
Thanks!

The way I would do it would be to create a map of
Node* map[height][width]
This means you would create the map which are pointers to Node* elements and you could define the Node* element to be...
class Node{
char displayCharacter;
int posx,poxy
unsigned int r; //red
unsigned int g; //green
unsigned int b; //blue
unsigned int a; //alpha
display(); // This function will know how to display a node using the colour etc
};
Then you could for example if you wanted to create a house you would give it the center point of the model etc... to draw to a function
void createHouse(Node* center)
{
if((center->posh > 0)&&(center->posh< maxheight))
{
if(map[center->posy-1][center->posx]!=NULL)
{
map[center->posy-1][center->posx]->displayCharacter = '_';
map[center->posy-1][center->posx]->r = 255;
}
}
}
Then in main you would have something like...
while(true)
{
for(int i=0; i<maxheight; i++)
{
for(int j=0; j< maxwidth; j++)
{
map[i][j]->Display();
}
}
}
I hope all this sample code is of help to you and answered your question. I have not debugged or looked for any syntax errors. If there any errors in the code, you will have to fix them!
Good luck to you!

Related

My code is printing strange characters instead of what it is meant to print

I'm trying to make a simple console game, and I am making a sort of console graphics engine that draws a screen with a map and some text. For some reason the engine writes strange characters to the console, rather than what is meant to be written.
This engine takes two 2d vectors that describe the characters and colors to be used in the console. I am using WriteConsole() to write to the console, and using SetConsoleTextAttribute() to change the color of the text as I draw. For some reason, when I try to print text, it writes some really weird characters that have no relation to the characters that are meant to be printed. Colors work just fine though. My characters are stored as TCHARs and my colors as ints.
My function to actually draw the screen:
void update()
{
for (int y = 0; y < SCREEN_HEIGHT; y++) //loop through all of the tiles
{
for (int x = 0; x < SCREEN_WIDTH; x++)
{
if (screen.at(y).at(x) != buffer.at(y).at(x) && screenColors.at(y).at(x) != bufferColors.at(y).at(x)) //only draw the tile if it has changed
{
pos.X = x; //set coords of cursor to tile to be drawn
pos.Y = y;
SetConsoleTextAttribute(hStdOut, bufferColors.at(y).at(x)); //set the text color
SetConsoleCursorPosition(hStdOut, pos); //move the cursor to the tile to be drawn
WriteConsole(hStdOut, &(buffer.at(y).at(x)), 1, dw, NULL); //actually draw the tile
screen.at(y).at(x) = buffer.at(y).at(x); //update 2d screen vector (used to read what is on the screen for other reasons)
}
}
}
SetConsoleCursorPosition(hStdOut, restPos); //move the cursor away, so it doesn't look ugly
}
My function to write to the buffer vector:
void draw2dVector(int x, int y, vector<vector<TCHAR>> draw, vector<vector<int>> colors)
{
for (unsigned int drawY = 0; drawY < draw.size(); drawY++)
{
for (unsigned int drawX = 0; drawX < draw.front().size(); drawX++)
{
buffer.at(y + drawY).at(x + drawX) = colors.at(drawY).at(drawX); // <- I found the problem. I am writing color ints to the buffer.
bufferColors.at(y + drawY).at(x + drawX) = colors.at(drawY).at(drawX);
}
}
}
My function to convert strings to vector<TCHAR>s.
vector<TCHAR> stringToTCHARvector(string str, int strLen) //convert a TCHAR string to a TCHAR vector
{
vector<TCHAR> result;
for (int i = 0; i < strLen; i++) //loop through the characters in the string
{
result.push_back(str[i]); //add the character to the TCHAR vector
}
return result;
}
I expect the output to look something like this:
###....### Inventory:
####....## Gold Piece
#..##...##
#......###
###.#..###
##########
But instead it gives me this:
êêêêêêêêêê *insert strange characters here, because stack overflow doesn't
êêêêêwwêêê show them*
êêêêwwwwww
êêêêwwwwww
êêêêêwwwww
êêêêêwwwww
UPDATE:
It turns out that the problem was that I was writing the ints from my color vector to my TCHAR buffer. I have resolved the issue. Thank you for your help.

C++ code for Microsoft Kinect - trying to dynamically allocate array of target positions

So I'm trying to modify the Kinect BodyBasicsD2D code so that a fixed number of "target positions" appear on the screen (as ellipses) for the user to move his hand toward. I'm having trouble creating the initial target positions.
This is my code in the header file for the allocation of the array of target positions (these are a public field of the CBodyBasics class, already built into the original BodyToBasics program):
D2D1_POINT_2F* targetPositions = NULL;
int numTargets = 3;
Then I have a function "GenerateTargetPositions" which is supposed to generate 3, in this case, target positions to be passed into the "DrawTargetPositions" function.
void CBodyBasics::GenerateTargetPositions(D2D1_POINT_2F * targetPositions, int numTargets)
{
targetPositions = new D2D1_POINT_2F[numTargets];
RECT rct;
GetClientRect(GetDlgItem(m_hWnd, IDC_VIDEOVIEW), &rct);
int width = rct.right;
int height = rct.bottom;
FLOAT x;
FLOAT y;
D2D1_POINT_2F tempPoint;
for (int i = 0; i < numTargets; i++) {
x = 1.0f*i*width / numTargets;
y = 1.0f*i*height / numTargets;
tempPoint = D2D1::Point2F(x, y);
targetPositions[i] = tempPoint;
}
}
My DrawTargetPositions function is:
void CBodyBasics::DrawTargetPositions(D2D1_POINT_2F * targetPositions, int numTargets)
{
D2D1_ELLIPSE ellipse;
for (int i = 0; i < numTargets; i++)
{
ellipse = D2D1::Ellipse(targetPositions[i], 50.f, 50.f);
m_pRenderTarget->FillEllipse(ellipse, m_pSilverBrush);
}
}
When I try to run my code, I get the error that both "targetPositions" and "targetPositions[i]" is NULL (and thus my GenerateTargetPositions function must not be working properly). I believe that targetPositions[i] is a struct (a point with x and y values) so I am wondering if this may be the reason for my errors.
I call GenerateTargetPositions and DrawTargetPositions before the main "while" loop in my code so that each function is not being called on each iteration (there are many iterations of through the while loop because this is an interactive Microsoft Kinect, recording one's movements).
Any suggestions and advice would be greatly appreciated. Thanks so much!

pad print line with white space C++

In C++ I am using unsigned char pointers to hold byte arrays so that I can fit 8 bit color codes in each element for a print line.
I have one array holding data, and one array holding white space, and I am using for loops to populate a third array so that the data is at the beginning and white space is at the end.
When the pointer is created, as I monitor memory at runtime, all elements have a default value of 0xCD, which is magenta on color chart. I use a for loop to populate the bytes I want with 0x00, but it will not write over the default array value of the third array.
So, I am stuck with my printer printing magenta instead of white space. Yet I can write over that array just fine with just the data. But not with the whitespace. Im unsure what is the reason for that. Can anyone give me any insight? Here is my code...
PrintLine(unsigned char* pbData, unsigned long ulDataSize, UINT xoffset)
{
if (xoffset > 0)
{ //create pointer to byte array for xoffset
unsigned char* offsetData;
offsetData = new unsigned char[(xoffset / 8)]; //x offset is divided by 8
//to convert pixels to bytes
//create pointer to byte array to hold image data and offset data
unsigned char* finalData;
finalData = new unsigned char[ulDataSize + (xoffset / 8)];
//begin final data with image data passed into the function
for (int count = 0; count < ulDataSize; count++)
{
finalData[count] = pbData[count];
}
//populate offset data with blank bytes
for (int count = 0; count < (xoffset / 8); count++)
{
offsetData[count] = 0x00;
}
//add blank data for offset to finalData
int position = 0;
for (int count = ulDataSize; count < ulDataSize + (xoffset / 8);count++)
{
finalData[ulDataSize] = offsetData[position];//also tried =0x00
position++;
}
//Send data to printer.
if (!(Write(finalData, ulDataSize + (xoffset / 8)))
{
return FALSE;
}
return TRUE;
}
}
At first glance your code don't have errors, but I see something that looks suspicious. I'm talking about the line:
finalData[ulDataSize] = offsetData[position]; //also tried =0x00
I think what you want is:
finalData[count] = offsetData[position];//also tried =0x00
On the other hand you could write your loop like this:
for (int count = 0; count < (xoffset / 8); count++)
{
finalData[ulDataSize + count] = offsetData[count];
}
Making the code much more readable.

Can someone explain how I am to access this array? (image processing program)

I am working on the implementation of functions for an already written image processing program. I am given explanations of functions, but not sure how they are designating pixels of the image.
In this case, I need to flip the image horizontally, i.e., rotates 180 degrees around the vertical axis
Is this what makes the "image" i am to flip?
void Image::createImage(int width_x, int height_y)
{
width = width_x;
height = height_y;
if (pixelData!=NULL)
freePixelData();
if (width <= 0 || height <= 0) {
return;
}
pixelData = new Color* [width]; // array of Pixel*
for (int x = 0; x < width; x++) {
pixelData[x] = new Color [height]; // this is 2nd dimension of pixelData
}
}
I do not know if all the functions I have written are correct.
Also, the Image class calls on a Color class
So to re-ask: what am I "flipping" here?
Prototype for function is:
void flipLeftRight();
As there is no input into the function, and I am told it modifies pixelData, how do I flip left to right?
A quick in place flip. Untested, but the idea is there.
void flipHorizontal(u8 *image, u32 width, u32 height)
{
for(int i=0; i < height; i++)
{
for(int j=0; j < width/2; j++)
{
int sourceIndex = i * width + j;
int destIndex = (i+1) * width - j - 1;
image[sourceIndex] ^= image[destIndex];
image[destIndex] ^= image[sourceIndex];
image[sourceIndex] ^= image[destIndex];
}
}
}
well, the simplest approach would be to read it 1 row at a time into a temporary buffer the same size as 1 row.
Then you could use something like std::reverse on the temporary buffer and write it back.
You could also do it in place, but this is the simplest approach.
EDIT: what i;ve described is a mirror, not a flip, to mirror you also need to reverse the order of the rows. Nothing too bad, to do that I would create a buffer the same size as the image, copy the image and then write it back with the coordinates adjusted. Something like y = height - x and x = width - x.

C++ vector element is different when accessed at different times

I'm developing a 3D game using SDL and OpenGL on Ubuntu 9.04 using Eclipse CDT. I've got a class to hold the mesh data in vectors for each type. Such as Vertex, Normal, UVcoord (texture coordinates), as well as a vector of faces. Each face has 3 int vectors which hold indexes to the other data. So far my game has been working quite well at rendering at nice rates. But then again I only had less then one hundred vertexes among two objects for testing purposes.
The loop accessing this data looks like this:
void RenderFace(oFace face)
{
/*
* More Stuff
*/
oVertice gvert;
oUVcoord tvert;
oNormal nvert;
for (unsigned int fvIndex = 0; fvIndex < face.GeoVerts.size(); fvIndex++)
{
gvert = obj.TheMesh.GetVertice(face.GeoVerts[fvIndex] - 1);
tvert = obj.TheMesh.GetUVcoord(face.UV_Verts[fvIndex] - 1);
nvert = obj.TheMesh.GetNormal(face.NrmVerts[fvIndex] - 1);
glNormal3f(nvert.X, nvert.Y, nvert.Z);
glTexCoord2f(tvert.U, tvert.V);
glVertex3f(scale * gvert.X, scale * gvert.Y, scale * gvert.Z);
}
/*
* More Stuff
*/
}
There is a loop that calls the renderFace() function which includes the above for loop. The minus one is because Wavefront .obj files are 1 indexed (instead of c++ 0 index). Anyway, I discovered that once you have about 30 thousand or so faces, all those calls to glVertex3f() and the like slow the game down to about 10 FPS. That I can't allow. So I learned about vertex arrays, which require pointers to arrays. Following the example of a NeHe tutorial I continued to use my oVertice class and the others. Which just have floats x, y, z, or u, v. So I added the same function above to my OnLoad() function to build the arrays which are just "oVertice*" and similar.
Here is the code:
bool oEntity::OnLoad(std::string FileName)
{
if (!obj.OnLoad(FileName))
{
return false;
}
unsigned int flsize = obj.TheMesh.GetFaceListSize();
obj.TheMesh.VertListPointer = new oVertice[flsize];
obj.TheMesh.UVlistPointer = new oUVcoord[flsize];
obj.TheMesh.NormListPointer = new oNormal[flsize];
oFace face = obj.TheMesh.GetFace(0);
oVertice gvert;
oUVcoord tvert;
oNormal nvert;
unsigned int counter = 0;
unsigned int temp = 0;
for (unsigned int flIndex = 0; flIndex < obj.TheMesh.GetFaceListSize(); flIndex++)
{
face = obj.TheMesh.GetFace(flIndex);
for (unsigned int fvIndex = 0; fvIndex < face.GeoVerts.size(); fvIndex++)
{
temp = face.GeoVerts[fvIndex];
gvert = obj.TheMesh.GetVertice(face.GeoVerts[fvIndex] - 1);
temp = face.UV_Verts[fvIndex];
tvert = obj.TheMesh.GetUVcoord(face.UV_Verts[fvIndex] - 1);
temp = face.NrmVerts[fvIndex];
nvert = obj.TheMesh.GetNormal(face.NrmVerts[fvIndex] - 1);
obj.TheMesh.VertListPointer[counter].X = gvert.X;
obj.TheMesh.VertListPointer[counter].Y = gvert.Y;
obj.TheMesh.VertListPointer[counter].Z = gvert.Z;
obj.TheMesh.UVlistPointer[counter].U = tvert.U;
obj.TheMesh.UVlistPointer[counter].V = tvert.V;
obj.TheMesh.NormListPointer[counter].X = nvert.X;
obj.TheMesh.NormListPointer[counter].Y = nvert.Y;
obj.TheMesh.NormListPointer[counter].Z = nvert.Z;
counter++;
}
}
return true;
}
The unsigned int temp variable is for debugging purposes. Apparently I don't have a default constructor for oFace that doesn't have something to initialize with. Anyway, as you can see it's pretty much that same exact routine. Only instead of calling a gl function I add the data to three arrays.
Here's the kicker:
I'm loading a typical cube made of triangles.
When I access element 16 (0 indexed) of the UV_Verts vector from the RenderFace() function I get 12.
But when I access element 16 (0 indexed) of the same UV_Verts vector from the OnLoad() function I get something like 3045472189
I am so confused.
Does anyone know what's causing this? And if so how to resolve it?
One possible reason could be that you're creating arrays with size flsize:
obj.TheMesh.VertListPointer = new oVertice[flsize];
obj.TheMesh.UVlistPointer = new oUVcoord[flsize];
obj.TheMesh.NormListPointer = new oNormal[flsize];
but use the arrays with indices up to flsize * face.GeoVerts.size
for (...; flIndex < obj.TheMesh.GetFaceListSize(); ...) { // flsize = GetFaceListSize
for (...; fvIndex < face.GeoVerts.size(); ...) {
...
obj.TheMesh.UVlistPointer[counter].U = ...;
...
counter++;
}
}
so your array creation code should actually be more like
obj.TheMesh.VertListPointer = new oVertice[flsize * face.GeoVerts.size()];
obj.TheMesh.UVlistPointer = new oUVcoord[flsize * face.GeoVerts.size()];
obj.TheMesh.NormListPointer = new oNormal[flsize * face.GeoVerts.size()];