Dark Bit Factory & Gravity

PROGRAMMING => Freebasic => Topic started by: ninogenio on October 08, 2006

Title: messing around with types
Post by: ninogenio on October 08, 2006
i was messing around with types tonight and having some fun.

does this look ok to you guys and i was wondering which version looks better also why does ubound not work?

Code: [Select]
type my_type
     size1 as integer
     size2 as integer
     buffer as integer ptr
end type


declare function allocate_and_assign_type() as my_type
declare function free_type(tmp_type as my_type)

dim ninos_type as my_type

ninos_type = allocate_and_assign_type()
print ninos_type.size1
print ninos_type.size2
'print ubound(ninos_type.buffer)
while inkey$="":wend
   
free_type(ninos_type)


function allocate_and_assign_type() as my_type
         
         dim tmp_type as my_type
         
         tmp_type.size1 = 10
         tmp_type.size2 = 20
         tmp_type.buffer = allocate(100)

         return tmp_type

end function



function free_type(tmp_type as my_type)
         
         if tmp_type.buffer then
                deallocate(tmp_type.buffer)
         endif
         
         return 0
         
end function


Code: [Select]
type my_type
     size1 as integer
     size2 as integer
     buffer as integer ptr
end type


declare function allocate_and_assign_type() as my_type ptr
declare function free_type(tmp_type as my_type ptr)

dim ninos_type as my_type ptr

ninos_type = allocate_and_assign_type()
print ninos_type->size1
print ninos_type->size2
'print ubound(ninos_type->buffer)
while inkey$="":wend
   
free_type(ninos_type)


function allocate_and_assign_type() as my_type ptr
         
         dim tmp_type as my_type ptr
         
         tmp_type = allocate(len(my_type))
         
         tmp_type->size1 = 10
         tmp_type->size2 = 20
         tmp_type->buffer = allocate(100)

         return tmp_type

end function



function free_type(tmp_type as my_type ptr)
   
         if tmp_type then
                if tmp_type->buffer then
                       deallocate(tmp_type->buffer)
                endif
                deallocate(tmp_type)
         endif
               
         return 0
         
end function
Title: Re: messing around with types
Post by: Blitz Amateur on October 08, 2006
Personally, I would say to go for the pointers. Though, that's just because I like the added flexibility :)
Title: Re: messing around with types
Post by: ninogenio on October 09, 2006
i agree i think the pointer ver gives added flexibility i just wonder why ubound wont work?
Title: Re: messing around with types
Post by: ninogenio on October 09, 2006
ive been messing around again using fbmld to check for memory leaks the first test frees its memory fine but for some reason the second one (drews zoom example) appears to have a memory leak even though im deallocating the space its weird.i just wondered if one of you guys could have a look to see if you can find whats going on.
Title: Re: messing around with types
Post by: ninogenio on October 12, 2006
right this is a strange one but i found out that if you youse a while wend main loop after the wend the program was exiting before executing my free sub so this bit of code clears that matter up :)

this was driving me nuts.

Code: [Select]
sub clear_memory() destructor
          free_image( drew_graphic )
end sub
Title: Re: messing around with types
Post by: ninogenio on October 15, 2006
ive found out something intresting while i was messing about dont know if anyone else will mind but here goes

heres the first example
Code: [Select]
#include "tinyptc.bi"

ptc_open( "nino", 640 , 480 )

dim shared buffer( 0 to 640*480-1 ) as integer

print "before program destruction"
while inkey$=""
      ptc_update @buffer(0)
wend
ptc_close

print "after program detruction"
while inkey$="" :wend

and heres the second
Code: [Select]
'#include "tinyptc.bi"

'ptc_open( "nino", 640 , 480 )

'dim shared buffer( 0 to 640*480-1 ) as integer

print "before program destruction"
while inkey$=""
      'ptc_update @buffer(0)
wend
'ptc_close

print "after program detruction"
while inkey$="" :wend

now the two programs look fair enough but as you can see on the top peice of code everything after the initial wend doesnt get executed even the ptc_close so there must be a bug somewhere in the lib ive tried it with jim/rbraz`s lib too and the same thing happens its weird.
Title: Re: messing around with types
Post by: Rbz on October 15, 2006
In both of those libs (tinyptc and tinyogl) when you press escape your window will be destroyed.
So, this code below will not be executed, the framework just call ptc_close for you and exit your program
Code: [Select]
ptc_close

print "after program detruction"
while inkey$="" :wend


For original freebasic tinyptc you can use another key to exit while/wend loop using "inkey$" command.

With the new tinyptc that i have compiled and for Jim's tinyogl you need to use an windows API command called "GetAsyncKeyState()" to check for another key, there's some topic in the freebasic area for it.

 :cheers:
Title: Re: messing around with types
Post by: ninogenio on October 17, 2006
ahh  :cheers: rbraz

i now know what this was all about!