#ifndef CGUI_HPP
#define CGUI_HPP
#include <iostream>
#include <dlfcn.h>
#include <cstdint>
typedef void* (*ptr_frm_void)(void);
typedef void (*void_frm_voidp)(void*);
typedef char* (*charp_frm_voidp)(void*);
typedef void (*v_frm_voidp_u32_x2_u8_x3)(void*, uint32_t, uint32_t, uint8_t, uint8_t, uint8_t);
class CGui {
public:
static void* libcgui;
ptr_frm_void alloc_win;
void_frm_voidp init_win;
charp_frm_voidp event_tick_win;
v_frm_voidp_u32_x2_u8_x3 write_px_win;
void_frm_voidp redraw_dirty_win;
void* cgui_ptr;
CGui(std::string lib_location="./target/release/libcgui.so") {
if (CGui::libcgui == nullptr) {
CGui::libcgui = dlopen(lib_location.c_str(), RTLD_NOW);
if (CGui::libcgui == nullptr) {
std::cerr << "CGui: Fatal error, cannot open " << lib_location << std::endl;
}
}
this->alloc_win = (ptr_frm_void) dlsym(libcgui, "alloc_win");
this->init_win = (void_frm_voidp) dlsym(libcgui, "init");
this->event_tick_win = (charp_frm_voidp) dlsym(libcgui, "event_tick");
this->write_px_win = (v_frm_voidp_u32_x2_u8_x3) dlsym(libcgui, "write_px");
this->redraw_dirty_win = (void_frm_voidp) dlsym(libcgui, "redraw_dirty");
this->cgui_ptr = (*this->alloc_win)();
if (this->cgui_ptr == nullptr) {
std::cerr << "CGui: Failed to allocate cgui_ptr" << std::endl;
}
}
void init() {
(*this->init_win)(this->cgui_ptr);
}
std::string event_tick() {
return (*this->event_tick_win)(this->cgui_ptr);
}
void write_px(uint32_t x, uint32_t y, uint8_t red, uint8_t green, uint8_t blue) {
(*this->write_px_win)(this->cgui_ptr, x, y, red, green, blue);
}
void redraw_dirty() {
(*this->redraw_dirty_win)(this->cgui_ptr);
}
private:
};
void* CGui::libcgui = nullptr;
#endif