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

Author Topic: Problem with TileMap  (Read 1891 times)

0 Members and 1 Guest are viewing this topic.

Ptlomej

  • Newbie
  • *
  • Posts: 48
    • ICQ Messenger - 353167418
    • View Profile
    • Local
Problem with TileMap
« on: October 24, 2011, 06:10:28 pm »
Hey,
I had a class for each Tile and a Vector for all Tiles.
Here are the code.
The Problem is that i get white Objects ?

Code: [Select]
#pragma once
#include <string>
#include <SFML\Graphics.hpp>

class tile {
public:
tile(std::string Path, float X, float Y, float Rotation, bool bCollision);
~tile();
public:
float X, Y, Rotation;
bool Collision;
sf::Sprite sTile;
sf::Image iTile;
};


Code: [Select]
#include "tile.h"

tile::tile(std::string Path, float X, float Y, float Rotation, bool bCollision){
iTile.LoadFromFile(Path);
iTile.SetSmooth(false);
sTile.SetImage(iTile);
sTile.SetRotation(Rotation);
sTile.SetPosition(X,Y);
sTile.SetCenter(16,16);
Collision = bCollision;
}
tile::~tile(){

}


Code: [Select]
std::vector<tile> Map;
//LoadMap
char *FileMap;
std::string CurLine;
int iCurLine = 0;
ifstream file("./data/map1.bsm");
while(getline(file, CurLine)){
iCurLine++;
FileMap = strtok(const_cast<char*>(CurLine.c_str()),"|");
int BuildUp = 0;
string Type;
float PosX, PosY, Angle;
bool Colide;
while (FileMap != NULL){
if(BuildUp == 0){
if(toStr(FileMap) == "sand"){
Type = "./data/sand.png";
}else if(toStr(FileMap) == "wall"){
Type = "./data/wall.png";
}else if(toStr(FileMap) == "walledge"){
Type = "./data/walledge.png";
}
}else if(BuildUp == 1){
PosX = atof(FileMap);
}else if(BuildUp == 2){
PosY = atof(FileMap);
}else if(BuildUp == 3){
Angle = atof(FileMap);
}else if(BuildUp == 4){
if(toStr(FileMap) == "true"){
Colide = true;
}else{
Colide = false;
}
}
BuildUp++;
FileMap = strtok (NULL, "|");
}
tile TempTile123(Type,PosX,PosY,Angle,Colide);
Map.push_back(TempTile123);
}


And the map1.bsm
Code: [Select]
sand|32.0f|32.0f|0.0f|false
wall|200.0f|200.0f|45.0f|true
sand|32.0f|32.0f|0.0f|false

asdatapel

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Problem with TileMap
« Reply #1 on: October 25, 2011, 08:08:19 pm »
When you use a Tile inside a vector, it makes a copy of the Tile, and the location of the Image in memory is changed. After you push_back, you need to reassign the Image to the Sprite.
For example add a function to the Tile class:
Code: [Select]

class tile {
public:
   tile(std::string Path, float X, float Y, float Rotation, bool bCollision);
   ~tile();
//right here
   void reAssign(){
sTile.SetImage(iTile);
}
public:
   float X, Y, Rotation;
   bool Collision;
   sf::Sprite sTile;
   sf::Image iTile;
};

Then after
Code: [Select]
Map.push_back(TempTile123);
add
Code: [Select]

Map[(the one you just put in)].reAssign;

 

anything