-
Notifications
You must be signed in to change notification settings - Fork 19
/
ZPlatform_SDL.inc
625 lines (524 loc) · 14 KB
/
ZPlatform_SDL.inc
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
//This file is INCLUDEd in ZPlatform.pas
implementation
{$if defined(darwin) and defined(fpc)}
//Must link with hacked main-function for sdl osx
{$linklib SDLMain.o}
{$ifend}
{$i-} //No io-test on files, code tests ioresult
uses sdl2, ZLog, AudioPlayer, ZApplication, SyncObjs
{$ifdef MSWINDOWS},Windows{$endif}
{$ifdef fpc}
{$if defined(darwin) or defined(linux)},dl{$endif}
{$endif}
;
var
//Current mouse pos, written to in eventhandling
SdlMousePos : TZPointi;
Keys : array[0..511] of boolean;
KeyMap : array[AnsiChar] of integer;
gZApp: TZApplication;
procedure Platform_ShowMouse(Visible : boolean);
begin
if Visible then
SDL_ShowCursor(1)
else
SDL_ShowCursor(0);
end;
{$ifdef macos}
//Need to init cocoa before calling SDL_SetVideoMode
//http://stackoverflow.com/questions/12641755/sdl-video-init-causes-exception-on-mac-os-x-10-8
procedure OsxInit;
var
cocoa_lib : nativeuint;
nsappload : procedure; cdecl;
begin
cocoa_lib := dlopen( '/System/Library/Frameworks/Cocoa.framework/Cocoa', RTLD_LAZY );
nsappload := dlsym( cocoa_lib, 'NSApplicationLoad');
nsappload();
end;
{$endif}
var
SdlScreen : PSDL_Window;
function Platform_InitScreen(var Width, Height : integer; UseFullScreen : boolean; Title : PAnsiChar; ZApp : pointer) : integer;
var
Flags : SDL2.UINT32;
display : TSDL_DisplayMode;
begin
{$ifdef macos}
OsxInit;
{$endif}
//Save a global handle to the app
gZApp := TZApplication(ZApp);
if UseFullScreen then
flags := SDL_WINDOW_FULLSCREEN or SDL_WINDOW_OPENGL
else
flags := SDL_WINDOW_OPENGL or SDL_WINDOW_RESIZABLE;
if Width=-1 then
begin
SDL_GetDesktopDisplayMode(0,@display);
Width := display.w;
Height := display.h;
end;
// SDL_WM_SetCaption(Title, nil);
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SdlScreen := SDL_CreateWindow(Title,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
Width, Height,
Flags);
if (SdlScreen = nil) then
ZHalt(SDL_GetError);
SDL_GL_CreateContext(SdlScreen);
//Returnera actual res
SDL_GetWindowSize(SdlScreen,@Width,@Height);
Result := 0;
end;
procedure Platform_SetWindowCaption(Title : PAnsiChar);
begin
SDL_SetWindowTitle(SdlScreen,Title);
end;
procedure Platform_ShutdownScreen;
begin
SDL_Quit;
end;
procedure Platform_SwapBuffers;
begin
SDL_GL_SwapWindow(SdlScreen);
end;
procedure Platform_Run(Callback : TRunCallback);
//WinMain som behandlar meddelanden och anropar callback
var
E : TSDL_Event;
finished : Boolean;
begin
Finished := False;
while not Finished do
begin
while (SDL_PollEvent(@E)>0) do
begin
case E.type_ of
SDL_QUITEV : Finished := True;
SDL_KEYDOWN :
begin
Keys[ E.Key.keysym.sym and 511 ] := True;
end;
SDL_KEYUP :
begin
{$ifdef minimal}
if (E.Key.keysym.sym=SDLK_ESCAPE) and gZApp.EscapeToQuit then
Finished := True;
{$endif}
Keys[ E.Key.keysym.sym and 511 ] := False;
end;
SDL_MOUSEMOTION :
begin
SdlMousePos.X := E.motion.X;
SdlMousePos.Y := E.motion.Y;
end;
SDL_MOUSEBUTTONDOWN,SDL_MOUSEBUTTONUP :
begin
//Button 1: Left mouse button
//Button 2: Middle mouse button
//Button 3: Right mouse button
//4=wheel up, 5=wheel down
case E.Button.button of
1 : Keys[ Ord('{') ] := E.type_=SDL_MOUSEBUTTONDOWN;
2 : Keys[ Ord('|') ] := E.type_=SDL_MOUSEBUTTONDOWN;
3 : Keys[ Ord('}') ] := E.type_=SDL_MOUSEBUTTONDOWN;
4 : gZApp.MouseWheelDelta := 1;
5 : gZApp.MouseWheelDelta := -1;
end;
end;
end;
end;
if not (Callback()) then
Finished := True;
end;
end;
function Platform_GetTime : single;
begin
//todo from beginning of time
Result := SDL_GetTicks/1000.0;
end;
function Platform_IsKeyPressed(C : AnsiChar) : boolean;
begin
Result := Keys[ KeyMap[C] ];
end;
function Platform_CommandLine(Switch : PAnsiChar) : boolean;
var
P : AnsiString;
I : integer;
begin
Result := False;
I := 1;
while I<=System.ParamCount do
begin
P := System.ParamStr(I);
if (P[1]='-') and (P[2]=Switch[0]) then
begin
Result := True;
Break;
end;
Inc(I);
end;
end;
procedure Platform_Error(ErrorMessage : PAnsiChar);
begin
WriteLn(ErrorMessage);
end;
function Platform_GetMousePos : TZPointi;
begin
Result := SdlMousePos;
end;
procedure Platform_SetMousePos(const X,Y : integer);
begin
SDL_WarpMouseInWindow(SdlScreen,X,Y);
end;
function Platform_GetExeFileName : PAnsiChar;
{$ifdef fpc}
var
S : string;
I : integer;
begin
//In Fpc paramstr returns shortstring, must manually copy to PAnsiChar
S := System.ParamStr(0);
I := Length(S);
if I=0 then
begin
//Upx-compressed binaries on Linux must read ' '-environ to get the name of executable
//todo: SDL_GetEnv missing in pascal headers?
Result := ''; //SDL_GetEnv(' ');
end
else
begin
GetMem(Result,I+1);
System.Move(S[1],Result^,I);
Result[I] := #0;
end;
{$else}
var
AppName: array [0..1024] of Char;
ResModuleName : string;
begin
GetModuleFileName(0, AppName, Length(AppName));
Result := PAnsiChar( AnsiString(PChar(@AppName)) );
{$endif}
end;
procedure MakeFullPath(FileName : PAnsiChar; var Buf : array of AnsiChar);
var
I : integer;
Tmp : PAnsiChar;
begin
Tmp := @Buf;
ZStrCopy(Tmp,Platform_GetExeFileName);
I := ZStrLength(Tmp);
while (I>0) and (not (Tmp[I] in ['\','/'])) do
Dec(I);
if I>0 then
Tmp[I+1] := #0;
//Append filename
ZStrCat(Tmp,FileName);
end;
procedure Platform_WriteFile(FileName : PAnsiChar; Memory : pointer; Size : integer; Append : Boolean);
var
Buf : array[0..511] of AnsiChar;
Written : longint;
F : file;
begin
MakeFullPath(FileName,Buf);
FileName := @Buf;
System.FileMode := 2;
System.Assign(F,FileName);
if Append then
begin
System.Reset(F,1);
System.Seek(F,System.FileSize(F));
end
else
System.Rewrite(F,1);
if IOResult<>0 then
begin
{$ifdef zlog}
ZLog.GetLog('Platform').Write('File could not be created: ' + FileName);
{$endif}
Exit;
end;
System.BlockWrite(F,Memory^,Size,Written);
System.Close(F);
end;
procedure Platform_ReadFile(FileName : PAnsiChar; var Memory : pointer; var Size : integer; IsRelative : Boolean);
var
F : file;
Buf : array[0..511] of AnsiChar;
begin
//Open file relative to path of exe-file
if IsRelative then
begin
MakeFullPath(FileName,Buf);
FileName := @Buf;
end;
System.FileMode := 0;
System.Assign(F,FileName);
System.Reset(F,1);
if IOResult<>0 then
begin
{$ifdef zlog}
ZLog.GetLog('Platform').Write('File missing: ' + FileName);
{$endif}
Exit;
end;
Size := System.FileSize(F);
GetMem(Memory,Size);
System.BlockRead(F,Memory^,Size);
System.Close(F);
end;
procedure InitKeyMap;
var
I : integer;
begin
for I := 0 to 255 do
KeyMap[AnsiChar(I)] := I;
for I := ord('A') to ord('Z') do
KeyMap[AnsiChar(I)] := I + (ord('a')-ord('A'));;
KeyMap[UpKey] := SDL_SCANCODE_UP;
KeyMap[DownKey] := SDL_SCANCODE_DOWN;
KeyMap[LeftKey] := SDL_SCANCODE_LEFT;
KeyMap[RightKey] := SDL_SCANCODE_RIGHT;
end;
procedure Platform_InitGlobals;
begin
InitKeyMap;
end;
//Audio
var
MixBuffer : array[0..SoundBufferFramesSize-1] of TSoundMixUnit;
procedure ClipAndCopy(Source : PSoundMixUnit; Dest : PFloat; FrameCount : integer);
begin
Move(Source^,Dest^,FrameCount * SizeOf(TSoundMixUnit));
end;
procedure SdlAudioCallback(userdata: Pointer; stream: PUInt8; len: Integer ); cdecl;
var
BytesToWrite,FramesToWrite : integer;
begin
BytesToWrite := Len;
FramesToWrite := BytesToWrite div SizeOf(TSoundOutputUnit);
FillChar(MixBuffer,FramesToWrite * SizeOf(TSoundMixUnit),0);
Platform_EnterMutex(VoicesMutex);
//Skriv SamplesToWrite samples från LastWritePosition
//dela upp i två anrop ifall buffer-wrap
AudioPlayer.RenderToMixBuffer(@MixBuffer[0],FramesToWrite);
Platform_LeaveMutex(VoicesMutex);
//Flytta paint buffer till dma-soundbuffer
ClipAndCopy(@MixBuffer[0],Pointer(Stream),FramesToWrite);
end;
procedure Platform_InitAudio;
var
Desired, Obtained: TSDL_AudioSpec;
begin
FillChar(Desired,SizeOf(Desired),0);
desired.freq := AudioPlayer.AudioRate;
desired.format:=AUDIO_F32LSB;
desired.channels:=AudioPlayer.StereoChannels;
desired.samples:=AudioPlayer.SoundBufferFramesSize;
desired.callback:=@SdlAudioCallback;
// desired->userdata=NULL;
if SDL_OpenAudio(@Desired,@Obtained)<0 then
ZHalt('Could not initialize audio. Start with -s for no sound.');
if (Obtained.Freq<>Desired.Freq) or
(Obtained.Format<>Desired.Format) or
(Obtained.Channels<>Desired.Channels) then
ZHalt('Could not initialize audio. Start with -s for no sound.');
//Start playing
SDL_PauseAudio(0);
end;
procedure Platform_ShutdownAudio;
begin
SDL_CloseAudio;
end;
function Platform_CreateMutex : pointer;
begin
Result := SDL_CreateMutex();
end;
procedure Platform_FreeMutex(P : pointer);
begin
SDL_DestroyMutex(PSDL_mutex(P));
end;
procedure Platform_EnterMutex(P : pointer);
begin
SDL_LockMutex(PSDL_mutex(P));
end;
procedure Platform_LeaveMutex(P : pointer);
begin
SDL_UnlockMutex(PSDL_mutex(P));
end;
function Platform_GenerateFontDisplayLists(Size : integer; FirstChar,LastChar : integer) : integer;
begin
//todo
Result := 0;
end;
function Platform_LoadLinkedResource : TZInputStream;
begin
//todo
Result := nil;
end;
function Platform_GLLoadProc(const P : PAnsiChar) : pointer;
begin
Result := SDL_GL_GetProcAddress(P);
end;
{$IFDEF LINUX}
function Platform_ShowOptionDialog(App : pointer) : boolean;
const
xstr = 'x'#0;
xdef = 'xmessage "Choose screen resolution:" -center -buttons '#0;
xcom = ','#0;
var
I : integer;
Tmp1 : array[0..10] of AnsiChar;
Str : array[0..511] of AnsiChar;
begin
Str[0]:=#0;
ZStrCat(Str,Xdef);
for I := 0 to High(ScreenModes) do
begin
//inttofloat conversion
ZStrConvertInt(ScreenModes[I].W, PAnsiChar(@Tmp1));
ZStrCat(Str,Tmp1);
ZStrCat(Str,xstr);
ZStrConvertInt(ScreenModes[I].H, PAnsiChar(@Tmp1));
ZStrCat(Str,Tmp1);
if I<High(ScreenModes) then
ZStrCat(Str,xcom);
end;
//TODO: import "System" call from libc
//Get exitvalue by rightshift 8
// I := libc.system(Str) shr 8;
//First button is 101
// if I>100 then
// byte(TZApplication(App).ScreenMode) := I-101;
Result := True;
end;
{$else}
function Platform_ShowOptionDialog(App : pointer) : boolean;
begin
Result := True;
end;
{$endif}
function Platform_GetDisplayRefreshRate : integer;
begin
//Not supported via SDL?
Result := 0;
end;
procedure Platform_Sleep(time : integer);
begin
SDL_Delay(time);
end;
function Platform_GetJoystickAxis(JoyId : integer; Axis : integer) : single;
begin
//TODO: joystick support
Result := 0;
end;
function Platform_GetJoystickButton(JoyId : integer; Button : integer) : boolean;
begin
//TODO: joystick support
Result := False;
end;
function Platform_GetJoystickPOV(JoyId : integer) : single;
begin
//TODO: joystick support
Result := -1;
end;
function Platform_LoadModule(const Name : PAnsiChar) : NativeUInt;
begin
{$if Defined(Linux) or Defined(Darwin)}
Result := NativeUInt(dlopen( Name, RTLD_NOW));
{$else}
Result := LoadLibraryA(Name);
{$endif}
end;
{$ifndef minimal}
procedure Platform_FreeModule(Handle : NativeUInt);
begin
end;
{$endif}
function Platform_GetModuleProc(Module : NativeUInt; const Name : PAnsiChar) : pointer;
begin
{$if Defined(Linux) or Defined(Darwin)}
Result := dlsym({$ifdef fpc}Pointer(Module){$else}Module{$endif}, Name );
{$else}
Result := GetProcAddress(Module,Name);
{$endif}
end;
function Platform_GetSystemTime : integer;
begin
//TODO: System time support
Result := 0;
end;
procedure Platform_NetOpen(Url : PAnsiChar; InBrowser : boolean; WebOpen : pointer);
begin
//TODO: Net support
end;
function Platform_NetRead(Handle,Buffer : pointer; Size : integer) : integer;
begin
//TODO: Net support
Result := 0;
end;
function Platform_TouchGetCount : integer;
begin
Result := 0;
end;
function Platform_TouchGetPos(const TouchIndex : integer) : TZPointi;
begin
Result.X := 0;
Result.Y := 0;
end;
function Platform_TouchGetId(const TouchIndex : integer) : integer;
begin
Result := 0;
end;
function Platform_CreateThread(ZThread : TZThread) : pointer;
begin
//TODO: thread support
Result := nil;
end;
procedure Platform_FreeThread(T : pointer);
begin
//TODO: thread support
end;
function Platform_CreateEvent : pointer;
begin
Result := SDL_CreateSemaphore(0);
end;
procedure Platform_WaitEvent(E : pointer);
begin
SDL_SemWait(PSDL_sem(E));
end;
procedure Platform_SignalEvent(E : pointer);
begin
SDL_SemPost(PSDL_sem(E));
end;
procedure Platform_FreeEvent(E : pointer);
begin
SDL_DestroySemaphore(PSDL_sem(E));
end;
function Platform_GetCpuCount : integer;
begin
//TODO: thread support
Result := 1;
end;
{$ifndef minimal}
procedure Platform_DesignerSetAudioCallback(F : TDesignerAudioCallback);
begin
end;
procedure Platform_DesignerSetFilePath(const P : string);
begin
end;
{$endif}
initialization
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) then
ZHalt(SDL_GetError);
end.