[Allergo] Timer?

External libraries (GTK, GSL, SDL, Allegro, OpenGL, etc) questions.
Swos2008
Posts: 91
Joined: May 15, 2008 13:21

[Allergo] Timer?

Postby Swos2008 » Mar 31, 2010 16:05

this is c++ Allergo

Code: Select all

volatile long speed_counter = 0;
                                             
void increment_speed_counter()
{
  speed_counter++;
}
END_OF_FUNCTION(increment_speed_counter);                             


so I try to convert to Freebasic allergo

Code: Select all

dim volatile speed_counter as long

speed_counter=0

sub  increment_speed_counter
  speed_counter=+1
end sub
END_OF_FUNCTION(increment_speed_counter)


I am not sure if I have got it right...
j_milton
Posts: 458
Joined: Feb 11, 2010 17:35

Postby j_milton » Mar 31, 2010 16:37

"volatile" is not part of freebasic, but this may well be close enough; I don't know the Allergo package however. It will make the variable "speed_counter" visible across modules and from within subs and functions without having to pass it as a parameter

Code: Select all

Common Shared speed_counter As  long

speed_counter = 0

Sub  increment_speed_counter
  speed_counter += 1
End Sub


In order for this to work though you also need to add a matching common statement in all the other modules that make use of the variable
Swos2008
Posts: 91
Joined: May 15, 2008 13:21

Postby Swos2008 » Mar 31, 2010 17:02

thank you for code J_milton :)

So far I put timer in my game

I have notice that freebasic allergo dont notice this one when i convert from C++ Allergo to freebasic allergo as I am making sure that my game run 60FPS on every pc :)

install_int_ex(increment_speed_counter, BPS_TO_TIMER(60))

freebasic allergo doesnt know this one above code :(

Code: Select all

#include "allegro.bi"
Option Explicit

#define WHITE makecol(255,255,255)
#define SCREEN_WIDTH  640                     
#define SCREEN_HEIGHT 480
#define Depth 32
#define Speed 3

Type Daft_Dog
    X AS integer
    Y as integer
end type

Type Stick
    X2 as integer
    Y2 as integer
end type

'Now we can have variables like Doggy.X, Doggy.Y
'------------------------------------------------------------------------------
Dim shared Doggy  As Daft_Dog
Dim shared Sticky As Stick
'------------------------------------------------------------------------------

'------------------------------------------------------------------------------
Dim shared Buffer       As bitmap ptr
Dim shared Background   AS Bitmap ptr
Dim shared Dog          As Bitmap Ptr
Dim shared Stick        As Bitmap Ptr
'------------------------------------------------------------------------------

'------------------------------------------------------------------------------
' You must Declare the function without you will get error when you run it!
'------------------------------------------------------------------------------
Declare Function Allergo_Setup
Declare Function Load_Images
Declare Function Move_Player
Declare Function Render_Sprites
Declare Function Shut_down
'------------------------------------------------------------------------------

Common Shared speed_counter As  Long

speed_counter = 0


Allergo_Setup

Load_Images

' Varibles
Doggy.x = SCREEN_W/2 - Dog->w/2 : Doggy.y = SCREEN_H/2 - Dog->h/2

Sticky.x2 = 200 : Sticky.y2 = 200

' Timer
LOCK_VARIABLE(speed_counter) ' Used to set the timer - which regulates the game's
LOCK_FUNCTION(increment_speed_counter)' speed.
install_int_ex(increment_speed_counter, BPS_TO_TIMER(60))' Set our BPS
   
' main loop
while (not key(KEY_ESC))
    while(speed_counter > 0)

        Move_Player    ' Move the Daft Dog..heh heh
   
        Render_Sprites ' Draw everythings   
       
         speed_counter +1
    wend
wend

Shut_down

'------------------------------------------------------------------------------     
' All the functions blow on what I have made!
'------------------------------------------------------------------------------
function Allergo_Setup
    allegro_init
    install_keyboard
    install_timer
    set_color_depth Depth
    set_gfx_mode GFX_AUTODETECT_WINDOWED, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0
end function

function Load_Images
    Background = load_bitmap("GFX\grass.bmp", NULL)
   Dog        = load_bitmap("GFX\Dog.bmp"       , NULL)       
    Stick      = load_bitmap("GFX\Stick.bmp"     , NULL)
    buffer = create_bitmap(640,480)
end function

function Move_Player
      if (key(KEY_UP)) then
          Doggy.Y=Doggy.Y-speed
      endif
     
      if (key(KEY_Down)) then
          Doggy.Y=Doggy.Y+speed
      endif
     
      if (key(KEY_LEFT)) then
          Doggy.X=Doggy.X-speed
      endif
     
      if (key(KEY_RIGHT)) then
          Doggy.X=Doggy.X+speed
      endif
     
      ' To stop the doggy moving off the player area
      IF Doggy.X<0    Then Doggy.X=0
      IF Doggy.X>=610 Then Doggy.X=610     
     
      IF DOGGY.Y<0    Then Doggy.Y=0
      IF DOGGY.Y>=450 Then Doggy.Y=450
       
end function

Function Render_Sprites   
     ' draw the sprite   
     clear_bitmap(buffer)
     draw_sprite(buffer, Background, 0,0)     
     draw_sprite(buffer, Dog, Doggy.x, Doggy.y)
     draw_sprite(buffer, Stick, Sticky.x2, Sticky.y2)
       
      vsync  '<- I added this

      blit(buffer, Screen, 0,0,0,0,640,480)
         
End Function

'-------------------------------------------------------------------------------
' Timer
'-------------------------------------------------------------------------------
Sub  incRement_speed_counter
  speed_counter += 1
end Sub


function Shut_down
   
    ' delete the bitmap
    destroy_bitmap(buffer)
    destroy_bitmap(Background)
    destroy_bitmap(dog)
    destroy_bitmap(Stick)
    remove_keyboard
    allegro_exit
   
end function
'------------------------------------------------------------------------------
'                                 THE END
'------------------------------------------------------------------------------
diffeecult
Posts: 84
Joined: Feb 03, 2007 2:37

Postby diffeecult » Mar 31, 2010 23:01

It's been a while since I played around with Allegro so this may not be exactly right. But it seems like I remember that you declare it with your other functions.
cdecl() tells the compiler to handle parameters like a C function.

Code: Select all

Declare Sub increment_speed_counter cdecl()


Then your sub would look like this.

Code: Select all

Sub increment_speed_counter cdecl ()
    speed_counter+=1
End Sub
End_OF_FUNCTION(increment_speed_counter)


Then when you use it you use the address of operator.

Code: Select all

install_int_ex(@increment_speed_counter, BPS_TO_TIMER(60))


I hope that's right. If it's not maybe someone else can chime in with the solution.
Swos2008
Posts: 91
Joined: May 15, 2008 13:21

Postby Swos2008 » Apr 01, 2010 14:31

Hiya diffeecult,

thank you...the timer work great :)

when I run the code...it work great but when i press esc...the window is gone is good but it say FBIDETEMP.EXE has stop working...

What does it really mean!?

Is the code I am doing right way when user press the Escape when user had enough of the game.

Code: Select all

' main loop
while (not key(KEY_ESC))
    while(speed_counter > 0)
        if key(KEY_ESC) THEN
            Shut_down
        ENDIF
       
        Move_Player    ' Move the Daft Dog..heh heh
   
        Render_Sprites ' Draw everythings   
       
        speed_counter +=1
    wend
wend

function Shut_down
   
    ' delete the bitmap
    destroy_bitmap(buffer)
    destroy_bitmap(Background)
    destroy_bitmap(dog)
    destroy_bitmap(Stick)
    remove_keyboard
    allegro_exit
   
end function

[/code]
Ophelius
Posts: 428
Joined: Feb 26, 2006 1:57

Postby Ophelius » Apr 01, 2010 21:52

I'm not sure why, but you should change that to this:

Code: Select all


' main loop
DO

        If Key(KEY_ESC) Then SHUTDOWN
       
        Move_Player    ' Move the Daft Dog..heh heh
   
        Render_Sprites ' Draw everythings   
       
LOOP


Function Shut_down
   
    ' delete the bitmap
    destroy_bitmap(buffer)
    destroy_bitmap(Background)
    destroy_bitmap(dog)
    destroy_bitmap(Stick)
    Remove_keyboard
    allegro_exit
    END '<- added this

End Function

Last edited by Ophelius on Apr 01, 2010 22:11, edited 1 time in total.
Ophelius
Posts: 428
Joined: Feb 26, 2006 1:57

Postby Ophelius » Apr 01, 2010 22:04

I noticed you're trying to use the Allegro timer routines. I find them confusing, so I made my own. Save the following code into a new file and call it something like 'Timer.bi':

Code: Select all


TYPE Timer_Type
     S AS DOUBLE       'Start time
     Delay AS DOUBLE   'Pause length between ticks
END TYPE

DECLARE FUNCTION TimeDif (BYVAL ST AS DOUBLE, BYVAL FT AS DOUBLE) AS DOUBLE
DECLARE FUNCTION TIMER_LOAD (Delay AS DOUBLE) AS Timer_Type PTR
DECLARE SUB TIMER_UNLOAD(T AS Timer_Type PTR)
DECLARE FUNCTION TIMER_CHECK(T AS Timer_Type PTR) AS INTEGER

FUNCTION TimeDif (BYVAL ST AS DOUBLE, BYVAL FT AS DOUBLE) AS DOUBLE
   
    'This function returns the time length
    'between ST(start time) and FT(finish time)
    'while checking and adjusting if the clock
    'rollover occurs.
   
    IF ST < FT THEN
         TimeDif = FT - ST
    ELSE
         TimeDif = FT - ST + 86400
    END IF

END FUNCTION



FUNCTION TIMER_LOAD(Delay AS DOUBLE) AS Timer_Type PTR
   
    'Delay: Time increment in seconds
   
    DIM T as Timer_Type Ptr
   
    T = CALLOCATE(1, Sizeof(Timer_Type))
   
    T[0].Delay = Delay
   
    T[0].S = TIMER
   
    RETURN @T[0]

END FUNCTION



SUB TIMER_UNLOAD(T AS Timer_Type PTR)
   
    DEALLOCATE(T)
   
END SUB



FUNCTION TIMER_CHECK(T AS Timer_Type PTR) AS INTEGER
   
    IF TimeDif(T->S, TIMER) >= T->Delay THEN
        T->S = TIMER
        RETURN 1
    END IF
   
END FUNCTION



Now use it like this:

Code: Select all


#include "Timer.bi"

DIM T as Timer_Type PTR

T = Timer_Load(2.25) '<- amount of seconds you want for your timer

DO
       IF Timer_Check(T) THEN EXIT DO
LOOP

Print "2.25 seconds have passed"
sleep

Timer_Unload T

Swos2008
Posts: 91
Joined: May 15, 2008 13:21

Postby Swos2008 » Apr 02, 2010 22:05

Thanks Ophelius for help

Timer code look simple to understand :)
diffeecult
Posts: 84
Joined: Feb 03, 2007 2:37

Postby diffeecult » Apr 02, 2010 22:33

Try going back to your original main loop by removing the if statement, moving the call to Shutdown outside the while loop and decrementing speed_counter instead of incrementing. Then move Render_Sprites to the outer while loop. I'm trying to think through those loops but I may be getting confused. I hope it works.

Code: Select all

' main loop
While (Not Key(KEY_ESC))
    While(speed_counter > 0)

        Move_Player    ' Move the Daft Dog..heh heh
   
        speed_counter -=1
    Wend
    Render_Sprites ' Draw everythings
wend

Shut_down
Swos2008
Posts: 91
Joined: May 15, 2008 13:21

Postby Swos2008 » Apr 04, 2010 21:28

' main loop
Do

If Key(KEY_ESC) Then SHUTDOWN

Move_Player ' Move the Daft Dog..heh heh

Render_Sprites ' Draw everythings

Loop




' main loop
While (Not Key(KEY_ESC))
While(speed_counter > 0)

Move_Player ' Move the Daft Dog..heh heh

speed_counter -=1
Wend
Render_Sprites ' Draw everythings
Wend


both of them work fine....thank you both :)

Return to “Libraries”

Who is online

Users browsing this forum: No registered users and 4 guests