/* GdiPlus Librarys for OxygenBasic by P. Wirbelauer 28.o6.2o12 */
indexbase 0

Type DEVMODE
   zstring dmDeviceName[32]    
   dmSpecVersion   as word    
   dmDriverVersion as word    
   dmSize          as word    
   dmDriverExtra   as word    
   dmFields        as long    
   dmOrientation   as word   
   dmPaperSize     as word   
   dmPaperLength   as word   
   dmPaperWidth    as word   
   dmScale         as word   
   dmCopies        as word   
   dmDefaultSource as word   
   dmPrintQuality  as word   
   dmColor         as word   
   dmDuplex        as word   
   dmYResolution   as word   
   dmTTOption      as word   
   dmCollate       as word   
   zstring dmFormName[32]    
   dmUnusedPadding as word   
   dmBitsPerPel    as long   
   dmPelsWidth     as long   
   dmPelsHeight    as long   
   dmDisplayFlags  as long   
   dmDisplayFrequency   as long  
   dmDisplayOrientation as long
End Type                     

Type WNDCLASS
   Style         as long
   lpfnwndproc   as sys
   cbClsextra    as long
   cbWndExtra    as long
   hInstance     as sys
   hIcon         as sys
   hCursor       as sys
   hbrBackground as sys
   lpszMenuName  as sys
   lpszClassName as sys
End Type

Type POINTAPI
   x as long
   y as long
End Type

Type MSG
   hwnd    as sys
   message as long
   wParam  as sys
   lParam  as sys
   time    as long
   pt      as POINTAPI
End Type

Type RECT
   Left   as Long
   Top    as Long
   Right  as Long
   Bottom as Long
End Type

Type RECTF
   x as Single
   y as Single
   w as Single
   h as Single
End Type

Type POINTF
   x as Single
   y as Single
End Type

Type GdiPlusStartupInput
   GdiPlusVersion           as Long
   DebugEventCallback       as Long
   SuppressBackgroundThread as Long
   SuppressExternalCodecs   as Long
End Type

% FontStyleRegular    = 0
% FontStyleBold       = 1
% FontStyleItalic     = 2
% FontStyleBoldItalic = 3
% FontStyleUnderline  = 4
% FontStyleStrikeout  = 8

% QualityModeInvalid = -1
% QualityModeDefault =  0
% QualityModeLow     =  1
% QualityModeHigh    =  2

% SmoothingModeInvalid       = QualityModeInvalid
% SmoothingModeDefault       = QualityModeDefault
% SmoothingModeHighSpeed     = QualityModeLow
% SmoothingModeHighQuality   = QualityModeHigh
% SmoothingModeNone          = QualityModeHigh + 1
% SmoothingModeAntiAlias8x4  = QualityModeHigh + 2
% SmoothingModeAntiAlias     = SmoothingModeAntiAlias8x4
% SmoothingModeAntiAlias8x8  = SmoothingModeAntiAlias8x4 + 1

% PixelOffsetModeInvalid     = QualityModeInvalid
% PixelOffsetModeDefault     = QualityModeDefault
% PixelOffsetModeHighSpeed   = QualityModeLow
% PixelOffsetModeHighQuality = QualityModeHigh
% PixelOffsetModeNone        = QualityModeHigh + 1
% PixelOffsetModeHalf        = QualityModeHigh + 2 

% BrushTypeSolidColor     = 0
% BrushTypeHatchFill      = 1
% BrushTypeTextureFill    = 2
% BrushTypePathGradient   = 3
% BrushTypeLinearGradient = 4 

% WarpModePerspective = 0
% WarpModeBilinear    = 1 
% WrapModeTile        = 0
% WrapModeTileFlipX   = 1
% WrapModeTileFlipY   = 2
% WrapModeTileFlipXY  = 3
% WrapModeClamp       = 4
% MatrixOrderPrepend  = 0
% MatrixOrderAppend   = 1 

% SW_SHOW        = 5
% CS_VREDRAW     = 1
% CS_HREDRAW     = 2
% IDI_WINLOGO    = 32517
% IDC_ARROW      = 32512
% WM_CREATE      = 1
% WM_DESTROY     = 2
% WM_PAINT       = 15
% WM_QUIT        = 18
% WM_SIZE        = 5
% WM_MOVE        = 3
% WM_CHAR        = 258
% WM_KEYDOWN     = 256
% WM_KEYUP       = 257
% WM_MOUSEMOVE   = 512
% WM_MBUTTONDOWN = 519
% WM_LBUTTONDOWN = 513
% WM_RBUTTONDOWN = 516
% WM_LBUTTONUP   = 514
% WM_RBUTTONUP   = 517
% WM_MBUTTONUP   = 520
% WM_CLOSE       = 16
% WS_OVERLAPPED  = 0x80000      
% WS_POPUP       = 0x80000000
% WS_MINIMIZEBOX = 0x20000
% PM_REMOVE      = 1
% WHITE_BRUSH    = 0
% BLACK_BRUSH    = 4
% DM_PELSWIDTH   = 0x80000
% DM_PELSHEIGHT  = 0x100000
% DM_BITSPERPEL  = 0x40000
% DI_NORMAL      = 3 
% GENERIC_WRITE  = 0x40000000
% GENERIC_READ   = 0x80000000
% FILE_SHARE_READ  = 1
% FILE_SHARE_WRITE = 2
% OPEN_ALWAYS      = 4

Declare QueryPerformanceCounter   lib "kernel32.dll" (quad *c)  
Declare QueryPerformanceFrequency lib "kernel32.dll" (quad *c)  

Dim wm  as MSG
Dim wc  as WNDCLASS 
Dim rc  as RECT
Dim dvm as DEVMODE
Dim Reso(3) as sys 
Dim tRect as RECTF

sys FontHnd    [64]
sys ImageHnd   [512]
sys ImageWidth [512]
sys ImageHeight[512]
sys TileHnd    [512]
sys TileWidth  [512]
sys TileHeight [512]
sys MouseButton,xMouse,yMouse,wExit,seed,AscKey,sys_fjs,sys_Frames
sys hwnd,hdc,bHdc,sWidth,sHeight,hBitmap,GdipToken,GdipStartUp,pPen,pGraphics
Quad sys_ct1,sys_ct2,sys_freq,sys_qc1,sys_qc2,sys_qf                                                               
Double sys_interval               

sys winmm,kernel,user,gdi,gdiplus
kernel = LoadLibrary "kernel32.dll"
user   = LoadLibrary "user32.dll"
gdi    = LoadLibrary "gdi32.dll"
winmm  = LoadLibrary "winmm.dll"
gdiplus= LoadLibrary "gdiplus.dll"

Sub CallDll(sys DllAdr, string libName)
    GetProcAddress DllAdr, libName
    'Return eax
End Sub

sys GetModuleHandle       = CallDll(kernel,"GetModuleHandleA")
sys ExitProcess           = CallDll(kernel,"ExitProcess")
sys Sleep                 = CallDll(kernel,"Sleep")  
sys GetTickCount          = CallDll(kernel,"GetTickCount")
sys ReadFile              = CallDll(kernel,"ReadFile")
sys WriteFile             = CallDll(kernel,"WriteFile")
sys CloseHandle           = CallDll(kernel,"CloseHandle")
sys CreateFile            = CallDll(kernel,"CreateFileA")
sys GetFileSize           = CallDll(kernel,"GetFileSize") 
sys LoadCursor            = CallDll(user,"LoadCursorA")
sys LoadIconA             = CallDll(user,"LoadIconA")
sys RegisterClass         = CallDll(user,"RegisterClassA")
sys CreateWindowEx        = CallDll(user,"CreateWindowExA")
sys ShowWindow            = CallDll(user,"ShowWindow")
sys GetDC                 = CallDll(user,"GetDC")  
sys InvalidateRect        = CallDll(user,"InvalidateRect")
sys ValidateRect          = CallDll(user,"ValidateRect")
sys DispatchMessage       = CallDll(user,"DispatchMessageA")
sys TranslateMessage      = CallDll(user,"TranslateMessage")
sys PeekMessage           = CallDll(user,"PeekMessageA")
sys PostQuitMessage       = CallDll(user,"PostQuitMessage")
sys DefWindowProc         = CallDll(user,"DefWindowProcA")
sys SetWindowText         = CallDll(user,"SetWindowTextA")
sys UpDateWindow          = CallDll(user,"UpdateWindow")
sys GetAsyncKeyState      = CallDll(user,"GetAsyncKeyState")
sys ReleaseDC             = CallDll(user,"ReleaseDC")
sys ChangeDisplaySettings = CallDll(user,"ChangeDisplaySettingsA")
sys EnumDisplaySettings   = CallDll(user,"EnumDisplaySettingsA") 
sys DestroyWindow         = CallDll(user,"DestroyWindow")
sys SetCursorPos          = CallDll(user,"SetCursorPos")
sys ShowCursor            = CallDll(user,"ShowCursor")
sys GetClientRect         = CallDll(user,"GetClientRect")
sys MoveWindow            = CallDll(user,"MoveWindow")
sys GetWindowRect         = CallDll(user,"GetWindowRect")
sys SetTimer              = CallDll(user,"SetTimer")
sys KillTimer             = CallDll(user,"KillTimer")
sys PlaySoundA            = CallDll(winmm,"PlaySoundA")
sys GetStockObject        = CallDll(gdi,"GetStockObject")

sys GdipPlusStartUp         = CallDll(gdiplus,"GdiplusStartup")
sys GdipShutdown            = CallDll(gdiplus,"GdiplusShutdown")
sys GdipDrawLineI           = CallDll(gdiplus,"GdipDrawLineI")
sys GdipDrawEllipseI        = CallDll(gdiplus,"GdipDrawEllipseI")
sys GdipFillEllipseI        = CallDll(gdiplus,"GdipFillEllipseI")
sys GdipFillRectangleI      = CallDll(gdiplus,"GdipFillRectangleI")
sys GdipDrawRectangleI      = CallDll(gdiplus,"GdipDrawRectangleI")
sys GdipBitmapGetPixel      = CallDll(gdiplus,"GdipBitmapGetPixel")
sys GdipBitmapSetPixel      = CallDll(gdiplus,"GdipBitmapSetPixel")
sys GdipCreateFromHDC       = CallDll(gdiplus,"GdipCreateFromHDC")
sys GdipDeleteGraphics      = CallDll(gdiplus,"GdipDeleteGraphics")
sys GdipGraphicsClear       = CallDll(gdiplus,"GdipGraphicsClear")
sys GdipCreatePen1          = CallDll(gdiplus,"GdipCreatePen1")
sys GdipDeletePen           = CallDll(gdiplus,"GdipDeletePen")
sys GdipDeleteBrush         = CallDll(gdiplus,"GdipDeleteBrush")
sys GdipCreateSolidFill     = CallDll(gdiplus,"GdipCreateSolidFill")
sys GdipSetSmoothingMode    = CallDll(gdiplus,"GdipSetSmoothingMode")
sys GdipDisposeImage        = CallDll(gdiplus,"GdipDisposeImage")
sys GdipLoadImageFromFile   = CallDll(gdiplus,"GdipLoadImageFromFile")
sys GdipDrawImageI          = CallDll(gdiplus,"GdipDrawImageI")
sys GdipGetImageHeight      = CallDll(gdiplus,"GdipGetImageHeight")
sys GdipGetImageWidth       = CallDll(gdiplus,"GdipGetImageWidth")
sys GdipDrawImageRectI      = CallDll(gdiplus,"GdipDrawImageRectI")
sys GdipSetSmoothingMode    = CallDll(gdiplus,"GdipSetSmoothingMode")
sys GdipImageRotateFlip     = CallDll(gdiplus,"GdipImageRotateFlip")
sys GdipSetPixelOffsetMode  = CallDll(gdiplus,"GdipSetPixelOffsetMode")
sys GdipCreateTexture2I     = CallDll(gdiplus,"GdipCreateTexture2I")
sys GdipCreateTexture       = CallDll(gdiplus,"GdipCreateTexture")
sys GdipCreateFont          = CallDll(gdiplus,"GdipCreateFont")
sys GdipDeleteFont          = CallDll(gdiplus,"GdipDeleteFont")
sys GdipDeleteFontFamily    = CallDll(gdiplus,"GdipDeleteFontFamily")
sys GdipDrawString          = CallDll(gdiplus,"GdipDrawString")

sys GdipScaleTextureTransform    = CallDll(gdiplus,"GdipScaleTextureTransform")
sys GdipRotateTextureTransform   = CallDll(gdiplus,"GdipRotateTextureTransform")
sys GdipSetTextRenderingHint     = CallDll(gdiplus,"GdipSetTextRenderingHint")
sys GdipCreateFontFamilyFromName = CallDll(gdiplus,"GdipCreateFontFamilyFromName")
sys GdipDrawImagePointRectI      = CallDll(gdiplus,"GdipDrawImagePointRectI")
sys GdipGetImageGraphicsContext  = CallDll(gdiplus,"GdipGetImageGraphicsContext")
sys GdipCreateBitmapFromGraphics = CallDll(gdiplus,"GdipCreateBitmapFromGraphics")

Function HiWord (sys hi) as sys
shr hi,16
Return hi
End Function

Function LoWord (sys lo) as sys
and lo,0xFFFF 
Return lo
End Function

Function SetDisplay(sys width, height, bpp) as sys
sys i,dev 
Do
dev = call EnumDisplaySettings 0,i,@dvm   
iF dev =0 Then Exit Do
i +=1
End Do
Reso(0) = dvm.dmPelsWidth
Reso(1) = dvm.dmPelsHeight  
Reso(2) = dvm.dmBitsPerPel
dvm.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_BITSPERPEL
dvm.dmPelsWidth  = width
dvm.dmPelsHeight = height
dvm.dmBitsPerPel = bpp
call ChangeDisplaySettings @dvm,4
Return eax
End Function

Sub RestoreDisplay()
dvm.dmPelsWidth  = Reso(0)
dvm.dmPelsHeight = Reso(1)
dvm.dmBitsPerPel = Reso(2)
call ChangeDisplaySettings 0,0   
End Sub

Sub StartUp()
Dim GdipStartupInput as GdiPlusStartupInput
GdipStartupInput.GdiPlusVersion =1 
call GdipPlusStartUp &GdipToken, &GdipStartupInput, ByVal 0
call GdipCreateFromHDC Hdc, &pGraphics
call GdipCreateBitmapFromGraphics sWidth, sHeight, pGraphics, &hBitmap 
call GdipGetImageGraphicsContext hBitmap, &bHdc
End Sub

Sub Ends()
sys ix
iF Reso(0) >0 and Reso(1) >0 Then RestoreDisplay
For ix=1 To 512
iF ImageHnd[ix] >0  
   call GdipDisposeImage ImageHnd[ix] 
End iF    
Next
For ix=1 To 512
iF TileHnd[ix] >0  
   call GdipDisposeImage TileHnd[ix] 
End iF    
Next
For ix=0 to 32
iF FontHnd[ix] >0
   Call GdipDeleteFont FontHnd[ix]
End iF
Next
call GdipDisposeImage hBitmap
call GdipDeleteGraphics pGraphics 
call GdipShutdown GdipToken 
call ReleaseDC hwnd,hdc
FreeLibrary GdiPlus
End Sub 

Sub PlayWav(string snd)
    call PlaysoundA snd,0,1
End Sub

Function LoadFont(wString FontName,single FontSize) as sys
    static sys id: iF id<32 Then id +=1
    sys FontFamily,pFont
    Call GdipCreateFontFamilyFromName StrPtr(FontName), 0, @FontFamily
    call GdipCreateFont FontFamily, FontSize, FontStyleRegular, 2, @pFont
    Call GdipDeleteFontFamily FontFamily
    FontHnd[id] = pFont
    Return id
End Function

Sub Text(sys id, single x,y, wString txt,sys col)
    sys brush
    tRect.x=x:tRect.y=y:tRect.w=sWidth:tRect.h=sHeight
    call GdipCreateSolidFill col, @brush
    call GdipDrawString bHdc, StrPtr(txt), -1, FontHnd[id], @tRect, 0, brush
    call GdipDeleteBrush brush
End Sub    

Function LoadImage(wString iName, sys fx) as sys
    sys image,width,height
    iF fx <=0 Then fx=1 
    static sys id: iF id <512 Then id +=1 
    call GdipLoadImageFromFile StrPtr(iName), &image
    call GdipGetImageWidth  image,@width
    call GdipGetImageHeight image,@height
    ImageHnd   [id]= image
    ImageWidth [id]= width/fx
    ImageHeight[id]= height
    Return id
End Function

Sub ZoomImage(sys id,x,y,zx,zy)
    call GdipDrawImageRectI bHdc,ImageHnd[id],x,y,zx,zy
End Sub

Sub DrawImage(sys id,x,y,fx)
    call GdipDrawImagePointRectI bHdc,ImageHnd[id],x,y,ImageWidth[id]*fx,0,ImageWidth[id],ImageHeight[id],2
End Sub    

Function LoadTiles(wString iName, sys fx,fy) as sys
    sys image,width,height
    iF fx <=0 Then fx=1: iF fy <=0 Then fy=1  
    static sys id: iF id <512 Then id +=1 
    call GdipLoadImageFromFile StrPtr(iName), &image
    call GdipGetImageWidth  image,&width
    call GdipGetImageHeight image,&height
    TileHnd   [id]= image
    TileWidth [id]= width /fx
    TileHeight[id]= height/fy
    Return id
End Function

Sub DrawTiles(sys id,x,y,fx,fy)
    call GdipDrawImagePointRectI bHdc,TileHnd[id],x,y,TileWidth[id]*fx,TileHeight[id]*fy,TileWidth[id],TileHeight[id],2
End Sub    

Sub iText(sys id,tx,ty, string txt) 
sys sx, rx
For sx=1 To Len(txt)
rx = Asc(txt, sx)
iF rx >=32 and rx <=127 
   rx = rx -32
   DrawImage id,tx,ty,rx
   iF rx=0 Then
      tx = tx + 12
   Else
      tx = tx + ImageWidth[id]
   End iF
End iF
Next 
End Sub

Sub SetPixelMode(sys pMode)
    call GdipSetPixelOffsetMode bHdc, pMode    '0-5
End Sub 

Sub SetSmoothMode(sys smoothing)
    call GdipSetSmoothingMode bHdc,smoothing   '0-5
End Sub    

Function ARGB(sys a,r,g,b) as sys 
    Return r << 0x10 | g << 8 | b | a << 0x18 & 0xFFFFFFFF
End Function

Sub ClsColor(byte a,r,g,b)
    call GdipGraphicsClear bHdc,ARGB(a,r,g,b)
End Sub    

Sub Oval(sys x,y,ra,rb,single thick,sys col)
    sys pen
    call GdipCreatePen1 col,thick, 2, &Pen
    call GdipDrawEllipseI bHdc,pen,x,y,ra,rb
    call GdipDeletePen Pen
End Sub

Sub FillOval(sys x,y,ra,rb,col)
    sys brush
    call GdipCreateSolidFill col,@brush
    call GdipFillEllipseI bHdc,brush,x,y,ra,rb
    call GdipDeleteBrush brush
End Sub

Sub Box(sys x,y,w,h,single thick,sys col)
    sys pen
    call GdipCreatePen1 col,thick, 2, &Pen
    call GdipDrawRectangleI bHdc,pen,x,y,w,h
    call GdipDeletePen Pen
End Sub    

Sub FillBox(sys x,y,w,h,col)
    sys brush
    call GdipCreateSolidFill col,@brush
    call GdipFillRectangleI bHdc,brush,x,y,w,h
    call GdipDeleteBrush brush
End Sub    

Sub Line(sys x1,y1,x2,y2,single thick,sys col)
    sys pen
    call GdipCreatePen1 col,thick, 2, &Pen
    call GdipDrawLineI bHdc,pen,x1,y1,x2,y2
    call GdipDeletePen Pen
End Sub         

Function GetImagePixel(sys id,x,y) as sys
    sys col
    call GdipBitmapGetPixel ImageHnd[id],x,y,@col
    Return col    
End Function

Sub SetImagePixel(sys id,x,y,col)
    call GdipBitmapSetPixel ImageHnd[id],x,y,col
End Sub    

Function GetColorA(sys kol) as byte
    Return (kol >> 24) & 0xFF
End Function

Function GetColorR(sys kol) as byte
    Return (kol >> 16) & 0xFF
End Function

Function GetColorG(sys kol) as byte
    Return (kol >> 8) & 0xFF
End Function

Function GetColorB(sys kol) as byte
    Return kol & 0xFF
End Function

Sub Events()
sys vRes
vRes = call PeekMessage &wm,0,0,0,Pm_Remove 
iF vRes >0
   iF wm.message = Wm_Quit Then wExit=1
   call TranslateMessage &wm
   call DispatchMessage  &wm
End iF
End Sub

Sub Flip()
    Events
    push 2    
    push sHeight
    push sWidth
    push 0
    push 0
    push 0
    push 0
    push hBitmap
    push pGraphics 
    call GdipDrawImagePointRectI  
End Sub

Sub SetCaption(string txt)
    call SetWindowText hwnd,txt
End Sub    

Sub ShowMouse(sys mouse)   
    call ShowCursor mouse
End Sub                

Function Key(sys cKey) As short
    iF wExit=1 Then Return 1
    Return call GetAsyncKeyState cKey 
End Function

Sub WaitKey(sys iKey)
    sys xKey
    While xKey=0  
    iF wExit=1 Then Return 1
    xKey = call GetAsyncKeyState iKey
    Flip
    call Sleep 10
    Wend
End Sub

Sub WaitMouse()
    While (1)
    iF MouseButton >=1 and MouseButton <=4
       Return MouseButton
    End iF
    iF wExit=1 Then Return 1
    Flip
    call Sleep 10
    Wend
End Sub

Function WndProc(byval hWnd as sys,byval wMsg as sys, byval wParam as sys,byval lparam as sys) as sys callback
iF wMsg = WM_CREATE
'
ElseiF wMsg = WM_MOVE 
Flip
call ValidateRect hwnd,0
ElseiF wMsg = WM_MOUSEMOVE
xMouse = LoWord(lParam)
yMouse = HiWord(lParam)
call InValidateRect hwnd,0,0
ElseiF wMsg = WM_LBUTTONDOWN
MouseButton =1
call InvalidateRect hWnd,0,0 
ElseiF wMsg = WM_LBUTTONUP
MouseButton =0  
call InvalidateRect hWnd,0,0 
ElseiF wMsg = WM_RBUTTONDOWN
MouseButton =2
call InvalidateRect hWnd,0,0 
ElseiF wMsg = WM_RBUTTONUP
MouseButton =0
call InvalidateRect hWnd,0,0
ElseiF wMsg = WM_MBUTTONDOWN
MouseButton =4
call InvalidateRect hWnd,0,0 
ElseiF wMsg = WM_MBUTTONUP
MouseButton =0
call InvalidateRect hWnd,0,0
ElseiF wMsg = WM_CHAR
AscKey = wParam
call InvalidateRect hWnd,0,0
ElseiF wMsg = WM_KEYUP
AscKey =0
call InvalidateRect hWnd,0,0
ElseiF wMsg = WM_DESTROY
call PostQuitMessage 0
Else
Return call DefWindowProc hWnd,wMsg,wParam,lParam
End iF
End Function

Function Screen(sys width, height, s) as sys
sys hInst, mode, w, h
iF s>=1 Then s= WS_OVERLAPPED | WS_MINIMIZEBOX
iF s<=0 
   mode = SetDisplay(width,height,32)
   iF mode =-2  
      s= WS_OVERLAPPED | WS_MINIMIZEBOX 
   Else   
      s= WS_POPUP
   End iF
End iF
hInst = call GetModuleHandle 0
wc.style = CS_HREDRAW or CS_VREDRAW   
wc.lpfnWndProc = &WndProc
wc.cbClsExtra =0
wc.cbWndExtra =0
wc.hInstance  = hInst
wc.hIcon  = call LoadIconA 0,IDI_WINLOGO            
wc.hCursor= call LoadCursor 0,IDC_ARROW       
wc.hbrBackground = call GetStockObject WHITE_BRUSH 
wc.lpszMenuName  = 0
wc.lpszClassName = StrPtr "window"
call RegisterClass &wc
hwnd = call CreateWindowEx 0,wc.lpszClassName,"",s,0,0,width,height,0,0,hInst,0
call ShowWindow hwnd, SW_SHOW
hdc = call GetDC hwnd
call GetClientRect hwnd,&rc   
w = width - rc.right
h = height- rc.bottom
call GetWindowRect hwnd,&rc
call MoveWindow hwnd,rc.left,rc.top,width+w,height+h,1
sWidth = width
sHeight= height
QueryPerformanceFrequency sys_qf   
QueryPerformanceCounter   sys_qc1
seed = call GetTickCount
StartUp
Return hwnd
End Function

Sub SetFps(sys frame)    
QueryPerformanceFrequency sys_freq                                                                                          
sys_Interval = sys_freq/frame  
While (1)                                                                       
QueryPerformanceCounter sys_ct2                                                 
iF sys_ct2 >= sys_ct1 + sys_interval                                             
QueryPerformanceCounter sys_ct1                                              
Exit Sub            
End iF
Wend                                                                             
End Sub

Function ShowFps() as sys 
QueryPerformanceCounter sys_qc2
iF sys_qc2 - sys_qc1 > sys_qf             
sys_fjs = sys_Frames
sys_Frames =0
sys_qc1 += sys_qf                         
End iF
sys_Frames +=1
Return sys_fjs
End Function

Sub Randomize()
    seed = GetTickCount
End Sub

Function Rand(sys z1,z2) as sys
sys  rnd 
mov  eax,z2
sub  eax,z1
inc  eax
imul edx,seed,0x8088405
inc  edx
mov  seed,edx
mul  edx
add  edx,z1
mov  rnd,edx
Return rnd
End Function 

