Subversion Repositories eduke32

Rev

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