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

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

base: implement UNUSED() macro and put it here and there.

  • 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://www.wtfpl.net/ 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 Tick() 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::Tick()
54{
55    static int init = 0;
56    if (!init)
57    {
58        init = 1;
59        g_main_queue.Push(1);
60        g_main_queue.Push(1);
61    }
62
63    /* Do nothing while the real render thread does the job. The
64     * real stuff happens in nativeRender() */
65    Timer t;
66    t.Wait(0.5f);
67}
68
69void *AndroidApp::MainRun(void *data)
70{
71    int argc = 1;
72    char *argv[] = { "", NULL };
73    char *env[] = { NULL };
74
75    /* Call the user's main() function. One of these will work. */
76    lol_android_main();
77    lol_android_main(argc, argv);
78    lol_android_main(argc, argv, env);
79
80    return NULL;
81}
82
83AndroidApp::~AndroidApp()
84{
85}
86
87};
88
89extern "C" jint
90JNI_OnLoad(JavaVM* vm, void* reserved)
91{
92    Log::Info("Java layer loading library, vm=0x%08lx", (long)(intptr_t)vm);
93    g_vm = vm;
94    return JNI_VERSION_1_4;
95}
96
97extern "C" void
98Java_org_zoy_LolEngine_LolActivity_nativeInit(JNIEnv* env, jobject thiz)
99{
100    Log::Info("Java layer initialising activity 0x%08lx", (long)thiz);
101    env->NewGlobalRef(thiz); /* FIXME: never released! */
102    g_activity = thiz;
103}
104
105extern "C" void
106Java_org_zoy_LolEngine_LolRenderer_nativeInit(JNIEnv* env)
107{
108    /* Initialise app thread and wait for it to be ready, ie. set
109     * the FPS value at least. */
110    g_main_thread = new Thread(lol::AndroidApp::MainRun, NULL);;
111    g_main_queue.Pop();
112
113    /* Launch our ticker */
114    Log::Info("Java layer initialising renderer at %g fps", g_fps);
115    Ticker::Setup(g_fps);
116    Video::Setup(ivec2(320, 200));
117
118    /* Wake up app thread */
119    g_main_queue.Pop();
120}
121
122extern "C" void
123Java_org_zoy_LolEngine_LolRenderer_nativeResize(JNIEnv* env, jobject thiz,
124                                                jint w, jint h)
125{
126    Log::Info("Java layer resizing to %i x %i", w, h);
127    Video::Setup(ivec2(w, h));
128}
129
130extern "C" void
131Java_org_zoy_LolEngine_LolRenderer_nativeDone(JNIEnv* env)
132{
133    /* FIXME: clean up */
134    delete g_main_thread;
135}
136
137extern "C" void
138Java_org_zoy_LolEngine_LolView_nativePause(JNIEnv* env)
139{
140    /* TODO: unimplemented */
141}
142
143extern "C" void
144Java_org_zoy_LolEngine_LolView_nativeDown(JNIEnv* env)
145{
146    Input::SetMouseButton(0);
147}
148
149extern "C" void
150Java_org_zoy_LolEngine_LolView_nativeUp(JNIEnv* env)
151{
152    Input::UnsetMouseButton(0);
153}
154
155extern "C" void
156Java_org_zoy_LolEngine_LolView_nativeMove(JNIEnv* env, jobject thiz,
157                                          jint x, jint y)
158{
159    Input::SetMousePos(ivec2(x, y));
160}
161
162/* Call to render the next GL frame */
163extern "C" void
164Java_org_zoy_LolEngine_LolRenderer_nativeRender(JNIEnv* env)
165{
166    Ticker::TickDraw();
167}
168
169/*
170 * Fake main() wrappers that let us call the user’s main() from within
171 * a separate thread.
172 */
173void lol_android_main(void) {}
174void lol_android_main(int argc, char **argv)
175{
176    UNUSED(argc, argv);
177}
178void lol_android_main(int argc, char **argv, char **envp)
179{
180    UNUSED(argc, argv, envp);
181}
182
183#endif /* __ANDROID__ */
184
Note: See TracBrowser for help on using the repository browser.