SFML community forums

Help => Graphics => Topic started by: Fantasy on July 03, 2011, 10:49:15 pm

Title: Draw map from text file
Post by: Fantasy on July 03, 2011, 10:49:15 pm
hello everyone

so i'm trying to make a load map function and draw map function but i cant get it to work correctly, here is the code.

Code: [Select]

#include <SFML\Graphics.hpp>
#include <iostream>
#include <string>
#include <fstream>

int Map[100][100];
int loadCounterX=0;
int loadCounterY=0;

int mapSizeX;
int mapSizeY;

bool once=false;

void LoadMap (const char *filename)
{
std::ifstream openfile(filename);
if(openfile.is_open())
{
while(!openfile.eof())
{
if(once==false)
{
openfile>>mapSizeX>>mapSizeY;
once = true;
}
openfile >> Map[loadCounterX][loadCounterY];
loadCounterX++;
if(loadCounterX>=mapSizeX)
{
loadCounterX=0;
loadCounterY++;
}
}
}
}

void DrawMap()
{
for(int i=0; i<mapSizeX; i++)
for(int a=0; a<mapSizeY; a++)
Game.Draw(Map[i][a]);
}


int WindowWidth = 800, WindowHight = 600, ColorDepth = 32;
std::string WindowTitle = "The Final Game !!!";

sf::RenderWindow Game(sf::VideoMode(WindowWidth, WindowHight, ColorDepth), WindowTitle);

int main()
{
sf::Event Event;

const char *tiles[2];
tiles[1]=("Data/Images/grass.png");
tiles[2]=("Data/Images/dirt.png");

Game.SetFramerateLimit(60);

while(Game.IsOpened())
{
while(Game.GetEvent(Event))
{


}


Game.Clear();
Game.Display();
}

return 0;
}


text file
10 2
1111111111
0000000000

Title: Draw map from text file
Post by: WitchD0ctor on July 04, 2011, 02:15:43 am
You never call DrawMap
Title: Draw map from text file
Post by: Fantasy on July 04, 2011, 06:22:46 am
Quote from: "WitchD0ctor"
You never call DrawMap

well here is the problem i cant do Game.Draw(Map[a]) it gives me error
Title: Draw map from text file
Post by: Ceylo on July 04, 2011, 08:01:00 am
Were you expecting to draw an int?  :roll:
Title: Draw map from text file
Post by: JAssange on July 04, 2011, 03:21:31 pm
You can only pass a derivative of Drawable to the Draw function.
Title: Draw map from text file
Post by: Haikarainen on July 04, 2011, 04:50:31 pm
I think you're forgetting a whole lot here, a hint:
Learn how to use classes, then create classes for:
Map
Tile


The mapclass should have an std::vector<Tile> Tiles; from wich you can iterate when drawing and saving maps, checking collisions and so fort.

The tileclass should have reference to wich image it should draw, wich position it should be in etcetcetc.

These are just guidelines to create a "working" result, and i can promise you it can be done so much better if you really give your heart into learning the stuff you are working with, so you understand every bit of it. Or close at least.
Title: Draw map from text file
Post by: unranked86 on July 04, 2011, 05:46:47 pm
I had a similar problem, then after a few minutes of searching I found this (http://www.dreamincode.net/forums/topic/230524-c-tile-engine-from-scratch-part-1/). Hope it helps.
But be aware, that code there is a bit flawed. Read the comments. Especially Part 4, otherwise you'll end up with some nasty memory leaks.
Title: Draw map from text file
Post by: Fantasy on July 04, 2011, 09:19:59 pm
Quote from: "unranked86"
I had a similar problem, then after a few minutes of searching I found this (http://www.dreamincode.net/forums/topic/230524-c-tile-engine-from-scratch-part-1/). Hope it helps.
But be aware, that code there is a bit flawed. Read the comments. Especially Part 4, otherwise you'll end up with some nasty memory leaks.


thank you so much you have no idea how much this article helped me, thank you.

thankx guys