Re: Comment créer un Screen() avec un fond transparent
Publié : ven. 14/mai/2010 17:37
Oui ça j'avais compris mais je ne comprends pas tellement la formule mathématique. 

Forums PureBasic - Français
http://forums.purebasic.com/french/
Code : Tout sélectionner
temps=GetTickCount_()
For x=0 To 20000000
i=(Int(x / 1) & $1) + 1
Next x
Debug GetTickCount_() -temps
temps=GetTickCount_()
For x=0 To 20000000
i=x%2+1 ;;;;;;;;;;;;;;; beaucoup plus rapide
Next x
Debug GetTickCount_() -temps
SPH a écrit : i=x%2+1 ;;;;;;;;;;;;;;; beaucoup plus rapide
Code : Tout sélectionner
ExamineDesktops()
CreateImage(0, DesktopWidth(0), DesktopHeight(0)) ; Capture d'écran
hDC = StartDrawing(ImageOutput(0))
DeskDC = GetDC_(GetDesktopWindow_())
BitBlt_(hDC, 0, 0, DesktopWidth(0), DesktopHeight(0), DeskDC, 0, 0, #SRCCOPY)
StopDrawing()
ReleaseDC_(GetDesktopWindow_(), DeskDC)
UsePNGImageDecoder()
If OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "", #PB_Window_BorderLess | #PB_Window_Maximize)
If InitSprite() And InitKeyboard()
If OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0), 1, 0, 0, #PB_Screen_WaitSynchronization)
CreateSprite(0, DesktopWidth(0), DesktopHeight(0))
StartDrawing(SpriteOutput(0))
DrawImage(ImageID(0), 0, 0)
StopDrawing()
LoadSprite(1, "E:\Tom\Documents\PureBasic\Pacman01v6.png")
LoadSprite(2, "E:\Tom\Documents\PureBasic\Pacman02v6.png")
LoadSprite(3, "E:\Tom\Documents\PureBasic\Pacman01v6d.png")
LoadSprite(4, "E:\Tom\Documents\PureBasic\Pacman01v6l.png")
LoadSprite(5, "E:\Tom\Documents\PureBasic\Pacman02v6.png")
LoadSprite(6, "E:\Tom\Documents\PureBasic\Pacman01v6u.png")
LoadSprite(7, "E:\Tom\Documents\PureBasic\Pacman02v6.png")
x=1680
y=0
test=1
Repeat
ExamineKeyboard()
DisplaySprite(0, 0, 0)
x + vX
y + vY
vX = 0
vY = 0
If KeyboardPushed(#PB_Key_Right)
vX = 1
Pacman = (Int(x / 30) & $1) + 1
If SpritePixelCollision(Pacman, x, y, 0, 0, 0)
EndIf
EndIf
If KeyboardPushed(#PB_Key_Left)
vX = -1
Pacman = (Int(x / 30) & $1) + 4
If SpritePixelCollision(Pacman, x, y, 0, 0, 0)
EndIf
EndIf
If KeyboardPushed(#PB_Key_Up)
vY = -1
Pacman = (Int(y / 30) & $1) + 6
If SpritePixelCollision(Pacman, x, y, 0, 0, 0)
EndIf
EndIf
If KeyboardPushed(#PB_Key_Down)
vY = 1
Pacman = (Int(y / 30) & $1) + 2
If SpritePixelCollision(Pacman, x, y, 0, 0, 0)
EndIf
EndIf
If KeyboardPushed(#PB_Key_Z)
test = 0
EndIf
If x > DesktopWidth(0)-25
x-3
EndIf
If x < DesktopWidth(0)-DesktopWidth(0)
x+3
EndIf
If y > DesktopHeight(0)-25
y-3
EndIf
If y < DesktopHeight(0)-DesktopHeight(0)
y+3
EndIf
DisplayTransparentSprite(Pacman, x, y)
If test=1
StartDrawing(SpriteOutput(0))
Circle(x+12,y+12,15,#Black)
StopDrawing()
EndIf
FlipBuffers()
Repeat
Event = WindowEvent()
If Event = #PB_Event_CloseWindow
End
EndIf
Until Event = 0
Until KeyboardPushed(#PB_Key_Escape)
EndIf
EndIf
EndIf
Code : Tout sélectionner
ExamineDesktops()
CreateImage(0, DesktopWidth(0), DesktopHeight(0)) ; Capture d'écran
hDC = StartDrawing(ImageOutput(0))
DeskDC = GetDC_(GetDesktopWindow_())
BitBlt_(hDC, 0, 0, DesktopWidth(0), DesktopHeight(0), DeskDC, 0, 0, #SRCCOPY)
StopDrawing()
ReleaseDC_(GetDesktopWindow_(), DeskDC)
UsePNGImageDecoder()
If OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "", #PB_Window_BorderLess | #PB_Window_Maximize)
If InitSprite() And InitKeyboard()
If OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0), 1, 0, 0, #PB_Screen_WaitSynchronization)
CreateSprite(0, DesktopWidth(0), DesktopHeight(0))
StartDrawing(SpriteOutput(0))
DrawImage(ImageID(0), 0, 0)
StopDrawing()
#Pacman_Dimension = 25
; Pacman va vers la droite
CreateSprite(1, #Pacman_Dimension, #Pacman_Dimension)
StartDrawing(SpriteOutput(1))
Circle(#Pacman_Dimension / 2, #Pacman_Dimension / 2, #Pacman_Dimension / 2, $00FFFF)
Circle(#Pacman_Dimension * 2 / 3, #Pacman_Dimension * 1 / 3, 2, $000000)
Line(#Pacman_Dimension / 2, #Pacman_Dimension / 2, #Pacman_Dimension / 2 + 1, 1, $000000)
StopDrawing()
CreateSprite(2, #Pacman_Dimension, #Pacman_Dimension)
StartDrawing(SpriteOutput(2))
Circle(#Pacman_Dimension / 2, #Pacman_Dimension / 2, #Pacman_Dimension / 2, $00FFFF)
Circle(#Pacman_Dimension / 2, #Pacman_Dimension * 1 / 3, 2, $000000)
For n = 0 To #Pacman_Dimension / 2
Line(#Pacman_Dimension / 2 + n, #Pacman_Dimension / 2 + n, #Pacman_Dimension / 2 - n + 1, 1, $000000)
Line(#Pacman_Dimension / 2 + n, #Pacman_Dimension / 2 - n, #Pacman_Dimension / 2 - n + 1, 1, $000000)
Next
StopDrawing()
; Pacman va vers la gauche
CreateSprite(3, #Pacman_Dimension, #Pacman_Dimension)
StartDrawing(SpriteOutput(3))
Circle(#Pacman_Dimension / 2, #Pacman_Dimension / 2, #Pacman_Dimension / 2, $00FFFF)
Circle(#Pacman_Dimension * 1 / 3, #Pacman_Dimension * 1 / 3, 2, $000000)
Line(0, #Pacman_Dimension / 2, #Pacman_Dimension / 2 + 1, 1, $000000)
StopDrawing()
CreateSprite(4, #Pacman_Dimension, #Pacman_Dimension)
StartDrawing(SpriteOutput(4))
Circle(#Pacman_Dimension / 2, #Pacman_Dimension / 2, #Pacman_Dimension / 2, $00FFFF)
Circle(#Pacman_Dimension / 2, #Pacman_Dimension * 1 / 3, 2, $000000)
For n = 0 To #Pacman_Dimension / 2
Line(0, #Pacman_Dimension / 2 + n, #Pacman_Dimension / 2 - n + 1, 1, $000000)
Line(0, #Pacman_Dimension / 2 - n, #Pacman_Dimension / 2 - n + 1, 1, $000000)
Next
StopDrawing()
; Pacman va vers le haut
CreateSprite(5, #Pacman_Dimension, #Pacman_Dimension)
StartDrawing(SpriteOutput(5))
Circle(#Pacman_Dimension / 2, #Pacman_Dimension / 2, #Pacman_Dimension / 2, $00FFFF)
Circle(#Pacman_Dimension * 1 / 3, #Pacman_Dimension * 1 / 3, 2, $000000)
Line(#Pacman_Dimension / 2, 0, 1, #Pacman_Dimension / 2 + 1, $000000)
StopDrawing()
CreateSprite(6, #Pacman_Dimension, #Pacman_Dimension)
StartDrawing(SpriteOutput(6))
Circle(#Pacman_Dimension / 2, #Pacman_Dimension / 2, #Pacman_Dimension / 2, $00FFFF)
Circle(#Pacman_Dimension * 1 / 3, #Pacman_Dimension / 2, 2, $000000)
For n = 0 To #Pacman_Dimension / 2
Line(#Pacman_Dimension / 2 + n, 0, 1, #Pacman_Dimension / 2 - n + 1, $000000)
Line(#Pacman_Dimension / 2 - n, 0, 1, #Pacman_Dimension / 2 - n + 1, $000000)
Next
StopDrawing()
; Pacman va vers le bas
CreateSprite(7, #Pacman_Dimension, #Pacman_Dimension)
StartDrawing(SpriteOutput(7))
Circle(#Pacman_Dimension / 2, #Pacman_Dimension / 2, #Pacman_Dimension / 2, $00FFFF)
Circle(#Pacman_Dimension * 2 / 3, #Pacman_Dimension * 2 / 3, 2, $000000)
Line(#Pacman_Dimension / 2, #Pacman_Dimension / 2, 1, #Pacman_Dimension / 2 + 1, $000000)
StopDrawing()
CreateSprite(8, #Pacman_Dimension, #Pacman_Dimension)
StartDrawing(SpriteOutput(8))
Circle(#Pacman_Dimension / 2, #Pacman_Dimension / 2, #Pacman_Dimension / 2, $00FFFF)
Circle(#Pacman_Dimension * 2 / 3, #Pacman_Dimension / 2, 2, $000000)
For n = 0 To #Pacman_Dimension / 2
Line(#Pacman_Dimension / 2 + n, #Pacman_Dimension / 2 + n, 1, #Pacman_Dimension / 2 - n + 1, $000000)
Line(#Pacman_Dimension / 2 - n, #Pacman_Dimension / 2 + n, 1, #Pacman_Dimension / 2 - n + 1, $000000)
Next
StopDrawing()
x = 0
y = 0
Creuse = 1
Pacman = 1
#Avance = 2
Repeat
ExamineKeyboard()
; Déplacement
vX = 0
vY = 0
If KeyboardPushed(#PB_Key_Right)
vX + #Avance
Pacman = 1
ElseIf KeyboardPushed(#PB_Key_Left)
vX - #Avance
Pacman = 3
ElseIf KeyboardPushed(#PB_Key_Up)
vY - #Avance
Pacman = 5
ElseIf KeyboardPushed(#PB_Key_Down)
vY + #Avance
Pacman = 7
EndIf
; Collision avec le bord de l'écran
If x + vX >= DesktopWidth(0) - #Pacman_Dimension Or x + vX < 0
vX = 0
EndIf
If y + vY >= DesktopHeight(0) - #Pacman_Dimension Or y + vY < 0
vY = 0
EndIf
; Collision avec les tunnels
If Creuse = 0
If SpritePixelCollision(Pacman, x + vX, y + vY, 0, 0, 0)
; Si on touche, on regarde si on ne peut pas aider le déplacement en bougeant un peu le pacman, c'est pour aider lors des changements de directions dans les tunnels en T
If vX And SpritePixelCollision(Pacman, x + vX, y + #Avance, 0, 0, 0) = 0
vY + #Avance
ElseIf vX And SpritePixelCollision(Pacman, x + vX, y - #Avance, 0, 0, 0) = 0
vY - #Avance
ElseIf vY And SpritePixelCollision(Pacman, x + #Avance, y + vY, 0, 0, 0) = 0
vX + #Avance
ElseIf vY And SpritePixelCollision(Pacman, x - #Avance, y + vY, 0, 0, 0) = 0
vX - #Avance
Else ; Sinon, on bloque le déplacement
vX = 0
vY = 0
EndIf
EndIf
EndIf
; On déplace
x + vX
y + vY
; On creuse
If KeyboardReleased(#PB_Key_Space)
Creuse = 1 - Creuse ; Pour faire basculer de 1 à 0 ou de 0 à 1 la valeur
EndIf
If Creuse = 1
StartDrawing(SpriteOutput(0))
Circle(x + #Pacman_Dimension / 2, y + #Pacman_Dimension / 2, #Pacman_Dimension / 2 + #Avance / 2, #Black)
StopDrawing()
EndIf
; On affiche
Pacman_Animation = Int((x + y) / (#Avance * 8)) & $1
DisplaySprite(0, 0, 0)
DisplayTransparentSprite(Pacman + Pacman_Animation, x, y)
FlipBuffers()
; Gestion des évènements windows
Repeat
Event = WindowEvent()
If Event = #PB_Event_CloseWindow
End
EndIf
Until Event = 0
Until KeyboardPushed(#PB_Key_Escape)
EndIf
EndIf
EndIf