From e6ee88af438e85d8ec2225b2a92dd0f858708331 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Thu, 7 Mar 2013 04:50:45 -0300 Subject: [PATCH] Changes to compile on android --- init.lua | 2 +- modules/client/client.lua | 7 ++++-- otclientrc.lua | 1 - src/framework/cmake/FindSDL2.cmake | 6 +++--- src/framework/core/application.cpp | 9 ++++++-- src/framework/core/graphicalapplication.cpp | 1 + src/framework/core/logger.cpp | 9 ++++++++ src/framework/core/resourcemanager.cpp | 15 ++++++++++++- src/framework/graphics/apngloader.cpp | 18 ++++++++-------- src/framework/graphics/graphicscontext.h | 10 ++++----- src/framework/graphics/ogl/painterogl.cpp | 4 +++- src/framework/input/mouse.h | 5 +++++ src/framework/platform/sdlwindow.cpp | 24 ++++++++++++++++++++- src/framework/platform/sdlwindow.h | 2 +- src/framework/platform/unixplatform.cpp | 2 +- src/framework/stdext/compiler.h | 4 ---- src/main.cpp | 6 +++++- 17 files changed, 92 insertions(+), 33 deletions(-) diff --git a/init.lua b/init.lua index 59798fda..5ef3a990 100644 --- a/init.lua +++ b/init.lua @@ -48,7 +48,7 @@ g_modules.ensureModuleLoaded("game_interface") -- mods 1000-9999 g_modules.autoLoadModules(9999) -local script = '/' .. g_app.getCompactName() .. 'rc' +local script = '/' .. g_app.getCompactName() .. 'rc.lua' if g_resources.fileExists(script) then dofile(script) diff --git a/modules/client/client.lua b/modules/client/client.lua index 436a6f5a..3f8d0cdb 100644 --- a/modules/client/client.lua +++ b/modules/client/client.lua @@ -1,5 +1,5 @@ local musicFilename = "/sounds/startup" -local musicChannel = g_sounds.getChannel(1) +local musicChannel = nil function setMusic(filename) musicFilename = filename @@ -57,11 +57,14 @@ function init() onExit = exit }) g_window.setMinimumSize({ width = 600, height = 480 }) + + musicChannel = g_sounds.getChannel(1) g_sounds.preload(musicFilename) -- initialize in fullscreen mode on mobile devices - if g_window.getPlatformType() == "X11-EGL" then + if g_app.getOs() == "android" then g_window.setFullscreen(true) + g_window.maximize() else -- window size local size = { width = 800, height = 600 } diff --git a/otclientrc.lua b/otclientrc.lua index b3a88ef7..b8b0805b 100644 --- a/otclientrc.lua +++ b/otclientrc.lua @@ -2,4 +2,3 @@ -- you can place any custom user code here print 'Startup done :]' - diff --git a/src/framework/cmake/FindSDL2.cmake b/src/framework/cmake/FindSDL2.cmake index f44363e6..7e4aaca4 100644 --- a/src/framework/cmake/FindSDL2.cmake +++ b/src/framework/cmake/FindSDL2.cmake @@ -3,9 +3,9 @@ # SDL2_INCLUDE_DIR - the SDL2 include directory # SDL2_LIBRARY - the SDL2 library -FIND_PATH(SDL2_INCLUDE_DIR NAMES SDL2/SDL.h) -SET(_SDL2_STATIC_LIBS libSDL2.a) -SET(_SDL2_SHARED_LIBS libSDL2.dll.a SDL2) +FIND_PATH(SDL2_INCLUDE_DIR PATH_SUFFIXES SDL2 SDL NAMES SDL.h) +SET(_SDL2_STATIC_LIBS libSDL2.a libSDL.a) +SET(_SDL2_SHARED_LIBS libSDL2.dll.a SDL2 SDL libSDL.dll.a SDL) IF(USE_STATIC_LIBS) FIND_LIBRARY(SDL2_LIBRARY NAMES ${_SDL2_STATIC_LIBS} ${_SDL2_SHARED_LIBS}) ELSE() diff --git a/src/framework/core/application.cpp b/src/framework/core/application.cpp index 0a643e04..8477a893 100644 --- a/src/framework/core/application.cpp +++ b/src/framework/core/application.cpp @@ -33,6 +33,7 @@ #include #include +#include #ifdef FW_NET #include @@ -163,10 +164,14 @@ void Application::close() std::string Application::getOs() { -#if defined(WIN32) +#if defined(ANDROID) + return "android"; +#elif defined(IOS) + return "ios"; +#elif defined(WIN32) return "windows"; #elif defined(__APPLE__) - return "mac"; + return "macos"; #elif __linux return "linux"; #else diff --git a/src/framework/core/graphicalapplication.cpp b/src/framework/core/graphicalapplication.cpp index e917a817..0b218003 100644 --- a/src/framework/core/graphicalapplication.cpp +++ b/src/framework/core/graphicalapplication.cpp @@ -24,6 +24,7 @@ #include "graphicalapplication.h" #include #include +#include #include #include #include diff --git a/src/framework/core/logger.cpp b/src/framework/core/logger.cpp index f67a6cc4..63603875 100644 --- a/src/framework/core/logger.cpp +++ b/src/framework/core/logger.cpp @@ -25,12 +25,17 @@ //#include #include +#include "application.h" #ifdef FW_GRAPHICS #include #include #endif +#ifdef MOBILE +#include +#endif + Logger g_logger; void Logger::log(Fw::LogLevel level, const std::string& message) @@ -66,7 +71,11 @@ void Logger::log(Fw::LogLevel level, const std::string& message) #endif */ +#ifdef ANDROID + __android_log_print(ANDROID_LOG_INFO, g_app.getCompactName().c_str(), outmsg.c_str()); +#else std::cout << outmsg << std::endl; +#endif if(m_outFile.good()) { m_outFile << outmsg << std::endl; diff --git a/src/framework/core/resourcemanager.cpp b/src/framework/core/resourcemanager.cpp index 60a2d300..a59648e2 100644 --- a/src/framework/core/resourcemanager.cpp +++ b/src/framework/core/resourcemanager.cpp @@ -29,6 +29,10 @@ #include +#ifdef MOBILE +#include +#endif + ResourceManager g_resources; void ResourceManager::init(const char *argv0) @@ -45,10 +49,15 @@ void ResourceManager::terminate() bool ResourceManager::discoverWorkDir(const std::string& existentFile) { // search for modules directory - std::string possiblePaths[] = { g_platform.getCurrentDir(), +#ifdef ANDROID + std::string possiblePaths[] = { std::string("/sdcard/") + g_app.getCompactName() + "/" }; +#else + std::string possiblePaths[] = { "./", + g_platform.getCurrentDir(), g_resources.getBaseDir(), g_resources.getBaseDir() + "../", g_resources.getBaseDir() + "../share/" + g_app.getCompactName() + "/" }; +#endif bool found = false; for(const std::string& dir : possiblePaths) { @@ -309,7 +318,11 @@ std::string ResourceManager::getBaseDir() std::string ResourceManager::getUserDir() { +#ifdef ANDROID + return std::string("/sdcard/"); +#else return PHYSFS_getUserDir(); +#endif } std::string ResourceManager::guessFilePath(const std::string& filename, const std::string& type) diff --git a/src/framework/graphics/apngloader.cpp b/src/framework/graphics/apngloader.cpp index fff7d642..5712c04a 100644 --- a/src/framework/graphics/apngloader.cpp +++ b/src/framework/graphics/apngloader.cpp @@ -33,15 +33,15 @@ #include #if defined(_MSC_VER) && _MSC_VER >= 1300 -#define swap16(data) _byteswap_ushort(data) -#define swap32(data) _byteswap_ulong(data) +#define lswap16(data) _byteswap_ushort(data) +#define lswap32(data) _byteswap_ulong(data) #elif __linux__ #include -#define swap16(data) bswap_16(data) -#define swap32(data) bswap_32(data) +#define lswap16(data) bswap_16(data) +#define lswap32(data) bswap_32(data) #else -#define swap16(data) (((data >> 8) & 255) | ((data & 255) << 8)) -#define swap32(data) ((swap16(data) << 16) | swap16(data >> 16)) +#define lswap16(data) (((data >> 8) & 255) | ((data & 255) << 8)) +#define lswap32(data) ((swap16(data) << 16) | swap16(data >> 16)) #endif #define PNG_ZBUF_SIZE 32768 @@ -865,7 +865,7 @@ int load_apng(std::stringstream& file, struct apng_data *apng) void write_chunk(std::ostream& f, const char* name, unsigned char* data, unsigned int length) { unsigned int crc = crc32(0, Z_NULL, 0); - unsigned int len = swap32(length); + unsigned int len = lswap32(length); f.write((char*)&len, 4); f.write(name, 4); @@ -876,7 +876,7 @@ void write_chunk(std::ostream& f, const char* name, unsigned char* data, unsigne crc = crc32(crc, data, length); } - crc = swap32(crc); + crc = lswap32(crc); f.write((char*)&crc, 4); } @@ -937,7 +937,7 @@ void save_png(std::stringstream& f, unsigned int width, unsigned int height, int unsigned char mCompression; unsigned char mFilterMethod; unsigned char mInterlaceMethod; - } ihdr = { swap32(width), swap32(height), 8, coltype, 0, 0, 0 }; + } ihdr = { lswap32(width), lswap32(height), 8, coltype, 0, 0, 0 }; z_stream zstream1; z_stream zstream2; diff --git a/src/framework/graphics/graphicscontext.h b/src/framework/graphics/graphicscontext.h index 23d36582..8376aca1 100644 --- a/src/framework/graphics/graphicscontext.h +++ b/src/framework/graphics/graphicscontext.h @@ -33,13 +33,13 @@ public: std::string getName() { return m_name; } - virtual void create() = 0; - virtual void destroy() = 0; - virtual void restore() = 0; + virtual void create() {} + virtual void destroy() {} + virtual void restore() {} - virtual void swapBuffers() = 0; + virtual void swapBuffers() {} - virtual void setVerticalSync(bool enable) = 0; + virtual void setVerticalSync(bool enable) {} protected: std::string m_name; diff --git a/src/framework/graphics/ogl/painterogl.cpp b/src/framework/graphics/ogl/painterogl.cpp index 17f1751e..9c2442a7 100644 --- a/src/framework/graphics/ogl/painterogl.cpp +++ b/src/framework/graphics/ogl/painterogl.cpp @@ -34,7 +34,9 @@ PainterOGL::PainterOGL() { -#ifdef OPENGL_ES +#ifdef SDL + m_graphicsContext = GraphicsContextPtr(new GraphicsContext("null")); +#elif OPENGL_ES m_graphicsContext = GraphicsContextPtr(new GraphicsContextEGL); #elif WIN32 m_graphicsContext = GraphicsContextPtr(new GraphicsContextWGL); diff --git a/src/framework/input/mouse.h b/src/framework/input/mouse.h index 3af0bd8f..ba1c89e2 100644 --- a/src/framework/input/mouse.h +++ b/src/framework/input/mouse.h @@ -20,6 +20,9 @@ * THE SOFTWARE. */ +#ifndef MOUSE_H +#define MOUSE_H + #include class Mouse @@ -43,3 +46,5 @@ private: }; extern Mouse g_mouse; + +#endif diff --git a/src/framework/platform/sdlwindow.cpp b/src/framework/platform/sdlwindow.cpp index 90f9b277..ed453661 100644 --- a/src/framework/platform/sdlwindow.cpp +++ b/src/framework/platform/sdlwindow.cpp @@ -36,14 +36,34 @@ void SDLWindow::init() { SDL_Init(SDL_INIT_VIDEO); + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 4); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 4); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 4); + SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 4); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0); + +#ifdef OPENGL_ES + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, OPENGL_ES); +#endif + +#ifdef MOBILE + int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_SHOWN; +#else + int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN; +#endif + m_window = SDL_CreateWindow(g_app.getName().c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, m_size.width(), m_size.height(), - SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN); + flags); if(!m_window) g_logger.fatal("Unable to create SDL window"); + int w, h; + SDL_GetWindowSize(m_window, &w, &h); + m_size = Size(w,h); + m_context = SDL_GL_CreateContext(m_window); if(!m_context) g_logger.fatal("Unable to create SDL GL context"); @@ -108,6 +128,7 @@ void SDLWindow::poll() m_position = Point(event.window.data1, event.window.data2); break; case SDL_WINDOWEVENT_RESIZED: + g_logger.info(stdext::format("resize %d %d", event.window.data1, event.window.data2)); m_size = Size(event.window.data1, event.window.data2); if(m_onResize) m_onResize(m_size); @@ -147,6 +168,7 @@ void SDLWindow::poll() case SDL_QUIT: if(m_onClose) m_onClose(); + break; } } diff --git a/src/framework/platform/sdlwindow.h b/src/framework/platform/sdlwindow.h index 9def1bb9..aae00135 100644 --- a/src/framework/platform/sdlwindow.h +++ b/src/framework/platform/sdlwindow.h @@ -24,7 +24,7 @@ #define SDLWINDOW_H #include "platformwindow.h" -#include +#include #include class SDLWindow : public PlatformWindow diff --git a/src/framework/platform/unixplatform.cpp b/src/framework/platform/unixplatform.cpp index 02761e52..c27f6c9d 100644 --- a/src/framework/platform/unixplatform.cpp +++ b/src/framework/platform/unixplatform.cpp @@ -36,7 +36,7 @@ void Platform::processArgs(std::vector& args) bool Platform::spawnProcess(std::string process, const std::vector& args) { struct stat sts; - if(stat(process.c_str(), &sts) == -1 && errno == ENOENT) + if(stat(process.c_str(), &sts) == -1) return false; pid_t pid = fork(); diff --git a/src/framework/stdext/compiler.h b/src/framework/stdext/compiler.h index 8139ff24..dd452a43 100644 --- a/src/framework/stdext/compiler.h +++ b/src/framework/stdext/compiler.h @@ -33,8 +33,4 @@ #error "Compiler not supported." #endif -#if !defined(__GXX_EXPERIMENTAL_CXX0X__) -#error "Sorry, you must enable C++0x to compile." -#endif - #endif diff --git a/src/main.cpp b/src/main.cpp index 091aa6a7..00a524f3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,7 +25,11 @@ #include #include -int main(int argc, const char* argv[]) +#ifdef SDL +#include +#endif + +int main(int argc, char** argv) { std::vector args(argv, argv + argc);