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

Commit

Permalink
Stuff?
Browse files Browse the repository at this point in the history
  • Loading branch information
OFFTKP committed Mar 22, 2024
1 parent 813bee6 commit 6485041
Show file tree
Hide file tree
Showing 15 changed files with 436 additions and 47 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ set(HYDRA_IMGUI_FILES
src/hydra/sdl3/opengl.cxx
src/hydra/sdl3/vulkan.cxx
src/hydra/sdl3/common.cxx
src/hydra/imgui/imgui.cxx
vendored/imgui/backends/imgui_impl_sdl3.cpp
vendored/imgui/backends/imgui_impl_vulkan.cpp
vendored/imgui/imgui.cpp
Expand All @@ -192,8 +193,10 @@ set(HYDRA_COMMON_FILES
src/main.cxx
src/hydra/core/wrapper.cxx
src/hydra/core/imports.cxx
src/hydra/core/thread.cxx
src/hydra/common/version.cxx
src/hydra/common/settings.cxx
src/hydra/common/system.cxx
vendored/argparse/argparse.c
vendored/miniaudio.c
vendored/stb_image.c
Expand Down
14 changes: 0 additions & 14 deletions include/compatibility.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,6 @@ namespace hydra
return bytes;
}

// Function for hashing a string in compile time in order to be used in a switch statement
// https://stackoverflow.com/a/46711735
// If there's a collision between two strings, we will know
// at compile time since the cases can't use the same number twice
constexpr uint32_t str_hash(std::string_view data) noexcept
{
uint32_t hash = 5381;
const size_t size = data.size();
for (size_t i = 0; i < size; i++)
hash = ((hash << 5) + hash) + (unsigned char)data[i];

return hash;
}

inline std::string fread(const std::filesystem::path& path)
{
FILE* file = std::fopen(path.string().c_str(), "rb");
Expand Down
1 change: 1 addition & 0 deletions include/hydra/common/settings.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ namespace hydra::settings
Saveable<int, "General", "Height"> windowHeight{};
Saveable<std::vector<std::string>, "General", "LastContentPaths"> lastContentPaths{};
Saveable<std::vector<std::string>, "General", "GameDirectories"> gameDirectories{};
Saveable<bool, "Core", "StartPaused"> startPaused{};

static Config& get()
{
Expand Down
9 changes: 9 additions & 0 deletions include/hydra/common/system.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <hydra/core.h>

namespace hydra::common
{
HcArchitecture architecture();
HcOperatingSystem operatingSystem();
} // namespace hydra::common
23 changes: 23 additions & 0 deletions include/hydra/common/utility.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <cstdint>
#include <string_view>

namespace hydra::common
{

// Function for hashing a string in compile time in order to be used in a switch statement
// https://stackoverflow.com/a/46711735
// If there's a collision between two strings, we will know
// at compile time since the cases can't use the same number twice
constexpr uint32_t str_hash(std::string_view data) noexcept
{
uint32_t hash = 5381;
const size_t size = data.size();
for (size_t i = 0; i < size; i++)
hash = ((hash << 5) + hash) + (unsigned char)data[i];

return hash;
}

} // namespace hydra::common
13 changes: 13 additions & 0 deletions include/hydra/core/thread.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <hydra/core.h>
#include <hydra/core/wrapper.hxx>
#include <memory>

namespace hydra::core::thread
{
void start(std::shared_ptr<Wrapper> wrapper);
void stop();
void reset(HcResetType type);
bool running();
} // namespace hydra::core::thread
7 changes: 7 additions & 0 deletions include/hydra/imgui/imgui.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

namespace hydra::imgui
{
void init();
void shutdown();
} // namespace hydra::imgui
46 changes: 46 additions & 0 deletions src/hydra/common/system.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <hydra/common/system.hxx>
#include <hydra/core.h>

namespace hydra::common
{

HcArchitecture architecture()
{
#if defined(__x86_64__) || defined(__x86_64) || defined(__amd64) || defined(_M_X64)
return HC_ARCHITECTURE_X86_64;
#elif defined(__i386__) || defined(__i386) || defined(_M_IX86)
return HC_ARCHITECTURE_X86;
#elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64)
return HC_ARCHITECTURE_AARCH64;
#elif defined(__arm__)
return HC_ARCHITECTURE_AARCH32;
#elif defined(__EMSCRIPTEN__)
return HC_ARCHITECTURE_WASM;
#else
#pragma message("Unknown architecture")
return HC_ARCHITECTURE_UNKNOWN;
#endif
}

HcOperatingSystem operatingSystem()
{
#if defined(HYDRA_WINDOWS)
return HC_OPERATING_SYSTEM_WINDOWS;
#elif defined(HYDRA_LINUX)
return HC_OPERATING_SYSTEM_LINUX;
#elif defined(HYDRA_MACOS)
return HC_OPERATING_SYSTEM_MACOS;
#elif defined(HYDRA_FREEBSD)
return HC_OPERATING_SYSTEM_FREEBSD;
#elif defined(HYDRA_WEB)
return HC_OPERATING_SYSTEM_WEB;
#elif defined(HYDRA_IOS)
return HC_OPERATING_SYSTEM_IOS;
#elif defined(HYDRA_ANDROID)
return HC_OPERATING_SYSTEM_ANDROID;
#else
return HC_OPERATING_SYSTEM_UNKNOWN;
#endif
}

} // namespace hydra::common
115 changes: 103 additions & 12 deletions src/hydra/core/imports.cxx
Original file line number Diff line number Diff line change
@@ -1,14 +1,104 @@
// Define the hydra core API imports

#include "compatibility.hxx"
#include "SDL_video.h"
#include <cstdio>
#include <cstring>
#include <hydra/common/log.hxx>
#include <hydra/common/system.hxx>
#include <hydra/common/utility.hxx>
#include <hydra/core.h>
#include <thread>

#include <glad/glad.h>
#include <SDL3/SDL.h>

#include <vulkan/vulkan.h>

static HcHostInfo* globalHostInfo = nullptr;

extern "C" {

void hydra_hcGetHostInfo(HcHostInfo* hostInfo)
{
printf("STUB: hcGetHostInfo\n");
if (!globalHostInfo)
{
globalHostInfo = new HcHostInfo();
globalHostInfo->architecture = hydra::common::architecture();
globalHostInfo->operatingSystem = hydra::common::operatingSystem();

// Create a hidden OpenGL window to get the OpenGL version
std::thread context([]() {
SDL_Window* window =
SDL_CreateWindow("hidden", 0, 0, SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN);
SDL_GLContext context = SDL_GL_CreateContext(window);
if (gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress) == 0)
{
hydra::log("Failed to load OpenGL functions when trying to get OpenGL version");
globalHostInfo->openGlVersion = HC_OPENGL_NOT_SUPPORTED;
}
else
{
int major, minor;
glGetIntegerv(GL_MAJOR_VERSION, &major);
glGetIntegerv(GL_MINOR_VERSION, &minor);
globalHostInfo->openGlVersion = (HcOpenGlVersion)((major << 16) | minor);
printf("OpenGL version: %d.%d\n", major, minor);
}

SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);

SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
window = SDL_CreateWindow("hidden", 0, 0, SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN);
context = SDL_GL_CreateContext(window);
if (gladLoadGLES2Loader((GLADloadproc)SDL_GL_GetProcAddress) == 0)
{
hydra::log(
"Failed to load OpenGL ES functions when trying to get OpenGL ES version");
globalHostInfo->openGlEsVersion = HC_OPENGL_ES_NOT_SUPPORTED;
}
else
{
int major, minor;
glGetIntegerv(GL_MAJOR_VERSION, &major);
glGetIntegerv(GL_MINOR_VERSION, &minor);
globalHostInfo->openGlEsVersion = (HcOpenGlEsVersion)((major << 16) | minor);
printf("OpenGL ES version: %d.%d\n", major, minor);
}

SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
});
context.join();

uint32_t vkVersion;
vkEnumerateInstanceVersion(&vkVersion);
int major = VK_VERSION_MAJOR(vkVersion);
int minor = VK_VERSION_MINOR(vkVersion);
globalHostInfo->vulkanVersion = (HcVulkanVersion)((major << 16) | minor);
printf("Vulkan version: %d.%d\n", major, minor);

#ifdef HYDRA_WINDOWS
printf("Direct3d stuff\n");
globalHostInfo->direct3DVersion = HC_DIRECT3D_NOT_SUPPORTED;
// DWORD dwVersion;
// DWORD dwRevision;
// if (DirectXSetupGetVersion(&dwVersion, &dwRevision))
// {
// printf("DirectX version is %d.%d.%d.%d\n",
// HIWORD(dwVersion), LOWORD(dwVersion),
// HIWORD(dwRevision), LOWORD(dwRevision));
// }
#else
// TODO: can we use dxvk? Is it too ambitious?
globalHostInfo->direct3DVersion = HC_DIRECT3D_NOT_SUPPORTED;
#endif
globalHostInfo->metalVersion = HC_METAL_NOT_SUPPORTED;
}

std::memcpy(hostInfo, globalHostInfo, sizeof(HcHostInfo));
}

HcResult hydra_hcGetInputsSync(const HcInputRequest* const* requests, int requestCount,
Expand Down Expand Up @@ -62,27 +152,28 @@ HcResult hydra_hcSetCallbacks(const HcCallbacks* callbacks)

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

switch (hash)
{
case hydra::str_hash("hcGetHostInfo"):
case str_hash("hcGetHostInfo"):
return (void*)hydra_hcGetHostInfo;
case hydra::str_hash("hcGetInputsSync"):
case str_hash("hcGetInputsSync"):
return (void*)hydra_hcGetInputsSync;
case hydra::str_hash("hcReconfigureEnvironment"):
case str_hash("hcReconfigureEnvironment"):
return (void*)hydra_hcReconfigureEnvironment;
case hydra::str_hash("hcPushSamples"):
case str_hash("hcPushSamples"):
return (void*)hydra_hcPushSamples;
case hydra::str_hash("hcSwPushVideoFrame"):
case str_hash("hcSwPushVideoFrame"):
return (void*)hydra_hcSwPushVideoFrame;
case hydra::str_hash("hcGlMakeCurrent"):
case str_hash("hcGlMakeCurrent"):
return (void*)hydra_hcGlMakeCurrent;
case hydra::str_hash("hcGlSwapBuffers"):
case str_hash("hcGlSwapBuffers"):
return (void*)hydra_hcGlSwapBuffers;
case hydra::str_hash("hcGlGetProcAddress"):
case str_hash("hcGlGetProcAddress"):
return (void*)hydra_hcGlGetProcAddress;
case hydra::str_hash("hcSetCallbacks"):
case str_hash("hcSetCallbacks"):
return (void*)hydra_hcSetCallbacks;
default:
return nullptr;
Expand Down
Loading

0 comments on commit 6485041

Please sign in to comment.