From b27ca380b0b6b27fb3f5a6359e7b33f3f5a40ded Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Sun, 13 Sep 2015 20:51:55 -0400 Subject: [PATCH] lib/gamnit: intro App integration and Android support MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- lib/gamnit/display.nit | 1 + lib/gamnit/display_android.nit | 47 ++++++++++++++++++++++++++++++++++++++ lib/gamnit/gamnit.nit | 49 ++++++++++++++++++++++++++++++++++++++++ lib/gamnit/gamnit_android.nit | 24 ++++++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 lib/gamnit/display_android.nit create mode 100644 lib/gamnit/gamnit_android.nit diff --git a/lib/gamnit/display.nit b/lib/gamnit/display.nit index 6f1fd53..94ece8a 100644 --- a/lib/gamnit/display.nit +++ b/lib/gamnit/display.nit @@ -18,6 +18,7 @@ module display import ::glesv2 import display_linux is conditional(linux) +import display_android is conditional(android) # Should Gamnit be more verbose? fun debug_gamnit: Bool do return false diff --git a/lib/gamnit/display_android.nit b/lib/gamnit/display_android.nit new file mode 100644 index 0000000..d0e5235 --- /dev/null +++ b/lib/gamnit/display_android.nit @@ -0,0 +1,47 @@ +# This file is part of NIT (http://www.nitlanguage.org). +# +# 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. + +# Gamnit display implementation for Android +# +# Generated APK files require OpenGL ES 2.0. +# +# This modules uses `android::native_app_glue` and the Android NDK. +module display_android is + android_manifest """""" +end + +import ::android + +private import gamnit::egl + +redef class GamnitDisplay + + redef fun setup + do + var native_display = egl_default_display + var native_window = app.native_app_glue.window + + setup_egl_display native_display + + # We need 8 bits per color for selection by color + select_egl_config(8, 8, 8, 0, 8, 0, 0) + + var format = egl_config.attribs(egl_display).native_visual_id + native_window.set_buffers_geometry(0, 0, format) + + setup_egl_context native_window + end + + redef fun close do close_egl +end diff --git a/lib/gamnit/gamnit.nit b/lib/gamnit/gamnit.nit index 8def2e9..43ec95c 100644 --- a/lib/gamnit/gamnit.nit +++ b/lib/gamnit/gamnit.nit @@ -15,4 +15,53 @@ # Game and multimedia framework for Nit module gamnit +import app + import display + +import gamnit_android is conditional(android) + +redef class App + + # Main `GamnitDisplay` initialized by `on_create` + var display: nullable GamnitDisplay = null + + redef fun on_create + do + super + + var display = new GamnitDisplay + display.setup + self.display = display + end + + # Core of the frame logic, executed only when the display is visible + # + # This method should be redefined by user modules to customize the behavior of the game. + protected fun frame_core do end + + # Full frame logic, executed even if the display is not visible + # + # This method wraps `frame_core` and other services to be executed in the main app loop. + # + # To customize the behavior on each turn, it is preferable to redefined `frame_core`. + # Still, `frame_full` can be redefined with care for more control. + protected fun frame_full + do + var display = display + if display != null then frame_core + + feed_events + end + + redef fun run + do + # TODO manage exit condition + loop frame_full + end + + # Loop on available events and feed them back to the app + # + # The implementation varies per platform. + private fun feed_events do end +end diff --git a/lib/gamnit/gamnit_android.nit b/lib/gamnit/gamnit_android.nit new file mode 100644 index 0000000..647c741 --- /dev/null +++ b/lib/gamnit/gamnit_android.nit @@ -0,0 +1,24 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# 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. + +# Support services for Gamnit on Android +module gamnit_android + +import android + +intrude import gamnit + +redef class App + redef fun feed_events do app.poll_looper 0 +end -- 1.7.9.5