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.


Messages - 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 / Re: Using an std::vector filled with sf::Strings
« on: June 25, 2013, 06:58:22 pm »
Thanks, I just figured out that code can be destroyed when just one character is added in the wrong place. GDB is very limited in my opinion, but I'll check the documentation. Oh and, could you explain why I have to compare myEvent.key.code to sf::Keyboard::Return? I don't get the same results when comparing myEvent.text.unicode to '\n' for some odd reason.

3
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?

4
General / Re: Oh boy, where did I go wrong?
« 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);
}
 

5
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.

6
Window / Re: Backspace not erasing last char?
« on: June 24, 2013, 07:18:34 pm »
http://www.sfml-dev.org/tutorials/2.0/graphics-draw.php

Quote
This clear/draw/display cycle is the only good way to draw things. Don't try other strategies, such as keeping pixels from the previous frame, "erasing" pixels, or drawing once and calling display multiple times. You'll get strange results due to double-buffering.
Modern graphics chipsets and APIs are really made for repeated clear/draw/display cycles where everything is completely refreshed at each iteration of the main loop. Don't be scared to draw 1000 sprites 60 times per second, you're far below the millions of triangles that your computer can handle.

(yes, it's in red also in the tutorial)

So the only logical thing to do would be to add the typed sf::String to the end of a std::vector<sf::String>, then render them in order on the screen from the 0 position to the end of the vector?

7
Window / Re: Backspace not erasing last char?
« on: June 24, 2013, 03:39:39 am »
What? Do you mean I'm not refreshing the window when I should? I got my code to erase the last char without using sf::RenderWindow::clear() a few hours ago, but for some reason something I added/removed/edited removed the effect.

8
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?

9
Window / Re: Backspace Char?
« on: June 23, 2013, 08:06:57 pm »
Sorry, I'll start a new thread.

10
Window / Re: Backspace Char?
« on: June 23, 2013, 07:41:00 pm »
Okay then, how would I go about changing the BG color without using sf::RenderWindow::clear()? When I put it right before the main while loop, the BG blinks from black to blue (I set the BG color to blue) at an extremely fast speed. When I place the clear() inside the main while loop, the screen is fine, but remember: this is supposed to imitate a console like CMD, meaning it's supposed to show every line rendered, so since clear() is used over and over again, it doesn't let the screen keep the previously rendered lines on the screen. So is there a way to change the BG color without using clear()?

11
Window / Re: Backspace Char?
« on: June 23, 2013, 06:04:39 pm »
All right, I've changed by code around a bit:

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

And see that the backspace character isn't being displayed anymore, but when I do press it, it doesn't erase the last character typed "o" for example when I type "hello". I'm erasing two characters including the '\b', so it should be doing what I expect. What am I doing wrong?

12
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();
    }
}
 

13
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]
anything