Просмотр исходного кода

Further work on Android - Rendering works now.

Joachim Meyer 10 лет назад
Родитель
Сommit
47a0e12143

+ 2 - 1
build/android/Polycore/jni/Application.mk

@@ -1,4 +1,5 @@
 APP_PLATFORM := android-14
 APP_ABI := all
 APP_STL := gnustl_static
-APP_CPPFLAGS += -std=c++11
+APP_CPPFLAGS += -std=c++11
+APP_OPTIM := release

+ 2 - 5
build/android/TemplateApp/AndroidManifest.xml

@@ -12,13 +12,10 @@
         <android:uses-permission android:name="android.permission.READ_PHONE_STATE" />
         <android:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 
-        <!--
-             Our activity is the built-in NativeActivity framework class.
-             This will take care of integrating with our NDK code.
-        -->
         <activity
             android:name="org.polycode.templateapp.PolycodeActivity"
-            android:configChanges="orientation|keyboardHidden"
+            android:configChanges="keyboardHidden"
+            android:screenOrientation="landscape"
             android:label="TemplateApp">
 
             <!-- Tell NativeActivity the name of our .so -->

+ 6 - 1
build/android/TemplateApp/build.gradle

@@ -38,8 +38,13 @@ android {
         }
     }
 
+    task copyAssets(type: Copy) {
+        from '../../../assets/default/default'
+        into 'src/main/assets/default'
+    }
+
     tasks.withType(JavaCompile) {
-        compileTask -> compileTask.dependsOn ndkBuild
+        compileTask -> compileTask.dependsOn ndkBuild, copyAssets
     }
 }
 

+ 1 - 6
build/android/TemplateApp/jni/Android.mk

@@ -12,11 +12,6 @@ LOCAL_MODULE := freetype
 LOCAL_SRC_FILES := $(LIBDIR)/libfreetype.a
 include $(PREBUILT_STATIC_LIBRARY)
 
-include $(CLEAR_VARS)
-LOCAL_MODULE := libpng
-LOCAL_SRC_FILES := $(LIBDIR)/libpng.a
-include $(PREBUILT_STATIC_LIBRARY)
-
 include $(CLEAR_VARS)
 LOCAL_MODULE := ogg
 LOCAL_SRC_FILES := $(LIBDIR)/libogg.so
@@ -30,7 +25,7 @@ include $(PREBUILT_SHARED_LIBRARY)
 include $(CLEAR_VARS)
 LOCAL_MODULE := TemplateApp
 LOCAL_LDLIBS := -landroid -lEGL -lGLESv2 -lz -llog
-LOCAL_STATIC_LIBRARIES := Polycore freetype libpng
+LOCAL_STATIC_LIBRARIES := Polycore freetype
 LOCAL_SHARED_LIBRARIES := ogg vorbis
 LOCAL_CFLAGS += -I$(LOCAL_PATH)/../../../../include -DUSE_EGL -DSTRICT_OPENGLES2
 LOCAL_SRC_FILES := PolycodeTemplate.cpp PolycodeTemplateApp.cpp

+ 2 - 1
build/android/TemplateApp/jni/Application.mk

@@ -1,4 +1,5 @@
 APP_PLATFORM := android-14
 APP_ABI := all
 APP_STL := gnustl_static
-APP_CPPFLAGS += -std=c++11
+APP_CPPFLAGS += -std=c++11
+APP_OPTIM := release

+ 8 - 9
build/android/TemplateApp/jni/PolycodeTemplateApp.cpp

@@ -9,21 +9,21 @@
 PolycodeTemplateApp::PolycodeTemplateApp(PolycodeView *view) {
     core = new POLYCODE_CORE(view, 800,480,false,false, 0,0,60);
 
-    core->addFileSource("android", "default");
     ResourcePool *globalPool = Services()->getResourceManager()->getGlobalPool();
     globalPool->loadResourcesFromFolder("default", true);
 
 	// Write your code here!
-    
+    srand(time(NULL));
     Scene *scene = new Scene(Scene::SCENE_2D);
     scene->useClearColor = true;
-    scene->clearColor.setColor(1.0f,1.0f,0.13f,1.0f);
+    scene->clearColor.setColor(1.0f / (float)(rand() % 5),1.0f / (float)(rand()%5),1.0f / (float)(rand() % 5),1.0f);
 
-    ScenePrimitive *test = new ScenePrimitive(ScenePrimitive::TYPE_VPLANE, 2, 2);
+    ScenePrimitive *test = new ScenePrimitive(ScenePrimitive::TYPE_VPLANE, 0.5, 0.5);
     scene->addChild(test);
-    test->setColor(1.0,0.0,0.0,1.0);
-	test->setPositionY(0.2);
-    LOGI("scene prim worked");
+    test->setPositionY(0.2);
+    test->setMaterialByName("Unlit");
+    test->getShaderPass(0).shaderBinding->loadTextureForParam("diffuse", "main_icon.png");
+    //test->setColor(1.0,0.0,0.0,1.0);
 
     SceneLabel *testLabel = new SceneLabel("Hello Polycode!", 32, "sans", Label::ANTIALIAS_FULL, 0.2);
 	testLabel->setPositionY(-0.2);
@@ -70,10 +70,9 @@ PolycodeTemplateApp::~PolycodeTemplateApp() {
 
 bool PolycodeTemplateApp::Update() {
     if (!core->paused) {
-        //LOGI("Update");
         return core->updateAndRender();
     } else {
-        usleep(200*1000);
+        usleep(200000);
         return true;
     }
 }

BIN
build/android/TemplateApp/src/main/assets/main_icon.png


+ 1 - 1
include/polycode/core/PolyAAssetFileProvider.h

@@ -51,7 +51,7 @@ namespace Polycode {
 			Polycode::CoreFile *openFile(const String &fileName, const String &opts);
             void closeFile(Polycode::CoreFile *file);
             
-			virtual bool parseFolder(const Polycode::String& pathString, bool showHidden, std::vector<OSFileEntry> &targetVector);
+			bool parseFolder(const Polycode::String& pathString, bool showHidden, std::vector<OSFileEntry> &targetVector);
 			
 			void addSource(const String &source);
             void removeSource(const String &source);

+ 2 - 0
include/polycode/core/PolyAndroidCore.h

@@ -120,6 +120,8 @@ namespace Polycode {
 		Number getBackingXRes();
 		Number getBackingYRes();
 		
+		CoreMutex* getEGLMutex();
+		
 		bool recreateContext;
 	private:
 

+ 1 - 2
include/polycode/core/PolyCore.h

@@ -307,8 +307,7 @@ namespace Polycode {
 
 		virtual void handleVideoModeChange(VideoModeChangeInfo *modeInfo) = 0;
 		virtual void flushRenderContext() = 0;
-        virtual void prepareRenderContext() {}
-        virtual bool isWindowInitialized() = 0;
+		virtual void prepareRenderContext() {}		virtual bool isWindowInitialized() {return true;};
 		CoreFile *openFile(const Polycode::String& fileName, const Polycode::String& opts);
 		void closeFile(CoreFile *file);
 		

+ 1 - 1
include/polycode/core/PolySDLCore.h

@@ -59,7 +59,7 @@ namespace Polycode {
 		void flushRenderContext();
 		
 		void handleVideoModeChange(VideoModeChangeInfo *modeInfo);
-		void setVideoMode(int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, bool retinaSupport = true);
+//		void setVideoMode(int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, bool retinaSupport = true);
 		void createThread(Threaded *target);
 		std::vector<Rectangle> getVideoModes();
 		

+ 1 - 1
include/polycode/core/PolySoundManager.h

@@ -51,7 +51,7 @@ namespace Polycode {
 	class _PolyExport AudioInterface {
 		public:
 			AudioInterface();
-			void addToBuffer(int16_t *data, unsigned int count);
+			//void addToBuffer(int16_t *data, unsigned int count);
 			virtual void setMixer(AudioMixer *mixer);
 			AudioMixer *getMixer();		   
 		protected:

+ 1 - 1
lib

@@ -1 +1 @@
-Subproject commit 9ef47aab442195e5bef4b28bc114ca2244ed59e9
+Subproject commit dca6caa74f7bd136039a643c496265c07dbb73c5

+ 3 - 3
src/core/PolyAAssetFileProvider.cpp

@@ -36,7 +36,7 @@ void AAssetFileProvider::addSource(const String &source) {
 	dir.name = source;
 	dir.system = AAssetManager_openDir(manager, source.c_str());
 	sourceFolders.push_back(dir);
-	Logger::log("addSource");
+// 	Logger::log("addSource");
 }
 
 void AAssetFileProvider::removeSource(const String &source) {
@@ -74,7 +74,7 @@ Polycode::CoreFile *AAssetFileProvider::openFile(const String &fileName, const S
 }
 
 bool AAssetFileProvider::parseFolder(const String& pathString, bool showHidden, std::vector< OSFileEntry >& targetVector){
-	Logger::log("Parse AAsset [%s]", pathString.c_str());
+// 	Logger::log("Parse AAsset [%s]", pathString.c_str());
 	String path = pathString;
 	if(pathString.substr(pathString.length(),1)!="/")
 		path = pathString + "/";
@@ -87,7 +87,7 @@ bool AAssetFileProvider::parseFolder(const String& pathString, bool showHidden,
 		String name = AAssetDir_getNextFileName(dir);
 		if(name == "")
 			break;
-		Logger::log("Parse AAsset Found File: %s", (path+name).c_str());
+// 		Logger::log("Parse AAsset Found File: %s", (path+name).c_str());
 		OSFileEntry entry = OSFileEntry(path + name, OSFileEntry::TYPE_FILE);
 		targetVector.push_back(entry);
 	}

+ 23 - 16
src/core/PolyAndroidCore.cpp

@@ -21,13 +21,12 @@
  */
 
 #include "polycode/core/PolyAndroidCore.h"
-// #include "polycode/core/PolyBasicFileProvider.h"
 #include "polycode/core/PolyOpenGLGraphicsInterface.h"
 #include "polycode/core/PolyAAssetFileProvider.h"
 #include "polycode/core/PolyLogger.h"
+#include "polycode/core/PolyResourceManager.h"
 #include "polycode/view/android/PolycodeView.h"
 
-// #include <EGL/egl.h>
 #include <dirent.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -56,6 +55,8 @@ AndroidCore::AndroidCore(PolycodeView *view, int xRes, int yRes, bool fullScreen
 	renderer->setGraphicsInterface(this, graphicsInterface);
 	services->setRenderer(renderer);
 	
+	context = NULL;
+	surface = NULL;
 	recreateContext = true;
 	setVideoMode(xRes, yRes, fullScreen, vSync, aaLevel, anisotropyLevel, retinaSupport);
 	//services->getSoundManager()->setAudioInterface(new XAudio2AudioInterface());
@@ -147,13 +148,13 @@ void AndroidCore::checkEvents() {
 
 bool AndroidCore::systemUpdate() {
 	if (!running) {
-		Logger::log("not running...");
 		return false;
 	}
 	doSleep();
-	updateCore();
 
+	updateCore();
 	checkEvents();
+
 	return running;
 }
 
@@ -224,7 +225,6 @@ String AndroidCore::saveFilePicker(std::vector<CoreFileExtension> extensions) {
 }
 
 void AndroidCore::handleVideoModeChange(VideoModeChangeInfo *modeInfo) {
-	Logger::log("handleVideoModeChange");
 	int32_t success = 0;
 	EGLBoolean result;
 	EGLint num_config;
@@ -237,10 +237,8 @@ void AndroidCore::handleVideoModeChange(VideoModeChangeInfo *modeInfo) {
 	this->anisotropyLevel = modeInfo->anisotropyLevel;
 
 	if(!view->native_window){
-		Logger::log("handleVideoModeChange2");
 		return;
 	}
-	Logger::log("handleVideoModeChange3");
 	
 	if (recreateContext){
 		eglMutex->lock();
@@ -262,21 +260,26 @@ void AndroidCore::handleVideoModeChange(VideoModeChangeInfo *modeInfo) {
 
 		EGLConfig config;
 		EGLint format;
-		Logger::log("handleVideoModeChange4");
+		
 		display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
 		result = eglInitialize(display, NULL, NULL);
+		
 		result = eglChooseConfig(display, attribute_list, &config, 1, &num_config);
 		assert(EGL_FALSE != result);
-		Logger::log("Config chosen");
 		
 		if (eglQueryAPI() == EGL_NONE){
 			result = eglBindAPI(EGL_OPENGL_ES_API);
 			assert(EGL_FALSE != result);
 		}
-	
-		context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attributes);
-		assert(context!=EGL_NO_CONTEXT);
-
+		
+		if(!context){
+			context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attributes);
+			assert(context!=EGL_NO_CONTEXT);
+		}
+		
+		if(surface)
+			eglDestroySurface(display, surface);
+		
 		surface = eglCreateWindowSurface( display, config, view->native_window, NULL );
 		assert(surface != EGL_NO_SURFACE);
 
@@ -297,7 +300,7 @@ void AndroidCore::flushRenderContext() {
 
 bool AndroidCore::isWindowInitialized(){
 	eglMutex->lock();
-	if (eglGetCurrentContext() == EGL_NO_CONTEXT){
+	if (eglGetCurrentContext() == EGL_NO_CONTEXT || recreateContext){
 		eglMutex->unlock();
 		return false;
 	} else {
@@ -306,14 +309,18 @@ bool AndroidCore::isWindowInitialized(){
 	}
 }
 
+CoreMutex* AndroidCore::getEGLMutex(){
+	return eglMutex;
+}
+
 void AndroidCore::openURL(String url) {
 
 }
 
 unsigned int AndroidCore::getTicks() {
 	struct timespec now;
-	clock_gettime(CLOCK_MONOTONIC, &now);
-	return now.tv_sec*1000000000LL + now.tv_nsec;
+	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &now);
+	return now.tv_sec * 1000000L + now.tv_nsec/1000;
 }
 
 String AndroidCore::executeExternalCommand(String command, String args, String inDirectory) {

+ 2 - 2
src/core/PolyOpenGLGraphicsInterface.cpp

@@ -735,6 +735,7 @@ void OpenGLGraphicsInterface::createShader(Shader *shader) {
 	
 	if(!shader->platformData) {
 		shader->platformData = (void*) new GLuint;
+		*((GLuint*)shader->platformData) = 0;
 	}
 	
 	GLuint shaderID = *((GLuint*)shader->platformData);
@@ -750,7 +751,6 @@ void OpenGLGraphicsInterface::createShader(Shader *shader) {
 	glAttachShader(shaderID, *((GLuint*)shader->fragmentProgram->platformData));
 	glAttachShader(shaderID, *((GLuint*)shader->vertexProgram->platformData));
 	glLinkProgram(shaderID);
-// 	Logger::log("createShader6");
 
 	GLint result;
 	glGetProgramiv( shaderID, GL_LINK_STATUS, &result);
@@ -758,7 +758,7 @@ void OpenGLGraphicsInterface::createShader(Shader *shader) {
 	if(result == GL_INVALID_VALUE || result == GL_INVALID_OPERATION) {
 		Services()->getLogger()->logBroadcast("ERROR: Error linking shader. Invalid shader program.");
 	}
-// 	Logger::log("createShader8");
+
 	int total = -1;
 	glGetProgramiv( shaderID, GL_ACTIVE_UNIFORMS, &total );
 	for(int i=0; i < total; i++)  {

+ 5 - 2
src/core/PolyRenderer.cpp

@@ -332,9 +332,10 @@ void RenderThread::clearFrameQueue() {
 
 void RenderThread::processJob(const RendererThreadJob &job) {
 	lockRenderMutex();
-	if (!core->isWindowInitialized() && job.jobType != JOB_REQUEST_CONTEXT_CHANGE) {
+#if PLATFORM == PLATFORM_ANDROID
+	if(!core->isWindowInitialized() && job.jobType != JOB_REQUEST_CONTEXT_CHANGE){
 		jobQueue.push(job);
-		for (int i = 0; i < jobQueue.size() - 1; i++) {
+		for(int i = 0; i < jobQueue.size()-1; i++){
 			RendererThreadJob fJob = jobQueue.front();
 			if (fJob.jobType != JOB_REQUEST_CONTEXT_CHANGE) {
 				jobQueue.push(fJob);
@@ -346,6 +347,8 @@ void RenderThread::processJob(const RendererThreadJob &job) {
 		}
 		return;
 	}
+	#endif
+
 	switch(job.jobType) {
 		case JOB_REQUEST_CONTEXT_CHANGE:
 		{

+ 2 - 1
src/core/PolyResourceManager.cpp

@@ -250,12 +250,13 @@ std::vector<Resource*> ResourcePool::getResources(int resourceType) {
 }
 
 Resource *ResourcePool::getResource(int resourceType, const String& resourceName) const {
+
 	for(int i =0; i < resources.size(); i++) {
 		if(resources[i]->getResourceName() == resourceName && resources[i]->getResourceType() == resourceType) {
 			return resources[i];
 		}
 	}
-	
+
 	if(resourceType == Resource::RESOURCE_TEXTURE && resourceName != "default/default.png") {
 		Logger::log("Texture [%s] not found in pool [%s], using default\n", resourceName.c_str(), name.c_str());
 		return getResource(Resource::RESOURCE_TEXTURE, "default/default.png");

+ 0 - 6
src/core/PolySDLCore.cpp

@@ -161,12 +161,6 @@ SDLCore::SDLCore(PolycodeView *view, int _xRes, int _yRes, bool fullScreen, bool
 #endif // USE_X11
 }
 
-void SDLCore::setVideoMode(int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, bool retinaSupport) {
-	//renderer->Resize(xRes, yRes);
-	//CoreServices::getInstance()->getMaterialManager()->reloadProgramsAndTextures();
-	//dispatchEvent(new Event(), EVENT_CORE_RESIZE);
-	Core::setVideoMode(xRes, yRes, fullScreen, vSync, aaLevel, anisotropyLevel, retinaSupport);
-}
 
 void SDLCore::handleVideoModeChange(VideoModeChangeInfo* modeInfo){
 

+ 10 - 4
src/view/android/PolycodeView.cpp

@@ -22,6 +22,7 @@
 
 #include "polycode/view/android/PolycodeView.h"
 #include "polycode/core/PolyLogger.h"
+#include "polycode/core/PolyRenderer.h"
 #include <android/looper.h>
 
 using namespace Polycode;
@@ -114,13 +115,15 @@ void onNativeWindowCreated(ANativeActivity* activity, ANativeWindow *window){
 	int height = ANativeWindow_getHeight(window);
 	if (width < 0)
 		width = 100;
-	if (height)
+	if (height < 0)
 		height = 100;
 	if(core){
 		core->recreateContext = true;
-		core->setVideoMode(width, height, true, false, core->getAALevel(), 0, (core->getBackingXRes() > core->getXRes()));
+ 		core->setDeviceSize(width, height);
+		//Logger::log("Width: %d, Height; %d", width, height);
+		core->getEGLMutex()->unlock();
+		core->setVideoMode(width, height, true, false, core->getAALevel(), 0, false);
 	}
-	Logger::log("core exists");
 }
 
 void onNativeWindowResized(ANativeActivity* activity, ANativeWindow *window){
@@ -134,8 +137,11 @@ void onNativeWindowRedrawNeeded(ANativeActivity* activity, ANativeWindow *window
 void onNativeWindowDestroyed(ANativeActivity* activity, ANativeWindow *window){
 	Logger::log("onNativeWindowDestroyed");
 	((PolycodeView*)activity->instance)->native_window = NULL;
-	if (core)
+	if (core){
+		core->getEGLMutex()->lock();
 		core->recreateContext = true;
+		Services()->getRenderer()->getRenderThread()->getFrameInfo();
+	}
 }
 
 void onInputQueueCreated(ANativeActivity* activity, AInputQueue *queue){