Skip to content
This repository
Browse code

SharedLib, pp::MessageLoop in File, System::bell()

- ozCore
  * SharedLib class added for loading shared libraries
  * PPAPI 25 non-main thread calls used to simplfy File class and System::bell()
- ozEngine
  * OpenGL.hh, OpenAL.hh added, Texture class stub
  • Loading branch information...
commit 9635f71d1b05cd2c742b29c554a53552d316fa95 1 parent a04b92f
Davorin Učakar authored

Showing 40 changed files with 1,074 additions and 579 deletions. Show diff stats Hide diff stats

  1. +3 3 CMakeLists.txt
  2. +10 3 autogen.sh
  3. +1 1  clean-blanks.sh
  4. +1 1  src/client/BuildInfo.cc.in
  5. +1 1  src/client/CMakeLists.txt
  6. +1 1  src/client/Client.cc
  7. +1 1  src/client/NaClUpdater.cc
  8. +1 1  src/client/OpenGL.hh
  9. +18 19 src/client/Render.cc
  10. +8 20 src/client/Sound.cc
  11. +3 3 src/client/Sound.hh
  12. +3 3 src/client/common.hh
  13. +1 1  src/client/config.hh.in
  14. +1 1  src/nirvana/common.hh
  15. +5 3 src/ozCore/CMakeLists.txt
  16. +62 336 src/ozCore/File.cc
  17. +6 11 src/ozCore/File.hh
  18. +100 0 src/ozCore/SharedLib.cc
  19. +118 0 src/ozCore/SharedLib.hh
  20. +38 74 src/ozCore/System.cc
  21. +15 13 src/ozCore/Thread.cc
  22. +2 1  src/ozCore/ozCore.hh
  23. +1 0  src/ozDynamics/CMakeLists.txt
  24. +10 7 src/ozDynamics/collision/Body.hh
  25. +2 2 src/ozDynamics/collision/Collider.cc
  26. +6 6 src/ozDynamics/physics/Physics.cc
  27. +7 0 src/ozEngine/CMakeLists.txt
  28. +74 0 src/ozEngine/OpenAL.cc
  29. +45 0 src/ozEngine/OpenAL.hh
  30. +229 0 src/ozEngine/OpenGL.cc
  31. +137 0 src/ozEngine/OpenGL.hh
  32. +53 0 src/ozEngine/Texture.cc
  33. +104 0 src/ozEngine/Texture.hh
  34. +1 0  src/ozEngine/common.hh
  35. +3 0  src/ozEngine/config.hh.in
  36. +0 6 src/tests/CMakeLists.txt
  37. +0 54 src/tests/collada.cc
  38. +1 0  src/tests/test.cc
  39. +2 3 src/tools/openzone.cc
  40. +0 4 src/tools/ozGettext.cc
6 CMakeLists.txt
@@ -11,11 +11,11 @@ set( OZ_VERSION 0.3.81 )
11 11 option( OZ_TRACK_ALLOCS "Track memory allocations to detect new/delete mismatches and memory leaks." OFF )
12 12 option( OZ_SIMD_MATH "Use SIMD implementation of linear algebra classes." OFF )
13 13 option( OZ_DYNAMICS "Build complete ozDynamics library with ODE support." OFF )
  14 +option( OZ_GLES "Use OpenGL ES 2.0 instead of OpenGL 2.1." OFF )
14 15
15 16 option( OZ_LUAJIT "Use use LuaJIT 2.0 instead of official Lua." OFF )
16 17 option( OZ_SDL2 "Use SDL 2.0 instead of SDL 1.2." OFF )
17 18 option( OZ_NET "Enable networking support." OFF )
18   -option( OZ_GL_ES "Use OpenGL ES 2.0 instead of OpenGL 2.1." OFF )
19 19 option( OZ_NONFREE "Enable support for building S3TC textures." OFF )
20 20
21 21 option( OZ_STANDALONE "Install layout appropriate for launching from its own folder." OFF )
@@ -34,7 +34,7 @@ set( OZ_PLATFORM_NAME ${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR} )
34 34
35 35 if( PLATFORM_EMBEDDED )
36 36 set( OZ_PLATFORM_NAME ${PLATFORM_NAME}-${CMAKE_SYSTEM_PROCESSOR} )
37   - set( OZ_GL_ES ON )
  37 + set( OZ_GLES ON )
38 38 set( OZ_STANDALONE OFF )
39 39 endif()
40 40
@@ -105,7 +105,7 @@ else()
105 105 include_directories( SYSTEM ${SDL_INCLUDE_DIRS} )
106 106 endif()
107 107
108   -if( OZ_GL_ES )
  108 +if( OZ_GLES )
109 109 # On embedded platform GLES is part of system libraries and is guaranteed to exist in toolchain or
110 110 # platform directory, no need to explicitly check existence or to detect library names.
111 111 if( ANDROID )
13 autogen.sh
@@ -17,9 +17,16 @@ for component in ${components[@]}; do
17 17 echo "Generating src/$component/CMakeLists.txt"
18 18
19 19 cd src/$component
20   - # The sed statement removes uninstantiated *.hh, *.cc, */*.hh and */*.cc expressions and makes the
21   - # file list newline-separated and indented.
22   - sources=`echo *.{hh,cc} */*.{hh,cc} | sed -r 's| (\*/)?\*\...||g; s| |\\\\n |g'`
  20 +
  21 + sources=`echo *.{hh,cc} */*.{hh,cc}`
  22 + # Remove uninstantiated *.hh, *.cc, */*.hh and */*.cc expressions.
  23 + sources=`echo $sources | sed -r 's|(\*/)?\*\...||g'`
  24 + # Remove PCH trigger library.
  25 + sources=`echo $sources | sed -r 's|pch\.cc||g'`
  26 + # Remove duplicated spaces that may have been introduced by the previous removals.
  27 + sources=`echo $sources | sed -r 's| +| |g'`
  28 + # Make file list newline-separated and indented.
  29 + sources=`echo $sources | sed -r 's| |\\\\n |g'`
23 30
24 31 # Insert source file list between "#BEGIN SOURCES" and "#END SOURCES" tags in CMakeLists.txt.
25 32 sed -r '/^#BEGIN SOURCES$/,/^#END SOURCES$/ c\#BEGIN SOURCES\n '"$sources"'\n#END SOURCES' \
2  clean-blanks.sh
@@ -10,7 +10,7 @@
10 10 # each file, remove duplicated blank lines and then remove one blank line at the end of each file.
11 11 #
12 12
13   -files=`git ls-files | egrep -v '^doc/licences/|^include/|\.patch$|\.po$'`
  13 +files=`git ls-files | egrep -v '^doc/licences/|^include/|^etc/android-project/|\.patch$|\.po$'`
14 14
15 15 for file in $files; do
16 16 # Remove trailing blanks at the end of line.
2  src/client/BuildInfo.cc.in
@@ -47,6 +47,6 @@ const char* const BuildInfo::CONFIG = "OZ_TRACK_ALLOCS: @OZ_TRACK_ALLOCS@
47 47 "OZ_DYNAMICS: @OZ_DYNAMICS@, "
48 48 "OZ_LUAJIT: @OZ_LUAJIT@, "
49 49 "OZ_SDL2: @OZ_SDL2@, "
50   - "OZ_GL_ES: @OZ_GL_ES@";
  50 + "OZ_GLES: @OZ_GLES@";
51 51
52 52 }
2  src/client/CMakeLists.txt
@@ -174,7 +174,7 @@ else()
174 174 target_link_libraries( client ${SDL_TTF_LIBRARIES} ${SDL_LIBRARIES} )
175 175 endif()
176 176
177   -if( OZ_GL_ES )
  177 +if( OZ_GLES )
178 178 target_link_libraries( client ${GLES2_LIBRARIES} )
179 179 else()
180 180 target_link_libraries( client ${OPENGL_gl_LIBRARY} )
2  src/client/Client.cc
@@ -94,7 +94,7 @@ int Client::init( int argc, char** argv )
94 94
95 95 optind = 1;
96 96 int opt;
97   - while( ( opt = getopt( argc, argv, "vlie:t:L:p:h?" ) ) >= 0 ) {
  97 + while( ( opt = getopt( argc, argv, "vli:e:t:L:p:h?" ) ) >= 0 ) {
98 98 switch( opt ) {
99 99 case 'v': {
100 100 Log::showVerbose = true;
2  src/client/NaClUpdater.cc
@@ -107,7 +107,7 @@ bool NaClUpdater::checkUpdates()
107 107 downloader.begin( REMOTE_MANIFEST );
108 108
109 109 do {
110   - Time::sleep( 100 );
  110 + Time::sleep( 20 );
111 111 Log::printRaw( "." );
112 112 }
113 113 while( !downloader.isComplete() );
2  src/client/OpenGL.hh
@@ -25,7 +25,7 @@
25 25
26 26 #include <client/NaClPlatform.hh>
27 27
28   -#ifdef OZ_GL_ES
  28 +#ifdef OZ_GLES
29 29 # include <GLES2/gl2.h>
30 30 #else
31 31 # define GL_GLEXT_PROTOTYPES
37 src/client/Render.cc
@@ -624,12 +624,12 @@ void Render::init()
624 624 bool hasFloatTex = false;
625 625 bool hasS3TC = false;
626 626
627   - const char* vendor = String::cstr( glGetString( GL_VENDOR ) );
628   - const char* renderer = String::cstr( glGetString( GL_RENDERER ) );
629   - const char* version = String::cstr( glGetString( GL_VERSION ) );
630   - const char* glslVersion = String::cstr( glGetString( GL_SHADING_LANGUAGE_VERSION ) );
631   - const char* sExtensions = String::cstr( glGetString( GL_EXTENSIONS ) );
632   - DArray<String> extensions = String::trim( sExtensions ).split( ' ' );
  627 + const char* vendor = String::cstr( glGetString( GL_VENDOR ) );
  628 + const char* renderer = String::cstr( glGetString( GL_RENDERER ) );
  629 + const char* version = String::cstr( glGetString( GL_VERSION ) );
  630 + const char* glslVersion = String::cstr( glGetString( GL_SHADING_LANGUAGE_VERSION ) );
  631 + const char* sExtensions = String::cstr( glGetString( GL_EXTENSIONS ) );
  632 + DArray<String> extensions = String::trim( sExtensions ).split( ' ' );
633 633
634 634 // glGetString( GL_EXTENSIONS ) generates an error when using OpenGL 3.2+ Core Profile.
635 635 glGetError();
@@ -818,12 +818,11 @@ void Render::drawDyn()
818 818 colour = Vec4( 0.0f, 0.0f, 1.0f, 1.0f );
819 819 }
820 820
821   - body->rot = ~body->rot;
822   - body->rotMat = Mat33::rotation( body->rot );
  821 + body->update();
823 822
824 823 shape.colour( colour );
825   - shape.wireBox( body->getBounds().toAABB() );
826   - shape.object( body->pos, body->rotMat, body->shape() );
  824 + shape.wireBox( body->bb.toAABB() );
  825 + shape.object( body->p, body->oMat, body->shape() );
827 826 }
828 827 }
829 828
@@ -841,10 +840,10 @@ void Render::loadDyn()
841 840 // Capsule* c = new Capsule( 1, 1 );
842 841
843 842 body = new DBody();
844   - body->pos = Point( 140, 0, 80 );
845   - body->rot = Quat::ID;
846   - body->rotMat = Mat33::rotation( body->rot );
  843 + body->p = Point( 140, 0, 80 );
  844 + body->o = Quat::ID;
847 845 body->setShape( c );
  846 + body->update();
848 847 space.bodies.add( body );
849 848
850 849 physics.add( body );
@@ -852,10 +851,10 @@ void Render::loadDyn()
852 851 Box* b = new Box( Vec3( 1, 1, 2 ) );
853 852
854 853 body = new DBody();
855   - body->pos = Point( 143, 0, 80 );
856   - body->rot = Quat::ID;
857   - body->rotMat = Mat33::rotation( body->rot );
  854 + body->p = Point( 143, 0, 80 );
  855 + body->o = Quat::ID;
858 856 body->setShape( b );
  857 + body->update();
859 858 space.bodies.add( body );
860 859
861 860 physics.add( body );
@@ -863,10 +862,10 @@ void Render::loadDyn()
863 862 Box* p = new Box( Vec3( 10, 10, 1 ) );
864 863
865 864 body = new DBody();
866   - body->pos = Point( 142, 0, 75 );
867   - body->rot = Quat::ID;
868   - body->rotMat = Mat33::rotation( body->rot );
  865 + body->p = Point( 142, 0, 75 );
  866 + body->o = Quat::ID;
869 867 body->setShape( p );
  868 + body->update();
870 869 space.bodies.add( body );
871 870 }
872 871
28 src/client/Sound.cc
@@ -104,7 +104,7 @@ void Sound::musicOpen( const char* path )
104 104 musicStreamType = OGG;
105 105 }
106 106 else if( file.hasExtension( "mp3" ) ) {
107   - if( libMad != nullptr ) {
  107 + if( libMad.isOpened() ) {
108 108 musicStreamType = MP3;
109 109 }
110 110 else {
@@ -112,7 +112,7 @@ void Sound::musicOpen( const char* path )
112 112 }
113 113 }
114 114 else if( file.hasExtension( "aac" ) ) {
115   - if( libFaad != nullptr ) {
  115 + if( libFaad.isOpened() ) {
116 116 musicStreamType = AAC;
117 117 }
118 118 else {
@@ -868,15 +868,9 @@ void Sound::destroy()
868 868 }
869 869
870 870 #ifndef __native_client__
871   - if( libFaad != nullptr ) {
872   - SDL_UnloadObject( libFaad );
873   - }
874   - if( libMad != nullptr ) {
875   - SDL_UnloadObject( libMad );
876   - }
877   - if( libeSpeak != nullptr ) {
878   - SDL_UnloadObject( libeSpeak );
879   - }
  871 + libFaad.close();
  872 + libMad.close();
  873 + libeSpeak.close();
880 874 #endif
881 875
882 876 Log::printEnd( " OK" );
@@ -901,9 +895,7 @@ void Sound::initLibs()
901 895
902 896 Log::print( "Linking eSpeak library '%s' ...", libeSpeakName );
903 897
904   - libeSpeak = SDL_LoadObject( libeSpeakName );
905   -
906   - if( libeSpeak == nullptr ) {
  898 + if( !libeSpeak.open( libeSpeakName ) ) {
907 899 Log::printEnd( " Not found, speech synthesis not supported" );
908 900 }
909 901 else {
@@ -919,9 +911,7 @@ void Sound::initLibs()
919 911
920 912 Log::print( "Linking MAD library '%s' ...", libMadName );
921 913
922   - libMad = SDL_LoadObject( libMadName );
923   -
924   - if( libMad == nullptr ) {
  914 + if( !libMad.open( libMadName ) ) {
925 915 liber.mapMP3s = false;
926 916
927 917 Log::printEnd( " Not found, MP3 not supported" );
@@ -943,9 +933,7 @@ void Sound::initLibs()
943 933
944 934 Log::print( "Linking FAAD library '%s' ...", libFaadName );
945 935
946   - libFaad = SDL_LoadObject( libFaadName );
947   -
948   - if( libFaad == nullptr ) {
  936 + if( !libFaad.open( libFaadName ) ) {
949 937 liber.mapAACs = false;
950 938
951 939 Log::printEnd( " Not found, AAC not supported" );
6 src/client/Sound.hh
@@ -59,9 +59,9 @@ class Sound
59 59 ALCdevice* soundDevice;
60 60 ALCcontext* soundContext;
61 61
62   - void* libeSpeak;
63   - void* libMad;
64   - void* libFaad;
  62 + SharedLib libeSpeak;
  63 + SharedLib libMad;
  64 + SharedLib libFaad;
65 65
66 66 Bitset playedStructs;
67 67 float volume;
6 src/client/common.hh
@@ -41,7 +41,7 @@
41 41 decltype( ::func )* func = nullptr
42 42
43 43 #define OZ_DL_LOAD( l, func ) \
44   - *( void** )( &func ) = SDL_LoadFunction( l, #func ); \
  44 + func = reinterpret_cast< decltype( func ) >( l.get( #func ) ); \
45 45 if( func == nullptr ) { \
46 46 OZ_ERROR( "Failed to link function: " #func ); \
47 47 }
@@ -118,8 +118,8 @@ struct TexCoord
118 118 }
119 119 };
120 120
121   -extern Collider collider;
122   -extern JSON config;
  121 +extern matrix::Collider collider;
  122 +extern JSON config;
123 123
124 124 }
125 125 }
2  src/client/config.hh.in
@@ -33,4 +33,4 @@
33 33 #cmakedefine OZ_NET
34 34
35 35 // Use OpenGL ES 2.0.
36   -#cmakedefine OZ_GL_ES
  36 +#cmakedefine OZ_GLES
2  src/nirvana/common.hh
@@ -33,7 +33,7 @@ namespace nirvana
33 33 using namespace common;
34 34 using namespace matrix;
35 35
36   -extern Collider collider;
  36 +extern matrix::Collider collider;
37 37
38 38 }
39 39 }
8 src/ozCore/CMakeLists.txt
@@ -39,6 +39,7 @@ add_library( ozCore
39 39 SBitset.hh
40 40 Semaphore.hh
41 41 Set.hh
  42 + SharedLib.hh
42 43 simd.hh
43 44 SList.hh
44 45 SpinLock.hh
@@ -63,6 +64,7 @@ add_library( ozCore
63 64 Point.cc
64 65 Quat.cc
65 66 Semaphore.cc
  67 + SharedLib.cc
66 68 StackTrace.cc
67 69 String.cc
68 70 System.cc
@@ -76,16 +78,16 @@ add_library( ozCore
76 78 if( EMSCRIPTEN )
77 79 # target_link_libraries( ozCore )
78 80 elseif( ANDROID )
79   - target_link_libraries( ozCore ${PHYSFS_LIBRARY} ${ZLIB_LIBRARY} -lOpenSLES -llog ${PLATFORM_STL_LIBRARY} )
  81 + target_link_libraries( ozCore ${PHYSFS_LIBRARY} ${ZLIB_LIBRARY} -ldl -lOpenSLES -llog ${PLATFORM_STL_LIBRARY} )
80 82 elseif( NACL )
81 83 target_link_libraries( ozCore ${PHYSFS_LIBRARY} ${ZLIB_LIBRARY} -lppapi_cpp -lppapi -lpthread )
82 84 elseif( WIN32 )
83 85 target_link_libraries( ozCore ${PHYSFS_LIBRARY} -lwinmm )
84 86 elseif( NOT CMAKE_SYSTEM_NAME STREQUAL Linux )
85   - target_link_libraries( ozCore ${PHYSFS_LIBRARY} -lpulse-simple -lrt -lpthread )
  87 + target_link_libraries( ozCore ${PHYSFS_LIBRARY} -ldl -lpulse-simple -lrt -lpthread )
86 88 else()
87 89 # TODO: Remove -lrt once glibc 2.17 becomes minimum.
88   - target_link_libraries( ozCore ${PHYSFS_LIBRARY} -lpulse-simple -lasound -lrt -lpthread )
  90 + target_link_libraries( ozCore ${PHYSFS_LIBRARY} -ldl -lpulse-simple -lasound -lrt -lpthread )
89 91 endif()
90 92
91 93 set_target_properties( ozCore PROPERTIES VERSION ${OZ_VERSION} SOVERSION 0 )
398 src/ozCore/File.cc
@@ -65,81 +65,8 @@ namespace oz
65 65
66 66 #ifdef __native_client__
67 67
68   -#define SEMAPHORE_POST() \
69   - pthread_mutex_lock( &_fd->semaphore.mutex ); \
70   - ++_fd->semaphore.counter; \
71   - pthread_mutex_unlock( &_fd->semaphore.mutex ); \
72   - pthread_cond_signal( &_fd->semaphore.cond )
73   -
74   -#define SEMAPHORE_WAIT() \
75   - pthread_mutex_lock( &descriptor->semaphore.mutex ); \
76   - while( descriptor->semaphore.counter == 0 ) { \
77   - pthread_cond_wait( &descriptor->semaphore.cond, &descriptor->semaphore.mutex ); \
78   - } \
79   - --descriptor->semaphore.counter; \
80   - pthread_mutex_unlock( &descriptor->semaphore.mutex )
81   -
82   -#define DEFINE_CALLBACK( name, code ) \
83   - struct _Callback##name \
84   - { \
85   - static void _main##name( void* _data, int _result ) \
86   - { \
87   - Descriptor* _fd = static_cast<Descriptor*>( _data ); \
88   - static_cast<void>( _fd ); \
89   - static_cast<void>( _result ); \
90   - code \
91   - } \
92   - }
93   -
94   -#define CALLBACK_OBJECT( name, arg ) \
95   - pp::CompletionCallback( _Callback##name::_main##name, arg )
96   -
97   -#define MAIN_CALL( name ) \
98   - ppCore->CallOnMainThread( 0, CALLBACK_OBJECT( name, descriptor ) )
99   -
100   -struct File::Descriptor
101   -{
102   - struct Semaphore
103   - {
104   - pthread_mutex_t mutex;
105   - pthread_cond_t cond;
106   - volatile int counter;
107   - };
108   -
109   - // Some Descriptor members are also useful for static functions.
110   - static Descriptor staticDesc;
111   -
112   - Semaphore semaphore;
113   - File* file;
114   - pp::FileRef* fref;
115   - pp::FileIO* fio;
116   - PP_FileInfo info;
117   - char* buffer;
118   - int size;
119   - int offset;
120   -
121   - OZ_HIDDEN
122   - explicit Descriptor( File* file_ ) :
123   - file( file_ )
124   - {
125   - pthread_mutex_init( &semaphore.mutex, nullptr );
126   - pthread_cond_init( &semaphore.cond, nullptr );
127   - semaphore.counter = 0;
128   - }
129   -
130   - OZ_HIDDEN
131   - ~Descriptor()
132   - {
133   - pthread_cond_destroy( &semaphore.cond );
134   - pthread_mutex_destroy( &semaphore.mutex );
135   - }
136   -};
137   -
138   -OZ_HIDDEN
139   -File::Descriptor File::Descriptor::staticDesc( nullptr );
140   -
141   -static pp::Core* ppCore = nullptr;
142   -static pp::FileSystem* ppFileSystem = nullptr;
  68 +static pp::Core* ppCore = nullptr;
  69 +static pp::FileSystem ppFileSystem;
143 70
144 71 #endif // __native_client__
145 72
@@ -152,10 +79,6 @@ File::File( FileSystem fileSystem, const char* path ) :
152 79 filePath( path ), fileType( MISSING ), fileFS( fileSystem ), fileSize( -1 ), fileTime( 0 ),
153 80 data( nullptr )
154 81 {
155   -#ifdef __native_client__
156   - descriptor = new Descriptor( this );
157   -#endif
158   -
159 82 // Avoid stat'ing obviously non-existent files.
160 83 if( filePath.isEmpty() ) {
161 84 return;
@@ -167,29 +90,17 @@ File::File( FileSystem fileSystem, const char* path ) :
167 90 File::~File()
168 91 {
169 92 unmap();
170   -
171   -#ifdef __native_client__
172   - delete descriptor;
173   -#endif
174 93 }
175 94
176 95 File::File( const File& file ) :
177 96 filePath( file.filePath ), fileType( file.fileType ), fileFS( file.fileFS ),
178 97 fileSize( file.fileSize ), fileTime( file.fileTime ), data( nullptr )
179   -{
180   -#ifdef __native_client__
181   - descriptor = new Descriptor( this );
182   -#endif
183   -}
  98 +{}
184 99
185 100 File::File( File&& file ) :
186 101 filePath( static_cast<String&&>( file.filePath ) ), fileType( file.fileType ),
187 102 fileFS( file.fileFS ), fileSize( file.fileSize ), fileTime( file.fileTime ), data( file.data )
188 103 {
189   -#ifdef __native_client__
190   - descriptor = new Descriptor( this );
191   -#endif
192   -
193 104 file.filePath = "";
194 105 file.fileType = MISSING;
195 106 file.fileFS = NATIVE;
@@ -311,51 +222,28 @@ bool File::stat()
311 222 return true;
312 223 }
313 224
314   - DEFINE_CALLBACK( queryResult, {
315   - if( _result == PP_OK ) {
316   - if( _fd->info.type == PP_FILETYPE_REGULAR ) {
317   - _fd->file->fileType = REGULAR;
318   - _fd->file->fileSize = int( _fd->info.size );
319   - _fd->file->fileTime = long64( max( _fd->info.creation_time,
320   - _fd->info.last_modified_time ) );
321   - }
322   - else if( _fd->info.type == PP_FILETYPE_DIRECTORY ) {
323   - _fd->file->fileType = DIRECTORY;
324   - _fd->file->fileSize = -1;
325   - _fd->file->fileTime = long64( max( _fd->info.creation_time,
326   - _fd->info.last_modified_time ) );
327   - }
328   - }
329   -
330   - delete _fd->fio;
331   - SEMAPHORE_POST();
332   - } );
333   - DEFINE_CALLBACK( query, {
334   - if( _result == PP_OK ) {
335   - int ret = _fd->fio->Query( &_fd->info, CALLBACK_OBJECT( queryResult, _fd ) );
336   - if( ret == PP_OK_COMPLETIONPENDING ) {
337   - return;
338   - }
339   - }
  225 + pp::FileRef file( ppFileSystem, filePath );
  226 + pp::FileIO fio( System::instance );
340 227
341   - delete _fd->fio;
342   - SEMAPHORE_POST();
343   - } );
344   - DEFINE_CALLBACK( open, {
345   - _fd->fio = new pp::FileIO( System::instance );
346   -
347   - int ret = _fd->fio->Open( pp::FileRef( *ppFileSystem, _fd->file->filePath ), 0,
348   - CALLBACK_OBJECT( query, _fd ) );
349   - if( ret == PP_OK_COMPLETIONPENDING ) {
350   - return;
351   - }
  228 + if( fio.Open( file, 0, pp::BlockUntilComplete() ) != PP_OK ) {
  229 + return false;
  230 + }
352 231
353   - delete _fd->fio;
354   - SEMAPHORE_POST();
355   - } );
  232 + PP_FileInfo fileInfo;
  233 + if( fio.Query( &fileInfo, pp::BlockUntilComplete() ) != PP_OK ) {
  234 + return false;
  235 + }
356 236
357   - MAIN_CALL( open );
358   - SEMAPHORE_WAIT();
  237 + if( fileInfo.type == PP_FILETYPE_REGULAR ) {
  238 + fileType = REGULAR;
  239 + fileSize = int( fileInfo.size );
  240 + fileTime = long64( max( fileInfo.creation_time, fileInfo.last_modified_time ) );
  241 + }
  242 + else if( fileInfo.type == PP_FILETYPE_DIRECTORY ) {
  243 + fileType = DIRECTORY;
  244 + fileSize = -1;
  245 + fileTime = long64( max( fileInfo.creation_time, fileInfo.last_modified_time ) );
  246 + }
359 247
360 248 #elif defined( _WIN32 )
361 249
@@ -566,59 +454,30 @@ bool File::read( char* buffer, int* size ) const
566 454
567 455 #if defined( __native_client__ )
568 456
569   - descriptor->buffer = buffer;
570   - descriptor->size = min( *size, fileSize );
571   - descriptor->offset = 0;
572   -
573   - DEFINE_CALLBACK( read, {
574   - if( _result > 0 ) {
575   - _fd->offset += _result;
576   -
577   - if( _fd->offset != _fd->size ) {
578   - int ret = _fd->fio->Read( _fd->offset, &_fd->buffer[_fd->offset], _fd->size - _fd->offset,
579   - CALLBACK_OBJECT( read, _fd ) );
580   - if( ret == PP_OK_COMPLETIONPENDING ) {
581   - return;
582   - }
583   - }
584   - }
585   -
586   - delete _fd->fio;
587   - SEMAPHORE_POST();
588   - } );
589   - DEFINE_CALLBACK( beginRead, {
590   - if( _result == PP_OK ) {
591   - int ret = _fd->fio->Read( 0, _fd->buffer, _fd->size, CALLBACK_OBJECT( read, _fd ) );
592   - if( ret == PP_OK_COMPLETIONPENDING ) {
593   - return;
594   - }
595   - }
596   -
597   - delete _fd->fio;
598   - SEMAPHORE_POST();
599   - } );
600   - DEFINE_CALLBACK( open, {
601   - _fd->fio = new pp::FileIO( System::instance );
  457 + int readSize = min( *size, fileSize );
  458 + int offset = 0;
602 459
603   - int ret = _fd->fio->Open( pp::FileRef( *ppFileSystem, _fd->file->filePath ),
604   - PP_FILEOPENFLAG_READ, CALLBACK_OBJECT( beginRead, _fd ) );
605   - if( ret == PP_OK_COMPLETIONPENDING ) {
606   - return;
607   - }
  460 + pp::FileRef file( ppFileSystem, filePath );
  461 + pp::FileIO fio( System::instance );
608 462
609   - delete _fd->fio;
610   - SEMAPHORE_POST();
611   - } );
  463 + if( fio.Open( file, PP_FILEOPENFLAG_READ, pp::BlockUntilComplete() ) != PP_OK ) {
  464 + *size = 0;
  465 + return false;
  466 + }
612 467
613   - MAIN_CALL( open );
614   - SEMAPHORE_WAIT();
  468 + int result;
  469 + while( ( result = fio.Read( offset, &buffer[offset], readSize - offset,
  470 + pp::BlockUntilComplete() ) ) > 0 )
  471 + {
  472 + offset += result;
  473 + }
615 474
616   - if( descriptor->offset != descriptor->size ) {
  475 + if( result < 0 ) {
617 476 *size = 0;
618 477 return false;
619 478 }
620 479
621   - *size = descriptor->offset;
  480 + *size = offset;
622 481
623 482 #elif defined( _WIN32 )
624 483
@@ -721,55 +580,26 @@ bool File::write( const char* buffer, int size ) const
721 580
722 581 #if defined( __native_client__ )
723 582
724   - descriptor->buffer = const_cast<char*>( buffer );
725   - descriptor->size = size;
726   - descriptor->offset = 0;
  583 + int offset = 0;
727 584
728   - DEFINE_CALLBACK( write, {
729   - if( _result > 0 ) {
730   - _fd->offset += _result;
731   -
732   - if( _fd->offset != _fd->size ) {
733   - int ret = _fd->fio->Write( _fd->offset, &_fd->buffer[_fd->offset],
734   - _fd->size - _fd->offset, CALLBACK_OBJECT( write, _fd ) );
735   - if( ret == PP_OK_COMPLETIONPENDING ) {
736   - return;
737   - }
738   - }
739   - }
740   -
741   - delete _fd->fio;
742   - SEMAPHORE_POST();
743   - } );
744   - DEFINE_CALLBACK( beginWrite, {
745   - if( _result == PP_OK ) {
746   - int ret = _fd->fio->Write( 0, _fd->buffer, _fd->size, CALLBACK_OBJECT( write, _fd ) );
747   - if( ret == PP_OK_COMPLETIONPENDING ) {
748   - return;
749   - }
750   - }
751   -
752   - delete _fd->fio;
753   - SEMAPHORE_POST();
754   - } );
755   - DEFINE_CALLBACK( open, {
756   - _fd->fio = new pp::FileIO( System::instance );
757   -
758   - int ret = _fd->fio->Open( pp::FileRef( *ppFileSystem, _fd->file->filePath ),
759   - PP_FILEOPENFLAG_WRITE | PP_FILEOPENFLAG_CREATE | PP_FILEOPENFLAG_TRUNCATE,
760   - CALLBACK_OBJECT( beginWrite, _fd ) );
761   - if( ret == PP_OK_COMPLETIONPENDING ) {
762   - return;
763   - }
  585 + pp::FileRef file( ppFileSystem, filePath );
  586 + pp::FileIO fio( System::instance );
764 587
765   - delete _fd->fio;
766   - SEMAPHORE_POST();
767   - } );
  588 + if( fio.Open( file, PP_FILEOPENFLAG_WRITE | PP_FILEOPENFLAG_CREATE | PP_FILEOPENFLAG_TRUNCATE,
  589 + pp::BlockUntilComplete() ) != PP_OK )
  590 + {
  591 + return false;
  592 + }
768 593
769   - MAIN_CALL( open );
770   - SEMAPHORE_WAIT();
  594 + int result;
  595 + while( offset != size &&
  596 + ( result = fio.Write( offset, &buffer[offset], size - offset,
  597 + pp::BlockUntilComplete() ) ) > 0 )
  598 + {
  599 + offset += result;
  600 + }
771 601
772   - if( descriptor->offset != size ) {
  602 + if( offset != size ) {
773 603 return false;
774 604 }
775 605
@@ -1024,37 +854,8 @@ bool File::mkdir( const char* path, FileSystem fileSystem )
1024 854 else {
1025 855 #if defined( __native_client__ )
1026 856
1027   - Descriptor localDescriptor( nullptr );
1028   - Descriptor* descriptor = &localDescriptor;
1029   -
1030   - // Abuse buffer for file path and size for result.
1031   - descriptor->buffer = const_cast<char*>( path );
1032   - descriptor->size = false;
1033   -
1034   - DEFINE_CALLBACK( mkdirResult, {
1035   - if( _result == PP_OK ) {
1036   - _fd->size = true;
1037   - }
1038   -
1039   - delete _fd->fref;
1040   - SEMAPHORE_POST();
1041   - } );
1042   - DEFINE_CALLBACK( mkdir, {
1043   - _fd->fref = new pp::FileRef( *ppFileSystem, _fd->buffer );
1044   -
1045   - int ret = _fd->fref->MakeDirectory( CALLBACK_OBJECT( mkdirResult, _fd ) );
1046   - if( ret == PP_OK_COMPLETIONPENDING ) {
1047   - return;
1048   - }
1049   -
1050   - delete _fd->fref;
1051   - SEMAPHORE_POST();
1052   - } );
1053   -
1054   - MAIN_CALL( mkdir );
1055   - SEMAPHORE_WAIT();
1056   -
1057   - return descriptor->size != 0;
  857 + pp::FileRef file( ppFileSystem, path );
  858 + return file.MakeDirectory( pp::BlockUntilComplete() ) == PP_OK;
1058 859
1059 860 #elif defined( _WIN32 )
1060 861
@@ -1076,37 +877,8 @@ bool File::rm( const char* path, FileSystem fileSystem )
1076 877 else {
1077 878 #if defined( __native_client__ )
1078 879
1079   - Descriptor localDescriptor( nullptr );
1080   - Descriptor* descriptor = &localDescriptor;
1081   -
1082   - // Abuse buffer for file path and size for result.
1083   - descriptor->buffer = const_cast<char*>( path );
1084   - descriptor->size = false;
1085   -
1086   - DEFINE_CALLBACK( rmResult, {
1087   - if( _result == PP_OK ) {
1088   - _fd->size = true;
1089   - }
1090   -
1091   - delete _fd->fref;
1092   - SEMAPHORE_POST();
1093   - } );
1094   - DEFINE_CALLBACK( rm, {
1095   - _fd->fref = new pp::FileRef( *ppFileSystem, _fd->buffer );
1096   -
1097   - int ret = _fd->fref->Delete( CALLBACK_OBJECT( rmResult, _fd ) );
1098   - if( ret == PP_OK_COMPLETIONPENDING ) {
1099   - return;
1100   - }
1101   -
1102   - delete _fd->fref;
1103   - SEMAPHORE_POST();
1104   - } );
1105   -
1106   - MAIN_CALL( rm );
1107   - SEMAPHORE_WAIT();
1108   -
1109   - return descriptor->size != 0;
  880 + pp::FileRef file( ppFileSystem, path );
  881 + return file.Delete( pp::BlockUntilComplete() ) == PP_OK;
1110 882
1111 883 #elif defined( _WIN32 )
1112 884
@@ -1187,38 +959,11 @@ void File::init( FileSystem fileSystem, NaClFileSystem naclFileSystem, int naclS
1187 959
1188 960 destroy();
1189 961
1190   - Descriptor* descriptor = &Descriptor::staticDesc;
1191   -
1192   - // We abuse staticDesc.size and staticDesc.offset variables to pass file system type and size to
1193   - // callback.
1194   - descriptor->size = naclSize;
1195   - descriptor->offset = naclFileSystem == PERSISTENT ? PP_FILESYSTEMTYPE_LOCALPERSISTENT :
1196   - PP_FILESYSTEMTYPE_LOCALTEMPORARY;
1197   -
1198   - DEFINE_CALLBACK( initResult, {
1199   - if( _result != PP_OK ) {
1200   - delete ppFileSystem;
1201   - ppFileSystem = nullptr;
1202   - }
1203   - SEMAPHORE_POST();
1204   - } );
1205   - DEFINE_CALLBACK( init, {
1206   - ppFileSystem = new pp::FileSystem( System::instance, PP_FileSystemType( _fd->offset ) );
1207   -
1208   - int ret = ppFileSystem->Open( _fd->size, CALLBACK_OBJECT( initResult, _fd ) );
1209   - if( ret == PP_OK_COMPLETIONPENDING ) {
1210   - return;
1211   - }
  962 + PP_FileSystemType type = naclFileSystem == PERSISTENT ? PP_FILESYSTEMTYPE_LOCALPERSISTENT :
  963 + PP_FILESYSTEMTYPE_LOCALTEMPORARY;
1212 964
1213   - delete ppFileSystem;
1214   - ppFileSystem = nullptr;
1215   - SEMAPHORE_POST();
1216   - } );
1217   -
1218   - MAIN_CALL( init );
1219   - SEMAPHORE_WAIT();
1220   -
1221   - if( ppFileSystem == nullptr ) {
  965 + ppFileSystem = pp::FileSystem( System::instance, type );
  966 + if( ppFileSystem.Open( naclSize, pp::BlockUntilComplete() ) != PP_OK ) {
1222 967 OZ_ERROR( "Local file system open failed" );
1223 968 }
1224 969
@@ -1231,25 +976,6 @@ void File::destroy( FileSystem fileSystem )
1231 976 if( fileSystem == VIRTUAL ) {
1232 977 PHYSFS_deinit();
1233 978 }
1234   - else {
1235   -#ifdef __native_client__
1236   -
1237   - if( ppFileSystem != nullptr ) {
1238   - Descriptor* descriptor = &Descriptor::staticDesc;
1239   -
1240   - DEFINE_CALLBACK( free, {
1241   - delete ppFileSystem;
1242   - ppFileSystem = nullptr;
1243   -
1244   - SEMAPHORE_POST();
1245   - } );
1246   -
1247   - MAIN_CALL( free );
1248   - SEMAPHORE_WAIT();
1249   - }
1250   -
1251   -#endif
1252   - }
1253 979 }
1254 980
1255 981 }
17 src/ozCore/File.hh
@@ -73,17 +73,12 @@ class File
73 73
74 74 private:
75 75
76   - struct Descriptor;
77   -
78   - String filePath; ///< %File path.
79   - Type fileType; ///< %File type.
80   - FileSystem fileFS; ///< %File system type.
81   - int fileSize; ///< %File size (>= 0 if `fileType == REGULAR`, -1 otherwise).
82   - long64 fileTime; ///< Modification or creation time, what is newer.
83   - char* data; ///< Mapped memory.
84   -#ifdef __native_client__
85   - Descriptor* descriptor; ///< Structure for control and data exchange with NaCl callbacks.
86   -#endif
  76 + String filePath; ///< %File path.
  77 + Type fileType; ///< %File type.
  78 + FileSystem fileFS; ///< %File system type.
  79 + int fileSize; ///< %File size (>= 0 if `fileType == REGULAR`, -1 otherwise).
  80 + long64 fileTime; ///< Modification or creation time, what is newer.
  81 + char* data; ///< Mapped memory.
87 82
88 83 public:
89 84
100 src/ozCore/SharedLib.cc
... ... @@ -0,0 +1,100 @@
  1 +/*
  2 + * ozCore - OpenZone Core Library.
  3 + *
  4 + * Copyright © 2002-2013 Davorin Učakar
  5 + *
  6 + * This software is provided 'as-is', without any express or implied warranty.
  7 + * In no event will the authors be held liable for any damages arising from
  8 + * the use of this software.
  9 + *
  10 + * Permission is granted to anyone to use this software for any purpose,
  11 + * including commercial applications, and to alter it and redistribute it
  12 + * freely, subject to the following restrictions:
  13 + *
  14 + * 1. The origin of this software must not be misrepresented; you must not
  15 + * claim that you wrote the original software. If you use this software in
  16 + * a product, an acknowledgement in the product documentation would be
  17 + * appreciated but is not required.
  18 + * 2. Altered source versions must be plainly marked as such, and must not be
  19 + * misrepresented as being the original software.
  20 + * 3. This notice may not be removed or altered from any source distribution.
  21 + */
  22 +
  23 +/**
  24 + * @file ozCore/SharedLib.cc
  25 + */
  26 +
  27 +#include "SharedLib.hh"
  28 +
  29 +#if defined( __native_client__ )
  30 +#elif defined( _WIN32 )
  31 +# include <windows.h>
  32 +#else
  33 +# include <dlfcn.h>
  34 +#endif
  35 +
  36 +namespace oz
  37 +{
  38 +
  39 +#ifdef __native_client__
  40 +
  41 +const bool SharedLib::SUPPORTED = false;
  42 +
  43 +SharedLib::Method* SharedLib::get( const char* ) const
  44 +{
  45 + return nullptr;
  46 +}
  47 +
  48 +bool SharedLib::open( const char* )
  49 +{
  50 + return nullptr;
  51 +}
  52 +
  53 +void SharedLib::close()
  54 +{}
  55 +
  56 +#else
  57 +
  58 +const bool SharedLib::SUPPORTED = true;
  59 +
  60 +SharedLib::Method* SharedLib::get( const char* symbol ) const
  61 +{
  62 + if( handle == nullptr ) {
  63 + return nullptr;
  64 + }
  65 + else {
  66 +#ifdef _WIN32
  67 + return reinterpret_cast<Method*>( GetProcAddress( static_cast<HMODULE>( handle ), symbol ) );
  68 +#else
  69 + Method* method;
  70 + *( void** ) &method = dlsym( handle, symbol );
  71 + return method;
  72 +#endif
  73 + }
  74 +}
  75 +
  76 +bool SharedLib::open( const char* name )
  77 +{
  78 +#ifdef _WIN32
  79 + handle = static_cast<void*>( LoadLibrary( name ) );
  80 +#else
  81 + handle = dlopen( name, RTLD_NOW );
  82 +#endif
  83 + return handle != nullptr;
  84 +}
  85 +
  86 +void SharedLib::close()
  87 +{
  88 + if( handle != nullptr ) {
  89 +#ifdef _WIN32
  90 + FreeLibrary( static_cast<HMODULE>( handle ) );
  91 +#else
  92 + dlclose( handle );
  93 +#endif
  94 + handle = nullptr;
  95 + }
  96 +}
  97 +
  98 +#endif
  99 +
  100 +}
118 src/ozCore/SharedLib.hh
... ... @@ -0,0 +1,118 @@
  1 +/*
  2 + * ozCore - OpenZone Core Library.
  3 + *
  4 + * Copyright © 2002-2013 Davorin Učakar
  5 + *
  6 + * This software is provided 'as-is', without any express or implied warranty.
  7 + * In no event will the authors be held liable for any damages arising from
  8 + * the use of this software.
  9 + *
  10 + * Permission is granted to anyone to use this software for any purpose,
  11 + * including commercial applications, and to alter it and redistribute it
  12 + * freely, subject to the following restrictions:
  13 + *
  14 + * 1. The origin of this software must not be misrepresented; you must not
  15 + * claim that you wrote the original software. If you use this software in
  16 + * a product, an acknowledgement in the product documentation would be
  17 + * appreciated but is not required.
  18 + * 2. Altered source versions must be plainly marked as such, and must not be
  19 + * misrepresented as being the original software.
  20 + * 3. This notice may not be removed or altered from any source distribution.
  21 + */
  22 +
  23 +/**
  24 + * @file ozCore/SharedLib.hh
  25 + *
  26 + * SharedLib class.
  27 + */
  28 +
  29 +#pragma once
  30 +
  31 +#include "common.hh"
  32 +
  33 +namespace oz
  34 +{
  35 +
  36 +/**
  37 + * Class for loading shared libraries.
  38 + */
  39 +class SharedLib
  40 +{
  41 + public:
  42 +
  43 + /**
  44 + * True iff platform supports shared libraries.
  45 + */
  46 + static const bool SUPPORTED;
  47 +
  48 + /**
  49 + * Generic method type to avoid casts between pointer-to-function and pointer-to-object.
  50 + */
  51 + typedef void Method();
  52 +
  53 + private:
  54 +
  55 + void* handle; ///< Internal handle to the library.
  56 +
  57 + public:
  58 +
  59 + /**
  60 + * Create uninitialised instance.
  61 + */
  62 + explicit SharedLib() :
  63 + handle( nullptr )
  64 + {}
  65 +
  66 + /**
  67 + * Destructor, closes library if still opened.
  68 + */
  69 + ~SharedLib()
  70 + {
  71 + close();
  72 + }
  73 +
  74 + /**
  75 + * Move constructor, transfers ownership.
  76 + */
  77 + SharedLib( SharedLib&& l ) :
  78 + handle( l.handle )
  79 + {
  80 + l.handle = nullptr;
  81 + }
  82 +
  83 + /**
  84 + * Move operator, transfers ownership.
  85 + */
  86 + SharedLib& operator = ( SharedLib&& l )
  87 + {
  88 + handle = l.handle;
  89 + l.handle = nullptr;
  90 + return *this;
  91 + }
  92 +
  93 + /**
  94 + * True iff successfully opened.
  95 + */
  96 + bool isOpened() const
  97 + {
  98 + return handle != nullptr;
  99 + }
  100 +
  101 + /**
  102 + * Obtain pointer to the requested function in the library (`nullptr` on error).
  103 + */
  104 + Method* get( const char* symbol ) const;
  105 +
  106 + /**
  107 + * Open shared library with the given (file) name.
  108 + */
  109 + bool open( const char* name );
  110 +
  111 + /**
  112 + * Close library if opened.
  113 + */
  114 + void close();
  115 +
  116 +};
  117 +
  118 +}
112 src/ozCore/System.cc
@@ -96,32 +96,25 @@ int raise( int )
96 96 namespace oz
97 97 {
98 98
99   -static const float BELL_TIME = 0.30f;
100   -static const float BELL_FREQUENCY = 1000.0f;
101   -static const int BELL_PREFERRED_RATE = 44100;
102   -
103   -#if defined( EMSCRIPTEN )
104   -
105   -static const timespec TIMESPEC_10MS = { 0, 10 * 1000000 };
106   -
107   -#elif defined( __ANDROID__ )
108   -
109   -static const timespec TIMESPEC_10MS = { 0, 10 * 1000000 };
  99 +static const float BELL_TIME = 0.30f;
  100 +static const float BELL_FREQUENCY = 1000.0f;
  101 +static const int BELL_PREFERRED_RATE = 44100;
  102 +#ifndef _WIN32
  103 +static const timespec TIMESPEC_10MS = { 0, 10 * 1000000 };
  104 +#endif
110 105
111   -#elif defined( __native_client__ )
  106 +#if defined( __native_client__ )
112 107
113 108 struct SampleInfo
114 109 {
115   - pp::Audio* audio;
116   - int nFrameSamples;
117   - int nSamples;
118   - int rate;
119   - int end;
120   - int offset;
  110 + int nFrameSamples;
  111 + int nSamples;
  112 + int rate;
  113 + int end;
  114 + int offset;
121 115 };
122 116
123   -static const timespec TIMESPEC_10MS = { 0, 10 * 1000000 };
124   -static pp::Core* core = nullptr;
  117 +static pp::Core* core = nullptr;
125 118
126 119 #elif defined( _WIN32 )
127 120
@@ -147,11 +140,6 @@ struct Wave
147 140 short samples[BELL_WAVE_SAMPLES * 2];
148 141 };
149 142
150   -#else
151   -
152   -static const timespec TIMESPEC_10MS = { 0, 10 * 1000000 };
153   -static const pa_sample_spec PA_SAMPLE_SPEC = { PA_SAMPLE_S16NE, BELL_PREFERRED_RATE, 2 };
154   -
155 143 #endif
156 144
157 145 static volatile int bellLock = 0;
@@ -282,65 +270,47 @@ static void* bellMain( void* )
282 270
283 271 #elif defined( __native_client__ )
284 272
285   -static void stopBellCallback( void* info_, int )
286   -{
287   - SampleInfo* info = static_cast<SampleInfo*>( info_ );
288   -
289   - info->audio->StopPlayback();
290   - info->audio->~Audio();
291   - free( info->audio );
292   - free( info );
293   -
294   - __sync_lock_release( &bellLock );
295   -}
296   -
297 273 static void bellPlayCallback( void* buffer, uint, void* info_ )
298 274 {
299 275 SampleInfo* info = static_cast<SampleInfo*>( info_ );
300 276 short* samples = static_cast<short*>( buffer );
301 277
302 278 if( info->offset >= info->end ) {
303   - core->CallOnMainThread( 0, pp::CompletionCallback( stopBellCallback, info ) );
  279 + __sync_lock_release( &bellLock );
  280 + }
  281 + else {
  282 + genBellSamples( samples, info->nSamples, info->rate, info->offset,
  283 + info->offset + info->nFrameSamples );
  284 + info->offset += info->nFrameSamples;