-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathApus.Engine.AndroidGame.pas
197 lines (169 loc) · 5.91 KB
/
Apus.Engine.AndroidGame.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Game object for Android platform (OpenGL ES)
//
// Copyright (C) 2017 Ivan Polyacov, Apus Software ([email protected])
// This file is licensed under the terms of BSD-3 license (see license.txt)
// This file is a part of the Apus Game Engine (http://apus-software.com/engine/)
unit Apus.Engine.AndroidGame;
interface
uses JNI, Classes,
Apus.Engine.EngineAPI,Apus.Images,Apus.Engine.EngineTools,
Apus.AnimatedValues,Apus.Common,Apus.Engine.BasicGame,Apus.Engine.UIClasses;
type
{ TAndroidGame }
TAndroidGame=class(TBasicGame)
constructor Create;
protected
procedure ApplySettings; override;
// Эти методы используются при смене режима работы (вызов только из главного потока)
procedure InitGraph; override; // Инициализация графической части (переключить режим и все такое прочее)
procedure DoneGraph; override; // Финализация графической части
procedure PresentFrame; override;
procedure CalcPixelFormats(needMem:integer); override;
procedure InitObjects; override;
procedure SetupRenderArea; override;
procedure ScreenToGame(var p:TPoint); override;
procedure GameToScreen(var p:TPoint); override;
public
// virtual keyboard
keyboardStatus:boolean;
lastEdit:TUIEditBox;
lastSelStart,lastSelCount,lastCursorPos:integer;
screenOffsetY:TAnimatedValue; // offset render area to fit virtual keyboard
function GetStatus(n:integer):string; override;
end;
implementation
uses Apus.Android, SysUtils, Apus.Engine.CmdProc, Apus.EventMan, Apus.Engine.CommonUI, Apus.GfxFormats,
Apus.Engine.Console, GLES20, Apus.Engine.GLImages, Apus.Engine.PainterGL2;
{ TAndroidGame }
procedure TAndroidGame.ApplySettings;
var
i:integer;
begin
if running then begin // смена параметров во время работы
//if texman<>nil then (texman as TDXTextureMan).releaseAll;
Signal('Debug\Settings Changing');
end;
if running then begin
InitGraph;
//if texman<>nil then (texman as TDXTextureMan).ReCreateAll;
if painter<>nil then (painter as TGLPainter2).Reset;
for i:=1 to length(scenes) do
if scenes[i]<>nil then scenes[i].ModeChanged;
end;
end;
// Эта процедура пытается установить запрошенный видеорежим
// В случае ошибки она просто бросит исключение
procedure TAndroidGame.InitGraph;
begin
ForceLogMessage('Loading GLES 2.0');
InitGLES;
ForceLogMessage('GLES Loaded');
displayWidth:=params.width;
displayHeight:=params.height;
windowWidth:=displayWidth;
windowHeight:=displayHeight;
ForceLogMessage(Format('Device screen size: %d %d',[displayWidth,displayHeight]));
ForceLogMessage('OpenGL version: '+PChar(glGetString(GL_VERSION)));
ForceLogMessage('OpenGL vendor: '+PChar(glGetString(GL_VENDOR)));
ForceLogMessage('OpenGL renderer: '+PChar(glGetString(GL_RENDERER)));
ForceLogMessage('OpenGL extensions: '+#13#10+StringReplace(PChar(glGetString(GL_EXTENSIONS)),' ',#13#10,[rfReplaceAll]));
AfterInitGraph;
end;
procedure TAndroidGame.InitObjects;
begin
texman:=TGLTextureMan.Create(1024*BestVidMem);
painter:=TGLPainter2.Create(texman);
end;
procedure TAndroidGame.SetupRenderArea;
begin
inherited;
TGLPainter2(painter).SetDefaultRenderArea(0,0,displayWidth,displayHeight,
displayWidth,displayHeight);
end;
procedure TAndroidGame.ScreenToGame(var p:TPoint);
begin
inc(p.y,screenOffsetY.IntValue);
end;
procedure TAndroidGame.GameToScreen(var p:TPoint);
begin
dec(p.y,screenOffsetY.IntValue);
end;
procedure TAndroidGame.PresentFrame;
var
keyboardNeeded:boolean;
y:integer;
change:boolean;
begin
// // No need to present frame on Android
inc(FrameNum);
// Virtual keyboard
keyboardNeeded:=(FocusedControl<>nil) and (focusedControl is TUIEditBox);
if keyboardNeeded<>keyboardStatus then begin
if keyboardNeeded then begin
ShowVirtualKeyboard(ktDefault);
y:=FocusedControl.GetPosOnScreen.Bottom;
if y>round(displayHeight*0.44) then
screenOffsetY.Animate(y-round(displayHeight*0.4),250,Spline1,100);
end else begin
HideVirtualKeyboard;
screenOffsetY.Animate(0,250,Spline1,50);
end;
keyboardStatus:=keyboardNeeded;
end;
// Monitor editor changed
if keyboardNeeded then begin
change:=false;
if lastEdit<>focusedControl then begin
lastEdit:=focusedControl as TUIEditBox;
change:=true;
end;
if lastEdit.cursorpos<>lastCursorPos then begin
lastCursorPos:=lastEdit.cursorpos;
change:=true;
end;
if lastEdit.selStart<>lastSelStart then begin
lastSelStart:=lastEdit.selstart;
change:=true;
end;
if lastEdit.selcount<>lastSelCount then begin
lastSelCount:=lastEdit.selcount;
change:=true;
end;
{ if change then
if lastEdit.selCount>0 then
UpdateVirtualKeyboard(android.mainView,lastSelStart,lastSelStart+lastSelCount-1)
else
UpdateVirtualKeyboard(android.mainView,lastCursorPos,lastCursorPos);}
end;
TGLPainter2(painter).SetDefaultRenderArea(0,screenOffsetY.IntValue,
displayWidth,displayHeight,displayWidth,displayHeight);
end;
procedure TAndroidGame.CalcPixelFormats(needMem:integer);
begin
pfTrueColor:=ipfARGB;
pfTrueColorAlpha:=ipfARGB;
pfTrueColorLow:=ipf565;
pfTrueColorAlphaLow:=ipfARGB;
pfRTLow:=ipf565;
pfRTNorm:=ipfARGB;
pfRTHigh:=ipfARGB;
pfRTAlphaLow:=ipfARGB;
pfRTAlphaNorm:=ipfARGB;
pfRTAlphaHigh:=ipfARGB;
end;
constructor TAndroidGame.Create;
begin
ForceLogMessage('Base address (Create$$TAndroidGame): '+PtrToStr(@TAndroidGame.Create));
inherited Create(30);
useMainThread:=false;
screenOffsetY.Init;
end;
procedure TAndroidGame.DoneGraph;
begin
inherited;
end;
function TAndroidGame.GetStatus(n: integer): string;
begin
result:='';
end;
end.