Hello everybody!
My (nick)name is Taharrie and I am learning SFML. So far, I really enjoy learning it. Recently, I started to create some sort of tile-map system for a game, so I could design maps / levels fairly easy.
However, it seems that I have messed up somewhere. When I run my code, the correct map shows up, but the frames per second are insanely low! I know my laptop is not a fast one, but 60 frames per second should be achievable. I hope somebody notices my error and give me some hints to get me back on track.
Note: I understand that vertex arrays are much faster, but I do not feel very comfortable with them. I will learn them later on.
My code is separated into classes and whenever I leave out the tile-map part, I get my 60 frames per second...
My code currently looks like this (C++ & SFML 2:
Map.h:#pragma once
#include "SFML/Graphics.hpp"
#include <iostream>
class Map
{
public:
Map();
void update(sf::RenderWindow &window);
private:
// The width and height of a tile in pixels
const int tileWidth = 30;
const int tileHeight = 30;
// Add a new texture when a new tile is added
sf::Texture tFloor;
sf::Texture tWoodenBox;
sf::Texture tStoneWall;
sf::Texture tPinkPath;
};
Map.cpp:#include "Map.h"
Map::Map()
{
// Initialize the textures that are going to be used
// UPDATE THIS IF-STATEMENT WHEN A NEW TEXTURE IS ADDED
if (
!tFloor.loadFromFile("Assets/Map/floor.png") ||
!tWoodenBox.loadFromFile("Assets/Map/wood.png") ||
!tStoneWall.loadFromFile("Assets/Map/stone.png") ||
!tPinkPath.loadFromFile("Assets/Map/pink_path.png")
)
{
std::cout << "ERROR => could not load the tile textures!" << std::endl;
}
}
void Map::update(sf::RenderWindow &window)
{
/*
The codes with their corresponding texture names
- 0 => floor
- 1 => stone wall
- 2 => wooden box
- 3 => pink path
*/
const int rowAmount = 20;
const int columnAmount = 20;
const int map[rowAmount][columnAmount] =
{
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, // row 1
{ 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1 }, // row 2
{ 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1 }, // row 3
{ 1, 3, 3, 2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 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1 }, // row 19
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, // row 20
};
// Loop through the rows
for (int row = 0; row < rowAmount; row++)
{
// Loop through the columns
for (int column = 0; column < columnAmount; column++)
{
// Temporary place to store the current texture and sprite
sf::Texture textureToApply;
sf::Sprite spriteHolder;
// Apply the corresponding texture
// To add a new tile, add a new "else if()"-statement
if (map[row][column] == 0)
{
textureToApply = tFloor;
}
else if (map[row][column] == 1)
{
textureToApply = tStoneWall;
}
else if (map[row][column] == 2)
{
textureToApply = tWoodenBox;
}
else if (map[row][column] == 3)
{
textureToApply = tPinkPath;
}
// Appy the texture to the sprite placeholder
spriteHolder.setTexture(textureToApply);
// Set the correct position
// The columns represent the X, the rows the Y
spriteHolder.setPosition(column * tileWidth, row * tileHeight);
// Draw the sprite to the screen before looping again
window.draw(spriteHolder);
}
}
}
Part of main.cpp:[...]
window.clear();
map.update(window);
window.display();
[...]
I try to make use of good commenting habits as I code, but if any explanations on certain bits are needed, I would be happy to provide them.
Merry Christmas!
Thanks in advance,
Taharrie