Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // PyxEngine/public/GameAPI.h
- #pragma once
- #include "Types.h"
- #ifdef PYX_PLATFORM_WINDOWS
- #ifdef PYX_BUILD_ENGINE
- #define PYX_GAME_API extern "C" __declspec(dllexport)
- #else
- #define PYX_GAME_API extern "C"
- #endif
- #else
- #error Platform not supported
- #endif
- class IGameAPI {
- public:
- virtual ~IGameAPI() = default;
- virtual void helloWorld() = 0;
- };
- typedef IGameAPI* GetPyxGameAPI(PyxContext* context);
- // PyxEngine/public/GameAPI.cpp
- #include "GameAPI.h"
- #include <iostream>
- class GameAPI : public IGameAPI {
- public:
- GameAPI(PyxContext* context) : m_Context(context) {}
- void helloWorld() override { std::cout << "goodbye world ):" << std::endl; }
- private:
- PyxContext* m_Context;
- };
- PYX_GAME_API IGameAPI* getPyxGameAPI(PyxContext* context) {
- GameAPI* api{ new GameAPI(context) };
- return api;
- }
- // PyxEngine/public/EngineAPI.h
- #pragma once
- #include "Types.h"
- #ifdef PYX_PLATFORM_WINDOWS
- #ifdef PYX_BUILD_ENGINE
- #define PYX_ENGINE_API extern "C" __declspec(dllexport)
- #else
- #define PYX_ENGINE_API extern "C"
- #endif
- #else
- #error Platform not supported
- #endif
- typedef struct SDL_Window SDL_Window;
- class IPyxEngineAPI {
- public:
- virtual ~IPyxEngineAPI() = default;
- virtual void init(SDL_Window* window) = 0;
- };
- typedef IPyxEngineAPI* GetPyxEngineAPI(PyxContext* context);
- // PyxEngine/public/EngineAPI.cpp
- #include "EngineAPI.h"
- #include "../src/Logger.h"
- #include "../src/VulkanRenderer/Renderer.h"
- #include <iostream>
- class PyxEngineAPI : public IPyxEngineAPI {
- public:
- PyxEngineAPI(PyxContext* context) : m_Context(context) {}
- void init(SDL_Window* window) override {
- pyx::Logger::init();
- VulkanRenderer::init(window);
- }
- private:
- PyxContext* m_Context;
- };
- PYX_ENGINE_API IPyxEngineAPI* getPyxEngineAPI(PyxContext* context) {
- IPyxEngineAPI* api{ new PyxEngineAPI(context) };
- return api;
- }
- // PyxEngine/public/GameInterface.h
- #pragma once
- #include "GameAPI.h"
- #ifdef PYX_PLATFORM_WINDOWS
- #ifdef PYX_BUILD_ENGINE
- #define PYX_GAME extern "C"
- #else
- #define PYX_GAME extern "C" __declspec(dllexport)
- #endif
- #else
- #error Platform not supported
- #endif
- class IGame {
- public:
- virtual ~IGame() = default;
- virtual void start() = 0;
- virtual void update() = 0;
- };
- typedef IGame* GetGame(IGameAPI* pyxGameAPI);
- // examples/FirstExample/src/MyGame.h
- #pragma once
- #include "GameInterface.h"
- class MyGame : public IGame {
- public:
- inline MyGame(IGameAPI* pyxGameAPI) : m_Pyx(pyxGameAPI) {}
- void start() override;
- void update() override;
- private:
- IGameAPI* m_Pyx;
- };
- // examples/FirstExample/src/MyGame.cpp
- #include "MyGame.h"
- void MyGame::start() {
- m_Pyx->helloWorld();
- }
- void MyGame::update() {}
- PYX_GAME IGame* getGame(IGameAPI* pyxGameAPI) {
- IGame* game{ new MyGame(pyxGameAPI) };
- return game;
- }
- // Runtime/src/Platforms/Windows/Main.cpp
- // previous code...
- HMODULE gameDLL{ LoadLibraryA("Game.dll") };
- HMODULE pyxEngineDLL{ LoadLibraryA("PyxEngine.dll") };
- GetPyxGameAPI* getPyxGameAPI{ reinterpret_cast<GetPyxGameAPI*>(
- GetProcAddress(pyxEngineDLL, "getPyxGameAPI")
- ) };
- GetPyxEngineAPI* getPyxEngineAPI{ reinterpret_cast<GetPyxEngineAPI*>(
- GetProcAddress(pyxEngineDLL, "getPyxEngineAPI")
- ) };
- GetGame* getGame{
- reinterpret_cast<GetGame*>(GetProcAddress(gameDLL, "getGame"))
- };
- PyxContext engineContext{};
- IGameAPI* gameAPI{ getPyxGameAPI(&engineContext) };
- IPyxEngineAPI* engineAPI{ getPyxEngineAPI(&engineContext) };
- engineAPI->init(window);
- IGame* game{ getGame(gameAPI) };
- game->start();
- // subsiquent code...
Advertisement
Add Comment
Please, Sign In to add comment