How can I declare a SFML window to all my class functions? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How do you declare an interface in C++?How to call a parent class function from derived class function?How can I profile C++ code running on Linux?What techniques can be used to define a class in JavaScript, and what are their trade-offs?When can I use a forward declaration?Meaning of 'const' last in a function declaration of a class?What is meant with “const” at end of function declaration?Interview: Can we instantiate abstract class?Unresolved Externals using Cmake and Visual Studio 2013SFML C++ not rendering

Dating a Former Employee

What is the appropriate index architecture when forced to implement IsDeleted (soft deletes)?

An adverb for when you're not exaggerating

How do I find out the mythology and history of my Fortress?

Crossing US/Canada Border for less than 24 hours

What is a fractional matching?

Why do we bend a book to keep it straight?

How to react to hostile behavior from a senior developer?

Using audio cues to encourage good posture

Most bit efficient text communication method?

How do I use the new nonlinear finite element in Mathematica 12 for this equation?

The code below, is it ill-formed NDR or is it well formed?

Generate an RGB colour grid

How does light 'choose' between wave and particle behaviour?

If Windows 7 doesn't support WSL, then what does Linux subsystem option mean?

Central Vacuuming: Is it worth it, and how does it compare to normal vacuuming?

Is there any word for a place full of confusion?

Effects on objects due to a brief relocation of massive amounts of mass

Do I really need to have a message in a novel to appeal to readers?

Question about debouncing - delay of state change

Drawing without replacement: why is the order of draw irrelevant?

How much damage would a cupful of neutron star matter do to the Earth?

Selecting user stories during sprint planning

Why weren't discrete x86 CPUs ever used in game hardware?



How can I declare a SFML window to all my class functions?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How do you declare an interface in C++?How to call a parent class function from derived class function?How can I profile C++ code running on Linux?What techniques can be used to define a class in JavaScript, and what are their trade-offs?When can I use a forward declaration?Meaning of 'const' last in a function declaration of a class?What is meant with “const” at end of function declaration?Interview: Can we instantiate abstract class?Unresolved Externals using Cmake and Visual Studio 2013SFML C++ not rendering



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2















This might be a noob question, but I'm trying to make a simple player move in a 2D-grid in SFML. I'm creating a while loop for rendering what is happening, and I can get it to work, but I want to use classes for the grid and players etc. The problem is that when I create a window called 'window', I don't know how to implement classes since these don't know what the 'window' is. I hope I have described my problem sufficiently, and I would like any respons on how to make this work or if my method of doing it is already bad and should be changed for another method. Here is a snippet of my code for the class and the undeclared window error.



class myEvents 
public:
//Variables
int tSize = 40;
int tileCount = 20;
int width = tileCount * tSize;
int height = tileCount * tSize;

//Function to create a grid with RectangleShapes
void grid()
for (int i = 0; i < tileCount; i++)

for (int j = 0; j < tileCount; j++)

sf::RectangleShape tile(sf::Vector2f(40, 40));
tile.setFillColor(sf::Color::Magenta);
tile.setPosition(i*tSize, j*tSize);
window.draw(tile); //Problem occurs here, 'window' is not declared, it is in the next function
//window.draw(tile); must execute in the loop to render a full grid




//Includes while loop for rendering and events. Could be written without class, but I'd still like a class for the grid and later on a player.
//So I need the window to work with my classes.
void loop()
sf::RenderWindow window(sf::VideoMode(width, height), "Game"); //'window' declared here. Can I move this statement
//somewhere so that my funcions know where it comes from?
while (window.isOpen)

sf::Event event;
while (window.pollEvent(e))

//to be further developed



;









share|improve this question
























  • You can create the window at some higher scope where all classes can access it, but I'd advise against it. If you have a lot of different places that try to draw into the same window tracking down bugs will be a huge pain. You should consider having a single class that is responsible for drawing (and that the other classes can call into) instead

    – UnholySheep
    Mar 8 at 20:23











  • Pass the window to your classes.

    – Jesper Juhl
    Mar 8 at 20:23

















2















This might be a noob question, but I'm trying to make a simple player move in a 2D-grid in SFML. I'm creating a while loop for rendering what is happening, and I can get it to work, but I want to use classes for the grid and players etc. The problem is that when I create a window called 'window', I don't know how to implement classes since these don't know what the 'window' is. I hope I have described my problem sufficiently, and I would like any respons on how to make this work or if my method of doing it is already bad and should be changed for another method. Here is a snippet of my code for the class and the undeclared window error.



class myEvents 
public:
//Variables
int tSize = 40;
int tileCount = 20;
int width = tileCount * tSize;
int height = tileCount * tSize;

//Function to create a grid with RectangleShapes
void grid()
for (int i = 0; i < tileCount; i++)

for (int j = 0; j < tileCount; j++)

sf::RectangleShape tile(sf::Vector2f(40, 40));
tile.setFillColor(sf::Color::Magenta);
tile.setPosition(i*tSize, j*tSize);
window.draw(tile); //Problem occurs here, 'window' is not declared, it is in the next function
//window.draw(tile); must execute in the loop to render a full grid




//Includes while loop for rendering and events. Could be written without class, but I'd still like a class for the grid and later on a player.
//So I need the window to work with my classes.
void loop()
sf::RenderWindow window(sf::VideoMode(width, height), "Game"); //'window' declared here. Can I move this statement
//somewhere so that my funcions know where it comes from?
while (window.isOpen)

sf::Event event;
while (window.pollEvent(e))

//to be further developed



;









share|improve this question
























  • You can create the window at some higher scope where all classes can access it, but I'd advise against it. If you have a lot of different places that try to draw into the same window tracking down bugs will be a huge pain. You should consider having a single class that is responsible for drawing (and that the other classes can call into) instead

    – UnholySheep
    Mar 8 at 20:23











  • Pass the window to your classes.

    – Jesper Juhl
    Mar 8 at 20:23













2












2








2








This might be a noob question, but I'm trying to make a simple player move in a 2D-grid in SFML. I'm creating a while loop for rendering what is happening, and I can get it to work, but I want to use classes for the grid and players etc. The problem is that when I create a window called 'window', I don't know how to implement classes since these don't know what the 'window' is. I hope I have described my problem sufficiently, and I would like any respons on how to make this work or if my method of doing it is already bad and should be changed for another method. Here is a snippet of my code for the class and the undeclared window error.



class myEvents 
public:
//Variables
int tSize = 40;
int tileCount = 20;
int width = tileCount * tSize;
int height = tileCount * tSize;

//Function to create a grid with RectangleShapes
void grid()
for (int i = 0; i < tileCount; i++)

for (int j = 0; j < tileCount; j++)

sf::RectangleShape tile(sf::Vector2f(40, 40));
tile.setFillColor(sf::Color::Magenta);
tile.setPosition(i*tSize, j*tSize);
window.draw(tile); //Problem occurs here, 'window' is not declared, it is in the next function
//window.draw(tile); must execute in the loop to render a full grid




//Includes while loop for rendering and events. Could be written without class, but I'd still like a class for the grid and later on a player.
//So I need the window to work with my classes.
void loop()
sf::RenderWindow window(sf::VideoMode(width, height), "Game"); //'window' declared here. Can I move this statement
//somewhere so that my funcions know where it comes from?
while (window.isOpen)

sf::Event event;
while (window.pollEvent(e))

//to be further developed



;









share|improve this question
















This might be a noob question, but I'm trying to make a simple player move in a 2D-grid in SFML. I'm creating a while loop for rendering what is happening, and I can get it to work, but I want to use classes for the grid and players etc. The problem is that when I create a window called 'window', I don't know how to implement classes since these don't know what the 'window' is. I hope I have described my problem sufficiently, and I would like any respons on how to make this work or if my method of doing it is already bad and should be changed for another method. Here is a snippet of my code for the class and the undeclared window error.



class myEvents 
public:
//Variables
int tSize = 40;
int tileCount = 20;
int width = tileCount * tSize;
int height = tileCount * tSize;

//Function to create a grid with RectangleShapes
void grid()
for (int i = 0; i < tileCount; i++)

for (int j = 0; j < tileCount; j++)

sf::RectangleShape tile(sf::Vector2f(40, 40));
tile.setFillColor(sf::Color::Magenta);
tile.setPosition(i*tSize, j*tSize);
window.draw(tile); //Problem occurs here, 'window' is not declared, it is in the next function
//window.draw(tile); must execute in the loop to render a full grid




//Includes while loop for rendering and events. Could be written without class, but I'd still like a class for the grid and later on a player.
//So I need the window to work with my classes.
void loop()
sf::RenderWindow window(sf::VideoMode(width, height), "Game"); //'window' declared here. Can I move this statement
//somewhere so that my funcions know where it comes from?
while (window.isOpen)

sf::Event event;
while (window.pollEvent(e))

//to be further developed



;






c++ class sfml variable-declaration






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 20:25









Guillaume Racicot

16.6k53872




16.6k53872










asked Mar 8 at 20:19









Rasmus HRasmus H

134




134












  • You can create the window at some higher scope where all classes can access it, but I'd advise against it. If you have a lot of different places that try to draw into the same window tracking down bugs will be a huge pain. You should consider having a single class that is responsible for drawing (and that the other classes can call into) instead

    – UnholySheep
    Mar 8 at 20:23











  • Pass the window to your classes.

    – Jesper Juhl
    Mar 8 at 20:23

















  • You can create the window at some higher scope where all classes can access it, but I'd advise against it. If you have a lot of different places that try to draw into the same window tracking down bugs will be a huge pain. You should consider having a single class that is responsible for drawing (and that the other classes can call into) instead

    – UnholySheep
    Mar 8 at 20:23











  • Pass the window to your classes.

    – Jesper Juhl
    Mar 8 at 20:23
















You can create the window at some higher scope where all classes can access it, but I'd advise against it. If you have a lot of different places that try to draw into the same window tracking down bugs will be a huge pain. You should consider having a single class that is responsible for drawing (and that the other classes can call into) instead

– UnholySheep
Mar 8 at 20:23





You can create the window at some higher scope where all classes can access it, but I'd advise against it. If you have a lot of different places that try to draw into the same window tracking down bugs will be a huge pain. You should consider having a single class that is responsible for drawing (and that the other classes can call into) instead

– UnholySheep
Mar 8 at 20:23













Pass the window to your classes.

– Jesper Juhl
Mar 8 at 20:23





Pass the window to your classes.

– Jesper Juhl
Mar 8 at 20:23












2 Answers
2






active

oldest

votes


















0














Well an easy thing for the window you can do is to create a class and
add the window in it, then include the .h where the class is as you've set the sf::window in the public section. Now I will show you how you can use it



we've got an .h file named window.h (you can name it whatever is comfortable for you)



#include <SFML/Grpahics.hpp>
class window

public:
sf::RenderWindow window;



and you have the main.cpp(or whatever file you want)



#include "window.h"
window w;

int main()

w.window.create(sf::VideoMode(800,600),"Title");



the int main() can be whatever function you want it just has to be
called as soon as possible so the window will show.
I hope this helps you with your problem =)
[EDID]:
As the "sf::RenderWindow window" is public every file that has the header with the public "sf::RenderWindow window" can use the window as it is not private. I think you know that but in any case to add it.






share|improve this answer

























  • Okay, thanks! This cleared some things up for me, but will have to test it as I get home to the computer. Thanks!

    – Rasmus H
    Mar 10 at 17:50











  • I hope it's what you need =)

    – xX randomryze Xx
    Mar 10 at 18:36











  • I tried it and it worked, thanks!

    – Rasmus H
    Mar 11 at 19:54


















1














Try making the window a class member:



class myEvents 
public:
//Variables
int tSize = 40;
int tileCount = 20;
int width = tileCount * tSize;
int height = tileCount * tSize;
sf::RenderWindow windowsf::VideoMode(width, height), "Game";

void grid()
for (int i = 0; i < tileCount; i++)

for (int j = 0; j < tileCount; j++)

sf::RectangleShape tile(sf::Vector2f(40, 40));
tile.setFillColor(sf::Color::Magenta);
tile.setPosition(i*tSize, j*tSize);
window.draw(tile);




void loop()
while (window.isOpen)

sf::Event event;
while (window.pollEvent(e))

//to be further developed



;


Alternatively, pass the window around:



class myEvents 
public:
//Variables
int tSize = 40;
int tileCount = 20;
int width = tileCount * tSize;
int height = tileCount * tSize;

void grid(sf::RenderWindow& window)
for (int i = 0; i < tileCount; i++)

for (int j = 0; j < tileCount; j++)

sf::RectangleShape tile(sf::Vector2f(40, 40));
tile.setFillColor(sf::Color::Magenta);
tile.setPosition(i*tSize, j*tSize);
window.draw(tile);




void loop()
sf::RenderWindow windowsf::VideoMode(width, height), "Game";

while (window.isOpen)

sf::Event event;
while (window.pollEvent(e))

//to be further developed
// call grid(window)



;





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55070452%2fhow-can-i-declare-a-sfml-window-to-all-my-class-functions%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Well an easy thing for the window you can do is to create a class and
    add the window in it, then include the .h where the class is as you've set the sf::window in the public section. Now I will show you how you can use it



    we've got an .h file named window.h (you can name it whatever is comfortable for you)



    #include <SFML/Grpahics.hpp>
    class window

    public:
    sf::RenderWindow window;



    and you have the main.cpp(or whatever file you want)



    #include "window.h"
    window w;

    int main()

    w.window.create(sf::VideoMode(800,600),"Title");



    the int main() can be whatever function you want it just has to be
    called as soon as possible so the window will show.
    I hope this helps you with your problem =)
    [EDID]:
    As the "sf::RenderWindow window" is public every file that has the header with the public "sf::RenderWindow window" can use the window as it is not private. I think you know that but in any case to add it.






    share|improve this answer

























    • Okay, thanks! This cleared some things up for me, but will have to test it as I get home to the computer. Thanks!

      – Rasmus H
      Mar 10 at 17:50











    • I hope it's what you need =)

      – xX randomryze Xx
      Mar 10 at 18:36











    • I tried it and it worked, thanks!

      – Rasmus H
      Mar 11 at 19:54















    0














    Well an easy thing for the window you can do is to create a class and
    add the window in it, then include the .h where the class is as you've set the sf::window in the public section. Now I will show you how you can use it



    we've got an .h file named window.h (you can name it whatever is comfortable for you)



    #include <SFML/Grpahics.hpp>
    class window

    public:
    sf::RenderWindow window;



    and you have the main.cpp(or whatever file you want)



    #include "window.h"
    window w;

    int main()

    w.window.create(sf::VideoMode(800,600),"Title");



    the int main() can be whatever function you want it just has to be
    called as soon as possible so the window will show.
    I hope this helps you with your problem =)
    [EDID]:
    As the "sf::RenderWindow window" is public every file that has the header with the public "sf::RenderWindow window" can use the window as it is not private. I think you know that but in any case to add it.






    share|improve this answer

























    • Okay, thanks! This cleared some things up for me, but will have to test it as I get home to the computer. Thanks!

      – Rasmus H
      Mar 10 at 17:50











    • I hope it's what you need =)

      – xX randomryze Xx
      Mar 10 at 18:36











    • I tried it and it worked, thanks!

      – Rasmus H
      Mar 11 at 19:54













    0












    0








    0







    Well an easy thing for the window you can do is to create a class and
    add the window in it, then include the .h where the class is as you've set the sf::window in the public section. Now I will show you how you can use it



    we've got an .h file named window.h (you can name it whatever is comfortable for you)



    #include <SFML/Grpahics.hpp>
    class window

    public:
    sf::RenderWindow window;



    and you have the main.cpp(or whatever file you want)



    #include "window.h"
    window w;

    int main()

    w.window.create(sf::VideoMode(800,600),"Title");



    the int main() can be whatever function you want it just has to be
    called as soon as possible so the window will show.
    I hope this helps you with your problem =)
    [EDID]:
    As the "sf::RenderWindow window" is public every file that has the header with the public "sf::RenderWindow window" can use the window as it is not private. I think you know that but in any case to add it.






    share|improve this answer















    Well an easy thing for the window you can do is to create a class and
    add the window in it, then include the .h where the class is as you've set the sf::window in the public section. Now I will show you how you can use it



    we've got an .h file named window.h (you can name it whatever is comfortable for you)



    #include <SFML/Grpahics.hpp>
    class window

    public:
    sf::RenderWindow window;



    and you have the main.cpp(or whatever file you want)



    #include "window.h"
    window w;

    int main()

    w.window.create(sf::VideoMode(800,600),"Title");



    the int main() can be whatever function you want it just has to be
    called as soon as possible so the window will show.
    I hope this helps you with your problem =)
    [EDID]:
    As the "sf::RenderWindow window" is public every file that has the header with the public "sf::RenderWindow window" can use the window as it is not private. I think you know that but in any case to add it.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 8 at 22:01

























    answered Mar 8 at 21:56









    xX randomryze XxxX randomryze Xx

    316




    316












    • Okay, thanks! This cleared some things up for me, but will have to test it as I get home to the computer. Thanks!

      – Rasmus H
      Mar 10 at 17:50











    • I hope it's what you need =)

      – xX randomryze Xx
      Mar 10 at 18:36











    • I tried it and it worked, thanks!

      – Rasmus H
      Mar 11 at 19:54

















    • Okay, thanks! This cleared some things up for me, but will have to test it as I get home to the computer. Thanks!

      – Rasmus H
      Mar 10 at 17:50











    • I hope it's what you need =)

      – xX randomryze Xx
      Mar 10 at 18:36











    • I tried it and it worked, thanks!

      – Rasmus H
      Mar 11 at 19:54
















    Okay, thanks! This cleared some things up for me, but will have to test it as I get home to the computer. Thanks!

    – Rasmus H
    Mar 10 at 17:50





    Okay, thanks! This cleared some things up for me, but will have to test it as I get home to the computer. Thanks!

    – Rasmus H
    Mar 10 at 17:50













    I hope it's what you need =)

    – xX randomryze Xx
    Mar 10 at 18:36





    I hope it's what you need =)

    – xX randomryze Xx
    Mar 10 at 18:36













    I tried it and it worked, thanks!

    – Rasmus H
    Mar 11 at 19:54





    I tried it and it worked, thanks!

    – Rasmus H
    Mar 11 at 19:54













    1














    Try making the window a class member:



    class myEvents 
    public:
    //Variables
    int tSize = 40;
    int tileCount = 20;
    int width = tileCount * tSize;
    int height = tileCount * tSize;
    sf::RenderWindow windowsf::VideoMode(width, height), "Game";

    void grid()
    for (int i = 0; i < tileCount; i++)

    for (int j = 0; j < tileCount; j++)

    sf::RectangleShape tile(sf::Vector2f(40, 40));
    tile.setFillColor(sf::Color::Magenta);
    tile.setPosition(i*tSize, j*tSize);
    window.draw(tile);




    void loop()
    while (window.isOpen)

    sf::Event event;
    while (window.pollEvent(e))

    //to be further developed



    ;


    Alternatively, pass the window around:



    class myEvents 
    public:
    //Variables
    int tSize = 40;
    int tileCount = 20;
    int width = tileCount * tSize;
    int height = tileCount * tSize;

    void grid(sf::RenderWindow& window)
    for (int i = 0; i < tileCount; i++)

    for (int j = 0; j < tileCount; j++)

    sf::RectangleShape tile(sf::Vector2f(40, 40));
    tile.setFillColor(sf::Color::Magenta);
    tile.setPosition(i*tSize, j*tSize);
    window.draw(tile);




    void loop()
    sf::RenderWindow windowsf::VideoMode(width, height), "Game";

    while (window.isOpen)

    sf::Event event;
    while (window.pollEvent(e))

    //to be further developed
    // call grid(window)



    ;





    share|improve this answer



























      1














      Try making the window a class member:



      class myEvents 
      public:
      //Variables
      int tSize = 40;
      int tileCount = 20;
      int width = tileCount * tSize;
      int height = tileCount * tSize;
      sf::RenderWindow windowsf::VideoMode(width, height), "Game";

      void grid()
      for (int i = 0; i < tileCount; i++)

      for (int j = 0; j < tileCount; j++)

      sf::RectangleShape tile(sf::Vector2f(40, 40));
      tile.setFillColor(sf::Color::Magenta);
      tile.setPosition(i*tSize, j*tSize);
      window.draw(tile);




      void loop()
      while (window.isOpen)

      sf::Event event;
      while (window.pollEvent(e))

      //to be further developed



      ;


      Alternatively, pass the window around:



      class myEvents 
      public:
      //Variables
      int tSize = 40;
      int tileCount = 20;
      int width = tileCount * tSize;
      int height = tileCount * tSize;

      void grid(sf::RenderWindow& window)
      for (int i = 0; i < tileCount; i++)

      for (int j = 0; j < tileCount; j++)

      sf::RectangleShape tile(sf::Vector2f(40, 40));
      tile.setFillColor(sf::Color::Magenta);
      tile.setPosition(i*tSize, j*tSize);
      window.draw(tile);




      void loop()
      sf::RenderWindow windowsf::VideoMode(width, height), "Game";

      while (window.isOpen)

      sf::Event event;
      while (window.pollEvent(e))

      //to be further developed
      // call grid(window)



      ;





      share|improve this answer

























        1












        1








        1







        Try making the window a class member:



        class myEvents 
        public:
        //Variables
        int tSize = 40;
        int tileCount = 20;
        int width = tileCount * tSize;
        int height = tileCount * tSize;
        sf::RenderWindow windowsf::VideoMode(width, height), "Game";

        void grid()
        for (int i = 0; i < tileCount; i++)

        for (int j = 0; j < tileCount; j++)

        sf::RectangleShape tile(sf::Vector2f(40, 40));
        tile.setFillColor(sf::Color::Magenta);
        tile.setPosition(i*tSize, j*tSize);
        window.draw(tile);




        void loop()
        while (window.isOpen)

        sf::Event event;
        while (window.pollEvent(e))

        //to be further developed



        ;


        Alternatively, pass the window around:



        class myEvents 
        public:
        //Variables
        int tSize = 40;
        int tileCount = 20;
        int width = tileCount * tSize;
        int height = tileCount * tSize;

        void grid(sf::RenderWindow& window)
        for (int i = 0; i < tileCount; i++)

        for (int j = 0; j < tileCount; j++)

        sf::RectangleShape tile(sf::Vector2f(40, 40));
        tile.setFillColor(sf::Color::Magenta);
        tile.setPosition(i*tSize, j*tSize);
        window.draw(tile);




        void loop()
        sf::RenderWindow windowsf::VideoMode(width, height), "Game";

        while (window.isOpen)

        sf::Event event;
        while (window.pollEvent(e))

        //to be further developed
        // call grid(window)



        ;





        share|improve this answer













        Try making the window a class member:



        class myEvents 
        public:
        //Variables
        int tSize = 40;
        int tileCount = 20;
        int width = tileCount * tSize;
        int height = tileCount * tSize;
        sf::RenderWindow windowsf::VideoMode(width, height), "Game";

        void grid()
        for (int i = 0; i < tileCount; i++)

        for (int j = 0; j < tileCount; j++)

        sf::RectangleShape tile(sf::Vector2f(40, 40));
        tile.setFillColor(sf::Color::Magenta);
        tile.setPosition(i*tSize, j*tSize);
        window.draw(tile);




        void loop()
        while (window.isOpen)

        sf::Event event;
        while (window.pollEvent(e))

        //to be further developed



        ;


        Alternatively, pass the window around:



        class myEvents 
        public:
        //Variables
        int tSize = 40;
        int tileCount = 20;
        int width = tileCount * tSize;
        int height = tileCount * tSize;

        void grid(sf::RenderWindow& window)
        for (int i = 0; i < tileCount; i++)

        for (int j = 0; j < tileCount; j++)

        sf::RectangleShape tile(sf::Vector2f(40, 40));
        tile.setFillColor(sf::Color::Magenta);
        tile.setPosition(i*tSize, j*tSize);
        window.draw(tile);




        void loop()
        sf::RenderWindow windowsf::VideoMode(width, height), "Game";

        while (window.isOpen)

        sf::Event event;
        while (window.pollEvent(e))

        //to be further developed
        // call grid(window)



        ;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 8 at 20:28









        Guillaume RacicotGuillaume Racicot

        16.6k53872




        16.6k53872



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55070452%2fhow-can-i-declare-a-sfml-window-to-all-my-class-functions%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            1928 у кіно

            Захаров Федір Захарович

            Ель Греко