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

Author Topic: Define structure with a sf::RenderWindow in it.  (Read 3075 times)

0 Members and 1 Guest are viewing this topic.

Jacob309

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Define structure with a sf::RenderWindow in it.
« on: March 20, 2020, 11:36:37 pm »
So im trying to define a structure that has a render window in it. Having this is not necessary to my program but I would like to learn how to do this if its possible because I am obviously trying to do it wrong.

My code:
    typedef struct KeyWindowStruct {
        sf::RenderWindow;
    }KeyWindow;

Thanks for your help.
« Last Edit: March 21, 2020, 02:19:08 am by Jacob309 »

lavinrp

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Define structure with a sf::RenderWindow in it.
« Reply #1 on: March 21, 2020, 09:09:25 am »
Hi Jacob,

Short version: Yes its possible. You just need to give the sf::RenderWindow a name

Long version:
Lets look at this with an int instead of a sf::RenderWindow.

Example: https://godbolt.org/z/dHcGaj
Code: [Select]
typedef struct KeyIntStruct {
    int;
} KeyInt;

This fails to compile with the error "declaration does not declare anything" (on gcc). What that error is really telling us is that we haven't given the int a name. The compiler knows that we want to declare an int, but it doesn't know what to call that int (there could even be more than one).

Lets give the int a name. https://godbolt.org/z/Uj4GJ6
Code: [Select]
typedef struct KeyIntStruct {
    int key;
} KeyInt;

// This isn't important. Just here to make the compiler happy.
int main()
{
}
Now everything compiles fine. We've told the compiler that we want an int named "key".

This next change is more just a preference. It brings things more in line with idiomatic C++ instead of C
(and saves me some reading and typing time)
Here is a good description of why this is Ok in C++ https://stackoverflow.com/a/612350
https://godbolt.org/z/GNWYGS
Code: [Select]
struct KeyInt {
    int key;
};

// This isn't important. Just here to make the compiler happy.
int main()
{
}

Now lets reintroduce RenderWindow
Code: [Select]
struct KeyWindow {
    sf::RenderWindow window;
};

int main()
{
}

I would also recommend further looking into the conventional differences between classes and structs. https://www.fluentcpp.com/2017/06/13/the-real-difference-between-struct-class/

Jacob309

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Define structure with a sf::RenderWindow in it.
« Reply #2 on: March 21, 2020, 10:40:06 pm »
Holy fuck i'm a dumbass.  Anyway I have one more question this time hopefully its less stupid. So i'm trying to get multiple windows open at once and im trying to do that by having a void that functions like the while loop would. It has a for loop that goes through a array of my structure key.

Key
    typedef struct KeyStruct {
        sf::Image Img;
        sf::Texture Tex;
        sf::Sprite Sprite;
        sf::RenderWindow* Window;//pointer because it is uncopyable
    }NewKey;

This is my void
 
   static void StepWindows()
    {
        sf::Clock clock;
        int i;
        for (i = 0; i > KeyArray.size(); i++)
            MakeTopWindow(KeyArray[i].Window);
            setShape(KeyArray[i].Window, KeyArray[i].Img);
            KeyArray[i].Window.clear(sf::Color::Transparent);
            KeyArray[i].Window.draw(KeyArray[i].Sprite);
            KeyArray[i].Window.display();
            if (clock.getElapsedTime().asMicroseconds() > 1000)
            {
                KeyArray[i].Window.setPosition(MakeKey::Gravity(KeyArray[i].Window, KeyArray[i]));
            }
    }

When Ever I try to use KeyArray.Window it says expression must have class type. How do I fix this?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Define structure with a sf::RenderWindow in it.
« Reply #3 on: March 22, 2020, 07:30:52 am »
KeyArray[i].Window is a pointer, you must access its members with -> not .

I strongly suggest that you learn C++ with proper resources (books, online tutorials, ...), we can't teach you this complex language on the SFML forum -- and, as you've probably noticed, your problems are not related to SFML at all ;)
Laurent Gomila - SFML developer