1
Graphics / [Solved] Text Display Problem
« on: January 27, 2012, 06:55:41 pm »
And, as always, it's a simple matter of deep-testing and reorganization that saved the day!
I made a barebones version of what I want based on this thread:
Based on the fact that this worked perfectly, I determined one part of the problem was that the RenderTexture was being displayed and drawn in the wrong order. That fixed the size issue, but the letters were still jumbled.
I took an older Message structure that I had made in the Allegro days and tweaked it to work with SFML, and now the text displays perfectly.
I made a barebones version of what I want based on this thread:
Code: [Select]
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <vector>
#include <string>
using std::vector;
using std::string;
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <sstream>
int main()
{
// Create the main window
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
sf::RenderTexture Texture;
Texture.Create(300, 100);
Texture.Clear();
// Create a graphical text to display
sf::Text text("");
sf::Clock timer;
unsigned int character = 0; float x = 0, y = 0;
std::string str = "... someone hates me !";
// Start the game loop
sf::Texture buff = Texture.GetTexture();
sf::Sprite sprite;
sprite.SetPosition(0, 0);
sprite.SetTexture(buff);
while (window.IsOpened()) {
// Process events
sf::Event event;
while (window.PollEvent(event)) {
// Close window : exit
if (event.Type == sf::Event::Closed || (event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Keyboard::Escape))
window.Close();
}
if (timer.GetElapsedTime() > 150 && character < str.size()) {
timer.Reset();
text.SetPosition(x, y);
text.SetString(sf::String(str[character]));
character++;
x += text.GetRect().Width;
}
buff = Texture.GetTexture();
// Draw the string
Texture.Draw(text);
Texture.Display();
window.Clear();
// Clear screen
window.Draw(sprite);
// Update the window
window.Display();
}
return EXIT_SUCCESS;
}
Based on the fact that this worked perfectly, I determined one part of the problem was that the RenderTexture was being displayed and drawn in the wrong order. That fixed the size issue, but the letters were still jumbled.
I took an older Message structure that I had made in the Allegro days and tweaked it to work with SFML, and now the text displays perfectly.