source: trunk/src/platform/android/androidapp.cpp @ 1964

Last change on this file since 1964 was 1964, checked in by sam, 7 years ago

android: use the FPS value provided by the application; we now run at
60 fps on Android devices, too.

  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#if defined __ANDROID__
16
17#include <jni.h>
18#include <android/log.h>
19
20#include "core.h"
21#include "loldebug.h"
22#include "androidapp.h"
23
24using namespace lol;
25
26/* One of these wrappers will be overridden by the user's version */
27void lol_android_main(void) __attribute__((weak));
28void lol_android_main(int argc, char **argv) __attribute__((weak));
29void lol_android_main(int argc, char **argv, char **envp) __attribute__((weak));
30
31namespace lol
32{
33JavaVM *g_vm;
34jobject g_activity;
35Queue<int> g_main_queue;
36Thread *g_main_thread;
37float g_fps;
38
39AndroidApp::AndroidApp(char const *title, ivec2 res, float fps)
40  : m_data(0)
41{
42    g_fps = fps;
43}
44
45void AndroidApp::ShowPointer(bool show)
46{
47}
48
49/* This is a fake Run() method. We just wait until we're called and
50 * signal nativeInit() that all the user's initialisation code was
51 * called. Then we sit here forever, the Java layer is in charge of
52 * calling TickDraw(). */
53void AndroidApp::Run()
54{
55    g_main_queue.Push(1);
56    g_main_queue.Push(1);
57
58    while (!Ticker::Finished())
59    {
60        /* Do nothing while the real render thread does the job. The
61         * real stuff happens in nativeRender() */
62        Timer t;
63        t.Wait(0.5f);
64    }
65}
66
67void *AndroidApp::MainRun(void *data)
68{
69    int argc = 1;
70    char *argv[] = { "", NULL };
71    char *env[] = { NULL };
72
73    /* Call the user's main() function. One of these will work. */
74    lol_android_main();
75    lol_android_main(argc, argv);
76    lol_android_main(argc, argv, env);
77
78    return NULL;
79}
80
81AndroidApp::~AndroidApp()
82{
83}
84
85};
86
87extern "C" jint
88JNI_OnLoad(JavaVM* vm, void* reserved)
89{
90    Log::Info("Java layer loading library, vm=0x%08lx", (long)(intptr_t)vm);
91    g_vm = vm;
92    return JNI_VERSION_1_4;
93}
94
95extern "C" void
96Java_org_zoy_LolEngine_LolActivity_nativeInit(JNIEnv* env, jobject thiz)
97{
98    Log::Info("Java layer initialising activity 0x%08lx", (long)thiz);
99    env->NewGlobalRef(thiz); /* FIXME: never released! */
100    g_activity = thiz;
101}
102
103extern "C" void
104Java_org_zoy_LolEngine_LolRenderer_nativeInit(JNIEnv* env)
105{
106    /* Initialise app thread and wait for it to be ready, ie. set
107     * the FPS value at least. */
108    g_main_thread = new Thread(lol::AndroidApp::MainRun, NULL);;
109    g_main_queue.Pop();
110
111    /* Launch our ticker */
112    Log::Info("Java layer initialising renderer at %g fps", g_fps);
113    Ticker::Setup(g_fps);
114    Video::Setup(ivec2(320, 200));
115
116    /* Wake up app thread */
117    g_main_queue.Pop();
118}
119
120extern "C" void
121Java_org_zoy_LolEngine_LolRenderer_nativeResize(JNIEnv* env, jobject thiz,
122                                                jint w, jint h)
123{
124    Log::Info("Java layer resizing to %i x %i", w, h);
125    Video::Setup(ivec2(w, h));
126}
127
128extern "C" void
129Java_org_zoy_LolEngine_LolRenderer_nativeDone(JNIEnv* env)
130{
131    /* FIXME: clean up */
132    delete g_main_thread;
133}
134
135extern "C" void
136Java_org_zoy_LolEngine_LolView_nativePause(JNIEnv* env)
137{
138    /* TODO: unimplemented */
139}
140
141extern "C" void
142Java_org_zoy_LolEngine_LolView_nativeDown(JNIEnv* env)
143{
144    Input::SetMouseButton(0);
145}
146
147extern "C" void
148Java_org_zoy_LolEngine_LolView_nativeUp(JNIEnv* env)
149{
150    Input::UnsetMouseButton(0);
151}
152
153extern "C" void
154Java_org_zoy_LolEngine_LolView_nativeMove(JNIEnv* env, jobject thiz,
155                                          jint x, jint y)
156{
157    Input::SetMousePos(ivec2(x, y));
158}
159
160/* Call to render the next GL frame */
161extern "C" void
162Java_org_zoy_LolEngine_LolRenderer_nativeRender(JNIEnv* env)
163{
164    Ticker::TickDraw();
165}
166
167/*
168 * Fake main() wrappers that let us call the user’s main() from within
169 * a separate thread.
170 */
171void lol_android_main(void) {}
172void lol_android_main(int argc, char **argv)
173{
174    (void)argc;
175    (void)argv;
176}
177void lol_android_main(int argc, char **argv, char **envp)
178{
179    (void)argc;
180    (void)argv;
181    (void)envp;
182}
183
184#endif /* __ANDROID__ */
185
Note: See TracBrowser for help on using the repository browser.