Subversion Repositories eduke32

Rev

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