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

Last change on this file since 1944 was 1944, checked in by sam, 10 years ago

android: fix a synchronisation issue between Java and the game thread.

  • Property svn:keywords set to Id
File size: 3.7 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(void) {}
29void lol_android_main(int argc, char **argv) __attribute__((weak));
30void lol_android_main(int argc, char **argv) {}
31void lol_android_main(int argc, char **argv, char **envp) __attribute__((weak));
32void lol_android_main(int argc, char **argv, char **envp) {}
33
34namespace lol
35{
36JavaVM *g_vm;
37jobject g_activity;
38Queue<int> g_main_queue;
39Thread *g_main_thread;
40
41AndroidApp::AndroidApp(char const *title, ivec2 res, float fps)
42  : data(0)
43{
44}
45
46void AndroidApp::ShowPointer(bool show)
47{
48}
49
50/* This is a fake Run() method. We just wait until we're called and
51 * signal nativeInit() that all the user's initialisation code was
52 * called. Then we sit here forever, the Java layer is in charge of
53 * calling TickDraw(). */
54void AndroidApp::Run()
55{
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    g_vm = vm;
91    return JNI_VERSION_1_4;
92}
93
94extern "C" void
95Java_org_zoy_LolEngine_LolActivity_nativeInit(JNIEnv* env, jobject thiz)
96{
97    Log::Info("Java layer initialising activity");
98    env->NewGlobalRef(thiz); /* FIXME: never released! */
99    g_activity = thiz;
100}
101
102extern "C" void
103Java_org_zoy_LolEngine_LolRenderer_nativeInit(JNIEnv* env)
104{
105    Log::Info("Java layer initialising renderer");
106    Ticker::Setup(30.0f);
107    Video::Setup(ivec2(320, 200));
108
109    g_main_thread = new Thread(lol::AndroidApp::MainRun, NULL);;
110    g_main_queue.Pop();
111}
112
113extern "C" void
114Java_org_zoy_LolEngine_LolRenderer_nativeResize(JNIEnv* env, jobject thiz,
115                                                jint w, jint h)
116{
117    Log::Info("Java layer resizing to %i x %i", w, h);
118    Video::Setup(ivec2(w, h));
119}
120
121extern "C" void
122Java_org_zoy_LolEngine_LolRenderer_nativeDone(JNIEnv* env)
123{
124    /* FIXME: clean up */
125    delete g_main_thread;
126}
127
128extern "C" void
129Java_org_zoy_LolEngine_LolView_nativePause(JNIEnv* env)
130{
131    /* TODO: unimplemented */
132}
133
134extern "C" void
135Java_org_zoy_LolEngine_LolView_nativeDown(JNIEnv* env)
136{
137    Input::SetMouseButton(0);
138}
139
140extern "C" void
141Java_org_zoy_LolEngine_LolView_nativeUp(JNIEnv* env)
142{
143    Input::UnsetMouseButton(0);
144}
145
146extern "C" void
147Java_org_zoy_LolEngine_LolView_nativeMove(JNIEnv* env, jobject thiz,
148                                          jint x, jint y)
149{
150    ivec2 pos = ivec2(0, 479) + ivec2(x * 640, -y * 480) / Video::GetSize();
151    Input::SetMousePos(pos);
152}
153
154/* Call to render the next GL frame */
155extern "C" void
156Java_org_zoy_LolEngine_LolRenderer_nativeRender(JNIEnv* env)
157{
158    Ticker::TickDraw();
159}
160
161#endif /* __ANDROID__ */
162
Note: See TracBrowser for help on using the repository browser.