Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
Even more stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
OFFTKP committed Mar 21, 2024
1 parent 8f4c924 commit 813bee6
Show file tree
Hide file tree
Showing 12 changed files with 554 additions and 41 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ set(HYDRA_QT_FILES
)

set(HYDRA_IMGUI_FILES
src/hydra/sdl3/opengl.cxx
src/hydra/sdl3/vulkan.cxx
src/hydra/sdl3/common.cxx
vendored/imgui/backends/imgui_impl_sdl3.cpp
Expand All @@ -189,6 +190,8 @@ set(HYDRA_IMGUI_FILES

set(HYDRA_COMMON_FILES
src/main.cxx
src/hydra/core/wrapper.cxx
src/hydra/core/imports.cxx
src/hydra/common/version.cxx
src/hydra/common/settings.cxx
vendored/argparse/argparse.c
Expand Down
2 changes: 1 addition & 1 deletion core
Submodule core updated from ec8808 to c78e81
37 changes: 37 additions & 0 deletions include/hydra/core/wrapper.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include <filesystem>
#include <hydra/core.h>
#include <hydra/dynlib/dynlib.hxx>

namespace hydra::core
{
struct Wrapper final
{
private:
using hcGetCoreInfoPtr = void (*)(HcCoreInfo* coreInfo);
using hcCreatePtr = HcResult (*)(HcEnvironmentInfo* environmentInfo);
using hcDestroyPtr = HcResult (*)(const HcDestroyInfo* destroyInfo);
using hcResetPtr = HcResult (*)(const HcResetInfo* resetInfo);
using hcSetRunStatePtr = HcResult (*)(const HcRunStateInfo* runInfo);
using hcLoadContentPtr = HcResult (*)(const HcContentLoadInfo* info);
using hcGetErrorPtr = const char* (*)();

public:
Wrapper(const std::filesystem::path& path);
~Wrapper();

bool okay() const;

hcGetCoreInfoPtr hcGetCoreInfo;
hcCreatePtr hcCreate;
hcDestroyPtr hcDestroy;
hcResetPtr hcReset;
hcSetRunStatePtr hcSetRunState;
hcLoadContentPtr hcLoadContent;
hcGetErrorPtr hcGetError;

private:
hydra::dynlib::handle_t handle;
};
} // namespace hydra::core
90 changes: 90 additions & 0 deletions include/hydra/dynlib/dynlib.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#if defined(HYDRA_LIBDL)
#include <dlfcn.h>
#elif defined(HYDRA_WINDOWS)
#include <windows.h>
#endif

#include <filesystem>
#include <string>
#include <string_view>

namespace hydra::dynlib
{
typedef void* handle_t;

inline handle_t open(const std::filesystem::path& path)
{
#if defined(HYDRA_LIBDL)
return dlopen(path.string().c_str(), RTLD_LAZY);
#elif defined(HYDRA_WINDOWS)
std::string spath = path.string();
std::wstring wpath = std::wstring(spath.c_str(), spath.c_str() + spath.size());
printf("Trying to convert string to wstring to load library with loadlibraryw, this is "
"untested\n");
return (void*)LoadLibraryW(wpath.c_str());
#else
#pragma message("dynlib_open not implemented for this platform")
#error dynlib_open not implemented for this platform
return nullptr;
#endif
}

inline void* symbol(handle_t handle, const char* name)
{
#if defined(HYDRA_LIBDL)
return dlsym(handle, name);
#elif defined(HYDRA_WINDOWS)
return (void*)GetProcAddress((HMODULE)handle, name);
#else
#pragma message("dynlib_get_symbol not implemented for this platform")
#error dynlib_get_symbol not implemented for this platform
return nullptr;
#endif
}

inline void close(handle_t handle)
{
#if defined(HYDRA_LIBDL)
dlclose(handle);
#elif defined(HYDRA_WINDOWS)
FreeLibrary((HMODULE)handle);
#else
#pragma message("dynlib_close not implemented for this platform")
#error dynlib_close not implemented for this platform
#endif
}

inline std::string_view extension()
{
#if defined(HYDRA_LINUX) || defined(HYDRA_ANDROID) || defined(HYDRA_FREEBSD)
return ".so";
#elif defined(HYDRA_WEB)
return ".wasm";
#elif defined(HYDRA_MACOS)
return ".dylib";
#elif defined(HYDRA_WINDOWS)
return ".dll";
#else
#pragma message("dynlib_get_extension not implemented for this platform")
#error dynlib_get_extension not implemented for this platform
return "";
#endif
}

inline std::string error()
{
#if defined(HYDRA_LIBDL)
return dlerror();
#elif defined(HYDRA_WINDOWS)
// DWORD error = GetLastError();
// LPVOID buffer;
// FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, error,
// 0,
// (LPWSTR)&buffer, 0, NULL);
// std::string ret = (char*)buffer;
// LocalFree(buffer);
// return ret;
return ""; // TODO: fix
#endif
}
} // namespace hydra::dynlib
31 changes: 16 additions & 15 deletions include/hydra/sdl3/window.hxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <hydra/core.h>
#include <SDL3/SDL.h>

namespace hydra
Expand All @@ -8,35 +9,35 @@ namespace hydra
{
struct Context
{
HcRendererType rendererType;
SDL_Window* window;
void* inner;
};

Context* init(const HcEnvironmentInfo* createInfo);
void present(Context* context);
void shutdown(Context* context);

namespace Vk
{
Context* init();
Context* init(const HcEnvironmentInfo* createInfo);
void present(Context* context);
void shutdown(Context* context);
void startFrame(Context* context);
void endFrame(Context* context);
} // namespace Vk

namespace Gl
{
Context* init();
Context* init(const HcEnvironmentInfo* createInfo);
void present(Context* context);
void shutdown(Context* context);
void startFrame(Context* context);
void endFrame(Context* context);
} // namespace Gl

namespace Common
enum class EventResult
{
enum class EventResult
{
Continue,
Quit
};
Continue,
Quit
};

EventResult poll(Context* context);
} // namespace Common
} // namespace SDL3
EventResult poll(Context* context);
} // namespace SDL3
} // namespace hydra
2 changes: 1 addition & 1 deletion src/app.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ int imgui_main(int argc, char* argv[])
while (result != hydra::SDL3::EventResult::Quit)
#endif
{
result = hydra::SDL3::Common::Poll(window);
result = hydra::SDL3::Poll(window);
hydra::SDL3::Renderer::StartFrame(ctx);

ImGuiIO& io = ImGui::GetIO();
Expand Down
91 changes: 91 additions & 0 deletions src/hydra/core/imports.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Define the hydra core API imports

#include "compatibility.hxx"
#include <cstdio>
#include <hydra/core.h>

extern "C" {

void hydra_hcGetHostInfo(HcHostInfo* hostInfo)
{
printf("STUB: hcGetHostInfo\n");
}

HcResult hydra_hcGetInputsSync(const HcInputRequest* const* requests, int requestCount,
const int64_t* const* values)
{
printf("STUB: hcGetInputsSync\n");
return HC_SUCCESS;
}

HcResult hydra_hcReconfigureEnvironment(const HcEnvironmentInfo* environmentInfo)
{
printf("STUB: hcReconfigureEnvironment\n");
return HC_SUCCESS;
}

HcResult hydra_hcPushSamples(const HcAudioData* audioData)
{
printf("STUB: hcPushSamples\n");
return HC_SUCCESS;
}

HcResult hydra_hcSwPushVideoFrame(const HcImageData* image)
{
printf("STUB: hcSwPushVideoFrame\n");
return HC_SUCCESS;
}

HcResult hydra_hcGlMakeCurrent()
{
printf("STUB: hcGlMakeCurrent\n");
return HC_SUCCESS;
}

HcResult hydra_hcGlSwapBuffers()
{
printf("STUB: hcGlSwapBuffers\n");
return HC_SUCCESS;
}

void* hydra_hcGlGetProcAddress(const char* name)
{
printf("STUB: hcGlGetProcAddress\n");
return nullptr;
}

HcResult hydra_hcSetCallbacks(const HcCallbacks* callbacks)
{
printf("STUB: hcSetCallbacks\n");
return HC_SUCCESS;
}

void* hydra_GetAddress(const char* name)
{
uint32_t hash = hydra::str_hash(name);

switch (hash)
{
case hydra::str_hash("hcGetHostInfo"):
return (void*)hydra_hcGetHostInfo;
case hydra::str_hash("hcGetInputsSync"):
return (void*)hydra_hcGetInputsSync;
case hydra::str_hash("hcReconfigureEnvironment"):
return (void*)hydra_hcReconfigureEnvironment;
case hydra::str_hash("hcPushSamples"):
return (void*)hydra_hcPushSamples;
case hydra::str_hash("hcSwPushVideoFrame"):
return (void*)hydra_hcSwPushVideoFrame;
case hydra::str_hash("hcGlMakeCurrent"):
return (void*)hydra_hcGlMakeCurrent;
case hydra::str_hash("hcGlSwapBuffers"):
return (void*)hydra_hcGlSwapBuffers;
case hydra::str_hash("hcGlGetProcAddress"):
return (void*)hydra_hcGlGetProcAddress;
case hydra::str_hash("hcSetCallbacks"):
return (void*)hydra_hcSetCallbacks;
default:
return nullptr;
}
}
}
78 changes: 78 additions & 0 deletions src/hydra/core/wrapper.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <hydra/common/log.hxx>
#include <hydra/core.h>
#include <hydra/core/wrapper.hxx>

extern "C" void* hydra_GetAddress(const char* name);

namespace hydra::core
{

Wrapper::Wrapper(const std::filesystem::path& path) : handle(nullptr)
{
if (!std::filesystem::exists(path) || !std::filesystem::is_regular_file(path))
{
hydra::panic("Core not found: {}", path.string());
return;
}

if (path.extension() != hydra::dynlib::extension())
{
hydra::panic("Not a dynamic library: {}", path.string());
return;
}

handle = hydra::dynlib::open(path);
if (!handle)
{
hydra::panic("Failed to load core: {}\nError: {}", path.string(),
hydra::dynlib::error());
return;
}

hcGetCoreInfo = (hcGetCoreInfoPtr)hydra::dynlib::symbol(handle, "hcGetCoreInfo");
hcCreate = (hcCreatePtr)hydra::dynlib::symbol(handle, "hcCreate");
hcDestroy = (hcDestroyPtr)hydra::dynlib::symbol(handle, "hcDestroy");
hcReset = (hcResetPtr)hydra::dynlib::symbol(handle, "hcReset");
hcSetRunState = (hcSetRunStatePtr)hydra::dynlib::symbol(handle, "hcSetRunState");
hcLoadContent = (hcLoadContentPtr)hydra::dynlib::symbol(handle, "hcLoadContent");
hcGetError = (hcGetErrorPtr)hydra::dynlib::symbol(handle, "hcGetError");

if (!hcGetCoreInfo || !hcCreate || !hcDestroy || !hcReset || !hcSetRunState ||
!hcLoadContent || !hcGetError)
{
hydra::log("Failed to load core functions: {}\nError: {}", path.string(),
hydra::dynlib::error());
hydra::dynlib::close(handle);
handle = nullptr;
}

void* (*hcInternalLoadFunctions)(void* (*loadFunctionPtr)(const char*)) =
(decltype(hcInternalLoadFunctions))hydra::dynlib::symbol(handle,
"hcInternalLoadFunctions");
if (hcInternalLoadFunctions)
{
hcInternalLoadFunctions(hydra_GetAddress);
}
else
{
hydra::log("Failed to load internal functions: {}\nError: {}", path.string(),
hydra::dynlib::error());
hydra::dynlib::close(handle);
handle = nullptr;
}
}

Wrapper::~Wrapper()
{
if (handle)
{
hydra::dynlib::close(handle);
}
}

bool Wrapper::okay() const
{
return handle != nullptr;
}

} // namespace hydra::core
Loading

0 comments on commit 813bee6

Please sign in to comment.