Is there any particular reason to use string[1] as a base type for the ContentType definition?
It will include an additional (unneeded) byte for the actual length of the string, which in your case will be always one.
No, not really. I'm pretty sure I can use
char in place of that. I think I started that way, and switched to
string[1] for some sort of debugging? I don't remember why I switched, to be honest...I don't see why I couldn't switch back.
I would use a one-dimensional array and do the coordinate calculation in place, this way it seems even much simpler to me:
Then you can allocate by: SetLength(Content, Size.X * Size.Y)
if you have (x,y) coordinate then the corresponding cell will be: Content[y * Size.X + x]
And the corresponding coordinates for the Content[ I ] will be ({x=}I mod Size.X, {y=}I div Size.X)
(assuming zero based x,y coordinates)
This will save all the trouble about shallowness and references in dynamic arrays. Also will save a small amount of memory used for keeping lengths and refcounts of the second dimension.
Of course, there is no need to use div/mod everywhere, eg. for drawing the contents you can just introduce an additional variable, z, and increment it into the inner cycle:
Interesting. I believe this is how the
video unit, and
VideoBuf works. The TextOut proc, which I sourced (and slightly modified) from the online documentation on the
video unit, had some maths in it that I didn't understand at all, but makes a bit more sense after reading your post:
procedure TextOut(x,y,fg,bg:Word;Const s:String);
Var
P,I,M : Word;
begin
P:=((X-1)+(Y-1)*ScreenWidth);
M:=Length(S);
If (P+M)>ScreenWidth*ScreenHeight then
M:=ScreenWidth*ScreenHeight-P;
For I:=1 to M do
VideoBuf^[P+I-1]:=Ord(S[i])+(fg shl 8)+(bg shl 12);
end;
I don't know how you could think this is 'simpler', though. I could see myself making a
lot of mistakes trying to implement this change. How much memory do you think this would save? That is, after all, one of the goals of this dynamic array endeavor.
In other news, I've updated my code to make WindowList[] a dynamic array, as well. I did away with the InUse variable at the same time, as it wouldn't be necessary. Therefore, WindowList is now just an array of WindowType whose length defines the number of active windows intrinsically. I had to make use of the
Delete() function in DestroyWindow (the entire proc also became a ton simpler), which wasn't too difficult, and had to make changes to any previous reference to WindowList[], which took quite a while. But it's bug-free!
Unfortunately, I didn't see any significant change in the memory footprint. I guess that array wasn't really using much to begin with. Today, I'll be doing some housekeeping on the code, cleaning up all the commented out code I'm no longer using, and making sure everything I
am using actually makes sense.
-McDoob
EDIT: I switched Content to
array of array of char with absolutely no problems. I really don't know why I was using
string[1]...
That's strange...my code's memory footprint has doubled? It's now using 262kb from 131...Things that make me go 'Hmm' #1234, I guess...