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

Author Topic: Text only drawing white  (Read 3642 times)

0 Members and 1 Guest are viewing this topic.

_Kronk_

  • Newbie
  • *
  • Posts: 11
    • View Profile
Text only drawing white
« on: August 27, 2012, 08:06:47 pm »
Hey folks! My engine is speeding right along and I'm making great progress, but I've hit an odd issue.

Fonts are loading fine, but my text objects won't display as any color other than white.

I can move them around and set the font, but they won't scale, rotate, change colors, or anything else. Any idea what's going on?

I'm using SFML 2 in Windows Vista.

There's nothing wrong with line 42.


/// Array.h

#include <cstddef>

template<class T> class Array
{
protected:

        T* data;
        unsigned size;

public:

        Array() : data(NULL), size(0) { }
        Array(const unsigned Size);
        Array(const unsigned Size, const T& Init);

        /// The = operator doesn't resize the arrays!
        Array<T>& operator= (const Array<T>& A)
        {
                if(A.Size() < size)
                {
                        for(int i = 0; i < A.Size(); i++)
                        {
                                data[i] = A[i];
                        }
                }
                else
                {
                        for(int i = 0; i < size; i++)
                        {
                                data[i] = A[i];
                        }
                }
                return (*this);
        }

        T& operator[](const unsigned i) { return data[i]; }
        const unsigned Size() const { return size; }

        void ClearTo(const T& t) { for(int i = 0; i < size; i++) data[i] = t; }

        void Reset(unsigned s);
        void Reset(unsigned s, const T& Init);

        ~Array();
};

template<class T> Array<T>::Array(const unsigned Size) : data(NULL), size(Size)
{
        data = new T[size];
}

template<class T> Array<T>::Array(const unsigned Size, const T& Init) : data(NULL), size(Size)
{
        data = new T[size];
        for(unsigned i = 0; i < size; i++) data[i] = Init;
}

template<class T> Array<T>::~Array() { if(!data) return; delete [] data; size = 0; }

template<class T> void Array<T>::Reset(unsigned s)
{
        if(data) delete [] data;

        size = s;
        data = new T[size];
}

template<class T> void Array<T>::Reset(unsigned s, const T& Init)
{
        if(data) delete [] data;

        size = s;
        data = new T[size];

        for(unsigned i = 0; i < size; i++) data[i] = Init;
}

 


/// FontManager.h

struct Font
{
private:

        std::string name;

        sf::Font sfFont;

public:

        Font() { }

        bool Load(std::string n, std::string path)
        {
                name = n;

                if(sfFont.loadFromFile(path)) return true;
                else return false;
        }

        const std::string& Name() const { return name; }

        operator sf::Font&() { return sfFont; }
};

class FontManager
{
private:

        Array<Font> fonts;

        static std::ofstream log;

public:

        FontManager(std::string cfgPath);

        const sf::Font& operator[](const unsigned i) { return fonts[i]; }
};

 

/// FontManager.cpp

#include "FontManager.h"
#include "BaseClasses/Dependant.h"
#include "Utilities/Stream.h"
#include <SFML/Graphics/Font.hpp>

template<> FontManager * Dependant<FontManager>::ptr = NULL;

std::ofstream FontManager::log("logs/FontManager.txt");

FontManager::FontManager(std::string cfgPath) : fonts(true)
{
        log << "Attemping to open file: " << cfgPath << ", ";

        std::ifstream file(cfgPath.c_str(), std::ios::binary);
        if(!file)
        {
                log << "unable to open file!" << std::endl;
                return;
        }
        log << "opened file." << std::endl;

        std::string folder;

        Utilities::Stream::Fetch<std::string>("folder", file, folder);

        log << "Folder path: " << folder << std::endl;

        unsigned count = Utilities::Stream::Count("font", file);
        if(!count)
        {
                log << "No fonts found; Intended? (Probably not.)" << std::endl;
                return;
        }
        log << count << " fonts found. Loading..." << std::endl;

        fonts.Reset(count);

        std::string fontName, fontPath;

        for(unsigned i = 0; i < count; i++)
        {
                Utilities::Stream::Ignore("font", file);
                file >> fontName >> fontPath;

                if(fonts[i].Load(fontName, folder + fontPath))
                {
                        log << "Loaded " << fontName << std::endl;
                }
                else
                {
                        log << "Unable to load font: " << fontName << std::endl;
                }
        }
        log << "Fonts loaded." << std::endl;
}

 

/// Text.h

#include "BaseClasses/Graphical.h"
#include "BaseClasses/Component.h"
#include "Color.h"
#include "Primative.h"
#include <SFML/Graphics/Text.hpp>

namespace sf
{
        class Font;
}

class FontManager;
class Renderer;

class Text :
        public Graphical,
        public Component,
        public Dependant<FontManager>,
        public Dependant<Renderer>
{
protected:

        sf::Text text;

        Float *x, *y;

public:

        virtual ~Text() { }

        Text(std::string n);

        virtual void Activate() { draw = true; }
        virtual void Deactivate() { draw = false;}
        virtual bool Active() const { return draw; }

        virtual void Read(std::istream& file);
        virtual void Write(std::ostream& file);

        virtual void Connect(Object * const Owner);

        virtual void SetColor(const Color& Color);
        virtual const Color GetColor() const;
        virtual void Draw(sf::RenderWindow& window);

        bool OnEvent(Event& e) { return false; }

        virtual void SetPosition        (const XYPairF& XY)     ;
        virtual void SetRotation        (float R)                               ;
        virtual void SetScale           (const XYPairF& XY)             ;
        virtual void SetOrigin          (const XYPairF& XY)             ;
        virtual void Offset             (const XYPairF& XY)             ;
        virtual void Rotate                     (float R)                               ;
        virtual void Scale                      (const XYPairF& XY)             ;
        virtual void SetString          (const std::string& S)  ;
        virtual void SetFont            (const sf::Font& F)             ;
        virtual void SetCharSize        (unsigned S)                    ;
        virtual void SetStyle           (sf::Text::Style s)             ;
};

 

/// Text.cpp

void Text::Read(std::istream& file)
{
        std::string string;
        XYPairF scale;
        float rotation;
        unsigned charSize, layer, font;
        Color c;

        Utilities::Stream::FetchString("string", file, string);
        Utilities::Stream::Fetch<bool>("draw", file, draw);
        Utilities::Stream::Fetch<float>("offsetX", file, offset.x);
        Utilities::Stream::Fetch<float>("offsetY", file, offset.y);
        Utilities::Stream::Fetch<float>("scaleX", file, scale.x);
        Utilities::Stream::Fetch<float>("scaleY", file, scale.y);
        Utilities::Stream::Fetch<float>("rotation", file, rotation);
        Utilities::Stream::Fetch<unsigned>("charSize", file, charSize);
        Utilities::Stream::Fetch<unsigned>("layer", file, layer);
        Utilities::Stream::Fetch<unsigned>("fontIndex", file, font);
        Color::FetchColor("color", file, c);

        text.setFont((*Dependant<FontManager>::ptr)[font]);
        text.setCharacterSize(charSize);
        text.setString(string);
        text.setScale(scale.x, scale.y);
        text.setRotation(rotation);
        text.setColor(c);

        Dependant<Renderer>::ptr->Add(this, layer);
}

 

/// Dependant.h

template<typename T> class Dependant
{
protected:

        static T* ptr;

public:

        static void SetPtr(T* const p) { ptr = p; }
        virtual ~Dependant() { }
};

 

[attachment deleted by admin]
« Last Edit: August 28, 2012, 02:50:31 am by _Kronk_ »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text only drawing white
« Reply #1 on: August 27, 2012, 08:18:16 pm »
There's an error line 42 in your code.
Laurent Gomila - SFML developer

_Kronk_

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Text only drawing white
« Reply #2 on: August 27, 2012, 08:35:57 pm »
You asked for it!  :) See the original post.

I'm 100% sure that all of the data is being parsed from the file correctly; I'm logging everything and it's behaving exactly as it should.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text only drawing white
« Reply #3 on: August 27, 2012, 09:07:58 pm »
Quote
See the original post.
You should have posted in a new message, now people will really think that there's an error at line 42 in your code :P
Laurent Gomila - SFML developer

_Kronk_

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Text only drawing white
« Reply #4 on: August 28, 2012, 02:51:14 am »
Fixed  ::)

Anyhow, why is this happening?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text only drawing white
« Reply #5 on: August 28, 2012, 03:47:33 am »
I don't know, your code is too complicated and incomplete. Try to reproduce the problem in a complete and minimal code.
Laurent Gomila - SFML developer

_Kronk_

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Text only drawing white
« Reply #6 on: August 28, 2012, 04:31:42 pm »
My code is too complicated. It turns out that I forgot that the RenderLayer class I was adding my text objects to was overwriting their properties. I got that fixed, so I now have working text and one less design flaw!

 

anything