Subversion Repositories eduke32

Rev

Rev 4436 | 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
void left_double_tap(int state)
275
{
4434 terminx 276
    //LOGTOUCH("L double %d",state);
4439 terminx 277
    if (droidinput.left_double_action != -1)
278
        PortableAction(state, droidinput.left_double_action);
4386 terminx 279
}
280
 
281
void right_double_tap(int state)
282
{
4434 terminx 283
    //LOGTOUCH("R double %d",state);
4439 terminx 284
    if (droidinput.right_double_action != -1)
285
        PortableAction(state, droidinput.right_double_action);
4386 terminx 286
}
287
 
4434 terminx 288
void mouseMove(int action, float x, float y, float dx, float dy)
4386 terminx 289
{
4434 terminx 290
    //LOGI(" mouse dx = %f",dx);
4386 terminx 291
 
4434 terminx 292
    if (weaponWheelVisible)
293
        return;
294
 
295
    double scale = (shooting && precisionShoot) ? PRECISIONSHOOTFACTOR : 1.f;
296
 
4439 terminx 297
    PortableLook(dx * 2 * droidinput.yaw_sens * scale,
298
        -dy * droidinput.pitch_sens * scale * invertLook ? -1 : 1);
4386 terminx 299
}
300
 
4434 terminx 301
void left_stick(float joy_x, float joy_y, float mouse_x, float mouse_y)
4386 terminx 302
{
4434 terminx 303
    joy_x *=10;
304
    float strafe = joy_x*joy_x;
305
    //float strafe = joy_x;
306
    if (joy_x < 0)
307
        strafe *= -1;
4386 terminx 308
 
4439 terminx 309
    PortableMove(joy_y * 15 * droidinput.forward_sens, -strafe * droidinput.strafe_sens);
4386 terminx 310
}
311
 
4434 terminx 312
void right_stick(float joy_x, float joy_y, float mouse_x, float mouse_y)
4386 terminx 313
{
4434 terminx 314
    //LOGI(" mouse x = %f",mouse_x);
4386 terminx 315
 
4434 terminx 316
    mouseMove(0, joy_x, joy_y, mouse_x, mouse_y);
4386 terminx 317
}
318
 
319
void setHideSticks(bool v)
320
{
4434 terminx 321
    if (touchJoyLeft) touchJoyLeft->setHideGraphics(v);
322
    if (touchJoyRight) touchJoyRight->setHideGraphics(v);
4386 terminx 323
}
324
 
325
void touchSettingsButton(int state)
326
{
4433 terminx 327
    int32_t paused = PortableRead(READ_PAUSED);
328
 
4434 terminx 329
    //We wanna pause the game when doing settings
330
    if (state && !paused || !state && paused)
331
    {
4436 terminx 332
        PortableKeyEvent(2, SDL_SCANCODE_PAUSE, 0);
4434 terminx 333
    }
4386 terminx 334
}
335
 
4434 terminx 336
void initControls(int width, int height, const char * graphics_path, const char *settings_file)
4386 terminx 337
{
4434 terminx 338
    touchcontrols::GLScaleWidth = (float)width;
339
    touchcontrols::GLScaleHeight = (float)height;
4386 terminx 340
 
4434 terminx 341
    LOGI("initControls %d x %d,x path = %s, settings = %s",width,height,graphics_path,settings_file);
4386 terminx 342
 
4434 terminx 343
    if (!controlsCreated)
344
    {
345
        LOGI("creating controls");
4386 terminx 346
 
4434 terminx 347
        touchcontrols::setGraphicsBasePath(graphics_path);
4386 terminx 348
 
4434 terminx 349
        controlsContainer.openGL_start.connect( sigc::ptr_fun(&openGLStart));
350
        controlsContainer.openGL_end.connect( sigc::ptr_fun(&openGLEnd));
351
        controlsContainer.signal_settings.connect( sigc::ptr_fun(&touchSettingsButton));
4386 terminx 352
 
353
 
4434 terminx 354
        tcMenuMain = new touchcontrols::TouchControls("menu",true,false);
355
        tcGameMain = new touchcontrols::TouchControls("game",false,true,1,true);
356
        tcGameWeapons = new touchcontrols::TouchControls("weapons",false,true,1,false);
357
        tcAutomap  = new touchcontrols::TouchControls("automap",false,true,1,false);
358
        tcInventory = new touchcontrols::TouchControls("inventory",false,true,1,false);
359
        tcGameLook = new touchcontrols::TouchControls("mouse",true,false);
360
        //controlsContainer.dukeHack = 1;
4386 terminx 361
 
4436 terminx 362
//        tcGameMain->signal_settingsButton.connect(  sigc::ptr_fun(&gameSettingsButton) );
363
        tcMenuMain->signal_settingsButton.connect(  sigc::ptr_fun(&gameSettingsButton) );
4386 terminx 364
 
4434 terminx 365
        //Menu
4436 terminx 366
        tcMenuMain->addControl(new touchcontrols::Button("down_arrow",  touchcontrols::RectF(20,13,23,16),  "arrow_down",   SDL_SCANCODE_DOWN,  true)); //Repeating buttons
367
        tcMenuMain->addControl(new touchcontrols::Button("up_arrow",    touchcontrols::RectF(20,10,23,13),  "arrow_up",     SDL_SCANCODE_UP,    true));
368
        tcMenuMain->addControl(new touchcontrols::Button("left_arrow",  touchcontrols::RectF(17,13,20,16),  "arrow_left",   SDL_SCANCODE_LEFT,  true));
369
        tcMenuMain->addControl(new touchcontrols::Button("right_arrow", touchcontrols::RectF(23,13,26,16),  "arrow_right",  SDL_SCANCODE_RIGHT, true));
370
        tcMenuMain->addControl(new touchcontrols::Button("enter",       touchcontrols::RectF(0,13,3,16),    "enter",        SDL_SCANCODE_RETURN));
371
        tcMenuMain->addControl(new touchcontrols::Button("esc",         touchcontrols::RectF(0,9,3,12),     "esc",          SDL_SCANCODE_ESCAPE));
4386 terminx 372
 
373
 
4434 terminx 374
        tcMenuMain->signal_button.connect(  sigc::ptr_fun(&menuButton) );
375
        tcMenuMain->setAlpha(0.5);
4386 terminx 376
 
377
 
4434 terminx 378
        tcInventory->addControl(new touchcontrols::Button("jetpack", touchcontrols::RectF(4,3,7,6),"jetpack",gamefunc_Jetpack));
379
        tcInventory->addControl(new touchcontrols::Button("medkit",  touchcontrols::RectF(7,3,10,6),"medkit",gamefunc_MedKit));
380
        tcInventory->addControl(new touchcontrols::Button("nightv",  touchcontrols::RectF(4,6,7,9),"nightvision",gamefunc_NightVision));
381
        tcInventory->addControl(new touchcontrols::Button("holoduke",touchcontrols::RectF(7,6,10,9),"holoduke",gamefunc_Holo_Duke));
382
        tcInventory->addControl(new touchcontrols::Button("steroids",touchcontrols::RectF(4,9,7,12),"steroids",gamefunc_Steroids));
383
        tcMenuMain->setAlpha(1);
4386 terminx 384
 
385
 
4434 terminx 386
        tcInventory->signal_button.connect(  sigc::ptr_fun(&inventoryButton));
4386 terminx 387
 
4436 terminx 388
        /* FIXME: definitely hook this up
4434 terminx 389
        //Automap
390
        tcAutomap->addControl(new touchcontrols::Button("down_arrow",touchcontrols::RectF(2,14,4,16),"arrow_down",PORT_ACT_MAP_DOWN));
391
        tcAutomap->addControl(new touchcontrols::Button("up_arrow",touchcontrols::RectF(2,10,4,12),"arrow_up",PORT_ACT_MAP_UP));
392
        tcAutomap->addControl(new touchcontrols::Button("left_arrow",touchcontrols::RectF(0,12,2,14),"arrow_left",PORT_ACT_MAP_LEFT));
393
        tcAutomap->addControl(new touchcontrols::Button("right_arrow",touchcontrols::RectF(4,12,6,14),"arrow_right",PORT_ACT_MAP_RIGHT));
394
        tcAutomap->addControl(new touchcontrols::Button("zoom_in",touchcontrols::RectF(4,10,6,12),"zoom_in",PORT_ACT_MAP_ZOOM_IN));
395
        tcAutomap->addControl(new touchcontrols::Button("zoom_out",touchcontrols::RectF(4,14,6,16),"zoom_out",PORT_ACT_MAP_ZOOM_OUT));
4386 terminx 396
 
4434 terminx 397
        tcAutomap->signal_button.connect(  sigc::ptr_fun(&automapButton) );
398
        tcAutomap->setAlpha(0.5);
399
            */
4386 terminx 400
 
4434 terminx 401
        //Game
402
        tcGameMain->setAlpha(gameControlsAlpha);
403
        controlsContainer.editButtonAlpha = gameControlsAlpha;
4436 terminx 404
        tcGameMain->addControl(new touchcontrols::Button("use",         touchcontrols::RectF(23,4,26,7),    "use",      gamefunc_Open));
405
        tcGameMain->addControl(new touchcontrols::Button("attack",      touchcontrols::RectF(20,7,23,10),   "fire2",    gamefunc_Fire));
406
        tcGameMain->addControl(new touchcontrols::Button("jump",        touchcontrols::RectF(23,7,26,10),   "jump",     gamefunc_Jump));
407
        tcGameMain->addControl(new touchcontrols::Button("crouch",      touchcontrols::RectF(24,12,26,14),  "crouch",   gamefunc_Crouch));
408
        tcGameMain->addControl(new touchcontrols::Button("kick",        touchcontrols::RectF(20,4,23,7),    "boot",     gamefunc_Quick_Kick,false,true));
4386 terminx 409
 
4436 terminx 410
        tcGameMain->addControl(new touchcontrols::Button("quick_save",  touchcontrols::RectF(24,0,26,2),    "save",     KEY_QUICK_SAVE));
411
        tcGameMain->addControl(new touchcontrols::Button("quick_load",  touchcontrols::RectF(20,0,22,2),    "load",     KEY_QUICK_LOAD));
412
        tcGameMain->addControl(new touchcontrols::Button("map",         touchcontrols::RectF(4,0,6,2),      "map",      gamefunc_Map,       false,true));
413
        tcGameMain->addControl(new touchcontrols::Button("keyboard",    touchcontrols::RectF(8,0,10,2),     "keyboard", KEY_SHOW_KBRD,      false,true));
4386 terminx 414
 
4436 terminx 415
        tcGameMain->addControl(new touchcontrols::Button("show_inventory",touchcontrols::RectF(2,0,4,2),    "inv",      KEY_SHOW_INVEN));
4386 terminx 416
 
4436 terminx 417
        tcGameMain->addControl(new touchcontrols::Button("next_weapon", touchcontrols::RectF(0,3,3,5),      "next_weap",gamefunc_Next_Weapon));
418
        tcGameMain->addControl(new touchcontrols::Button("prev_weapon", touchcontrols::RectF(0,7,3,9),      "prev_weap",gamefunc_Previous_Weapon));
4434 terminx 419
        /*
420
                //quick actions binds
421
                tcGameMain->addControl(new touchcontrols::Button("quick_key_1",touchcontrols::RectF(4,3,6,5),"quick_key_1",KEY_QUICK_KEY1,false,true));
422
                tcGameMain->addControl(new touchcontrols::Button("quick_key_2",touchcontrols::RectF(6,3,8,5),"quick_key_2",KEY_QUICK_KEY2,false,true));
423
                tcGameMain->addControl(new touchcontrols::Button("quick_key_3",touchcontrols::RectF(8,3,10,5),"quick_key_3",KEY_QUICK_KEY3,false,true));
424
                tcGameMain->addControl(new touchcontrols::Button("quick_key_4",touchcontrols::RectF(10,3,12,5),"quick_key_4",KEY_QUICK_KEY4,false,true));
425
        */
426
        //Left stick
427
        touchJoyLeft = new touchcontrols::TouchJoy("stick",touchcontrols::RectF(0,7,8,16),"strafe_arrow");
428
        tcGameMain->addControl(touchJoyLeft);
429
        touchJoyLeft->signal_move.connect(sigc::ptr_fun(&left_stick) );
430
        touchJoyLeft->signal_double_tap.connect(sigc::ptr_fun(&left_double_tap) );
4386 terminx 431
 
4434 terminx 432
        //Right stick
433
        touchJoyRight = new touchcontrols::TouchJoy("touch",touchcontrols::RectF(17,7,26,16),"look_arrow");
434
        tcGameMain->addControl(touchJoyRight);
435
        touchJoyRight->signal_move.connect(sigc::ptr_fun(&right_stick) );
436
        touchJoyRight->signal_double_tap.connect(sigc::ptr_fun(&right_double_tap) );
437
        touchJoyRight->setEnabled(false);
4386 terminx 438
 
4434 terminx 439
        tcGameMain->signal_button.connect(  sigc::ptr_fun(&gameButton) );
4386 terminx 440
 
441
 
4434 terminx 442
        //Mouse look for whole screen
443
        touchcontrols::Mouse *mouse = new touchcontrols::Mouse("mouse",touchcontrols::RectF(3,0,26,16),"");
444
        mouse->signal_action.connect(sigc::ptr_fun(&mouseMove));
445
        mouse->signal_double_tap.connect(sigc::ptr_fun(&right_double_tap) );
4386 terminx 446
 
4434 terminx 447
        mouse->setHideGraphics(true);
448
        tcGameMain->addControl(mouse);
449
        //tcGameLook->addControl(mouse);
4386 terminx 450
 
451
 
452
 
4434 terminx 453
        //Weapons
454
        weaponWheel = new touchcontrols::WheelSelect("weapon_wheel",touchcontrols::RectF(7,2,19,14),"weapon_wheel_orange_blank",10);
455
        weaponWheel->signal_selected.connect(sigc::ptr_fun(&weaponWheelChosen) );
456
        weaponWheel->signal_enabled.connect(sigc::ptr_fun(&weaponWheelSelected));
457
        tcGameWeapons->addControl(weaponWheel);
4386 terminx 458
 
459
 
4434 terminx 460
        tcGameWeapons->setAlpha(0.9);
4386 terminx 461
 
4434 terminx 462
        controlsContainer.addControlGroup(tcMenuMain);
463
        controlsContainer.addControlGroup(tcGameMain);
464
        controlsContainer.addControlGroup(tcInventory);//Need to be above tcGameMain incase buttons over stick
465
        controlsContainer.addControlGroup(tcGameWeapons);
466
        //controlsContainer.addControlGroup(tcAutomap);
467
        //controlsContainer.addControlGroup(tcGameLook);
468
        controlsCreated = 1;
4386 terminx 469
 
4434 terminx 470
        tcGameMain->setXMLFile((std::string)graphics_path +  "/game.xml");
471
        tcGameWeapons->setXMLFile((std::string)graphics_path +  "/weapons.xml");
472
        tcAutomap->setXMLFile((std::string)graphics_path +  "/automap.xml");
473
        tcInventory->setXMLFile((std::string)graphics_path +  "/inventory.xml");
4386 terminx 474
 
4434 terminx 475
    }
476
    else
477
        LOGI("NOT creating controls");
4386 terminx 478
 
4434 terminx 479
    //controlsContainer.initGL();
4386 terminx 480
}
481
 
482
void frameControls()
483
{
4433 terminx 484
    static int loadedGLImages = 0;
485
    static int inMenuLast = 1;
486
    static int inAutomapLast = 0;
487
 
4434 terminx 488
    //LOGI("frameControls");
489
    //We need to do this here now because duke loads a new gl context
490
    if (!loadedGLImages)
491
    {
492
        touchJoyRight->setEnabled(false);
4386 terminx 493
 
4434 terminx 494
        controlsContainer.initGL();
495
        loadedGLImages = 1;
496
    }
4386 terminx 497
 
4434 terminx 498
    //LOGI("frameControls");
499
    curRenderer = (PortableRead(READ_RENDERER) != REND_CLASSIC);
4386 terminx 500
 
4434 terminx 501
    int inMenuNew = PortableRead(READ_MENU);
502
    if (inMenuLast != inMenuNew)
503
    {
504
        inMenuLast = inMenuNew;
505
        if (!inMenuNew)
506
        {
507
            tcGameMain->setEnabled(true);
508
            tcGameWeapons->setEnabled(true);
509
            tcGameWeapons->fade(1,5);
510
            tcMenuMain->setEnabled(false);
511
        }
512
        else
513
        {
514
            tcGameMain->setEnabled(false);
515
            tcGameWeapons->setEnabled(false);
516
            tcMenuMain->setEnabled(true);
517
        }
518
    }
4386 terminx 519
 
4434 terminx 520
    int inAutomapNew =  PortableRead(READ_AUTOMAP) && !inMenuLast; //Dont show if menu comes up
521
    if (inAutomapLast != inAutomapNew)
522
    {
523
        inAutomapLast = inAutomapNew;
524
        if (inAutomapNew)
525
        {
526
            tcAutomap->animateIn(5);
527
        }
528
        else
529
        {
530
            tcAutomap->animateOut(5);
531
        }
532
    }
4386 terminx 533
 
4434 terminx 534
    setHideSticks(!showSticks);
535
    controlsContainer.draw();
4386 terminx 536
}
537
 
538
void setTouchSettings(float alpha,float strafe,float fwd,float pitch,float yaw,int other)
539
{
4436 terminx 540
    gameControlsAlpha = MINCONTROLALPHA + (alpha * (1.0f - MINCONTROLALPHA));
4386 terminx 541
 
4434 terminx 542
    if (tcGameMain)
543
    {
544
        tcGameMain->setAlpha(gameControlsAlpha);
545
        controlsContainer.editButtonAlpha = gameControlsAlpha;
546
        tcGameWeapons->setAlpha(gameControlsAlpha);
547
        tcInventory->setAlpha(gameControlsAlpha);
548
    }
4386 terminx 549
 
4436 terminx 550
    // TODO: defined names for these values
551
    toggleCrouch        = other & 0x2 ? true : false;
552
    invertLook      = other & 0x4 ? true : false;
553
    precisionShoot  = other & 0x8 ? true : false;
554
    showSticks      = other & 0x1000 ? true : false;
4386 terminx 555
 
4436 terminx 556
    hideTouchControls = other & 0x80000000 ? true : false;
557
 
4434 terminx 558
    switch ((other>>4) & 0xF)
559
    {
560
    case 1:
4439 terminx 561
        droidinput.left_double_action = gamefunc_Fire;
4434 terminx 562
        break;
563
    case 2:
4439 terminx 564
        droidinput.left_double_action = gamefunc_Jump;
4434 terminx 565
        break;
566
    default:
4439 terminx 567
        droidinput.left_double_action = -1;
4434 terminx 568
    }
4386 terminx 569
 
4434 terminx 570
    switch ((other>>8) & 0xF)
571
    {
572
    case 1:
4439 terminx 573
        droidinput.right_double_action = gamefunc_Fire;
4434 terminx 574
        break;
575
    case 2:
4439 terminx 576
        droidinput.right_double_action = gamefunc_Jump;
4434 terminx 577
        break;
578
    default:
4439 terminx 579
        droidinput.right_double_action = -1;
4434 terminx 580
    }
4386 terminx 581
 
4439 terminx 582
    droidinput.strafe_sens = strafe;
583
    droidinput.forward_sens = fwd;
584
    droidinput.pitch_sens = pitch;
585
    droidinput.yaw_sens = yaw;
4386 terminx 586
}
587
 
588
#define EXPORT_ME __attribute__ ((visibility("default")))
589
 
590
JNIEnv* env_;
591
 
592
int argc=1;
593
const char * argv[32];
594
std::string graphicpath;
595
std::string doom_path;
596
 
4434 terminx 597
static const char * getGamePath()
4386 terminx 598
{
4434 terminx 599
    return doom_path.c_str();
4386 terminx 600
}
601
 
602
jint EXPORT_ME
603
Java_com_beloko_duke_engine_NativeLib_init( JNIEnv* env,
4434 terminx 604
        jobject thiz,jstring graphics_dir,jint audio_rate,jint audio_buffer_size,jobjectArray argsArray,jint renderer,jstring doom_path_ )
4386 terminx 605
{
4434 terminx 606
    env_ = env;
4386 terminx 607
 
4436 terminx 608
    droidinfo.audio_sample_rate = audio_rate;
609
    droidinfo.audio_buffer_size = audio_buffer_size;
4386 terminx 610
 
4434 terminx 611
    curRenderer = renderer;
612
    //curRenderer = REND_SOFT;
613
    curRenderer = REND_GL;
4386 terminx 614
 
4434 terminx 615
    argv[0] = "eduke32";
616
    int argCount = (env)->GetArrayLength( argsArray);
617
    LOGI("argCount = %d",argCount);
618
    for (int i=0; i<argCount; i++) {
619
        jstring string = (jstring) (env)->GetObjectArrayElement( argsArray, i);
620
        argv[argc] = (char *)(env)->GetStringUTFChars( string, 0);
621
        LOGI("arg = %s",argv[argc]);
622
        argc++;
623
    }
4386 terminx 624
 
4434 terminx 625
    doom_path = (char *)(env)->GetStringUTFChars( doom_path_, 0);
4386 terminx 626
 
4434 terminx 627
    //Change working dir, save games etc
628
    // FIXME: potentially conflicts with chdirs in -game_dir support
629
    chdir(getGamePath());
630
    char timidity_env[512];
4386 terminx 631
 
4434 terminx 632
    sprintf(timidity_env,"TIMIDITY_CFG=%s/../timidity.cfg",getGamePath());
633
    //putenv("TIMIDITY_CFG=../timidity.cfg");
634
    putenv(timidity_env);
4386 terminx 635
 
4434 terminx 636
    LOGI("doom_path = %s",getGamePath());
4386 terminx 637
 
4434 terminx 638
    const char * p = env->GetStringUTFChars(graphics_dir,NULL);
639
    graphicpath =  std::string(p);
4386 terminx 640
 
4436 terminx 641
    initControls(droidinfo.screen_width, -droidinfo.screen_height,
642
        graphicpath.c_str(),(graphicpath + "/touch_controls.xml").c_str());
4386 terminx 643
 
4434 terminx 644
    /*
645
    if (renderer != REND_SOFT)
646
        SDL_SetSwapBufferCallBack(frameControls);
4386 terminx 647
 
4434 terminx 648
    if (renderer == REND_SOFT)// In soft mode SDL calls swap buffer, disable so it does not flicker
649
        SDL_SwapBufferPerformsSwap(false);
650
        */
4386 terminx 651
 
652
 
4434 terminx 653
    SDL_SetSwapBufferCallBack(frameControls);
4386 terminx 654
 
4434 terminx 655
    //Now doen in java to keep context etc
656
    //SDL_SwapBufferPerformsSwap(false);
4436 terminx 657
 
4434 terminx 658
    PortableInit(argc, argv);
4436 terminx 659
 
4434 terminx 660
    return 0;
4386 terminx 661
}
662
 
663
 
664
jint EXPORT_ME
665
Java_com_beloko_duke_engine_NativeLib_frame( JNIEnv* env,
4434 terminx 666
        jobject thiz )
4386 terminx 667
{
4434 terminx 668
    LOGI("Java_com_beloko_duke_engine_NativeLib_frame");
4386 terminx 669
 
4434 terminx 670
    frameControls();
671
    return 0;
4386 terminx 672
}
673
 
674
__attribute__((visibility("default"))) jint JNI_OnLoad(JavaVM* vm, void* reserved)
675
{
4434 terminx 676
    LOGI("JNI_OnLoad");
677
    setTCJNIEnv(vm);
678
    jvm_ = vm;
679
    return JNI_VERSION_1_4;
4386 terminx 680
}
681
 
682
 
683
void EXPORT_ME
684
Java_com_beloko_duke_engine_NativeLib_keypress(JNIEnv *env, jobject obj,
4434 terminx 685
        jint down, jint keycode, jint unicode)
4386 terminx 686
{
4434 terminx 687
    LOGI("keypress %d",keycode);
688
    if (controlsContainer.isEditing())
689
    {
690
        if (down && (keycode == SDL_SCANCODE_ESCAPE ))
691
            controlsContainer.finishEditing();
4436 terminx 692
        return;
4434 terminx 693
    }
4436 terminx 694
 
695
    PortableKeyEvent(down,keycode,unicode);
4386 terminx 696
}
697
 
698
 
699
void EXPORT_ME
700
Java_com_beloko_duke_engine_NativeLib_touchEvent(JNIEnv *env, jobject obj,
4434 terminx 701
        jint action, jint pid, jfloat x, jfloat y)
4386 terminx 702
{
4434 terminx 703
    //LOGI("TOUCHED");
704
    controlsContainer.processPointer(action,pid,x,y);
4386 terminx 705
}
706
 
707
 
708
void EXPORT_ME Java_com_beloko_duke_engine_NativeLib_doAction(JNIEnv *env, jobject obj,
4434 terminx 709
        jint state, jint action)
4386 terminx 710
{
4434 terminx 711
    LOGI("doAction %d %d",state,action);
712
 
713
    //gamepadButtonPressed();
714
    if (hideTouchControls && tcGameMain)
715
    {
716
        if (tcGameMain->isEnabled())
717
            tcGameMain->animateOut(30);
718
 
719
        if (tcGameWeapons->isEnabled())
720
            tcGameWeapons->animateOut(30);
721
    }
722
 
723
    PortableAction(state,action);
4386 terminx 724
}
725
 
726
void EXPORT_ME Java_com_beloko_duke_engine_NativeLib_analogFwd(JNIEnv *env, jobject obj,
4434 terminx 727
        jfloat v)
4386 terminx 728
{
4434 terminx 729
    PortableMove(v, 0.f);
4386 terminx 730
}
731
 
732
void EXPORT_ME Java_com_beloko_duke_engine_NativeLib_analogSide(JNIEnv *env, jobject obj,
4434 terminx 733
        jfloat v)
4386 terminx 734
{
4434 terminx 735
    PortableMove(0.f, v);
4386 terminx 736
}
737
 
738
void EXPORT_ME Java_com_beloko_duke_engine_NativeLib_analogPitch(JNIEnv *env, jobject obj,
4434 terminx 739
        jint mode,jfloat v)
4386 terminx 740
{
4434 terminx 741
    PortableLook(0.f, v);
4386 terminx 742
}
743
 
744
void EXPORT_ME Java_com_beloko_duke_engine_NativeLib_analogYaw(JNIEnv *env, jobject obj,
4434 terminx 745
        jint mode,jfloat v)
4386 terminx 746
{
4434 terminx 747
    PortableLook(v, 0.f);
4386 terminx 748
}
749
 
750
void EXPORT_ME Java_com_beloko_duke_engine_NativeLib_setTouchSettings(JNIEnv *env, jobject obj,
4434 terminx 751
        jfloat alpha,jfloat strafe,jfloat fwd,jfloat pitch,jfloat yaw,int other)
4386 terminx 752
{
4434 terminx 753
    setTouchSettings(alpha,strafe,fwd,pitch,yaw,other);
4386 terminx 754
}
755
 
756
void EXPORT_ME Java_com_beloko_duke_engine_NativeLib_resetTouchSettings(JNIEnv *env, jobject obj)
757
{
4434 terminx 758
    controlsContainer.resetDefaults();
4386 terminx 759
}
760
 
761
std::string quickCommandString;
762
jint EXPORT_ME
763
Java_com_beloko_duke_engine_NativeLib_quickCommand(JNIEnv *env, jobject obj,
4434 terminx 764
        jstring command)
4386 terminx 765
{
4434 terminx 766
    const char * p = env->GetStringUTFChars(command,NULL);
767
    quickCommandString =  std::string(p) + "\n";
768
    env->ReleaseStringUTFChars(command, p);
769
    PortableCommand(quickCommandString.c_str());
4386 terminx 770
}
771
 
772
void EXPORT_ME
773
Java_com_beloko_duke_engine_NativeLib_setScreenSize( JNIEnv* env,
4434 terminx 774
        jobject thiz, jint width, jint height)
4386 terminx 775
{
4436 terminx 776
    droidinfo.screen_width = width;
777
    droidinfo.screen_height = height;
4386 terminx 778
}
779
 
4434 terminx 780
void EXPORT_ME  Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv* env, jclass cls)
4386 terminx 781
{
4434 terminx 782
    /* This interface could expand with ABI negotiation, calbacks, etc. */
783
    SDL_Android_Init(env, cls);
784
    SDL_SetMainReady();
785
    // SDL_EventState(SDL_TEXTINPUT,SDL_ENABLE);
4386 terminx 786
}
787
 
788
}