Advanced coding question

Miscellaneous Forums/General Discussion/Advanced coding question

Hi,

I want to use this function in sdl to copy a list of rectangle areas to the front buffer in a fast way. However I don't know how best to use the sdl function. I have access to the function.


However I am not sure if i should pass a bank of sequential x,y,w,h values, or instead somehow point to this structure:

    Type SDL_Rect
        Field x@@, y@@
        Field w@@, h@@
        
        Method Fill( from@ Ptr )
            MemCopy( GetPtr( ), from, SizeOf( Self ) )
        End Method
        
        Method GetPtr@ Ptr( )
            Return Varptr x
        End Method
        
        Method SetPosition( _x%, _y% )
            x = IntShort(_x)
            y = IntShort(_y)
        End Method
        
        Method GetPosition( _x% Var, _y% Var )
            _x = ShortInt(x)
            _y = ShortInt(y)
        End Method
    End Type
    
    Type SDL_Color
        Field r@
        Field g@
        Field b@
        Field unused@   ' O_o
        
        Method GetPtr@ Ptr( )
            Return Varptr r
        End Method
    End Type

    Type SDL_PixelFormat
        Field palette@ Ptr
        Field BitsPerPixel@
        Field BytesPerPixel@
        Field RLoss@
        Field GLoss@
        Field BLoss@
        Field ALoss@
        Field Rshift@
        Field Gshift@
        Field Bshift@
        Field Ashift@
        Field Rmask%
        Field Gmask%
        Field Bmask%
        Field Amask%
        Field colorkey%
        Field alpha@
        
        Method Fill( from@ Ptr )
            MemCopy( GetPtr( ), from, SizeOf( Self ) )
        End Method
        
        Method GetPtr@ Ptr( )
            Return Varptr palette
        End Method
    End Type


The docs for SDL_UpdateRects() says only this:


Name
SDL_UpdateRects -- Makes sure the given list of rectangles is updated on the given screen.

Synopsis
void SDL_UpdateRects(SDL_Surface *screen, int numrects, SDL_Rect *rects);


Description
Makes sure the given list of rectangles is updated on the given screen. The rectangles must all be confined within the screen boundaries (no clipping is done).

This function should not be called while screen is locked.

Note: It is adviced to call this function only once per frame, since each call has some processing overhead. This is no restriction since you can pass any number of rectangles each time.

The rectangles are not automatically merged or checked for overlap. In general, the programmer can use his knowledge about his particular rectangles to merge them in an efficient way, to avoid overdraw.



Banks are overkill for it I think.

Function RectBank:Byte[]( rects:IList )
    Local b@...( ) * 8]
    Local p@ Ptr = Varptr b[0]
    For Local i:SDL_Rect = EachIn rects
        MemCopy( p, i.GetPtr( ), 8 )
        p :+ 8
    Next
    Return b
End Function


You need to pass a pointer to a list of Rects.

You could create an array of Type SDL_Rect and pass the pointer to that array, I think that would be the simplest way of doing it.

Wouldn't work.

Why not?

Because it expects a contiguous list of SDL_Rects, not a contiguous list of pointers to SDL_Rects.

But surely the arrays are stored in memory in a sequencial manner? That would mean that the memory would contain the same information as your function creates.

The arrays are, but the arrays contain pointers to the objects, and the objects are, with very high probability, not located in a contiguous block of memory.

Odd, I use this exact method, as does BRL, for passing vertex data to DirectX.

The only thing I see BRL doing is modifying an array of floats, which are not objects.

yeah you are right and I'm being stupid.

You could pass arrays of data to the function but not arrays of types :)

Thanks noel, that worked a treat!