Minor refactor

This commit is contained in:
MihailRis
2023-12-05 23:01:29 +03:00
parent 08701bca3c
commit 64295e2170
3 changed files with 30 additions and 14 deletions
+9 -14
View File
@@ -3,7 +3,6 @@
#include <iostream> #include <iostream>
#include <limits.h> #include <limits.h>
#include <memory> #include <memory>
#include <chrono>
#include "../voxels/Block.h" #include "../voxels/Block.h"
#include "../voxels/Chunk.h" #include "../voxels/Chunk.h"
@@ -17,22 +16,18 @@
#include "../world/Level.h" #include "../world/Level.h"
#include "../world/World.h" #include "../world/World.h"
#include "../maths/voxmaths.h" #include "../maths/voxmaths.h"
#include "../util/timeutil.h"
const uint MAX_WORK_PER_FRAME = 16; const uint MAX_WORK_PER_FRAME = 64;
const uint MIN_SURROUNDING = 9; const uint MIN_SURROUNDING = 9;
using std::unique_ptr; using std::unique_ptr;
using std::shared_ptr; using std::shared_ptr;
using std::chrono::high_resolution_clock;
using std::chrono::duration_cast;
using std::chrono::microseconds;
ChunksController::ChunksController(Level* level,
ChunksController::ChunksController( Chunks* chunks,
Level* level, Lighting* lighting,
Chunks* chunks, uint padding)
Lighting* lighting,
uint padding)
: level(level), : level(level),
chunks(chunks), chunks(chunks),
lighting(lighting), lighting(lighting),
@@ -46,11 +41,11 @@ ChunksController::~ChunksController(){
void ChunksController::update(int64_t maxDuration) { void ChunksController::update(int64_t maxDuration) {
int64_t mcstotal = 0; int64_t mcstotal = 0;
for (uint i = 0; i < MAX_WORK_PER_FRAME; i++) { for (uint i = 0; i < MAX_WORK_PER_FRAME; i++) {
auto start = high_resolution_clock::now(); timeutil::Timer timer;
if (loadVisible()) { if (loadVisible()) {
auto elapsed = high_resolution_clock::now() - start; int64_t mcs = timer.stop();
int64_t mcs = duration_cast<microseconds>(elapsed).count();
avgDurationMcs = mcs * 0.2 + avgDurationMcs * 0.8; avgDurationMcs = mcs * 0.2 + avgDurationMcs * 0.8;
if (mcstotal + max(avgDurationMcs, mcs) * 2 < maxDuration * 1000) { if (mcstotal + max(avgDurationMcs, mcs) * 2 < maxDuration * 1000) {
mcstotal += mcs; mcstotal += mcs;
+11
View File
@@ -1,5 +1,16 @@
#include "timeutil.h" #include "timeutil.h"
using std::chrono::high_resolution_clock;
using std::chrono::duration_cast;
using std::chrono::microseconds;
timeutil::Timer::Timer() {
start = high_resolution_clock::now();
}
int64_t timeutil::Timer::stop() {
return duration_cast<microseconds>(high_resolution_clock::now()-start).count();
}
float timeutil::time_value(float hour, float minute, float second) { float timeutil::time_value(float hour, float minute, float second) {
return (hour + (minute + second / 60.0f) / 60.0f) / 24.0f; return (hour + (minute + second / 60.0f) / 60.0f) / 24.0f;
} }
+10
View File
@@ -1,7 +1,17 @@
#ifndef UTIL_TIMEUTIL_H_ #ifndef UTIL_TIMEUTIL_H_
#define UTIL_TIMEUTIL_H_ #define UTIL_TIMEUTIL_H_
#include "../typedefs.h"
#include <chrono>
namespace timeutil { namespace timeutil {
class Timer {
std::chrono::high_resolution_clock::time_point start;
public:
Timer();
int64_t stop();
};
float time_value(float hour, float minute, float second); float time_value(float hour, float minute, float second);
void from_value(float value, int& hour, int& minute, int& second); void from_value(float value, int& hour, int& minute, int& second);
} }