J'ai loupé Windows
Publié : dim. 09/janv./2005 12:49
Une petite loupe pour windows
Flèche haut et bas pour agrandir la loupe
Flèche gauche et droite pour changer le zoom
F1 pour voir le FPS
il s'agit d'un dérivé de ce code :
http://purebasic.hmt-forum.com/viewtopic.php?t=1596&
Je l'ai optimiser (FPS * 2 par raport à l'ancien code) et j'ai changé l'effet pour avoir qlqchose qui ressemble plus à une loupe.
ici, il est en version avec lissage, pour les petites config, suffit de passer avec la version sans lissage en jouant sur les commentaires
Flèche haut et bas pour agrandir la loupe
Flèche gauche et droite pour changer le zoom
F1 pour voir le FPS
il s'agit d'un dérivé de ce code :
http://purebasic.hmt-forum.com/viewtopic.php?t=1596&
Je l'ai optimiser (FPS * 2 par raport à l'ancien code) et j'ai changé l'effet pour avoir qlqchose qui ressemble plus à une loupe.
Code : Tout sélectionner
; Auteur : Le Soldat Inconnu
; Version de PB : 3.91
;
; Explication du programme :
; Fait un effet de loupe sur votre écran (comme ci une sphèse se déplacant dessus en déformant l'image)
; Utiliser les flèhes pour changer les paramètres
; F1 pour afficher les paramètres
#Pi.f = 3.14159265
; Calcul préliminaire
Global Zoom.f
#DefinitionConversion = 2000
Dim Conversion.f(#DefinitionConversion)
Procedure CalculConversion()
For n = 0 To #DefinitionConversion
Conversion(n) = 1 / (1 + Zoom * Cos(n / #DefinitionConversion * #Pi / 4))
Next
EndProcedure
ProcedureDLL.l GetImageBits2(ImageID, HList) ; Transfert d'une image vers un tableau
Protected bmi.BITMAPINFO, Hdc.l, Resultat, Mem, n, nn, bm.BITMAP, Temp1, Temp2, Temp3, Temp4, Temp5
Resultat = 0
GetObject_(ImageID, SizeOf(BITMAP), @bm.BITMAP)
bmi\bmiHeader\biSize = SizeOf(BITMAPINFOHEADER)
bmi\bmiHeader\biWidth = bm\bmWidth
bmi\bmiHeader\biHeight = bm\bmHeight
bmi\bmiHeader\biPlanes = 1
bmi\bmiHeader\biBitCount = 32
bmi\bmiHeader\biCompression = #BI_RGB
Mem = AllocateMemory(bm\bmWidth * bm\bmHeight * 4)
If Mem
Hdc = CreateCompatibleDC_(GetDC_(ImageID))
If Hdc
GetDIBits_(Hdc, ImageID, 0, bm\bmHeight, Mem, @bmi, #DIB_RGB_COLORS) ; on envoie la liste dans l'image
ReleaseDC_(0, Hdc)
Resultat = ImageID
EndIf
; On convertit la liste dans le bon format
; On convertit la liste dans le bon format
Temp1 = bm\bmHeight * 4
Temp2 = bm\bmWidth * 4
Temp3 = bm\bmHeight - 1
For n = 0 To bm\bmHeight - 1
For nn = 0 To bm\bmWidth - 1
Temp4 = HList + n * 4 + nn * Temp1
Temp5 = Mem + nn * 4 + (Temp3 - n) * Temp2
PokeB(Temp4 + 2, PeekB(Temp5))
PokeB(Temp4 + 1, PeekB(Temp5 + 1))
PokeB(Temp4, PeekB(Temp5 + 2))
Next
Next
FreeMemory(Mem)
EndIf
ProcedureReturn Resultat
EndProcedure
; Copie d'écran
Largeur_Ecran = GetSystemMetrics_(#SM_CXSCREEN)
Hauteur_Ecran = GetSystemMetrics_(#SM_CYSCREEN)
DC = GetDC_(0)
CreateImage(0, Largeur_Ecran, Hauteur_Ecran)
Dessin = StartDrawing(ImageOutput())
BitBlt_(Dessin, 0, 0, Largeur_Ecran, Hauteur_Ecran, DC, 0, 0, #SRCPAINT)
StopDrawing()
ReleaseDC_(0, DC)
; On copie l'image dans un Tableau
Dim Image.l(Largeur_Ecran - 1, Hauteur_Ecran - 1)
GetImageBits2(UseImage(0), @Image())
; On ouvre l'openscreen
If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
MessageRequester("Erreur", "Impossible d'initialiser la souris ,le clavier ou l'écran. Vérifiez la présence de DirectX 7 ou supérieur.", 0)
End
EndIf
If OpenScreen(Largeur_Ecran, Hauteur_Ecran, 32, "Sphère") = 0
MessageRequester("Erreur", "Impossible d'ouvrir l'écran.", 0)
End
EndIf
; Création du sprite représentant l'écran
CreateSprite(1, Largeur_Ecran, Hauteur_Ecran)
StartDrawing(SpriteOutput(1))
DrawImage(UseImage(0), 0, 0)
StopDrawing()
; Initialisation des variables
#ZoomMax = 4
Zoom = 1
CalculConversion()
#RayonMax = 400
Rayon = 100
Repeat
; On lit les évènements clavier et souris
ExamineMouse()
ExamineKeyboard()
; Position de la souris
x = MouseX()
y = MouseY()
; Agrandir ou réduire la loupe
If KeyboardReleased(#PB_Key_Up) And Rayon < #RayonMax
Rayon + 20
ElseIf KeyboardReleased(#PB_Key_Down) And Rayon > 0
Rayon - 20
EndIf
; Changer le zoom
If KeyboardReleased(#PB_Key_Right) And Zoom < #ZoomMax
Zoom + 0.25
CalculConversion()
ElseIf KeyboardReleased(#PB_Key_Left) And Zoom > 0
Zoom - 0.25
CalculConversion()
EndIf
; On affiche l'image
DisplaySprite(1, 0, 0)
StartDrawing(ScreenOutput())
; Calcul du FPS
If KeyboardPushed(#PB_Key_F1)
Cpt + 1
If Cpt >= FPS.f / 2
FPS = Cpt * 1000 / (ElapsedMilliseconds() - Temps)
Cpt = 0
Temps = ElapsedMilliseconds()
EndIf
Locate(5, 5)
DrawText("FPS = " + StrF(FPS, 1))
Locate(5, 20)
DrawText("Rayon = " + Str(Rayon))
Locate(5, 35)
DrawText("Zoom = " + StrF(Zoom + 1, 2))
EndIf
; On édite l'image
Memoire = DrawingBuffer()
For n = -Rayon To Rayon
x2 = x + n
If x2 > 0 And x2 < Largeur_Ecran - 1 ; Si on est sur l'image en x
For nn = -Rayon To Rayon
y2 = y + nn
If y2 > 0 And y2 < Hauteur_Ecran - 1 ; Si on est sur l'image en y
Longueur.f = Sqr(n * n + nn * nn) ; On calcul la distance d'un point de la sphère à partir du centre
If Rayon - Longueur <= 1 And Rayon - Longueur >= -1 ; On dessine le coutour de la loupe
Plot(x2, y2, 0)
ElseIf Longueur <= Rayon ; Si le pixel est situé dans le rayon du cercle
Longueur2.f = Conversion(Int((Longueur / Rayon) * #DefinitionConversion)) ; on calcul la distance du point de l'image correspondant à celui de la sphère
; Avec lissage
PosX.f = x + n * Longueur2
PosY.f = y + nn * Longueur2
PosX_Int.l = PosX
PosY_Int.l = PosY
PosX_Ecart.f = Abs(PosX - PosX_Int)
PosY_Ecart.f = Abs(PosY - PosY_Int)
PosX_EcartInv.f = 1 - PosX_Ecart
PosY_EcartInv.f = 1 - PosY_Ecart
If PosX >= PosX_Int
If PosY >= PosY_Int ; bas, droite
Rouge = Red(Image(PosX_Int, PosY_Int)) * PosX_EcartInv * PosY_EcartInv + Red(Image(PosX_Int + 1, PosY_Int)) * PosX_Ecart * PosY_EcartInv + Red(Image(PosX_Int, PosY_Int + 1)) * PosX_EcartInv * PosY_Ecart + Red(Image(PosX_Int + 1, PosY_Int + 1)) * PosX_Ecart * PosY_Ecart
Vert = Green(Image(PosX_Int, PosY_Int)) * PosX_EcartInv * PosY_EcartInv + Green(Image(PosX_Int + 1, PosY_Int)) * PosX_Ecart * PosY_EcartInv + Green(Image(PosX_Int, PosY_Int + 1)) * PosX_EcartInv * PosY_Ecart + Green(Image(PosX_Int + 1, PosY_Int + 1)) * PosX_Ecart * PosY_Ecart
Bleu = Blue(Image(PosX_Int, PosY_Int)) * PosX_EcartInv * PosY_EcartInv + Blue(Image(PosX_Int + 1, PosY_Int)) * PosX_Ecart * PosY_EcartInv + Blue(Image(PosX_Int, PosY_Int + 1)) * PosX_EcartInv * PosY_Ecart + Blue(Image(PosX_Int + 1, PosY_Int + 1)) * PosX_Ecart * PosY_Ecart
Else ; haut, droite
Rouge = Red(Image(PosX_Int, PosY_Int)) * PosX_EcartInv * PosY_EcartInv + Red(Image(PosX_Int + 1, PosY_Int)) * PosX_Ecart * PosY_EcartInv + Red(Image(PosX_Int, PosY_Int - 1)) * PosX_EcartInv * PosY_Ecart + Red(Image(PosX_Int + 1, PosY_Int - 1)) * PosX_Ecart * PosY_Ecart
Vert = Green(Image(PosX_Int, PosY_Int)) * PosX_EcartInv * PosY_EcartInv + Green(Image(PosX_Int + 1, PosY_Int)) * PosX_Ecart * PosY_EcartInv + Green(Image(PosX_Int, PosY_Int - 1)) * PosX_EcartInv * PosY_Ecart + Green(Image(PosX_Int + 1, PosY_Int - 1)) * PosX_Ecart * PosY_Ecart
Bleu = Blue(Image(PosX_Int, PosY_Int)) * PosX_EcartInv * PosY_EcartInv + Blue(Image(PosX_Int + 1, PosY_Int)) * PosX_Ecart * PosY_EcartInv + Blue(Image(PosX_Int, PosY_Int - 1)) * PosX_EcartInv * PosY_Ecart + Blue(Image(PosX_Int + 1, PosY_Int - 1)) * PosX_Ecart * PosY_Ecart
EndIf
Else
If PosY >= PosY_Int ; bas, gauche
Rouge = Red(Image(PosX_Int, PosY_Int)) * PosX_EcartInv * PosY_EcartInv + Red(Image(PosX_Int - 1, PosY_Int)) * PosX_Ecart * PosY_EcartInv + Red(Image(PosX_Int, PosY_Int + 1)) * PosX_EcartInv * PosY_Ecart + Red(Image(PosX_Int - 1, PosY_Int + 1)) * PosX_Ecart * PosY_Ecart
Vert = Green(Image(PosX_Int, PosY_Int)) * PosX_EcartInv * PosY_EcartInv + Green(Image(PosX_Int - 1, PosY_Int)) * PosX_Ecart * PosY_EcartInv + Green(Image(PosX_Int, PosY_Int + 1)) * PosX_EcartInv * PosY_Ecart + Green(Image(PosX_Int - 1, PosY_Int + 1)) * PosX_Ecart * PosY_Ecart
Bleu = Blue(Image(PosX_Int, PosY_Int)) * PosX_EcartInv * PosY_EcartInv + Blue(Image(PosX_Int - 1, PosY_Int)) * PosX_Ecart * PosY_EcartInv + Blue(Image(PosX_Int, PosY_Int + 1)) * PosX_EcartInv * PosY_Ecart + Blue(Image(PosX_Int - 1, PosY_Int + 1)) * PosX_Ecart * PosY_Ecart
Else ; haut, gauche
Rouge = Red(Image(PosX_Int, PosY_Int)) * PosX_EcartInv * PosY_EcartInv + Red(Image(PosX_Int - 1, PosY_Int)) * PosX_Ecart * PosY_EcartInv + Red(Image(PosX_Int, PosY_Int - 1)) * PosX_EcartInv * PosY_Ecart + Red(Image(PosX_Int - 1, PosY_Int - 1)) * PosX_Ecart * PosY_Ecart
Vert = Green(Image(PosX_Int, PosY_Int)) * PosX_EcartInv * PosY_EcartInv + Green(Image(PosX_Int - 1, PosY_Int)) * PosX_Ecart * PosY_EcartInv + Green(Image(PosX_Int, PosY_Int - 1)) * PosX_EcartInv * PosY_Ecart + Green(Image(PosX_Int - 1, PosY_Int - 1)) * PosX_Ecart * PosY_Ecart
Bleu = Blue(Image(PosX_Int, PosY_Int)) * PosX_EcartInv * PosY_EcartInv + Blue(Image(PosX_Int - 1, PosY_Int)) * PosX_Ecart * PosY_EcartInv + Blue(Image(PosX_Int, PosY_Int - 1)) * PosX_EcartInv * PosY_Ecart + Blue(Image(PosX_Int - 1, PosY_Int - 1)) * PosX_Ecart * PosY_Ecart
EndIf
EndIf
Couleur = RGB(Rouge, Vert, Bleu)
; Sans lissage
; Couleur.l = Image(Int(x + n * Longueur2 + 0.5), Int(y + nn * Longueur2 + 0.5))
; On affiche le pixel
Plot(x2, y2, Couleur)
EndIf
EndIf
Next
EndIf
Next
StopDrawing()
FlipBuffers()
If IsScreenActive() = 0
End
EndIf
Until KeyboardPushed(#PB_Key_Escape)