Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PGEX extensions not usable in dll context #363

Open
CrazyDaisyRecords opened this issue Feb 29, 2024 · 0 comments
Open

PGEX extensions not usable in dll context #363

CrazyDaisyRecords opened this issue Feb 29, 2024 · 0 comments

Comments

@CrazyDaisyRecords
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant