allegro5 icon indicating copy to clipboard operation
allegro5 copied to clipboard

Errors building with SDL backend on windows/mac

Open connorjclark opened this issue 2 years ago • 6 comments

The SDL backend works for building with emscripten, but I can't get it to build on Windows or Mac.

cmake .. -DALLEGRO_SDL=on -DSDL2_INCLUDE_DIR="/c/Program Files/SDL2/include/"
cmake --build . -t ex_draw --config RelWithDebInfo

output:

C:\Users\cjamc\code\allegro5\src\sdl\sdl_thread.c(49,21): warning C4047: 'function': 'const size_t' differs in levels of indirection from '_AL_THREAD *' [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_time.c(39,24): error C2065: 'ALLEGRO_TIMEOUT_SDL': undeclared identifier [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_time.c(39,37): error C2065: 'timeout_sdl': undeclared identifier [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_time.c(39,54): warning C4047: '=': 'int' differs in levels of indirection from 'void *' [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_thread.c(49,21): warning C4024: 'SDL_CreateThreadWithStackSize': different types for formal and actual parameter 3 [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_time.c(39,54): error C2106: '=': left operand must be l-value [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_thread.c(49,21): warning C4113: 'pfnSDL_CurrentEndThread' differs in parameter lists from 'pfnSDL_CurrentBeginThread' [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_time.c(40,15): error C2065: 'timeout_sdl': undeclared identifier [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_thread.c(49,21): warning C4133: 'function': incompatible types - from 'pfnSDL_CurrentEndThread' to 'pfnSDL_CurrentBeginThread' [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_thread.c(49,21): error C2198: 'SDL_CreateThreadWithStackSize': too few arguments for call [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_time.c(40,17): error C2223: left of '->ms' must point to struct/union [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_thread.c(84,11): error C2039: 'mutex': is not a member of '_AL_MUTEX' [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\include\allegro5/platform/aintwthr.h(25): message : see declaration of '_AL_MUTEX' [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_thread.c(96,15): error C2039: 'mutex': is not a member of '_AL_MUTEX' [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\include\allegro5/platform/aintwthr.h(25): message : see declaration of '_AL_MUTEX' [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_thread.c(97,31): error C2039: 'mutex': is not a member of '_AL_MUTEX' [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\include\allegro5/platform/aintwthr.h(25): message : see declaration of '_AL_MUTEX' [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_thread.c(97,23): error C2198: 'SDL_DestroyMutex': too few arguments for call [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_thread.c(98,14): error C2039: 'mutex': is not a member of '_AL_MUTEX' [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\include\allegro5/platform/aintwthr.h(25): message : see declaration of '_AL_MUTEX' [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_thread.c(108,24): error C2065: 'ALLEGRO_TIMEOUT_SDL': undeclared identifier [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_thread.c(108,37): error C2065: 'timeout_sdl': undeclared identifier [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_thread.c(108,54): warning C4047: '=': 'int' differs in levels of indirection from 'void *' [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_thread.c(108,54): error C2106: '=': left operand must be l-value [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_thread.c(109,38): error C2039: 'cond': is not a member of '_AL_COND' [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\include\allegro5/platform/aintwthr.h(33): message : see declaration of '_AL_COND' [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_thread.c(109,51): error C2039: 'mutex': is not a member of '_AL_MUTEX' [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\include\allegro5/platform/aintwthr.h(25): message : see declaration of '_AL_MUTEX' [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_thread.c(109,69): error C2065: 'timeout_sdl': undeclared identifier [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_thread.c(109,71): error C2223: left of '->ms' must point to struct/union [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_thread.c(109,31): error C2198: 'SDL_CondWaitTimeout': too few arguments for call [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_display.c(412,73): warning C4047: 'function': 'ALLEGRO_DISPLAY *' differs in levels of indirection from 'int' [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]
C:\Users\cjamc\code\allegro5\src\sdl\sdl_display.c(412,66): warning C4024: '_al_create_default_shader': different types for formal and actual parameter 1 [C:\Users\cjamc\code\allegro5\build\allegro.vcxproj]

For reference, I am using SDL 2.0.22. I tried with master allegro and with most recent published version 5.2.7.0, same issue.

Related: #1002

connorjclark avatar May 15 '22 05:05 connorjclark

There's multiple places where other platform's implementations take precedence over SDL. If I move the SDL implementation above them, the examples build. Here's my patch so far:

patch
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ca16bdc16..a284e8f65 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -912,7 +912,7 @@ if(WIN32)
     endif(MINGW AND NOT SHARED)
 endif(WIN32)
 
-if(MACOSX)
+if(MACOSX AND NOT ALLEGRO_SDL)
     list(APPEND LIBRARY_SOURCES ${ALLEGRO_SRC_MACOSX_FILES})
     find_library(APPKIT_LIBRARY AppKit)
     find_library(IOKIT_LIBRARY IOKit)
diff --git a/addons/main/CMakeLists.txt b/addons/main/CMakeLists.txt
index c8a589d69..51f89add9 100644
--- a/addons/main/CMakeLists.txt
+++ b/addons/main/CMakeLists.txt
@@ -1,4 +1,4 @@
-if(MACOSX)
+if(MACOSX AND NOT ALLEGRO_SDL)
     set(MAIN_SOURCES osx_main.m)
     set(SUPPORT_ALLEGRO_MAIN 1)
 endif(MACOSX)
diff --git a/include/allegro5/internal/alconfig.h b/include/allegro5/internal/alconfig.h
index 50367595a..f637a9c79 100644
--- a/include/allegro5/internal/alconfig.h
+++ b/include/allegro5/internal/alconfig.h
@@ -29,7 +29,9 @@
 
 
 
-#if defined ALLEGRO_WATCOM
+#if defined ALLEGRO_SDL
+   #include "allegro5/platform/allegro_sdl_config.h"
+#elif defined ALLEGRO_WATCOM
    #include "allegro5/platform/alwatcom.h"
 #elif defined ALLEGRO_MINGW32
    #include "allegro5/platform/almngw32.h"
@@ -47,8 +49,6 @@
    #include "allegro5/platform/alraspberrypicfg.h"
 #elif defined ALLEGRO_UNIX
    #include "allegro5/platform/alucfg.h"
-#elif defined ALLEGRO_SDL
-   #include "allegro5/platform/allegro_sdl_config.h"
 #else
    #error platform not supported
 #endif
diff --git a/src/allegro.c b/src/allegro.c
index b63c50f75..c33b7fb84 100644
--- a/src/allegro.c
+++ b/src/allegro.c
@@ -35,7 +35,7 @@ uint32_t al_get_allegro_version(void)
  */
 int al_run_main(int argc, char **argv, int (*user_main)(int, char **))
 {
-#ifdef ALLEGRO_MACOSX
+#if defined ALLEGRO_MACOSX && !defined ALLEGRO_SDL
     return _al_osx_run_main(argc, argv, user_main);
 #else
     return user_main(argc, argv);
diff --git a/src/system.c b/src/system.c
index bbf81d2b0..f3a7799e1 100644
--- a/src/system.c
+++ b/src/system.c
@@ -98,7 +98,12 @@ static void shutdown_system_driver(void)
  */
 static ALLEGRO_PATH *early_get_exename_path(void)
 {
-#if defined(ALLEGRO_WINDOWS)
+#if defined(ALLEGRO_SDL)
+   char* p = SDL_GetBasePath();
+   ALLEGRO_PATH *path = al_create_path_for_directory(p);
+   SDL_free(p);
+   return path;
+#elif defined(ALLEGRO_WINDOWS)
    return _al_win_get_path(ALLEGRO_EXENAME_PATH);
 #elif defined(ALLEGRO_MACOSX)
    return _al_osx_get_path(ALLEGRO_EXENAME_PATH);
@@ -108,11 +113,6 @@ static ALLEGRO_PATH *early_get_exename_path(void)
    return _al_unix_get_path(ALLEGRO_EXENAME_PATH);
 #elif defined(ALLEGRO_ANDROID)
    return _al_android_get_path(ALLEGRO_EXENAME_PATH);
-#elif defined(ALLEGRO_SDL)
-   char* p = SDL_GetBasePath();
-   ALLEGRO_PATH *path = al_create_path_for_directory(p);
-   SDL_free(p);
-   return path;
 #else
    #error early_get_exename_path not implemented
 #endif
diff --git a/src/tls.c b/src/tls.c
index 9e0903f1b..546df6c08 100644
--- a/src/tls.c
+++ b/src/tls.c
@@ -164,7 +164,7 @@ static void initialize_tls_values(thread_local_state *tls)
 
 #if defined(ALLEGRO_CFG_DLL_TLS)
    #include "tls_dll.inc"
-#elif defined(ALLEGRO_MACOSX) || defined(ALLEGRO_IPHONE) || defined(ALLEGRO_ANDROID) || defined(ALLEGRO_RASPBERRYPI)
+#elif !defined(ALLEGRO_SDL) && (defined(ALLEGRO_MACOSX) || defined(ALLEGRO_IPHONE) || defined(ALLEGRO_ANDROID) || defined(ALLEGRO_RASPBERRYPI))
    #include "tls_pthread.inc"
 #else
    #include "tls_native.inc"

But when I run the example I get this error:

# in examples build folder
> ./ex_draw        
dyld[24519]: symbol not found in flat namespace '_NSImageFromAllegroBitmap'
[1]    24519 abort      ./ex_draw

I copied all the lib/ files into examples/, but same error.

(also had to build with -DWANT_NATIVE_DIALOG=off)

connorjclark avatar May 15 '22 17:05 connorjclark

OK, just had to disable some stuff in a similar manner in the images addon.

patch
diff --git a/addons/image/CMakeLists.txt b/addons/image/CMakeLists.txt
index 710a49744..7e6732937 100644
--- a/addons/image/CMakeLists.txt
+++ b/addons/image/CMakeLists.txt
@@ -72,7 +72,7 @@ if(WANT_NATIVE_IMAGE_LOADER)
         endif(SUPPORT_GDIPLUS)
     endif(WIN32)
 
-    if (MACOSX)
+    if (MACOSX AND NOT ALLEGRO_SDL)
         set(ALLEGRO_CFG_IIO_SUPPORT_PNG 1)
         set(ALLEGRO_CFG_IIO_SUPPORT_JPG 1)
         list(APPEND IMAGE_SOURCES macosx.m)
diff --git a/addons/image/iio.c b/addons/image/iio.c
index c593438d7..ac64b4196 100644
--- a/addons/image/iio.c
+++ b/addons/image/iio.c
@@ -150,7 +150,7 @@ bool al_init_image_addon(void)
    }
 #endif
 
-#ifdef ALLEGRO_MACOSX
+#if defined ALLEGRO_MACOSX && !defined ALLEGRO_SDL
    success |= _al_osx_register_image_loader();
 #endif
 #endif

Now the example works.

This is quite a lot of conditions to change, so I'm looking into simply setting ALLEGRO_MACOSX to 0 if ALLEGRO_SDL is defined, just like is done for ALLEGRO_UNIX.

connorjclark avatar May 15 '22 18:05 connorjclark

#1336 enables building some simple programs with the SDL backend, but anything using threads throws an error at runtime:

Assertion failed: (NSViewIsCurrentlyBuildingLayerTreeForDisplay() != currentlyBuildingLayerTree), function NSViewSetCurrentlyBuildingLayerTreeForDisplay, file NSView.m, line 13477.
Assertion failed: (NSViewIsCurrentlyBuildingLayerTreeForDisplay() != currentlyBuildingLayerTree), function NSViewSetCurrentlyBuildingLayerTreeForDisplay, file NSView.m, line 13477.
Assertion failed: (NSViewIsCurrentlyBuildingLayerTreeForDisplay() != currentlyBuildingLayerTree), function NSViewSetCurrentlyBuildingLayerTreeForDisplay, file NSView.m, line 13477.
[1]    68792 illegal hardware instruction  ./ex_threads

I think this is because osx_app_delegate.m is required to proxy certain API calls to the main thread on OSX, but is not included if SDL is requested. I've been tinkering with it a bit, but not really getting anywhere.

EDIT: seems osx_app_delegate.m is not the answer

src/macosx/osxgl.m proxies display stuff to the main thread. Apparently, SDL doesn't do anything like that. In e7b2fc9f31a1cf1d08f646bd8b1d3f71e0efc74c @pedro-w proxied some cocoa osx calls to the main thread. Perhaps the same can be done in sdl_system.c, if on osx.

connorjclark avatar May 15 '22 22:05 connorjclark

Any idea on how to progress here?

connorjclark avatar Jun 01 '22 02:06 connorjclark

I think a first step would be to understand how you're meant to use SDL on OSX. Do they have functionality similar to allegro_main-addon?

SiegeLord avatar Jun 01 '22 02:06 SiegeLord

From memory, SDL runs on the main thread - so it's OK to call Cocoa directly but the downside is that you need to call SDL_PumpEvents frequently (directly or indirectly via one of the other SDL_ event functions) otherwise you get the beachball. There is an SDL_main but it doesn't do anything on MacOS (needed for Android maybe?) I may have some time this weekend to do some revision on how it all works.

pedro-w avatar Jun 01 '22 08:06 pedro-w