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

Author Topic: Some beginer's remarks  (Read 5081 times)

0 Members and 1 Guest are viewing this topic.

bugy

  • Newbie
  • *
  • Posts: 7
    • View Profile
Some beginer's remarks
« on: September 01, 2010, 05:33:18 pm »
Hi,
I'm new to sfml and I found some facts quite interesting.

1)
The line below is ok.
Code: [Select]
sf::RenderWindow object(sf::VideoMode(800,500), "hello world");

but when I try to call that constructor again I get an error (like it doesn't exist):
Code: [Select]
object.RenderWindow(sf::VideoMode(800,500), "hello world");

I looked it up in the documentation and there are two possibilities:
Code: [Select]
sf::RenderWindow::RenderWindow   (   VideoMode   Mode,
const std::string &   Title,
unsigned long   WindowStyle = Style::Resize | Style::Close,
const WindowSettings &   Params = WindowSettings()
)


and

Code: [Select]
sf::RenderWindow::RenderWindow   (   WindowHandle   Handle,
const WindowSettings &   Params = WindowSettings()
)

I wanted to set the title after initializing the object and I finally managed to do it by calling Create()

What's the reason?

2)
The second thing is related to the first one. I mentioned that I wanted to set a title of a window after it's already initialized. There is a special method for this: SetTitle().
But when I try:
object.SetTitle("hello");
I get an error: "error: 'class sf::RenderWindow' has no member named 'SetTitle'|"
But the documentation claim it has.

WHY?

3)
The third thing concerns mouse control
sf::Event::MouseMoved; // this is an event type and it works fine
but:
event.MouseMoveEvent.X; // this causes an error ("invalid use of struct...")
to get  cursor position I had to call:
event.MouseMove.X;

The thing is that auto completion (I use code::blocks) suggests only event.MouseMoveEvent.X
while event.MouseMove.X is "invisible"

It seems a bit strange. Why is it like that?

4)
The last thing is the sign "|" in function arguments - like here:
Code: [Select]
Text.SetStyle(sf::String::Bold | sf::String::Italic | sf::String::Underlined);

I see it has something to do with enums and I'm interested if it is possible to write such a function in pure c++.

It looks like it takes any number of arguments, which are in fact just one. It also seems that the sequence doesn't metter.

Can somebody explain this to me? ( not the use of it, but how to make it) I tried to google it but I couldn't make out good keywords and found nothing helpful.

I appreciate your help

Bye.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Some beginer's remarks
« Reply #1 on: September 01, 2010, 05:41:46 pm »
Quote from: "bugy"
but when I try to call that constructor again I get an error (like it doesn't exist):
You may not call constructors "again". They are used for initialization, and this happens only once an object's lifetime. Besides, you cannot call constructors explicitly apart from placement new.

Quote from: "bugy"
I get an error: "error: 'class sf::RenderWindow' has no member named 'SetTitle'|"
But the documentation claim it has.
Then you're probably looking at a documentation that doesn't correspond to your SFML version.

Quote from: "bugy"
It seems a bit strange. Why is it like that?
Bad auto-completion of Code::Blocks? Obviously, event.MouseMoveEvent.X is no valid C++.

Quote from: "bugy"
Can somebody explain this to me?
The arguments are called flags. They can be combined with the bitwise or operator. You could for example google for "c++ bitwise flags".
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

bugy

  • Newbie
  • *
  • Posts: 7
    • View Profile
Some beginer's remarks
« Reply #2 on: September 01, 2010, 07:36:50 pm »
Thanks for fast reply.

Quote
bugy wrote:
I get an error: "error: 'class sf::RenderWindow' has no member named 'SetTitle'|"
But the documentation claim it has.
Then you're probably looking at a documentation that doesn't correspond to your SFML version.


I'm using sfml 1.6 and I'm using proper documentation. Seems there must be a bug.



I have one more problem.

I want to have some global variables, available from all *.cpp files. I created a structure with them and placed it over main():
Code: [Select]

Resources res;

int main()
{


that's the structure:
Code: [Select]
struct Resources
{
    public:
    sf::RenderWindow App;
    sf::Image Image;
    sf::Sprite Sprite;
    sf::String DebugTextBlock;
};


The problem occurs when I want to draw DebugTextBlock - it simply doesn't show up.. below there is a part of another *.cpp file:
Code: [Select]

    res.DebugTextBlock.SetFont(sf::Font::GetDefaultFont());
    res.DebugTextBlock.SetText("---------------");//buffer.str());
    res.DebugTextBlock.SetSize(50);
      res.DebugTextBlock.SetCenter(res.DebugTextBlock.GetRect().GetWidth(), 0);
    res.DebugTextBlock.SetColor(sf::Color(100, 100, 100));
    res.DebugTextBlock.SetRotation(0.f);
    res.DebugTextBlock.SetScale(1.f, 1.f);
    res.DebugTextBlock.Move(780.f, 10.f);

    res.App.Draw(res.DebugTextBlock);


If I change the code a little bit and declare the object right above this code than it works.

I think the problem is with initialization of that DebugTextBlock inside the structure, but I can't fix it. Help me please.[/quote]

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Some beginer's remarks
« Reply #3 on: September 01, 2010, 10:07:34 pm »
Quote from: "bugy"
I want to have some global variables, available from all *.cpp files. I created a structure with them and placed it over main()
That's very bad design, which is going to lead to troublesome problems, as the project grows. Nearly nothing needs to be everywhere. You should keep things as local as possible and specify class relationships. This might look like more effort at the beginning, but it certainly pays in the long term.

The initialization order of global variables in different modules is undefined. If you have globals that depend on other global state which is possibly not initialized yet, undefined behaviour can occur. Although I'm not sure this is the case here, the different behaviour in local and global memory area is a strong indicator. By the way, that's just one of the problems of global variables. Try to avoid them.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

bugy

  • Newbie
  • *
  • Posts: 7
    • View Profile
Some beginer's remarks
« Reply #4 on: September 01, 2010, 10:56:28 pm »
Ok, I would like it this way, but I can't find another solution.  For example textures? They are used in many modules - which means different filse.
1) Must be loaded
2) In Draw()
3) In function responsible for detecting collisions
4) Maybe some others

Do you have any ides how to handle this?

To be honest, I really dislike my idea with that structure. I used to work with c# before and there was a good solution: static class

I also don't like the idea of using extern variables, because it requires 2 "copies" of whole set of variables. One in main.cpp and another in a hearer file.

Like I said something like c# static class would be perfect :D

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Some beginer's remarks
« Reply #5 on: September 01, 2010, 11:07:28 pm »
First, you should group your variables according to their area of responsibility. A sprite and a debug text block are certainly no resources, so either rename the class or split it. For example, what does the sf::Sprite represent? Your answer probably tells you where it should belong.

It's not uncommon that something is used in different modules. But a global variable is only in the rarest cases an acceptable solution. Mostly, it results from the apparent easyness, which is an illusion. Why don't you pass your texture (respectively a pointer or reference) to the functions that need it? Depending on your design, you can also pass it to the constructor, if there is a whole class taking care of it.

And no, static class is everything but perfect for this. It brings many of the issues of global variables, but looks nicer. Beware of being too euphoric about such "object-oriented solutions" (in C++, the singleton can be tempting, for example), they're not always better. Your variables don't need to be static!
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

bugy

  • Newbie
  • *
  • Posts: 7
    • View Profile
Some beginer's remarks
« Reply #6 on: September 02, 2010, 10:42:00 am »
Well, than I'll try with the pointers. :)

I have the last one question for now. As you rightly pointed out DebugTextBlock has nothing to do with the resources. Well, that was just a temporary solution, and I wanted to put it in another structure later. But after your recent post, I'm not sure if it makes sense to have this kind of global element.

This object could be created any time it's needed. In my case, a few times per second (before each screen refresh), or it could be created only once and stored somewhere. Which option would be better? (I'm concerned about the time consumed during initialization.)

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Some beginer's remarks
« Reply #7 on: September 02, 2010, 11:17:25 am »
Quote from: "bugy"
I'm using sfml 1.6 and I'm using proper documentation. Seems there must be a bug.
Nop, SetTitle is in SFML 2.0. You must use the bad documentation, be careful while going to the list.

I just verified...
Mindiell
----

bugy

  • Newbie
  • *
  • Posts: 7
    • View Profile
Some beginer's remarks
« Reply #8 on: September 02, 2010, 11:53:07 am »
Sorry, You're right. I must have found it through "Search", because I've never consciously opened 2.0 documentation. :oops:

Kingdom of Fish

  • Jr. Member
  • **
  • Posts: 72
    • View Profile
Some beginer's remarks
« Reply #9 on: September 03, 2010, 02:06:41 pm »
You should really start this out by googling object oriented design and reading up on how c++ works.

Object orientation:
http://www.oodesign.com/ - This seems like a pretty awesome page but I haven't used it myself... yet.
http://download.oracle.com/javase/tutorial/java/concepts/ - This is Java but the theories are applicable.

C++:
http://www.cplusplus.com/doc/tutorial/ - My goto page when it comes to c++.