Re: [AD] Preparing for 4.9.9

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


On Wed, 2009-03-11 at 00:17 +1100, Peter Wang wrote:
> > 
> > Well, it's a very minimal impact patch - I don't think it could break
> > anything. Basically I just exposed what END_OF_MAIN does behind drawn
> > curtains right now.
> 
> I found the patch.  Ok, probably simple enough.  But if it breaks
> something, you get to fix it ;)
> 
> Evert sounds like he has some objections about it though,
> so you'll have to work it out with him.
> 

Updated the patch, including the "#define main al_main" to make it work
just like SDL. I also left in END_OF_MAIN defined to nothing as to not
have a line for each example in the patch.

However, some testing with the mingw cross-compiler reveals that it is
only working under Windows with static linking. With SHARED enabled in
cmake, gcc complains about not finding al_main while creating the DLL.

I have no idea why, but I guess you somehow need to use those __declspec
stuff to tell it that the symbol will be imported from the main program
at runtime. In Linux there is no problem with calling al_main() from the
main() inside the .so... really strange. I compared why this works with
SDL, but there you always have to statically link in libSDLmain.a -
which conveniently has the WinMain() in it :) In fact, maybe that's the
real reason why they introduced libSDLmain...

So if nobody sees a simple solution how to tell gcc about the external
al_main symbol when creating the DLL in Windows, I'll try to change the
build to have WinMain() not inside the DLL, but inside liballegro_main.a
- just like SDL does it. It means this has to wait for after 4.9.9
though.

Also, the trick to allow "int main(void)" does not work for C++ as the
prototype has to be declared with extern "C", and C++ does not allow
inexact prototypes as C does. The two solutions were to either keep
allowing both prototypes, but require C++ programs to define main with
'extern "C"', or require C++ programs to use the full prototype. In the
patch I went for the latter.

-- 
Elias Pschernig <elias@xxxxxxxxxx>
diff --git a/examples/ex_audio_props.cpp b/examples/ex_audio_props.cpp
index d3ad958..384410a 100644
--- a/examples/ex_audio_props.cpp
+++ b/examples/ex_audio_props.cpp
@@ -86,7 +86,7 @@ void Prog::update_properties()
    al_set_sample_instance_float(sample_inst, ALLEGRO_AUDIOPROP_GAIN, gain);
 }
 
-int main(int argc, const char *argv[])
+int main(int argc, char *argv[])
 {
    ALLEGRO_DISPLAY *display;
 
diff --git a/examples/ex_blend2.cpp b/examples/ex_blend2.cpp
index 5990cac..0295815 100644
--- a/examples/ex_blend2.cpp
+++ b/examples/ex_blend2.cpp
@@ -209,7 +209,7 @@ void Prog::draw_samples()
    al_restore_state(&state);
 }
 
-int main()
+int main(int argc, char **argv)
 {
    ALLEGRO_DISPLAY *display;
    ALLEGRO_FONT *font;
diff --git a/examples/ex_color.cpp b/examples/ex_color.cpp
index dda93ef..e33caed 100644
--- a/examples/ex_color.cpp
+++ b/examples/ex_color.cpp
@@ -147,7 +147,7 @@ void Prog::run()
    }
 }
 
-int main()
+int main(int argc, char **argv)
 {
    ALLEGRO_DISPLAY *display;
    ALLEGRO_FONT *font;
diff --git a/examples/ex_d3d.cpp b/examples/ex_d3d.cpp
index a195adc..ab258b0 100644
--- a/examples/ex_d3d.cpp
+++ b/examples/ex_d3d.cpp
@@ -17,7 +17,7 @@ struct D3DVERTEX
    DWORD dwColor;
 };
 
-int main(void)
+int main(int argc, char **argv)
 {
    ALLEGRO_DISPLAY *display;
 
diff --git a/examples/ex_font_justify.cpp b/examples/ex_font_justify.cpp
index 391da4f..9682f06 100644
--- a/examples/ex_font_justify.cpp
+++ b/examples/ex_font_justify.cpp
@@ -82,7 +82,7 @@ void Prog::draw_text()
       al_map_rgb(0, 255, 0), 0);
 }
 
-int main()
+int main(int argc, char **argv)
 {
    ALLEGRO_DISPLAY *display;
 
diff --git a/examples/ex_pixelformat.cpp b/examples/ex_pixelformat.cpp
index ffde653..df81cb3 100644
--- a/examples/ex_pixelformat.cpp
+++ b/examples/ex_pixelformat.cpp
@@ -167,7 +167,7 @@ void Prog::draw_sample()
 }
 
 
-int main(void)
+int main(int argc, char **argv)
 {
    ALLEGRO_DISPLAY *display;
    ALLEGRO_FONT *font;
diff --git a/include/allegro5/internal/alconfig.h b/include/allegro5/internal/alconfig.h
index d6b1b1c..3785cd1 100644
--- a/include/allegro5/internal/alconfig.h
+++ b/include/allegro5/internal/alconfig.h
@@ -261,6 +261,17 @@
    }
 #endif
 
+#ifndef ALLEGRO_NO_MAGIC_MAIN
+   #ifdef ALLEGRO_SRC
+      extern int al_main(int argc, char **argv);
+   #else
+      #ifdef __cplusplus
+      extern "C" int al_main(int argc, char **argv);
+      #endif
+      #define main al_main
+   #endif
+#endif
+
 #ifndef END_OF_MAIN
    #define END_OF_MAIN()
 #endif
diff --git a/include/allegro5/platform/almngw32.h b/include/allegro5/platform/almngw32.h
index 5bfa872..2cd3979 100644
--- a/include/allegro5/platform/almngw32.h
+++ b/include/allegro5/platform/almngw32.h
@@ -43,7 +43,6 @@
 
 #ifdef ALLEGRO_USE_CONSOLE
    #define ALLEGRO_CONSOLE_OK
-   #define ALLEGRO_NO_MAGIC_MAIN
 #endif
 
 
diff --git a/include/allegro5/platform/almsvc.h b/include/allegro5/platform/almsvc.h
index bcf8d3c..e7b5bf1 100644
--- a/include/allegro5/platform/almsvc.h
+++ b/include/allegro5/platform/almsvc.h
@@ -43,7 +43,6 @@
 
 #ifdef ALLEGRO_USE_CONSOLE
    #define ALLEGRO_CONSOLE_OK
-   #define ALLEGRO_NO_MAGIC_MAIN
 #endif
 
 #ifdef ALLEGRO_AND_MFC
diff --git a/include/allegro5/platform/alosx.h b/include/allegro5/platform/alosx.h
index ff0c2df..559d5cb 100644
--- a/include/allegro5/platform/alosx.h
+++ b/include/allegro5/platform/alosx.h
@@ -51,22 +51,6 @@
 #endif
 
 
-/* The following code comes from alunix.h */
-/* Magic to capture name of executable file */
-extern int    __crt0_argc;
-extern char **__crt0_argv;
-
-#ifndef ALLEGRO_NO_MAGIC_MAIN
-   #define ALLEGRO_MAGIC_MAIN
-   #define main _mangled_main
-   #undef END_OF_MAIN
-   #define END_OF_MAIN() void *_mangled_main_address = (void*) _mangled_main;
-#else
-   #undef END_OF_MAIN
-   #define END_OF_MAIN() void *_mangled_main_address;
-#endif
-
-
 /* System driver */
 #define SYSTEM_MACOSX           AL_ID('O','S','X',' ')
 /* Keyboard driver */
diff --git a/include/allegro5/platform/alunix.h b/include/allegro5/platform/alunix.h
index 3f14850..695b49b 100644
--- a/include/allegro5/platform/alunix.h
+++ b/include/allegro5/platform/alunix.h
@@ -19,23 +19,3 @@
 #ifndef ALLEGRO_UNIX
    #error bad include
 #endif
-
-
-/* magic to capture name of executable file */
-extern int    __crt0_argc;
-extern char **__crt0_argv;
-
-#ifdef ALLEGRO_WITH_MAGIC_MAIN
-
-   #ifndef ALLEGRO_NO_MAGIC_MAIN
-      #define ALLEGRO_MAGIC_MAIN
-      #define main _mangled_main
-      #undef END_OF_MAIN
-      #define END_OF_MAIN() void *_mangled_main_address = (void*) _mangled_main;
-   #else
-      #undef END_OF_MAIN
-      #define END_OF_MAIN() void *_mangled_main_address;
-   #endif
-
-#endif
-
diff --git a/include/allegro5/platform/alwin.h b/include/allegro5/platform/alwin.h
index e2f2a70..93ff453 100644
--- a/include/allegro5/platform/alwin.h
+++ b/include/allegro5/platform/alwin.h
@@ -24,42 +24,6 @@
    #include <windows.h>
 #endif
 
-/*******************************************/
-/********** magic main emulation ***********/
-/*******************************************/
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-AL_FUNC(int, _WinMain, (void *_main, void *hInst, void *hPrev, char *Cmd, int nShow));
-
-#ifdef __cplusplus
-}
-#endif
-
-
-#if (!defined ALLEGRO_NO_MAGIC_MAIN) && (!defined ALLEGRO_SRC)
-
-   #define ALLEGRO_MAGIC_MAIN
-   #define main _mangled_main
-   #undef END_OF_MAIN
-
-   /* disable strict pointer typing because of the vague prototype below */
-   #define NO_STRICT
-
-   #ifdef __cplusplus
-      extern "C" int __stdcall WinMain(HINSTANCE hInst, HINSTANCE hPrev, char *Cmd, int nShow);
-   #endif
-
-   #define END_OF_MAIN()                                                     \
-                                                                             \
-      int __stdcall WinMain(HINSTANCE hInst, HINSTANCE hPrev, char *Cmd, int nShow)  \
-      {                                                                      \
-         return _WinMain((void *)_mangled_main, hInst, hPrev, Cmd, nShow);   \
-      }
-
-#endif
-
 
 
 /*******************************************/
diff --git a/src/macosx/main.m b/src/macosx/main.m
index 274a701..f41b3da 100644
--- a/src/macosx/main.m
+++ b/src/macosx/main.m
@@ -32,7 +32,6 @@
 /* For compatibility with the unix code */
 extern int    __crt0_argc;
 extern char **__crt0_argv;
-extern void *_mangled_main_address;
 
 static char *arg0, *arg1 = NULL;
 static int refresh_rate = 70;
@@ -223,8 +222,7 @@ static BOOL in_bundle(void)
 /* Call the user main() */
 static void call_user_main(void)
 {
-	int (*real_main)(int, char*[]) = (int (*)(int, char*[])) _mangled_main_address;
-	exit(real_main(__crt0_argc, __crt0_argv));
+	exit(al_main(__crt0_argc, __crt0_argv));
 }
 
 
diff --git a/src/unix/umain.c b/src/unix/umain.c
index 2afeda7..0493862 100644
--- a/src/unix/umain.c
+++ b/src/unix/umain.c
@@ -18,28 +18,11 @@
 
 #include "allegro5/internal/alconfig.h"
 
-#ifdef ALLEGRO_WITH_MAGIC_MAIN
-
-#undef main
-
-
-extern int    __crt0_argc;
-extern char **__crt0_argv;
-extern void *_mangled_main_address;
-
-
-
 /* main:
  *  Replacement for main function (capture arguments and call real main).
  */
-int main(int argc, char *argv[])
+int main(int argc, char **argv)
 {
-   int (*real_main) (int, char*[]) = (int (*) (int, char*[])) _mangled_main_address;
-
-   __crt0_argc = argc;
-   __crt0_argv = argv;
-
-   return (*real_main)(argc, argv);
+   return al_main(argc, argv);
 }
 
-#endif
diff --git a/src/win/wnewsys.c b/src/win/wnewsys.c
index 6ed920b..ea59de0 100644
--- a/src/win/wnewsys.c
+++ b/src/win/wnewsys.c
@@ -59,14 +59,14 @@ static bool using_higher_res_timer;
 static ALLEGRO_SYSTEM_WIN *_al_win_system;
 
 
+
 /* _WinMain:
  *  Entry point for Windows GUI programs, hooked by a macro in alwin.h,
  *  which makes it look as if the application can still have a normal
  *  main() function.
  */
-int _WinMain(void *_main, void *hInst, void *hPrev, char *Cmd, int nShow)
+int __stdcall WinMain(HINSTANCE hInst, HINSTANCE hPrev, char *Cmd, int nShow)
 {
-   int (*mainfunc) (int argc, char *argv[]) = (int (*)(int, char *[]))_main;
    char *argbuf;
    char *cmdline;
    char **argv;
@@ -133,7 +133,7 @@ int _WinMain(void *_main, void *hInst, void *hPrev, char *Cmd, int nShow)
    argv[argc] = NULL;
 
    /* call the application entry point */
-   i = mainfunc(argc, argv);
+   i = al_main(argc, argv);
 
    _AL_FREE(argv);
    _AL_FREE(argbuf);


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/