include "window.inc"
Window "SPACE WATER",800,600,1

'=================
'MICRO-GDIPLUS API
'=================

  typedef struct tagGDIStartup {
    dword          GdiplusVersion;
    sys            DebugEventCallback;
    bool           SuppressBackgroundThread;
    bool           SuppressExternalCodecs;
  } GdiplusStartupInput;

  enum  GpUnit {
  UnitWorld        = 0,
  UnitDisplay      = 1,
  UnitPixel        = 2,
  UnitPoint        = 3,
  UnitInch         = 4,
  UnitDocument     = 5,
  UnitMillimeter   = 6 
  };

  enum  GpFontStyle {
  FontStyleRegular      = 0,
  FontStyleBold         = 1,
  FontStyleItalic       = 2,
  FontStyleBoldItalic   = 3,
  FontStyleUnderline    = 4,
  FontStyleStrikeout    = 8 
  };


  type RECTF single x,y,w,h

  extern library "gdiplus.dll"
  '
  long GdiplusStartup( dword *token, GdiplusStartupInput*input,  GdiplusStartupOutput *output)
  long GdiplusShutdown(dword*token)
  long GdipLoadImageFromFile(wstring filename, sys*image)
  long GdipCreateBitmapFromFile(wstring filename, sys*bitmap)
  long GdipCreateHBITMAPFromBitmap(sys bitmap, sys*hbmReturn, dword backgroundColor)
  long GdipDisposeImage(sys image)
  '
  long GdipCreateFromHDC( sys hdc, *pGraphics)
  long GdipGraphicsClear(sys graphics,argb)
  long GdipDeleteGraphics(sys graphics)
  '
  long GdipBitmapGetPixel (sys image,x,y,*argb)
  long GdipBitmapSetPixel (sys image,x,y,argb)
  long GdipDrawImageRectI(sys graphics,image,x,y,w,h)
  long GdipCloneBitmapAreaI(sys x,y,width,height,PixelFormat,srcBitmap,dstBitmap)
  '
  long GdipCreateSolidFill(sys argb, *brush)
  long GdipSetSolidFillColor(sys brush, argb)
  long GdipGetSolidFillColor(sys brush, *argb)
  long GdipDeleteBrush(sys delete brush)
  long GdipCreatePen1(sys argb, single width, GpUnit unit, sys *pen)
  long GdipCreatePen2(sys brush, single width, GpUnit unit,sys *pen)
  long GdipGetPenWidth(sys pen, single width)
  long GdipSetPenColor(sys pen,argb)
  long GdipDeletePen(sys pen)
  '
  long GdipDrawLineI(sys graphics, pen,x1,y1,x2,y2)
  long GdipDrawRectangleI(sys graphics, GpPen pen, x, y,width,height)
  long GdipDrawEllipseI(sys graphics, pen,x,y,width,height)
  long GdipFillRectangleI(sys graphics,brush,x,y,width,height)
  long GdipFillEllipseI(sys graphics,brush,x,y,width,height)
  '
  long GdipNewInstalledFontCollection(sys * fontCollection);
  long GdipCreateFontFamilyFromName(wstring name,sys fontCollection,*FontFamily)
  long GdipDeleteFontFamily(sys FontFamily)
  long GdipCreateFont(sys fontFamily,single emSize,GpFontStyle style,GpUnit unit,sys *font)
  long GdipDeleteFont(sys font)
  '
  long GdipDrawString(sys graphics,wstring ws,sys length,font,RECTF *layoutRect,sys stringFormat,brush)
  '
  end extern


Function LoadBmpPlus(string File, sys Frames,*pimage) as sys
    iF Frames=0 or Frames <0 Then Frames =1
    static id as sys
    iF id <512 Then id +=1
    'iHnd(id) = LoadImage(0,File + ".bmp",0,0,0,16)'
    sys     hstatus,hbitmap
    wstring strFileName
    '
    strFileName = File
    hStatus = GdipLoadImageFromFile strFileName, pImage
    hstatus=GdipCreateHBITMAPFromBitmap(pImage,hbitmap,0)
    iHnd(id)=hBitMap
    iHdc(id) = CreateCompatibleDC(sys_hdc)
    SelectObject iHdc(id), iHnd(id)
    GetObject iHnd(id), sizeof info, &info
    BmpWidth (id) = info.bmWidth /Frames 
    BmpHeight(id) = info.bmHeight         
    BmpFrame (id) = Frames
    'if pImage then GdipDisposeImage pImage
    Return id
End Function        

sys hr,token
GdiplusStartupInput StartupInput

StartupInput.GdiplusVersion = 1
hr=GdiplusStartup token, StartupInput, byval 0
'
if hr then
  print "Error initializing GDIplus: " hex hr
end if

sys pGraphics
GdipCreateFromHDC bhdc,pGraphics

sys sky,psky,drop,pdrop
sky = LoadBmpPlus("images\night.jpg",0,psky)
drop = LoadBmpPlus("images\myDrop.bmp",0,pdrop)


'MAKE TRANSPARENT DROPS
'======================

sys x,y,c
for y=0 to 31
for x=0 to 31
  GdipBitmapGetPixel pdrop,x,y,c
  c and=0x0000ffff
  if c>0x4000 then c or=0x60000000 'make semi-transparent
  GdipBitmapSetPixel pdrop,x,y,c
next
next


'=========
'PARTICLES
'=========



Type pType
     single lifespan
     single x
     single y
     single xv
     single yv
End Type

Dim Particle(10000) as pType
sys xstart, ystart, i, mx=1000

Sub ParticleInit()
Xstart = Width  \ 10
Ystart = Height \ 10
For i = 0 To mx
    Particle(i).lifespan = Rand 1,500
    Particle(i).x  = Xstart+rand(0,80)-40
    Particle(i).y  = Ystart+rand(0,80)-40
    Particle(i).yv = Rand(1,4)
    Particle(i).xv = Rand(1,8) 
Next 
End Sub

ParticleInit()


'
'CREATE FONT ETC
'===============
'
sys FontFamily,Font,FontBrush
'
GdipCreateFontFamilyFromName "Arial",0,FontFamily
GdipCreateFont FontFamily,50,FontStyleRegular,UnitPixel,font
GdipCreateSolidFill 0xffffffff, FontBrush
RECTF pointf<=(200,50,800,600)
wstring wtxt="Waterfall"

  '



'=======
'ANIMATE
'=======

sys brush

While EscKey()=0

'ClsColor (200,200,200)
'SetBmp (sky,0,0,800,600,0) 
GdipDrawImageRectI pgraphics,psky,0,0,800,600

'SHAPES

GdipCreateSolidFill(0x40ffff00, brush)
'GdipFillRectangleI(pgraphics,brush,100,100,200,200)
GdipFillEllipseI(pgraphics,brush,50,20,100,150)
GdipDeleteBrush brush

'TEXT

GdipDrawString pGraphics,wtxt,-1,font,pointf,0,FontBrush

For i=0 To mx
'RoundBox Particle(i).x, Particle(i).y,12,10,10,10, 0,Rand(32,64),Rand(200,255)
GdipDrawImageRectI pgraphics,pdrop,Particle(i).x, Particle(i).y,32,32
Particle(i).x  = Particle(i).x + Particle(i).xv
Particle(i).y  = Particle(i).y + Particle(i).yv
Particle(i).yv = Particle(i).yv + 0.96
iF Particle(i).y >= Height  
   Particle(i).yv = -(Particle(i).yv * 0.46)
   Particle(i).y  =  Height
End iF
Particle(i).LifeSpan = Particle(i).LifeSpan -1
iF Particle(i).LifeSpan <=0 
   Particle(i).LifeSpan = Rand(1,500)
   Particle(i).x = Xstart
   Particle(i).y = Ystart
   Particle(i).yv = Rand(1,4)
   Particle(i).xv = Rand(1,8) 
End iF
Next
FlipBuffer
WaitFPS 14
Wend

'========
'SHUTDOWN
'========
'
'
'GdipDeletePen pen
GdipDeleteBrush brush
GdipDeleteBrush FontBrush
GdipDeleteFont font
GdipDeleteFontFamily fontfamily
'
GdipDisposeImage pdrop
GdipDisposeImage psky
GdipDeleteGraphics pGraphics
GdiplusShutdown token

WinEnd