[jeu] lumière 2D

Vous débutez et vous avez besoin d'aide ? N'hésitez pas à poser vos questions
Avatar de l’utilisateur
blendman
Messages : 2017
Inscription : sam. 19/févr./2011 12:46

[jeu] lumière 2D

Message par blendman »

salut

J'ai trouvé sur un forum Russe consacré à Purebasic ce code de Artem (ou Aptem) :

Code : Tout sélectionner

InitSprite()
InitSprite3D()
InitKeyboard()
OpenWindow(0,100,100,800,600,"Light")
OpenWindowedScreen(WindowID(0),0,0,800,600,0,0,0)
Structure foton
  x.f
  y.f
  size.l
  life.l
  a.f
EndStructure 

Structure Light
  id.l
  x.l
  y.l
  life.l
  size.l
  List foton.foton()
EndStructure

Structure stena
  id.l
  x.l
  y.l
  s.l
EndStructure

Global NewList s.stena()

For i=0 To 200
  AddElement(s())
  ix=Random(800)/32
  iy=Random(600)/32
s()\x=Random(800)
s()\y=Random(600)
s()\s=32
s()\id=CreateSprite(#PB_Any,32,32)
Next

CatchSprite(0,?b1)



Global NewList light.light(),LightB

Procedure InitLight()
  LightB=CreateSprite(#PB_Any,800,600,#PB_Sprite_Texture)
  CreateSprite3D(0,LightB)
  LightC=2
EndProcedure

Procedure ExamineLight()
  Start3D()
    Sprite3DBlendingMode(9,8)
    DisplaySprite3D(LightC,0,0)
  Stop3D()
  StartDrawing(SpriteOutput(LightB))
    Box(0,0,800,600,RGB(0,0,0))
  StopDrawing()
  ForEach Light()
    With light()
      c.f=(359/50)
      If ListSize(light()\foton())<50
      For i=1 To 51
        AddElement(light()\foton())
        \foton()\life=\life
        \foton()\size=\size
        \foton()\x=\x
        \foton()\y=\y
        \foton()\a=c*i
      Next
      EndIf
    EndWith
  Next
EndProcedure

Procedure AddLight(id, x, y, life, size)
 
  AddElement(light())
    With light()
      \x=x
      \y=y
      \id=id
      \life=life
      \size=size
    EndWith
 
EndProcedure

Procedure DrawLight(id,x,y)
  StartDrawing(SpriteOutput(LightB))
  ForEach Light()
    If light()\id=id
    light()\x=x
    light()\y=y
    With light()
      While ListSize(\foton())>0
      ForEach \foton()
        \foton()\x=\foton()\x+Cos(\foton()\a*#PI/180)*(\foton()\size)
        \foton()\y=\foton()\y+Sin(\foton()\a*#PI/180)*(\foton()\size)
        \foton()\life-(\foton()\size/2)
        ForEach s()
          If \foton()\x<s()\x+s()\s And \foton()\x>s()\x
            If \foton()\y<s()\y+s()\s And \foton()\y>s()\y
              \foton()\life=0
            EndIf
          EndIf
        Next
        If \foton()\life>0
          cof=(255/light()\life)*\foton()\life
          Circle(\foton()\x,\foton()\y,\foton()\size,RGB(cof,cof,cof))
        EndIf
        If \foton()\life<=0
          DeleteElement(\foton())
        EndIf
      Next
      Wend
    EndWith
    EndIf
  Next
  StopDrawing()
EndProcedure

Procedure FPS()
  Shared s, fps
  ss=Second(Date())
  fps+1
  If s<>ss
    s=ss
    SetWindowTitle(0,"Свет   FPS: "+Str(FPS))
    fps=0
  EndIf
EndProcedure

InitLight()
AddLight(1, 400, 300, 50, 10)
AddLight(2, 400, 300, 50, 10)

ForEach s()
  StartDrawing(SpriteOutput(s()\id))
    Box(0,0,64,64,RGB(255,Random(255),0))
  StopDrawing()
Next

Repeat
  FPS()
  event=WaitWindowEvent(1)
  FlipBuffers()
  ClearScreen(RGB(0,0,0))
 
  ExamineKeyboard()
 
  For x=0 To 800 Step 146
  For y=0 To 600 Step 146
    DisplaySprite(0,x,y)
  Next
  Next
 
  ForEach s()
    DisplaySprite(s()\id,s()\x, s()\y)
  Next
 
  ExamineLight()
 
  DrawLight(2,400, 300)
 
  x=WindowMouseX(0)
  y=WindowMouseY(0)
  DrawLight(1,x,y)
 
  If event=#WM_CLOSE Or KeyboardPushed(#PB_Key_Escape)
    q=1
  EndIf 
Until q=1
End

DataSection
b1:
IncludeBinary "1.bmp"
EndDataSection 
Je le trouve pas mal du tout, j'ai essayé d'utiliser des sprites 3D pour faire des ombres/lights plus soft, mais je n'ai pas encore réussi, si vous y parvenez, n'hésitez pas à poster le résultat ;).

PS : le lien original : http://purebasic.info/phpBB2/viewtopic.php?t=1889

EDIT : Bon, en bidouillant un peu le code et en passant par des sprites3D plutôt que par des circle(), on arrive à des résultats fabulous breakdown :D, la preuve :
Image

En plus, je suis passé à 60fps au lieu de 32, grâce aux sprites3D :)
Je croyais que ce n'était pas possible en purebasic, mais visiblement c'est tout à fait possible, il est puissant ce Purebasic :)
Avatar de l’utilisateur
Atlante
Messages : 337
Inscription : mer. 29/juin/2011 18:35
Localisation : Paris

Re: [jeu] lumière 2D

Message par Atlante »

Sympa comme effet ;)
Modérateur
Config : Intel I5 4670K, Nvidia Geforce GTX 1060, 16go RAM, SSD 256go, DD 2000go
Avatar de l’utilisateur
blendman
Messages : 2017
Inscription : sam. 19/févr./2011 12:46

Re: [jeu] lumière 2D

Message par blendman »

et c'est encore mieux en live :
http://vimeo.com/32071358
:D
Avatar de l’utilisateur
venom
Messages : 3138
Inscription : jeu. 29/juil./2004 16:33
Localisation : Klyntar
Contact :

Re: [jeu] lumière 2D

Message par venom »

8O Impressionnant en effet Bravo.






@++
Windows 10 x64, PureBasic 5.73 x86 & x64
GPU : radeon HD6370M, CPU : p6200 2.13Ghz
Avatar de l’utilisateur
Ar-S
Messages : 9540
Inscription : dim. 09/oct./2005 16:51
Contact :

Re: [jeu] lumière 2D

Message par Ar-S »

Super !
~~~~Règles du forum ~~~~
⋅.˳˳.⋅ॱ˙˙ॱ⋅.˳Ar-S ˳.⋅ॱ˙˙ॱ⋅.˳˳.⋅
W11x64 PB 6.x
Section HORS SUJET : ICI
LDV MULTIMEDIA : Dépannage informatique & mes Logiciels PB
UPLOAD D'IMAGES : Uploader des images de vos logiciels
Répondre