1. TileEngine Wrapper Issue

Hello all,

I was currently writing a wrapper of TileEngine for Euphoria. Everything was going smoothly until I noticed I'd need to make a shim for it due to the way its functions are handled. Some work without shims, but its the structs.

C Code 
struct Tilemap 
{ 
	DEFINE_OBJECT; 
	int		rows;		/* rows*/ 
	int		cols;		/* columns */ 
	int		maxindex;	/* highest tile index */ 
	int		bgcolor;	/* background color */ 
	int		id;			/* id property */ 
	bool	visible;	/* visible property */ 
	struct Tileset* tileset; /* attached tileset (if any) */ 
	Tile	tiles[]; 
}; 
From here https://github.com/megamarc/Tilengine/blob/master/src/Tilemap.h

Tileengine.h file 
TLNAPI TLN_Tilemap TLN_LoadTilemap (const char* filename, const char* layername); 

--What I have currently 
public constant xTLN_LoadTilemap = define_c_func(tile,"+TLN_LoadTilemap",{C_POINTER,C_POINTER},C_POINTER) 
 
 
public function TLN_LoadTilemap(sequence filename,sequence layername) 
 
 atom str = allocate_string(filename,1) 
 atom str2 = allocate_string(layername,1) 
  
 return c_func(xTLN_LoadTilemap,{str,str2}) 
	 
end function 
--shim idea 
enum ROWS, 
     COLS, 
     MAXINDEX, 
     BGCOLOR, 
     ID, 
     VISIBLE 

Maybe I should just switch to Phix since has simple struct support. -slight sarcarsm

new topic     » topic index » view message » categorize

2. Re: TileEngine Wrapper Issue

Icy_Viking said...

I was currently writing a wrapper of TileEngine for Euphoria. Everything was going smoothly until I noticed I'd need to make a shim for it due to the way its functions are handled. Some work without shims, but its the structs.

Mmm nope. Not in this case at least. According to the Tilengine.h include file, TLN_Tilemap is an opaque reference:

typedef struct Tilemap*		 TLN_Tilemap;			/*!< Opaque tilemap reference */ 

An "opaque" pointer or reference is just that: you can't see "through" it to the object on the other side (like an opaque wall versus a transparent window). The function expects to return a pointer to you and that's it. So in this case what you've already done is quite sufficient. No muss no fuss.

One tip I can offer when wrapping a libray is to start with the user-facing include files for a library (what you might call its "interface") before digging into back-end source code. If something isn't meant to be exposed to the programmer using the library then it's unlikely to be found in the include files and probably doesn't need to be wrapped.

Icy_Viking said...

Maybe I should just switch to Phix since has simple struct support. -slight sarcarsm

Har har. I've been working on the struct branch as best I can. I've had to re-familiarize myself with the Euphoria internals and then start work on finishing up the bits to make it complete. I'm making progress.

-Greg

new topic     » goto parent     » topic index » view message » categorize

3. Re: TileEngine Wrapper Issue

ghaberek said...
Icy_Viking said...

I was currently writing a wrapper of TileEngine for Euphoria. Everything was going smoothly until I noticed I'd need to make a shim for it due to the way its functions are handled. Some work without shims, but its the structs.

Mmm nope. Not in this case at least. According to the Tilengine.h include file, TLN_Tilemap is an opaque reference:

typedef struct Tilemap*		 TLN_Tilemap;			/*!< Opaque tilemap reference */ 

An "opaque" pointer or reference is just that: you can't see "through" it to the object on the other side (like an opaque wall versus a transparent window). The function expects to return a pointer to you and that's it. So in this case what you've already done is quite sufficient. No muss no fuss.

One tip I can offer when wrapping a libray is to start with the user-facing include files for a library (what you might call its "interface") before digging into back-end source code. If something isn't meant to be exposed to the programmer using the library then it's unlikely to be found in the include files and probably doesn't need to be wrapped.

Icy_Viking said...

Maybe I should just switch to Phix since has simple struct support. -slight sarcarsm

Har har. I've been working on the struct branch as best I can. I've had to re-familiarize myself with the Euphoria internals and then start work on finishing up the bits to make it complete. I'm making progress.

-Greg

Ah I see. Well that makes more sense. Also, I wasn't trying to jab at you, I was just saying. I'm surprised Robert Craig didn't think of adding in some sorta struct type when he adding the ability to wrap DLLs in Euphoria. I know there is a simple way to wrap structs or use them through peek and poke, etc, but its still pretty primitive compared to other programming languages. Anyways I've almost got an example going except nothing shows up on the window.

public constant xTLN_Init = define_c_func(tile,"+TLN_Init",{C_INT,C_INT,C_INT,C_INT,C_INT},C_POINTER) 
 
public function TLN_Init(integer hres,integer vres,integer numlayers,integer numsprites,integer numanimations) 
 
 return c_func(xTLN_Init,{hres,vres,numlayers,numsprites,numanimations}) 
	 
end function 
 
public constant xTLN_CreateWindow = define_c_func(tile,"+TLN_CreateWindow",{C_POINTER,C_INT},C_BOOL) 
 
 
public function TLN_CreateWindow(sequence overlay,integer flags) 
 
 atom str = allocate_string(overlay,1) 
  
 return c_func(xTLN_CreateWindow,{str,flags}) 
	 
end function 
 
public constant xTLN_DrawFrame = define_c_proc(tile,"+TLN_DrawFrame",{C_INT}) 
 
public procedure TLN_DrawFrame(integer frame) 
 
 c_proc(xTLN_DrawFrame,{frame}) 
	 
end procedure 
 
public constant xTLN_LoadWorld = define_c_func(tile,"+TLN_LoadWorld",{C_POINTER,C_INT},C_BOOL), 
xTLN_SetWorldPosition = define_c_proc(tile,"+TLN_SetWorldPosition",{C_INT,C_INT}) 
 
 
public function TLN_LoadWorld(sequence tmxfile,integer first_layer) 
 
 atom str = allocate_string(tmxfile,1) 
  
 return c_func(xTLN_LoadWorld,{str,first_layer}) 
	 
end function 
 
public procedure TLN_SetWorldPosition(integer x,integer y) 
 
 c_proc(xTLN_SetWorldPosition,{x,y}) 
	 
end procedure 
--EuTile Engine Basic Example 
 
without warning 
 
include std/dll.e 
include EuTileEngine.ew 
 
TLN_Init(400,240,8,80,0) --width,height,layers,sprites,animations 
TLN_CreateWindow("",0) --NOTE: Adding NULL caused it to crash 
TLN_SetWindowTitle("EuTile Engine Example") 
 
atom m 
 
m = TLN_LoadWorld("map.tmx",0) --this shows nothing 
 
if m = -1 then 
	puts(1,"Cannot load map!\n") 
end if 
 
atom x = 0 
 
while (TLN_ProcessWindow()) do 
 
	TLN_SetWorldPosition(x,0) 
	TLN_DrawFrame(0) 
	x += 2 
end while 
 
TLN_DeleteWindow() 
TLN_Deinit() 
TLN_ReleaseWorld() 

The above example almost works, except the map doesn't show, its just a blank window.

new topic     » goto parent     » topic index » view message » categorize

4. Re: TileEngine Wrapper Issue

Icy_Viking said...

Ah I see. Well that makes more sense. Also, I wasn't trying to jab at you, I was just saying.

Lol I know, it's all in good fun.

Icy_Viking said...

I'm surprised Robert Craig didn't think of adding in some sorta struct type when he adding the ability to wrap DLLs in Euphoria. I know there is a simple way to wrap structs or use them through peek and poke, etc, but its still pretty primitive compared to other programming languages.

I think that under Rob's direction there just wasn't as much incentive to add that kind of feature compared to today. Especially when everything was 32-bit it might have been not "much" work, but trying to support 32-bit and 64-bit requires using ifdef blocks and effectively wrapping most structures twice. Yikes!

Icy_Viking said...

Anyways I've almost got an example going except nothing shows up on the window.

One thing I can help you with is this:

TLN_CreateWindow("",0) --NOTE: Adding NULL caused it to crash 

Allow the overlay parameter to be an object and then check if it's a sequence and then allocate it into memory if necessary.

public constant xTLN_CreateWindow = define_c_func(tile,"+TLN_CreateWindow",{C_POINTER,C_INT},C_BOOL) 
 
 
public function TLN_CreateWindow(object overlay,integer flags) 
 
 if sequence(overlay) then 
  overlay = allocate_string(overlay,1) 
 end if 
  
 return c_func(xTLN_CreateWindow,{overlay,flags}) 
	 
end function 
Icy_Viking said...

The above example almost works, except the map doesn't show, its just a blank window.

I will test it out when I have some time and see if I can track down the issue.

-Greg

new topic     » goto parent     » topic index » view message » categorize

5. Re: TileEngine Wrapper Issue

ghaberek said...
Icy_Viking said...

Ah I see. Well that makes more sense. Also, I wasn't trying to jab at you, I was just saying.

Lol I know, it's all in good fun.

Icy_Viking said...

I'm surprised Robert Craig didn't think of adding in some sorta struct type when he adding the ability to wrap DLLs in Euphoria. I know there is a simple way to wrap structs or use them through peek and poke, etc, but its still pretty primitive compared to other programming languages.

I think that under Rob's direction there just wasn't as much incentive to add that kind of feature compared to today. Especially when everything was 32-bit it might have been not "much" work, but trying to support 32-bit and 64-bit requires using ifdef blocks and effectively wrapping most structures twice. Yikes!

Icy_Viking said...

Anyways I've almost got an example going except nothing shows up on the window.

One thing I can help you with is this:

TLN_CreateWindow("",0) --NOTE: Adding NULL caused it to crash 

Allow the overlay parameter to be an object and then check if it's a sequence and then allocate it into memory if necessary.

public constant xTLN_CreateWindow = define_c_func(tile,"+TLN_CreateWindow",{C_POINTER,C_INT},C_BOOL) 
 
 
public function TLN_CreateWindow(object overlay,integer flags) 
 
 if sequence(overlay) then 
  overlay = allocate_string(overlay,1) 
 end if 
  
 return c_func(xTLN_CreateWindow,{overlay,flags}) 
	 
end function 
Icy_Viking said...

The above example almost works, except the map doesn't show, its just a blank window.

I will test it out when I have some time and see if I can track down the issue.

-Greg

Thanks Greg. Your notes are always helpful.

new topic     » goto parent     » topic index » view message » categorize

6. Re: TileEngine Wrapper Issue

Icy_Viking said...
ghaberek said...
Icy_Viking said...

Maybe I should just switch to Phix since has simple struct support. -slight sarcarsm

Har har. I've been working on the struct branch as best I can. I'm making progress.

Ah I see. Well that makes more sense. Also, I wasn't trying to jab at you, I was just saying.

Was any of that sarcasm pointed at me?

new topic     » goto parent     » topic index » view message » categorize

7. Re: TileEngine Wrapper Issue

petelomax said...
Icy_Viking said...
ghaberek said...
Icy_Viking said...

Maybe I should just switch to Phix since has simple struct support. -slight sarcarsm

Har har. I've been working on the struct branch as best I can. I'm making progress.

Ah I see. Well that makes more sense. Also, I wasn't trying to jab at you, I was just saying.

Was any of that sarcasm pointed at me?

No, lol. If anything it was probably praise. I mean having a built-in struct function would come in handy on so many levels. Again though its all in good fun.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu