From ce71e7dc1aff971aef0ca05007036b0d055d05cd Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Mon, 28 Apr 2014 16:35:36 +0400 Subject: [PATCH] support SDL2 under Windows --- dlanguilib.visualdproj | 15 +- examples/example1/example1.visualdproj | 4 +- examples/helloworld/helloworld.visualdproj | 4 +- src/dlangui/platforms/common/platform.d | 14 +- src/dlangui/platforms/sdl/sdlapp.d | 182 ++++++++++++++----- src/dlangui/platforms/windows/win32drawbuf.d | 3 +- 6 files changed, 163 insertions(+), 59 deletions(-) diff --git a/dlanguilib.visualdproj b/dlanguilib.visualdproj index 525c00be..466a77aa 100644 --- a/dlanguilib.visualdproj +++ b/dlanguilib.visualdproj @@ -66,7 +66,7 @@ 0 0 - Unicode USE_OPENGL + Unicode USE_OPENGL USE_SDL 0 0 1 @@ -225,6 +225,15 @@ + + + + + + + + + @@ -320,12 +329,14 @@ + + + - diff --git a/examples/example1/example1.visualdproj b/examples/example1/example1.visualdproj index b79d8820..03c3ef63 100644 --- a/examples/example1/example1.visualdproj +++ b/examples/example1/example1.visualdproj @@ -47,7 +47,7 @@ 0 0 $(DMDInstallDir)windows\bin\dmd.exe - $(SolutionDir)/src $(SolutionDir)/3rdparty $(SolutionDir)/3rdparty/libpng/source $(SolutionDir)/../DerelictGL3/source $(SolutionDir)/../DerelictUtil/source $(SolutionDir)/../gl3n + $(SolutionDir)/src $(SolutionDir)/3rdparty $(SolutionDir)/3rdparty/libpng/source $(SolutionDir)/../DerelictGL3/source $(SolutionDir)/../DerelictUtil/source $(SolutionDir)/../DerelictFT/source $(SolutionDir)/../DerelictSDL2/source $(ConfigurationName) $(OutDir) @@ -66,7 +66,7 @@ 0 0 - Unicode USE_OPENGL + Unicode USE_OPENGL USE_SDL 0 3 0 diff --git a/examples/helloworld/helloworld.visualdproj b/examples/helloworld/helloworld.visualdproj index e8f178cf..51a1a43b 100644 --- a/examples/helloworld/helloworld.visualdproj +++ b/examples/helloworld/helloworld.visualdproj @@ -47,7 +47,7 @@ 0 0 $(DMDInstallDir)windows\bin\dmd.exe - $(SolutionDir)/src $(SolutionDir)/3rdparty $(SolutionDir)/3rdparty/libpng/source $(SolutionDir)/../DerelictGL3/source $(SolutionDir)/../DerelictUtil/source $(SolutionDir)/../gl3n + $(SolutionDir)/src $(SolutionDir)/3rdparty $(SolutionDir)/3rdparty/libpng/source $(SolutionDir)/../DerelictGL3/source $(SolutionDir)/../DerelictUtil/source $(SolutionDir)/../DerelictFT/source $(SolutionDir)/../DerelictSDL2/source $(ConfigurationName) $(OutDir) @@ -66,7 +66,7 @@ 0 0 - Unicode USE_OPENGL + Unicode USE_OPENGL USE_SDL 0 3 0 diff --git a/src/dlangui/platforms/common/platform.d b/src/dlangui/platforms/common/platform.d index 91f113de..c6bb4fef 100644 --- a/src/dlangui/platforms/common/platform.d +++ b/src/dlangui/platforms/common/platform.d @@ -525,13 +525,15 @@ mixin template APP_ENTRY_POINT() { /// workaround for link issue when WinMain is located in library version(Windows) { private import win32.windows; + private import dlangui.platforms.sdl.sdlapp; private import dlangui.platforms.windows.winapp; + extern (Windows) - int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, - LPSTR lpCmdLine, int nCmdShow) - { - return DLANGUIWinMain(hInstance, hPrevInstance, - lpCmdLine, nCmdShow); - } + int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, + LPSTR lpCmdLine, int nCmdShow) + { + return DLANGUIWinMain(hInstance, hPrevInstance, + lpCmdLine, nCmdShow); + } } } diff --git a/src/dlangui/platforms/sdl/sdlapp.d b/src/dlangui/platforms/sdl/sdlapp.d index b821fcba..3bed9bb0 100644 --- a/src/dlangui/platforms/sdl/sdlapp.d +++ b/src/dlangui/platforms/sdl/sdlapp.d @@ -1,8 +1,14 @@ module src.dlangui.platforms.sdl.sdlapp; version(USE_SDL) { + import core.runtime; import std.string; import std.conv; + import std.string; + import std.utf; + import std.stdio; + import std.algorithm; + import std.file; import dlangui.core.logger; import dlangui.core.events; @@ -243,59 +249,145 @@ version(USE_SDL) { // entry point extern(C) int UIAppMain(string[] args); + + version (Windows) { + import win32.windows; + import dlangui.platforms.windows.win32fonts; + pragma(lib, "gdi32.lib"); + pragma(lib, "user32.lib"); + extern(Windows) + int DLANGUIWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, + LPSTR lpCmdLine, int nCmdShow) { + int result; + + try + { + Runtime.initialize(); + result = myWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow); + Runtime.terminate(); + } + catch (Throwable e) // catch any uncaught exceptions + { + MessageBox(null, toUTF16z(e.toString()), "Error", + MB_OK | MB_ICONEXCLAMATION); + result = 0; // failed + } + + return result; + } + + string[] splitCmdLine(string line) { + string[] res; + int start = 0; + bool insideQuotes = false; + for (int i = 0; i <= line.length; i++) { + char ch = i < line.length ? line[i] : 0; + if (ch == '\"') { + if (insideQuotes) { + if (i > start) + res ~= line[start .. i]; + start = i + 1; + insideQuotes = false; + } else { + insideQuotes = true; + start = i + 1; + } + } else if (!insideQuotes && (ch == ' ' || ch == '\t' || ch == 0)) { + if (i > start) { + res ~= line[start .. i]; + } + start = i + 1; + } + } + return res; + } + + int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow) + { + setFileLogger(std.stdio.File("ui.log", "w")); + setLogLevel(LogLevel.Trace); + Log.d("myWinMain()"); + string basePath = exePath(); + Log.i("Current executable: ", exePath()); + string cmdline = fromStringz(lpCmdLine); + Log.i("Command line: ", cmdline); + string[] args = splitCmdLine(cmdline); + Log.i("Command line params: ", args); + + try { + // Load the SDL 2 library. + DerelictSDL2.load(); + } catch (Exception e) { + Log.e("Cannot load SDL2 library", e); + return 1; + } + + //_cmdShow = iCmdShow; + //_hInstance = hInstance; + + FontManager.instance = new Win32FontManager(); + + return sdlmain(args); + } + } else { + + int main(string[] args) + { - int main(string[] args) - { - - setStderrLogger(); - setLogLevel(LogLevel.Trace); + setStderrLogger(); + setLogLevel(LogLevel.Trace); - try { - // Load the SDL 2 library. - DerelictSDL2.load(); - } catch (Exception e) { - Log.e("Cannot load SDL2 library", e); - return 1; - } + try { + // Load the SDL 2 library. + DerelictSDL2.load(); + } catch (Exception e) { + Log.e("Cannot load SDL2 library", e); + return 1; + } - SDL_DisplayMode displayMode; - if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_EVENTS) != 0) { - Log.e("Cannot init SDL2"); - return 2; - } - scope(exit)SDL_Quit(); - int request = SDL_GetDesktopDisplayMode(0,&displayMode); + SDL_DisplayMode displayMode; + if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_EVENTS) != 0) { + Log.e("Cannot init SDL2"); + return 2; + } + scope(exit)SDL_Quit(); + int request = SDL_GetDesktopDisplayMode(0,&displayMode); - FreeTypeFontManager ft = new FreeTypeFontManager(); - // TODO: use FontConfig - ft.registerFont("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", FontFamily.SansSerif, "DejaVu", false, FontWeight.Normal); - FontManager.instance = ft; + FreeTypeFontManager ft = new FreeTypeFontManager(); + // TODO: use FontConfig + ft.registerFont("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", FontFamily.SansSerif, "DejaVu", false, FontWeight.Normal); + FontManager.instance = ft; - currentTheme = createDefaultTheme(); - - SDLPlatform sdl = new SDLPlatform(); - if (!sdl.connect()) { - return 1; - } - Platform.setInstance(sdl); + return sdlmain(args); + } + } - int res = 0; - - res = UIAppMain(args); + int sdlmain(string[] args) { + currentTheme = createDefaultTheme(); - Platform.setInstance(null); - Log.d("Destroying SDL platform"); - destroy(sdl); - - currentTheme = null; - drawableCache = null; - imageCache = null; - FontManager.instance = null; - - Log.d("Exiting main"); + SDLPlatform sdl = new SDLPlatform(); + if (!sdl.connect()) { + return 1; + } + Platform.setInstance(sdl); - return res; - } + int res = 0; + + res = UIAppMain(args); + + Platform.setInstance(null); + Log.d("Destroying SDL platform"); + destroy(sdl); + + currentTheme = null; + drawableCache = null; + imageCache = null; + FontManager.instance = null; + + Log.d("Exiting main"); + + return res; + } } diff --git a/src/dlangui/platforms/windows/win32drawbuf.d b/src/dlangui/platforms/windows/win32drawbuf.d index b91e56b4..512c908f 100644 --- a/src/dlangui/platforms/windows/win32drawbuf.d +++ b/src/dlangui/platforms/windows/win32drawbuf.d @@ -1,7 +1,6 @@ module dlangui.platforms.windows.win32drawbuf; -version (USE_SDL) { } -else version (Windows) { +version (Windows) { import win32.windows; import dlangui.core.logger;