A 2D tile-based sandbox game.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Game.cc 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include "Game.h"
  2. #include <dlfcn.h>
  3. #include "Tile.h"
  4. namespace Swan {
  5. void Game::loadMod(const std::string &path) {
  6. std::string dlpath = path + "/mod.so";
  7. void *dl = dlopen(dlpath.c_str(), RTLD_LAZY);
  8. if (dl == NULL) {
  9. fprintf(stderr, "%s\n", dlerror());
  10. return;
  11. }
  12. void (*mod_init)(Mod &) = (void (*)(Mod &))dlsym(dl, "mod_init");
  13. if (mod_init == NULL) {
  14. fprintf(stderr, "%s\n", dlerror());
  15. }
  16. registered_mods_.push_back(Mod());
  17. Mod &mod = registered_mods_.back();
  18. mod.path_ = path;
  19. mod_init(mod);
  20. }
  21. void Game::createWorld(std::string worldgen) {
  22. world_.reset(new World());
  23. for (auto &mod: registered_mods_) {
  24. for (auto &tile: mod.tiles_)
  25. world_->registerTile(tile);
  26. for (auto &worldgen: mod.worldgens_)
  27. world_->registerWorldGen(worldgen);
  28. for (auto &entity: mod.entities_)
  29. world_->registerEntity(entity);
  30. for (auto &asset: mod.assets_)
  31. world_->registerAsset(asset);
  32. }
  33. world_->setWorldGen(worldgen);
  34. world_->setCurrentPlane(world_->addPlane());
  35. world_->spawnPlayer();
  36. }
  37. void Game::draw(Win &win) {
  38. world_->draw(win);
  39. }
  40. void Game::update(float dt) {
  41. world_->update(dt);
  42. }
  43. void Game::tick() {
  44. world_->tick();
  45. }
  46. void Game::initGlobal() {
  47. Tile::initInvalid();
  48. }
  49. }