5 Replies - 162 Views - Last Post: 05 February 2012 - 05:21 PM Rate Topic: -----

Topic Sponsor:

#1 Crockeo  Icon User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 229
  • Joined: 21-June 11

Initializing Classes Post Declaration

Posted 05 February 2012 - 01:04 PM

I've been working with the library SFML recently, and all of their tutorials are very C-looking (instead of doing OOP; with their examples they cram it all into the main function (even then, that's bad for C)). What I've been trying to do is make it more OO, as in create a Game class with an init, update, and render function (switching over from Slick does that), with of course, a constructor.

But with the main window class they use (sf::RenderWindow) I haven't found a way to declare it for the whole class, then initialize it within the init function (or the constructor, for that matter)

Here's what I've tried so far, but I've just been stumbling around in the dark, to be honest:
class Game {
public:
    Game(int Width, int Height, std::string title) {
        App(sf::VideoMode(Width, Height), title.c_str()); // I've tried this
        App = sf::RenderWindow(sf::VideoMode(Width, Height), title.c_str()); // This
        App.RenderWindow(sf::VideoMode(Width, Height), title.c_str()); // And this (not at once, of course)
    }

private:
    sf::RenderWindow App;
    ...
};



So that's what I've tried so far, and I can't really think of anything else.
As you can probably tell I'm still very much an amateur to C++, so please help someone just getting back to C++.

Source for the RenderWindow class: http://www.sfml-dev....8hpp_source.php

Thank you!
~Crockeo

Is This A Good Question/Topic? 0
  • +

Replies To: Initializing Classes Post Declaration

#2 ishkabible  Icon User is online

  • spelling expert
  • member icon



Reputation: 1142
  • View blog
  • Posts: 4,783
  • Joined: 03-August 09

Re: Initializing Classes Post Declaration

Posted 05 February 2012 - 01:18 PM

you have a few options here;

1) initialize it in the constructor list(probably the best option)
Game(int Width, int Height, std::string title) 
    : App(sf::VideoMode(Width, Height), title.c_str()) {
    //your other code goes here
}


2) dynamically allocate the class and call it's constructor with 'new'
3) if you can use the '=' operator you can do what you did on line 5 but if you can't use the = operator(declared private or doesn't behave as expected) then that option is out
4) you can set the attributes separately using the classes methods assuming it has them

option 1 is probably your best option.
Was This Post Helpful? 1
  • +
  • -

#3 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 301
  • View blog
  • Posts: 1,039
  • Joined: 31-December 10

Re: Initializing Classes Post Declaration

Posted 05 February 2012 - 01:39 PM

Where are you putting the main "game" loop, the loop that handles events and clears the window and then re-draws it?
Was This Post Helpful? 0
  • +
  • -

#4 Crockeo  Icon User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 229
  • Joined: 21-June 11

Re: Initializing Classes Post Declaration

Posted 05 February 2012 - 02:16 PM

@ishkabible: I'll try it, but could you explain what that really does, like in the inner workings?

@vividexstance: While that's not related to my question, this is my source code so far (I've just been working on it)
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>

class Game {
public:
    Game(int Width, int Height, std::string title) {
        App = sf::RenderWindow(sf::VideoMode(Width, Height), title.c_str());
    }

private:
    sf::RenderWindow App;

    void init() {
        //Put any pre-loop declarations here
        
        //Starting l'loop
        loop();
    }

    //L'Loop
    void loop() {
        while (App.IsOpened()) {
            update();
            render();
        }
    }

    //L'Logic
    void update() {
        //L'Event Handling
        sf::Event Event;
        while (App.GetEvent(Event)) {
            
        }
        
        //L'Other stuff
    }

    //L'Graphics
    void render() {
        //L'Clearing the screen
        App.Clear();
        
        //L'Painting what needs to be painted
        

        //L'Updating the screen
        App.Display();
    }
};



As I said, it isn't really buffed out, but it's just a skeleton.
Was This Post Helpful? 0
  • +
  • -

#5 ishkabible  Icon User is online

  • spelling expert
  • member icon



Reputation: 1142
  • View blog
  • Posts: 4,783
  • Joined: 03-August 09

Re: Initializing Classes Post Declaration

Posted 05 February 2012 - 04:12 PM

Quote

@ishkabible: I'll try it, but could you explain what that really does, like in the inner workings?


each time an object is constructed it's members need to be constructed too. to specify how to construct them you can use the constructor list which starts with a ':' and ends in the definition block. it's the syntax to construct members of a class basically. beyond that it works exactly the same way as if you constructed any other variable; the constructor is called.

in the event that you don't construct them; it uses the default constructor.

This post has been edited by ishkabible: 05 February 2012 - 04:13 PM

Was This Post Helpful? 1
  • +
  • -

#6 Crockeo  Icon User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 229
  • Joined: 21-June 11

Re: Initializing Classes Post Declaration

Posted 05 February 2012 - 05:21 PM

Ah, cool, thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1