So I've been working on setting up a tile system for a block-pushing puzzle game, and I got it to work awhile back, but it was extremely inefficient and ran at like 5 frames a second.
So I think I did a fair amount to amend that: creating a TextureManager class and having my tiles sprites simply point to tiles in that object. However somewhere in all of my changes something got screwed up and it's now just displaying tiny white dots in the upper left corner of each tile.
I think it's an issue with pointers but I'm really not sure. Because when I close down the program it shows a bunch of OpenGL errors really fast in console before closing. It was impossible to read before closing so I grabbed a snapshot:
http://imgbox.com/aayX2yHWThe textures do show up in the TextureManager object when I look at it in debug mode, but something is screwing up in between, as the Tile's sprite doesn't have any characteristics:
http://imgbox.com/aavNp9fwThe important part(s) of TextureManager.cpp
-----------------------------------------------------------
void TextureManager::loadTextureMap(string fileName)
{
sf::Image imageLoad;
if(!imageLoad.LoadFromFile(fileName))
{
cout << "Error loading image, TextureManager addImageByFile function\n";
}
for(int i = 1; i <= 6; i++)
{
for(int k = 1; k <= 6; k++)
{
sf::Texture tempTexture;
tempTexture.LoadFromImage(imageLoad, sf::IntRect(((k-1)*48),((i-1)*48),48,48));
addTextureFromData(tempTexture);
//Empty up the memory for safety reasons(not sure if it actually does anything)
tempTexture.~Texture();
}
}
}
The Tile.h code
---------------------------------------------
class Tile
{
public:
Tile(int);
Tile(sf::Texture &);
Tile(void);
~Tile(void);
void setSprite(sf::Texture &);
void drawTile(int, int, sf::RenderWindow*);
sf::Sprite getSprite();
int getId();
private:
int id;
sf::Sprite tileSprite;
};
The important part(s) of the Tile.cpp code
-------------------------------------------------------------------
Tile::Tile(sf::Texture & textureSet)
{
tileSprite.SetTexture(textureSet);
}
void Tile::setSprite(sf::Texture & textureSet)
{
tileSprite.SetTexture(textureSet);
}
void Tile::drawTile(int xPos, int yPos, sf::RenderWindow *Window)
{
tileSprite.SetPosition(xPos, yPos);
Window -> Draw(tileSprite);
}
sf::Sprite Tile::getSprite()
{
return tileSprite;
}
Room.h code
------------------------------------------
class Room
{
public:
Room(vector<vector<Tile>>, vector<vector<Block>>);
Room(void);
~Room(void);
void initializeRoom(string, string, TextureManager &);
void setFloor(vector<vector<Tile>>);
void setBlocks(vector<vector<Block>>);
void addTile(int, Tile);
void addBlock(int, Block);
void assignTileSprites(TextureManager &);
void loadFromImages(sf::Image, sf::Image);
void saveToImages();
void displayRoomConsole();
void drawRoom(sf::RenderWindow *, TextureManager &);
Tile getTile(int, int);
Block getBlock(int, int);
vector<vector<Tile>> getFloor();
vector<vector<Block>> getBlocks();
int getRowNum();
int getCollumnNum();
private:
vector<vector<Tile>> floor;
vector<vector<Block>> blocks;
int rows;
int collumns;
};
The important parts of Room.cpp
----------------------------------------
void Room::initializeRoom(string floorFileLoc, string blocksFileLoc, TextureManager & textures)
{
sf::Image floorImage, blocksImage;
if(!floorImage.LoadFromFile(floorFileLoc))
{
cout << "Error loading Image, Floor file\n";
}
if(!blocksImage.LoadFromFile(blocksFileLoc))
{
cout << "Error loading Image, Blocks file\n";
}
collumns = floorImage.GetWidth();
rows = floorImage.GetHeight();
loadFromImages(floorImage, blocksImage);
assignTileSprites(textures);
}
void Room::assignTileSprites(TextureManager & textures)
{
for(int i = 0; i < getRowNum(); i++)
{
for(int k = 0; k < getCollumnNum(); k++)
{
switch(getTile(i, k).getId())
{
case plain:
getTile(i, k).setSprite(textures.getTexture(0));
break;
case iceFloor:
getTile(i, k).setSprite(textures.getTexture(1));
break;
case magmaFloor:
getTile(i, k).setSprite(textures.getTexture(2));
break;
case spikeFloor:
getTile(i, k).setSprite(textures.getTexture(3));
break;
case stickyFloor:
getTile(i, k).setSprite(textures.getTexture(4));
break;
case raisedPlain:
getTile(i, k).setSprite(textures.getTexture(5));
break;
case ditch:
getTile(i, k).setSprite(textures.getTexture(6));
break;
case water:
getTile(i, k).setSprite(textures.getTexture(7));
break;
case deathPit:
getTile(i, k).setSprite(textures.getTexture(8));
break;
case button:
getTile(i, k).setSprite(textures.getTexture(9));
break;
default:
cout << "Problem rendering tile, invalid tile id\n";
break;
}
}
}
}
void Room::drawRoom(sf::RenderWindow *window, TextureManager & textures)
{
for(int i = 0; i < getRowNum(); i++)
{
for(int k = 0; k < getCollumnNum(); k++)
{
getTile(i, k).drawTile(i*48, k*48, window);
}
}
}
..and finally main.cpp
-------------------------------------
int main()
{
int speed = 16;
Room testRoom;
Character testPlayer;
TextureManager textures;
bool rightPress = false;
bool leftPress = false;
bool upPress = false;
bool downPress = false;
int xMove = 0;
int yMove = 0;
testPlayer.setSpriteTexture("Resources/Sprites/chain.bmp");
//Loads a 36x36 texture map and splits it into individual textures
textures.loadTextureMap("Resources/TexturePacks/defaultTexturePack.bmp");
//loads the room data from the map bitmap files
testRoom.initializeRoom("Resources/Maps/emptyroomfloor.bmp", "Resources/Maps/emptyroomblocks.bmp", textures);
sf::RenderWindow Window(sf::VideoMode(640, 480, 32), "SFML Basic Build");
Window.SetFramerateLimit(60);
while (Window.IsOpened())
{
sf::Event Event;
while (Window.PollEvent(Event))
{
//This part was removed for conciseness :)
}
testPlayer.moveChar(xMove,yMove);
testPlayer.updatePos();
yMove = 0;
xMove = 0;
Window.Clear(sf::Color(50, 50, 50));
testRoom.drawRoom(& Window, textures);
Window.Draw(testPlayer.getSprite());
Window.Display();
}
system("pause");
return 0;
I know that's a ton of stuff to look through, but I thought that I might as well get a couple more sets of eyes(more experienced ones
to help me figure out where I was going wrong, because my brain is tied into knots right now...
EDIT:If someone replies, I'll read it in about 8 hours, because I'm going to bed at the moment. Hopefully it's an easy error to fix, and not a design flaw!!!