Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef ARCADE_CORE_HPP
- #define ARCADE_CORE_HPP
- #include "GameScene.hpp"
- #include "Exception.hpp"
- #include "Object.hpp"
- #include "IGLibrary.hpp"
- #include "IGfxLibrary.hpp"
- #include "Utils/Utils.hpp"
- #include <dlfcn.h>
- #include <map>
- #include <iostream>
- class Core {
- private:
- std::map<std::string, std::string> gfxMap;
- std::map<std::string, std::string> gamesMap;
- IGfxLibrary *gfx;
- void *gfx_lib;
- IGLibrary *game;
- void *game_lib;
- std::string playerName;
- size_t score;
- int error;
- bool end;
- public:
- Core(std::string lib_path);
- ~Core();
- void run();
- std::vector<Object *> getObjects();
- Input input;
- GameScene *scene;
- private:
- IGfxLibrary *loadGraphics(const char *path);
- IGLibrary *loadGame(const char *path);
- void loadLibrary(char type, const char *path);
- void destroyLibrary(char type);
- void linkLibrary();
- void gameLoop();
- void specialInputs();
- };
- #endif //ARCADE_CORE_HPP
- //-----------------------------------------------------------------------------------------
- #include "Core.hpp"
- Core::Core(std::string lib_path)
- {
- gfxMap["ncurses"] = "./lib/graphics/lib_arcade_ncurses.so";
- gfxMap["sdl"] = "./lib/graphics/lib_arcade_sdl2.so";
- gfxMap["sfml"] = "./lib/graphics/lib_arcade_sfml.so";
- gfxMap["prueba"] = "./lib/graphics/lib_arcade_prueba.so";
- gamesMap["centipede"] = "./lib/games/lib_arcade_centipede.so";
- gamesMap["nibbler"] = "./lib/games/lib_arcade_nibbler.so";
- gamesMap["solarfox"] = "./lib/games/lib_arcade_solarfox.so";
- gamesMap["prueba"] = "./lib/games/lib_arcade_prueba.so";
- scene = new GameScene();
- score = 0;
- error = 0;
- input = None;
- gfx_lib = nullptr;
- game_lib = nullptr;
- game = loadGame("./lib/games/menu/lib_arcade_menu.so");
- gfx = loadGraphics(lib_path.c_str());
- }
- void Core::run()
- {
- game->initGameScene(this);
- while (!this->end) {
- gfx->collectInput(this);
- specialInputs();
- game->updateStatus(this);
- gfx->refreshScreen(this);
- }
- }
- std::vector<Object *> Core::getObjects()
- {
- return (this->scene->getObjects());
- }
- void Core::specialInputs()
- {
- //std::cout << "Checkeamos si hay un input especial" << std::endl;
- }
- IGfxLibrary *Core::loadGraphics(const char *path)
- {
- const char *dlsym_error;
- loadLibrary(GFX, path);
- create_gfx createGfx = (create_gfx) dlsym(gfx_lib, "create");
- dlsym_error = dlerror();
- if (dlsym_error)
- throw Exception(dlsym_error);
- return (createGfx());
- }
- IGLibrary *Core::loadGame(const char *path)
- {
- const char *dlsym_error;
- loadLibrary(GAME, path);
- create_g createGame = (create_g) dlsym(game_lib, "create");
- dlsym_error = dlerror();
- if (dlsym_error)
- throw Exception(dlsym_error);
- return (createGame());
- }
- void Core::loadLibrary(char type, const char *path)
- {
- if (type == GAME) {
- if (game_lib != nullptr)
- dlclose(game_lib);
- game_lib = dlopen(path, RTLD_NOW);
- if (!game_lib)
- throw Exception(dlerror());
- dlerror();
- } else if (type == GFX) {
- if (gfx_lib != nullptr)
- dlclose(gfx_lib);
- gfx_lib = dlopen(path, RTLD_NOW);
- if (!gfx_lib)
- throw Exception(dlerror());
- dlerror();
- } else
- throw Exception("Runtime error loading libraries.");
- }
- void Core::destroyLibrary(char type)
- {
- const char *dlsym_error;
- if (type == GAME) {
- destroy_g destroyGame = (destroy_g) dlsym(game_lib, "destroy");
- dlsym_error = dlerror();
- if (dlsym_error)
- throw Exception(dlsym_error);
- destroyGame(this->game);
- dlclose(game_lib);
- } else if (type == GFX) {
- destroy_gfx destroyGfx = (destroy_gfx) dlsym(gfx_lib, "destroy");
- dlsym_error = dlerror();
- if (dlsym_error)
- throw Exception(dlsym_error);
- destroyGfx(this->gfx);
- dlclose(gfx_lib);
- } else
- throw Exception("Runtime error destroying libraries.");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement