From ea051bb11f8b17db75cb1f9ef82794aac063b17c Mon Sep 17 00:00:00 2001 From: pelya Date: Tue, 21 Dec 2010 14:29:52 +0000 Subject: [PATCH] Added cout/cerr that output to Android log to STLPort, fixed broken compilation for NDK r4b --- .../ballfield/{ballfield.c => ballfield.cpp} | 4 + project/jni/application/src | 2 +- project/jni/stlport/src/iostream.cpp | 111 ++++++++++++++++++ project/jni/stlport/src/locale_impl.cpp | 2 + .../jni/stlport/stlport/stl/config/_android.h | 6 +- 5 files changed, 121 insertions(+), 4 deletions(-) rename project/jni/application/ballfield/{ballfield.c => ballfield.cpp} (98%) diff --git a/project/jni/application/ballfield/ballfield.c b/project/jni/application/ballfield/ballfield.cpp similarity index 98% rename from project/jni/application/ballfield/ballfield.c rename to project/jni/application/ballfield/ballfield.cpp index ee57f1c3c..b60836aac 100644 --- a/project/jni/application/ballfield/ballfield.c +++ b/project/jni/application/ballfield/ballfield.cpp @@ -16,9 +16,11 @@ #include "SDL.h" #include "SDL_image.h" +#include "test.h" #include "ballfield.h" + /*---------------------------------------------------------- General tool functions ----------------------------------------------------------*/ @@ -359,6 +361,8 @@ int main(int argc, char* argv[]) int fps_start = 0; float x_speed, y_speed, z_speed; + __android_log_print(ANDROID_LOG_INFO, "==TEST==", "SDL_Main: test::initCount %d test::initCount2", test::initCount, test::initCount2); + SDL_Init(SDL_INIT_VIDEO); atexit(SDL_Quit); diff --git a/project/jni/application/src b/project/jni/application/src index 104f796a6..59d41f41e 120000 --- a/project/jni/application/src +++ b/project/jni/application/src @@ -1 +1 @@ -ballfield \ No newline at end of file +fheroes2 \ No newline at end of file diff --git a/project/jni/stlport/src/iostream.cpp b/project/jni/stlport/src/iostream.cpp index f32b368a5..6ff92d0ca 100644 --- a/project/jni/stlport/src/iostream.cpp +++ b/project/jni/stlport/src/iostream.cpp @@ -57,6 +57,7 @@ using _STLP_VENDOR_CSTD::_streams; // the stream objects by calling the init() member function. #if defined (ANDROID_NO_COUT) +#include /* Outputting anything to cout/cerr WILL CRASH YOUR PROGRAM on specific devices - x5a/x6d Android 2.1 tablet, and some other tablets, however the same code runs on my HTC Evo without problem. @@ -64,20 +65,130 @@ using _STLP_VENDOR_CSTD::_streams; */ long ios_base::Init::_S_count = 0; +// by default, those are synced +bool ios_base::_S_was_synced = true; ios_base::Init::Init() { if (_S_count++ == 0) { _Locale_init(); + //ios_base::_S_initialize(); _Filebuf_base::_S_initialize(); } } ios_base::Init::~Init() { if (--_S_count == 0) { + //ios_base::_S_uninitialize(); _Locale_final(); } } +class _android_debugbuf: public streambuf +{ + public: + _android_debugbuf() + { + pos = 0; + buf[0] = 0; + } + + protected: + + +virtual int overflow(int c = EOF) +{ + if (EOF == c) + { + return '\0'; // returning EOF indicates an error + } + else + { + outputchar(c); + return c; + } +}; + + +// we don’t do input so always return EOF +virtual int uflow() {return EOF;} + +// we don’t do input so always return 0 chars read +virtual int xsgetn(char *, int) {return 0;} + +// Calls outputchar() for each character. +virtual int xsputn(const char *s, int n) +{ + for (int i = 0; i < n; ++i) + { + outputchar(s[i]); + } + return n;// we always process all of the chars +}; + +private: + +// the buffer +char buf[256]; +int pos; + +void outputchar(char c) +{ + // TODO: mutex + if( pos >= sizeof(buf)-1 || c == '\n' || c == '\r' || c == 0 ) + { + buf[pos] = 0; + __android_log_print(ANDROID_LOG_INFO, "libSDL", "%s", buf); + pos = 0; + }; + buf[pos] = c; + pos++; +}; + +}; + +class debugbufinit +{ + static unsigned int count; + public: + debugbufinit(); + + // Our destructor is not virtual. It is important that objects of this class have no + // memory footprint. We will end up with one object of this class per translation + // unit (.cp file). If this class has any virtual member functions then objects of + // this class would have v tables in memory. Since this is not intended to be a + // base class for other class, there is no need to be virtual. + + ~debugbufinit(); +}; + +static filebuf* +_Stl_create_filebuf(int f, ios_base::openmode mode ) { + basic_filebuf >* result = + new basic_filebuf >(); + + _STLP_TRY { + result->_M_open(f, mode); + } + _STLP_CATCH_ALL {} + + if (!result->is_open()) { + delete result; + result = 0; + } + return result; +} + +static ios_base::Init _IosInit; + +bool _STLP_CALL ios_base::sync_with_stdio(bool sync) +{ +} + +_STLP_DECLSPEC istream cin(_Stl_create_filebuf(0, ios_base::in)); +_STLP_DECLSPEC ostream cout(new _android_debugbuf()); +_STLP_DECLSPEC ostream cerr(new _android_debugbuf()); +_STLP_DECLSPEC ostream clog(new _android_debugbuf()); + #else #if defined (_STLP_USE_NOT_INIT_SEGMENT) diff --git a/project/jni/stlport/src/locale_impl.cpp b/project/jni/stlport/src/locale_impl.cpp index a3495cb35..edb614ed2 100644 --- a/project/jni/stlport/src/locale_impl.cpp +++ b/project/jni/stlport/src/locale_impl.cpp @@ -579,7 +579,9 @@ static locale* _Stl_get_global_locale() { # pragma init_seg(lib) #endif +#if !defined(ANDROID) && !defined(__ANDROID__) static ios_base::Init _IosInit; +#endif void _Locale_impl::make_classic_locale() { // This funcion will be called once: during build classic _Locale_impl diff --git a/project/jni/stlport/stlport/stl/config/_android.h b/project/jni/stlport/stlport/stl/config/_android.h index 81a4db932..6acb89162 100644 --- a/project/jni/stlport/stlport/stl/config/_android.h +++ b/project/jni/stlport/stlport/stl/config/_android.h @@ -70,15 +70,15 @@ #if defined(__ANDROID__) /* NDK r5 */ # define _STLP_NATIVE_CPP_C_INCLUDE_PATH ../../../cxx-stl/system/include # define _STLP_NATIVE_CPP_RUNTIME_INCLUDE_PATH ../../../cxx-stl/system/include +#else /* NDK r4b */ +# define _STLP_NATIVE_CPP_C_INCLUDE_PATH ../../usr/include +# define _STLP_NATIVE_CPP_RUNTIME_INCLUDE_PATH ../../usr/include #endif #ifdef __cplusplus // Hack to prevent including buggy stl_pair.h system header, introduced in Android 1.6 NDK #define _CPP_UTILITY 1 #define __SGI_STL_INTERNAL_PAIR_H 1 -//#include -//inline void* operator new(size_t, void* p) { return p; } -//inline void* operator new[](size_t, void* p) { return p; } #endif #endif /* __stl_config__android_h */