Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Draw map from text file  (Read 4816 times)

0 Members and 1 Guest are viewing this topic.

Fantasy

  • Newbie
  • *
  • Posts: 47
    • View Profile
    • Email
Draw map from text file
« 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


WitchD0ctor

  • Full Member
  • ***
  • Posts: 100
    • View Profile
    • http://www.teleforce-blogspot.com
Draw map from text file
« Reply #1 on: July 04, 2011, 02:15:43 am »
You never call DrawMap
John Carmack can Divide by zer0.

Fantasy

  • Newbie
  • *
  • Posts: 47
    • View Profile
    • Email
Draw map from text file
« Reply #2 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

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Draw map from text file
« Reply #3 on: July 04, 2011, 08:01:00 am »
Were you expecting to draw an int?  :roll:
Want to play movies in your SFML application? Check out sfeMovie!

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Draw map from text file
« Reply #4 on: July 04, 2011, 03:21:31 pm »
You can only pass a derivative of Drawable to the Draw function.

Haikarainen

  • Guest
Draw map from text file
« Reply #5 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.

unranked86

  • Newbie
  • *
  • Posts: 37
    • View Profile
Draw map from text file
« Reply #6 on: July 04, 2011, 05:46:47 pm »
I had a similar problem, then after a few minutes of searching I found this. 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.

Fantasy

  • Newbie
  • *
  • Posts: 47
    • View Profile
    • Email
Draw map from text file
« Reply #7 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. 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

 

anything