This repository has been archived by the owner on Nov 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
app.monkey
292 lines (223 loc) · 4.88 KB
/
app.monkey
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
' Module mojo.app
'
' Copyright 2011 Mark Sibly, all rights reserved.
' No warranty implied; use at your own risk.
#If MOJO_VERSION_X
Import mojox.app
#Else
Private
Import graphicsdevice
Import audiodevice
Import inputdevice
Import graphics
Import audio
Import input
Import data
Global _app:App
Global _game:=BBGame.Game()
Global _delegate:GameDelegate
Global _devWidth:Int
Global _devHeight:Int
Global _updateRate:Int
Global _displayModes:DisplayMode[]
Global _desktopMode:DisplayMode
Function EnumDisplayModes:Void()
Local modes:=_game.GetDisplayModes()
Local mmap:=New IntMap<DisplayMode>
Local mstack:=New Stack<DisplayMode>
For Local i:=0 Until modes.Length
Local w:=modes[i].width
Local h:=modes[i].height
Local size:=w Shl 16 | h
If mmap.Contains( size )
Else
Local mode:=New DisplayMode( modes[i].width,modes[i].height )
mmap.Insert size,mode
mstack.Push mode
Endif
Next
_displayModes=mstack.ToArray()
Local mode:=_game.GetDesktopMode()
If mode
_desktopMode=New DisplayMode( mode.width,mode.height )
Else
_desktopMode=New DisplayMode( DeviceWidth,DeviceHeight )
Endif
End
Function ValidateDeviceWindow:Void( notifyApp:Bool )
Local w:=_game.GetDeviceWidth()
Local h:=_game.GetDeviceHeight()
If w=_devWidth And h=_devHeight Return
_devWidth=w
_devHeight=h
If notifyApp _app.OnResize
End
Class GameDelegate Extends BBGameDelegate
Field _graphics:GraphicsDevice
Field _audio:AudioDevice
Field _input:InputDevice
'***** BBGameDelegate *****
Method StartGame:Void()
_graphics=New GraphicsDevice
graphics.SetGraphicsDevice _graphics
graphics.SetFont Null
_audio=New AudioDevice
audio.SetAudioDevice _audio
_input=New InputDevice
input.SetInputDevice _input
ValidateDeviceWindow False
EnumDisplayModes
_app.OnCreate()
End
Method SuspendGame:Void()
_app.OnSuspend()
_audio.Suspend
End
Method ResumeGame:Void()
_audio.Resume
_app.OnResume()
End
Method UpdateGame:Void()
ValidateDeviceWindow True
_input.BeginUpdate
_app.OnUpdate()
_input.EndUpdate
End
Method RenderGame:Void()
ValidateDeviceWindow True
Local mode:=_graphics.BeginRender()
If mode graphics.BeginRender
If mode=2 _app.OnLoading Else _app.OnRender
If mode graphics.EndRender
_graphics.EndRender
End
Method KeyEvent:Void( event:Int,data:Int )
_input.KeyEvent event,data
If event<>BBGameEvent.KeyDown Return
Select data
Case KEY_CLOSE
_app.OnClose
Case KEY_BACK
_app.OnBack
End
End
Method MouseEvent:Void( event:Int,data:Int,x:Float,y:Float )
_input.MouseEvent event,data,x,y
End
Method TouchEvent:Void( event:Int,data:Int,x:Float,y:Float )
_input.TouchEvent event,data,x,y
End
Method MotionEvent:Void( event:Int,data:Int,x:Float,y:Float,z:Float )
_input.MotionEvent event,data,x,y,z
End
Method DiscardGraphics:Void()
_graphics.DiscardGraphics
End
End
Public
Class App
Method New()
If _app Error "App has already been created"
_app=Self
_delegate=New GameDelegate
_game.SetDelegate _delegate
End
Method OnCreate:Int()
End
Method OnUpdate:Int()
End
Method OnRender:Int()
End
Method OnLoading:Int()
End
Method OnSuspend:Int()
End
Method OnResume:Int()
End
Method OnClose:Int()
EndApp
End
Method OnBack:Int()
OnClose
End
Method OnResize:Int()
End
End
Class DisplayMode
Method New( width:Int,height:Int )
_width=width
_height=height
End
Method Width:Int() Property
Return _width
End
Method Height:Int() Property
Return _height
End
Private
Field _width:Int
Field _height:Int
End
Function LoadState:String()
Return _game.LoadState()
End
Function SaveState:Void( state:String )
_game.SaveState( state )
End
Function LoadString:String( path:String )
Return _game.LoadString( FixDataPath(path) )
End
Function SetUpdateRate:Void( hertz )
_updateRate=hertz
_game.SetUpdateRate hertz
End
Function UpdateRate:Int()
Return _updateRate
End
Function Millisecs:Int()
Return _game.Millisecs()
End
Function GetDate:Int[]()
Local date:Int[7]
GetDate date
Return date
End
Function GetDate:Void( date:Int[] )
_game.GetDate date
End
Function OpenUrl:Void( url:String )
_game.OpenUrl url
End
Function HideMouse:Void()
_game.SetMouseVisible False
End
Function ShowMouse:Void()
_game.SetMouseVisible True
End
Function EndApp:Void()
Error ""
End
Function DeviceWidth:Int()
Return _devWidth
End
Function DeviceHeight:Int()
Return _devHeight
End
Function SetDeviceWindow:Void( width:Int,height:Int,flags:Int )
_game.SetDeviceWindow( width,height,flags )
ValidateDeviceWindow False
End
Function SetDisplayMode:Void( width:Int,height:Int,depth:Int,hertz:Int,flags:Int )
_game.SetDisplayMode width,height,depth,hertz,flags
ValidateDeviceWindow False
End
Function DisplayModes:DisplayMode[]()
Return _displayModes
End
Function DesktopMode:DisplayMode()
Return _desktopMode
End
Function SetSwapInterval:Void( interval:Int )
_game.SetSwapInterval interval
End
#Endif