Subversion Repositories eduke32

Rev

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

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