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

Author Topic: Cannot add text to window  (Read 12956 times)

0 Members and 1 Guest are viewing this topic.

GroundZero

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Cannot add text to window
« on: June 23, 2012, 06:35:48 pm »
Dear readers,

when I use this code in SFML 2.0:

sf::Font font;
font.loadFromFile("arial.ttf");
sf::Text text("Crystallibrium", font, 50);

I receive the error:

expected constructor, destructor, or type conversion before '.' token

I cannot find a solution on Google, can anyone help me out please?


- Windows 7 Prof
- Qt SDK (newest)
- SFML 2.0

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Cannot add text to window
« Reply #1 on: June 23, 2012, 06:44:21 pm »
This piece of code is ok, can you show a complete code that produces the error please?
Laurent Gomila - SFML developer

GroundZero

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Re: Cannot add text to window
« Reply #2 on: June 23, 2012, 06:56:19 pm »
Thanks for your answer Laurent, my complete code is:

// include SFML headers
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

// include other headers
#include <string>

// custom headers
#include <userCharacter.h>
#include <collisionDetection.h>

// global settings
bool showSplashScreen = false;
bool isGameAvailable = false;
bool isGameExiting = false;
bool isGamePaused = false;
bool isShowingMenu = false;

// game title
sf::Font font;
font.loadFromFile("arial.ttf");
sf::Text text("GameName", font, 50);

// main loop
int main()
{
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(800, 600), "GameName");
    window.setFramerateLimit(60);

    // Start the game loop
    while (window.isOpen())
    {
        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Close window : exit
            if (event.type == sf::Event::Closed)
                window.close();
        }



        // Clear screen
        window.clear();

        // Draw text
        window.draw(text);

        // Update the window
        window.display();
    }

    return 0;
}
 

my character header (not finished lol) is:
#ifndef USERCHARACTER_H
#define USERCHARACTER_H

class userCharacter{
private:
    std::string characterName;
    int characterLevel;
    float characterExperience;

public:
    userCharacter( std::string cName, int cLevel, float cExperience );
};

#endif // USERCHARACTER_H
 

best regards

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Cannot add text to window
« Reply #3 on: June 23, 2012, 07:29:44 pm »
Try putting
sf::Font font;
font.loadFromFile("arial.ttf");
sf::Text text("Crystallibrium", font, 50);
inside your main().

Calling class methods ( font.loadFromFile(...) ) in global scope is illegal C++ at least in some languages (Visual C++ always manages to prove me wrong).
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

GroundZero

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Re: Cannot add text to window
« Reply #4 on: June 23, 2012, 07:32:57 pm »
ugh... you are right, thanks so much for your answer lol :)
stupid actually I didnt notice, as I knew this (didnt touched C++ for a long time though)

Thanks for you help!

EDIT: Just a small question, it says "Failed to load font "arial.ttf" (failed to create the font face)".
I am using the Arial.ttf form my windows/fonts folder...

Should I give a static link i.e. C:\Users\MySelf\Desktop\Project\Arial.tff or something or is the file unusable ?

EDIT: I gave it the static links as I meantioned, now it is working :) thanks again all ;)
« Last Edit: June 23, 2012, 07:44:19 pm by GroundZero »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11053
    • View Profile
    • development blog
    • Email
Re: Cannot add text to window
« Reply #5 on: June 23, 2012, 10:34:23 pm »
EDIT: Just a small question, it says "Failed to load font "arial.ttf" (failed to create the font face)".
I am using the Arial.ttf form my windows/fonts folder...

Should I give a static link i.e. C:\Users\MySelf\Desktop\Project\Arial.tff or something or is the file unusable ?

EDIT: I gave it the static links as I meantioned, now it is working :) thanks again all ;)

Giving the absolute path isn't such a nice solutino since your application could later be moved somewhere randomly and will still look in the maybe not anymore existing folder.
If you start your application from inside of Visual Studio it will by default search in the folder where your .vcxproj file is.
If you start it with the explorer the font should be placed next to the exe.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Celtic Minstrel

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: Cannot add text to window
« Reply #6 on: June 24, 2012, 08:46:14 am »
You should probably try to give either a path to a font relative to your program, or an absolute path to a font in the global Fonts directory (which I'd guess is something like "C:\Windows\Fonts"). Since this is Arial, I'd recommend the absolute path. (Of course, this does mean your program will only work properly on Windows.) If you were using some obscure font you found somewhere that you don't expect someone else to have, I'd do what Exploiter said and put the font in your project folder.

 

anything