PDA

View Full Version : (re)Defining library API



Ñuño Martínez
10-05-2012, 07:22 PM
You know, I'm working on the next high revision of Allegro.pas. In the FPC mailing list somebody suggested to use the "ctypes" unit to fix the problems with data types, specially because it ensures the correct data type despite the platform (i.e. 32bit -> 64bit and such).

I was wondering it would be better to define INLINE wrappers from standard Pascal data types to ctype data type. For example:



INTERFACE

FUNCTION al_get_allegro_version: LONGWORD; INLINE;
FUNCTION al_load_bitmap (CONST filename: STRING): ALLEGRO_BITMAPptr; INLINE;

IMPLEMENTATION

FUNCTION _al_get_allegro_version_: cuint32; CDECL;
EXTERNAL ALLEGRO_LIB_NAME 'al_get_allegro_version';

FUNCTION al_get_allegro_version: LONGWORD;
BEGIN
al_get_allegro_version := _al_get_allegro_version_;
END;

FUNCTION _al_load_bitmap_ (CONST filename: pcchar): ALLEGRO_BITMAPptr; CDECL;
EXTERNAL ALLEGRO_LIB_NAME 'al_load_bitmap';

FUNCTION al_load_bitmap (CONST filename: STRING): ALLEGRO_BITMAPptr;
VAR
TheString: pccar;
BEGIN
TheString := StrAlloc (Length (filename));
StrPCopy (TheString, filename);
TRY
al_load_bitmap := _al_load_bitmap_ (TheString);
FINALLY
StrDispose (TheString);
END;
END;

In some cases it's mandatory, as with STRING, but what about the other cases? It's a good idea?

And has Delphi the "ctypes" unit too?

Nattsurf
25-01-2017, 09:26 PM
yes i have also heard about ctypes and it can perfectly work for it.

Ñuño Martínez
27-01-2017, 11:21 AM
I was thinking about it (note this thread is from 2012!!!) and finally I defined my own types. As I said, Delphi don't had ctype (or don't have it in some of the most used versions) so that was the best solution.

You can see the final solution here (https://sourceforge.net/p/allegro-pas/code/HEAD/tree/TRUNK/lib/al5base.pas) (for Allegro 5, but for Allegro 4 is the same).