unmeinks

Untitled

Mar 24th, 2024
826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.61 KB | Source Code | 0 0
  1. // PyxEngine/public/GameAPI.h
  2. #pragma once
  3. #include "Types.h"
  4.  
  5. #ifdef PYX_PLATFORM_WINDOWS
  6.     #ifdef PYX_BUILD_ENGINE
  7.         #define PYX_GAME_API extern "C" __declspec(dllexport)
  8.     #else
  9.         #define PYX_GAME_API extern "C"
  10.     #endif
  11. #else
  12.     #error Platform not supported
  13. #endif
  14.  
  15. class IGameAPI {
  16.    public:
  17.     virtual ~IGameAPI() = default;
  18.     virtual void helloWorld() = 0;
  19. };
  20.  
  21. typedef IGameAPI* GetPyxGameAPI(PyxContext* context);
  22.  
  23. // PyxEngine/public/GameAPI.cpp
  24. #include "GameAPI.h"
  25.  
  26. #include <iostream>
  27.  
  28. class GameAPI : public IGameAPI {
  29.    public:
  30.     GameAPI(PyxContext* context) : m_Context(context) {}
  31.     void helloWorld() override { std::cout << "goodbye world ):" << std::endl; }
  32.  
  33.    private:
  34.     PyxContext* m_Context;
  35. };
  36.  
  37. PYX_GAME_API IGameAPI* getPyxGameAPI(PyxContext* context) {
  38.     GameAPI* api{ new GameAPI(context) };
  39.     return api;
  40. }
  41.  
  42. // PyxEngine/public/EngineAPI.h
  43. #pragma once
  44. #include "Types.h"
  45.  
  46. #ifdef PYX_PLATFORM_WINDOWS
  47.     #ifdef PYX_BUILD_ENGINE
  48.         #define PYX_ENGINE_API extern "C" __declspec(dllexport)
  49.     #else
  50.         #define PYX_ENGINE_API extern "C"
  51.     #endif
  52. #else
  53.     #error Platform not supported
  54. #endif
  55.  
  56. typedef struct SDL_Window SDL_Window;
  57.  
  58. class IPyxEngineAPI {
  59.    public:
  60.     virtual ~IPyxEngineAPI() = default;
  61.     virtual void init(SDL_Window* window) = 0;
  62. };
  63.  
  64. typedef IPyxEngineAPI* GetPyxEngineAPI(PyxContext* context);
  65.  
  66. // PyxEngine/public/EngineAPI.cpp
  67. #include "EngineAPI.h"
  68. #include "../src/Logger.h"
  69. #include "../src/VulkanRenderer/Renderer.h"
  70.  
  71. #include <iostream>
  72.  
  73. class PyxEngineAPI : public IPyxEngineAPI {
  74.    public:
  75.     PyxEngineAPI(PyxContext* context) : m_Context(context) {}
  76.     void init(SDL_Window* window) override {
  77.         pyx::Logger::init();
  78.         VulkanRenderer::init(window);
  79.     }
  80.  
  81.    private:
  82.     PyxContext* m_Context;
  83. };
  84.  
  85. PYX_ENGINE_API IPyxEngineAPI* getPyxEngineAPI(PyxContext* context) {
  86.     IPyxEngineAPI* api{ new PyxEngineAPI(context) };
  87.     return api;
  88. }
  89.  
  90. // PyxEngine/public/GameInterface.h
  91. #pragma once
  92. #include "GameAPI.h"
  93.  
  94. #ifdef PYX_PLATFORM_WINDOWS
  95.     #ifdef PYX_BUILD_ENGINE
  96.         #define PYX_GAME extern "C"
  97.     #else
  98.         #define PYX_GAME extern "C" __declspec(dllexport)
  99.     #endif
  100. #else
  101.     #error Platform not supported
  102. #endif
  103.  
  104. class IGame {
  105.    public:
  106.     virtual ~IGame() = default;
  107.     virtual void start() = 0;
  108.     virtual void update() = 0;
  109. };
  110.  
  111. typedef IGame* GetGame(IGameAPI* pyxGameAPI);
  112.  
  113. // examples/FirstExample/src/MyGame.h
  114. #pragma once
  115. #include "GameInterface.h"
  116.  
  117. class MyGame : public IGame {
  118.    public:
  119.     inline MyGame(IGameAPI* pyxGameAPI) : m_Pyx(pyxGameAPI) {}
  120.     void start() override;
  121.     void update() override;
  122.  
  123.    private:
  124.     IGameAPI* m_Pyx;
  125. };
  126.  
  127. // examples/FirstExample/src/MyGame.cpp
  128. #include "MyGame.h"
  129.  
  130. void MyGame::start() {
  131.     m_Pyx->helloWorld();
  132. }
  133.  
  134. void MyGame::update() {}
  135.  
  136. PYX_GAME IGame* getGame(IGameAPI* pyxGameAPI) {
  137.     IGame* game{ new MyGame(pyxGameAPI) };
  138.     return game;
  139. }
  140.  
  141. // Runtime/src/Platforms/Windows/Main.cpp
  142. // previous code...
  143.  
  144.     HMODULE gameDLL{ LoadLibraryA("Game.dll") };
  145.     HMODULE pyxEngineDLL{ LoadLibraryA("PyxEngine.dll") };
  146.  
  147.     GetPyxGameAPI* getPyxGameAPI{ reinterpret_cast<GetPyxGameAPI*>(
  148.         GetProcAddress(pyxEngineDLL, "getPyxGameAPI")
  149.     ) };
  150.     GetPyxEngineAPI* getPyxEngineAPI{ reinterpret_cast<GetPyxEngineAPI*>(
  151.         GetProcAddress(pyxEngineDLL, "getPyxEngineAPI")
  152.     ) };
  153.     GetGame* getGame{
  154.         reinterpret_cast<GetGame*>(GetProcAddress(gameDLL, "getGame"))
  155.     };
  156.  
  157.     PyxContext engineContext{};
  158.  
  159.     IGameAPI* gameAPI{ getPyxGameAPI(&engineContext) };
  160.     IPyxEngineAPI* engineAPI{ getPyxEngineAPI(&engineContext) };
  161.  
  162.     engineAPI->init(window);
  163.     IGame* game{ getGame(gameAPI) };
  164.     game->start();
  165.  
  166. // subsiquent code...
Advertisement
Add Comment
Please, Sign In to add comment