Hi all.
I'm working on roguelike game, and so my first idea of how to render the map was like this:
void fill(sf::RenderWindow &window, const std::string &map) {
sf::Text text;
text.setFont(font_); // font_ is loaded on app start
text.setCharacterSize(20);
sf::FloatRect backgroundRect;
sf::RectangleShape background;
background.setFillColor(sf::Color(0x00, 0x00, 0x00)); // Always black for simplicity
sf::Color wallColor(0x9E, 0x86, 0x64);
sf::Color floorColor(0x33, 0x33, 0x33);
for (int y = 0; y < 60; ++y) {
for (int x = 0; x < 80; ++x) {
const char ch = data[y * 80 + x];
if (ch == '#') {
text.setColor(wallColor);
} else {
text.setColor(floorColor);
}
text.setString(ch);
text.setPosition(x * 20, y * 20);
backgroundRect = text.getLocalBounds();
background.setPosition(sf::Vector2f(backgroundRect.width, backgroundRect.height));
window.draw(background, text.getTransform());
window.draw(text);
}
}
}
int main() {
sf::RenderWindow window;
window.create(sf::VideoMode(1920, 1200), "test",
sf::Style::Close | sf::Style::Titlebar), settings);
window.setFramerateLimit(30);
const char map[] = "80 cols x 60 lines of chars"; // 80x60 here, basically "# ...... #"
while(window.isOpen()) {
window.clear();
fill(window, map);
window.display();
}
}
Well, it's a little bit more complex but idea is shown. If i comment-out map fill then my app takes about 1% CPU, but if not - it takes 100% CPU all the time. So I guess fill function is not efficient enough. Is there a way to speed up things?
The map is just a matrix (an array if you want) of chars. Different chars (as they represents different entities, like walls, doors, etc) have different attributes (background/foreground color).
Thanks in advance.