Subversion Repositories eduke32

Rev

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