Problem with a seperate MainAppWindow class


#1

Hi,

i am racking my brain since hours about the following problem, which i apprently can't solve on my own.

Maybe its just a simple stupid mistake and i am blind... i don't know...

Until this morning my JUCE project had the MainWindow class integrated into the main.cpp file, as it has been auto-generated.

When i had a look at the OpenGL2D demo code i noticed, that its making use of a static 'getMainAppWindow()' method, so other classes can get a pointer to finally use methods like 'getOpenGLContext()' or whatever. I thought this might be a good idea and started refactoring my code like this:


/*

This file was auto-generated by the Introjucer!

It contains the basic startup code for a Juce application.

==============================================================================
*/

// => main.cpp

#include “…/JuceLibraryCode/JuceHeader.h”
#include “MainAppWindow.h”

//==============================================================================
class GameTestApplication : public JUCEApplication
{
public:
//==============================================================================
GameTestApplication() {}

const String getApplicationName() override       { return ProjectInfo::projectName; }
const String getApplicationVersion() override    { return ProjectInfo::versionString; }
bool moreThanOneInstanceAllowed() override       { return false; }

//==============================================================================
void initialise (const String& commandLine) override
{
    mainWindow = new MainAppWindow();

    LookAndFeel::setDefaultLookAndFeel(mainWindow->getStyleEngine());
}

void shutdown() override
{
    // Add your application's shutdown code here..
    mainWindow = nullptr; // (deletes our window)
}

//==============================================================================
void systemRequestedQuit() override
{
    // This is called when the app is being asked to quit: you can ignore this
    // request and let the app carry on running, or call quit() to allow the app to close.
    quit();
}

void anotherInstanceStarted (const String& commandLine) override
{
    // When another instance of the app is launched while this one is running,
    // this method is invoked, and the commandLine parameter tells you what
    // the other instance's command-line arguments were.
}

private:
ScopedPointer<MainAppWindow> mainWindow;
};

//==============================================================================
// This macro generates the main() routine that launches the app.
START_JUCE_APPLICATION (GameTestApplication)

// => MainAppWindow.h

#ifndef MAINAPPWINDOW_H_INCLUDED
#define MAINAPPWINDOW_H_INCLUDED

#include "../JuceLibraryCode/JuceHeader.h"
#include "MainScreen.h"
#include "StyleEngine.h"

class MainAppWindow : public DocumentWindow
{
public:
    MainAppWindow();
    ~MainAppWindow();

    static MainAppWindow* getMainWindow();

    void closeButtonPressed() override;
    StyleEngine* getStyleEngine();

private:
    ScopedPointer<StyleEngine> styleEngine;
    TooltipWindow tooltipWindow;

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MainAppWindow)
};

#endif  // MAINAPPWINDOW_H_INCLUDED

// ==============================================================================
// => MainAppWindow.cpp

#include "../JuceLibraryCode/JuceHeader.h"
#include "MainAppWindow.h"

MainAppWindow::MainAppWindow() : DocumentWindow(JUCEApplication::getInstance()->getApplicationName(),
                                                Colours::lightgrey, DocumentWindow::allButtons),
                                                tooltipWindow(nullptr, 1500)
{
    styleEngine = new StyleEngine();
    setContentOwned(new MainScreen(), true);
    centreWithSize(getWidth(), getHeight());
    setUsingNativeTitleBar(true);
    setFullScreen(true);
    setVisible(true);
}

MainAppWindow::~MainAppWindow()
{

}

MainAppWindow* MainAppWindow::getMainWindow()
{
    for (int i = TopLevelWindow::getNumTopLevelWindows(); --i >= 0;)
    {
        if (MainAppWindow* mw = dynamic_cast<MainAppWindow*> (TopLevelWindow::getTopLevelWindow(i)))
            return mw;
    }

    return nullptr;
}

StyleEngine* MainAppWindow::getStyleEngine()
{
    return styleEngine;
}

void MainAppWindow::closeButtonPressed()
{
    // This is called when the user tries to close this window. Here, we'll just
    // ask the app to quit when this happens, but you can change this to do
    // whatever you need.
    JUCEApplication::getInstance()->systemRequestedQuit();
}

And my project contains lots of other component classes, which worked and work totally fine as long as the 'MainAppWindow.h' is NOT included... but as soon as i do include it like that:

=> GalaxyMap.h

#ifndef GALAXYMAP_H_INCLUDED
#define GALAXYMAP_H_INCLUDED

#include "../JuceLibraryCode/JuceHeader.h"
#include "MainAppWindow.h"
#include "StyleEngine.h"
#include "GalaxyObject.h"

#define _USE_MATH_DEFINES
#include <math.h>

//==============================================================================
/*
*/
class GalaxyMap    : public Component
{
public:
    GalaxyMap();
    ~GalaxyMap();

    void paint (Graphics&);
    void resized();

private:
    typedef Array<ScopedPointer<GalaxyObject>> GalaxyObjectList;
    ScopedPointer<GalaxyObjectList> objectList;
    Image cachedGalaxy;

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (GalaxyMap)
};

#endif  // GALAXYMAP_H_INCLUDED

The compiler is throwing weird errors like...

error C2065: 'GalaxyObject' : undeclared identifier (..\..\Source\GalaxyObject.cpp)

Somehow it seems like the inclusion of the 'MainAppWindow.h' header file is disturbing the correct inclusion of the 'GalaxyObject.h' header file and this happens with some other component classes as well.

I don't even know if this has anything to do with JUCE or whether its just a stupid c++ mistake, but maybe someone can give me a hint why that happens. :/

 

Regards,

Benjamin

 


#2

maybe adding "#pragma once" to your GalaxyMap.h ?