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

View File

@ -3,7 +3,6 @@
#include <iostream>
#include <limits.h>
#include <memory>
#include <chrono>
#include "../voxels/Block.h"
#include "../voxels/Chunk.h"
@ -17,22 +16,18 @@
#include "../world/Level.h"
#include "../world/World.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;
using std::unique_ptr;
using std::shared_ptr;
using std::chrono::high_resolution_clock;
using std::chrono::duration_cast;
using std::chrono::microseconds;
ChunksController::ChunksController(
Level* level,
Chunks* chunks,
Lighting* lighting,
uint padding)
ChunksController::ChunksController(Level* level,
Chunks* chunks,
Lighting* lighting,
uint padding)
: level(level),
chunks(chunks),
lighting(lighting),
@ -46,11 +41,11 @@ ChunksController::~ChunksController(){
void ChunksController::update(int64_t maxDuration) {
int64_t mcstotal = 0;
for (uint i = 0; i < MAX_WORK_PER_FRAME; i++) {
auto start = high_resolution_clock::now();
timeutil::Timer timer;
if (loadVisible()) {
auto elapsed = high_resolution_clock::now() - start;
int64_t mcs = duration_cast<microseconds>(elapsed).count();
int64_t mcs = timer.stop();
avgDurationMcs = mcs * 0.2 + avgDurationMcs * 0.8;
if (mcstotal + max(avgDurationMcs, mcs) * 2 < maxDuration * 1000) {
mcstotal += mcs;

View File

@ -1,5 +1,16 @@
#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) {
return (hour + (minute + second / 60.0f) / 60.0f) / 24.0f;
}

View File

@ -1,7 +1,17 @@
#ifndef UTIL_TIMEUTIL_H_
#define UTIL_TIMEUTIL_H_
#include "../typedefs.h"
#include <chrono>
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);
void from_value(float value, int& hour, int& minute, int& second);
}