123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- /*
- * Bael'Zharon's Respite
- * Copyright (C) 2014 Daniel Skorupski
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
- #include "Core.h"
- #ifndef HEADLESS
- #include "graphics/Renderer.h"
- #endif
- #include "net/SessionManager.h"
- #include "resource/Region.h"
- #include "Camera.h"
- #include "Config.h"
- #include "DatFile.h"
- #include "Land.h"
- #include "LandcellManager.h"
- #include "Log.h"
- #include "ObjectManager.h"
- #include "ResourceCache.h"
- #include "util.h"
- static const fp_t kStepRate = 60.0;
- static unique_ptr<Core> g_singleton;
- void Core::execute()
- {
- assert(!g_singleton);
- g_singleton.reset(new Core{});
- g_singleton->init();
- g_singleton->run();
- g_singleton->cleanup();
- }
- Core& Core::get()
- {
- return *g_singleton;
- }
- void Core::stop()
- {
- done_ = true;
- }
- Config& Core::config()
- {
- return *config_;
- }
- Log& Core::log()
- {
- return *log_;
- }
- DatFile& Core::portalDat()
- {
- return *portalDat_;
- }
- DatFile& Core::cellDat()
- {
- return *cellDat_;
- }
- DatFile& Core::highresDat()
- {
- return *highresDat_;
- }
- ResourceCache& Core::resourceCache()
- {
- return *resourceCache_;
- }
- LandcellManager& Core::landcellManager()
- {
- return *landcellManager_;
- }
- ObjectManager& Core::objectManager()
- {
- return *objectManager_;
- }
- SessionManager& Core::sessionManager()
- {
- return *sessionManager_;
- }
- const Region& Core::region() const
- {
- return region_->cast<Region>();
- }
- Camera& Core::camera()
- {
- return *camera_;
- }
- #ifndef HEADLESS
- Renderer& Core::renderer()
- {
- return *renderer_;
- }
- #endif
- Core::Core() : done_(false)
- {}
- void Core::init()
- {
- if(SDL_Init(SDL_INIT_TIMER) < 0)
- {
- throwSDLError();
- }
- config_.reset(new Config{});
- log_.reset(new Log{});
- portalDat_.reset(new DatFile{"data/client_portal.dat"});
- cellDat_.reset(new DatFile{"data/client_cell_1.dat"});
- highresDat_.reset(new DatFile{"data/client_highres.dat"});
- resourceCache_.reset(new ResourceCache{});
- landcellManager_.reset(new LandcellManager{});
- objectManager_.reset(new ObjectManager{});
- sessionManager_.reset(new SessionManager{});
- region_ = resourceCache_->get(0x13000000);
- camera_.reset(new Camera{});
- #ifndef HEADLESS
- renderer_.reset(new Renderer{});
- renderer_->init();
- #endif
- landcellManager_->setCenter(LandcellId(0x31, 0xD6));
- #if 0
- for(uint32_t resourceId : portalDat_->list())
- {
- if((resourceId & 0xFF000000) > 0x0D000000)
- {
- continue;
- }
- try
- {
- resourceCache_->get(resourceId);
- printf("%08x OK\n", resourceId);
- }
- catch(runtime_error& e)
- {
- printf("%08x FAIL %s\n", resourceId, e.what());
- }
- }
- #endif
- }
- void Core::cleanup()
- {
- #ifndef HEADLESS
- renderer_.reset();
- #endif
- camera_.reset();
- sessionManager_.reset();
- objectManager_.reset();
- landcellManager_.reset();
- resourceCache_.reset();
- portalDat_.reset();
- cellDat_.reset();
- highresDat_.reset();
- log_.reset();
- config_.reset();
- SDL_Quit();
- }
- void Core::run()
- {
- uint64_t frequency = SDL_GetPerformanceFrequency();
- uint64_t fixedStep = frequency / static_cast<uint64_t>(kStepRate);
- uint64_t maxTotalDelta = fixedStep * 6;
- uint64_t stepTime = SDL_GetPerformanceCounter();
- while(!done_)
- {
- uint64_t loopTime = SDL_GetPerformanceCounter();
- if(loopTime > stepTime + maxTotalDelta)
- {
- stepTime = loopTime - maxTotalDelta;
- }
- while(loopTime >= stepTime + fixedStep)
- {
- handleEvents();
- sessionManager_->handleBlobs();
- step(fp_t(1.0) / kStepRate);
- stepTime += fixedStep;
- }
- #ifndef HEADLESS
- fp_t interp = static_cast<fp_t>(loopTime - stepTime) / static_cast<fp_t>(frequency);
- renderer_->render(interp);
- #else
- // simulate ~83 without game logic
- SDL_Delay(12);
- #endif
- }
- }
- void Core::handleEvents()
- {
- SDL_Event event;
- while(SDL_PollEvent(&event) != 0)
- {
- switch(event.type)
- {
- case SDL_QUIT:
- done_ = true;
- break;
- case SDL_KEYDOWN:
- #ifdef _WIN32
- // SDL does not respond normally to alt-f4 on Windows, so handle it ourselves
- if(event.key.keysym.sym == SDLK_F4 && (event.key.keysym.mod & KMOD_ALT) != 0)
- {
- done_ = true;
- }
- #endif
- break;
- }
- }
- }
- void Core::step(fp_t dt)
- {
- const Uint8* state = SDL_GetKeyboardState(nullptr);
- fp_t speed = 0.0;
- if(state[SDL_SCANCODE_LSHIFT])
- {
- speed = 100.0;
- }
- else
- {
- speed = 10.0;
- }
- camera_->setSpeed(speed);
- fp_t lx = 0.0;
- fp_t ly = 0.0;
- if(state[SDL_SCANCODE_LEFT])
- {
- lx -= dt;
- }
- if(state[SDL_SCANCODE_RIGHT])
- {
- lx += dt;
- }
- if(state[SDL_SCANCODE_DOWN])
- {
- ly -= dt;
- }
- if(state[SDL_SCANCODE_UP])
- {
- ly += dt;
- }
- if(lx != 0.0 || ly != 0.0)
- {
- camera_->look(lx, ly);
- }
- fp_t mx = 0.0;
- fp_t my = 0.0;
- if(state[SDL_SCANCODE_A])
- {
- mx -= dt;
- }
- if(state[SDL_SCANCODE_D])
- {
- mx += dt;
- }
- if(state[SDL_SCANCODE_S])
- {
- my -= dt;
- }
- if(state[SDL_SCANCODE_W])
- {
- my += dt;
- }
- if(mx != 0 || my != 0)
- {
- camera_->move(mx, my);
- }
- const glm::vec3& position = camera_->position();
- LandcellId id = landcellManager_->center();
- if(position.x < 0.0)
- {
- camera_->setPosition(glm::vec3(position.x + Land::kBlockSize, position.y, position.z));
- landcellManager_->setCenter(LandcellId(id.x() - 1, id.y()));
- }
- if(position.x >= Land::kBlockSize)
- {
- camera_->setPosition(glm::vec3(position.x - Land::kBlockSize, position.y, position.z));
- landcellManager_->setCenter(LandcellId(id.x() + 1, id.y()));
- }
- if(position.y < 0.0)
- {
- camera_->setPosition(glm::vec3(position.x, position.y + Land::kBlockSize, position.z));
- landcellManager_->setCenter(LandcellId(id.x(), id.y() - 1));
- }
- if(position.y >= Land::kBlockSize)
- {
- camera_->setPosition(glm::vec3(position.x, position.y - Land::kBlockSize, position.z));
- landcellManager_->setCenter(LandcellId(id.x(), id.y() + 1));
- }
- camera_->step(dt);
- }
|