I started a layout app for my simple programs and games that I write using ptcgraph. I waste a lot of time guessing where I want a message box to pop up or an area to have info and/or graphics. So I had to bite the bullet and start to relearn how to use objects. One object by itself in this sort of app is useless and I could have used an array, but a linked list is better I think. Right now the layout app has one object, a rectangle, that I can move on the screen and resize using the mouse. It writes the code of the rectangle to a text file. The app will eventually allow multiple rectangles on the screen, each with a name for identification and for writing to the text file. The position of the rectangle is a record:
trect = record
x1,y1,
x2,y2 :integer;
end;
I have a simple procedure that sets the rectangle's area:
Procedure SetTrect(var rect:Trect;x,y,xx,yy:integer);
begin
with rect do
begin
x1 := x; y1 := y;
x2 := xx; y2 :=yy;
end;
end;
The code written by the app will be simply be a list of procedure calls with the trect names and positions.
A Trect is used to draw message boxes, push buttons, check boxes, layout screen graphics, capture mouse events, etc so it is the basis of much of what I'm doing right now.
Here's the code to my crude linked list, any pointers (no pun intended) or comments anyone can give would be appreciated. This code has been tested and doesn't crash, lol.
program listtest;
uses
ptccrt;
type
Pitem = ^Items;
Items = record
x, y: integer;
Next: PItem;
Name: string;
end;
var
First, Current, last: Pitem;
ItemNumber: integer; {for example output}
procedure additem(xx, yy: integer);
var
t: Pitem;
begin
T := New(Pitem);
T^.x := xx;
T^.Y := yy;
Inc(ItemNumber);
Str(ItemNumber, T^.Name);
if First = nil then
begin
First := T;
Current := T;
end
else
begin
Current^.Next := T;
Current := T;
end;
Current^.Next := nil;
Last := Current;
Last^.Next := nil;
end;
procedure displayItem(Pt: Pitem);
begin
with Pt^ do
begin
writeln('ItemNumber:', Name + ' has this data: x=', x, ' y = ', y);
end;
end;
begin
ItemNumber := 0;
First := nil;
Last := nil;
Current := nil;
AddItem(1, 1);
AddItem(5, 5);
additem(1, 8);
AddItem(4, 5);
AddItem(10, 10);
Current := First;
repeat
DisplayItem(current);
Current := Current^.Next;
until Current = nil;
repeat until keypressed;
end.