Subversion Repositories eduke32

Rev

Rev 5000 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4989 terminx 1
#include "compat.h"
4386 terminx 2
#include "sdl_inc.h"
3
#include "baselayer.h"
4
#include "keys.h"
5
#include "duke3d.h"
6
#include "common_game.h"
7
#include "osd.h"
8
#include "player.h"
4433 terminx 9
#include "game.h"
10
#include "build.h"
4989 terminx 11
#include "anim.h"
12
#include "player.h"
4386 terminx 13
 
14
#include "jmact/keyboard.h"
15
#include "jmact/control.h"
4989 terminx 16
#include "jmact/_control.h"
4386 terminx 17
 
4529 terminx 18
#include "menus.h"
19
 
4989 terminx 20
#ifdef __cplusplus
21
extern "C" {
22
#endif
23
extern int SDL_SendKeyboardKey(Uint8 state, SDL_Scancode scancode);
24
extern int SDL_SendKeyboardText(const char *text);
25
extern int SDL_SendMouseMotion(SDL_Window * window, Uint32 mouseID, int relative, int x, int y);
26
extern int SDL_SendMouseButton(SDL_Window * window, Uint32 mouseID, Uint8 state, Uint8 button);
4386 terminx 27
 
4989 terminx 28
#ifdef __cplusplus
29
}
30
#endif
31
 
4386 terminx 32
#include "in_android.h"
33
#include <android/log.h>
34
 
35
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO,"DUKE", __VA_ARGS__))
36
 
4433 terminx 37
extern int32_t main(int32_t argc, char *argv []);
4386 terminx 38
 
4433 terminx 39
static char sdl_text[2];
4386 terminx 40
 
4439 terminx 41
droidinput_t droidinput;
4434 terminx 42
 
4433 terminx 43
int PortableKeyEvent(int state, int code,int unicode)
44
{
4989 terminx 45
    LOGI("PortableKeyEvent %d %d %d",state,(SDL_Scancode)code,unicode);
4386 terminx 46
 
4989 terminx 47
    SDL_SendKeyboardKey(state ? SDL_PRESSED : SDL_RELEASED, (SDL_Scancode)code);
4434 terminx 48
    SDL_EventState(SDL_TEXTINPUT, SDL_ENABLE);
4386 terminx 49
 
4693 terminx 50
    // if (code == 42)
51
    //    unicode = 42;
4386 terminx 52
 
4434 terminx 53
    if (state)
54
    {
55
        //if (unicode < 128)
56
        {
57
            sdl_text[0] = unicode;
58
            sdl_text[1] = 0;
4386 terminx 59
 
4434 terminx 60
            int posted = SDL_SendKeyboardText((const char*)sdl_text);
61
            LOGI("posted = %d",posted);
62
        }
4386 terminx 63
 
4434 terminx 64
        if (state == 2)
65
            PortableKeyEvent(0, code, unicode);
66
    }
4386 terminx 67
 
4434 terminx 68
    return 0;
4386 terminx 69
}
70
 
4989 terminx 71
void PortableMouseMenu(float x,float y)
72
{
73
    SDL_SendMouseMotion(NULL,0,0,x,y);
74
}
75
 
76
void PortableMouseMenuButton(int state,int button)
77
{
78
    SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, state?SDL_PRESSED:SDL_RELEASED, SDL_BUTTON_LEFT);
79
}
80
 
4386 terminx 81
void changeActionState(int state, int action)
82
{
4434 terminx 83
    if (state)
84
    {
85
        //BUTTONSET(action,1);
86
        droidinput.functionSticky  |= ((uint64_t)1<<((uint64_t)(action)));
87
        droidinput.functionHeld    |= ((uint64_t)1<<((uint64_t)(action)));
4433 terminx 88
        return;
89
    }
90
 
91
    //BUTTONCLEAR(action);
4434 terminx 92
    droidinput.functionHeld  &= ~((uint64_t) 1<<((uint64_t) (action)));
4386 terminx 93
}
94
 
4440 terminx 95
#ifdef GP_LIC
96
#define GP_LIC_INC 4
97
#include "s-setup/gp_lic_include.h"
98
#endif
99
 
4386 terminx 100
void PortableAction(int state, int action)
101
{
4434 terminx 102
    LOGI("PortableAction action = %d, state = %d", action, state);
4386 terminx 103
 
4436 terminx 104
    if (action >= MENU_UP && action <= MENU_BACK)
4434 terminx 105
    {
5000 terminx 106
//        if (PortableRead(READ_SCREEN_MODE) == TOUCH_SCREEN_MENU)
4434 terminx 107
        {
5000 terminx 108
            int const sdl_code [] = { SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT,
4440 terminx 109
                    SDL_SCANCODE_RIGHT, SDL_SCANCODE_RETURN, SDL_SCANCODE_ESCAPE };
4436 terminx 110
            PortableKeyEvent(state, sdl_code[action-MENU_UP], 0);
4434 terminx 111
            return;
112
        }
113
    }
114
    else
4433 terminx 115
    {
4529 terminx 116
        //if (PortableRead(READ_SCREEN_MODE) != TOUCH_SCREEN_GAME) //If not in game don't do any of this
4693 terminx 117
        ///     return;
4433 terminx 118
 
4440 terminx 119
#ifdef GP_LIC
120
#define GP_LIC_INC 5
121
#include "s-setup/gp_lic_include.h"
122
#endif
123
 
4434 terminx 124
        //Special toggle for crouch, NOT when using jetpack or in water
4989 terminx 125
        if (droidinput.toggleCrouch)
4434 terminx 126
        {
4989 terminx 127
            int lotag = sector[g_player[myconnectindex].ps->cursectnum].lotag;
128
 
129
            if (droidinput.crouchToggleState && (lotag == ST_2_UNDERWATER || lotag == ST_1_ABOVE_WATER))
4434 terminx 130
            {
4989 terminx 131
                droidinput.crouchToggleState = 0;
4434 terminx 132
                if (action == gamefunc_Crouch)
4989 terminx 133
                    state = 0;
134
                else PortableAction(0, gamefunc_Crouch);
135
            }
136
 
137
            if (action == gamefunc_Crouch)
138
            {
139
                if (!g_player[myconnectindex].ps->jetpack_on && g_player[myconnectindex].ps->on_ground &&
140
                    lotag != ST_2_UNDERWATER && lotag != ST_1_ABOVE_WATER)
4434 terminx 141
                {
142
                    if (state)
143
                        droidinput.crouchToggleState = !droidinput.crouchToggleState;
4386 terminx 144
 
4434 terminx 145
                    state = droidinput.crouchToggleState;
146
                }
147
            }
4989 terminx 148
 
4434 terminx 149
        }
4386 terminx 150
 
4434 terminx 151
        //Check if jumping while crouched
152
        if (action == gamefunc_Jump)
153
        {
4989 terminx 154
            if (droidinput.crouchToggleState)
155
            {
156
                droidinput.crouchToggleState = 0;
157
                changeActionState(0, gamefunc_Crouch);
158
            }
159
            else
160
                changeActionState(state, action);
4434 terminx 161
        }
4989 terminx 162
        else
163
            changeActionState(state, action);
4386 terminx 164
 
4440 terminx 165
        if (state == 2)
4989 terminx 166
            PortableAction(0, action);
4440 terminx 167
 
168
        // LOGI("PortableAction state = 0x%016llX", CONTROL_ButtonState);
4434 terminx 169
    }
4386 terminx 170
}
171
 
5066 terminx 172
int const deadRegion = 0.3;
173
 
174
float analogCalibrate(float v)
175
{
176
    float rv = 0;
177
 
178
    if (v > deadRegion) rv = (v - deadRegion) * (v - deadRegion);
179
    else if (-v > deadRegion) rv = -(-v - deadRegion) * (-v - deadRegion);
180
 
181
    return rv;
182
}
183
 
4440 terminx 184
//Need these NAN check as not cumulative.
4386 terminx 185
void PortableMove(float fwd, float strafe)
186
{
4989 terminx 187
    if (!ud.auto_run)
188
    {
189
        fwd *= 0.5f;
190
        strafe *= 0.5f;
191
    }
192
 
4440 terminx 193
    if (!isnan(fwd))
5066 terminx 194
        droidinput.forwardmove = fclamp2(analogCalibrate(fwd), -1.f, 1.f);
4440 terminx 195
 
196
    if (!isnan(strafe))
5066 terminx 197
        droidinput.sidemove    = fclamp2(analogCalibrate(strafe),    -1.f, 1.f);
4386 terminx 198
}
199
 
5066 terminx 200
void PortableLook(float yaw, float pitch)
4386 terminx 201
{
4434 terminx 202
    droidinput.pitch += pitch;
203
    droidinput.yaw   += yaw;
4386 terminx 204
}
205
 
5066 terminx 206
void PortableLookJoystick(float yaw, float pitch)
4440 terminx 207
{
208
    if (!isnan(pitch))
5066 terminx 209
        droidinput.pitch_joystick = analogCalibrate(pitch);
4440 terminx 210
 
211
    if (!isnan(yaw))
5066 terminx 212
        droidinput.yaw_joystick   = analogCalibrate(yaw);
4440 terminx 213
}
214
 
4433 terminx 215
void PortableCommand(const char * cmd)
216
{
217
    OSD_Dispatch(cmd);
4386 terminx 218
}
5066 terminx 219
 
4436 terminx 220
void PortableInit(int argc, const char ** argv)
4386 terminx 221
{
4989 terminx 222
    main(argc, ( char **)argv);
4386 terminx 223
}
224
 
4693 terminx 225
int consoleShown = 0;
226
void AndroidSetConsoleShown(int onf)
227
{
228
    consoleShown = onf;
229
}
4529 terminx 230
 
4693 terminx 231
extern int inExtraScreens; //In game.c
4433 terminx 232
int32_t PortableRead(portableread_t r)
4386 terminx 233
{
4989 terminx 234
    int32_t rv;
235
 
4433 terminx 236
    switch (r)
237
    {
4529 terminx 238
    case READ_SCREEN_MODE:
4989 terminx 239
        if (g_animPtr || inExtraScreens)
240
            rv = TOUCH_SCREEN_BLANK_TAP;
4693 terminx 241
        else if (consoleShown)
4989 terminx 242
            rv = TOUCH_SCREEN_CONSOLE;
4529 terminx 243
        else if ((g_player[myconnectindex].ps->gm & MODE_MENU) == MODE_MENU)
4989 terminx 244
            rv = (m_currentMenu->type == Verify) ? TOUCH_SCREEN_YES_NO : TOUCH_SCREEN_MENU;
5066 terminx 245
/*
4693 terminx 246
        else if (ud.overhead_on == 2)
4989 terminx 247
            rv = TOUCH_SCREEN_AUTOMAP;
5066 terminx 248
*/
4529 terminx 249
        else if ((g_player[myconnectindex].ps->gm & MODE_GAME))
4693 terminx 250
            if (PortableRead(READ_IS_DEAD))
4989 terminx 251
                rv = TOUCH_SCREEN_BLANK_TAP;
4693 terminx 252
            else
4989 terminx 253
                rv = TOUCH_SCREEN_GAME;
4529 terminx 254
        else
4989 terminx 255
            rv = TOUCH_SCREEN_BLANK_TAP;
256
         break;
4433 terminx 257
    case READ_WEAPONS:
4989 terminx 258
        rv = g_player[myconnectindex].ps->gotweapon; break;
4433 terminx 259
    case READ_AUTOMAP:
5066 terminx 260
        rv = 0; break;//ud.overhead_on != 0;  break;// ud.overhead_on ranges from 0-2
4436 terminx 261
    case READ_MAPFOLLOWMODE:
4989 terminx 262
        rv = ud.scrollmode; break;
4433 terminx 263
    case READ_RENDERER:
4989 terminx 264
        rv = getrendermode(); break;
4433 terminx 265
    case READ_LASTWEAPON:
4989 terminx 266
        rv = droidinput.lastWeapon;
267
        if ((unsigned)rv < MAX_WEAPONS && !g_player[myconnectindex].ps->ammo_amount[rv])
268
            rv = -1;
269
        break;
4433 terminx 270
    case READ_PAUSED:
4989 terminx 271
        rv = ud.pause_on != 0; break;
4693 terminx 272
    case READ_IS_DEAD:
4989 terminx 273
        rv = g_player[myconnectindex].ps->dead_flag; break;
274
    case READ_INVENTORY:
275
        rv = 0;
276
        for (int i = 0; i < GET_MAX; i++)
277
        {
278
            if (g_player[myconnectindex].ps->inv_amount[i])
279
                rv += (1 << i);
280
        }
281
        break;
282
    case READ_SOMETHINGONPLAYER:
283
        rv = g_player[myconnectindex].ps->somethingonplayer != -1;
284
        break;
4433 terminx 285
    default:
4989 terminx 286
        rv = 0; break;
4433 terminx 287
    }
4989 terminx 288
 
289
    return rv;
4386 terminx 290
}
291
 
4693 terminx 292
static float map_zoom,map_dx,map_dy = 0;
293
 
294
void PortableAutomapControl(float zoom,float dx,float dy)
295
{
296
    map_zoom += zoom;
297
    map_dx += dx;
298
    map_dy += dy;
299
}
300
 
4386 terminx 301
///This stuff is called from the game/engine
302
 
4693 terminx 303
extern void  CONTROL_Android_ScrollMap(int32_t *angle,int32_t *x, int32_t *y, uint16_t *zoom )
304
{
305
 
306
    *x += ((int)(map_dx * -30000)*sintable[(512+2048-*angle)&2047])>>14;
307
    *y += ((int)(map_dy * -30000)*sintable[(512+1024-512-*angle)&2047])>>14;
308
 
4989 terminx 309
//    *zoom += map_zoom * 2000;
4693 terminx 310
    //*angle = 0;
311
    map_dx = map_dy = map_zoom = 0;
312
}
313
 
4433 terminx 314
void CONTROL_Android_SetLastWeapon(int w)
4386 terminx 315
{
4434 terminx 316
    droidinput.lastWeapon = w;
4386 terminx 317
}
318
 
319
void CONTROL_Android_ClearButton(int32_t whichbutton)
320
{
4434 terminx 321
    BUTTONCLEAR(whichbutton);
322
    droidinput.functionHeld  &= ~((uint64_t)1<<((uint64_t)(whichbutton)));
4386 terminx 323
}
324
 
4436 terminx 325
void CONTROL_Android_PollDevices(ControlInfo *info)
4386 terminx 326
{
4434 terminx 327
    //LOGI("CONTROL_Android_PollDevices %f %f",forwardmove,sidemove);
4440 terminx 328
    //LOGI("CONTROL_Android_PollDevices %f %f",droidinput.pitch,droidinput.yaw);
4386 terminx 329
 
5066 terminx 330
    info->dz = (int32_t)nearbyintf(-droidinput.forwardmove * ANDROIDMOVEFACTOR);
331
    info->dx = (int32_t)nearbyintf(droidinput.sidemove * ANDROIDMOVEFACTOR) >> 5;
332
    info->dpitch =
333
    (int32_t)nearbyint(droidinput.pitch * ANDROIDLOOKFACTOR + droidinput.pitch_joystick * ANDROIDPITCHFACTORJOYSTICK);
334
    info->dyaw =
335
    (int32_t)nearbyint(-droidinput.yaw * ANDROIDLOOKFACTOR - droidinput.yaw_joystick * ANDROIDYAWFACTORJOYSTICK);
4386 terminx 336
 
4434 terminx 337
    droidinput.pitch = droidinput.yaw = 0.f;
5066 terminx 338
    CONTROL_ButtonState = droidinput.functionSticky | droidinput.functionHeld;
4434 terminx 339
    droidinput.functionSticky = 0;
4386 terminx 340
 
4434 terminx 341
    //LOGI("poll state = 0x%016llX",CONTROL_ButtonState);
4386 terminx 342
}
343
 
344