Re: Maze Solver - Updated Version 06 Oct 07
I'm not sure I understand what you are saying. If a API function requires a structure parameter, how can you get away with not passing that structure? I'm sorry I'm not understanding what you are saying, I've probably missed the point entirely.
However, thank you for subtly pointing out my inefficient coding! I coded the routine to get it working and never went back to optimize the code. My bad! You took out the "x2" and "y2" variables and substituted "gridRight" and "gridBottom", which I was assigning every iteration of the for/next loop. There are also other assignments that need to be refactored out.
Here is an updated version of the code:
Code:
function Draw_Grid() as long
dim col_number as dword
dim row_number as dword
dim x_pos,y_pos as dword
dim pt as POINT_TYPE
SelectObject(hdc,GetStockObject(%BLACK_PEN))
'draw horizontal lines
for row_number = 0 to astar_RowCount
y_pos = gridTop + (row_number * cellHeight)
MoveToEx(hdc,gridLeft,y_pos,pt)
LineTo(hdc,gridRight,y_pos)
next
'draw vertical lines
for col_number = 0 to astar_ColumnCount
x_pos = gridLeft + (col_number * cellWidth)
MoveToEx(hdc,x_pos,gridTop,pt)
LineTo(hdc,x_pos,gridBottom)
next
end function
I generally write my code to be easy to understand rather than optimized for speed (unless speed is required), and I think the code above is still clear, and more efficient.
Thanks Eros!
Re: Maze Solver - Updated Version 06 Oct 07
Quote:
Originally Posted by Randall
I'm not sure I understand what you are saying. If a API function requires a structure parameter, how can you get away with not passing that structure? I'm sorry I'm not understanding what you are saying, I've probably missed the point entirely.
Randall,
it depends on the API function. If you check Microsoft documentation about MoveToEx at http://msdn2.microsoft.com/en-us/library/ms534247.aspx you will see that lpPoint parameter is a pointer to a structure and below, in parameters description, documentation says "If this parameter is a NULL pointer, the previous position is not returned.".
What does it means?
You correctly defined MoveToEx in this way:
[code=thinbasic]
DECLARE FUNCTION MoveToEx LIB "gdi32.dll" ALIAS "MoveToEx" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As POINT_TYPE) As Long
[/code]
"lpPoint As POINT_TYPE" is a structure passed BYREF (because with no explicit indications about BYREF/BYVAL, thinbasic assume BYREF in exteternal fucntions). Every time a parameter is passed BYREF in reality a pointer to the parameter is passed, so it is like calling
[code=thinbasic]MoveToEx(hdc, x, y, VARPTR(lpPoint))[/code]
But this operation to pass a pointer to the structure is done automatically by thinBasic.
Now, a pointer to a strcuture is nothing different than a standard 32bit DWORD number so called function (MoveToEx) internaly can do something like the following (imagine code in C or ASM, I do not know):
[code=thinbasic]...
if lpPoint = 0 then
'---Do nothing
else
'---Assign to a POINT_TYPE structure pointed by lpPoint some values
end if
...[/code]
That's why you can pass now a %NULL or zero number. Mainly you are telling you do not need to have back the previous position pointer. In Microsoft API there are a lot of situations like that where a ZERO pointer means "I'm not passing any structure". Till now it was not possible in thinBasic because engine was expecting a variable and a pointer to that variable was passed automatically. Now you can pass an UDT (and thinbasic will pass a pointer to that UDT) or a numeric expression (and thinbasic will pass the result of the expression). Of course it will be programmer responsability to pass a number that will represent a memory location where the structure is present or enough memory is reserved to have back an UDT otherwise Windows will generate a GPF.
Hope to have given some more info about why i changed and better optimized external function calling.
One of the activities I often do is to look at the code people using thinBasic post in forum. What I search is ocde that is enough general to be converted into native compiled thinBasic function because this will improve script execution when used as native. Another thing I check are situations like the one I reported about the UDT passed BYREF and see if I can do something about it to improve the language. Many time people do not report difficulties in using a language but we all tend to try our own solutions. Well, I try to catch those clever solutions and transform them into thinBasic features :D
Sometimes I'm able, sometimes not but what Ive seen is that it is worth to try.
Ciao
Eros
Re: Maze Solver - Updated Version 06 Oct 07
I understand now, I didn't realize the parameter was optional. :-[
Thanks for the detailed explanation! I should investigate these API functions more before I use them.
And thanks for the improvement to thinBasic, you guys do an amazing job and it shows!
Randall
Re: Maze Solver - Updated Version 06 Oct 07
Well,
all together we do a good job I think.
Without your code, we can hardly think about possible implementations but thanks to the user code we can see how thinBasic is used by others. Even if it seems secondary, we can understand really a lot about our mistakes and how we can improve the parser. If we do not do this way, sooner or later, project will fall down.
Ciao
Eros