Page 1 sur 1

2D et Alpha

Publié : jeu. 29/oct./2009 20:09
par kwandjeen
Bonsoir, pendant mes recherches pour un jeu que je bidouille je me suis amusé à faire quelques trucs avec la gestion de l'alphablend. pb4.40. C'est de la bidouille mais bon :D

Code : Tout sélectionner

Enumeration
  #mouse
EndEnumeration

If ExamineDesktops()
  screen_x = DesktopWidth(0)
  screen_y = DesktopHeight(0)
  tx_grille = (screen_x*2) /3
  ty_grille = (screen_y*4)/5-20
  tx_grille = Round(tx_grille/10,#PB_Round_Down)*10
  ty_grille = Round(ty_grille/10,#PB_Round_Down)*10
EndIf
Global posx_carte = 50
Global posy_carte = 50
Global texte_grille = 1
;-INITIALISATION DIRECTX
If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0 Or InitSprite3D() = 0
  MessageRequester("Error", "Can't open the sprite system", 0)
  End
EndIf

If OpenWindow(0, 0, 0, 1024, 768, "Gadget and sprites!", #PB_Window_SystemMenu |#PB_Window_Maximize|#PB_Window_SizeGadget | #PB_Window_ScreenCentered|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget)

  OpenWindowedScreen(WindowID(0), 0, 0, screen_x, screen_y, 0, 0, 0)
EndIf

Gosub dessin_grille

CreateSprite(#mouse,10,10)
StartDrawing(SpriteOutput(#mouse))
  Circle(2,2,5,RGB(255,255,255))
StopDrawing()

CreateSprite(1,tx_grille/10-1,ty_grille/10-1,#PB_Sprite_Texture)
StartDrawing(SpriteOutput(1))
  Box(0,0,tx_grille/10-1,ty_grille/10-1,RGB(255,255,255))
StopDrawing()
CreateSprite3D(2,1)
;-BOUCLE
Repeat
  event = WindowEvent()
  ClearScreen(RGB(50,0,0))
  souris_x = MouseX()
  souris_y = MouseY()

  If souris_x>posx_carte And souris_x<tx_grille+posx_carte And souris_y>posy_carte And souris_y<ty_grille+posy_carte ;on est sur la grille 
    cox = Round((souris_x-posx_carte)/(tx_grille/10),#PB_Round_Up)
    coy = Round((souris_y-posy_carte)/(ty_grille/10),#PB_Round_Up)
    secteur = (coy-1)*10+cox
    Start3D()
      DisplaySprite3D(2,(cox-1)*(tx_grille/10)+1+posx_carte,(coy-1)*(ty_grille/10)+1+posy_carte,50)
    If texte_grille!1
      StartDrawing(ScreenOutput())
  ;       DrawingMode(#PB_2DDrawing_AlphaBlend)
  ;       Box((cox-1)*(tx_grille/10),(coy-1)*(ty_grille/10),(tx_grille/10),(ty_grille/10),RGBA(255,255,255,100))
        DrawingMode(#PB_2DDrawing_Transparent)
        FrontColor(RGB(100,100,255))
        DrawText((cox-1)*(tx_grille/10)+posx_carte,(coy-1)*(ty_grille/10)+posy_carte,"secteur "+Str(secteur))
      StopDrawing()
    EndIf
    Stop3D()
  EndIf
  If StartDrawing(ScreenOutput())
    DrawAlphaImage(ImageID(1),posx_carte,posy_carte)
    StopDrawing()
  EndIf
  ExamineKeyboard()
  If KeyboardReleased(#PB_Key_A) And KeyboardPushed(#PB_Key_A)=0
    texte_grille = ~texte_grille
    ;Delay(200)
    Gosub dessin_grille
  EndIf
  ExamineMouse()
  DisplaySprite(#mouse,MouseX(),MouseY())
  If MouseButton(#PB_MouseButton_Left)
    cox = Round((souris_x-posx_carte)/(tx_grille/10),#PB_Round_Up)
    coy = Round((souris_y-posy_carte)/(ty_grille/10),#PB_Round_Up)
    secteur = (coy-1)*10+cox
    Debug secteur 
  EndIf
  FlipBuffers()
  If IsScreenActive()!1
    ReleaseMouse(1)
  Else
    ReleaseMouse(0)
  EndIf
Until KeyboardPushed(#PB_Key_Escape) Or event = #PB_Event_CloseWindow
End

;-DESSIN DE LA GRILLE
dessin_grille:
CreateImage(1,tx_grille,ty_grille,32)
StartDrawing(ImageOutput(1))
    DrawingMode(#PB_2DDrawing_AlphaChannel)
    Box(0,0,tx_grille,ty_grille, $00000000)
    
  DrawingMode(#PB_2DDrawing_AlphaBlend)  
  Box(0,0,tx_grille,2,RGBA(80,150,80,255))
  Box(0,0,2,ty_grille,RGBA(80,150,80,255))
  Box(tx_grille-2,0,tx_grille,ty_grille,RGBA(80,150,80,255))
  Box(0,ty_grille-2,tx_grille,ty_grille,RGBA(80,150,80,255))
  incx = tx_grille/10
  incy = ty_grille/10
  For i=0 To 9
    Box(incx*i,0,1,ty_grille,RGBA(80,150,80,128))
    Box(0,incy*i,tx_grille,1,RGBA(80,150,80,128))
  Next i
  DrawingMode(#PB_2DDrawing_AlphaBlend | #PB_2DDrawing_Transparent)
  If texte_grille&1
    For x=0 To 9
      For y=0 To 9
        texte$ = "secteur "+Str(y*10+x+1)
        largeur = (incx-TextWidth(texte$))/2
        DrawText(incx*x+largeur,incy*y,texte$,RGBA(100,100,255,255))
      Next y
    Next x
  EndIf
StopDrawing()
Return
Avec la touche Q ou A vous affichez les infos de secteurs ou pas.