Improves return types with Progress type

This commit is contained in:
Jaby 2022-12-23 22:06:10 +01:00 committed by Jaby
parent 9857b367aa
commit c995fe0713
7 changed files with 71 additions and 15 deletions

View File

@ -1,7 +1,32 @@
#ifndef __JABYENGINE_LZ4_DECOMPRESSOR_HPP__ #ifndef __JABYENGINE_LZ4_DECOMPRESSOR_HPP__
#define __JABYENGINE_LZ4_DECOMPRESSOR_HPP__ #define __JABYENGINE_LZ4_DECOMPRESSOR_HPP__
#include "../../stddef.h"
#include "types.hpp"
namespace JabyEngine { namespace JabyEngine {
class LZ4Decompressor {
public:
enum struct Result {
InProgress = 0,
Done,
Error
};
private:
struct State {};
private:
uint8_t* dst_adr = nullptr;
State state;
public:
LZ4Decompressor() = default;
void setup(uint8_t* dst_adr);
void reset();
Progress process(const uint8_t* data, size_t size);
};
} }
#endif //!__JABYENGINE_LZ4_DECOMPRESSOR_HPP__ #endif //!__JABYENGINE_LZ4_DECOMPRESSOR_HPP__

View File

@ -0,0 +1,12 @@
#ifndef __JABYENGINE_TYPES_HPP__
#define __JABYENGINE_TYPES_HPP__
namespace JabyEngine {
enum struct Progress {
InProgress = 0,
Done,
Error
};
}
#endif //!__JABYENGINE_TYPES_HPP__

View File

@ -1,5 +1,6 @@
#ifndef __JABYENGINE_FILE_PROCESSOR_HPP__ #ifndef __JABYENGINE_FILE_PROCESSOR_HPP__
#define __JABYENGINE_FILE_PROCESSOR_HPP__ #define __JABYENGINE_FILE_PROCESSOR_HPP__
#include "../../Auxiliary/types.hpp"
#include "../file_types.hpp" #include "../file_types.hpp"
namespace JabyEngine { namespace JabyEngine {
@ -11,7 +12,11 @@ namespace JabyEngine {
}; };
struct Configuration; struct Configuration;
typedef bool (*ProcessRoutine)(Configuration&, Reserved&);
template<typename T>
using GenericProcessRoutine = Progress (*)(Configuration&, T&);
typedef GenericProcessRoutine<Reserved> ProcessRoutine;
struct Configuration { struct Configuration {
ProcessRoutine process_routine = nullptr; ProcessRoutine process_routine = nullptr;
@ -19,7 +24,7 @@ namespace JabyEngine {
size_t data_word_size = 0ull; size_t data_word_size = 0ull;
template<typename T> template<typename T>
static __always_inline Configuration from(bool (*process_routine)(Configuration&, T&), const uint32_t* data_adr) { static __always_inline Configuration from(GenericProcessRoutine<T> process_routine, const uint32_t* data_adr) {
return {reinterpret_cast<ProcessRoutine>(process_routine), data_adr}; return {reinterpret_cast<ProcessRoutine>(process_routine), data_adr};
} }
@ -34,13 +39,13 @@ namespace JabyEngine {
Reserved reserved; Reserved reserved;
template<typename T> template<typename T>
static __always_inline State from(const T& reserved, const uint32_t* data_adr, bool (*process_routine)(Configuration&, T&)) { static __always_inline State from(const T& reserved, const uint32_t* data_adr, GenericProcessRoutine<T> process_routine) {
return {Configuration::from(process_routine, data_adr), *reinterpret_cast<const Reserved*>(&reserved)}; return {Configuration::from(process_routine, data_adr), *reinterpret_cast<const Reserved*>(&reserved)};
static_assert(sizeof(T) <= sizeof(Reserved)); static_assert(sizeof(T) <= sizeof(Reserved));
} }
public: public:
bool process(size_t word_size) { Progress process(size_t word_size) {
this->config.data_word_size += word_size; this->config.data_word_size += word_size;
return (*this->config.process_routine)(this->config, this->reserved); return (*this->config.process_routine)(this->config, this->reserved);
} }

View File

@ -1,4 +1,16 @@
#include <PSX/Auxiliary/lz4_decompressor.hpp> #include <PSX/Auxiliary/lz4_decompressor.hpp>
namespace JabyEngine { namespace JabyEngine {
void LZ4Decompressor :: setup(uint8_t* dst_adr) {
this->dst_adr = dst_adr;
LZ4Decompressor::reset();
}
void LZ4Decompressor :: reset() {
}
Progress LZ4Decompressor :: process(const uint8_t* data, size_t size) {
return Progress::Error;
}
} }

View File

@ -13,7 +13,7 @@ namespace JabyEngine {
void display_logo() { void display_logo() {
// Upload SplashScreen picture // Upload SplashScreen picture
auto state = FileProcessor::create(reinterpret_cast<const uint32_t*>(SplashScreen), SimpleTIM(32, 0, 0, 0)); auto state = FileProcessor::create(reinterpret_cast<const uint32_t*>(SplashScreen), SimpleTIM(32, 0, 0, 0));
while(state.process((sizeof(SplashScreen)/sizeof(uint32_t)))); while(state.process((sizeof(SplashScreen)/sizeof(uint32_t))) == Progress::InProgress);
Display::enable(); Display::enable();
} }

View File

@ -1,5 +1,7 @@
#ifndef __JABYENGINE_INTERNAL_SIMPLE_HELPER_HPP__ #ifndef __JABYENGINE_INTERNAL_SIMPLE_HELPER_HPP__
#define __JABYENGINE_INTERNAL_SIMPLE_HELPER_HPP__ #define __JABYENGINE_INTERNAL_SIMPLE_HELPER_HPP__
// Instead of using friend we use this to access the private members
#define private public #define private public
#include <PSX/File/Processor/file_processor.hpp> #include <PSX/File/Processor/file_processor.hpp>
@ -17,7 +19,7 @@ namespace JabyEngine {
} }
template<typename T> template<typename T>
static bool exchange_and_execute_process_function(bool (*process_routine)(State::Configuration&, T&), State::Configuration& config, T& state) { static Progress exchange_and_execute_process_function(State::GenericProcessRoutine<T> process_routine, State::Configuration& config, T& state) {
config.process_routine = reinterpret_cast<State::ProcessRoutine>(process_routine); config.process_routine = reinterpret_cast<State::ProcessRoutine>(process_routine);
return process_routine(config, state); return process_routine(config, state);
} }

View File

@ -49,7 +49,7 @@ namespace JabyEngine {
set_gpu_receive(src, state.dst_info.getTextureX(), state.dst_info.getTextureY(), width, height); set_gpu_receive(src, state.dst_info.getTextureX(), state.dst_info.getTextureY(), width, height);
} }
static bool parse_data(State::Configuration& config, SimpleTIMState& state) { static Progress parse_data(State::Configuration& config, SimpleTIMState& state) {
const auto words_to_use = (config.data_word_size > state.words_left) ? state.words_left : config.data_word_size; const auto words_to_use = (config.data_word_size > state.words_left) ? state.words_left : config.data_word_size;
bool is_last = (words_to_use == state.words_left); bool is_last = (words_to_use == state.words_left);
auto block_count = (words_to_use >> 4); auto block_count = (words_to_use >> 4);
@ -77,7 +77,7 @@ namespace JabyEngine {
state.words_left = 0; state.words_left = 0;
config.processed(words_to_use); config.processed(words_to_use);
return false; return Progress::Done;
} }
else { else {
@ -85,18 +85,18 @@ namespace JabyEngine {
state.words_left -= words_used; state.words_left -= words_used;
config.processed(words_used); config.processed(words_used);
return true; return Progress::InProgress;
} }
} }
static bool switch_state_parse_data(State::Configuration& config, SimpleTIMState& state) { static Progress switch_state_parse_data(State::Configuration& config, SimpleTIMState& state) {
set_gpu_receive_data(config.data_adr, state, state.size_info.getTextureWidth(), state.size_info.getTextureHeight()); set_gpu_receive_data(config.data_adr, state, state.size_info.getTextureWidth(), state.size_info.getTextureHeight());
return Helper::exchange_and_execute_process_function(parse_data, config, state); return Helper::exchange_and_execute_process_function(parse_data, config, state);
} }
static bool parse_clut(State::Configuration& config, SimpleTIMState& state) { static Progress parse_clut(State::Configuration& config, SimpleTIMState& state) {
if(parse_data(config, state)) { if(const auto result = parse_data(config, state); result != Progress::Done) {
return true; return result;
} }
else { else {
@ -104,7 +104,7 @@ namespace JabyEngine {
} }
} }
static bool parse_header(State::Configuration& config, SimpleTIMState& state) { static Progress parse_header(State::Configuration& config, SimpleTIMState& state) {
if(config.data_word_size >= (sizeof(SimpleTIMSize)/sizeof(uint32_t))) { if(config.data_word_size >= (sizeof(SimpleTIMSize)/sizeof(uint32_t))) {
Helper::simple_read(state.size_info, config); Helper::simple_read(state.size_info, config);
@ -121,7 +121,7 @@ namespace JabyEngine {
} }
} }
return true; return Progress::InProgress;
} }
State create(const uint32_t* data_adr, const SimpleTIM& file) { State create(const uint32_t* data_adr, const SimpleTIM& file) {