Jump to content
• Advertisement

Public Group

# Map generation-reading from a file

This topic is 3749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

hi guys, im new to GDC after my friend highly recommended u guys for my help. im at uni studying comp games dev, and im currently on an assingment implementing AI on pathfinding. now the juicy bit. im currently stuck on a section where i have to read a file which contains 0,1,2 and 3's which make up a small 20x20 map. 0 are impassable walls, 1 is walkable ground, 2 are trees and the 3's are water terrain. the problem i ahve been having is that i am trying to read this file and ut all the numbers into a 2D char array in my front end update section, and then using those stored numbers in the array, create cubes that represent the corresponding numbers. however i have had problems jsut trying to read the file, and after hours or staring at this code, trying all sorts of diferent ways around it and even my friend coming over to have a crack we cant see the problem, at first i thought it was the original map read but i dont think it is. i have posted my code here for it all sry if its a mess, but its not quite complete can any1 see where im going wrong at all?? any help would be wonderous!!!!!!!!! x
// MapReading.cpp: A program using the TL-Engine

#include &lt;TL-Engine.h&gt;	// TL-Engine include file and namespace
using namespace tle;
#include &lt;fstream&gt; // include the file stream functions

//Global variables
I3DEngine* myEngine;
ICamera* myCamera;
IMesh* cubeMesh;
IModel* cube;
IFont* myFont;

char MapArray[20][20];
ifstream InFile;
#define MEDIA_FOLDER = "Media\\";

void FrontEndSetup();
void FrontEndUpdate();
void FrontEndShutdown();

void GameSetup();
void GameUpdate();
void GameShutdown();

void main()
{
// Create a 3D engine (using Irrlicht engine here) and open a window for it
myEngine = New3DEngine( kIrrlicht );
myEngine-&gt;StartWindowed();

// Add default folder for meshes and other media
myEngine-&gt;AddMediaFolder( "C:\\Documents and Settings\\Sean P Jones\\My Documents\\TL-Engine\\Media" );

FrontEndSetup();
// The front-end loop, repeat until stopped
while (myEngine-&gt;IsRunning())
{
FrontEndUpdate();
if(myEngine-&gt;KeyHit(Key_S))
{
break;
}
}
FrontEndShutdown();

GameSetup();
// The main game loop, repeat untill exited
while (myEngine-&gt;IsRunning())
{
GameUpdate();
if(myEngine-&gt;KeyHit(Key_Escape))
{
break;
}
}
GameShutdown();
}

void FrontEndSetup()
{
cubeMesh = myEngine-&gt;LoadMesh("cube.x");
cube = cubeMesh-&gt;CreateModel(0,0,50);
myCamera = myEngine-&gt;CreateCamera(kManual);
myFont = myEngine-&gt;LoadFont("Font1.bmp");

}

void FrontEndUpdate()
{
// Draw the scene
myEngine-&gt;DrawScene();
cube-&gt;RotateX(0.5);
myFont-&gt;Draw("Press 'L' to load a map", 0, 0, kWhite);
myFont-&gt;Draw("Press 'S' to start", 0, 20, kWhite);
myFont-&gt;Draw("Map Generation!!", 225, 200, kRed);
if(myEngine-&gt;KeyHit(Key_L))
{
InFile.open("C:\\Documents and Settings\\Sean P Jones\\My Documents\\1Work\\Year2\\2301-GamesDev1\\MapReading\\Media\\map1.txt");
for(int x=0;x&lt;20;x++)
{
for(int y=0;y&lt;20;y++)
{
InFile&gt;&gt; MapArray[x][y];
}
}
InFile.close();
}
if(InFile.fail())
{
myFont-&gt;Draw("MAP FAILED",0,60,kBlack);
}
}

void FrontEndShutdown()
{
myEngine-&gt;RemoveFont(myFont);
cubeMesh-&gt;RemoveModel(cube);
myEngine-&gt;RemoveMesh(cubeMesh);
}

void GameSetup()
{
myFont = myEngine-&gt;LoadFont("Font1.bmp");
cubeMesh = myEngine-&gt;LoadMesh("cube.x");

for (int i=0;i&lt;20;i++)
{
for (int j=0;j&lt;20;j++)
{
if(MapArray[0][0]==0)
{
cube = cubeMesh-&gt;CreateModel(i,j);
cube-&gt;Scale(0.05);
}
}
}
}

void GameUpdate()
{
// Draw the scene
myEngine-&gt;DrawScene();
myFont-&gt;Draw("Escape to Quit", 0, 0, kWhite);

}

void GameShutdown()
{
myEngine-&gt;RemoveFont(myFont);
// Delete the 3D engine now we are finished with it
myEngine-&gt;Delete();
}


[Edited by - 15Peter20 on February 20, 2008 5:29:18 PM]

#### Share this post

##### Share on other sites
Advertisement
Original post by 15Peter20
char MapArray[20][20];

Don't do that, it is inefficent.

Use a STL container instead to loop through as a map. (Vector or List, your choice.)

#### Share this post

##### Share on other sites
Hi, and welcome to the forums :)

You can get your code to appear in a 'source code' box by enclosing it in [source] tags (just click the 'edit' button, and you can add these to your original post). In most cases that will preserve the code formatting and make it easier for us to read.

Also, once you've been around the forums a bit, you'll find that many of us appreciate (and value) clear communication skills (and that, in fact, these are important skills for a programmer to have in general). Clear, correct writing (e.g. correct capitalization and punctuation, and avoidance of abbreviated words such as 'any1') will make your posts easier to read and follow. That, in turn, will make more folks likely to read them, which means you'll have a better chance of getting the help you need.

Finally, there are some rules on the forum regarding helping with (and posting about) homework problems. I would go ahead and clean up your post though, since even if we can't give you the answer outright, we might be able at least to point you in the right direction.

#### Share this post

##### Share on other sites
o right, i didnt think of that, well i am jsut literally going through a tutorial now that is trying to teach me to use the STL, but its only covered deque at this time, but i think it goes onto vectors and list's, lists being the one im intrested in.

if u can give me a hand with a starting place i will be very appreciative. x

#### Share this post

##### Share on other sites
http://www.cppreference.com/index.html
http://www.cplusplus.com/doc/

Practically all you need to know about STL classes.

#### Share this post

##### Share on other sites
Quote:
 Original post by ChJeesOriginal post by 15Peter20*** Source Snippet Removed ***Don't do that, it is inefficent.Use a STL container instead to loop through as a map. (Vector or List, your choice.)
What do you mean by 'inefficient'?

You'll often see folks on this forum recommend alternatives to raw arrays, such as std::vector (which you alluded to above) or boost::multi_array. However, improved run-time efficiency (if that's what you're referring to) is usually not high on the list of reasons for recommending these alternatives.

Now, if you mean something else by 'efficient' - for example, using your development time wisely - then yes, using raw arrays is indeed 'inefficient' :)

#### Share this post

##### Share on other sites
sorry about my bad typing skills, I just think of the words, and my hands do the rest :)but i will make an effort.

didnt know about the source tags, again sorry, and i'll go voer the forum rules etc as soon as i can.

#### Share this post

##### Share on other sites
Quote:
 Original post by jykNow, if you mean something else by 'efficient' - for example, using your development time wisely - then yes, using raw arrays is indeed 'inefficient' :)

Mainly that, and i have a expandable array if i want to :P.

But i am not in the mood of argumenting and don't want to, so i will help the OP instead.

#### Share this post

##### Share on other sites
Quote:
 Original post by ChJeesBut i am not in the mood of argumenting and don't want to, so i will help the OP instead.
I'm not arguing :) But, I also have to point out that, just like you, I'm trying to help the OP (in this forum especially, 'helping the OP' often takes the form of questioning or attempting to clarify the posts of other respondents).

[Edited by - jyk on February 20, 2008 7:42:47 PM]

#### Share this post

##### Share on other sites
thanks again for the help people,

but can any-one spot the problem I was having with my code before? it has really been bugging me for me hours driving me crazy just can not find why!?

and as a reply to your ineeficent coding before, i know it is but it what i know at time of coding, and i intend to apadt it in future but right now i jsut want the crazy thing to work lol :)

#### Share this post

##### Share on other sites

• Advertisement

### Announcements

• Advertisement

• ### Popular Now

• 10
• 17
• 9
• 14
• 41
• Advertisement
• Advertisement
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!