Subversion Repositories eduke32

Rev

Rev 4433 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4386 terminx 1
/*
2
 *
3
 * Designed by Emile Belanger
4
 *
5
 */
6
 
7
#include <stdlib.h>
8
#include <stdio.h>
9
#include <string.h>
10
#include <jni.h>
11
#include <android/log.h>
12
 
13
#include "SDL_scancode.h"
14
 
15
#include "TouchControlsContainer.h"
16
extern "C"
17
{
18
 
19
//This is a new function I put into SDL2, file SDL_androidgl.c
20
extern void SDL_SetSwapBufferCallBack(void (*pt2Func)(void));
21
 
22
#include "in_android.h"
23
 
24
#include "../GL/gl.h"
25
#include "../GL/nano_gl.h"
26
 
27
#ifndef LOGI
28
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO,"DUKE", __VA_ARGS__))
29
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "DUKE", __VA_ARGS__))
30
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR,"DUKE", __VA_ARGS__))
31
#endif
32
 
33
#define REND_SOFT  0
34
#define REND_GL    1
35
 
36
static int curRenderer;
37
 
38
int android_screen_width;
39
int android_screen_height;
40
 
41
 
42
#define KEY_QUICK_CMD    0x1005
43
#define KEY_SHOW_KBRD    0x1008
44
#define KEY_SHOW_INVEN    0x1009
45
#define KEY_QUICK_SAVE    0x100A
46
#define KEY_QUICK_LOAD    0x100B
47
#define KEY_SNIPER        0x100C
48
 
49
#define KEY_QUICK_KEY1    0x1011
50
#define KEY_QUICK_KEY2    0x1012
51
#define KEY_QUICK_KEY3    0x1013
52
#define KEY_QUICK_KEY4    0x1014
53
 
54
 
55
float gameControlsAlpha = 0.5;
56
bool showWeaponCycle = false;
57
bool turnMouseMode = true;
58
bool invertLook = false;
59
bool precisionShoot = false;
60
bool showSticks = true;
61
bool hideTouchControls = true;
62
 
63
bool shooting = false;
64
bool sniperMode = false;
65
 
66
static int controlsCreated = 0;
67
touchcontrols::TouchControlsContainer controlsContainer;
68
 
69
touchcontrols::TouchControls *tcMenuMain=0;
70
touchcontrols::TouchControls *tcGameMain=0;
71
touchcontrols::TouchControls *tcGameWeapons=0;
72
touchcontrols::TouchControls *tcAutomap=0;
73
touchcontrols::TouchControls *tcInventory=0;
74
touchcontrols::TouchControls *tcGameLook=0; //Just used for whole screen look mode
75
 
76
 
77
touchcontrols::TouchJoy *touchJoyLeft;
78
touchcontrols::TouchJoy *touchJoyRight;
79
touchcontrols::WheelSelect *weaponWheel;
80
 
81
extern JNIEnv* env_;
82
 
83
void openGLStart()
84
{
85
 
86
        if (curRenderer == REND_GL)
87
        {
88
                glPushMatrix();
89
 
90
                glMatrixMode(GL_PROJECTION);
91
                glLoadIdentity();
92
                glOrthof (0, android_screen_width, android_screen_height, 0, 0, 1);
93
 
94
                //glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
95
                //glClear(GL_COLOR_BUFFER_BIT);
96
                //LOGI("openGLStart");
97
                glDisable(GL_ALPHA_TEST);
98
                glDisable(GL_DEPTH_TEST);
99
                glDisable(GL_FOG);
100
                glEnable(GL_TEXTURE_2D);
101
                glEnable (GL_BLEND);
102
 
103
                glColor4f(1,1,1,1);
104
 
105
                glDisableClientState(GL_COLOR_ARRAY);
106
                glEnableClientState(GL_VERTEX_ARRAY);
107
                glEnableClientState(GL_TEXTURE_COORD_ARRAY );
108
 
109
                glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
110
 
111
                glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
112
 
113
                glDisable(GL_CULL_FACE);
114
 
115
                glMatrixMode(GL_MODELVIEW);
116
 
117
                nanoPushState();
118
        }
119
        else //software mode
120
        {
121
 
122
                glDisable(GL_ALPHA_TEST);
123
                glDisableClientState(GL_COLOR_ARRAY);
124
                glEnableClientState(GL_VERTEX_ARRAY);
125
                glEnableClientState(GL_TEXTURE_COORD_ARRAY );
126
                glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
127
                glEnable (GL_BLEND);
128
                glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
129
                glEnable(GL_TEXTURE_2D);
130
                glDisable(GL_CULL_FACE);
131
                glMatrixMode(GL_MODELVIEW);
132
 
133
        }
134
 
135
}
136
 
137
void openGLEnd()
138
{
139
 
140
        if (curRenderer == REND_GL)
141
        {
142
                //glPopMatrix();
143
                nanoPopState();
144
                glPopMatrix();
145
        }
146
        else// Software mode
147
        {
148
                glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
149
                glMatrixMode(GL_MODELVIEW);
150
 
151
        }
152
}
153
 
154
void gameSettingsButton(int state)
155
{
156
 
157
        //LOGTOUCH("gameSettingsButton %d",state);
158
        if (state == 1)
159
        {
160
                jclass helloWorldClass;
161
                jmethodID mainMethod;
162
 
163
                helloWorldClass = env_->FindClass("com/beloko/duke/QuakeTouchControlsSettings");
164
                mainMethod = env_->GetStaticMethodID(helloWorldClass, "showSettings", "()V");
165
                env_->CallStaticVoidMethod(helloWorldClass, mainMethod);
166
        }
167
}
168
 
169
 
170
void showCustomCommands()
171
{
172
 
173
        jclass helloWorldClass;
174
        jmethodID mainMethod;
175
        helloWorldClass = env_->FindClass("com/beloko/duke/QuakeCustomCommands");
176
        mainMethod = env_->GetStaticMethodID(helloWorldClass, "showCommands", "()V");
177
        env_->CallStaticVoidMethod(helloWorldClass, mainMethod);
178
}
179
 
180
void toggleKeyboard()
181
{
182
        LOGI("toggleKeyboard");
183
        jclass helloWorldClass;
184
        jmethodID mainMethod;
185
        helloWorldClass = env_->FindClass("com/beloko/duke/ShowKeyboard");
186
        mainMethod = env_->GetStaticMethodID(helloWorldClass, "toggleKeyboard", "()V");
187
        env_->CallStaticVoidMethod(helloWorldClass, mainMethod);
188
}
189
 
190
//Because there is no Frame(), we need to check back to java each frame to see if the app hase paused
191
 
192
static jclass NativeLibClass = 0;
193
static jmethodID checkPauseMethod = 0;
194
void swapBuffers()
195
{
196
        if (NativeLibClass == 0)
197
        {
198
                NativeLibClass = env_->FindClass("com/beloko/duke/engine/NativeLib");
199
                checkPauseMethod = env_->GetStaticMethodID(NativeLibClass, "swapBuffers", "()V");
200
        }
201
        env_->CallStaticVoidMethod(NativeLibClass, checkPauseMethod);
202
}
203
 
204
 
205
void gameButton(int state,int code)
206
{
207
        if (code == gamefunc_Fire)
208
        {
209
                shooting = state;
210
                PortableAction(state, code);
211
        }
212
        if (code == KEY_SNIPER)
213
        {
214
                sniperMode = state;
215
        }
216
        else if  (code == KEY_SHOW_KBRD)
217
        {
218
                if (state)
219
                        toggleKeyboard();
220
 
221
                PortableKeyEvent(state, SDL_SCANCODE_GRAVE,0);
222
        }
223
        else if  (code == KEY_QUICK_CMD){
224
                if (state == 1)
225
                        showCustomCommands();
226
        }
227
        else if  (code == KEY_SHOW_INVEN){
228
                if (state == 1)
229
                {
230
                        if (!tcInventory->isEnabled())
231
                        {
232
                                tcInventory->setEnabled(true);
233
                                tcInventory->fade(0,5);
234
                                //disable weapon wheel so it does not show, enabled again after inventory cleared
235
                                tcGameWeapons->setEnabled(false);
236
                        }
237
                        else
238
                        {
239
                                tcInventory->setEnabled(false);
240
                                tcGameWeapons->setEnabled(true);
241
                        }
242
                }
243
        }
244
        else if (code == KEY_QUICK_SAVE){
245
                LOGI("QUICK SAVE");
246
                PortableKeyEvent(state, SDL_SCANCODE_F6,0);
247
        }
248
        else if (code == KEY_QUICK_LOAD){
249
                LOGI("QUICK LOAD");
250
                PortableKeyEvent(state, SDL_SCANCODE_F9,0);
251
        }
252
        else
253
                PortableAction(state, code);
254
}
255
 
256
void automapButton(int state,int code)
257
{
258
        PortableAction(state,code);
259
}
260
 
261
void inventoryButton(int state,int code)
262
{
263
        PortableAction(state,code);
264
        if (state == 0)
265
        {
266
                tcGameWeapons->setEnabled(true);
267
                tcInventory->setEnabled(false);
268
        }
269
 
270
}
271
 
272
void menuButton(int state,int code)
273
{
274
        PortableKeyEvent(state, code,code);
275
}
276
 
277
static int weaponWheelVisible = false;
278
void weaponWheelSelected(int enabled)
279
{
280
        if (enabled)
281
        {
282
                for (int n=0;n<10;n++)
283
                {
284
                        weaponWheel->setSegmentEnabled(n,(PortableGetWeapons() >> n) & 0x1);
285
                }
286
 
287
                tcGameWeapons->fade(0,5); //fade in
288
                weaponWheelVisible = true;
289
        }
290
        else
291
        {
292
                tcGameWeapons->fade(1,5);
293
                weaponWheelVisible = false;
294
        }
295
}
296
void weaponWheelChosen(int segment)
297
{
298
        LOGI("weaponWheel %d",segment);
299
        if (segment != -1)
300
        {
301
                PortableAction(1, gamefunc_Weapon_1 + segment);
302
                PortableAction(0, gamefunc_Weapon_1 + segment);
303
        }
304
        else //Nothing was selected
305
        {
306
                if (getLastWeapon() != -1)
307
                {
308
                        PortableAction(1, gamefunc_Weapon_1 + getLastWeapon());
309
                        PortableAction(0, gamefunc_Weapon_1 + getLastWeapon());
310
                }
311
        }
312
}
313
 
314
 
315
int left_double_action;
316
int right_double_action;
317
 
318
void left_double_tap(int state)
319
{
320
        //LOGTOUCH("L double %d",state);
321
        if (left_double_action)
322
                PortableAction(state,left_double_action);
323
}
324
 
325
void right_double_tap(int state)
326
{
327
        //LOGTOUCH("R double %d",state);
328
        if (right_double_action)
329
                PortableAction(state,right_double_action);
330
}
331
 
332
 
333
//To be set by android
334
float strafe_sens,forward_sens;
335
float pitch_sens,yaw_sens;
336
 
337
void left_stick(float joy_x, float joy_y,float mouse_x, float mouse_y)
338
{
339
        joy_x *=10;
340
        float strafe = joy_x*joy_x;
341
        //float strafe = joy_x;
342
        if (joy_x < 0)
343
                strafe *= -1;
344
 
345
        PortableMove(joy_y * 15 * forward_sens,-strafe * strafe_sens);
346
}
347
 
348
extern int crouchToggleState; //Set in anroid_in.c
349
 
350
void right_stick(float joy_x, float joy_y,float mouse_x, float mouse_y)
351
{
352
        //LOGI(" mouse x = %f",mouse_x);
353
        int invert = invertLook?-1:1;
354
 
355
        float scale = ((shooting && precisionShoot) || crouchToggleState)?0.3:1;
356
 
357
        PortableLookPitch(LOOK_MODE_MOUSE,-mouse_y  * pitch_sens * invert * scale);
358
 
359
        if (turnMouseMode)
360
                PortableLookYaw(LOOK_MODE_MOUSE,mouse_x*2*yaw_sens * scale);
361
        else
362
                PortableLookYaw(LOOK_MODE_JOYSTICK,joy_x*6*yaw_sens * scale);
363
}
364
 
365
void mouseMove(int action,float x, float y,float dx, float dy)
366
{
367
        LOGI(" mouse dx = %f",dx);
368
 
369
        if (weaponWheelVisible)
370
                return;
371
        /*
372
        if (abs(dx) < 0.001)
373
        {
374
                dx * 0.1;
375
        }
376
         */
377
        int invert = invertLook?-1:1;
378
        float scale;
379
 
380
        if (sniperMode)
381
                scale = 0.2;
382
        else
383
                scale = ((shooting && precisionShoot) || crouchToggleState)?0.2:1;
384
 
385
 
386
        PortableLookPitch(LOOK_MODE_MOUSE,-dy  * pitch_sens * invert * scale);
387
 
388
        PortableLookYaw(LOOK_MODE_MOUSE,dx*2*yaw_sens * scale);
389
}
390
 
391
void setHideSticks(bool v)
392
{
393
        if (touchJoyLeft) touchJoyLeft->setHideGraphics(v);
394
        if (touchJoyRight) touchJoyRight->setHideGraphics(v);
395
}
396
 
397
void touchSettingsButton(int state)
398
{
399
        //We wanna pause the game when doign settings
400
        if (state && !isPaused())
401
        {
402
                PortableKeyEvent(1,SDL_SCANCODE_PAUSE,0);
403
                PortableKeyEvent(0,SDL_SCANCODE_PAUSE,0);
404
        }
405
        else if (!state && isPaused())
406
        {
407
                PortableKeyEvent(1,SDL_SCANCODE_PAUSE,0);
408
                PortableKeyEvent(0,SDL_SCANCODE_PAUSE,0);
409
        }
410
}
411
 
412
void initControls(int width, int height,const char * graphics_path,const char *settings_file)
413
{
414
        touchcontrols::GLScaleWidth = (float)width;
415
        touchcontrols::GLScaleHeight = (float)height;
416
 
417
        LOGI("initControls %d x %d,x path = %s, settings = %s",width,height,graphics_path,settings_file);
418
 
419
        if (!controlsCreated)
420
        {
421
                LOGI("creating controls");
422
 
423
                touchcontrols::setGraphicsBasePath(graphics_path);
424
 
425
                controlsContainer.openGL_start.connect( sigc::ptr_fun(&openGLStart));
426
                controlsContainer.openGL_end.connect( sigc::ptr_fun(&openGLEnd));
427
                controlsContainer.signal_settings.connect( sigc::ptr_fun(&touchSettingsButton));
428
 
429
 
430
                tcMenuMain = new touchcontrols::TouchControls("menu",true,false);
431
                tcGameMain = new touchcontrols::TouchControls("game",false,true,1,true);
432
                tcGameWeapons = new touchcontrols::TouchControls("weapons",false,true,1,false);
433
                tcAutomap  = new touchcontrols::TouchControls("automap",false,true,1,false);
434
                tcInventory = new touchcontrols::TouchControls("inventory",false,true,1,false);
435
                tcGameLook = new touchcontrols::TouchControls("mouse",true,false);
436
                controlsContainer.dukeHack = 1;
437
 
438
                tcGameMain->signal_settingsButton.connect(  sigc::ptr_fun(&gameSettingsButton) );
439
 
440
                //Menu
441
                tcMenuMain->addControl(new touchcontrols::Button("down_arrow",touchcontrols::RectF(20,13,23,16),"arrow_down",SDL_SCANCODE_DOWN,true)); //Repeating buttons
442
                tcMenuMain->addControl(new touchcontrols::Button("up_arrow",touchcontrols::RectF(20,10,23,13),"arrow_up",SDL_SCANCODE_UP,true));
443
                tcMenuMain->addControl(new touchcontrols::Button("left_arrow",touchcontrols::RectF(17,13,20,16),"arrow_left",SDL_SCANCODE_LEFT,true));
444
                tcMenuMain->addControl(new touchcontrols::Button("right_arrow",touchcontrols::RectF(23,13,26,16),"arrow_right",SDL_SCANCODE_RIGHT,true));
445
                tcMenuMain->addControl(new touchcontrols::Button("enter",touchcontrols::RectF(0,12,4,16),"enter",SDL_SCANCODE_RETURN));
446
                tcMenuMain->addControl(new touchcontrols::Button("esc",touchcontrols::RectF(0,9,4,12),"esc",SDL_SCANCODE_ESCAPE));
447
 
448
 
449
                tcMenuMain->signal_button.connect(  sigc::ptr_fun(&menuButton) );
450
                tcMenuMain->setAlpha(0.5);
451
 
452
 
453
                tcInventory->addControl(new touchcontrols::Button("jetpack", touchcontrols::RectF(4,3,7,6),"jetpack",gamefunc_Jetpack));
454
                tcInventory->addControl(new touchcontrols::Button("medkit",  touchcontrols::RectF(7,3,10,6),"medkit",gamefunc_MedKit));
455
                tcInventory->addControl(new touchcontrols::Button("nightv",  touchcontrols::RectF(4,6,7,9),"nightvision",gamefunc_NightVision));
456
                tcInventory->addControl(new touchcontrols::Button("holoduke",touchcontrols::RectF(7,6,10,9),"holoduke",gamefunc_Holo_Duke));
457
                tcInventory->addControl(new touchcontrols::Button("steroids",touchcontrols::RectF(4,9,7,12),"steroids",gamefunc_Steroids));
458
                tcMenuMain->setAlpha(1);
459
 
460
 
461
                tcInventory->signal_button.connect(  sigc::ptr_fun(&inventoryButton));
462
 
463
                /*
464
                //Automap
465
                tcAutomap->addControl(new touchcontrols::Button("down_arrow",touchcontrols::RectF(2,14,4,16),"arrow_down",PORT_ACT_MAP_DOWN));
466
                tcAutomap->addControl(new touchcontrols::Button("up_arrow",touchcontrols::RectF(2,10,4,12),"arrow_up",PORT_ACT_MAP_UP));
467
                tcAutomap->addControl(new touchcontrols::Button("left_arrow",touchcontrols::RectF(0,12,2,14),"arrow_left",PORT_ACT_MAP_LEFT));
468
                tcAutomap->addControl(new touchcontrols::Button("right_arrow",touchcontrols::RectF(4,12,6,14),"arrow_right",PORT_ACT_MAP_RIGHT));
469
                tcAutomap->addControl(new touchcontrols::Button("zoom_in",touchcontrols::RectF(4,10,6,12),"zoom_in",PORT_ACT_MAP_ZOOM_IN));
470
                tcAutomap->addControl(new touchcontrols::Button("zoom_out",touchcontrols::RectF(4,14,6,16),"zoom_out",PORT_ACT_MAP_ZOOM_OUT));
471
 
472
                tcAutomap->signal_button.connect(  sigc::ptr_fun(&automapButton) );
473
                tcAutomap->setAlpha(0.5);
474
                 */
475
 
476
                //Game
477
                tcGameMain->setAlpha(gameControlsAlpha);
478
                controlsContainer.editButtonAlpha = gameControlsAlpha;
479
                tcGameMain->addControl(new touchcontrols::Button("use",touchcontrols::RectF(23,4,26,7),"use",gamefunc_Open));
480
                tcGameMain->addControl(new touchcontrols::Button("attack",touchcontrols::RectF(20,7,23,10),"fire2",gamefunc_Fire));
481
                tcGameMain->addControl(new touchcontrols::Button("jump",touchcontrols::RectF(23,7,26,10),"jump",gamefunc_Jump));
482
                tcGameMain->addControl(new touchcontrols::Button("crouch",touchcontrols::RectF(24,14,26,16),"crouch",gamefunc_Crouch));
483
                tcGameMain->addControl(new touchcontrols::Button("kick",touchcontrols::RectF(20,4,23,7),"boot",gamefunc_Quick_Kick));
484
 
485
                tcGameMain->addControl(new touchcontrols::Button("quick_save",touchcontrols::RectF(24,0,26,2),"save",KEY_QUICK_SAVE));
486
                tcGameMain->addControl(new touchcontrols::Button("quick_load",touchcontrols::RectF(20,0,22,2),"load",KEY_QUICK_LOAD));
487
                tcGameMain->addControl(new touchcontrols::Button("map",touchcontrols::RectF(4,0,6,2),"map",gamefunc_Map));
488
                tcGameMain->addControl(new touchcontrols::Button("keyboard",touchcontrols::RectF(8,0,10,2),"keyboard",KEY_SHOW_KBRD));
489
 
490
                tcGameMain->addControl(new touchcontrols::Button("show_inventory",touchcontrols::RectF(0,0,2,3),"inv",KEY_SHOW_INVEN));
491
 
492
                tcGameMain->addControl(new touchcontrols::Button("next_weapon",touchcontrols::RectF(0,3,3,5),"next_weap",gamefunc_Next_Weapon));
493
                tcGameMain->addControl(new touchcontrols::Button("prev_weapon",touchcontrols::RectF(0,7,3,9),"prev_weap",gamefunc_Previous_Weapon));
494
                //tcGameMain->addControl(new touchcontrols::Button("sniper",touchcontrols::RectF(0,5,3,7),"sniper",KEY_SNIPER));
495
 
496
                //quick actions binds
497
                tcGameMain->addControl(new touchcontrols::Button("quick_key_1",touchcontrols::RectF(4,3,6,5),"quick_key_1",KEY_QUICK_KEY1,false,true));
498
                tcGameMain->addControl(new touchcontrols::Button("quick_key_2",touchcontrols::RectF(6,3,8,5),"quick_key_2",KEY_QUICK_KEY2,false,true));
499
                tcGameMain->addControl(new touchcontrols::Button("quick_key_3",touchcontrols::RectF(8,3,10,5),"quick_key_3",KEY_QUICK_KEY3,false,true));
500
                tcGameMain->addControl(new touchcontrols::Button("quick_key_4",touchcontrols::RectF(10,3,12,5),"quick_key_4",KEY_QUICK_KEY4,false,true));
501
 
502
                //Left stick
503
                touchJoyLeft = new touchcontrols::TouchJoy("stick",touchcontrols::RectF(0,7,8,16),"strafe_arrow");
504
                tcGameMain->addControl(touchJoyLeft);
505
                touchJoyLeft->signal_move.connect(sigc::ptr_fun(&left_stick) );
506
                touchJoyLeft->signal_double_tap.connect(sigc::ptr_fun(&left_double_tap) );
507
 
508
                //Right stick
509
                touchJoyRight = new touchcontrols::TouchJoy("touch",touchcontrols::RectF(17,7,26,16),"look_arrow");
510
                tcGameMain->addControl(touchJoyRight);
511
                touchJoyRight->signal_move.connect(sigc::ptr_fun(&right_stick) );
512
                touchJoyRight->signal_double_tap.connect(sigc::ptr_fun(&right_double_tap) );
513
                touchJoyRight->setEnabled(false);
514
 
515
                tcGameMain->signal_button.connect(  sigc::ptr_fun(&gameButton) );
516
 
517
 
518
                //Mouse look for whole screen
519
                touchcontrols::Mouse *mouse = new touchcontrols::Mouse("mouse",touchcontrols::RectF(3,0,26,16),"");
520
                mouse->signal_action.connect(sigc::ptr_fun(&mouseMove));
521
                mouse->signal_double_tap.connect(sigc::ptr_fun(&right_double_tap) );
522
 
523
                mouse->setHideGraphics(true);
524
                tcGameLook->addControl(mouse);
525
 
526
 
527
 
528
                //Weapons
529
                weaponWheel = new touchcontrols::WheelSelect("weapon_wheel",touchcontrols::RectF(7,2,19,14),"weapon_wheel",10);
530
                weaponWheel->signal_selected.connect(sigc::ptr_fun(&weaponWheelChosen) );
531
                weaponWheel->signal_enabled.connect(sigc::ptr_fun(&weaponWheelSelected));
532
                tcGameWeapons->addControl(weaponWheel);
533
 
534
 
535
                tcGameWeapons->setAlpha(0.9);
536
 
537
                controlsContainer.addControlGroup(tcMenuMain);
538
 
539
                controlsContainer.addControlGroup(tcInventory);//Need to be above tcGameMain incase buttons over stick
540
                controlsContainer.addControlGroup(tcGameMain);
541
                controlsContainer.addControlGroup(tcGameWeapons);
542
                //controlsContainer.addControlGroup(tcAutomap);
543
                controlsContainer.addControlGroup(tcGameLook);
544
                controlsCreated = 1;
545
 
546
                tcGameMain->setXMLFile((std::string)graphics_path +  "/game.xml");
547
                tcGameWeapons->setXMLFile((std::string)graphics_path +  "/weapons.xml");
548
                tcAutomap->setXMLFile((std::string)graphics_path +  "/automap.xml");
549
                tcInventory->setXMLFile((std::string)graphics_path +  "/inventory.xml");
550
 
551
        }
552
        else
553
                LOGI("NOT creating controls");
554
 
555
        //controlsContainer.initGL();
556
}
557
 
558
int loadedGLImages=0;
559
 
560
int inMenuLast = 1;
561
int inAutomapLast = 0;
562
void frameControls()
563
{
564
        //LOGI("frameControls");
565
        //We need to do this here now because duke loads a new gl context
566
        if (!loadedGLImages)
567
        {
568
                touchJoyRight->setEnabled(false);
569
 
570
                controlsContainer.initGL();
571
                loadedGLImages = 1;
572
 
573
        }
574
 
575
        //LOGI("frameControls");
576
        if (PortableIsSoftwareMode())
577
                curRenderer = REND_SOFT;
578
        else
579
                curRenderer = REND_GL;
580
 
581
        int inMenuNew = PortableInMenu();
582
        if (inMenuLast != inMenuNew)
583
        {
584
                inMenuLast = inMenuNew;
585
                if (!inMenuNew)
586
                {
587
                        tcGameMain->setEnabled(true);
588
                        tcGameWeapons->setEnabled(true);
589
                        tcGameWeapons->fade(1,5);
590
                        tcMenuMain->setEnabled(false);
591
                }
592
                else
593
                {
594
                        tcGameMain->setEnabled(false);
595
                        tcGameWeapons->setEnabled(false);
596
                        tcMenuMain->setEnabled(true);
597
                }
598
        }
599
 
600
        int inAutomapNew =  PortableInAutomap() && !inMenuLast; //Dont show if menu comes up
601
        if (inAutomapLast != inAutomapNew)
602
        {
603
                inAutomapLast = inAutomapNew;
604
                if (inAutomapNew)
605
                {
606
                        tcAutomap->animateIn(5);
607
                }
608
                else
609
                {
610
                        tcAutomap->animateOut(5);
611
                }
612
        }
613
 
614
        setHideSticks(!showSticks);
615
        controlsContainer.draw();
616
}
617
 
618
 
619
void setTouchSettings(float alpha,float strafe,float fwd,float pitch,float yaw,int other)
620
{
621
 
622
        gameControlsAlpha = alpha;
623
        if (tcGameMain)
624
        {
625
                tcGameMain->setAlpha(gameControlsAlpha);
626
                controlsContainer.editButtonAlpha = gameControlsAlpha;
627
        }
628
 
629
        showWeaponCycle = other & 0x1?true:false;
630
        turnMouseMode   = other & 0x2?true:false;
631
        invertLook      = other & 0x4?true:false;
632
        precisionShoot  = other & 0x8?true:false;
633
        showSticks      = other & 0x1000?true:false;
634
 
635
        hideTouchControls = other & 0x80000000?true:false;
636
 
637
 
638
        switch ((other>>4) & 0xF)
639
        {
640
        case 1:
641
                left_double_action = gamefunc_Fire;
642
                break;
643
        case 2:
644
                left_double_action = gamefunc_Jump;
645
                break;
646
        default:
647
                left_double_action = 0;
648
        }
649
 
650
        switch ((other>>8) & 0xF)
651
        {
652
        case 1:
653
                right_double_action = gamefunc_Fire;
654
                break;
655
        case 2:
656
                right_double_action = gamefunc_Jump;
657
                break;
658
        default:
659
                right_double_action = 0;
660
        }
661
 
662
        strafe_sens = strafe;
663
        forward_sens = fwd;
664
        pitch_sens = pitch;
665
        yaw_sens = yaw;
666
 
667
}
668
 
669
int quit_now = 0;
670
 
671
#define EXPORT_ME __attribute__ ((visibility("default")))
672
 
673
JNIEnv* env_;
674
 
675
int argc=1;
676
const char * argv[32];
677
std::string graphicpath;
678
 
679
 
680
std::string doom_path;
681
 
682
const char * getDoomPath()
683
{
684
        return doom_path.c_str();
685
}
686
 
687
jint EXPORT_ME
688
Java_com_beloko_duke_engine_NativeLib_init( JNIEnv* env,
689
                jobject thiz,jstring graphics_dir,jint mem_mb,jobjectArray argsArray,jint renderer,jstring doom_path_ )
690
{
691
        env_ = env;
692
        curRenderer = renderer;
693
        //curRenderer = REND_SOFT;
694
        curRenderer = REND_GL;
695
 
696
        argv[0] = "eduke32";
697
        int argCount = (env)->GetArrayLength( argsArray);
698
        LOGI("argCount = %d",argCount);
699
        for (int i=0; i<argCount; i++) {
700
                jstring string = (jstring) (env)->GetObjectArrayElement( argsArray, i);
701
                argv[argc] = (char *)(env)->GetStringUTFChars( string, 0);
702
                LOGI("arg = %s",argv[argc]);
703
                argc++;
704
        }
705
 
706
 
707
 
708
        doom_path = (char *)(env)->GetStringUTFChars( doom_path_, 0);
709
 
710
        //Change working dir, save games etc
711
 
712
        chdir(getDoomPath());
713
        putenv("TIMIDITY_CFG=../timidity.cfg");
714
 
715
        LOGI("doom_path = %s",getDoomPath());
716
        //argv[0] = "vavoom";
717
        //argv[1] = "-basedir";
718
        //argv[2] = "/sdcard/Beloko/Quake/FULL";
719
        //args[3] = "-doom";
720
 
721
 
722
        const char * p = env->GetStringUTFChars(graphics_dir,NULL);
723
        graphicpath =  std::string(p);
724
 
725
 
726
 
727
        initControls(android_screen_width,-android_screen_height,graphicpath.c_str(),(graphicpath + "/touch_controls.xml").c_str());
728
        //initControls(2,-2,graphicpath.c_str(),(graphicpath + "/prdoom.xml").c_str());
729
 
730
        /*
731
        if (renderer != REND_SOFT)
732
                SDL_SetSwapBufferCallBack(frameControls);
733
 
734
        if (renderer == REND_SOFT)// In soft mode SDL calls swap buffer, disable so it does not flicker
735
                SDL_SwapBufferPerformsSwap(false);
736
         */
737
 
738
 
739
        SDL_SetSwapBufferCallBack(frameControls);
740
 
741
        //Now doen in java to keep context etc
742
        //SDL_SwapBufferPerformsSwap(false);
743
 
744
 
745
        PortableInit(argc,argv);
746
 
747
 
748
        return 0;
749
}
750
 
751
 
752
jint EXPORT_ME
753
Java_com_beloko_duke_engine_NativeLib_frame( JNIEnv* env,
754
                jobject thiz )
755
{
756
 
757
        LOGI("Java_com_beloko_duke_engine_NativeLib_frame");
758
 
759
        PortableFrame();
760
 
761
        frameControls();
762
 
763
        return 0;
764
}
765
 
766
__attribute__((visibility("default"))) jint JNI_OnLoad(JavaVM* vm, void* reserved)
767
{
768
        LOGI("JNI_OnLoad");
769
 
770
        return JNI_VERSION_1_4;
771
}
772
 
773
 
774
void EXPORT_ME
775
Java_com_beloko_duke_engine_NativeLib_keypress(JNIEnv *env, jobject obj,
776
                jint down, jint keycode, jint unicode)
777
{
778
        LOGI("keypress %d",keycode);
779
        if (controlsContainer.isEditing())
780
        {
781
                if (down && (keycode == SDL_SCANCODE_ESCAPE ))
782
                        controlsContainer.finishEditing();
783
 
784
        }
785
        else
786
                PortableKeyEvent(down,keycode,unicode);
787
 
788
}
789
 
790
 
791
void EXPORT_ME
792
Java_com_beloko_duke_engine_NativeLib_touchEvent(JNIEnv *env, jobject obj,
793
                jint action, jint pid, jfloat x, jfloat y)
794
{
795
        //LOGI("TOUCHED");
796
        controlsContainer.processPointer(action,pid,x,y);
797
}
798
 
799
 
800
void EXPORT_ME Java_com_beloko_duke_engine_NativeLib_doAction(JNIEnv *env, jobject obj,
801
                jint state, jint action)
802
{
803
        //gamepadButtonPressed();
804
        if (hideTouchControls)
805
                if (tcGameMain->isEnabled())
806
                        tcGameMain->animateOut(30);
807
        LOGI("doAction %d %d",state,action);
808
        PortableAction(state,action);
809
}
810
 
811
void EXPORT_ME Java_com_beloko_duke_engine_NativeLib_analogFwd(JNIEnv *env, jobject obj,
812
                jfloat v)
813
{
814
        PortableMoveFwd(v);
815
}
816
 
817
void EXPORT_ME Java_com_beloko_duke_engine_NativeLib_analogSide(JNIEnv *env, jobject obj,
818
                jfloat v)
819
{
820
        PortableMoveSide(v);
821
}
822
 
823
void EXPORT_ME Java_com_beloko_duke_engine_NativeLib_analogPitch(JNIEnv *env, jobject obj,
824
                jint mode,jfloat v)
825
{
826
        PortableLookPitch(mode, v);
827
}
828
 
829
void EXPORT_ME Java_com_beloko_duke_engine_NativeLib_analogYaw(JNIEnv *env, jobject obj,
830
                jint mode,jfloat v)
831
{
832
        PortableLookYaw(mode, v);
833
}
834
 
835
void EXPORT_ME Java_com_beloko_duke_engine_NativeLib_setTouchSettings(JNIEnv *env, jobject obj,
836
                jfloat alpha,jfloat strafe,jfloat fwd,jfloat pitch,jfloat yaw,int other)
837
{
838
        setTouchSettings(alpha,strafe,fwd,pitch,yaw,other);
839
}
840
 
841
void EXPORT_ME Java_com_beloko_duke_engine_NativeLib_resetTouchSettings(JNIEnv *env, jobject obj)
842
{
843
        controlsContainer.resetDefaults();
844
}
845
 
846
std::string quickCommandString;
847
jint EXPORT_ME
848
Java_com_beloko_duke_engine_NativeLib_quickCommand(JNIEnv *env, jobject obj,
849
                jstring command)
850
{
851
        const char * p = env->GetStringUTFChars(command,NULL);
852
        quickCommandString =  std::string(p) + "\n";
853
        env->ReleaseStringUTFChars(command, p);
854
        PortableCommand(quickCommandString.c_str());
855
}
856
 
857
 
858
 
859
 
860
void EXPORT_ME
861
Java_com_beloko_duke_engine_NativeLib_setScreenSize( JNIEnv* env,
862
                jobject thiz, jint width, jint height)
863
{
864
        android_screen_width = width;
865
        android_screen_height = height;
866
}
867
 
868
 
869
#include "SDL_main.h"
870
extern void SDL_Android_Init(JNIEnv* env, jclass cls);
871
 
872
void Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv* env, jclass cls)
873
{
874
        /* This interface could expand with ABI negotiation, calbacks, etc. */
875
        SDL_Android_Init(env, cls);
876
        SDL_SetMainReady();
877
        // SDL_EventState(SDL_TEXTINPUT,SDL_ENABLE);
878
}
879
 
880
 
881
}