diff --git a/.vscode/settings.json b/.vscode/settings.json index 0c918c87..20f36e4c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,5 +2,9 @@ "mesonbuild.configureOnOpen": true, "C_Cpp.codeAnalysis.clangTidy.checks.disabled": [ "clang-diagnostic-pragma-once-outside-header" - ] + ], + "files.associations": { + "phtml": "php", + "new": "cpp" + } } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index bd655fd4..8b0c5fc9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,9 +43,17 @@ include_directories(./prod_include) find_package(PkgConfig REQUIRED) -#libdsetup.so: +#dependency +include_directories(${GTK4_INCLUDE_DIRS}) +link_directories(${GTK4_LIBRARY_DIRS}) -#resolve wildcards and add "dsetup.cpp" as the first of the list +#libopendx.so: +set(OPENDX_CPP libs/opendx/opendx.cpp) +#file(GLOB_RECURSE OPENDX_CPP libs/opendx/fun/*.cpp) +add_library(opendx SHARED ${OPENDX_CPP}) +target_link_libraries(opendx ${GTK4_LIBRARIES}) + +#libdsetup.so: set(DSETUP_CPP libs/dsetup/dsetup.cpp) file(GLOB_RECURSE DSETUP_CPP libs/dsetup/fun/*.cpp) @@ -57,10 +65,6 @@ add_library(d3d9 SHARED libs/d3d9/d3d9.cpp) #dxdiag: pkg_check_modules(GTK4 REQUIRED gtk4) - -include_directories(${GTK4_INCLUDE_DIRS}) -link_directories(${GTK4_LIBRARY_DIRS}) - add_executable(dxdiag tools/dxdiag/main.cpp) target_link_libraries(dxdiag ${GTK4_LIBRARIES}) target_link_libraries(dxdiag dsetup) diff --git a/libs/dsetup/dsetup.hpp b/libs/dsetup/dsetup.hpp index cd2f8708..03275dcf 100644 --- a/libs/dsetup/dsetup.hpp +++ b/libs/dsetup/dsetup.hpp @@ -1 +1,2 @@ +#pragma onnce #include "fun/DirectXSetupGetVersion.hpp" \ No newline at end of file diff --git a/libs/dsetup/fun/DirectXSetupGetVersion.hpp b/libs/dsetup/fun/DirectXSetupGetVersion.hpp index 9b66ea8f..dcb7d06b 100644 --- a/libs/dsetup/fun/DirectXSetupGetVersion.hpp +++ b/libs/dsetup/fun/DirectXSetupGetVersion.hpp @@ -1,3 +1,4 @@ +#pragma onnce #include /** diff --git a/libs/opendx/opendx.cpp b/libs/opendx/opendx.cpp index 8b137891..6179c6f7 100644 --- a/libs/opendx/opendx.cpp +++ b/libs/opendx/opendx.cpp @@ -1 +1,86 @@ +#include +#include +#include +#include +/** + * @brief Create a Window + * + * @return HWND + */ +HWND CreateWindowExA( + DWORD extStyle, + LPCSTR className, //optional + LPCSTR title, //optional + DWORD style, + int x, //ignored. It's the user responsability to set the window position. (GTK4) + int y, //ignored. It's the user responsability to set the window position. (GTK4) + int width, + int height, + HWND parent, //optional + HMENU menu, //optional + HINSTANCE instance, //optional (Windows ignores it) + LPVOID param //optional +) { + GtkWidget* window = gtk_window_new(); + + if (title != nullptr) { + gtk_window_set_title(GTK_WINDOW(window), title); + } + + gtk_window_set_default_size(GTK_WINDOW(window), width, height); + + //style: + // TODO: Complete the list + if (!(style & WS_CAPTION)) { + gtk_window_set_title(GTK_WINDOW(window), ""); + } + if (!(style & WS_SYSMENU)) { + gtk_window_set_deletable(GTK_WINDOW(window), false); + } + if (style & WS_VISIBLE) { + gtk_widget_show(GTK_WIDGET(window)); + } + + return window; +} + +BOOL ShowWindow(HWND window, int nCmdShow) { + BOOL r = gtk_widget_get_visible(GTK_WIDGET(window)); + gtk_widget_show(GTK_WIDGET(window)); + return r; +} + +/* + * OpenDX utility class + */ +OpenDX::OpenDX(int argc, char* argv[], int (*WinMain)(HINSTANCE, HINSTANCE, LPSTR, int)) { + //Converts argc and argv to WinMain params + char* cmdline = (char*) malloc(sizeof(char)); + cmdline[0] = '\0'; + + for (int i = 0; i < argc; i++) { + cmdline = (char*) realloc(cmdline, strlen(cmdline) + strlen(argv[i]) + 2); + strcat(cmdline, argv[i]); + if (i < argc - 1) { + strcat(cmdline, " "); + } + } + + gtk_init(); + if (WinMain != nullptr) { + winMain_r = (*WinMain)(nullptr,nullptr,cmdline,0); + } +} + +OpenDX::OpenDX(int (*WinMain)(HINSTANCE, HINSTANCE, LPSTR, int)) { + gtk_init(); + + if (WinMain != nullptr) { + winMain_r = (*WinMain)(nullptr,nullptr,"",0); + } +} + +int OpenDX::getReturnCode() { + return winMain_r; +} \ No newline at end of file diff --git a/libs/opendx/opendx.hpp b/libs/opendx/opendx.hpp index 8b137891..3f59c932 100644 --- a/libs/opendx/opendx.hpp +++ b/libs/opendx/opendx.hpp @@ -1 +1,2 @@ +#pragma once diff --git a/prod_include/opendx.h b/prod_include/opendx.h new file mode 100644 index 00000000..ae4bff09 --- /dev/null +++ b/prod_include/opendx.h @@ -0,0 +1,15 @@ +#pragma once +#include + +/** + * @brief OpenDX utility class + */ +class OpenDX { + public: + OpenDX(int argc, char* argv[],int (*WinMain)(HINSTANCE, HINSTANCE, LPSTR, int) = nullptr); + OpenDX(int (*WinMain)(HINSTANCE, HINSTANCE, LPSTR, int) = nullptr); + int getReturnCode(); + + private: + int winMain_r = 0; +}; \ No newline at end of file diff --git a/prod_include/windows.h b/prod_include/windows.h index cdb98861..0f06a542 100644 --- a/prod_include/windows.h +++ b/prod_include/windows.h @@ -21,10 +21,13 @@ #define TCHAR char #define UINT unsigned int #define ULONG unsigned long -#define UlONG_PTR unsigned long +#define ULONG_PTR unsigned long #define LONG long #define BOOL bool #define BYTE unsigned char +#define LPCTSTR const char* +#define LPCSTR const char* +#define LPCWSTR const char* #include #include diff --git a/prod_include/winuser.h b/prod_include/winuser.h new file mode 100644 index 00000000..2fb0adbf --- /dev/null +++ b/prod_include/winuser.h @@ -0,0 +1,101 @@ +/** + * From "libopendx.so" + */ +#pragma once +#include + +/** + * Extended Window Styles + * ref: https://learn.microsoft.com/en-us/windows/win32/winmsg/extended-window-styles + */ +#define WS_EX_ACCEPTFILES 0x00000010L +#define WS_EX_APPWINDOW 0x00040000L +#define WS_EX_CLIENTEDGE 0x00000200L +#define WS_EX_COMPOSITED 0x02000000L +#define WS_EX_CONTEXTHELP 0x00000400L +#define WS_EX_CONTROLPARENT 0x00010000L +#define WS_EX_DLGMODALFRAME 0x00000001L +#define WS_EX_LAYERED 0x00080000 +#define WS_EX_LAYOUTRTL 0x00400000L +#define WS_EX_LEFT 0x00000000L +#define WS_EX_LEFTSCROLLBAR 0x00004000L +#define WS_EX_LTRREADING 0x00000000L +#define WS_EX_MDICHILD 0x00000040L +#define WS_EX_NOACTIVATE 0x08000000L +#define WS_EX_NOINHERITLAYOUT 0x00100000L +#define WS_EX_NOPARENTNOTIFY 0x00000004L +#define WS_EX_NOREDIRECTIONBITMAP 0x00200000L +#define WS_EX_RIGHT 0x00001000L +#define WS_EX_RIGHTSCROLLBAR 0x00000000L +#define WS_EX_RTLREADING 0x00002000L +#define WS_EX_STATICEDGE 0x00020000L +#define WS_EX_TOOLWINDOW 0x00000080L +#define WS_EX_TOPMOST 0x00000008L +#define WS_EX_TRANSPARENT 0x00000020L +#define WS_EX_WINDOWEDGE 0x00000100L +#define WS_EX_OVERLAPPEDWINDOW (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE) +#define WS_EX_PALETTEWINDOW (WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST) + +/* + * Window Styles + * ref: https://learn.microsoft.com/en-us/windows/win32/winmsg/window-styles + */ +#define WS_BORDER 0x00800000L +#define WS_CAPTION 0x00C00000L +#define WS_CHILD 0x40000000L +#define WS_CHILDWINDOW 0x40000000L +#define WS_CLIPCHILDREN 0x02000000L +#define WS_CLIPSIBLINGS 0x04000000L +#define WS_DISABLED 0x08000000L +#define WS_DLGFRAME 0x00400000L +#define WS_GROUP 0x00020000L +#define WS_HSCROLL 0x00100000L +#define WS_ICONIC 0x20000000L +#define WS_MAXIMIZE 0x01000000L +#define WS_MAXIMIZEBOX 0x00010000L +#define WS_MINIMIZE 0x20000000L +#define WS_MINIMIZEBOX 0x00020000L +#define WS_OVERLAPPED 0x00000000L +#define WS_POPUP 0x80000000L +#define WS_SIZEBOX 0x00040000L +#define WS_SYSMENU 0x00080000L +#define WS_TABSTOP 0x00010000L +#define WS_THICKFRAME 0x00040000L +#define WS_TILED 0x00000000L +#define WS_VISIBLE 0x10000000L +#define WS_VSCROLL 0x00200000L +#define WS_OVERLAPPEDWINDOW (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX) +#define WS_POPUPWINDOW (WS_POPUP | WS_BORDER | WS_SYSMENU) +#define WS_TILEDWINDOW (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX) + +HWND CreateWindowExA( + DWORD extStyle, + LPCSTR className, //optional + LPCSTR title, //optional + DWORD style, + int x, //ignored. It's the user responsability to set the window position. (GTK4) + int y, //ignored. It's the user responsability to set the window position. (GTK4) + int width, + int height, + HWND parent, //optional + HMENU menu, //optional + HINSTANCE instance, //optional (Windows ignores it) + LPVOID param //optional +); + +BOOL ShowWindow(HWND window, int nCmdShow); + +#define CreateWindowExW CreateWindowExA + +// https://www.codeproject.com/Answers/136442/Differences-Between-CreateWindow-and-CreateWindowE#answer3 +#define CreateWindowA(lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam)\ +CreateWindowExA(0L, lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam) +#define CreateWindowW(lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam)\ +CreateWindowExW(0L, lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam) + +//TODO: check if it's needed in a linux environment +#if UNICODE + #define CreateWindow CreateWindowW +#else + #define CreateWindow CreateWindowA +#endif \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 91f14dfa..ffe8eff8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -8,5 +8,5 @@ include_directories(${GTK4_INCLUDE_DIRS}) link_directories(${GTK4_LIBRARY_DIRS}) add_executable(sample basic_window.cpp) -target_link_libraries(sample ${GTK4_LIBRARIES}) +target_link_libraries(sample opendx) target_link_libraries(sample d3d9) diff --git a/tests/basic_window.cpp b/tests/basic_window.cpp index e6c84a8a..f747a5a3 100644 --- a/tests/basic_window.cpp +++ b/tests/basic_window.cpp @@ -4,6 +4,8 @@ * * Code that doesn't work is also commented. */ +#include +#include #include #include #include @@ -12,12 +14,21 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { // Create the window - //HWND hWnd = CreateWindow("DX9 Window", "DX9 Window", 0, 0, 0, 640, 480, NULL, NULL, hInstance, NULL); + HWND hWnd = CreateWindow( + "DX9 Window", "DX9 Window", + WS_OVERLAPPEDWINDOW | WS_VISIBLE, + 0, 0, 640, 480, NULL, NULL, hInstance, NULL + ); // Create the Direct3D device LPDIRECT3D9 pD3D = Direct3DCreate9(D3D_SDK_VERSION); LPDIRECT3DDEVICE9 pDevice = NULL; D3DPRESENT_PARAMETERS pp; + + + //wait 5 seconds (Linux): + sleep(5); + /*ZeroMemory(&pp, sizeof(pp)); pp.Windowed = TRUE; pp.SwapEffect = D3DSWAPEFFECT_DISCARD; @@ -49,18 +60,13 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine return 0; } -//Linux's main +//Linux's main or MinGW's main: int main(int argc, char* argv[]) { - char* cmdline = (char*) malloc(sizeof(char)); - cmdline[0] = '\0'; + //OpenDX transforms argc and argv to WinMain params + //then calls WinMain. You can also initialize OpenDX + //without params and insert your code right here if + //you are using MinGW. + OpenDX odx(argc, argv,WinMain); //or OpenDX odx(); - for (int i = 0; i < argc; i++) { - cmdline = (char*) realloc(cmdline, strlen(cmdline) + strlen(argv[i]) + 2); - strcat(cmdline, argv[i]); - if (i < argc - 1) { - strcat(cmdline, " "); - } - } - - return WinMain(nullptr, nullptr, cmdline, 0); + return odx.getReturnCode(); // 0 if WinMain is not passed. }