[pb 4.40b2] gradient/alphaBlend demo

Share your advanced PureBasic knowledge/code with the community.
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

[pb 4.40b2] gradient/alphaBlend demo

Post by Demivec »

Code updated for 5.20+

Code: Select all

Enumeration
  ;windows
  #mainWindow
  ;images
  #boardImg
  #tileGradientImg
  ;sizes
  #boardWidth = 500
  #boardHeight = #boardWidth
  #tileWidth = 50
  #tileHeight = #tileWidth
EndEnumeration

Procedure placeMinorTiles(x,y,width,height,Color)
  Protected x2,y2
  
  x * #tileWidth
  y * #tileHeight
  
  DrawingMode(#PB_2DDrawing_AlphaBlend)
    For x2 = x To x + width * #tileWidth - 1 Step #tileWidth / 5
      For y2 = y To y + height * #tileHeight - 1 Step #tileHeight / 5
        Box(x2,y2,#tileWidth / 5,#tileHeight / 5,RGBA(Red(Color) + Random(128),Green(Color) + Random(128),Blue(Color) + Random(128),$FF))
        DrawImage(ImageID(#tileGradientImg),x2,y2,#tileWidth / 5,#tileHeight/ 5)
      Next
    Next
EndProcedure

Procedure createBoard()
  Protected x,y, color = $000080

  CreateImage(#boardImg,#boardWidth,#boardHeight,32)
  StartDrawing(ImageOutput(#boardImg))
    DrawingMode(#PB_2DDrawing_AlphaBlend)
    For x = 0 To #boardWidth - 1 Step #tileWidth
      For y = 0 To #boardHeight - 1 Step #tileHeight
        Box(x,y,#tileWidth,#tileHeight,RGBA(Random(225) + 25,Random(225) + 25,Random(225) + 25,$FF))
        DrawImage(ImageID(#tileGradientImg),x,y,#tileWidth,#tileHeight)
      Next
    Next
    
    placeMinorTiles(1,1,1,5,color)
    placeMinorTiles(2,1,2,1,color)
    placeMinorTiles(3,2,1,1,color)
    placeMinorTiles(2,3,2,1,color)
    
    placeMinorTiles(5,4,1,5,color)
    placeMinorTiles(6,4,2,1,color)
    placeMinorTiles(8,5,1,1,color)
    placeMinorTiles(6,6,2,1,color)
    placeMinorTiles(8,7,1,1,color)
    placeMinorTiles(6,8,2,1,color)
  StopDrawing()
EndProcedure

If Not OpenWindow(#mainWindow,0,0,#boardWidth,#boardHeight,"Test",#PB_Window_SystemMenu)
  MessageRequester("Error","Unable to open main window")
  End
EndIf

;create tileGradient
CreateImage(#tileGradientImg,#tileWidth,#tileWidth,32)
StartDrawing(ImageOutput(#tileGradientImg))
  DrawingMode(#PB_2DDrawing_AlphaChannel | #PB_2DDrawing_Gradient)
  LinearGradient(0,0,OutputWidth(),OutputHeight())
  GradientColor(0,$00000000)
  GradientColor(0.49,$30000000)
  GradientColor(0.50,$50000000)
  GradientColor(1,$80000000)
  Box(0,0,OutputWidth(),OutputHeight(),$00000000)
  DrawingMode(#PB_2DDrawing_AlphaChannel)
  Line(0,0,#tileWidth / 2,#tileHeight / 2,$00000000)
  Line(#tileWidth / 2,#tileHeight / 2,#tileWidth / 2,#tileHeight / 2,$80000000)
  Box(#tileWidth / 10,#tileHeight / 10,#tileWidth - #tileWidth / 5,#tileHeight - #tileHeight / 5,$40000000)
StopDrawing()

createBoard()

ImageGadget(0,0,0,0,0,ImageID(#boardImg))  
Define event
Repeat
  event = WaitWindowEvent(10)
Until event = #PB_Event_CloseWindow
@Edit: corrected code to remove exception generated when in debug mode (thanks eddy).
@Edit: updated code for PB v4.40b2 by adding the image depth (32) to createImage()
Last edited by Demivec on Tue Aug 18, 2009 2:49 am, edited 2 times in total.
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Post by eddy »

placeMinorTiles function raises an exception :!:
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

The exception is only raised when in debug mode, otherwise it doesn't hinder anything.

Code corrected to remove the exception. Thanks.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

DrawingMode(#PB_2DDrawing_AlphaBlend) > constant not found ??

whats the value of #PB_2DDrawing_AlphaBlend ?
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Post by eddy »

PB 4.40b1
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

eddy wrote:PB 4.40b1
DoH! :)

Thanks eddy :)
Post Reply