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

Author Topic: [Solved] Text Display Problem  (Read 2986 times)

0 Members and 2 Guests are viewing this topic.

Mikebissle

  • Newbie
  • *
  • Posts: 10
    • MSN Messenger - freezingblue
    • Yahoo Instant Messenger - mikebissle
    • View Profile
[Solved] Text Display Problem
« on: January 26, 2012, 03:54:25 pm »
I'm testing displaying text in sfml 2.0, "typewriter" style. The way it's working now, it works properly for the first seven characters, then it all goes to hell. Even weirder, the text gets smaller every time I access the message. I don't know what I'm doing wrong here!

Here's some relevant code--if more is needed, let me know.

Message structure:
Code: [Select]

void Message::Create(string file, string key, float xx, float yy, float ww, float hh, float sz) {
    string temp;
  ifstream istream(file);
 
  //until the stream finds the search term to search in the text document, it will iterate through it.
  for(file; temp.find(key) == -1; getline(istream, temp));
  for(file; temp != "[end]"; text += temp, getline(istream, temp));

  x = y = 0;
  w = ww; h = hh; size = sz;

  pos = 0;;
  pause = done;
  print = undone;

//Display is a Canvas structure which contains an sf::RenderTexture (buff) among other values
  Display.Make(w, h);
  Display.Set(xx, yy);
  Display.buff.Clear(BLACK);
 
//Data is sf::Text
  Data.SetString(text.at(pos));
  Data.SetPosition(0, 0);
  Data.SetCharacterSize(size);

  istream.close();
}

 void Message::Logic() {
  if (print) return;
  ++pos;
  if (pos == text.length() - 1) {
    print = done;
    return;
  }
 
  x + Data.GetRect().Width > w? x = 0, y += Data.GetRect().Height:  x += Data.GetRect().Width;
  Data.SetString(text.at(pos));
  Data.SetPosition(x, y);
}


void Message::Drawing() {

  Display.DrawText(Data);
  Display.Refresh();
  Display.Blit(*Buffer);
 
}


Relevant Canvas structure :
Code: [Select]

void Canvas::Make(short ww, short hh) {
   w = ww; h = hh; x = y = 0;
   buff.Create(ww, hh); //buff = sf::RenderTexture
     buff.Clear(CLEAR);  
     img = this->buff.GetTexture(); //img = sf::Texture
     spr.SetTexture(img); //sf::Sprite
}

void Canvas::Set(float xx, float yy) {
  x = xx; y = yy;
  spr.SetPosition(x, y);
}

void Canvas::DrawText(sf::Text txt) {
  buff.Draw(txt);
  this->Refresh();
}

void Canvas::Blit(Canvas &bb) {
  img = buff.GetTexture();
  bb.Draw(*this);
}

void Canvas::Refresh() {
  buff.Display();
}
//pasted from header file for relevance:
    template<class Object>
     void Draw(Object &gfx) {
      buff.Draw(gfx.spr);
      this->Refresh();
}

Mikebissle

  • Newbie
  • *
  • Posts: 10
    • MSN Messenger - freezingblue
    • Yahoo Instant Messenger - mikebissle
    • View Profile
[Solved] Text Display Problem
« Reply #1 on: January 26, 2012, 09:19:36 pm »
Having done some testing, I narrowed the problem down some--I copied the Display object to an Image and saved it  as a file. The file shows the letters perfectly displayed at the right size, but when the Display is drawn to the buffer, it shows mangled characters after the first few, and repeated attempts only show the first letter.

Could them both being both RenderTextures be the problem? I haven't  seen much around here about drawing between RenderTextures--is it impractical/unstable?

Mikebissle

  • Newbie
  • *
  • Posts: 10
    • MSN Messenger - freezingblue
    • Yahoo Instant Messenger - mikebissle
    • View Profile
[Solved] Text Display Problem
« Reply #2 on: January 27, 2012, 03:34:20 am »
I've also had an Image copy the printed data and save it to an image. This is what comes up the first time I open the message:



All subsequent tries show the same, but in an even smaller font that's indecipherable.

For what's it worth, the message should be "Oh, for heaven's sake! Why will this not work?"

I hope that helps if anyone has any idea what the problem could be.

Mikebissle

  • Newbie
  • *
  • Posts: 10
    • MSN Messenger - freezingblue
    • Yahoo Instant Messenger - mikebissle
    • View Profile
[Solved] Text Display Problem
« Reply #3 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:

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. :)

 

anything