Re: [ooc-compiler] Handling C "in" parameters
Brought to you by:
mva
|
From: Stewart G. <sgr...@ii...> - 2005-02-18 02:09:50
|
Hi August,
> In my program I want to use some functionality from the SDL (Simple
> Directmedia Layer) C library. The API contains the function:
>
> int SDL_PollEvent(SDL_Event *event)
>
> where `SDL_Event' is a union type. In the interface file I have the
> definitions:
>
> EventPtr* = POINTER TO Event;
> Event* = RECORD [UNION] ... END;
>
> PROCEDURE ["SDL_PollEvent"] PollEvent*(event: EventPtr): C.int;
>
> If the client code contains:
>
> event: EventPtr
> ...
> NEW(event)
>
> I get an error about `_td_Sdl__EventPtr' being undeclared. How do I
> use/port this function?
You can't use NEW to allocate a non-Oberon record type, not foreign "C"
types. I think the compiler should have given you a more intelligent
error message (ie. at the NEW() statement), but the problem is
occurring later because the record has no type descriptor. If you
really need to allocate the record on the heap use RT0.NewBlock, but I
think it is OK to just allocate the event as a local variable.
I suggest that you do something like this:
PROCEDURE ["SDL_PollEvent"] PollEvent*(VAR event: Event): C.int;
Then you can use it like:
PROCEDURE HandleEvents;
VAR
e : SDL.Event;
BEGIN
...
WHILE SDL.PollEvent(e) # 0 DO
CASE e.type OF
SDL.KEYDOWN:
Another option is wrapping the event in an Oberon-2 RECORD. This might
be useful if (for example) you want to queue the events for later
processing.
TYPE
Event = RECORD
event : SDL.Event;
END:
PtrEvent = POINTER TO Event;
PROCEDURE HandleEvents;
VAR
se : SDL.Event;
e : PtrEvent;
BEGIN
...
WHILE SDL.PollEvent(se) # 0 DO
NEW(e);
e.event := se;
Enqueue(e);
END;
Hope this helps.
Cheers,
Stewart
|