Skip to content

Commit

Permalink
Initializes moui view for Android.
Browse files Browse the repository at this point in the history
The initialization for Android is much like the same one for iOS. However, some method implementations are not done yet for Android such as the NativeView::SetBounds() method.

The OpenGL view is implemented as Java class and can be instantiated by the View class through JNI. To setup the moui environment for Android. The activity on the Java side must call a native method to initializes the Application class.

Signed-off-by: Olli Wang <[email protected]>
  • Loading branch information
olliwang committed Feb 22, 2014
1 parent d884727 commit 52701a3
Show file tree
Hide file tree
Showing 18 changed files with 511 additions and 19 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,13 @@
# Build
build
out
*.mk
*.xcodeproj

# Android
android/bin
android/gen
android/libs
android/build.xml
android/proguard-project.txt
android/local.properties
10 changes: 10 additions & 0 deletions android/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ollix.moui"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk android:minSdkVersion="8" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" />

</manifest>
29 changes: 29 additions & 0 deletions android/jni/jni.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2014 Ollix
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
{
'includes': [
'../../moui/common.gypi',
],
'targets': [
{
'target_name': 'libmoui-jni',
'type': 'shared_library',
'sources': [
'renderer.cc',
],
'dependencies': [ '../../moui/moui.gyp:libmoui' ],
'export_dependent_settings': [ '../../moui/moui.gyp:libmoui' ],
}
]
}
33 changes: 33 additions & 0 deletions android/jni/renderer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2014 Ollix. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ---
// Author: [email protected] (Olli Wang)

#include "jni.h"
#include "moui/moui.h"

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT void JNICALL Java_com_ollix_moui_OpenGLRenderer_drawFrame
(JNIEnv* env, jobject obj, jlong moui_view_pointer) {
moui::View* view = (moui::View*)moui_view_pointer;
view->Render();
}

#ifdef __cplusplus
}
#endif
15 changes: 15 additions & 0 deletions android/project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

android.library=true
# Project target.
target=android-19
46 changes: 46 additions & 0 deletions android/src/com/ollix/moui/OpenGLRenderer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2014 Ollix. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ---
// Author: [email protected] (Olli Wang)

package com.ollix.moui;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class OpenGLRenderer implements GLSurfaceView.Renderer {

private long mMouiViewPointer;
private native void drawFrame(long mouiViewPointer);

public OpenGLRenderer(long mouiViewPointer) {
mMouiViewPointer = mouiViewPointer;
}

@Override
public void onDrawFrame(GL10 unused) {
drawFrame(mMouiViewPointer);
}

@Override
public void onSurfaceCreated(GL10 unused, EGLConfig config) {}

@Override
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
}
}
37 changes: 37 additions & 0 deletions android/src/com/ollix/moui/OpenGLView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2014 Ollix. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ---
// Author: [email protected] (Olli Wang)

package com.ollix.moui;

import com.ollix.moui.OpenGLRenderer;
import android.content.Context;
import android.graphics.PixelFormat;
import android.opengl.GLSurfaceView;

public class OpenGLView extends GLSurfaceView {
public OpenGLView(Context context, long mouiViewPointer) {
super(context);

setEGLContextClientVersion(2); // adopts OpenGL ES 2.0 context
setZOrderOnTop(true);
setEGLConfigChooser(8, 8, 8, 8, 16, 0);
getHolder().setFormat(PixelFormat.TRANSLUCENT);

setRenderer(new OpenGLRenderer(mouiViewPointer));
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
}
61 changes: 61 additions & 0 deletions moui/core/android/application.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) 2014 Ollix. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ---
// Author: [email protected] (Olli Wang)

#include "moui/core/application.h"

#include "jni.h"

namespace {

JavaVM* java_vm = nullptr;
jobject main_activity = nullptr;
moui::Application* shared_application = nullptr;

} // namespace

namespace moui {

Application* Application::SharedApplication() {
if (shared_application == nullptr)
shared_application = new Application();
return shared_application;
}

Application::Application() {
}

Application::~Application() {
}

void Application::Init(JNIEnv* env, jobject activity) {
if (main_activity != nullptr)
env->DeleteGlobalRef(main_activity);
main_activity = reinterpret_cast<jobject>(env->NewGlobalRef(activity));
env->GetJavaVM(&java_vm);
}

JNIEnv* Application::GetJNIEnv() const {
JNIEnv* env;
java_vm->GetEnv((void **)&env, JNI_VERSION_1_6);
return env;
}

jobject Application::GetMainActivity() const {
return main_activity;
}

} // namespace moui
45 changes: 45 additions & 0 deletions moui/core/android/application.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2014 Ollix. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ---
// Author: [email protected] (Olli Wang)

#ifndef MOUI_CORE_ANDROID_APPLICATION_H_
#define MOUI_CORE_ANDROID_APPLICATION_H_

#include "jni.h"
#include "moui/base.h"

namespace moui {

class Application {
public:
static Application* SharedApplication();

Application();
~Application();

void Init(JNIEnv* env, jobject activity);

JNIEnv* GetJNIEnv() const;

jobject GetMainActivity() const;

private:
DISALLOW_COPY_AND_ASSIGN(Application);
};

} // namespace moui

#endif // MOUI_CORE_ANDROID_APPLICATION_H_
25 changes: 25 additions & 0 deletions moui/core/application.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2014 Ollix. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ---
// Author: [email protected] (Olli Wang)

#ifndef MOUI_CORE_APPLICATION_H_
#define MOUI_CORE_APPLICATION_H_

#if MOUI_ANDROID
#include "moui/core/android/application.h"
#endif

#endif // MOUI_CORE_APPLICATION_H_
37 changes: 24 additions & 13 deletions moui/moui.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,31 @@
'include_dirs': [ '..' ],
'conditions': [
['OS=="android"', {
'defines': [ 'MOUI_ANDROID_PLATFORM' ],
'defines': [ 'MOUI_ANDROID' ],
}],
['OS=="ios"', {
'defines': [ 'MOUI_IOS_PLATFORM' ],
'defines': [ 'MOUI_IOS' ],
}],
['OS=="mac"', {
'defines': [ 'MOUI_MAC_PLATFORM' ],
'defines': [ 'MOUI_MAC' ],
}],
['OS=="win"', {
'defines': [ 'MOUI_WINDOWS_PLATFORM' ],
'defines': [ 'MOUI_WINDOWS' ],
}],
], # conditions
},
'conditions': [
['OS=="android"', {
'defines': [ 'MOUI_ANDROID_PLATFORM' ],
'sources': [
'core/android/application.cc',
'ui/android/native_view.cc',
'ui/android/view.cc',
],
'defines': [ 'MOUI_ANDROID' ],
'ldflags': [ '-lGLESv2' ],
'direct_dependent_settings': {
'ldflags': [ '-lGLESv2' ],
},
}],
['OS=="ios"', {
'sources': [
Expand All @@ -52,19 +61,21 @@
'ui/ios/native_view.mm',
'ui/ios/view.mm',
],
'defines': [ 'MOUI_IOS_PLATFORM' ],
'libraries': [
'$(SDKROOT)/System/Library/Frameworks/Foundation.framework',
'$(SDKROOT)/System/Library/Frameworks/UIKit.framework',
],
'defines': [ 'MOUI_IOS' ],
'link_settings': {
'libraries': [
'$(SDKROOT)/System/Library/Frameworks/Foundation.framework',
'$(SDKROOT)/System/Library/Frameworks/UIKit.framework',
],
},
}],
['OS=="mac"', {
'defines': [ 'MOUI_MAC_PLATFORM' ],
'defines': [ 'MOUI_MAC' ],
}],
['OS=="win"', {
'defines': [ 'MOUI_WINDOWS_PLATFORM' ],
'defines': [ 'MOUI_WINDOWS' ],
}],
], # conditions
} # libmoui
}, # libmoui target
], # targets
}
Loading

0 comments on commit 52701a3

Please sign in to comment.