1
0
mirror of https://github.com/moparisthebest/Simba synced 2024-11-10 11:25:06 -05:00

libMML: More fixes.

This commit is contained in:
Merlijn Wajer 2010-12-01 22:12:45 +01:00
parent d7d7472386
commit dbf86cbddc

View File

@ -19,6 +19,8 @@ var
last_error: String;
debug: boolean;
{ This must be called on Library load }
function init: integer; cdecl;
begin
last_error := '';
@ -26,6 +28,14 @@ begin
result := RESULT_OK;
end;
procedure set_last_error(s: string);
begin
last_error := s;
if debug then
writeln('ERROR: ' + s);
end;
{ Validate the TClient. If it is NULL, set last error and return false }
function validate_client(C: TClient): boolean; inline;
begin
result := Assigned(C);
@ -37,6 +47,10 @@ begin
end;
end;
{
Create a TClient.
You can use multiple, but you'll have to manage them yourself.
}
function create_client: PtrUInt; cdecl;
var
C: TClient;
@ -46,29 +60,33 @@ begin
Result := PtrUInt(C);
except on e : Exception do
begin
writeln('ERROR');
// FIXME UINT negative
result := PtrUInt(RESULT_ERROR);
last_error := e.Message;
set_last_error(e.Message);
end;
end;
writeln(format('C: %d, IOManager: %d', [PtrUInt(C), PtrUInt(C.IOManager)]));
end;
{ Destroy a TClient }
function destroy_client(C: TClient): integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
C.Free;
end;
procedure set_debug(v: Boolean);
{ Set (verbose) debug on/off }
procedure set_debug(v: Boolean); cdecl;
begin
debug := v;
end;
function get_debug: boolean;
{ Get debug }
function get_debug: boolean; cdecl;
begin
exit(debug);
end;
@ -84,29 +102,34 @@ begin
exit(@last_error[1]);
end;
{ Turn an array into a pointer. The pointer memory is not managed by FPC, so we can pass
it along happily. It'll have to be freed by the external control though }
function array_to_ptr(ptr: Pointer; size: PtrUInt; objsize: PtrUInt): Pointer; cdecl;
begin
result := GetMem(objsize * size);
Move(ptr^, result^, objsize * size);
end;
{ Free memory previously allocated by libMML }
function free_ptr(ptr: pointer): boolean; cdecl;
begin
result := Assigned(ptr);
if not result then
begin
last_error := PChar('TClient is NULL');
set_last_error('TClient is NULL');
if debug then
writeln(last_error);
end else
FreeMem(ptr);
end;
{ Allocate memory with libMML }
function alloc_mem(size, objsize: PtrUInt): Pointer; cdecl;
begin
result := GetMem(size * objsize);
end;
{ Reallocate memory with libMML }
function realloc_mem(ptr: Pointer; size, objsize: PtrUInt): Pointer; cdecl;
begin
result := ReAllocMem(ptr, size*objsize);
@ -114,6 +137,7 @@ end;
{ Mouse }
{ Returns mouse position of client C to point t }
function get_mouse_pos(C: TClient; var t: tpoint): integer; cdecl;
begin
@ -128,11 +152,12 @@ begin
except on e : Exception do
begin
result := RESULT_ERROR;
last_error := e.Message;
set_last_error(e.Message);
end;
end;
end;
{ Set mouse position of client C to point t }
function set_mouse_pos(C: TClient; var t: tpoint): integer; cdecl;
begin
if not validate_client(C) then
@ -146,11 +171,13 @@ begin
except on e : Exception do
begin
result := RESULT_ERROR;
last_error := e.Message;
set_last_error(e.Message);
end;
end;
end;
{ Helper function }
function ConvIntClickType(Int : Integer) : TClickType; inline;
begin
case int of
@ -160,13 +187,13 @@ begin
end;
end;
{ Return the state of a mouse button given client C }
function get_mouse_button_state(C: TClient; But: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
writeln(but);
try
if C.IOManager.IsMouseButtonDown(ConvIntClickType(But)) then
@ -174,11 +201,12 @@ begin
except on e : Exception do
begin
result := RESULT_ERROR;
last_error := e.Message;
set_last_error(e.Message);
end;
end;
end;
{ Set the state of a mouse button given client C }
function set_mouse_button_state(C: TClient; But, State, X, Y: Integer): Integer; cdecl;
begin
if not validate_client(C) then
@ -199,11 +227,32 @@ begin
except on e : Exception do
begin
result := RESULT_ERROR;
last_error := e.Message;
set_last_error(e.Message);
end;
end;
end;
{ Colour }
function get_color(C: TClient; x, y: integer; var color: integer): integer;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
color := C.IOManager.GetColor(x, y);
except on e : exception do
begin
result := RESULT_ERROR;
set_last_error(e.Message);
end;
end;
end;
{ Find color on client C in area (x1,y1,x2,y2) and return coordinate (if any) in x, y }
function find_color(C: TClient; var x, y: integer; color, x1, y1, x2, y2: integer): integer; cdecl;
begin
if not validate_client(C) then
@ -219,13 +268,11 @@ begin
except on e : Exception do
begin
result := RESULT_ERROR;
last_error := e.Message;
writeln('last_error: ' + last_error);
set_last_error(e.Message);
end;
end;
end;
{ Colour }
function find_color_tolerance(C: TClient; var x, y: integer; color, tol, x1, y1, x2, y2: integer): integer; cdecl;
@ -243,7 +290,7 @@ begin
except on e : Exception do
begin
result := RESULT_ERROR;
last_error := e.Message;
set_last_error(e.Message);
end;
end;
end;
@ -263,7 +310,7 @@ begin
except on e : Exception do
begin
result := RESULT_ERROR;
last_error := e.Message;
set_last_error(e.Message);
end;
end;
@ -288,7 +335,7 @@ begin
except on e : Exception do
begin
result := RESULT_ERROR;
last_error := e.Message;
set_last_error(e.Message);
end;
end;
@ -302,7 +349,7 @@ end;
{ FIXME: DTM has not been tested yet! }
{ Create a MDTM}
function create_dtm(PointLen: integer; Points: PMDTMPoint; DTM: TMDTM): integer;
function create_dtm(PointLen: integer; Points: PMDTMPoint; DTM: TMDTM): integer; cdecl;
var
i: integer;
begin
@ -314,12 +361,12 @@ begin
exit(RESULT_OK);
DTM.Free;
last_error := 'Invalid DTM';
set_last_error('Invalid DTM');
result := RESULT_ERROR;
end;
{ Delete a MDTM. Don't delete it if it is managed! use remove_dtm instead }
function delete_dtm(C: TClient; DTM: TMDTM): integer;
function delete_dtm(C: TClient; DTM: TMDTM): integer; cdecl;
begin
if not validate_client(C) then
begin
@ -328,7 +375,7 @@ begin
if not assigned(DTM) then
begin
last_error := 'DTM is NULL';
set_last_error('DTM is NULL');
exit(RESULT_ERROR);
end;
@ -338,7 +385,7 @@ begin
end;
{ Add a previously created DTM to the DTM Manager }
function add_dtm(C: TClient; DTM: TMDTM; var index: integer): integer;
function add_dtm(C: TClient; DTM: TMDTM; var index: integer): integer; cdecl;
begin
if not validate_client(C) then
begin
@ -347,15 +394,15 @@ begin
if not assigned(DTM) then
begin
last_error := 'DTM is NULL';
set_last_error('DTM is NULL');
exit(RESULT_ERROR);
end;
C.MDTMs.AddDTM(DTM);
index := C.MDTMs.AddDTM(DTM);
end;
{ Remove a previously added DTM from the DTM manager. This also frees the DTM }
function remove_dtm(C: TClient; DTMi: integer): integer;
function remove_dtm(C: TClient; DTMi: integer): integer; cdecl;
begin
if not validate_client(C) then
begin
@ -365,7 +412,8 @@ begin
C.MDTMs.FreeDTM(DTMi);
end;
function find_dtm(C: TClient; DTMi: integer; var x, y: integer; x1, y1, x2, y2: integer): integer;
{ Find a DTM given DTM index i, client C in area x1,y1,x2,y2. Return coord at x, y. }
function find_dtm(C: TClient; DTMi: integer; var x, y: integer; x1, y1, x2, y2: integer): integer; cdecl;
var
res: boolean;
begin
@ -379,7 +427,7 @@ begin
except on e : Exception do
begin
result := RESULT_ERROR;
last_error := e.Message;
set_last_error(e.Message);
end;
end;
@ -389,11 +437,30 @@ begin
result := RESULT_FALSE;
end;
function set_array_target(C: TClient; Arr: PRGB32; Size: TPoint): integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
if not assigned(Arr) then
begin
set_last_error('Arr is not assigned');
exit(RESULT_FALSE);
end;
// FIXME: Catch exceptions.
C.IOManager.SetTarget(Arr, Size);
result := RESULT_OK;
end;
exports
init,
create_client,
destroy_client,
get_last_error,
get_debug,
set_debug,
@ -404,9 +471,17 @@ exports
get_mouse_pos, set_mouse_pos,
get_mouse_button_state, set_mouse_button_state,
get_color,
find_color, find_color_tolerance,
find_colors, find_colors_tolerance;
find_colors, find_colors_tolerance,
create_dtm, delete_dtm, add_dtm, remove_dtm,
find_dtm,
set_array_target;
begin