Reworked the sfml-main module, added Main.hpp, moved the main() internal entry point for iOS from sfml-window to sfml-main

This commit is contained in:
Laurent Gomila 2013-09-06 14:50:15 +02:00 committed by Jonathan De Wachter
parent b9fd685a60
commit 3872b27569
15 changed files with 170 additions and 120 deletions

View File

@ -218,6 +218,9 @@ macro(sfml_add_example target)
target_link_libraries(${target} sfml-main) target_link_libraries(${target} sfml-main)
elseif(IOS) elseif(IOS)
add_executable(${target} MACOSX_BUNDLE ${THIS_SOURCES}) add_executable(${target} MACOSX_BUNDLE ${THIS_SOURCES})
if(THIS_GUI_APP)
target_link_libraries(${target} sfml-main)
endif()
else() else()
add_executable(${target} ${THIS_SOURCES}) add_executable(${target} ${THIS_SOURCES})
endif() endif()
@ -261,6 +264,13 @@ macro(sfml_add_example target)
RUNTIME DESTINATION ${INSTALL_MISC_DIR}/examples/${target} COMPONENT examples RUNTIME DESTINATION ${INSTALL_MISC_DIR}/examples/${target} COMPONENT examples
BUNDLE DESTINATION ${INSTALL_MISC_DIR}/examples/${target} COMPONENT examples) BUNDLE DESTINATION ${INSTALL_MISC_DIR}/examples/${target} COMPONENT examples)
# fix install rules broken in CMake (see http://public.kitware.com/Bug/view.php?id=12506)
if(IOS)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/\$ENV{CONFIGURATION}\$ENV{EFFECTIVE_PLATFORM_NAME}/${target}.app
DESTINATION ${INSTALL_MISC_DIR}/examples/${target}
COMPONENT examples)
endif()
# install the example's source code # install the example's source code
install(FILES ${THIS_SOURCES} install(FILES ${THIS_SOURCES}
DESTINATION ${INSTALL_MISC_DIR}/examples/${target} DESTINATION ${INSTALL_MISC_DIR}/examples/${target}

View File

@ -4,6 +4,7 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp> #include <SFML/OpenGL.hpp>
#include <SFML/Main.hpp>
// Some platform-specific stuff // Some platform-specific stuff

View File

@ -4,6 +4,7 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include "Effect.hpp" #include "Effect.hpp"
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <SFML/Main.hpp>
#include <vector> #include <vector>
#include <cmath> #include <cmath>

View File

@ -3,6 +3,7 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/Window.hpp> #include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp> #include <SFML/OpenGL.hpp>
#include <SFML/Main.hpp>
// Some platform-specific stuff // Some platform-specific stuff

View File

@ -31,10 +31,12 @@
#include <SFML/Config.hpp> #include <SFML/Config.hpp>
// On iOS, we have no choice but to have our own main,
// so we need to rename the user one and call it later
#if defined(SFML_SYSTEM_IOS) #if defined(SFML_SYSTEM_IOS)
// On iOS, we have no choice but to have our own main,
// so we need to rename the user one and call it later
#define main sfmlMain #define main sfmlMain
#endif #endif

View File

@ -35,7 +35,6 @@
#include <SFML/Window/Event.hpp> #include <SFML/Window/Event.hpp>
#include <SFML/Window/Joystick.hpp> #include <SFML/Window/Joystick.hpp>
#include <SFML/Window/Keyboard.hpp> #include <SFML/Window/Keyboard.hpp>
#include <SFML/Window/Main.hpp>
#include <SFML/Window/Mouse.hpp> #include <SFML/Window/Mouse.hpp>
#include <SFML/Window/VideoMode.hpp> #include <SFML/Window/VideoMode.hpp>
#include <SFML/Window/Window.hpp> #include <SFML/Window/Window.hpp>

View File

@ -45,6 +45,4 @@ add_subdirectory(Graphics)
if(NOT SFML_OS_IOS) if(NOT SFML_OS_IOS)
add_subdirectory(Audio) add_subdirectory(Audio)
endif() endif()
if(SFML_OS_WINDOWS OR SFML_OS_ANDROID) add_subdirectory(Main)
add_subdirectory(Main)
endif()

View File

@ -1,8 +1,17 @@
set(INCROOT ${PROJECT_SOURCE_DIR}/include/SFML/Main)
set(SRCROOT ${PROJECT_SOURCE_DIR}/src/SFML/Main)
# sources # sources
set(SRC if(WINDOWS)
${PROJECT_SOURCE_DIR}/src/SFML/Main/SFML_Main.cpp set(SRC ${SRC} ${SRCROOT}/MainWin32.cpp)
) elseif(IOS)
set(SRC ${SRC} ${SRCROOT}/MainiOS.mm)
elseif(ANDROID)
set(SRC ${SRC} ${SRCROOT}/MainAndroid.cpp)
else()
return()
endif()
source_group("" FILES ${SRC}) source_group("" FILES ${SRC})
if(NOT ANDROID) if(NOT ANDROID)

View File

@ -23,48 +23,9 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////// #include <SFML/Config.hpp>
// Windows specific : defines the WinMain entry function,
// so that developers can use the standard main function
// even in a Win32 Application project, and keep a portable code
////////////////////////////////////////////////////////////
#ifdef SFML_SYSTEM_ANDROID
#if defined(_WIN32)
#include <windows.h>
extern int main(int argc, char* argv[]);
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
{
return main(__argc, __argv);
}
#endif // _WIN32
#if defined(_WIN32)
////////////////////////////////////////////////////////////////////////////////
// Windows specific : defines the WinMain entry function,
// so that developers can use the standard main function
// even in a Win32 Application project, and keep a portable code
////////////////////////////////////////////////////////////////////////////////
#include <windows.h>
extern int main(int argc, char* argv[]);
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
{
return main(__argc, __argv);
}
#elif defined(__ANDROID__)
#include <SFML/Window/Keyboard.hpp> #include <SFML/Window/Keyboard.hpp>
#include <SFML/System/Sleep.hpp> #include <SFML/System/Sleep.hpp>
@ -700,4 +661,4 @@ void ANativeActivity_onCreate(ANativeActivity* activity, void* savedState, size_
activity->instance = states; activity->instance = states;
} }
#endif #endif // SFML_SYSTEM_ANDROID

View File

@ -2,6 +2,7 @@
// //
// SFML - Simple and Fast Multimedia Library // SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com) // Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com)
// Copyright (C) 2013 Jonathan De Wachter (dewachter.jonathan@gmail.com)
// //
// This software is provided 'as-is', without any express or implied warranty. // This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software. // In no event will the authors be held liable for any damages arising from the use of this software.
@ -22,17 +23,32 @@
// //
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Windows specific: we define the WinMain entry point,
// so that developers can use the standard main function
// even in a Win32 Application project, and thus keep a
// portable code
////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Headers // Headers
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/Window/iOS/SFAppDelegate.hpp> #include <SFML/Config.hpp>
#ifdef SFML_SYSTEM_WINDOWS
#include <windows.h>
extern int main(int argc, char* argv[]);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
int main(int argc, char** argv) int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
{ {
@autoreleasepool return main(__argc, __argv);
{
[SFAppDelegate main:argc argv:argv];
}
} }
#endif // SFML_SYSTEM_WINDOWS

63
src/SFML/Main/MainiOS.mm Normal file
View File

@ -0,0 +1,63 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com)
// Copyright (C) 2013 Jonathan De Wachter (dewachter.jonathan@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// iOS specific: SFML needs to hook the main function, to
// launch the iOS application (event loop), and then call the
// user main from inside it.
//
// Our strategy is to rename the user main to 'sfmlMain' with
// a macro (see Main.hpp), and call this modified main ourselves.
//
// Note that half of this trick (the sfmlMain placeholders and
// the application delegate) is defined sfml-window; see there
// for the full implementation.
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#ifdef SFML_SYSTEM_IOS
#include <UIKit/UIKit.h>
////////////////////////////////////////////////////////////
int main(int argc, char** argv)
{
// Note: we intentionally drop command line arguments,
// there's no such thing as a command line on an iOS device :)
// Important: "SFAppDelegate" must always match the name of the
// application delegate class defined in sfml-window
return UIApplicationMain(argc, argv, nil, @"SFAppDelegate");
}
#endif // SFML_SYSTEM_IOS

View File

@ -21,7 +21,6 @@ set(SRC
${SRCROOT}/JoystickManager.hpp ${SRCROOT}/JoystickManager.hpp
${INCROOT}/Keyboard.hpp ${INCROOT}/Keyboard.hpp
${SRCROOT}/Keyboard.cpp ${SRCROOT}/Keyboard.cpp
${INCROOT}/Main.hpp
${INCROOT}/Mouse.hpp ${INCROOT}/Mouse.hpp
${SRCROOT}/Mouse.cpp ${SRCROOT}/Mouse.cpp
${SRCROOT}/VideoMode.cpp ${SRCROOT}/VideoMode.cpp
@ -127,7 +126,6 @@ elseif(IOS)
${SRCROOT}/iOS/VideoModeImpl.mm ${SRCROOT}/iOS/VideoModeImpl.mm
${SRCROOT}/iOS/WindowImplUIKit.hpp ${SRCROOT}/iOS/WindowImplUIKit.hpp
${SRCROOT}/iOS/WindowImplUIKit.mm ${SRCROOT}/iOS/WindowImplUIKit.mm
${SRCROOT}/iOS/Main.mm
${SRCROOT}/iOS/ObjCType.hpp ${SRCROOT}/iOS/ObjCType.hpp
${SRCROOT}/iOS/SFAppDelegate.hpp ${SRCROOT}/iOS/SFAppDelegate.hpp
${SRCROOT}/iOS/SFAppDelegate.mm ${SRCROOT}/iOS/SFAppDelegate.mm

View File

@ -41,12 +41,6 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
@interface SFAppDelegate : NSObject<UIApplicationDelegate> @interface SFAppDelegate : NSObject<UIApplicationDelegate>
////////////////////////////////////////////////////////////
/// \brief Run the application
///
////////////////////////////////////////////////////////////
+(int)main:(int)argc argv:(char**)argv;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Return the instance of the application delegate /// \brief Return the instance of the application delegate
/// ///

View File

@ -31,10 +31,6 @@
namespace namespace
{ {
// Save the main's arguments, to pass them back to the user's main
int mainArgc;
char** mainArgv;
// Save the global instance of the delegate // Save the global instance of the delegate
SFAppDelegate* delegateInstance = NULL; SFAppDelegate* delegateInstance = NULL;
} }
@ -53,15 +49,6 @@ namespace
@synthesize sfWindow; @synthesize sfWindow;
////////////////////////////////////////////////////////////
+(int)main:(int)argc argv:(char**)argv
{
mainArgc = argc;
mainArgv = argv;
return UIApplicationMain(argc, argv, nil, NSStringFromClass([SFAppDelegate class]));
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
+(SFAppDelegate*)getInstance +(SFAppDelegate*)getInstance
{ {
@ -72,7 +59,8 @@ namespace
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
-(void)runUserMain -(void)runUserMain
{ {
sfmlMain(mainArgc, mainArgv); // Arguments intentionally dropped, see comments in main in sfml-main
sfmlMain(0, NULL);
} }

View File

@ -28,14 +28,23 @@
#include <SFML/Window/iOS/SFMain.hpp> #include <SFML/Window/iOS/SFMain.hpp>
// We declare both versions of sfmlMain, but with the 'weak' attribute (GCC extension) // sfmlMain is called by the application delegate (SFAppDelegate).
// so that the user-declared one will replace SFML's one at linking stage.
// //
// If user defines main(argc, argv) then it will be called directly, // Since we don't know which prototype of main the user
// if he defines main() then it will be called by our placeholder. // defines, we declare both versions of sfmlMain, but with
// the 'weak' attribute (GCC extension) so that the
// user-declared one will replace SFML's one at linking stage.
// //
// The sfmlMain() version is never called, it is just defined to avoid a // If user defines main(argc, argv) then it will be called
// linker error if the user directly defines the version with arguments. // directly, if he defines main() then it will be called by
// our placeholder.
//
// The sfmlMain() version is never called, it is just defined
// to avoid a linker error if the user directly defines the
// version with arguments.
//
// See the sfml-main module for the other half of this
// initialization trick.
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////