Subversion Repositories eduke32

Rev

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