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

Author Topic: I have absolutely no idea why this isn't working  (Read 1717 times)

0 Members and 1 Guest are viewing this topic.

TrampolineGames

  • Newbie
  • *
  • Posts: 4
    • View Profile
I have absolutely no idea why this isn't working
« on: September 06, 2011, 05:45:37 am »
I have this code:

Code: [Select]
#include <SFML/Graphics.hpp>

int main(){
    sf::RenderWindow App(sf::VideoMode(1024, 768), "Game");
App.UseVerticalSync(true);
   
    //Loads the game font and images
    sf::Font GameFont;
    if (!GameFont.LoadFromFile("calist.ttf")) {
        return EXIT_FAILURE;
    }
    sf::Image TextBoxImage;
    sf::Sprite TextBox;
    if (!TextBoxImage.LoadFromFile("Blue.png")) {
        return EXIT_FAILURE;
    }
    TextBox.SetImage(TextBoxImage);
    TextBox.SetColor(sf::Color(0, 0, 255, 155));
    TextBox.SetPosition(0, 490.f);
    TextBox.Scale(1.6f, 1.6f);
    const sf::Input& Input = App.GetInput();
    bool DrawText = false;
    bool Next = Input.IsKeyDown(sf::Key::Return);
   
    //Loads the game Text
    sf::String dia1a ("That piece of trash over there is Joey.", GameFont);
   
    //Game Loop
    while (App.IsOpened())
    {
        App.Clear();
        App.Draw(TextBox);
        App.Display();
    //Dialogue for level 1
sf::Event Dialogue1;
        while (App.GetEvent(Dialogue1)) {
            if (Next = true){
                DrawText = true;
            }
            if ((DrawText = true)) {
                App.Draw(dia1a);
                App.Display();
        }
    }
    //Close Game    
        sf::Event GameClose;
        while (App.GetEvent(GameClose)) {
            if (GameClose.Type == sf::Event::Closed)
                App.Close();
            }
        }
    return EXIT_SUCCESS;
}


Which is supposed to display a line of text when I hit enter. The text should also stay on the screen because of DrawText. However, whenever I hit any key, or move the mouse at all, the text flickers for a while and then stops whenever I stop doing what I'm doing. If someone could please help me that would be much appreciated.

Relic

  • Newbie
  • *
  • Posts: 43
    • View Profile
I have absolutely no idea why this isn't working
« Reply #1 on: September 06, 2011, 06:57:23 am »
At a quick glance:

You are drawing the text inside the event-processing while loop. While there are events in the queue the text is visible.

The line:
if (DrawText = true)
should be:
if (DrawText == true)
or simply:
if (DrawText)

The variable Next is useless. You initialize it once before getting in while loop. After that it knows nothing about the actual Return key state.

Two sequential event-processing loops look rather odd.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10390
    • View Profile
    • development blog
    • Email
I have absolutely no idea why this isn't working
« Reply #2 on: September 06, 2011, 10:21:17 am »
Like Relic said, use just one event processing loop and for programming style indent your text right!
So there you go:
Code: [Select]
#include <SFML/Graphics.hpp>

int main(){
    sf::RenderWindow App(sf::VideoMode(1024, 768), "Game");
    App.UseVerticalSync(true);
   
    //Loads the game font and images
    sf::Font GameFont;
    if (!GameFont.LoadFromFile("calist.ttf")) {
        return EXIT_FAILURE;
    }
    sf::Image TextBoxImage;
    sf::Sprite TextBox;
    if (!TextBoxImage.LoadFromFile("Blue.png")) {
        return EXIT_FAILURE;
    }
    TextBox.SetImage(TextBoxImage);
    TextBox.SetColor(sf::Color(0, 0, 255, 155));
    TextBox.SetPosition(0, 490.f);
    TextBox.Scale(1.6f, 1.6f);
   
    // Realtime keyboard input
    const sf::Input& Input = App.GetInput();
    bool DrawText = false;
   
    //Loads the game Text
    sf::String dia1a ("That piece of trash over there is Joey.", GameFont);
   
    //Game Loop
    while (App.IsOpened())
    {
        // Event processing
        sf::Event EventHandler;
        while (App.GetEvent(EventHandler)) {
            // Close event
            if (EventHandler == sf::Event::Closed) {
               App.Close();
            }
        }
       
        // Check whether Return is pressed or not
        if(Input.IsKeyDown(sf::Key::Return)
            DrawText = true;
        // Reset it, otherwise it gets constanty drawn
        else
            DrawText = false;
       
        // Clear screen
        App.Clear();
       
        // Draw text if DrawText is true
        if(DrawText)
         App.Draw(TextBox);
         
        // Display everything
        App.Display();
    }
    return EXIT_SUCCESS;
}


And try to stick to the following plan:
- Process events / check inputs
- Run thought the game logic (reposition etc.)
- Draw you objects

eXp
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://dev.my-gate.net/

TrampolineGames

  • Newbie
  • *
  • Posts: 4
    • View Profile
I have absolutely no idea why this isn't working
« Reply #3 on: September 06, 2011, 06:08:18 pm »
Thanks everyone, I got it to work...but I'm sure I'll be posting in this forum a lot over the course of this project ^_^

 

anything