Page 1 of 1

Posted: Tue Dec 03, 2002 3:46 pm
by BackupUser
Restored from previous forum. Originally posted by duschman.

I found the problem with the thread.
It seems that it needs an extra OpenScreen()
in the thread function. But thats really stupid
because I just nead a small area where the counter is
diplayed.

So how do I make a counter in purebasic running in the back
of the programm and doesn't slow down my
other parts of the program?

THX for comments

Martin Strojek

Posted: Tue Dec 03, 2002 4:42 pm
by BackupUser
Restored from previous forum. Originally posted by WolfgangS.

Hi
Open a Window with #PB_Window_BorderLess and generate a thread with
ThreadID = CreateThread(@ProcedureName(), Wert)

MFG
:)WolfgangS

Posted: Tue Dec 03, 2002 7:25 pm
by BackupUser
Restored from previous forum. Originally posted by fred.

PB graphics functions are not safe when used in thread (I don't know if DX one are safe), and using thread to display gfx, is IHMO not a good idea. To get a counter, just put it in your main loop..

Fred - AlphaSND

Posted: Tue Dec 03, 2002 8:22 pm
by BackupUser
Restored from previous forum. Originally posted by plouf.

you can also use timer lib's fuctions to count a
global variable and draw it from the main loop

Christos

Posted: Tue Dec 03, 2002 8:30 pm
by BackupUser
Restored from previous forum. Originally posted by Berikco.

I never used the DX stuff before, here is my first attempt

Code: Select all

Global counter
counter=60

Procedure myCallback(WindowID, Message, wParam, lParam)
  Result = #PB_ProcessPureBasicEvents
  Select Message
    Case #WM_TIMER
      Select wParam
        Case 1  ; timer number 1
          beep_(500,50)
          counter-1
          result=0
      EndSelect
  EndSelect
  ProcedureReturn Result
EndProcedure

Procedure KeyCheck()
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Escape)
    End
  EndIf
EndProcedure

hwnd=OpenWindow(0,100,350,400,400, #PB_Window_Invisible         ,"testing")
If hwnd
  SetWindowCallback(@MyCallback())
  
  SetTimer_(hWnd,1,1000,0) ; 1 sec timer
  If InitSprite() = 0
    MessageRequester("Error!","InitSprite failed!",0)
    End
  EndIf
  
  If InitKeyboard()=0
    MessageRequester("Error!","InitKeyboard failed!",0)
    End
  EndIf
  
  If OpenScreen(800,600,32,"counter") = 0
    MessageRequester("Error","OpenScreen failed!",0)
    End
  EndIf
  
  Repeat
    WindowEvent()
    FlipBuffers()
    ClearScreen(0,0,0)
    KeyCheck()
    
    If StartDrawing(ScreenOutput())
      Locate(700, 20)
      DrawText("Time:"+Str(counter))
      StopDrawing()
    EndIf
    
  ForEver
  CloseScreen()
EndIf
Regards,

Berikco

http://www.benny.zeb.be

Posted: Tue Dec 03, 2002 8:44 pm
by BackupUser
Restored from previous forum. Originally posted by fred.

Why not use GetTickCount_() which is the windows internal counter ?

Fred - AlphaSND

Posted: Tue Dec 03, 2002 9:38 pm
by BackupUser
Restored from previous forum. Originally posted by Franco.
Originally posted by fred

PB graphics functions are not safe when used in thread (I don't know if DX one are safe), and using thread to display gfx, is IHMO not a good idea. To get a counter, just put it in your main loop..

Fred - AlphaSND
@Fred
In September I sent you a personal email to AlphaSND regarding problems with Threads, Sprites, and OpenWindowedScreen specially under WinXP - and NEVER got an answer.

I wasted a lot of time searching for a bug (and didn't found one...).

Now this statement of yours.
Thanks.

:) :cry: :) :)

BTW: It was not a simple counter...

Posted: Tue Dec 03, 2002 9:49 pm
by BackupUser
Restored from previous forum. Originally posted by fred.
Originally posted by Franco
In September I sent you a personal email to AlphaSND regarding problems with Threads, Sprites, and OpenWindowedScreen specially under WinXP - and NEVER got an answer.

I wasted a lot of time searching for a bug (and didn't found one...).

Now this statement of yours.
Thanks.

:) :cry: :) :)

BTW: It was not a simple counter...
Sorry, I should have skipped it..

Fred - AlphaSND

Posted: Tue Dec 03, 2002 11:24 pm
by BackupUser
Restored from previous forum. Originally posted by MrVainSCL.

hi duschman
sorry i dont really understand your posting... if you need an area for displaying any stuff like your counter while coding/debugging i.e. you can print it to your gamescreen with DrawText() for example... If you want just only see the vals in an seperate window for testing, run your game (screen) in WinowedMode and print the counter stuff in an console with OpenConsole for example...

If you need a counter for something in the background, just use it in your mainloop... So you can
count your value but its still hidden for the user...



Hi Berikco and Fred
TimeGetTime_() is more exact as GetTickCount_() !! So you should better use TimeGetTime() for any internal counters... If you want move any objects in same speed on all PCs (based on timer and fps independent instead of setting a fixed fps) you should take a look to the tips and tricks section where i posed a small example how to move objects in same speed on all PCs but fps independent...




PIII450, 256MB Ram, 80GB HD + 6,4 GB, RivaTNT, DirectX8.1, SB AWE64, Win2000 + all Updates...

greetz
MrVainSCL! aka Thorsten

Posted: Wed Dec 04, 2002 4:01 am
by BackupUser
Restored from previous forum. Originally posted by PB.

> TimeGetTime_() is more exact as GetTickCount_()

True, but it's almost 8 times slower than using GetTickCount_():

http://www.mvps.org/directx/articles/se ... ctions.htm

Posted: Wed Dec 04, 2002 6:40 am
by BackupUser
Restored from previous forum. Originally posted by Danilo.

Code: Select all

Procedure ColorChanger()
Shared color, colorflag, counter
   If colorflag = 0
      color + 10
      If color => 255 : color = 255 : colorflag = 1 : EndIf
   Else
      color - 3
      If color <= 20  : color = 20  : colorflag = 0 : EndIf
   EndIf
   counter + 1
EndProcedure

Procedure KeyCheck()
  If KeyboardPushed(#PB_Key_Escape)
     End
  EndIf
EndProcedure

If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
   MessageRequester("ERROR","Cant initialize DirectX!",0) : End
EndIf

#SCREENNAME.s = "TEST"

If OpenScreen(800,600,32,#SCREENNAME) = 0
  If OpenScreen(800,600,24,#SCREENNAME) = 0
    If OpenScreen(800,600,16,#SCREENNAME) = 0
      MessageRequester("ERROR","Cant open Screen!",0) : End
EndIf:EndIf:EndIf

StartTimer(1,40,@ColorChanger())

Repeat
    FlipBuffers()
    ExamineKeyboard()
    If IsScreenActive()
       ClearScreen(0,0,0)
       KeyCheck()
       ExamineMouse()
       hDC = StartDrawing(ScreenOutput())
         Locate(MouseX(),MouseY())
         DrawingMode(1)
         FrontColor(color,color,color)
         DrawText("PureBasic")
         Locate(350,20)
         FrontColor($FF,$FF,color)
         DrawText("Counter: "+StrU(counter,2))
       StopDrawing()
    Else
       Delay(10)
    EndIf
ForEver
This is using a high resolution timer.

The Timer is running in its own thread.
Dont use DirectX functions in threads, only
change variables, and check the variables in
your game-loop.

cya,
...Danilo

(registered PureBasic user)

Posted: Wed Dec 04, 2002 5:15 pm
by BackupUser
Restored from previous forum. Originally posted by duschman.

Hi all,

thanks for your comments. Maybe it helps if I say
what I'm trying to do:

I want to convert a C64 game and for solving the game
you have just a little time. The first thing, my counter isn't
just a text but some sprite for each digit of the actual counter.
So I swap my counter value into digits and load sprite for the numbers. But thats not the problem, it works fine.

I just wanted to be sure that the timer is running completely in the back and all other would be handled from the main loop of the program.
I don't know if there's a problem counting in the main loop, but I wanted to be sure that the counter will run independent. I use some delays of about 20 because I have to slow down moving some time to have a good speed for gameplay. So that would slow down my timer, too.