SFML

Pages: 12
i don't understand SFML, im use to displaying to the console via cout and theyre website doesnt explain well to noobies that are going from console to SFML
Alas, I feel your pain. It's a lot of work to display text without a tty.
What are you trying to do? Just print some text?

This example shows how to do some very basic things, including printing text and drawing an image:

http://sfml-dev.org/documentation/2.0/

Duoas wrote:
It's a lot of work to display text without a tty.


I'd disagree. I don't particularly find it to be difficult in SFML.

1
2
sf::Text sometext("Hello World");
myWindow.Draw(sometext);
Last edited on
Don't think that displaying text will be the easiest thing to do in SFML just because it's the easiest thing to do on a console. I'd suggest following the SFML tutorials in order, they are pretty well laid out.

If you just can't seem to learn SFML that way, try here:
http://www.youtube.com/watch?v=PFUK0yCPUu0&playnext=1&list=PL265E899C3318EC67
boom
Last edited on
what is tty?

i am just trying to learn how to use SFML, and how to do different things. like
I'd disagree. I don't particularly find it to be difficult in SFML.

1
2
sf::Text sometext("Hello World");
myWindow.Draw(sometext);


1) why wouldn't i just use using namespace sf; and not have to do sf:: every time?
2) is Text the type or the object? same with sometext?
3) Is Draw the member function of myWindow?
4) and my biggest question is why if SFML can make classes do all these things, wouldnt you just do them yourself...then you would understand what each class is, why it is, etc.

5) and i dont find their tutorials that understanding, ... for one if i didnt undrstand C++ in console i would just look through numerous books, but with SFML, i do no have that many resources, i seem to have only one explanation at their website and everyone tells me to go to the tutorials that i didnt understand in the first place.

FOR EXAMPLE in console explanations , i would see cout << and know that it is being outputted to screen , and the same with cin >> for input. If i saw an example with an unknown word i would normally look in the beginning of the code for a class, enum, struct, union, etc. and then i would know why that name is there, in SFML i am clueless.

and in addition i was expecting more of an example in SFML saying ...in console to display output is cout and in SFML it is____, but i do not see such

another thing i dont understand is why would they tell noobies to start at console (when no one legitimately makes programs in the console) ( well in a gamers sense) cuz then it seems you have to relearn everything?

if anyone could help me more understand , i would greatly appreciate it, thanks in adavance
Last edited on
1) why wouldn't i just use using namespace sf; and not have to do sf:: every time?


That's certainly an option. The reason I don't recommend it is because it defeats the point of having a namespace in the first place. The namespace is there to prevent accidental name conflicts. If you do using namespace sf; then the namespace gets flooded with every identifier in sf. If you unknowingly use some identifier that sf uses you'll have issues.

2) is Text the type or the object? same with sometext?


Text is a class. sometext is an object/instance of that class.

The way SFML works with drawing is you have several objects that an be drawn. Each object has characteristics, like a position on the screen, rotation, etc, etc. The Text class allows you to create one such object that displays some text. You can then move around 'sometext' as if it were a "thing", rotate it, assign it different colors, etc.

There are other classes like that, too. sf::Sprite is probably the most common. It can be used to drawn portions of an image file to the screen. But it works the same way. You create an sf::Sprite object, assign it a picture, position, rotation, what-have-you, and then you just draw it.

3) Is Draw the member function of myWindow?


Yes. It's actually a member function of sf::RenderWindow (or one of its parent classes). myWindow is just an object of that class -- so you could create several windows and draw to each of them if you wanted.

One of the bigger differences between graphical programs and console programs is that the console manages the window for you. when you do cout << whatever; it just takes whatever you wanted and puts it on the window. And when you do cin >> whatever, it patiently waits for the user to input something, then when they're done, it gives it back to you. If the user clicks the X to close the program, or if they resize the window, your program doesn't care. It doesn't get any notice.

With graphical programs it's different. You are responsible for creating the window, and watching for things like the user wanting to close it. And respond to those events if you care about them (or you can ignore them if you don't care).

But the big part is input. The console makes your program wait around for the user to input stuff, but in a game, you would want other things to keep happening (enemies moving around, things animating, etc). So the overall flow is different. Instead of just saying cin >> foo; and waiting around for the user, you have event and real-time status systems that can be polled while your game does other things.

That page I linked previously has an example of one way to do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
     // Start the game loop
     while (window.IsOpened())
     {
         // 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 the sprite
         window.Draw(sprite);
 
         // Draw the string
         window.Draw(text);
 
         // Update the window
         window.Display();
     }


This is a very simple game loop. It consists of 3 basic parts:

1) process any events
2) run game logic
3) draw the scene

Events are things like the user wanting to close the window (so you can intercept that and save their game -- or prevent them from closing it if the program is in a critical state), resize the window (so you can adjust what is drawn on screen), mouse clicks, keyboard presses, joystick activity, etc. As these things happen, they build up in a "queue" that SFML manages. Periodically you go through the queue and see what events have built up and respond to them. That's what the while (window.PollEvent(event)) loop is doing. As you can see, all he's doing here is closing the window when the user wants to.

There isn't really any game logic in that example. But you could do things like move the text a pixel or two after that loop to have it appear to slowly move across the screen.

After that, you draw your scene. This is done by:
1) clearing the screen
2) drawing all your drawable objects (Text, Sprites, shapes, etc)
3) displaying it

Here, they are drawing 1 text and 1 sprite. So 2 things will be visible on screen.


Throw all that in a loop so that it will keep happening as long as the window is open. Each time it runs, you move the player a little bit, move the enemies a little bit, etc. So the world keeps on changing as the program runs.

4) and my biggest question is why if SFML can make classes do all these things, wouldnt you just do them yourself...


The same reason you use std::vector or std::iostream (cout). Those are classes too.

SFML made them to make it easier for you, so you don't have to reinvent the wheel. You can just use its simplified interface to make your game.

You could redo it all from scratch if you really wanted, but then you'd have to worry about portability issues, and it would introduce bugs, etc, etc.

then you would understand what each class is, why it is, etc.


It's easy to understand each class. There's no shortage of documentation on SFML. The website lists all the available classes, all their functions, and often gives examples of how to use them. If you don't understand it, you can post on the forums (either this forum or the SFML forum) and you'll get a response pretty quick.

i seem to have only one explanation at their website and everyone tells me to go to the tutorials that i didnt understand in the first place.


I'm trying to remedy that. If you still have confusions or questions, you can post and I'm happy to help if I have the time.

i would see cout << and know that it is being outputted to screen


in SFML, if you see window.Draw(foo); you know that 'foo' is being drawn to 'window'. It's not any more complicated, it's just different.

same with cin >> for input


If you see if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Left)) you know the user is currently pressing the Left arrow key. Reference and examples:

http://sfml-dev.org/documentation/2.0/classsf_1_1Keyboard.php

in console to display output is cout and in SFML it is____, but i do not see such


Remember you have to actually create and manage the window yourself in SFML. The console does that for you automatically in console programs. That previous link shows how to do it:

 
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");


That's all it takes. That line of code gives you 'window', which is an 800x600 pixel window, whose title is "SFML window". You can then draw to that window whatever you want with window.Draw( whateveryouwant );


another thing i dont understand is why would they tell noobies to start at console


Beats the hell out of me. I never advocated that. IMO you're much better off starting with what your interests are. If you are interested in making games, then starting with the console is a dumb move, IMO.

I've debated this a few times on the boards. Some people disagree with me, but I stand by that.


cuz then it seems you have to relearn everything?


Yup. It's a huge waste of time and effort. Of all the things you have to learn to make console programs, very little of it transfers over to the world of gaming. I've been saying this for years.
Last edited on
I have to say- for SFML i hate that they are so slow to update the site with SFML 2.0 tutorials, 1.6 doesnt work with AMD graphics cards so i have to use 2.0 :(
Last edited on
2.0 is better anyway.

Even if the tutorial section isn't updated, the documentation is. The docs are really all you should need once you get the general idea. There are even examples scattered throughout the docs.


Though you're right. It would be nice to have more/better tutorials.
iv just started messing around with 2.0 also i think there should be more tuts and so fort but im getting on.glad to know im not lost among the dust as` i do understand most and what i dont i pick up later. and i was just saying how many lines it took just to do simple things in sfml just noteing though its not a prob for me and thanks for the outlook to just look at it as maintaing the window it helped.

but the question that bothers me is how do iknow im not just wasting my time learning this, i want to be a dev and make games/apps across platforms(psp,android.......) how do i know im retaing the proper knowlage to do these things?
and i was just saying how many lines it took just to do simple things in sfml

Huh? Can't confirm that. What exactly do you mean?

but the question that bothers me is how do iknow im not just wasting my time learning this, i want to be a dev and make games/apps across platforms(psp,android.......) how do i know im retaing the proper knowlage to do these things?

SFML is for PCs, so if you want to develop for other devices, you're on the wrong train.
first of all thanks Disch, very descriptive explanation

i tried making my own using examples, but i got stuck with loadig files. i want to just display the text for my first test run. the problem is i decided to switch to graphical from console before learning loading files. i have no idea of which file types are usable in SFML that can be loaded? so i know this is wrong (due to it not loading the file) , but all i did was bring up getit (im using linux) type some text and named it testtext.ttf (in which it changed to a .ttf file) (which i have no idea also what a .ttf file is?) then i threw that file into the files with this .cpp file below , then i just took out their named file and put in my own, but it wont load the file?

What am i doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600),"New Window");

     sf::Font Arial;
     if (!Arial.LoadFromFile("testtext.ttf"))
         return EXIT_FAILURE;
     sf::String Text("Hello SFML", Arial, 50);

    while(window.IsOpened())
    {
         sf::Event Event;
         while (window.GetEvent(Event))
         {
             // Close window : exit
             if (Event.Type == sf::Event::Closed)
                 window.Close();
         }

        window.Clear();

        window.Draw(Text);

        window.Display();

    }

    return EXIT_SUCCESS;

}// end main 
Last edited on
Yah SFML is certainly cross platform but only on computers. If you want to make a game that will run on ANY device your best bet is Java - although not typically known for great games I think Minecraft prooved that even simple games can be amazing.

Also you must note - that alot of cross - platform games are re-written completly for the new platforms, like games that exist on PS3 and Xbox 360
i plan on learning java after C++, but C++ is where i started so i am going to continue it till i get comfortable . besides one month ago i didnt know know how to do the "Hello World" Program in the console ( 5-8 hrs a day) since ive been spending not doing anyhting else but C++, and waiting for college to start. And now im comfortable making mini console games (from 2d arrays to classes) not bad for a month i think, eh?
Last edited on
which i have no idea also what a .ttf file is?

It's a font file. See: http://de.wikipedia.org/wiki/TrueType
But you really, really should know such basics before considering to start programming.

See here for how to read text from a file:
http://www.cplusplus.com/doc/tutorial/files/
yeah i know this just thought where better to start than the pc right. im not worried about that im worried about how do i know at the end of the day when i finished learning the various libraries ect. i will be able to right these various games/apps
yeah i know this just thought where better to start than the pc right. im not worried about that im worried about how do i know at the end of the day when i finished learning the various libraries ect. i will be able to right these various games/apps

Of course, it'll be easy to write games for whichever platform once you have enough general programming experience.
So get to know SFML and write a number of games.
@Athar how do i mix that with SFML?

@zander excluding FORTRAN, B, C (i was told that C++ was the main foundation of programming) and that java, HTML all branched off of C++ (so obviously i started with C++)
Last edited on
@Athar how do i mix that with SFML?

Well, the Text constructor apparently expects a string as its first parameter.
So you pass whatever you want to be printed (such as a line you just read from a file).
im not sure what has to be here or what could be here to display the file testtext.ttf?
1
2
3
4
     sf::Font Arial;
     if (!Arial.LoadFromFile("testtext.txt"))
         return EXIT_FAILURE;
     sf::String Text("Hello SFML", Arial, 50);


console error says: Failed to load font "testtext.txt" (unknown file format)

what is a known file format under SFML?

wouldn't
1
2
 #include <iostream>
#include <fstream> 
read and write to the console not the graphics window????
Last edited on
Pages: 12