Skip to content

Commit

Permalink
winMain.h
Browse files Browse the repository at this point in the history
  • Loading branch information
malytomas committed Oct 23, 2024
1 parent cea08ec commit 109c112
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 6 deletions.
5 changes: 0 additions & 5 deletions sources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@ file(GLOB_RECURSE cage-engine-sources "libengine/*" "include/cage-engine/*")
add_library(cage-engine SHARED ${cage-engine-sources})
target_link_libraries(cage-engine PRIVATE cubeb glfw openxr_loader)
target_link_libraries(cage-engine PUBLIC cage-core glad)
if(WIN32)
set_source_files_properties(libengine/virtualReality/platforms.cpp PROPERTIES COMPILE_DEFINITIONS "XR_USE_PLATFORM_WIN32")
else()
# todo
endif()
file(GLOB_RECURSE controller-bindings RELATIVE "${CMAKE_CURRENT_LIST_DIR}" "controller-bindings/*")
set(index 0)
foreach(cb IN ITEMS ${controller-bindings})
Expand Down
32 changes: 32 additions & 0 deletions sources/include/cage-engine/winMain.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef guard_winMain_h_jh4ser564uj
#define guard_winMain_h_jh4ser564uj

#include <cage-engine/core.h>

#ifdef _WIN32

#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>

namespace cage
{
namespace privat
{
CAGE_ENGINE_API std::pair<int, const char **> winMainParams();
}
}

int main(int argc, const char *args[]);

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
const auto cmd = ::cage::privat::winMainParams();
return ::main(cmd.first, cmd.second);
}
#endif
#endif
3 changes: 2 additions & 1 deletion sources/libengine/virtualReality/platforms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#endif
#include <Windows.h>
#pragma comment(lib, "opengl32") // wglGetCurrentDC
#define XR_USE_PLATFORM_WIN32
#endif // CAGE_SYSTEM_WINDOWS

#define XR_USE_GRAPHICS_API_OPENGL
Expand Down Expand Up @@ -51,7 +52,7 @@ namespace cage

#else

CAGE_THROW_CRITICAL(Exception, "openxr currently works on windows only");
CAGE_THROW_CRITICAL(Exception, "cage openxr currently works on windows only");

char binding[100] = {}; // dummy structure to allow compiling
//XrGraphicsBindingOpenGLXlibKHR binding;
Expand Down
56 changes: 56 additions & 0 deletions sources/libengine/winMain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#ifdef _WIN32

#include <cage-engine/core.h>

#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>

namespace cage
{
namespace privat
{
CAGE_API_EXPORT std::pair<int, const char **> winMainParams()
{
using namespace cage;

CAGE_LOG(SeverityEnum::Info, "cage-engine", "winMain parsing parameters");

static int consoleDummy = []()
{
AllocConsole(); // allocate new console for sub-programs
ShowWindow(GetConsoleWindow(), SW_HIDE);
return 0;
}();
(void)consoleDummy;

LPWSTR cmdLine = GetCommandLineW();
int argc;
LPWSTR *argvW = CommandLineToArgvW(cmdLine, &argc);

if (argvW == nullptr)
{
static_assert(sizeof(DWORD) == sizeof(uint32));
CAGE_LOG_THROW(Stringizer() + "error code: " + (uint32)GetLastError());
CAGE_THROW_CRITICAL(Exception, "failed to convert command line arguments to an array (CommandLineToArgvW)");
}

// convert wide char to multibyte
char **argv = new char *[argc];
for (int i = 0; i < argc; i++)
{
size_t len = wcslen(argvW[i]) + 1;
argv[i] = new char[len];
wcstombs(argv[i], argvW[i], len);
}

return { argc, (const char **)argv };
}
}
}

#endif

0 comments on commit 109c112

Please sign in to comment.