From a1036418976ccea25932ef6986e6f497843fa8c4 Mon Sep 17 00:00:00 2001 From: Merlijn Wajer Date: Tue, 1 Jun 2010 12:52:32 +0200 Subject: [PATCH] Array passing works, with copy overhead. --- Projects/MMLLib/pymml/mml.py | 4 +++- Projects/MMLLib/pymml/mmlcolor.py | 13 +++++++++++++ Projects/MMLLib/pymml/mmltypes.py | 27 +++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Projects/MMLLib/pymml/mml.py b/Projects/MMLLib/pymml/mml.py index 753c0f9..238f258 100755 --- a/Projects/MMLLib/pymml/mml.py +++ b/Projects/MMLLib/pymml/mml.py @@ -32,6 +32,9 @@ c = Color(DLL) ret = c.find((0, 0, 100, 100), 0) print ret +ret = c.findAll((0, 0, 100, 100), 0) +print ret + m = Mouse(DLL) @@ -53,5 +56,4 @@ print m.getPos() if hasattr(ret,'__iter__'): m.setPos(ret) - del DLL diff --git a/Projects/MMLLib/pymml/mmlcolor.py b/Projects/MMLLib/pymml/mmlcolor.py index a5e1abf..2c39c15 100644 --- a/Projects/MMLLib/pymml/mmlcolor.py +++ b/Projects/MMLLib/pymml/mmlcolor.py @@ -1,6 +1,7 @@ from ctypes import * from mmltypes import isiterable from mmltypes import POINT, PPOINT, PINTEGER +from mmltypes import PascalArray class ColorException(Exception): @@ -24,7 +25,19 @@ class Color(object): self._mc.dll.findColor(byref(x), byref(y), color, *box) return (x, y) + def findAll(self, box, color): + ptr = PPOINT() + self._mc.dll.findColors(byref(ptr), color, *box) + arr = PascalArray(POINT, ptr, self._mc) + print 'Length:', len(arr) +# for i in range(len(arr)): +# print i, arr[i].x, arr[i].y + return arr + def _initialiseDLLFuncs(self): self._mc.dll.findColor.restype = c_int self._mc.dll.findColor.argtypes = [PINTEGER, PINTEGER, c_int, c_int, c_int, c_int, c_int] + self._mc.dll.findColors.restype = c_int + self._mc.dll.findColors.argtypes = [POINTER(PPOINT), c_int, c_int, + c_int, c_int, c_int] diff --git a/Projects/MMLLib/pymml/mmltypes.py b/Projects/MMLLib/pymml/mmltypes.py index e1b8b60..a015416 100644 --- a/Projects/MMLLib/pymml/mmltypes.py +++ b/Projects/MMLLib/pymml/mmltypes.py @@ -8,6 +8,33 @@ class POINT(Structure): _fields_ = [('x', c_int), ('y', c_int)] +class PascalArray(object): + def __init__(self, pastype, ptr, MC): + self._type = pastype + self._p = ptr + self._mc = MC + + def __del__(self): + self._mc.dll.fpc_freemem_(self._p) + + def __len__(self): + return cast(self._p, POINTER(c_ulong))[0] + + def __getitem__(self, pos): + if pos > len(self): + print 'Out of range' + return None + return cast(self._p, POINTER(self._type))[pos+1] + + def __setitem__(self, pos, item): + if pos > len(self): + print 'Out of range' + return + if sizeof(item) != sizeof(self._type): + print 'Incorrect structure' + return + cast(self._p, POINTER(self._type))[pos] = item + PPOINT = POINTER(POINT) PINTEGER = POINTER(c_int)