1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-10-06 09:07:19 +02:00

Merge pull request #604 from ShFil119/minor_memory_improvements

Minor memory improvements
This commit is contained in:
Daniel Evans 2018-09-01 23:55:48 +01:00 committed by GitHub
commit 0e098b4fa0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 70 additions and 75 deletions

View File

@ -113,10 +113,10 @@ void Clump::recalculateMetrics() {
}
}
Clump* Clump::clone() const {
ClumpPtr Clump::clone() const {
// Clone frame hierarchy
auto newroot = rootframe_->cloneHierarchy();
auto clump = new Clump;
auto clump = std::make_shared<Clump>();
clump->setFrame(newroot);
// This isn't the most optimal implementation, but this code is likely

View File

@ -278,7 +278,7 @@ public:
/**
* @return A Copy of the frames and atomics in this clump
*/
Clump* clone() const;
ClumpPtr clone() const;
private:
float boundingRadius;

View File

@ -289,10 +289,10 @@ void GameData::loadHandling(const std::string& path) {
l.loadHandling(syspath, vehicleInfo);
}
SCMFile* GameData::loadSCM(const std::string& path) {
SCMFile GameData::loadSCM(const std::string& path) {
auto scm_h = index.openFileRaw(path);
SCMFile* scm = new SCMFile;
scm->loadFile(scm_h.data.get(), scm_h.length);
SCMFile scm{};
scm.loadFile(scm_h.data.get(), scm_h.length);
return scm;
}

View File

@ -104,7 +104,7 @@ public:
void loadHandling(const std::string& path);
SCMFile* loadSCM(const std::string& path);
SCMFile loadSCM(const std::string& path);
void loadGXT(const std::string& name);

View File

@ -560,7 +560,7 @@ bool SaveGame::loadGame(GameState& state, const std::string& file) {
BlockDword scriptVarCount;
READ_SIZE(scriptVarCount)
RW_ASSERT(scriptVarCount == state.script->getFile()->getGlobalsSize());
RW_ASSERT(scriptVarCount == state.script->getFile().getGlobalsSize());
if (fread(state.script->getGlobals(), sizeof(SCMByte), scriptVarCount,
loadFile) != scriptVarCount) {

View File

@ -24,7 +24,7 @@ void GenericDATLoader::loadDynamicObjects(const std::string& name,
if (lineBuff.at(0) == '*') continue;
std::stringstream ss(lineBuff);
DynamicObjectDataPtr dyndata(new DynamicObjectData);
auto dyndata = std::make_shared<DynamicObjectData>();
ss >> dyndata->modelName;
auto cpos = dyndata->modelName.find(',');
@ -73,7 +73,7 @@ void GenericDATLoader::loadWeapons(const std::string& name,
if (linebuffer[0] == '#') continue;
std::stringstream ss(linebuffer);
WeaponDataPtr data(new WeaponData);
auto data = std::make_shared<WeaponData>();
ss >> data->name;
if (data->name == "ENDWEAPONDATA") continue;

View File

@ -32,7 +32,7 @@ CharacterObject::CharacterObject(GameWorld* engine, const glm::vec3& pos,
: GameObject(engine, pos, rot, modelinfo)
, controller(controller) {
auto info = getModelInfo<PedModelInfo>();
setClump(ClumpPtr(info->getModel()->clone()));
setClump(info->getModel()->clone());
if (info->getModel()) {
setModel(info->getModel());
animator = new Animator(getClump());

View File

@ -15,7 +15,7 @@ CutsceneObject::CutsceneObject(GameWorld *engine, const glm::vec3 &pos,
else {
setModel(getModelInfo<ClumpModelInfo>()->getModel());
}
setClump(ClumpPtr(getModel()->clone()));
setClump(getModel()->clone());
animator = new Animator(getClump());
}

View File

@ -167,7 +167,7 @@ VehicleObject::VehicleObject(GameWorld* engine, const glm::vec3& pos,
}
setModel(getVehicle()->getModel());
setClump(ClumpPtr(getModelInfo<VehicleModelInfo>()->getModel()->clone()));
setClump(getModelInfo<VehicleModelInfo>()->getModel()->clone());
setupModel();
}

View File

@ -14,8 +14,6 @@
#include "render/GameRenderer.hpp"
DebugDraw::DebugDraw() {
lineBuff = new GeometryBuffer;
dbuff = new DrawBuffer;
dbuff->setFaceType(GL_LINES);
glGenTextures(1, &texture);
@ -31,11 +29,6 @@ DebugDraw::DebugDraw() {
maxlines = 0;
}
DebugDraw::~DebugDraw() {
delete dbuff;
delete lineBuff;
}
void DebugDraw::drawLine(const btVector3 &from, const btVector3 &to,
const btVector3 &color) {
btVector3 c = color * 255;
@ -65,7 +58,7 @@ void DebugDraw::flush(GameRenderer *renderer) {
renderer->getRenderer()->useProgram(shaderProgram);
lineBuff->uploadVertices(lines);
dbuff->addGeometry(lineBuff);
dbuff->addGeometry(lineBuff.get());
Renderer::DrawParameters dp;
dp.textures = {texture};
@ -75,7 +68,7 @@ void DebugDraw::flush(GameRenderer *renderer) {
dp.count = lines.size();
dp.diffuse = 1.f;
renderer->getRenderer()->drawArrays(glm::mat4(1.f), dbuff, dp);
renderer->getRenderer()->drawArrays(glm::mat4(1.f), dbuff.get(), dp);
renderer->getRenderer()->invalidate();

View File

@ -20,7 +20,7 @@ class GeometryBuffer;
class DebugDraw final : public btIDebugDraw {
public:
DebugDraw();
~DebugDraw() override;
~DebugDraw() override = default;
void drawLine(const btVector3 &from, const btVector3 &to,
const btVector3 &color) override;
@ -43,8 +43,8 @@ protected:
std::vector<GeometryVertex> lines;
size_t maxlines;
GeometryBuffer *lineBuff;
DrawBuffer *dbuff;
std::unique_ptr<GeometryBuffer> lineBuff = std::make_unique<GeometryBuffer>();
std::unique_ptr<DrawBuffer> dbuff = std::make_unique<DrawBuffer>();
//Ownership is handled by worldProg in renderer
Renderer::ShaderProgram *shaderProgram = nullptr;

View File

@ -29,20 +29,17 @@ GLuint compileShader(GLenum type, const char* source) {
GLint len;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
if (len > 1) {
GLchar* buffer = new GLchar[len];
glGetShaderInfoLog(shader, len, nullptr, buffer);
auto buffer = std::make_unique<GLchar[]>(len);
glGetShaderInfoLog(shader, len, nullptr, buffer.get());
GLint sourceLen;
glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &sourceLen);
GLchar* sourceBuff = new GLchar[sourceLen];
glGetShaderSource(shader, sourceLen, nullptr, sourceBuff);
auto sourceBuff = std::make_unique<GLchar[]>(sourceLen);
glGetShaderSource(shader, sourceLen, nullptr, sourceBuff.get());
RW_ERROR("[OGL] Shader InfoLog(" << shader << "):\n"
<< buffer << "\nSource:\n"
<< sourceBuff);
delete[] buffer;
delete[] sourceBuff;
<< buffer.get() << "\nSource:\n"
<< sourceBuff.get());
}
if (status != GL_TRUE) {
@ -78,13 +75,11 @@ GLuint compileProgram(const char* vertex, const char* fragment) {
GLint len;
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &len);
if (len > 1) {
GLchar* buffer = new GLchar[len];
glGetProgramInfoLog(prog, len, nullptr, buffer);
auto buffer = std::make_unique<GLchar[]>(len);
glGetProgramInfoLog(prog, len, nullptr, buffer.get());
RW_ERROR("[OGL] Program InfoLog(" << prog << "):\n"
<< buffer);
delete[] buffer;
<< buffer.get());
}
if (status != GL_TRUE) {

View File

@ -4,8 +4,8 @@
#include <cstddef>
void SCMFile::loadFile(char *data, unsigned int size) {
_data = new SCMByte[size];
std::copy(data, data + size, _data);
_data = std::make_unique<SCMByte[]>(size);
std::copy(data, data + size, _data.get());
// Bytes required to hop over a jump opcode.
const unsigned int jumpOpSize = 2u + 1u + 4u;

View File

@ -17,20 +17,27 @@ public:
enum SCMTarget { NoTarget = 0, GTAIII = 0xC6, GTAVC = 0x6D, GTASA = 0x73 };
SCMFile() = default;
SCMFile(SCMFile&& info) = default;
SCMFile(SCMFile& info) = delete;
~SCMFile() {
delete[] _data;
~SCMFile() = default;
SCMFile& operator=(SCMFile&& info) = default;
SCMFile& operator=(SCMFile& info) = delete;
operator bool() {
return _data.get();
}
void loadFile(char* data, unsigned int size);
SCMByte* data() const {
return _data;
return _data.get();
}
template <class T>
T read(unsigned int offset) const {
return bit_cast<T>(*(_data + offset));
return bit_cast<T>(*(_data.get() + offset));
}
uint32_t getMainSize() const {
@ -66,7 +73,7 @@ public:
}
private:
SCMByte* _data = nullptr;
std::unique_ptr<SCMByte[]> _data;
SCMTarget _target{NoTarget};

View File

@ -35,7 +35,7 @@ const char* script::getBlipSprite(ScriptRadarSprite sprite) {
ScriptModel script::getModel(const ScriptArguments& args, ScriptModel model) {
if (model < 0) {
/// @todo verify that this is how the game uses negative models
const auto& m = args.getVM()->getFile()->getModels()[-model];
const auto& m = args.getVM()->getFile().getModels()[-model];
return args.getWorld()->data->findModelObject(m);
}
return model;

View File

@ -36,7 +36,7 @@ void ScriptMachine::executeThread(SCMThread& t, int msPassed) {
while (t.wakeCounter == 0) {
auto pc = t.programCounter;
auto opcode = file->read<SCMOpcode>(pc);
auto opcode = file.read<SCMOpcode>(pc);
bool isNegatedConditional = ((opcode & SCM_NEGATE_CONDITIONAL_MASK) ==
SCM_NEGATE_CONDITIONAL_MASK);
@ -56,7 +56,7 @@ void ScriptMachine::executeThread(SCMThread& t, int msPassed) {
auto requiredParams = std::abs(code.arguments);
for (int p = 0; p < requiredParams || hasExtraParameters; ++p) {
auto type_r = file->read<SCMByte>(pc);
auto type_r = file.read<SCMByte>(pc);
auto type = static_cast<SCMType>(type_r);
if (type_r > 42) {
@ -72,27 +72,27 @@ void ScriptMachine::executeThread(SCMThread& t, int msPassed) {
hasExtraParameters = false;
break;
case TInt8:
parameters.back().integer = file->read<std::int8_t>(pc);
parameters.back().integer = file.read<std::int8_t>(pc);
pc += sizeof(SCMByte);
break;
case TInt16:
parameters.back().integer = file->read<std::int16_t>(pc);
parameters.back().integer = file.read<std::int16_t>(pc);
pc += sizeof(SCMByte) * 2;
break;
case TGlobal: {
auto v = file->read<std::uint16_t>(pc);
auto v = file.read<std::uint16_t>(pc);
parameters.back().globalPtr =
globalData.data() + v; //* SCM_VARIABLE_SIZE;
if (v >= file->getGlobalsSize()) {
if (v >= file.getGlobalsSize()) {
state->world->logger->error(
"SCM", "Global Out of bounds! " +
std::to_string(v) + " " +
std::to_string(file->getGlobalsSize()));
std::to_string(file.getGlobalsSize()));
}
pc += sizeof(SCMByte) * 2;
} break;
case TLocal: {
auto v = file->read<std::uint16_t>(pc);
auto v = file.read<std::uint16_t>(pc);
parameters.back().globalPtr =
t.locals.data() + v * SCM_VARIABLE_SIZE;
if (v >= SCM_THREAD_LOCAL_SIZE) {
@ -102,17 +102,17 @@ void ScriptMachine::executeThread(SCMThread& t, int msPassed) {
pc += sizeof(SCMByte) * 2;
} break;
case TInt32:
parameters.back().integer = file->read<std::int32_t>(pc);
parameters.back().integer = file.read<std::int32_t>(pc);
pc += sizeof(SCMByte) * 4;
break;
case TString:
std::copy(file->data() + pc, file->data() + pc + 8,
std::copy(file.data() + pc, file.data() + pc + 8,
parameters.back().string);
pc += sizeof(SCMByte) * 8;
break;
case TFloat16:
parameters.back().real =
file->read<std::int16_t>(pc) / 16.f;
file.read<std::int16_t>(pc) / 16.f;
pc += sizeof(SCMByte) * 2;
break;
default:
@ -185,7 +185,7 @@ void ScriptMachine::executeThread(SCMThread& t, int msPassed) {
}
}
ScriptMachine::ScriptMachine(GameState* _state, SCMFile* file,
ScriptMachine::ScriptMachine(GameState* _state, SCMFile& file,
ScriptModule* ops)
: file(file)
, module(ops)
@ -193,10 +193,10 @@ ScriptMachine::ScriptMachine(GameState* _state, SCMFile* file,
, debugFlag(false)
, randomNumberGen(std::random_device()()) {
// Copy globals
auto size = file->getGlobalsSize();
auto size = file.getGlobalsSize();
globalData.resize(size);
auto offset = file->getGlobalSection();
std::copy(file->data() + offset, file->data() + offset + size,
auto offset = file.getGlobalSection();
std::copy(file.data() + offset, file.data() + offset + size,
globalData.begin());
}

View File

@ -129,10 +129,10 @@ struct SCMThread {
*/
class ScriptMachine {
public:
ScriptMachine(GameState* state, SCMFile* file, ScriptModule* ops);
ScriptMachine(GameState* state, SCMFile& file, ScriptModule* ops);
~ScriptMachine() = default;
SCMFile* getFile() const {
SCMFile& getFile() const {
return file;
}
@ -185,7 +185,7 @@ public:
void execute(float dt);
private:
SCMFile* file = nullptr;
SCMFile& file;
ScriptModule* module = nullptr;
GameState* state = nullptr;
bool debugFlag;

View File

@ -32,7 +32,7 @@ int ScriptArguments::getModel(unsigned int arg) const {
/// @todo verify this behaviour
if (id < 0) {
id = -id;
const auto& model = getVM()->getFile()->getModels()[id];
const auto& model = getVM()->getFile().getModels()[id];
id = getWorld()->data->findModelObject(model);
}

View File

@ -9611,7 +9611,7 @@ void opcode_0362(const ScriptArguments& args, const ScriptCharacter character,
@arg visible Boolean true/false
*/
void opcode_0363(const ScriptArguments& args, ScriptVec3 coord, const ScriptFloat radius, const ScriptModel model, const ScriptBoolean visible) {
auto& models = args.getVM()->getFile()->getModels();
auto& models = args.getVM()->getFile().getModels();
auto& modelName = models[-model];
// Attempt to find the closest object
@ -10874,8 +10874,8 @@ void opcode_03b6(const ScriptArguments& args, ScriptVec3 coord, const ScriptFloa
int name0 = std::abs(model0);
int name1 = std::abs(model1);
auto oldmodel = args.getVM()->getFile()->getModels()[name0];
auto newmodel = args.getVM()->getFile()->getModels()[name1];
auto oldmodel = args.getVM()->getFile().getModels()[name0];
auto newmodel = args.getVM()->getFile().getModels()[name1];
std::transform(newmodel.begin(), newmodel.end(), newmodel.begin(), ::tolower);
std::transform(oldmodel.begin(), oldmodel.end(), oldmodel.begin(), ::tolower);
@ -12030,7 +12030,7 @@ void opcode_0415(const ScriptArguments& args, const ScriptVehicle vehicle, const
@arg arg1
*/
void opcode_0417(const ScriptArguments& args, const ScriptInt arg1) {
auto offset = args.getVM()->getFile()->getMissionOffsets()[arg1];
auto offset = args.getVM()->getFile().getMissionOffsets()[arg1];
args.getVM()->startThread(offset, true);
}

View File

@ -152,10 +152,9 @@ void RWGame::loadGame(const std::string& savename) {
}
void RWGame::startScript(const std::string& name) {
script.reset(data.loadSCM(name));
script = data.loadSCM(name);
if (script) {
vm = std::make_unique<ScriptMachine>(&state, script.get(), &opcodes);
vm = std::make_unique<ScriptMachine>(&state, script, &opcodes);
state.script = vm.get();
} else {
log.error("Game", "Failed to load SCM: " + name);

View File

@ -11,6 +11,7 @@
#include <engine/GameWorld.hpp>
#include <render/DebugDraw.hpp>
#include <render/GameRenderer.hpp>
#include <script/SCMFile.hpp>
#include <script/ScriptMachine.hpp>
#include <script/modules/GTA3Module.hpp>
#include "game.hpp"
@ -29,7 +30,7 @@ class RWGame final : public GameBase {
GTA3Module opcodes;
std::unique_ptr<ScriptMachine> vm;
std::unique_ptr<SCMFile> script;
SCMFile script;
bool inFocus = true;
ViewCamera currentCam;

View File

@ -319,7 +319,7 @@ std::shared_ptr<Menu> DebugState::createMissionsMenu() {
if (vm) {
std::list<SCMThread>& threads = vm->getThreads();
const auto& offsets = vm->getFile()->getMissionOffsets();
const auto& offsets = vm->getFile().getMissionOffsets();
RW_ASSERT(!offsets.empty());