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

Author Topic: Oh boy, where did I go wrong?  (Read 1833 times)

0 Members and 1 Guest are viewing this topic.

lmsmi1

  • Newbie
  • *
  • Posts: 13
  • Console Obsession
    • View Profile
Oh boy, where did I go wrong?
« on: June 24, 2013, 08:14:34 pm »
I've just completely recoded my application, and it compiles fine, but doesn't open. I've got the main loop, am handling events correctly (I hope), and am doing everything correctly apparently. However something is going wrong in the code because the final application doesn't open an SFML window. If you can check over my code and possibly debug it, here it is:

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <vector>

sf::RenderWindow rWnd;
sf::Color bgColor;
sf::Font mainFont;
sf::String pre = "System!>";
sf::Text prefix;
sf::String buf;
sf::Text buffer;
sf::String lastStr;
sf::Text lastLine;
std::vector<sf::String> lineVector;
int lineCounter = 1;
int currentVectorPosition = 0;
int i = 0;

void MakeNewLine() {
    lineVector.push_back(buf);
    lineCounter += 1;
}

void PrintLastLines() {
    i = 1;
    prefix.setPosition(0, 0);
    while(i <= lineCounter) {
        lastLine.setPosition(prefix.getPosition().x + prefix.getGlobalBounds().width + 1, prefix.getPosition().y + prefix.getGlobalBounds().height);
        lastLine.setString(lineVector[currentVectorPosition]);
        rWnd.draw(prefix);
        rWnd.draw(lastLine);
        i += 1;
        currentVectorPosition += 1;
    }
}

void LoadFont() {
    mainFont.loadFromFile("dos.ttf");
}

void SetTextInfo() {
    prefix.setCharacterSize(18);
    prefix.setColor(sf::Color(255, 255, 255));
    prefix.setFont(mainFont);
    prefix.setPosition(0, 0);
    prefix.setString(pre);
    prefix.setStyle(sf::Text::Regular);

    buffer.setCharacterSize(18);
    buffer.setColor(sf::Color(255, 255, 255));
    buffer.setFont(mainFont);
    buffer.setPosition(prefix.getPosition().x + prefix.getGlobalBounds().width + 1, 0);
    buffer.setString(buf);
    buffer.setStyle(sf::Text::Regular);

    lastLine.setCharacterSize(18);
    lastLine.setColor(sf::Color(255, 255, 255));
    lastLine.setFont(mainFont);
    lastLine.setStyle(sf::Text::Regular);
}

int main() {
    rWnd.setTitle("OpenCMD [Version 2, Beta 1: Private Build]");
    rWnd.setSize(sf::Vector2u(800, 600));

    LoadFont();
    SetTextInfo();

    while(rWnd.isOpen()) {
        rWnd.clear(sf::Color(0, 0, 203));

        sf::Event event;
        while(rWnd.pollEvent(event)) {
            if(event.type == sf::Event::Closed) {
                rWnd.close();
            }
            if(event.type == sf::Event::KeyPressed) {
                if(event.key.code == sf::Keyboard::Return) {
                    MakeNewLine(); /* Make this later */
                }
                if(event.key.code == sf::Keyboard::Escape) {
                    rWnd.close();
                }
            }
            if(event.type == sf::Event::TextEntered) {
                if(event.text.unicode == '\b') {
                    buf.erase(buf.getSize() - 1, 1);
                } else {
                    buf.insert(buf.getSize(), event.text.unicode);
                }
            }
        }
        if(lineCounter == 0) {
            prefix.setPosition(0, 0);
            buffer.setPosition(prefix.getPosition().x + prefix.getGlobalBounds().width + 1, 0);
        } else {
            PrintLastLines();
            prefix.setPosition(0, prefix.getPosition().y + prefix.getGlobalBounds().height + 1);
            buffer.setPosition(prefix.getPosition().x + prefix.getGlobalBounds().width + 1, prefix.getPosition().y + prefix.getGlobalBounds().height);
        }
        buffer.setString(buf);
        rWnd.draw(prefix);
        rWnd.draw(buffer);
    }
}
 

I doubt this has anything to do with the compiler or linker, so I won't go into my specs just yet.


lmsmi1

  • Newbie
  • *
  • Posts: 13
  • Console Obsession
    • View Profile
Re: Oh boy, where did I go wrong?
« Reply #2 on: June 24, 2013, 08:38:57 pm »
Oh duh! You can use things in functions that haven't been declared yet X/

All right, now it's got the "*.exe has stopped working" when I run it. Something in the loop is causing that? Here's the new code:

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <vector>

void MakeNewLine();
void LoadFont();
void SetTextInfo();

sf::Color bgColor;
sf::Font mainFont;
sf::String pre = "System!>";
sf::Text prefix;
sf::String buf;
sf::Text buffer;
sf::String lastStr;
sf::Text lastLine;
std::vector<sf::String> lineVector;
int lineCounter = 1;
int currentVectorPosition = 0;
int i = 0;

int main() {
    sf::RenderWindow rWnd(sf::VideoMode(800, 600), "OpenCMD [Version 2, Beta 1: Private Build]");
    rWnd.setSize(sf::Vector2u(800, 600));
    LoadFont();
    SetTextInfo();

    while(rWnd.isOpen()) {
        rWnd.clear(sf::Color(0, 0, 203));

        sf::Event event;
        while(rWnd.pollEvent(event)) {
            if(event.type == sf::Event::Closed) {
                rWnd.close();
            }
            if(event.type == sf::Event::KeyPressed) {
                if(event.key.code == sf::Keyboard::Return) {
                    MakeNewLine(); /* Make this later */
                }
                if(event.key.code == sf::Keyboard::Escape) {
                    rWnd.close();
                }
            }
            if(event.type == sf::Event::TextEntered) {
                if(event.text.unicode == '\b') {
                    buf.erase(buf.getSize() - 1, 1);
                } else {
                    buf.insert(buf.getSize(), event.text.unicode);
                }
            }
        }
        if(lineCounter == 0) {
            prefix.setPosition(0, 0);
            buffer.setPosition(prefix.getPosition().x + prefix.getGlobalBounds().width + 1, 0);
        } else {
            i = 1;
            prefix.setPosition(0, 0);
            while(i <= lineCounter) {
            lastLine.setPosition(prefix.getPosition().x + prefix.getGlobalBounds().width + 1, prefix.getPosition().y + prefix.getGlobalBounds().height);
            lastLine.setString(lineVector[currentVectorPosition]);
            rWnd.draw(prefix);
            rWnd.draw(lastLine);
            i += 1;
            currentVectorPosition += 1;
    }
            prefix.setPosition(0, prefix.getPosition().y + prefix.getGlobalBounds().height + 1);
            buffer.setPosition(prefix.getPosition().x + prefix.getGlobalBounds().width + 1, prefix.getPosition().y + prefix.getGlobalBounds().height);
        }
        buffer.setString(buf);
        rWnd.draw(prefix);
        rWnd.draw(buffer);
    }
}

void MakeNewLine() {
    lineVector.push_back(buf);
    lineCounter += 1;
}

void LoadFont() {
    mainFont.loadFromFile("dos.ttf");
}

void SetTextInfo() {
    prefix.setCharacterSize(18);
    prefix.setColor(sf::Color(255, 255, 255));
    prefix.setFont(mainFont);
    prefix.setPosition(0, 0);
    prefix.setString(pre);
    prefix.setStyle(sf::Text::Regular);

    buffer.setCharacterSize(18);
    buffer.setColor(sf::Color(255, 255, 255));
    buffer.setFont(mainFont);
    buffer.setPosition(prefix.getPosition().x + prefix.getGlobalBounds().width + 1, 0);
    buffer.setString(buf);
    buffer.setStyle(sf::Text::Regular);

    lastLine.setCharacterSize(18);
    lastLine.setColor(sf::Color(255, 255, 255));
    lastLine.setFont(mainFont);
    lastLine.setStyle(sf::Text::Regular);
}
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Oh boy, where did I go wrong?
« Reply #3 on: June 24, 2013, 08:53:48 pm »
It would better if you went through your code yourself. Start commenting out parts that you think are not related and run it everytime afterwards, till you comment out the line that causes the issue. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Oh boy, where did I go wrong?
« Reply #4 on: June 24, 2013, 09:04:21 pm »
You don't even have a display() btw  :o

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Oh boy, where did I go wrong?
« Reply #5 on: June 24, 2013, 09:48:38 pm »
..and avoid all these global variables! Google «why global variables are bad ?» to know why. ;-)
SFML / OS X developer