An approach using threads, some advice please.

Advanced game related topics
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Thanks on that.

I butchered the Sprite3D example to see if I could split input and engine into threads. Seems to be ok.

Just used a delay to slow down the x increments otherwise it is over before it started. :)

Code: Select all

Global start.l
Global done.l
Global x.l
Global threadAI.l
Global threadUI.l
Global mutexAll.l

Procedure setUp()
  LoadSprite(0, "C:\100\pb400\Examples\Sources\Data\Geebee2.bmp", #PB_Sprite_Texture)
  CreateSprite3D(0, 0)
  CreateSprite3D(1, 0)
  CreateSprite3D(2, 0)
  Sprite3DQuality(1)
  TransparentSpriteColor(0, RGB(255, 0, 255))
  MouseLocate(320,240)
  mutexAll = CreateMutex()
EndProcedure

Procedure userInput(p.l)
  Repeat
    Delay(1)
    ExamineKeyboard()
    ExamineMouse()
  Until KeyboardPushed(#PB_Key_Escape) Or MouseX()<=0 Or done
  LockMutex(mutexAll)
  done = #True
  UnlockMutex(mutexAll)
EndProcedure

Procedure engineAI(p.l)
  While Not start : Wend
  Repeat
    Delay(15)                         ; This pretends to be careful timing :)
    x + 1
  Until x > 500 Or done
  LockMutex(mutexAll)
  done = #True
  UnlockMutex(mutexAll)
EndProcedure

Procedure renderScreen()
  start = #True
  Repeat
    FlipBuffers()
    ClearScreen(RGB(0,50,128))
    If Start3D()
      DisplaySprite3D(0, 0, 30)
      DisplaySprite3D(0, x+100, 100, x)
      DisplaySprite3D(0, x*2, 100, x)
      ZoomSprite3D(1, x, x)
      RotateSprite3D(1, x, 0)
      DisplaySprite3D  (1, 0, 100, x/2)
      DisplaySprite3D  (1, x*2, 100, x)
      DisplaySprite3D  (1, 0, 100, x/2)
      DisplaySprite3D  (1, x*2, 200+x, x)
      Stop3D()
    EndIf
  Until done
EndProcedure

If InitSprite()
  If InitKeyboard()
    If InitMouse()
      If InitSprite3D()
        If OpenScreen(640, 480, 16, "Sprite")
          setUp()
          threadAI = CreateThread(@userInput(),1)
          threadUI = CreateThread(@engineAI(),1)
          renderScreen()
        EndIf
      EndIf
    EndIf
  EndIf
EndIf

WaitThread(threadAI,1000)
WaitThread(threadUI,1000)

FreeMutex(mutexAll)

MessageRequester("?",Str(IsThread(threadAI))+" "+Str(IsThread(threadUI)))
End
Edit: If you run this, change the absolute path to where your pb examples data is for the image.
Dare2 cut down to size
Post Reply