now engine can be optionally built as library, there's an example wrapper of how to use it
authorkoda
Sat, 05 Jun 2010 14:07:58 +0000 (2010-06-05)
changeset 3495 a6b4f351d400
parent 3494 208c5671b202
child 3496 8fb007d36c09
now engine can be optionally built as library, there's an example wrapper of how to use it building server is now disabled by default, saves users some headaches
CMakeLists.txt
cocoaTouch/GameSetup.m
cocoaTouch/MapConfigViewController.h
cocoaTouch/MapConfigViewController.m
cocoaTouch/otherSrc/CommodityFunctions.m
hedgewars/CMakeLists.txt
hedgewars/hwengine.pas
misc/wrapper.c
--- a/CMakeLists.txt	Fri Jun 04 20:50:24 2010 +0000
+++ b/CMakeLists.txt	Sat Jun 05 14:07:58 2010 +0000
@@ -39,6 +39,14 @@
 #	MESSAGE(FATAL_ERROR "In-tree-build detected!")
 #ENDIF (${CMAKE_SOURCE_DIR} MATCHES ${CMAKE_BINARY_DIR})
 
+#set some safe values
+IF(NOT WITH_SERVER)
+	SET(WITH_SERVER 0)
+ENDIF(NOT WITH_SERVER)
+IF(NOT BUILD_ENGINE_LIBRARY)
+	SET(BUILD_ENGINE_LIBRARY 0)
+ENDIF(NOT BUILD_ENGINE_LIBRARY)
+
 
 if(APPLE)
 	set(CMAKE_FIND_FRAMEWORK "FIRST")
@@ -92,17 +100,18 @@
 		set(CMAKE_OSX_SYSROOT "/Developer/SDKs/MacOSX10.4u.sdk/")
 		if(current_macosx_version MATCHES "10.4")
 			find_package(SDL_mixer REQUIRED)
-			set(pascal_compiler_flags_cmn "-k-dylib_file @loader_path/Frameworks/smpeg.framework/Versions/A/smpeg:${SDLMIXER_LIBRARY}/Versions/A/Frameworks/smpeg.framework/Versions/A/smpeg" ${pascal_compiler_flags_cmn})
-			set(pascal_compiler_flags_cmn "-k-dylib_file @loader_path/Frameworks/mikmod.framework/Versions/A/mikmod:${SDLMIXER_LIBRARY}/Versions/A/Frameworks/mikmod.framework/Versions/A/mikmod" ${pascal_compiler_flags_cmn})
+			set(pascal_compiler_flags_cmn "-k-dylib_file @loader_path/Frameworks/smpeg.framework/Versions/A/smpeg:${SDLMIXER_LIBRARY}/Versions/A/Frameworks/smpeg.framework/Versions/A/smpeg" "-k-dylib_file @loader_path/Frameworks/mikmod.framework/Versions/A/mikmod:${SDLMIXER_LIBRARY}/Versions/A/Frameworks/mikmod.framework/Versions/A/mikmod" ${pascal_compiler_flags_cmn})
 			set(CMAKE_C_FLAGS "-dylib_file @loader_path/Frameworks/smpeg.framework/Versions/A/smpeg:${SDLMIXER_LIBRARY}/Versions/A/Frameworks/smpeg.framework/Versions/A/smpeg -dylib_file @loader_path/Frameworks/mikmod.framework/Versions/A/mikmod:${SDLMIXER_LIBRARY}/Versions/A/Frameworks/mikmod.framework/Versions/A/mikmod")
 		endif()
 	else()
 		set(CMAKE_OSX_SYSROOT "/Developer/SDKs/MacOSX${minimum_macosx}.sdk/")
 	endif()
 
-	#1.set deployment target; 2.link with libsdlmain.a; 3.link with liblua.a (which requires readline)
+	#1.set deployment target; 2.link with libsdlmain.a (when building an executable); 3.link with liblua.a (which requires readline)
 	set(pascal_compiler_flags_cmn "-k-macosx_version_min" "-k${minimum_macosx}" "-XR${CMAKE_OSX_SYSROOT}" ${pascal_compiler_flags_cmn})
-	set(pascal_compiler_flags_cmn "-k${CMAKE_BINARY_DIR}/bin/libSDLmain.a" ${pascal_compiler_flags_cmn})
+	if(NOT BUILD_ENGINE_LIBRARY)
+		set(pascal_compiler_flags_cmn "-k${CMAKE_BINARY_DIR}/bin/libSDLmain.a" ${pascal_compiler_flags_cmn})
+	endif()
 	set(pascal_compiler_flags_cmn "-k${CMAKE_BINARY_DIR}/bin/liblua.a" "-k-lreadline" ${pascal_compiler_flags_cmn})
 else(APPLE)
 	set(target_dir "bin")
@@ -161,6 +170,7 @@
 set(HEDGEWARS_PROTO_VER 32)
 
 if(WITH_SERVER)
+	message(STATUS "Server is going to be built! Make sure you have GHC installed")
 	set(HAVE_NETSERVER true)
 	add_subdirectory(gameServer)
 else(WITH_SERVER)
--- a/cocoaTouch/GameSetup.m	Fri Jun 04 20:50:24 2010 +0000
+++ b/cocoaTouch/GameSetup.m	Sat Jun 05 14:07:58 2010 +0000
@@ -397,7 +397,7 @@
     CGRect screenBounds = [[UIScreen mainScreen] bounds];
     NSString *wSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.width];
     NSString *hSize = [[NSString alloc] initWithFormat:@"%d", (int) screenBounds.size.height];
-	const char **gameArgs = (const char**) malloc(sizeof(char *) * 8);
+	const char **gameArgs = (const char**) malloc(sizeof(char *) * 9);
 
     /*
     size_t size;
@@ -435,6 +435,7 @@
 	gameArgs[5] = [[[self.systemSettings objectForKey:@"alternate"] stringValue] UTF8String];	//cAltDamage
 	gameArgs[6] = [wSize UTF8String];                                                           //cScreenHeight
     gameArgs[7] = [hSize UTF8String];                                                           //cScreenWidth
+    gameArgs[8] = NULL;                                                                         //recordFileName
     
     [wSize release];
     [hSize release];
--- a/cocoaTouch/MapConfigViewController.h	Fri Jun 04 20:50:24 2010 +0000
+++ b/cocoaTouch/MapConfigViewController.h	Sat Jun 05 14:07:58 2010 +0000
@@ -65,6 +65,6 @@
 -(void) setLabelText:(NSString *)str;
 -(void) setButtonImage:(UIImage *)img;
 
--(uint8_t *)engineProtocol:(NSInteger) port;
+-(const uint8_t *)engineProtocol:(NSInteger) port;
 
 @end
--- a/cocoaTouch/MapConfigViewController.m	Fri Jun 04 20:50:24 2010 +0000
+++ b/cocoaTouch/MapConfigViewController.m	Sat Jun 05 14:07:58 2010 +0000
@@ -33,10 +33,10 @@
 	return SDLNet_TCP_Send(csd, [string UTF8String], length);
 }
 
--(uint8_t *)engineProtocol:(NSInteger) port {
+-(const uint8_t *)engineProtocol:(NSInteger) port {
 	IPaddress ip;
 	BOOL serverQuit = NO;
-    uint8_t map[128*32];
+    static uint8_t map[128*32];
     
 	if (SDLNet_Init() < 0) {
 		NSLog(@"SDLNet_Init: %s", SDLNet_GetError());
@@ -93,7 +93,7 @@
     
     // select the port for IPC and launch the preview generation through engineProtocol:
     int port = randomPort();
-    uint8_t *map = [self engineProtocol:port];
+    const uint8_t *map = [self engineProtocol:port];
     uint8_t mapExp[128*32*8];
 
     // draw the buffer (1 pixel per component, 0= transparent 1= color)
--- a/cocoaTouch/otherSrc/CommodityFunctions.m	Fri Jun 04 20:50:24 2010 +0000
+++ b/cocoaTouch/otherSrc/CommodityFunctions.m	Sat Jun 05 14:07:58 2010 +0000
@@ -7,7 +7,6 @@
 //
 
 #import "CommodityFunctions.h"
-#import "SDL_uikitappdelegate.h"
 #import <mach/mach.h>
 #import <mach/mach_host.h>
 
--- a/hedgewars/CMakeLists.txt	Fri Jun 04 20:50:24 2010 +0000
+++ b/hedgewars/CMakeLists.txt	Sat Jun 05 14:07:58 2010 +0000
@@ -11,24 +11,24 @@
 #if the headers are not installed, the newer apis won't be activated
 find_file(sdlmixer_h SDL_mixer.h ${SDLMIXER_INCLUDE_DIR})
 if(sdlmixer_h)
-file(STRINGS ${sdlmixer_h} sdlmixer_version_tmp REGEX "SDL_MIXER_PATCHLEVEL[\t' ']+[0-9]+")
-string(REGEX MATCH ".([0-9]+)" sdlmixer_version "${sdlmixer_version_tmp}")
+	file(STRINGS ${sdlmixer_h} sdlmixer_version_tmp REGEX "SDL_MIXER_PATCHLEVEL[\t' ']+[0-9]+")
+	string(REGEX MATCH ".([0-9]+)" sdlmixer_version "${sdlmixer_version_tmp}")
 
-if(sdlmixer_version GREATER 9)
-message(STATUS "Enabling enhanced SDL_Mixer calls")
-set(pascal_compiler_flags_cmn "-dSDL_MIXER_NEWER" ${pascal_compiler_flags_cmn})
-endif()
+	if(sdlmixer_version GREATER 9)
+		message(STATUS "Enabling enhanced SDL_Mixer calls")
+		set(pascal_compiler_flags_cmn "-dSDL_MIXER_NEWER" ${pascal_compiler_flags_cmn})
+	endif()
 endif()
 
 find_file(sdlimage_h SDL_image.h ${SDLIMAGE_INCLUDE_DIR})
 if(sdlimage_h)
-file(STRINGS ${sdlimage_h} sdlimage_version_tmp REGEX "SDL_IMAGE_PATCHLEVEL[\t' ']+[0-9]+")
-string(REGEX MATCH ".([0-9]+)" sdlimage_version "${sdlimage_version_tmp}")
+	file(STRINGS ${sdlimage_h} sdlimage_version_tmp REGEX "SDL_IMAGE_PATCHLEVEL[\t' ']+[0-9]+")
+	string(REGEX MATCH ".([0-9]+)" sdlimage_version "${sdlimage_version_tmp}")
 
-if(sdlimage_version GREATER 7)
-message(STATUS "Enabling enhanced SDL_Image calls")
-set(pascal_compiler_flags_cmn "-dSDL_IMAGE_NEWER" ${pascal_compiler_flags_cmn})
-endif()
+	if(sdlimage_version GREATER 7)
+		message(STATUS "Enabling enhanced SDL_Image calls")
+		set(pascal_compiler_flags_cmn "-dSDL_IMAGE_NEWER" ${pascal_compiler_flags_cmn})
+	endif()
 endif()
 
 #SOURCE AND PROGRAMS SECTION 
@@ -78,6 +78,14 @@
 	${CMAKE_CURRENT_BINARY_DIR}/config.inc
 	)
 
+if(BUILD_ENGINE_LIBRARY)
+	message(STATUS "Engine will be built as library (experimental)")
+	set(hwengine_project ${hedgewars_SOURCE_DIR}/hedgewars/hwLibrary.pas)
+	set(engine_sources ${hwengine_project} PascalExports.pas ${engine_sources})
+	set(pascal_compiler_flags_cmn "-dHWLIBRARY" "-k-no_order_inits" ${pascal_compiler_flags_cmn})
+endif(BUILD_ENGINE_LIBRARY)
+
+
 find_program(fpc_executable ${fpc_tryexe})
 
 if(fpc_executable)
@@ -133,8 +141,8 @@
 
 
 #DEPENDECIES AND EXECUTABLES SECTION
-IF(NOT APPLE)
-#here is the standard command for any system
+IF(NOT APPLE OR BUILD_ENGINE_LIBRARY)
+	#here is the command for standard executables or for shared library
 	add_custom_command(OUTPUT "${EXECUTABLE_OUTPUT_PATH}/hwengine${CMAKE_EXECUTABLE_SUFFIX}"
 		COMMAND "${pascal_compiler}"
 		ARGS ${pascal_compiler_flags}
@@ -145,7 +153,6 @@
 	#let's build sdlmain, which is absent from the framework
 	find_package(SDL REQUIRED)
 
-#	set(CMAKE_OSX_ARCHITECTURES "x86_64;i386;ppc7400")
 	set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
 	include_directories(${SDL_INCLUDE_DIR})
 
--- a/hedgewars/hwengine.pas	Fri Jun 04 20:50:24 2010 +0000
+++ b/hedgewars/hwengine.pas	Sat Jun 05 14:07:58 2010 +0000
@@ -73,7 +73,7 @@
 var isTerminated: boolean = false;
     alsoShutdownFrontend: boolean = false;
 {$IFDEF HWLIBRARY}
-type arrayofpchar = array[0..7] of PChar;
+type arrayofpchar = array[0..8] of PChar;
 
 procedure initEverything;
 procedure freeEverything;
@@ -265,6 +265,7 @@
     val(gameArgs[7], cScreenWidth);
     cInitHeight:= cScreenHeight;
     cInitWidth:= cScreenWidth;
+    recordFileName:= gameArgs[8];
 {$ENDIF}
 
     for p:= Succ(Low(TPathType)) to High(TPathType) do
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/misc/wrapper.c	Sat Jun 05 14:07:58 2010 +0000
@@ -0,0 +1,32 @@
+/*
+ This is an experimental main to use with hwLibary
+ - create the library with `cmake . -DBUILD_ENGINE_LIBRARY=1' and `make hwengine'
+ - compile this file with `gcc libhwLibrary.dylib libSDLmain.a wrapper.c -o wrapper -framework Cocoa -framework SDL'
+   (in Mac OS X, but this command line shouldn't be much different in other OSes; make sure to set the correct files/paths)
+ - this executable expect a save file "Save.hws" and the data folder "Data" to be in the same launching directory
+ */
+
+#import <stdio.h>
+#import <stdlib.h>
+
+extern void Game (const char **);
+
+int SDL_main (int argc, const char **argv) {
+    
+    const char **gameArgs = (const char**) malloc(sizeof(char *) * 9);
+    
+    gameArgs[0] = "wrapper";    //UserNick
+	gameArgs[1] = "0";          //ipcPort
+	gameArgs[2] = "0";          //isSoundEnabled
+	gameArgs[3] = "0";          //isMusicEnabled
+	gameArgs[4] = "en.txt";     //cLocaleFName
+	gameArgs[5] = "0";          //cAltDamage
+	gameArgs[6] = "768";        //cScreenHeight
+    gameArgs[7] = "1024";       //cScreenHeight
+    gameArgs[8] = "Save.hws";   //recordFileName
+    
+    Game(gameArgs);
+    free(gameArgs);
+    
+    return 0;
+}