Just another cool window effect :)

Share your advanced PureBasic knowledge/code with the community.
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Just another cool window effect :)

Post by Inf0Byt3 »

Code updated For 5.20+

As allways, this code was started in the 'Coding Questions' area :lol: , but now maybe somebody will find a good use for it. Nothing fancy or hard, just a small effect... If some bright minds can improve this, it would be really cool. Source and demo are here:



I hope you like it :)

Code: Select all

    ;-
    ;- Gradient Fill Example, For PureBasic 4.0.
    ;-

    EnableExplicit

    ;-
    ;- Initialising Win32 API (Msimg32.dll)
    ;-

    Macro NotDef(x, y)
      Defined(x, y) = #False
    EndMacro

    CompilerIf NotDef(GRADIENT_RECT, #PB_Structure) ;{
      Structure GRADIENT_RECT
        UpperLeft.l
        LowerRight.l
      EndStructure
    CompilerEndIf ;}
    CompilerIf NotDef(GRADIENT_TRIANGLE, #PB_Structure) ;{
      Structure GRADIENT_TRIANGLE
        Vertex1.l
        Vertex2.l
        Vertex3.l
      EndStructure
    CompilerEndIf ;}

    Enumeration ; #GRADIENT_FILL_
      #GRADIENT_FILL_RECT_H
      #GRADIENT_FILL_RECT_V
      #GRADIENT_FILL_TRIANGLE
    EndEnumeration

    Import "msimg32.lib"
      GradientFill(*hDC, *pVertex, numVertex.l, *pMesh, numMesh.l, flags.l)
    EndImport

    ;-
    ;- Defining Rectangle
    ;-

    Global Dim vRectangle.TRIVERTEX(1)
    Global Dim gRectangle.GRADIENT_RECT(0)

    With vRectangle(0) ;{
      \x     = 10
      \y     = 10
      \Red   = $0000
      \Green = $0000
      \Blue  = $A000
      \Alpha = $0000
    EndWith ;}
    With vRectangle(1) ;{
      \x     = 630
      \y     = 470
      \Red   = $0000
      \Green = $0000
      \Blue  = $0000
      \Alpha = $0000
    EndWith ;}
    With gRectangle(0) ;{
      \UpperLeft  = 0
      \LowerRight = 1
    EndWith ;}

    ;-
    ;- Defining Triangle
    ;-

    Global Dim vTriangle.TRIVERTEX(2)
    Global Dim gTriangle.GRADIENT_TRIANGLE(0)

    With vTriangle(0) ;{
      \x     = 50
      \y     = 50
      \Red   = $FF00
      \Green = $0000
      \Blue  = $0000
      \Alpha = $0000
    EndWith ;}
    With vTriangle(1) ;{
      \x     =  350
      \y     =  75
      \Red   =  $0000
      \Green =  $FF00
      \Blue  =  $0000
      \Alpha =  $0000
    EndWith ;}
    With vTriangle(2) ;{
      \x     =  200
      \y     =  400
      \Red   =  $0000
      \Green =  $0000
      \Blue  =  $FF00
      \Alpha =  $0000
    EndWith ;}
    With gTriangle(0) ;{
      \Vertex1 = 0
      \Vertex2 = 1
      \Vertex3 = 2
    EndWith ;}

    ;-
    ;- Drawing Rectangle & Triangle
    ;-

    Procedure myCallback(window, message, wParam, lParam)
     
      Protected result.l, dc.l, ps.PAINTSTRUCT
     
      result = #PB_ProcessPureBasicEvents
     
      Select message
        Case #WM_PAINT, #WM_NCPAINT
          dc = BeginPaint_(window, @ps)
          If dc
            GradientFill(dc, @vRectangle(), 2, @gRectangle(), 1, #GRADIENT_FILL_RECT_V)
            GradientFill(dc, @vTriangle(),  3, @gTriangle(),  1, #GRADIENT_FILL_TRIANGLE)
            EndPaint_(window, @ps)
          EndIf
      EndSelect
     
      ProcedureReturn result
     
    EndProcedure

    If OpenWindow(0,100,100,640,480,"")
     
      SetWindowColor(0, #Black)
      SetWindowCallback(@myCallback(), 0)
     
      Repeat
        Select WaitWindowEvent()
          Case #PB_Event_CloseWindow: Break
        EndSelect
      ForEver
     
    EndIf
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

Unfortunately, this doesn't seems fast enough for a real application. Is there any way of speeding up the drawing process while resizing the window? If i draw directly on the window, massive flickering appears :(.
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

you might be interested with this gradientfill code (api based) :

Code: Select all

;- 
;- Gradient Fill Example, For PureBasic 4.0.
;- 

EnableExplicit

;- 
;- Initialising Win32 API (Msimg32.dll) 
;- 

Macro NotDef(x, y)
  Defined(x, y) = #False
EndMacro

CompilerIf NotDef(GRADIENT_RECT, #PB_Structure) ;{ 
  Structure GRADIENT_RECT
    UpperLeft.l
    LowerRight.l
  EndStructure
CompilerEndIf ;} 
CompilerIf NotDef(GRADIENT_TRIANGLE, #PB_Structure) ;{ 
  Structure GRADIENT_TRIANGLE
    Vertex1.l
    Vertex2.l
    Vertex3.l
  EndStructure
CompilerEndIf ;} 

Enumeration ; #GRADIENT_FILL_
  #GRADIENT_FILL_RECT_H
  #GRADIENT_FILL_RECT_V
  #GRADIENT_FILL_TRIANGLE
EndEnumeration

Import "msimg32.lib"
  GradientFill(*hDC, *pVertex, numVertex.l, *pMesh, numMesh.l, flags.l) 
EndImport

;- 
;- Defining Rectangle
;- 

Global Dim vRectangle.TRIVERTEX(1)
Global Dim gRectangle.GRADIENT_RECT(0)

With vRectangle(0) ;{ 
  \x     = 10
  \y     = 10
  \Red   = $0000
  \Green = $0000
  \Blue  = $A000
  \Alpha = $0000
EndWith ;} 
With vRectangle(1) ;{ 
  \x     = 630
  \y     = 470
  \Red   = $0000
  \Green = $0000
  \Blue  = $0000
  \Alpha = $0000
EndWith ;} 
With gRectangle(0) ;{ 
  \UpperLeft  = 0
  \LowerRight = 1
EndWith ;} 

;- 
;- Defining Triangle
;- 

Global Dim vTriangle.TRIVERTEX(2)
Global Dim gTriangle.GRADIENT_TRIANGLE(0)

With vTriangle(0) ;{
  \x     = 50 
  \y     = 50 
  \Red   = $FF00
  \Green = $0000
  \Blue  = $0000
  \Alpha = $0000
EndWith ;} 
With vTriangle(1) ;{
  \x     =  350
  \y     =  75 
  \Red   =  $0000
  \Green =  $FF00
  \Blue  =  $0000
  \Alpha =  $0000
EndWith ;} 
With vTriangle(2) ;{
  \x     =  200 
  \y     =  400 
  \Red   =  $0000
  \Green =  $0000
  \Blue  =  $FF00
  \Alpha =  $0000
EndWith ;} 
With gTriangle(0) ;{
  \Vertex1 = 0
  \Vertex2 = 1
  \Vertex3 = 2
EndWith ;} 

;- 
;- Drawing Rectangle & Triangle
;- 

Procedure myCallback(window, message, wParam, lParam)
  
  Protected result.l, dc.l, ps.PAINTSTRUCT
  
  result = #PB_ProcessPureBasicEvents
  
  Select message
    Case #WM_PAINT, #WM_NCPAINT
      dc = BeginPaint_(window, @ps)
      If dc 
        GradientFill(dc, @vRectangle(), 2, @gRectangle(), 1, #GRADIENT_FILL_RECT_V)
        GradientFill(dc, @vTriangle(),  3, @gTriangle(),  1, #GRADIENT_FILL_TRIANGLE)
        EndPaint_(window, @ps)
      EndIf
  EndSelect
  
  ProcedureReturn result
  
EndProcedure

If OpenWindow(0,100,100,640,480,"")
  
  SetWindowColor(0, #Black)
  SetWindowCallback(@myCallback(), 0)
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow: Break
    EndSelect
  ForEver
  
EndIf
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

Much faster! Thanks!

[Edit]
Would this work on Windows98 or anything else besides XP? I don't have any of those to test...
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
eJan
Enthusiast
Enthusiast
Posts: 366
Joined: Sun May 21, 2006 11:22 pm
Location: Sankt Veit am Flaum

Post by eJan »

Inf0Byt3 wrote:Would this work on Windows98 or anything else besides XP? I don't have any of those to test...
I can confirm, yes its working on Windows 98 SE (tested on Virtual PC).
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

eJan wrote:
Inf0Byt3 wrote:Would this work on Windows98 or anything else besides XP? I don't have any of those to test...
I can confirm, yes its working on Windows 98 SE (tested on Virtual PC).
I confirm (on real Win98SE).
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

That's very nice.... Thanks for testing.
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
Post Reply