1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-07 03:12:36 +01:00

Avoid allocating RWGame members where possible

These objects don't need to be dynamically allocated, they can
take on the same lifetime as RWGame itself.
This commit is contained in:
Daniel Evans 2016-10-16 20:37:07 +01:00
parent 2f118631dc
commit ad998e8a80
6 changed files with 66 additions and 82 deletions

View File

@ -50,7 +50,7 @@ public:
m_strings.emplace(id, text); m_strings.emplace(id, text);
} }
GameString text(const GameStringKey& id) { GameString text(const GameStringKey& id) const {
auto a = m_strings.find(id); auto a = m_strings.find(id);
if (a != m_strings.end()) { if (a != m_strings.end()) {
return a->second; return a->second;

View File

@ -12,8 +12,6 @@
#include <engine/GameWorld.hpp> #include <engine/GameWorld.hpp>
#include <engine/SaveGame.hpp> #include <engine/SaveGame.hpp>
#include <objects/GameObject.hpp> #include <objects/GameObject.hpp>
#include <render/DebugDraw.hpp>
#include <render/GameRenderer.hpp>
#include <script/ScriptMachine.hpp> #include <script/ScriptMachine.hpp>
#include <script/modules/GTA3Module.hpp> #include <script/modules/GTA3Module.hpp>
@ -33,10 +31,11 @@ std::map<GameRenderer::SpecialModel, std::string> kSpecialModels = {
#define MOUSE_SENSITIVITY_SCALE 2.5f #define MOUSE_SENSITIVITY_SCALE 2.5f
DebugDraw* debug = nullptr;
RWGame::RWGame(Logger& log, int argc, char* argv[]) RWGame::RWGame(Logger& log, int argc, char* argv[])
: GameBase(log, argc, argv) { : GameBase(log, argc, argv)
, data(&log, &work, config.getGameDataPath())
, renderer(&log, &data) {
bool newgame = options.count("newgame"); bool newgame = options.count("newgame");
bool test = options.count("test"); bool test = options.count("test");
std::string startSave( std::string startSave(
@ -45,8 +44,6 @@ RWGame::RWGame(Logger& log, int argc, char* argv[])
? options["benchmark"].as<std::string>() ? options["benchmark"].as<std::string>()
: ""); : "");
work = new WorkContext();
log.info("Game", "Game directory: " + config.getGameDataPath()); log.info("Game", "Game directory: " + config.getGameDataPath());
if (!GameData::isValidGameDirectory(config.getGameDataPath())) { if (!GameData::isValidGameDirectory(config.getGameDataPath())) {
@ -54,40 +51,34 @@ RWGame::RWGame(Logger& log, int argc, char* argv[])
config.getGameDataPath()); config.getGameDataPath());
} }
data = new GameData(&log, work, config.getGameDataPath()); data.load();
data->load();
// Initialize renderer
renderer = new GameRenderer(&log, data);
for (const auto& p : kSpecialModels) { for (const auto& p : kSpecialModels) {
auto model = data->loadClump(p.second); auto model = data.loadClump(p.second);
renderer->setSpecialModel(p.first, model); renderer.setSpecialModel(p.first, model);
} }
// Set up text renderer // Set up text renderer
renderer->text.setFontTexture(0, "pager"); renderer.text.setFontTexture(0, "pager");
renderer->text.setFontTexture(1, "font1"); renderer.text.setFontTexture(1, "font1");
renderer->text.setFontTexture(2, "font2"); renderer.text.setFontTexture(2, "font2");
debug = new DebugDraw; debug.setDebugMode(btIDebugDraw::DBG_DrawWireframe |
debug->setDebugMode(btIDebugDraw::DBG_DrawWireframe | btIDebugDraw::DBG_DrawConstraints |
btIDebugDraw::DBG_DrawConstraints | btIDebugDraw::DBG_DrawConstraintLimits);
btIDebugDraw::DBG_DrawConstraintLimits); debug.setShaderProgram(renderer.worldProg);
debug->setShaderProgram(renderer->worldProg);
data->loadDynamicObjects(config.getGameDataPath() + "/data/object.dat"); data.loadDynamicObjects(config.getGameDataPath() + "/data/object.dat");
data->loadGXT("text/" + config.getGameLanguage() + ".gxt"); data.loadGXT("text/" + config.getGameLanguage() + ".gxt");
getRenderer()->water.setWaterTable(data->waterHeights, 48, data->realWater, getRenderer().water.setWaterTable(data.waterHeights, 48, data.realWater,
128 * 128); 128 * 128);
for (int m = 0; m < MAP_BLOCK_SIZE; ++m) { for (int m = 0; m < MAP_BLOCK_SIZE; ++m) {
std::string num = (m < 10 ? "0" : ""); std::string num = (m < 10 ? "0" : "");
std::string name = "radar" + num + std::to_string(m); std::string name = "radar" + num + std::to_string(m);
data->loadTXD(name + ".txd"); data.loadTXD(name + ".txd");
} }
auto loading = new LoadingState(this); auto loading = new LoadingState(this);
@ -112,22 +103,16 @@ RWGame::~RWGame() {
log.info("Game", "Beginning cleanup"); log.info("Game", "Beginning cleanup");
log.info("Game", "Stopping work queue"); log.info("Game", "Stopping work queue");
work->stop(); work.stop();
log.info("Game", "Cleaning up scripts"); log.info("Game", "Cleaning up scripts");
delete script; delete script;
log.info("Game", "Cleaning up renderer");
delete renderer;
log.info("Game", "Cleaning up world"); log.info("Game", "Cleaning up world");
delete world; delete world;
log.info("Game", "Cleaning up state"); log.info("Game", "Cleaning up state");
delete state; delete state;
log.info("Game", "Cleaning up work queue");
delete work;
} }
void RWGame::newGame() { void RWGame::newGame() {
@ -137,8 +122,8 @@ void RWGame::newGame() {
} }
state = new GameState(); state = new GameState();
world = new GameWorld(&log, work, data); world = new GameWorld(&log, &work, &data);
world->dynamicsWorld->setDebugDrawer(debug); world->dynamicsWorld->setDebugDrawer(&debug);
// Associate the new world with the new state and vice versa // Associate the new world with the new state and vice versa
state->world = world; state->world = world;
@ -481,7 +466,7 @@ int RWGame::run() {
RW_PROFILE_BEGIN("state"); RW_PROFILE_BEGIN("state");
if (StateManager::get().states.size() > 0) { if (StateManager::get().states.size() > 0) {
StateManager::get().draw(renderer); StateManager::get().draw(&renderer);
} }
RW_PROFILE_END(); RW_PROFILE_END();
RW_PROFILE_END(); RW_PROFILE_END();
@ -572,12 +557,12 @@ void RWGame::tick(float dt) {
} }
void RWGame::render(float alpha, float time) { void RWGame::render(float alpha, float time) {
lastDraws = getRenderer()->getRenderer()->getDrawCount(); lastDraws = getRenderer().getRenderer()->getDrawCount();
getRenderer()->getRenderer()->swap(); getRenderer().getRenderer()->swap();
glm::ivec2 windowSize = getWindow().getSize(); glm::ivec2 windowSize = getWindow().getSize();
renderer->setViewport(windowSize.x, windowSize.y); renderer.setViewport(windowSize.x, windowSize.y);
ViewCamera viewCam; ViewCamera viewCam;
viewCam.frustum.fov = glm::radians(90.f); viewCam.frustum.fov = glm::radians(90.f);
@ -638,13 +623,13 @@ void RWGame::render(float alpha, float time) {
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
renderer->getRenderer()->pushDebugGroup("World"); renderer.getRenderer()->pushDebugGroup("World");
RW_PROFILE_BEGIN("world"); RW_PROFILE_BEGIN("world");
renderer->renderWorld(world, viewCam, alpha); renderer.renderWorld(world, viewCam, alpha);
RW_PROFILE_END(); RW_PROFILE_END();
renderer->getRenderer()->popDebugGroup(); renderer.getRenderer()->popDebugGroup();
RW_PROFILE_BEGIN("debug"); RW_PROFILE_BEGIN("debug");
switch (debugview_) { switch (debugview_) {
@ -654,7 +639,7 @@ void RWGame::render(float alpha, float time) {
case DebugViewMode::Physics: case DebugViewMode::Physics:
if (world) { if (world) {
world->dynamicsWorld->debugDrawWorld(); world->dynamicsWorld->debugDrawWorld();
debug->flush(renderer); debug.flush(&renderer);
} }
break; break;
case DebugViewMode::Navigation: case DebugViewMode::Navigation:
@ -668,7 +653,7 @@ void RWGame::render(float alpha, float time) {
} }
RW_PROFILE_END(); RW_PROFILE_END();
drawOnScreenText(world, renderer); drawOnScreenText(world, &renderer);
} }
void RWGame::renderDebugStats(float time) { void RWGame::renderDebugStats(float time) {
@ -693,8 +678,8 @@ void RWGame::renderDebugStats(float time) {
ss << "FPS: " << (1000.f / time_average) << " (" << time_average << "ms)\n" ss << "FPS: " << (1000.f / time_average) << " (" << time_average << "ms)\n"
<< "Frame: " << time_ms << "ms\n" << "Frame: " << time_ms << "ms\n"
<< "Draws/Textures/Buffers: " << lastDraws << "/" << "Draws/Textures/Buffers: " << lastDraws << "/"
<< renderer->getRenderer()->getTextureCount() << "/" << renderer.getRenderer()->getTextureCount() << "/"
<< renderer->getRenderer()->getBufferCount() << "\n"; << renderer.getRenderer()->getBufferCount() << "\n";
TextRenderer::TextInfo ti; TextRenderer::TextInfo ti;
ti.text = GameStringUtil::fromString(ss.str()); ti.text = GameStringUtil::fromString(ss.str());
@ -702,7 +687,7 @@ void RWGame::renderDebugStats(float time) {
ti.screenPosition = glm::vec2(10.f, 10.f); ti.screenPosition = glm::vec2(10.f, 10.f);
ti.size = 15.f; ti.size = 15.f;
ti.baseColour = glm::u8vec3(255); ti.baseColour = glm::u8vec3(255);
renderer->text.renderText(ti); renderer.text.renderText(ti);
/*while( engine->log.size() > 0 && engine->log.front().time + 10.f < /*while( engine->log.size() > 0 && engine->log.front().time + 10.f <
engine->gameTime ) { engine->gameTime ) {
@ -744,16 +729,16 @@ void RWGame::renderDebugPaths(float time) {
for (AIGraphNode* n : world->aigraph.nodes) { for (AIGraphNode* n : world->aigraph.nodes) {
btVector3 p(n->position.x, n->position.y, n->position.z); btVector3 p(n->position.x, n->position.y, n->position.z);
auto& col = n->type == AIGraphNode::Pedestrian ? pedColour : roadColour; auto& col = n->type == AIGraphNode::Pedestrian ? pedColour : roadColour;
debug->drawLine(p - btVector3(0.f, 0.f, 1.f), debug.drawLine(p - btVector3(0.f, 0.f, 1.f),
p + btVector3(0.f, 0.f, 1.f), col); p + btVector3(0.f, 0.f, 1.f), col);
debug->drawLine(p - btVector3(1.f, 0.f, 0.f), debug.drawLine(p - btVector3(1.f, 0.f, 0.f),
p + btVector3(1.f, 0.f, 0.f), col); p + btVector3(1.f, 0.f, 0.f), col);
debug->drawLine(p - btVector3(0.f, 1.f, 0.f), debug.drawLine(p - btVector3(0.f, 1.f, 0.f),
p + btVector3(0.f, 1.f, 0.f), col); p + btVector3(0.f, 1.f, 0.f), col);
for (AIGraphNode* c : n->connections) { for (AIGraphNode* c : n->connections) {
btVector3 f(c->position.x, c->position.y, c->position.z); btVector3 f(c->position.x, c->position.y, c->position.z);
debug->drawLine(p, f, col); debug.drawLine(p, f, col);
} }
} }
@ -764,13 +749,13 @@ void RWGame::renderDebugPaths(float time) {
btVector3 maxColor(0.f, 1.f, 0.f); btVector3 maxColor(0.f, 1.f, 0.f);
btVector3 min(garage.min.x, garage.min.y, garage.min.z); btVector3 min(garage.min.x, garage.min.y, garage.min.z);
btVector3 max(garage.max.x, garage.max.y, garage.max.z); btVector3 max(garage.max.x, garage.max.y, garage.max.z);
debug->drawLine(min, min + btVector3(0.5f, 0.f, 0.f), minColor); debug.drawLine(min, min + btVector3(0.5f, 0.f, 0.f), minColor);
debug->drawLine(min, min + btVector3(0.f, 0.5f, 0.f), minColor); debug.drawLine(min, min + btVector3(0.f, 0.5f, 0.f), minColor);
debug->drawLine(min, min + btVector3(0.f, 0.f, 0.5f), minColor); debug.drawLine(min, min + btVector3(0.f, 0.f, 0.5f), minColor);
debug->drawLine(max, max - btVector3(0.5f, 0.f, 0.f), maxColor); debug.drawLine(max, max - btVector3(0.5f, 0.f, 0.f), maxColor);
debug->drawLine(max, max - btVector3(0.f, 0.5f, 0.f), maxColor); debug.drawLine(max, max - btVector3(0.f, 0.5f, 0.f), maxColor);
debug->drawLine(max, max - btVector3(0.f, 0.f, 0.5f), maxColor); debug.drawLine(max, max - btVector3(0.f, 0.f, 0.5f), maxColor);
} }
// Draw vehicle generators // Draw vehicle generators
@ -786,12 +771,12 @@ void RWGame::renderDebugPaths(float time) {
.rotate(btVector3(0.f, 0.f, 1.f), heading); .rotate(btVector3(0.f, 0.f, 1.f), heading);
auto left = btVector3(-0.15f, -0.15f, 0.f) auto left = btVector3(-0.15f, -0.15f, 0.f)
.rotate(btVector3(0.f, 0.f, 1.f), heading); .rotate(btVector3(0.f, 0.f, 1.f), heading);
debug->drawLine(position, position + back, color); debug.drawLine(position, position + back, color);
debug->drawLine(position, position + right, color); debug.drawLine(position, position + right, color);
debug->drawLine(position, position + left, color); debug.drawLine(position, position + left, color);
} }
debug->flush(renderer); debug.flush(&renderer);
} }
void RWGame::renderDebugObjects(float time, ViewCamera& camera) { void RWGame::renderDebugObjects(float time, ViewCamera& camera) {
@ -799,7 +784,7 @@ void RWGame::renderDebugObjects(float time, ViewCamera& camera) {
std::stringstream ss; std::stringstream ss;
ss << "Models: " << data->modelinfo.size() << "\n" ss << "Models: " << data.modelinfo.size() << "\n"
<< "Dynamic Objects:\n" << "Dynamic Objects:\n"
<< " Vehicles: " << world->vehiclePool.objects.size() << "\n" << " Vehicles: " << world->vehiclePool.objects.size() << "\n"
<< " Peds: " << world->pedestrianPool.objects.size() << "\n"; << " Peds: " << world->pedestrianPool.objects.size() << "\n";
@ -810,7 +795,7 @@ void RWGame::renderDebugObjects(float time, ViewCamera& camera) {
ti.screenPosition = glm::vec2(10.f, 10.f); ti.screenPosition = glm::vec2(10.f, 10.f);
ti.size = 15.f; ti.size = 15.f;
ti.baseColour = glm::u8vec3(255); ti.baseColour = glm::u8vec3(255);
renderer->text.renderText(ti); renderer.text.renderText(ti);
// Render worldspace overlay for nearby objects // Render worldspace overlay for nearby objects
constexpr float kNearbyDistance = 25.f; constexpr float kNearbyDistance = 25.f;
@ -832,7 +817,7 @@ void RWGame::renderDebugObjects(float time, ViewCamera& camera) {
screen.y = viewport.w - screen.y; screen.y = viewport.w - screen.y;
ti.screenPosition = glm::vec2(screen); ti.screenPosition = glm::vec2(screen);
ti.size = 10.f; ti.size = 10.f;
renderer->text.renderText(ti); renderer.text.renderText(ti);
}; };
for (auto& p : world->vehiclePool.objects) { for (auto& p : world->vehiclePool.objects) {

View File

@ -5,6 +5,7 @@
#include <engine/GameData.hpp> #include <engine/GameData.hpp>
#include <engine/GameWorld.hpp> #include <engine/GameWorld.hpp>
#include <render/GameRenderer.hpp> #include <render/GameRenderer.hpp>
#include <render/DebugDraw.hpp>
#include <script/ScriptMachine.hpp> #include <script/ScriptMachine.hpp>
#include "game.hpp" #include "game.hpp"
@ -13,14 +14,14 @@
class PlayerController; class PlayerController;
class RWGame : public GameBase { class RWGame : public GameBase {
WorkContext work;
GameData data;
GameRenderer renderer;
DebugDraw debug;
GameState* state = nullptr; GameState* state = nullptr;
GameData* data = nullptr;
GameWorld* world = nullptr; GameWorld* world = nullptr;
// must be allocated after Logger setup.
GameRenderer* renderer = nullptr;
ScriptMachine* script = nullptr; ScriptMachine* script = nullptr;
// Background worker
WorkContext* work = nullptr;
std::chrono::steady_clock clock; std::chrono::steady_clock clock;
std::chrono::steady_clock::time_point last_clock_time; std::chrono::steady_clock::time_point last_clock_time;
@ -62,11 +63,11 @@ public:
return world; return world;
} }
GameData* getGameData() const { const GameData& getGameData() const {
return data; return data;
} }
GameRenderer* getRenderer() const { GameRenderer& getRenderer() {
return renderer; return renderer;
} }

View File

@ -115,7 +115,7 @@ Menu* DebugState::createDebugMenu() {
kDebugEntryHeight)); kDebugEntryHeight));
m->addEntry(Menu::lambda( m->addEntry(Menu::lambda(
"Cull Here", "Cull Here",
[=] { game->getRenderer()->setCullOverride(true, _debugCam); }, [=] { game->getRenderer().setCullOverride(true, _debugCam); },
kDebugEntryHeight)); kDebugEntryHeight));
// Optional block if the player is in a vehicle // Optional block if the player is in a vehicle

View File

@ -11,8 +11,7 @@ MenuState::MenuState(RWGame* game) : State(game) {
} }
void MenuState::enterMainMenu() { void MenuState::enterMainMenu() {
auto data = game->getGameData(); auto& t = game->getGameData().texts;
auto& t = data->texts;
Menu* m = new Menu; Menu* m = new Menu;
m->offset = glm::vec2(200.f, 200.f); m->offset = glm::vec2(200.f, 200.f);

View File

@ -4,8 +4,7 @@
#include "RWGame.hpp" #include "RWGame.hpp"
PauseState::PauseState(RWGame* game) : State(game) { PauseState::PauseState(RWGame* game) : State(game) {
auto data = game->getGameData(); auto& t = game->getGameData().texts;
auto& t = data->texts;
Menu* m = new Menu; Menu* m = new Menu;
m->offset = glm::vec2(200.f, 200.f); m->offset = glm::vec2(200.f, 200.f);
@ -42,7 +41,7 @@ void PauseState::draw(GameRenderer* r) {
map.screenPosition = glm::vec2(vp.x / 2, vp.y / 2); map.screenPosition = glm::vec2(vp.x / 2, vp.y / 2);
map.screenSize = std::max(vp.x, vp.y); map.screenSize = std::max(vp.x, vp.y);
game->getRenderer()->map.draw(getWorld(), map); game->getRenderer().map.draw(getWorld(), map);
State::draw(r); State::draw(r);
} }