Minor refactor

This commit is contained in:
MihailRis
2024-01-03 20:42:26 +03:00
parent 0d36f52bdd
commit 2ad115a2a8
9 changed files with 19 additions and 20 deletions
+4 -4
View File
@@ -3,6 +3,7 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <cstring> #include <cstring>
#include <memory>
#include <type_traits> #include <type_traits>
#ifdef __APPLE__ #ifdef __APPLE__
@@ -192,13 +193,12 @@ char* load_wav(const std::string& filename,
return nullptr; return nullptr;
} }
char* data = new char[size]; std::unique_ptr<char[]> data (new char[size]);
try { try {
in.read(data, size); in.read(data.get(), size);
return data; return data.release();
} }
catch (const std::exception&) { catch (const std::exception&) {
delete[] data;
std::cerr << "ERROR: Could not load wav data of \"" << filename << "\"" << std::endl; std::cerr << "ERROR: Could not load wav data of \"" << filename << "\"" << std::endl;
return nullptr; return nullptr;
} }
+1 -1
View File
@@ -356,7 +356,7 @@ JObject& JObject::put(string key, int value) {
JObject& JObject::put(string key, int64_t value) { JObject& JObject::put(string key, int64_t value) {
auto found = map.find(key); auto found = map.find(key);
if (found != map.end()) delete found->second; if (found != map.end()) found->second;
valvalue val; valvalue val;
val.integer = value; val.integer = value;
map.insert(make_pair(key, new Value(valtype::integer, val))); map.insert(make_pair(key, new Value(valtype::integer, val)));
+2 -3
View File
@@ -66,7 +66,7 @@ Engine::Engine(EngineSettings& settings, EnginePaths* paths)
} }
Audio::initialize(); Audio::initialize();
gui = new gui::GUI(); gui = std::make_unique<gui::GUI>();
if (settings.ui.language == "auto") { if (settings.ui.language == "auto") {
settings.ui.language = langs::locale_by_envlocale(platform::detect_locale(), paths->getResources()); settings.ui.language = langs::locale_by_envlocale(platform::detect_locale(), paths->getResources());
} }
@@ -125,7 +125,6 @@ void Engine::mainloop() {
Engine::~Engine() { Engine::~Engine() {
scripting::close(); scripting::close();
screen = nullptr; screen = nullptr;
delete gui;
Audio::finalize(); Audio::finalize();
@@ -182,7 +181,7 @@ void Engine::setLanguage(std::string locale) {
} }
gui::GUI* Engine::getGUI() { gui::GUI* Engine::getGUI() {
return gui; return gui.get();
} }
EngineSettings& Engine::getSettings() { EngineSettings& Engine::getSettings() {
+1 -1
View File
@@ -40,7 +40,7 @@ class Engine {
double lastTime = 0.0; double lastTime = 0.0;
double delta = 0.0; double delta = 0.0;
gui::GUI* gui; std::unique_ptr<gui::GUI> gui;
public: public:
Engine(EngineSettings& settings, EnginePaths* paths); Engine(EngineSettings& settings, EnginePaths* paths);
~Engine(); ~Engine();
+5 -5
View File
@@ -73,12 +73,10 @@ WorldFiles::WorldFiles(fs::path directory, const DebugSettings& settings)
: directory(directory), : directory(directory),
generatorTestMode(settings.generatorTestMode), generatorTestMode(settings.generatorTestMode),
doWriteLights(settings.doWriteLights) { doWriteLights(settings.doWriteLights) {
compressionBuffer = new ubyte[CHUNK_DATA_LEN * 2]; compressionBuffer.reset(new ubyte[CHUNK_DATA_LEN * 2]);
} }
WorldFiles::~WorldFiles(){ WorldFiles::~WorldFiles(){
delete[] compressionBuffer;
regions.clear();
} }
WorldRegion* WorldFiles::getRegion(regionsmap& regions, int x, int z) { WorldRegion* WorldFiles::getRegion(regionsmap& regions, int x, int z) {
@@ -98,10 +96,12 @@ WorldRegion* WorldFiles::getOrCreateRegion(regionsmap& regions, int x, int z) {
} }
ubyte* WorldFiles::compress(const ubyte* src, size_t srclen, size_t& len) { ubyte* WorldFiles::compress(const ubyte* src, size_t srclen, size_t& len) {
len = extrle::encode(src, srclen, compressionBuffer); ubyte* buffer = this->compressionBuffer.get();
len = extrle::encode(src, srclen, buffer);
ubyte* data = new ubyte[len]; ubyte* data = new ubyte[len];
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
data[i] = compressionBuffer[i]; data[i] = buffer[i];
} }
return data; return data;
} }
+1 -1
View File
@@ -107,7 +107,7 @@ public:
regionsmap regions; regionsmap regions;
regionsmap lights; regionsmap lights;
std::filesystem::path directory; std::filesystem::path directory;
ubyte* compressionBuffer; std::unique_ptr<ubyte[]> compressionBuffer;
bool generatorTestMode; bool generatorTestMode;
bool doWriteLights; bool doWriteLights;
+2 -2
View File
@@ -10,7 +10,7 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
files::rafile::rafile(std::filesystem::path filename) files::rafile::rafile(fs::path filename)
: file(filename, std::ios::binary | std::ios::ate) { : file(filename, std::ios::binary | std::ios::ate) {
if (!file) { if (!file) {
throw std::runtime_error("could not to open file "+filename.string()); throw std::runtime_error("could not to open file "+filename.string());
@@ -102,7 +102,7 @@ json::JObject* files::read_json(fs::path file) {
} }
} }
std::vector<std::string> files::read_list(std::filesystem::path filename) { std::vector<std::string> files::read_list(fs::path filename) {
std::ifstream file(filename); std::ifstream file(filename);
if (!file) { if (!file) {
throw std::runtime_error("could not to open file "+filename.u8string()); throw std::runtime_error("could not to open file "+filename.u8string());
+1 -2
View File
@@ -18,11 +18,10 @@ BlocksPreview::BlocksPreview(Shader* shader,
Atlas* atlas, Atlas* atlas,
const ContentGfxCache* cache) const ContentGfxCache* cache)
: shader(shader), atlas(atlas), cache(cache) { : shader(shader), atlas(atlas), cache(cache) {
batch = new Batch3D(1024); batch = std::make_unique<Batch3D>(1024);
} }
BlocksPreview::~BlocksPreview() { BlocksPreview::~BlocksPreview() {
delete batch;
} }
void BlocksPreview::begin(const Viewport* viewport) { void BlocksPreview::begin(const Viewport* viewport) {
+2 -1
View File
@@ -3,6 +3,7 @@
#include "../typedefs.h" #include "../typedefs.h"
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <memory>
class Viewport; class Viewport;
class Shader; class Shader;
@@ -14,7 +15,7 @@ class ContentGfxCache;
class BlocksPreview { class BlocksPreview {
Shader* shader; Shader* shader;
Atlas* atlas; Atlas* atlas;
Batch3D* batch; std::unique_ptr<Batch3D> batch;
const ContentGfxCache* const cache; const ContentGfxCache* const cache;
const Viewport* viewport; const Viewport* viewport;
public: public: