diff --git a/.gitignore b/.gitignore index 5b6d4431..2e3c214e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,13 @@ CMakeFiles cmake_install.cmake Makefile /otclient +/android/project/build.xml +/android/project/proguard-project.txt +/android/project/gen* +/android/project/bin* +/android/project/libs/* +libs* +.idea* /*.h /*.cxx *.o diff --git a/CMakeLists.txt b/CMakeLists.txt index 376c54ac..93ccd98f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,9 +33,10 @@ add_definitions(-D"VERSION=\\"${VERSION}\\"") if(ANDROID) + include_directories($ENV{ANDROID_NDK}/sources/android/native_app_glue) set(executable_SOURCES - android/android.cpp - src/main.cpp + "android/android.cpp" + "$ENV{ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c" ) # add shared library for android diff --git a/android/android.cpp b/android/android.cpp index 52518cbb..57d58cb4 100644 --- a/android/android.cpp +++ b/android/android.cpp @@ -1,10 +1,35 @@ -#include -#include +/* + * Copyright (c) 2010-2014 OTClient + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include void android_main( struct android_app* state ) { - int argc = 0; + int argc = 1; const char* argv[1]; - argv[0] = NULL; + argv[0] = "NULL"; - main(argc, argv); + Client client( argc, argv ); + client.terminateAndFreeMemory(); + + ANativeActivity_finish(state->activity); } diff --git a/android/compile_android.sh b/android/compile_android.sh index 18d570b6..dcdca2ef 100644 --- a/android/compile_android.sh +++ b/android/compile_android.sh @@ -1,7 +1,19 @@ #!/bin/sh mkdir ../build && cd ../build -cmake -DCMAKE_TOOLCHAIN_FILE=../android/android.toolchain.cmake -DANDROID_ABI=armeabi -DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.6 .. +cmake -DCMAKE_TOOLCHAIN_FILE=../android/android.toolchain.cmake -DANDROID_ABI=armeabi-v7a -DANDROID_NATIVE_API_LEVEL=android-16 -DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.6 .. make -cd ../ && rm -r build && mkdir build && cd build +cd ../ && rm -r build + +cp -r libs android/project/ && rm -r libs + +cd android/project + +android update project -p . --name OTClientMob --target android-16 + +ant debug + +cd bin + +adb install -r OTClientMob-debug.apk diff --git a/src/client/client.cpp b/src/client/client.cpp index 2159dcda..b02e22a4 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -21,29 +21,38 @@ */ #include "client.h" -#include -#include -#include #include "game.h" #include "map.h" #include "shadermanager.h" #include "spritemanager.h" #include "minimap.h" +#include +#include +#include +#include #include +#include -Client g_client; +Client::Client(int argc, const char* argv[]) { + std::vector args(argv, argv + argc); + initAppFrameworkAndOTClient(args); +} -void Client::init(std::vector& args) +void Client::initAppFrameworkAndOTClient(std::vector& args) { - // register needed lua functions - registerLuaFunctions(); + setupAppNameAndVersion(); + g_app.init(args); g_map.init(); g_minimap.init(); g_game.init(); g_shaders.init(); g_things.init(); + registerLuaFunctions(); + findLuaInitScript(); + + g_app.runAppMainLoop(); //TODO: restore options /* if(g_graphics.parseOption(arg)) @@ -81,8 +90,28 @@ void Client::init(std::vector& args) */ } -void Client::terminate() +void Client::setupAppNameAndVersion() { + g_app.setName("OTClient"); + g_app.setCompactName("otclient"); + g_app.setVersion(VERSION); +} + +void Client::findLuaInitScript() { + if(!g_resources.discoverWorkDir("init.lua")) + g_logger.fatal("Unable to find work directory, the application cannot be initialized."); + else + runLuaInitScript(); +} + +void Client::runLuaInitScript() { + if(!g_lua.safeRunScript("init.lua")) + g_logger.fatal("Unable to run script init.lua!"); +} + +void Client::terminateAndFreeMemory() { + g_app.unloadModules(); + g_creatures.terminate(); g_game.terminate(); g_map.terminate(); @@ -90,4 +119,5 @@ void Client::terminate() g_things.terminate(); g_sprites.terminate(); g_shaders.terminate(); + g_app.terminate(); } diff --git a/src/client/client.h b/src/client/client.h index 22fca2bd..b010dc66 100644 --- a/src/client/client.h +++ b/src/client/client.h @@ -27,12 +27,14 @@ class Client { -public: - void init(std::vector& args); - void terminate(); + void initAppFrameworkAndOTClient(std::vector& args); + void setupAppNameAndVersion(); + void findLuaInitScript(); + void runLuaInitScript(); void registerLuaFunctions(); +public: + Client(int argc, const char* argv[]); + void terminateAndFreeMemory(); }; -extern Client g_client; - #endif diff --git a/src/framework/CMakeLists.txt b/src/framework/CMakeLists.txt index 476a8b0c..aa555466 100644 --- a/src/framework/CMakeLists.txt +++ b/src/framework/CMakeLists.txt @@ -126,10 +126,10 @@ set(framework_SOURCES ${framework_SOURCES} # crash handler ${CMAKE_CURRENT_LIST_DIR}/platform/crashhandler.h - #${CMAKE_CURRENT_LIST_DIR}/platform/unixcrashhandler.cpp + ${CMAKE_CURRENT_LIST_DIR}/platform/unixcrashhandler.cpp ${CMAKE_CURRENT_LIST_DIR}/platform/win32crashhandler.cpp ${CMAKE_CURRENT_LIST_DIR}/platform/win32platform.cpp - #${CMAKE_CURRENT_LIST_DIR}/platform/unixplatform.cpp + ${CMAKE_CURRENT_LIST_DIR}/platform/unixplatform.cpp ${CMAKE_CURRENT_LIST_DIR}/platform/platform.cpp ${CMAKE_CURRENT_LIST_DIR}/platform/platform.h ) @@ -140,7 +140,11 @@ set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/luafunctions.cpp # some build options option(LUAJIT "Use lua jit" OFF) if(NOT APPLE) - option(CRASH_HANDLER "Generate crash reports" ON) + if(ANDROID) + set(CRASH_HANDLER OFF) + else() + option(CRASH_HANDLER "Generate crash reports" ON) + endif() option(USE_STATIC_LIBS "Don't use shared libraries (dlls)" ON) option(USE_LIBCPP "Use the new libc++ library instead of stdc++" OFF) option(USE_LTO "Use link time optimizations" OFF) @@ -206,12 +210,7 @@ if(WIN32) endif() set(Boost_USE_MULTITHREADED ON) set(Boost_USE_STATIC_LIBS ${USE_STATIC_LIBS}) - -if(ANDROID) - -else() - find_package(Boost 1.48.0 COMPONENTS ${REQUIRED_BOOST_COMPONENTS} REQUIRED) -endif() +find_package(Boost 1.48.0 COMPONENTS ${REQUIRED_BOOST_COMPONENTS} REQUIRED) #find lua if(LUAJIT) @@ -230,11 +229,10 @@ message(STATUS "LuaJIT: " ${LUAJIT}) find_package(PhysFS REQUIRED) find_package(OpenSSL REQUIRED) -# android already has zlib if(NOT ANDROID) find_package(ZLIB REQUIRED) else() - set(framework_LIBRARIES ${framework_LIBRARIES} android) + set(framework_LIBRARIES ${framework_LIBRARIES} z android log) endif() set(framework_LIBRARIES ${framework_LIBRARIES} @@ -292,7 +290,11 @@ else() else() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -rdynamic -Wl,-rpath,./libs") # rdynamic is needed by backtrace.h used in crash handler - set(SYSTEM_LIBRARIES dl rt) + if(ANDROID) + set(SYSTEM_LIBRARIES dl) + else() + set(SYSTEM_LIBRARIES dl rt) + endif() endif() endif() set(framework_LIBRARIES ${framework_LIBRARIES} ${SYSTEM_LIBRARIES}) @@ -303,7 +305,7 @@ endif() if(FRAMEWORK_GRAPHICS) set(OPENGLES "OFF" CACHE "Use OpenGL ES 1.0 or 2.0 (for mobiles devices)" STRING) - if(OPENGLES STREQUAL "2.0" OR ANDROID_NATIVE_API_LEVEL VERSION_GREATER 7) + if(OPENGLES STREQUAL "2.0" OR (ANDROID_NATIVE_API_LEVEL AND ANDROID_NATIVE_API_LEVEL VERSION_GREATER 7)) if(NOT ANDROID) find_package(OpenGLES2 REQUIRED) find_package(EGL REQUIRED) @@ -313,7 +315,7 @@ if(FRAMEWORK_GRAPHICS) set(framework_LIBRARIES ${framework_LIBRARIES} GLESv2) endif() set(framework_DEFINITIONS ${framework_DEFINITIONS} -DOPENGL_ES=2) - ELSEif(OPENGLES STREQUAL "1.0" OR ANDROID_NATIVE_API_LEVEL VERSION_LESS 8) + ELSEif(OPENGLES STREQUAL "1.0" OR (ANDROID_NATIVE_API_LEVEL AND ANDROID_NATIVE_API_LEVEL VERSION_LESS 8)) if(NOT ANDROID) find_package(OpenGLES1 REQUIRED) find_package(EGL REQUIRED) @@ -460,8 +462,8 @@ if(FRAMEWORK_GRAPHICS) ${CMAKE_CURRENT_LIST_DIR}/platform/win32window.h ${CMAKE_CURRENT_LIST_DIR}/platform/x11window.cpp ${CMAKE_CURRENT_LIST_DIR}/platform/x11window.h - ${CMAKE_CURRENT_LIST_DIR}/platform/sdlplatform.cpp - ${CMAKE_CURRENT_LIST_DIR}/platform/sdlplatform.h + ${CMAKE_CURRENT_LIST_DIR}/platform/sdlwindow.cpp + ${CMAKE_CURRENT_LIST_DIR}/platform/sdlwindow.h # window input ${CMAKE_CURRENT_LIST_DIR}/input/mouse.cpp diff --git a/src/framework/core/application.cpp b/src/framework/core/application.cpp index ae07adbb..d54dec1e 100644 --- a/src/framework/core/application.cpp +++ b/src/framework/core/application.cpp @@ -101,7 +101,7 @@ void Application::init(std::vector& args) registerLuaFunctions(); } -void Application::deinit() +void Application::unloadModules() { g_lua.callGlobalField("g_app", "onTerminate"); diff --git a/src/framework/core/application.h b/src/framework/core/application.h index 7bb5473b..9b9ebc32 100644 --- a/src/framework/core/application.h +++ b/src/framework/core/application.h @@ -34,9 +34,9 @@ public: virtual ~Application() {} virtual void init(std::vector& args); - virtual void deinit(); + virtual void unloadModules(); virtual void terminate(); - virtual void run() = 0; + virtual void runAppMainLoop() = 0; virtual void poll(); virtual void exit(); virtual void close(); diff --git a/src/framework/core/consoleapplication.cpp b/src/framework/core/consoleapplication.cpp index a40b0b55..53f998f3 100644 --- a/src/framework/core/consoleapplication.cpp +++ b/src/framework/core/consoleapplication.cpp @@ -31,7 +31,7 @@ ConsoleApplication g_app; -void ConsoleApplication::run() +void ConsoleApplication::runAppMainLoop() { m_running = true; diff --git a/src/framework/core/consoleapplication.h b/src/framework/core/consoleapplication.h index 38092b3b..f8def290 100644 --- a/src/framework/core/consoleapplication.h +++ b/src/framework/core/consoleapplication.h @@ -29,7 +29,7 @@ class ConsoleApplication : public Application { public: - void run(); + void runAppMainLoop(); int getFps() { return m_frameCounter.getLastFps(); } diff --git a/src/framework/core/graphicalapplication.cpp b/src/framework/core/graphicalapplication.cpp index f77469cc..0ab29338 100644 --- a/src/framework/core/graphicalapplication.cpp +++ b/src/framework/core/graphicalapplication.cpp @@ -66,12 +66,12 @@ void GraphicalApplication::init(std::vector& args) #endif } -void GraphicalApplication::deinit() +void GraphicalApplication::unloadModules() { // hide the window because there is no render anymore g_window.hide(); - Application::deinit(); + Application::unloadModules(); } void GraphicalApplication::terminate() @@ -101,7 +101,7 @@ void GraphicalApplication::terminate() m_terminated = true; } -void GraphicalApplication::run() +void GraphicalApplication::runAppMainLoop() { m_running = true; diff --git a/src/framework/core/graphicalapplication.h b/src/framework/core/graphicalapplication.h index 1e8ca88c..f32fc86f 100644 --- a/src/framework/core/graphicalapplication.h +++ b/src/framework/core/graphicalapplication.h @@ -36,9 +36,9 @@ class GraphicalApplication : public Application public: void init(std::vector& args); - void deinit(); + void unloadModules(); void terminate(); - void run(); + void runAppMainLoop(); void poll(); void close(); diff --git a/src/framework/platform/platformwindow.cpp b/src/framework/platform/platformwindow.cpp index 7baab121..0918c867 100644 --- a/src/framework/platform/platformwindow.cpp +++ b/src/framework/platform/platformwindow.cpp @@ -26,8 +26,8 @@ #include "win32window.h" WIN32Window window; #elif defined ANDROID -#include "sdlplatform.h" -SDLPlatform window; +#include "sdlwindow.h" +SDLWindow window; #else #include "x11window.h" #include diff --git a/src/framework/platform/sdlplatform.cpp b/src/framework/platform/sdlplatform.cpp deleted file mode 100644 index 39265c15..00000000 --- a/src/framework/platform/sdlplatform.cpp +++ /dev/null @@ -1,75 +0,0 @@ -#include "sdlplatform.h" - -void SDLPlatform::init() { -} - -void SDLPlatform::terminate() { -} - -void SDLPlatform::move(const Point& pos) { -} - -void SDLPlatform::resize(const Size& size) { -} - -void SDLPlatform::show() { -} - -void SDLPlatform::hide() { -} - -void SDLPlatform::maximize() { -} - -void SDLPlatform::poll() { -} - -void SDLPlatform::swapBuffers() { -} - -void SDLPlatform::showMouse() { -} - -void SDLPlatform::hideMouse() { -} - -int SDLPlatform::internalLoadMouseCursor(const ImagePtr& image, const Point& hotSpot) { - return 0; -} - -void SDLPlatform::setMouseCursor(int cursorId) { -} - -void SDLPlatform::restoreMouseCursor() { -} - -void SDLPlatform::setTitle(const std::string& title) { -} - -void SDLPlatform::setMinimumSize(const Size& minimumSize) { -} - -void SDLPlatform::setFullscreen(bool fullscreen) { -} - -void SDLPlatform::setVerticalSync(bool enable) { -} - -void SDLPlatform::setIcon(const std::string& iconFile) { -} - -void SDLPlatform::setClipboardText(const std::string& text) { -} - -Size SDLPlatform::getDisplaySize() { - Size TODO; - return TODO; -} - -std::string SDLPlatform::getClipboardText() { - return nullptr; -} - -std::string SDLPlatform::getPlatformType() { - return nullptr; -} diff --git a/src/framework/platform/sdlplatform.h b/src/framework/platform/sdlplatform.h deleted file mode 100644 index 18f9bf35..00000000 --- a/src/framework/platform/sdlplatform.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef SDL_PLATFORM_H -#define SDL_PLATFORM_H - -#include "platformwindow.h" - -class SDLPlatform : public PlatformWindow -{ -public: - void init(); - void terminate(); - - void move(const Point& pos); - void resize(const Size& size); - void show(); - void hide(); - void maximize(); - void poll(); - void swapBuffers(); - void showMouse(); - void hideMouse(); - - void setMouseCursor(int cursorId); - void restoreMouseCursor(); - - void setTitle(const std::string& title); - void setMinimumSize(const Size& minimumSize); - void setFullscreen(bool fullscreen); - void setVerticalSync(bool enable); - void setIcon(const std::string& iconFile); - void setClipboardText(const std::string& text); - - Size getDisplaySize(); - std::string getClipboardText(); - std::string getPlatformType(); -protected: - int internalLoadMouseCursor(const ImagePtr& image, const Point& hotSpot); -}; - -#endif diff --git a/src/framework/platform/sdlwindow.cpp b/src/framework/platform/sdlwindow.cpp index 3559948a..3d43ed56 100644 --- a/src/framework/platform/sdlwindow.cpp +++ b/src/framework/platform/sdlwindow.cpp @@ -1,5 +1,12 @@ #include "sdlwindow.h" +SDLWindow::SDLWindow() { + m_window = 0; + m_cursor = 0; + m_minimumSize = Size(600,480); + m_size = Size(600,480); +} + void SDLWindow::init() { } @@ -83,7 +90,7 @@ Size SDLWindow::getDisplaySize() { } std::string SDLWindow::getClipboardText() { - return nullptr; + return ""; } std::string SDLWindow::getPlatformType() { diff --git a/src/framework/platform/unixcrashhandler.cpp b/src/framework/platform/unixcrashhandler.cpp index baa5031c..f2b53515 100644 --- a/src/framework/platform/unixcrashhandler.cpp +++ b/src/framework/platform/unixcrashhandler.cpp @@ -20,7 +20,7 @@ * THE SOFTWARE. */ -#if !defined(WIN32) && defined(CRASH_HANDLER) +#if !defined(WIN32) && !defined(ANDROID) && defined(CRASH_HANDLER) #include "crashhandler.h" #include diff --git a/src/framework/platform/unixplatform.cpp b/src/framework/platform/unixplatform.cpp index fcb8bd0b..3e30ebf8 100644 --- a/src/framework/platform/unixplatform.cpp +++ b/src/framework/platform/unixplatform.cpp @@ -29,7 +29,12 @@ #include #include + +#ifdef ANDROID +#include +#else #include +#endif void Platform::processArgs(std::vector& args) { @@ -169,6 +174,7 @@ std::string Platform::getOSName() return std::string(); } +#ifndef ANDROID std::string Platform::traceback(const std::string& where, int level, int maxDepth) { std::stringstream ss; @@ -199,5 +205,8 @@ std::string Platform::traceback(const std::string& where, int level, int maxDept return ss.str(); } +#else +std::string Platform::traceback(const std::string& where, int level, int maxDepth){ return "TODO"; } +#endif #endif diff --git a/src/main.cpp b/src/main.cpp index 3108e896..a7b3f80d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,39 +20,12 @@ * THE SOFTWARE. */ -#include -#include -#include #include int main(int argc, const char* argv[]) { - std::vector args(argv, argv + argc); + Client client(argc, argv); + client.terminateAndFreeMemory(); - // setup application name and version - g_app.setName("OTClient"); - g_app.setCompactName("otclient"); - g_app.setVersion(VERSION); - - // initialize application framework and otclient - g_app.init(args); - g_client.init(args); - - // find script init.lua and run it - if(!g_resources.discoverWorkDir("init.lua")) - g_logger.fatal("Unable to find work directory, the application cannot be initialized."); - - if(!g_lua.safeRunScript("init.lua")) - g_logger.fatal("Unable to run script init.lua!"); - - // the run application main loop - g_app.run(); - - // unload modules - g_app.deinit(); - - // terminate everything and free memory - g_client.terminate(); - g_app.terminate(); return 0; }