Advertisement
Guest User

Core

a guest
Apr 5th, 2021
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.09 KB | None | 0 0
  1. #ifndef ARCADE_CORE_HPP
  2. #define ARCADE_CORE_HPP
  3.  
  4. #include "GameScene.hpp"
  5. #include "Exception.hpp"
  6. #include "Object.hpp"
  7. #include "IGLibrary.hpp"
  8. #include "IGfxLibrary.hpp"
  9. #include "Utils/Utils.hpp"
  10. #include <dlfcn.h>
  11. #include <map>
  12. #include <iostream>
  13.  
  14. class Core {
  15. private:
  16.     std::map<std::string, std::string> gfxMap;
  17.     std::map<std::string, std::string> gamesMap;
  18.     IGfxLibrary *gfx;
  19.     void *gfx_lib;
  20.     IGLibrary *game;
  21.     void *game_lib;
  22.     std::string playerName;
  23.     size_t score;
  24.     int error;
  25.     bool end;
  26.  
  27. public:
  28.     Core(std::string lib_path);
  29.     ~Core();
  30.     void run();
  31.     std::vector<Object *> getObjects();
  32.     Input input;
  33.     GameScene *scene;
  34.  
  35. private:
  36.     IGfxLibrary *loadGraphics(const char *path);
  37.     IGLibrary *loadGame(const char *path);
  38.     void loadLibrary(char type, const char *path);
  39.     void destroyLibrary(char type);
  40.     void linkLibrary();
  41.     void gameLoop();
  42.     void specialInputs();
  43. };
  44. #endif //ARCADE_CORE_HPP
  45. //-----------------------------------------------------------------------------------------
  46. #include "Core.hpp"
  47.  
  48. Core::Core(std::string lib_path)
  49. {
  50.     gfxMap["ncurses"] = "./lib/graphics/lib_arcade_ncurses.so";
  51.     gfxMap["sdl"] = "./lib/graphics/lib_arcade_sdl2.so";
  52.     gfxMap["sfml"] = "./lib/graphics/lib_arcade_sfml.so";
  53.     gfxMap["prueba"] = "./lib/graphics/lib_arcade_prueba.so";
  54.     gamesMap["centipede"] = "./lib/games/lib_arcade_centipede.so";
  55.     gamesMap["nibbler"] = "./lib/games/lib_arcade_nibbler.so";
  56.     gamesMap["solarfox"] = "./lib/games/lib_arcade_solarfox.so";
  57.     gamesMap["prueba"] = "./lib/games/lib_arcade_prueba.so";
  58.     scene = new GameScene();
  59.     score = 0;
  60.     error = 0;
  61.     input = None;
  62.     gfx_lib = nullptr;
  63.     game_lib = nullptr;
  64.     game = loadGame("./lib/games/menu/lib_arcade_menu.so");
  65.     gfx = loadGraphics(lib_path.c_str());
  66. }
  67.  
  68. void Core::run()
  69. {
  70.     game->initGameScene(this);
  71.     while (!this->end) {
  72.         gfx->collectInput(this);
  73.         specialInputs();
  74.         game->updateStatus(this);
  75.         gfx->refreshScreen(this);
  76.     }
  77. }
  78.  
  79. std::vector<Object *> Core::getObjects()
  80. {
  81.     return (this->scene->getObjects());
  82. }
  83.  
  84.  
  85. void Core::specialInputs()
  86. {
  87.     //std::cout << "Checkeamos si hay un input especial" << std::endl;
  88. }
  89.  
  90. IGfxLibrary *Core::loadGraphics(const char *path)
  91. {
  92.     const char *dlsym_error;
  93.  
  94.     loadLibrary(GFX, path);
  95.     create_gfx createGfx = (create_gfx) dlsym(gfx_lib, "create");
  96.     dlsym_error = dlerror();
  97.     if (dlsym_error)
  98.         throw Exception(dlsym_error);
  99.     return (createGfx());
  100. }
  101.  
  102. IGLibrary *Core::loadGame(const char *path)
  103. {
  104.     const char *dlsym_error;
  105.  
  106.     loadLibrary(GAME, path);
  107.     create_g createGame = (create_g) dlsym(game_lib, "create");
  108.     dlsym_error = dlerror();
  109.     if (dlsym_error)
  110.         throw Exception(dlsym_error);
  111.     return (createGame());
  112. }
  113.  
  114. void Core::loadLibrary(char type, const char *path)
  115. {
  116.     if (type == GAME) {
  117.         if (game_lib != nullptr)
  118.             dlclose(game_lib);
  119.         game_lib = dlopen(path, RTLD_NOW);
  120.         if (!game_lib)
  121.             throw Exception(dlerror());
  122.         dlerror();
  123.     } else if (type == GFX) {
  124.         if (gfx_lib != nullptr)
  125.             dlclose(gfx_lib);
  126.         gfx_lib = dlopen(path, RTLD_NOW);
  127.         if (!gfx_lib)
  128.             throw Exception(dlerror());
  129.         dlerror();
  130.     } else
  131.         throw Exception("Runtime error loading libraries.");
  132. }
  133.  
  134. void Core::destroyLibrary(char type)
  135. {
  136.     const char *dlsym_error;
  137.  
  138.     if (type == GAME) {
  139.         destroy_g destroyGame = (destroy_g) dlsym(game_lib, "destroy");
  140.         dlsym_error = dlerror();
  141.         if (dlsym_error)
  142.             throw Exception(dlsym_error);
  143.         destroyGame(this->game);
  144.         dlclose(game_lib);
  145.     } else if (type == GFX) {
  146.         destroy_gfx destroyGfx = (destroy_gfx) dlsym(gfx_lib, "destroy");
  147.         dlsym_error = dlerror();
  148.         if (dlsym_error)
  149.             throw Exception(dlsym_error);
  150.         destroyGfx(this->gfx);
  151.         dlclose(gfx_lib);
  152.     } else
  153.         throw Exception("Runtime error destroying libraries.");
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement