+{$IFDEF UNIX}\r
+procedure init_windows;\r
+ var lang:string;\r
+ begin\r
+ nEcho(false);\r
+ stdscr:=nscreen;\r
+ { Get ENV LANG and check for UTF-8 support}\r
+ lang:=upcase(GetEnvironmentVariable('LANG')); \r
+ UTF8Scr:= (Pos('.UTF-8', lang) > 0) or (Pos('.UTF8', lang) > 0);\r
+ { Check min req col & rows}\r
+ {writeln(nCols(stdscr),nRows(stdscr));}\r
+ end;\r
+\r
+procedure WritePrompt(x,y:integer);\r
+ begin\r
+ { UTF-8 print }\r
+ if UTF8Scr then\r
+ { U+00BB, ยป, C2 BB, RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK.}\r
+ nFWrite(x,y,TextAttr,0,#$C2#$BB)\r
+ else \r
+ nFWrite(x,y,TextAttr,0,chr(187));\r
+ refresh;\r
+ end;\r
+\r
+{$ELSE}\r
+procedure nSetActiveWin(win:pwin);\r
+ begin\r
+ with win^ do\r
+ window(x,y,x1,y1);\r
+ end;\r
+\r
+procedure nWindow(var win : pwin; x,y,x1,y1 : integer);\r
+ begin\r
+ win^.x:=x; win^.y:=y;\r
+ win^.x1:=x1; win^.y1:=y1;\r
+ end;\r
+\r
+procedure init_windows;\r
+ begin\r
+ stdscr:=@win_arr[1];\r
+ win1:=@win_arr[2];\r
+ win2:=@win_arr[3];\r
+ nWindow(stdscr,1,1,80,25);\r
+ end;\r
+\r
+procedure WritePrompt(x,y:integer);\r
+ begin\r
+ gotoxy(x,y);\r
+ write(chr(175));\r
+ end;\r
+{$ENDIF}\r
+\r
+function square_wave(time : Real):integer;\r
+ var l:longint;\r
+ begin\r
+ l:=trunc(time);\r
+ if time-l < 0.5 then square_wave:=0 \r
+ else square_wave:=1;\r
+ end;\r
+\r
+{callback function to generate sound}\r
+procedure ProccessAudio(userdata: Pointer; stream: PUInt8; len: LongInt); cdecl;\r
+ var i,j,k,step:integer;\r
+ begin\r
+ step:=audio_STEP;\r
+ for i:=0 to trunc((len-1)/step) do begin\r
+ \r
+ if speaker_on then\r
+ if sound_i > 0 then begin\r
+ current_freq:=sound_Freqs[sound_play]/audio_freq;\r
+ if current_freq=0 then current_freq:=1;\r
+ sound_play:=sound_play+1;\r
+ if sound_play > sound_i then begin\r
+ speaker_on:=false; sound_i:=0; sound_play:=0; end;\r
+ end;\r
+\r
+ lasttime:=trunc(lasttime*lastfreq/current_freq);\r
+ \r
+ for j:=0 to step-1 do begin\r
+ k:=i*step+j;\r
+ if k<len then\r
+ if speaker_on then\r
+ stream[k]:=audio_VOLUME*square_wave(current_freq * (j+lasttime) )\r
+ else\r
+ stream[k]:=0;\r
+ end;\r
+\r
+ lasttime:=lasttime+step;\r
+ lastfreq:=current_freq; \r
+ end;\r
+ end; \r
+\r
+function InitAudio:boolean;\r
+ var Desired, Obtained: TSDL_AudioSpec;\r
+ begin\r
+ { Set up the requested settings }\r
+ Desired.freq := audio_FREQ;\r
+ Desired.format := AUDIO_U8;\r
+ Desired.channels:= 1;\r
+ Desired.samples := audio_SAMPLES;\r
+ Desired.callback:= @ProccessAudio;\r
+ Desired.userdata:= nil;\r
+\r
+ if SDL_Init(SDL_INIT_AUDIO) = 0 then\r
+ if SDL_OpenAudio(@Desired, @Obtained) = 0 then begin\r
+\r
+ speaker_on:=false;\r
+ current_freq:=1; lastfreq:=1;\r
+ sound_ticks:=1;\r
+ SDL_PauseAudio(0);\r
+ InitAudio:=true;\r
+ end \r
+ else InitAudio:=false\r
+ else\r
+ InitAudio:=false;\r
+ end;\r
+\r
+procedure InitScrKbd;\r
+ begin\r
+ buflen:=127;\r
+ CheckBreak:=false;\r
+ if initaudio then sound_supported:=true\r
+ else begin\r
+ WriteLn('SDL failed to initialize audio: ', SDL_GetError);\r
+ sound_supported:=false;\r
+ end;\r
+ UTF8Scr:=false;\r
+ init_windows;\r
+ nWindow(win1,2,2,79,4);\r
+ nWindow(win2,2,5,79,24);\r
+ nsetActiveWin(stdscr);\r
+ end;\r
+\r
+procedure DoneAudio;\r
+ begin\r
+ if sound_supported then begin\r
+ SDL_CloseAudio;\r
+ SDL_Quit;\r
+ end;\r
+ end;\r
+\r
+procedure DoneScrKbd;\r
+ begin\r
+ DoneAudio;\r
+ nSetActiveWin(stdscr);\r
+ clrscr;\r
+ end;\r
+\r
+procedure sound( Hz: Integer );\r
+ begin\r
+ if sound_supported then begin\r
+ sound_i:=0;\r
+ current_freq:=Hz/audio_Freq;\r
+ if current_freq > 0 then\r
+ speaker_on:=true\r
+ else\r
+ current_freq:=1;\r
+ end;\r
+ end;\r
+\r
+procedure nosound;\r
+ begin\r
+ if sound_supported then begin\r
+ if sound_i > 0 then begin\r
+ speaker_on:=true;\r
+ while speaker_on do;\r
+ end else\r
+ speaker_on:=false;\r
+ end;\r
+ end;\r
+\r
+procedure sounddelayed( Hz,step: Integer ); forward;\r
+\r
+procedure sounddelayed( Hz: Integer );\r
+ begin\r
+ sounddelayed( Hz, audio_STEP );\r
+ end;\r
+\r
+procedure sounddelayed( Hz,step: Integer );\r
+ begin\r
+ if sound_supported then begin\r
+ sound_ticks:=sound_ticks+1;\r
+ if sound_ticks mod step = 0 then begin\r
+ if sound_i < audio_MAXENTRIES then begin\r
+ Sound_Freqs[sound_i]:=Hz;\r
+ sound_i:=sound_i+1;\r
+ end;\r
+ end;\r
+ end;\r
+ end;\r
+\r
+procedure delay( MS: Integer);\r
+ begin\r
+ if sound_supported then\r
+ SDL_delay(MS)\r
+ else\r
+{$IFDEF UNIX}\r
+ ocrt.delay(MS);\r
+{$ELSE}\r
+ crt.delay(MS);\r
+{$ENDIF}\r
+ end;\r
+\r
+procedure ReadLine(var S:String);\r
+ var Ch: Char;\r
+ begin\r
+ Repeat\r
+ Ch:=ReadKey;\r
+ write(ch);\r
+ if (buflen>0) and (ch<>Chr(13)) then begin\r
+ S:=S+ch;\r
+ buflen:=buflen-1;\r
+ end;\r
+ Until (Ch=chr(13));\r
+ buflen:=127;\r
+ end;\r
+\r