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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - lmsmi1

Pages: [1]
1
Graphics / Load TrueType Font From Resource?
« on: June 26, 2013, 08:10:23 pm »
Is it possible to embed a TTF (TrueType Font) file in an application (resource file or direct embed) and then load the font from it? I'm looking for the most portable way to do this, but is it even possible?

2
System / Using an std::vector filled with sf::Strings
« on: June 25, 2013, 05:09:06 am »
I've got a program here that doesn't like to set sf::Text's sf::Strings from an std::vector<sf::String>. I've narrowed it down to this line:

lastLine.setString(lineVector[currentVectorPosition]);

And here's the full code:

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

int main() {
    /* Set Variables */
    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 currentPrefixY = 0;
    int i = 0;

    bgColor.r = 0;
    bgColor.g = 0;
    bgColor.b = 203;

    sf::RenderWindow rWnd(sf::VideoMode(800, 600), "OpenCMD [Version 2, Beta 1: Private Build]");
    rWnd.setSize(sf::Vector2u(800, 600));
    mainFont.loadFromFile("dos.ttf");

    prefix.setCharacterSize(18);
    prefix.setColor(sf::Color(255, 255, 255));
    prefix.setFont(mainFont);
    prefix.setPosition(0, currentPrefixY);
    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.setPosition(0, 0);
    lastLine.setString(lastStr);
    lastLine.setStyle(sf::Text::Regular);

    while(rWnd.isOpen()) {
        rWnd.clear(bgColor);

        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) {
                    lineVector.push_back(buf);
                    lineCounter += 1;
                }
                if(event.key.code == sf::Keyboard::Escape) {
                    rWnd.close();
                }
            }
            if(event.type == sf::Event::TextEntered) {
                if(event.text.unicode == '\b') {
                    if(!buf.isEmpty()) {
                        buf.erase(buf.getSize() - 1, 1);
                    }
                } else {
                    buf.insert(buf.getSize(), event.text.unicode);
                }
            }
        }
        /* Somewhere between here */
        if(lineCounter == 1) {
            prefix.setPosition(0, 0);
            buffer.setPosition(prefix.getPosition().x + prefix.getGlobalBounds().width + 1, 0);
        } else {
            i = 1; // Counter
            currentVectorPosition = 0;
            currentPrefixY = 0; // Set current text Y to 0
            while(i <= lineCounter) { // while i s less than or equal to the number of lines
                prefix.setPosition(0, currentPrefixY); // set the prefix x to 0, y to 0
                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);
                currentPrefixY = prefix.getPosition().y + prefix.getGlobalBounds().height;
                i += 1;
                currentVectorPosition += 1;
            }
            prefix.setPosition(0, currentPrefixY);
            buffer.setPosition(prefix.getPosition().x + prefix.getGlobalBounds().width + 1, prefix.getPosition().y + prefix.getGlobalBounds().height);
        }
        buffer.setString(buf);
        rWnd.draw(prefix);
        rWnd.draw(buffer);
        rWnd.display();
    }
}
 

My question is: why is my sf::Text not able to set it's sf::String from an std::vector of sf::Strings?

3
General / 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.

4
Window / Backspace not erasing last char?
« on: June 24, 2013, 12:00:57 am »
So I've got this little loop in my code:

    while(wnd.isOpen()) {
        sf::Event myEvent;

        while (wnd.pollEvent(myEvent)) {
            if (myEvent.type == sf::Event::Closed) {
                wnd.close();
            }

            if (myEvent.type == sf::Event::KeyPressed) {
                if (myEvent.key.code == sf::Keyboard::Escape) {
                    wnd.close();
                }
                if (myEvent.key.code == sf::Keyboard::Return) {
                    newLine = true;
                }
            }

            if (myEvent.type == sf::Event::TextEntered) {
                if (myEvent.text.unicode == '\b') {
                    if (!bText.isEmpty()) {
                        bText.erase(bText.getSize() - 1, 1);
                        buffer.setString(bText);
                    }
                } else {
                    bText.insert(bText.getSize(), myEvent.text.unicode);
                    buffer.setString(bText);
                }
            }
        }

        if (newLine == true) {
            myTxt.setPosition(0, myTxt.getPosition().y + myTxt.getGlobalBounds().height + 1);
            bText.clear();
            buffer.setString(bText);
            buffer.setPosition(myTxt.getGlobalBounds().width + 1, myTxt.getPosition().y + 1);
            newLine = !newLine;
        }

        wnd.draw(myTxt);
        wnd.draw(buffer);

        /*

        if (myClock.getElapsedTime() >= sf::milliseconds(500)) {
            myClock.restart();
            showCursor = !showCursor;
        }

        if(showCursor == true) {
            cursor = "_";
            myCursor.setPosition(myTxt.getGlobalBounds().width + buffer.getGlobalBounds().width + 1, myTxt.getPosition().y + 1);
        } else {
            cursor.clear();
        }

        wnd.draw(myCursor);

        */

        wnd.display();
    }

It works like a charm. However the application doesn't erase the last character printed on the screen when [BACKSPACE] is pressed. Can anyone explain why this is happening and how to fix this?

5
Window / Backspace Char?
« on: June 23, 2013, 07:50:08 am »
I've loaded a TTF file and am trying to get the BackSpace key to make the last character be erased. However I'm getting some weird character when Backspace is pressed and am wondering how I can fix it to where it works like I want it to. Here's the code:

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

sf::Color fontColor;
sf::Font mainFont;
sf::Clock myClock;

bool showCursor = false;
bool newLine = false;

void LoadFont() {
    mainFont.loadFromFile("dos.ttf");
    fontColor.r = 0;
    fontColor.g = 203;
    fontColor.b = 0;
}

int main() {
    sf::RenderWindow wnd(sf::VideoMode(1366, 768), "OpenCMD [Version 2, Beta 1]");
    wnd.setSize(sf::Vector2u(1366, 768));

    LoadFont();

    sf::Text myChar;
    myChar.setColor(fontColor);
    myChar.setFont(mainFont);
    myChar.setCharacterSize(18);
    myChar.setStyle(sf::Text::Regular);

    sf::String mainString = "System$~> ";
    sf::Text myTxt;
    myTxt.setColor(fontColor);
    myTxt.setString(mainString);
    myTxt.setFont(mainFont);
    myTxt.setCharacterSize(18);
    myTxt.setStyle(sf::Text::Regular);
    myTxt.setPosition(0, 0);

    sf::String cursor = "_";
    sf::Text myCursor;
    myCursor.setColor(fontColor);
    myCursor.setString(cursor);
    myCursor.setFont(mainFont);
    myCursor.setCharacterSize(18);
    myCursor.setStyle(sf::Text::Regular);
    myCursor.setPosition(myTxt.getGlobalBounds().width + 1, 0);

    sf::String bText;

    sf::Text buffer;
    buffer.setColor(fontColor);
    buffer.setFont(mainFont);
    buffer.setCharacterSize(18);
    buffer.setStyle(sf::Text::Regular);
    buffer.setPosition(myTxt.getGlobalBounds().width + 1, 0);

    while(wnd.isOpen()) {
        sf::Event myEvent;

        while (wnd.pollEvent(myEvent)) {
            if (myEvent.type == sf::Event::Closed) {
                wnd.close();
            }

            if (myEvent.type == sf::Event::KeyPressed) {
                if (myEvent.key.code == sf::Keyboard::Escape) {
                    wnd.close();
                }
                if (myEvent.key.code == sf::Keyboard::Return) {
                    newLine = true;
                }
            }

            if (myEvent.type == sf::Event::TextEntered) {
                if (myEvent.text.unicode < 128) {
                    bText.insert(bText.getSize(), myEvent.text.unicode);
                    buffer.setString(bText);
                }
                if (myEvent.text.unicode == sf::Keyboard::BackSpace) {
                    if (!bText.isEmpty()) {
                        bText.erase(bText.getSize(), 1);
                    }
                }
            }
        }

        if (newLine == true) {
            myTxt.setPosition(0, myTxt.getPosition().y + myTxt.getGlobalBounds().height + 1);
            bText.clear();
            buffer.setString(bText);
            buffer.setPosition(myTxt.getGlobalBounds().width + myCursor.getGlobalBounds().width + 1, myTxt.getPosition().y + 1);
            newLine = false;
        }

        wnd.draw(myTxt);
        wnd.draw(buffer);

        /*

        if (myClock.getElapsedTime() >= sf::milliseconds(500)) {
            myClock.restart();
            showCursor = !showCursor;
        }

        if(showCursor == true) {
            cursor = "_";
            myCursor.setPosition(myTxt.getGlobalBounds().width + buffer.getGlobalBounds().width + 1, myTxt.getPosition().y + 1);
        } else {
            cursor.clear();
        }

        wnd.draw(myCursor);

        */

        wnd.display();
    }
}
 

6
General / Rendering a Text Cursor
« on: June 22, 2013, 02:41:27 am »
So uhm, I've just "transferred" from SDL and so far... I LOVE SFML!!! I've noticed it has all the basic functions I need for a console simulator all in one archive. I don't have to use it with external libs (like SDL & SDL_ttf for example).

So here's my question: What will I need to do in SFML to render a blinking text cursor on an imitation console window? I've already got some code here for you (that works when it's linked to the correct libs). By the way: I'm using G++ (GCC) TDM on Windows 7 (I compile apps in 32-bit mode.)

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

int main() {
    sf::RenderWindow wnd(sf::VideoMode(650, 300), "SFML Console");
    sf::Vector2u myVector(650, 300);
    wnd.setSize(myVector);

    sf::Font myFont;
    myFont.loadFromFile("theFont.ttf");

    sf::Color myClr;
    myClr.r = 0;
    myClr.g = 203;
    myClr.b = 0;

    sf::String myStr = "Hello world!";

    sf::Text myTxt;
    myTxt.setColor(myClr);
    myTxt.setString(myStr);
    myTxt.setFont(myFont);
    myTxt.setCharacterSize(12);
    myTxt.setStyle(sf::Text::Regular);
    myTxt.setPosition(0, 0);

    while(wnd.isOpen()) {
        sf::Event myEvent;

        while (wnd.pollEvent(myEvent)) {
            if (myEvent.type == sf::Event::Closed) {
                wnd.close();
            }

            if (myEvent.type == sf::Event::KeyPressed) {
                if (myEvent.key.code == sf::Keyboard::Escape) {
                    wnd.close();
                }
            }

            wnd.clear();
            wnd.draw(myTxt);
            wnd.display();
        }
    }
}
 

Thanks in advance.

~lmsmi1

Pages: [1]