Question about the use of data structures

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
p_m21987
Posts: 177
Joined: Mon 02 Apr 2018, 21:51

Question about the use of data structures

Post by p_m21987 »

Hello,

Is it possible to take an address in memory and treat it as a structure?

In my case, I'm calling the SDL function "SDL_LoadBMP_RW" which returns a pointer to an SDL_Surface structure. Would it be possible to define a data structure in BBC BASIC which matches the SDL_Surface data structure definition, and then take the memory address from "SDL_LoadBMP_RW" and treat it as that sort of data structure?

Right now I have a program that uses indirection operators to get the width and height integer values from the SDL_Surface structure, I was wondering if I could replace the indirection operators with a more simple "struct.width%" sort of construction.

PM

DBZ FOREVER!!! !!! !!! !!!
guest

Re: Question about the use of data structures

Post by guest »

Patrick M wrote: Tue 13 Nov 2018, 14:39Is it possible to take an address in memory and treat it as a structure?
Yes but you can only do it by poking the address into the structure descriptor:

Code: Select all

      DIM mystruct{mymember%}
      DIM memory%% DIM(mystruct{})

      IF @platform% AND &40 ](^mystruct{}+8) = memory%% ELSE !(^mystruct{}+4) = memory%%

      !memory%% = 12345678
      PRINT mystruct.mymember%
I'm calling the SDL function "SDL_LoadBMP_RW" which returns a pointer to an SDL_Surface structure. Would it be possible to define a data structure in BBC BASIC which matches the SDL_Surface data structure definition, and then take the memory address from "SDL_LoadBMP_RW" and treat it as that sort of data structure?

Code: Select all

      DIM rect{x%, y%, w%, h%}
      IF @platform% AND &40 THEN
        DIM surface{flags%, pad1%, format%%, width%, height%, pitch%, pad2%, pixels%%, userdata%%, \
        \           locked%, pad3%, lock_data%%, clip_rect{}=rect{}, map%%, refcount%, pad4%}
        ](^surface{} + 8) = memory%%
      ELSE
        DIM surface{flags%, format%, width%, height%, pitch%, pixels%, userdata%, \
        \           locked%, lock_data%, clip_rect{}=rect{}, map%, refcount%}
        !(^surface{} + 4) = memory%%
      ENDIF
Right now I have a program that uses indirection operators to get the width and height integer values from the SDL_Surface structure
TBH I think that's what I would do in this case! The width and height are at offsets 16 and 20 respectively in 64-bit BBCSDL and 8 and 12 respectively in 32-bit BBCSDL.
p_m21987
Posts: 177
Joined: Mon 02 Apr 2018, 21:51

Re: Question about the use of data structures

Post by p_m21987 »

Hello,

Thanks very much for the reply and for the information, I appreciate it.

PM