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

Author Topic: [solved]Problem with App.IsOpened  (Read 2602 times)

0 Members and 1 Guest are viewing this topic.

asiajek

  • Guest
[solved]Problem with App.IsOpened
« on: March 08, 2011, 07:56:38 pm »
Hi, I'm learning C++ so I wrote a small class to manage SFML easier :-D
Code: [Select]
#ifndef ENGINE_H
#define ENGINE_H
#include "global.h"

class Engine
{
    public:
        Engine(int iWidth, int iHeight, int iBits, const char* sTitle);
        void Update(float fTime);
        void Delete();
        bool GetIsOpened();
        virtual ~Engine();

        float fElapsedTime;
    protected:
    private:
        sf::RenderWindow App;
        sf::Input Input();
        sf::Event Event;
        sf::Clock Clock;
};

#endif // ENGINE_H

Code: [Select]
#include "include/Engine.h"

using namespace sf;

Engine::Engine(int iWidth, int iHeight, int iBits, const char* sTitle)
{
    RenderWindow App(VideoMode(iWidth, iHeight, iBits), sTitle);
    App.SetActive(true);
    std::cout << iWidth << iHeight << iBits << sTitle << std::endl;
}

void Engine::Update(float fTime)
{
    fElapsedTime += Clock.GetElapsedTime();
    Clock.Reset();

    while(App.GetEvent(Event))
    {
        if (Event.Type == Event::Closed || Event.Key.Code == Key::Escape) Engine::Delete();
    }

    App.Clear();
    App.Display();
}

bool Engine::GetIsOpened()
{
  return App.IsOpened();
}

void Engine::Delete()
{
   App.Close();
}

Engine::~Engine()
{
    App.Close();
}

Code: [Select]
#include "include/global.h"
#include "include/Engine.h"

#define fTIME_STEP 0.01f
#define iWIDTH  1024
#define iHEIGHT 768
#define iBITS   32
#define sTITLE  "Panzer-panzer"

using namespace std;

int main()
{
    Engine Engine(iWIDTH, iHEIGHT, iBITS, sTITLE);

    while (Engine.GetIsOpened())
    {
        Engine.Update(fTIME_STEP);
    }

    Engine.~Engine();
    system("Pause");
    return 0;
}

But I have problem with App.IsOpened() function - it returns 0 (false) causing my game to close just after opening (while(Engine.GetIsOpened)). Why is that?
PS. If you saw any other mistakes in the code - write it here. I'm still learning

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
[solved]Problem with App.IsOpened
« Reply #1 on: March 08, 2011, 08:16:47 pm »
because this line of code
Code: [Select]
RenderWindow App(VideoMode(iWidth, iHeight, iBits), sTitle); doesn't do what you expected. ;-)

You're creating here a new variable hiding your object's 'app' attribute. In order to create your window correctly you have to use the initialisation list of the constructor because RenderWindow is non-compiable. (Google it).

So the correct code should looks like :
Code: [Select]
Engine::Engine(int iWidth, int iHeight, int iBits, const char* sTitle)
: App(VideoMode(iWidth, iHeight, iBits), sTitle)
{
}
SFML / OS X developer

asiajek

  • Guest
[solved]Problem with App.IsOpened
« Reply #2 on: March 08, 2011, 08:51:11 pm »
Thank you very much!!! It works just fine!
But, do I need to put this initialization list in the header(declaration) also? I googled some pages but nobody writes about separating declaration and implementation in the context of initialization list.

devlin

  • Full Member
  • ***
  • Posts: 128
    • View Profile
[solved]Problem with App.IsOpened
« Reply #3 on: March 08, 2011, 10:19:25 pm »
No you don't - you can use the initialization list in a cpp-file and just leave the definition in the header file.

This is how you set const-variables within classes for instance.

asiajek

  • Guest
[solved]Problem with App.IsOpened
« Reply #4 on: March 09, 2011, 07:35:05 am »
Thank you! I'll read more about it.
(How can I close the topic? Or should I ask moderators?)
Ok, so, dear Moderators please close this topic

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
[solved]Problem with App.IsOpened
« Reply #5 on: March 09, 2011, 08:13:11 am »
You don't need to close the topic but you can edit your first message and add [solved] at the beginning of the title.  :wink:
SFML / OS X developer

 

anything