Map generation-reading from a file

Started by
16 comments, last by 15Peter20 16 years, 1 month ago
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 <TL-Engine.h>	// TL-Engine include file and namespace
using namespace tle;
#include <fstream> // 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->StartWindowed();

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

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


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


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

void FrontEndUpdate()
{
	// Draw the scene
	myEngine->DrawScene();
	cube->RotateX(0.5);
	myFont->Draw("Press 'L' to load a map", 0, 0, kWhite);
	myFont->Draw("Press 'S' to start", 0, 20, kWhite);
	myFont->Draw("Map Generation!!", 225, 200, kRed);
	if(myEngine->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<20;x++)
		{
			for(int y=0;y<20;y++)
			{
				InFile>> MapArray[x][y];
			}
		}
		InFile.close();
	}
	if(InFile.fail())
	{
		myFont->Draw("MAP FAILED",0,60,kBlack);
	}
}

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


void GameSetup()
{
	myFont = myEngine->LoadFont("Font1.bmp");
	cubeMesh = myEngine->LoadMesh("cube.x");
	
	for (int i=0;i<20;i++)
	{
		for (int j=0;j<20;j++)
		{			
			if(MapArray[0][0]==0)
			{ 
				cube = cubeMesh->CreateModel(i,j);
				cube->Scale(0.05);								
			}
		}		
	}	
}

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

}

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

[Edited by - 15Peter20 on February 20, 2008 5:29:18 PM]
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.)
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.
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
http://www.cppreference.com/index.html
http://www.cplusplus.com/doc/

Practically all you need to know about STL classes.
Quote:Original post by ChJees
Original 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' :)
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.
Quote:Original post by jyk
Now, 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.
Quote:Original post by ChJees
But 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]
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 :)

This topic is closed to new replies.

Advertisement