Skip to content

PGEX extensions not usable in dll context #363

@CrazyDaisyRecords

Description

@CrazyDaisyRecords

Issue

I have started to use the pixel game engine in a more complex project structure, where the main engine is running in the main process (.exe), but you can load dynamic components (.dll), which are kind of plugged in into the UI drawing. In my case I would like to have the dynamically loaded components to be able to draw on my olc::TransformedView, which represents my main canvas and the UI elements where I am using the DearImGui implementation for the olcPixelGameEngine.

But caused by the design how the PGEX base class holds the pointer to the actual engine, it´s not possible to use extensions on the dll side. Since the pge pointer is a static class member, the dll which also has to include the required pixelGameEngine headers, has a different context.

class PGEX
	{
		friend class olc::PixelGameEngine;
	public:
		PGEX(bool bHook = false);

	protected:
		virtual void OnBeforeUserCreate();
		virtual void OnAfterUserCreate();
		virtual bool OnBeforeUserUpdate(float &fElapsedTime);
		virtual void OnAfterUserUpdate(float fElapsedTime);

	protected:
		static PixelGameEngine* pge;
	};

The pointer to the actual engine is set in the constructor of the engine (which by design would not allow to have multiple engines running by the way?)

PixelGameEngine::PixelGameEngine()
	{
		sAppName = "Undefined";
		olc::PGEX::pge = this;

		// Bring in relevant Platform & Rendering systems depending
		// on compiler parameters
		olc_ConfigureSystem();
	}

Proposal

Since I have a single point where my dynamically loaded components register their draw callbacks, I currently use this step to pass the actual olc::PixelGameEngine* to the DLL. Further I added a public static method to the PGEX base class, to be able to set the engine pointer with this function simple as that. This allowes me to set the extension context in all loaded DLLs

class PGEX
	{
		friend class olc::PixelGameEngine;
	public:
		PGEX(bool bHook = false);
                static void SetEngine(PixelGameEngine* pPge) {
                    pge = pPge;
                }
                
            // ...

The DearImGui implementation handles this problem by providing static methods GetCurrentContext() and SetCurrentContext(), which also works fine for me. So if you think this improvement is worth to be implemented, maybe such Interface would be a little bit more intuitive?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions