Qbasicnews.com

Full Version: Redim Problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Sorry again for reporting these bugs but there's a problem with redim. The compiler says there is a duplicate definition. Here's the code

Code:
'$include: 'win\kernel32.bi'
Type SDL_Rect
    x as short
    y as short
    w as ushort
    h as ushort
End Type

type SDL_PixelFormat
    BitsPerPixel as ubyte
    BytesPerPixel as ubyte
    Rloss as ubyte
    Gloss as ubyte
    Bloss as ubyte
    Aloss as ubyte
    Rshift as ubyte
    Gshift as ubyte
    Bshift as ubyte
    Ashift as ubyte
    Rmask as uinteger
    Gmask as uinteger
    Bmask as uinteger
    Amask as uinteger
    colorkey as uinteger
    alpha as ubyte
end type

Dim SDL_Init As  Function CDECL (flags As UInteger) As Integer
Dim SDL_ListModes As Function CDECL (ByVal pixelfmt As Long, ByVal flags As UInteger) As Long
Dim CopyMemory As Sub(ByVal dst As Long ,ByVal src As Long ,ByVal size As Long)
Dim vidmodeptr As Long
Dim vidmodes() As SDL_Rect
Dim a As String, b As String, pixfmt As SDL_PixelFormat

Const SDL_INIT_VIDEO = &H20
Const SDL_FULLSCREEN = &H80000000

Function DLLFunc(dllname As String, dllproc As String) As Integer    
   Dim dllhand As Integer, tempfunc As Integer

   dllhand = LoadLibrary(dllname)

   If dllhand Then
         tempfunc = GetProcAddress(dllhand, dllproc)

         If tempfunc Then
            DLLFunc = tempfunc
         Else
             DLLFunc = 0
         End If
   Else
       DLLFunc = 0
   End If

End Function

SDL_Init = DLLFunc("sdl.dll","SDL_Init")
SDL_ListModes =  DLLFunc("sdl.dll", "SDL_ListModes")
CopyMemory = DLLFunc("kernel32.dll", "RtlMoveMemory")

Print "Init: ", SDL_Init(SDL_INIT_VIDEO)

ReDim vidmodes(7)

vidmodeptr = SDL_ListModes(Null,SDL_FULLSCREEN)

Print "Works: ", vidmodeptr

CopyMemory(VarPtr(vidmodes),vidmodeptr, Len(SDL_Rect)*7)
Print "Width: ", vidmodes(0)->w, "Height: ", vidmodes(0)->h

Do Until Inkey$ <> ""

Loop
put '$dynamic in front of the declaration of the array and make sure you redim into the same type: ReDim vidmodes(7) as sdl_Rect

but easier use sdl header: '$include: 'sdl\sdl.bi'
Yeah, if you dont use the fully qualified type the compiler will have to "guess" one, that will be the last DEF### used (INTEGER by default on FB).

I dunno why you are having all that trouble using GetProcAddress, it's not needed for SDL, GL and any Win API that has already the headers ported, see the gfx/sdl*.bas examples. Msg box could be done simply by: '$include: 'win/user32.bi' : messagebox ...
I was just using GetProcAddress for dynamic loading for dll's instead of making a import lib. It's just a test. GetProcAddress is working however.
Once again thanks for your help. Now it's off to make the super-duper 3d life sim game! Big Grin Naw, I'm just going to practice making games. I hope the Basic language stops being so underated in the game industry.
Cool! You allow arrays in UDT's to be dynamic too! Is that a main feature or something that would be removed later?

Code:
Type SDL_Rect
    x as short
    y as short
    w as ushort
    h as ushort
End Type

Type Test
    a As String
    b(0) As SDL_Rect
End Type

Dim c As Test

ReDim c.b(2) As SDL_Rect

c.b(0).w = 120
c.b(1).w = 320
c.b(2).w = 640
Print Len(c)
Print c.b(2).w

Do Until Inkey$ <> ""
Loop
No, all array fields are static, you shouldn't be allowed to reallocate them.. another check to add.

I wonder how that code didn't crash, er..
I guess that could cause problems in the long run. (memory leaks?) Do you think FB could support pointer arrays directly like C? It coincides with pointer arithmetic and is EXTREMELY useful. (It's one the reasons I use BCX Wink )

Ex.

Type Item
' stuff
End Type

Type Test
numof As Integer
items As Item Ptr
End Type

Dim a() As Test

Redim a(2) As Test

a(0).items = Allocate(Len(Test) * 3)

a(0).items(2)