Ver código fonte

* Updated PTCPas to version 0.99.12

git-svn-id: trunk@19633 -
nickysn 13 anos atrás
pai
commit
6a0078e38a
100 arquivos alterados com 3048 adições e 3449 exclusões
  1. 2 0
      .gitattributes
  2. 40 45
      packages/graph/src/ptcgraph/ptccrt.pp
  3. 12 23
      packages/graph/src/ptcgraph/ptcgraph.pp
  4. 10 0
      packages/ptc/docs/CHANGES.txt
  5. 3 3
      packages/ptc/docs/INSTALL.txt
  6. 29 0
      packages/ptc/docs/INTF-CHANGES-0.99.12.txt
  7. 77 0
      packages/ptc/docs/INTF-CHANGES-FAQ-0.99.12.txt
  8. 41 25
      packages/ptc/docs/INTRO.txt
  9. 8 8
      packages/ptc/docs/README.txt
  10. 10 13
      packages/ptc/examples/area.pp
  11. 11 15
      packages/ptc/examples/buffer.pp
  12. 20 27
      packages/ptc/examples/clear.pp
  13. 10 19
      packages/ptc/examples/clip.pp
  14. 5 5
      packages/ptc/examples/con_info.pp
  15. 12 13
      packages/ptc/examples/console.pp
  16. 15 18
      packages/ptc/examples/fire.pp
  17. 16 22
      packages/ptc/examples/flower.pp
  18. 8 10
      packages/ptc/examples/hicolor.pp
  19. 13 21
      packages/ptc/examples/image.pp
  20. 13 27
      packages/ptc/examples/keyboard.pp
  21. 15 30
      packages/ptc/examples/keyboard2.pp
  22. 11 16
      packages/ptc/examples/land.pp
  23. 12 15
      packages/ptc/examples/lights.pp
  24. 21 31
      packages/ptc/examples/modes.pp
  25. 9 11
      packages/ptc/examples/mojo.pp
  26. 16 19
      packages/ptc/examples/mouse.pp
  27. 13 16
      packages/ptc/examples/palette.pp
  28. 9 11
      packages/ptc/examples/pixel.pp
  29. 8 10
      packages/ptc/examples/random.pp
  30. 19 29
      packages/ptc/examples/save.pp
  31. 29 49
      packages/ptc/examples/stretch.pp
  32. 12 15
      packages/ptc/examples/texwarp.pp
  33. 10 13
      packages/ptc/examples/timer.pp
  34. 8 10
      packages/ptc/examples/tunnel.pp
  35. 20 22
      packages/ptc/examples/tunnel3d.pp
  36. 19 13
      packages/ptc/src/core/aread.inc
  37. 59 15
      packages/ptc/src/core/areai.inc
  38. 27 27
      packages/ptc/src/core/baseconsoled.inc
  39. 120 32
      packages/ptc/src/core/baseconsolei.inc
  40. 33 34
      packages/ptc/src/core/basesurfaced.inc
  41. 3 3
      packages/ptc/src/core/cleard.inc
  42. 4 6
      packages/ptc/src/core/cleari.inc
  43. 5 4
      packages/ptc/src/core/clipperd.inc
  44. 110 121
      packages/ptc/src/core/clipperi.inc
  45. 25 19
      packages/ptc/src/core/colord.inc
  46. 88 27
      packages/ptc/src/core/colori.inc
  47. 2 74
      packages/ptc/src/core/consoled.inc
  48. 140 74
      packages/ptc/src/core/consolei.inc
  49. 2 2
      packages/ptc/src/core/copyd.inc
  50. 7 7
      packages/ptc/src/core/copyi.inc
  51. 4 11
      packages/ptc/src/core/eventd.inc
  52. 20 8
      packages/ptc/src/core/eventi.inc
  53. 28 19
      packages/ptc/src/core/formatd.inc
  54. 95 5
      packages/ptc/src/core/formati.inc
  55. 32 31
      packages/ptc/src/core/keyeventd.inc
  56. 121 3
      packages/ptc/src/core/keyeventi.inc
  57. 1 1
      packages/ptc/src/core/log.inc
  58. 21 17
      packages/ptc/src/core/moded.inc
  59. 67 18
      packages/ptc/src/core/modei.inc
  60. 30 23
      packages/ptc/src/core/mouseeventd.inc
  61. 81 1
      packages/ptc/src/core/mouseeventi.inc
  62. 9 11
      packages/ptc/src/core/paletted.inc
  63. 46 4
      packages/ptc/src/core/palettei.inc
  64. 2 53
      packages/ptc/src/core/surfaced.inc
  65. 129 111
      packages/ptc/src/core/surfacei.inc
  66. 7 19
      packages/ptc/src/core/timerd.inc
  67. 48 5
      packages/ptc/src/core/timeri.inc
  68. 36 36
      packages/ptc/src/dos/cga/cgaconsoled.inc
  69. 52 199
      packages/ptc/src/dos/cga/cgaconsolei.inc
  70. 2 2
      packages/ptc/src/dos/textfx2/textfx2.pp
  71. 46 46
      packages/ptc/src/dos/textfx2/textfx2consoled.inc
  72. 138 285
      packages/ptc/src/dos/textfx2/textfx2consolei.inc
  73. 38 38
      packages/ptc/src/dos/vesa/vesaconsoled.inc
  74. 95 201
      packages/ptc/src/dos/vesa/vesaconsolei.inc
  75. 38 38
      packages/ptc/src/dos/vga/vgaconsoled.inc
  76. 60 234
      packages/ptc/src/dos/vga/vgaconsolei.inc
  77. 1 1
      packages/ptc/src/ptc.pp
  78. 9 8
      packages/ptc/src/ptcwrapper/ptceventqueue.pp
  79. 28 35
      packages/ptc/src/ptcwrapper/ptcwrapper.pp
  80. 2 5
      packages/ptc/src/win32/base/kbd.inc
  81. 16 0
      packages/ptc/src/win32/base/window.inc
  82. 40 40
      packages/ptc/src/win32/directx/directxconsoled.inc
  83. 81 151
      packages/ptc/src/win32/directx/directxconsolei.inc
  84. 89 143
      packages/ptc/src/win32/directx/display.inc
  85. 10 10
      packages/ptc/src/win32/directx/displayd.inc
  86. 1 1
      packages/ptc/src/win32/directx/hook.inc
  87. 30 83
      packages/ptc/src/win32/directx/primary.inc
  88. 12 12
      packages/ptc/src/win32/directx/primaryd.inc
  89. 1 1
      packages/ptc/src/win32/directx/translate.inc
  90. 34 34
      packages/ptc/src/win32/gdi/gdiconsoled.inc
  91. 39 76
      packages/ptc/src/win32/gdi/gdiconsolei.inc
  92. 2 2
      packages/ptc/src/win32/gdi/win32dibd.inc
  93. 0 1
      packages/ptc/src/win32/gdi/win32dibi.inc
  94. 38 38
      packages/ptc/src/wince/gapi/wincegapiconsoled.inc
  95. 41 130
      packages/ptc/src/wince/gapi/wincegapiconsolei.inc
  96. 3 3
      packages/ptc/src/wince/gdi/wincebitmapinfod.inc
  97. 0 3
      packages/ptc/src/wince/gdi/wincebitmapinfoi.inc
  98. 38 38
      packages/ptc/src/wince/gdi/wincegdiconsoled.inc
  99. 46 136
      packages/ptc/src/wince/gdi/wincegdiconsolei.inc
  100. 30 30
      packages/ptc/src/x11/x11consoled.inc

+ 2 - 0
.gitattributes

@@ -5589,6 +5589,8 @@ packages/ptc/Makefile.fpc svneol=native#text/plain
 packages/ptc/docs/AUTHORS.txt svneol=native#text/plain
 packages/ptc/docs/CHANGES.txt svneol=native#text/plain
 packages/ptc/docs/INSTALL.txt svneol=native#text/plain
+packages/ptc/docs/INTF-CHANGES-0.99.12.txt svneol=native#text/plain
+packages/ptc/docs/INTF-CHANGES-FAQ-0.99.12.txt svneol=native#text/plain
 packages/ptc/docs/INTRO.txt svneol=native#text/plain
 packages/ptc/docs/README.txt svneol=native#text/plain
 packages/ptc/docs/TODO.txt svneol=native#text/plain

+ 40 - 45
packages/graph/src/ptcgraph/ptccrt.pp

@@ -110,56 +110,51 @@ end;
 
 procedure GetKeyEvents;
 var
-  ev: TPTCEvent;
-  KeyEv: TPTCKeyEvent;
+  ev: IPTCEvent;
+  KeyEv: IPTCKeyEvent;
 begin
-  ev := nil;
-  try
-    repeat
-      PTCWrapperObject.NextEvent(ev, False, [PTCKeyEvent]);
-      if ev <> nil then
+  repeat
+    PTCWrapperObject.NextEvent(ev, False, [PTCKeyEvent]);
+    if ev <> nil then
+    begin
+      KeyEv := ev as IPTCKeyEvent;
+      if KeyEv.Press then
       begin
-        KeyEv := TPTCKeyEvent(ev);
-        if KeyEv.Press then
-        begin
-          case KeyEv.Code of
-            PTCKEY_BACKSPACE:
-              if KeyEv.Control then
-                KeyBufAdd(#127)
-              else
-                KeyBufAdd(#8);
-            PTCKEY_ENTER:  KeyBufAdd(#13);
-            PTCKEY_ESCAPE: KeyBufAdd(#27);
-            PTCKEY_INSERT: KeyBufAdd(#0#82);
-            PTCKEY_DELETE: KeyBufAdd(#0#83);
-            PTCKEY_LEFT:   KeyBufAdd(#0#75);
-            PTCKEY_UP:     KeyBufAdd(#0#72);
-            PTCKEY_RIGHT:  KeyBufAdd(#0#77);
-            PTCKEY_DOWN:   KeyBufAdd(#0#80);
-            PTCKEY_HOME:     KeyBufAdd(#0#71);
-            PTCKEY_END:      KeyBufAdd(#0#79);
-            PTCKEY_PAGEUP:   KeyBufAdd(#0#73);
-            PTCKEY_PAGEDOWN: KeyBufAdd(#0#81);
-            PTCKEY_F1:     KeyBufAdd(#0#59);
-            PTCKEY_F2:     KeyBufAdd(#0#60);
-            PTCKEY_F3:     KeyBufAdd(#0#61);
-            PTCKEY_F4:     KeyBufAdd(#0#62);
-            PTCKEY_F5:     KeyBufAdd(#0#63);
-            PTCKEY_F6:     KeyBufAdd(#0#64);
-            PTCKEY_F7:     KeyBufAdd(#0#65);
-            PTCKEY_F8:     KeyBufAdd(#0#66);
-            PTCKEY_F9:     KeyBufAdd(#0#67);
-            PTCKEY_F10:    KeyBufAdd(#0#68);
+        case KeyEv.Code of
+          PTCKEY_BACKSPACE:
+            if KeyEv.Control then
+              KeyBufAdd(#127)
             else
-              if (KeyEv.Unicode >= 32) and (KeyEv.Unicode <= 127) then
-                KeyBufAdd(Chr(KeyEv.Unicode));
-          end;
+              KeyBufAdd(#8);
+          PTCKEY_ENTER:  KeyBufAdd(#13);
+          PTCKEY_ESCAPE: KeyBufAdd(#27);
+          PTCKEY_INSERT: KeyBufAdd(#0#82);
+          PTCKEY_DELETE: KeyBufAdd(#0#83);
+          PTCKEY_LEFT:   KeyBufAdd(#0#75);
+          PTCKEY_UP:     KeyBufAdd(#0#72);
+          PTCKEY_RIGHT:  KeyBufAdd(#0#77);
+          PTCKEY_DOWN:   KeyBufAdd(#0#80);
+          PTCKEY_HOME:     KeyBufAdd(#0#71);
+          PTCKEY_END:      KeyBufAdd(#0#79);
+          PTCKEY_PAGEUP:   KeyBufAdd(#0#73);
+          PTCKEY_PAGEDOWN: KeyBufAdd(#0#81);
+          PTCKEY_F1:     KeyBufAdd(#0#59);
+          PTCKEY_F2:     KeyBufAdd(#0#60);
+          PTCKEY_F3:     KeyBufAdd(#0#61);
+          PTCKEY_F4:     KeyBufAdd(#0#62);
+          PTCKEY_F5:     KeyBufAdd(#0#63);
+          PTCKEY_F6:     KeyBufAdd(#0#64);
+          PTCKEY_F7:     KeyBufAdd(#0#65);
+          PTCKEY_F8:     KeyBufAdd(#0#66);
+          PTCKEY_F9:     KeyBufAdd(#0#67);
+          PTCKEY_F10:    KeyBufAdd(#0#68);
+          else
+            if (KeyEv.Unicode >= 32) and (KeyEv.Unicode <= 127) then
+              KeyBufAdd(Chr(KeyEv.Unicode));
         end;
       end;
-    until ev = nil;
-  finally
-    ev.Free;
-  end;
+    end;
+  until ev = nil;
 end;
 
 function KeyPressed: Boolean;

+ 12 - 23
packages/graph/src/ptcgraph/ptcgraph.pp

@@ -293,9 +293,9 @@ var
   ptcformat: TPTCFormat = nil;}
   PTCWidth: Integer;
   PTCHeight: Integer;
-  PTCFormat8: TPTCFormat;
-  PTCFormat15: TPTCFormat;
-  PTCFormat16: TPTCFormat;
+  PTCFormat8: IPTCFormat;
+  PTCFormat15: IPTCFormat;
+  PTCFormat16: IPTCFormat;
 
   EGAPaletteEnabled: Boolean;
   EGAPalette: TEGAPalette;
@@ -416,7 +416,7 @@ begin
 //  writeln('Initializing mode');
   { create format }
 {  FreeAndNil(PTCFormat);
-  PTCFormat:=TPTCFormat.Create(16,$f800,$07e0,$001f);}
+  PTCFormat:=TPTCFormatFactory.CreateNew(16,$f800,$07e0,$001f);}
   { open the console }
 {  ptcconsole.open(paramstr(0),ptcformat);}
   { create surface matching console dimensions }
@@ -640,7 +640,7 @@ begin
   CurrentCGABkColor := 0;
 end;
 
-procedure ptc_InternalOpen(const ATitle: string; AWidth, AHeight: Integer; AFormat: TPTCFormat; AVirtualPages: Integer);
+procedure ptc_InternalOpen(const ATitle: string; AWidth, AHeight: Integer; AFormat: IPTCFormat; AVirtualPages: Integer);
 var
   ConsoleWidth, ConsoleHeight: Integer;
 begin
@@ -1503,11 +1503,11 @@ end;
   { Returns nil if no graphics mode supported.        }
   { This list is READ ONLY!                           }
   var
-    PTCModeList: PPTCMode;
+    PTCModeList: TPTCModeList;
 
     function ModeListEmpty: Boolean;
     begin
-      ModeListEmpty := (PTCModeList = nil) or (not PTCModeList[0].Valid);
+      ModeListEmpty := Length(PTCModeList) = 0;
     end;
 
     function ContainsExactResolution(AWidth, AHeight: Integer): Boolean;
@@ -1520,9 +1520,7 @@ end;
         exit;
       end;
 
-      I := 0;
-      while (PTCModeList[I].Valid) do
-      begin
+      for I := Low(PTCModeList) to High(PTCModeList) do
         with PTCModeList[I] do
           if (Width = AWidth) and
              (Height = AHeight) then
@@ -1530,8 +1528,6 @@ end;
             ContainsExactResolution := True;
             exit;
           end;
-        Inc(I);
-      end;
       ContainsExactResolution := False;
     end;
 
@@ -1545,9 +1541,7 @@ end;
         exit;
       end;
 
-      I := 0;
-      while (PTCModeList[I].Valid) do
-      begin
+      for I := Low(PTCModeList) to High(PTCModeList) do
         with PTCModeList[I] do
           if (Width >= AWidth) and
              (Height >= AHeight) then
@@ -1555,8 +1549,6 @@ end;
             ContainsAtLeast := True;
             exit;
           end;
-        Inc(I);
-      end;
       ContainsAtLeast := False;
     end;
 
@@ -2828,16 +2820,13 @@ end;
   end;
 
 initialization
-  PTCFormat8 := TPTCFormat.Create(8);
-  PTCFormat15 := TPTCFormat.Create(16, $7C00, $03E0, $001F);
-  PTCFormat16 := TPTCFormat.Create(16, $F800, $07E0, $001F);
+  PTCFormat8 := TPTCFormatFactory.CreateNew(8);
+  PTCFormat15 := TPTCFormatFactory.CreateNew(16, $7C00, $03E0, $001F);
+  PTCFormat16 := TPTCFormatFactory.CreateNew(16, $F800, $07E0, $001F);
   PTCWrapperObject := TPTCWrapperThread.Create;
   InitializeGraph;
 finalization
   PTCWrapperObject.Terminate;
   PTCWrapperObject.WaitFor;
   PTCWrapperObject.Free;
-  PTCFormat16.Free;
-  PTCFormat15.Free;
-  PTCFormat8.Free;
 end.

+ 10 - 0
packages/ptc/docs/CHANGES.txt

@@ -1,3 +1,13 @@
+0.99.12
+ - pressing Alt or F10 under Windows no longer pauses the application.
+ - API changes:
+    - PTC classes have been made descendants of TInterfacedObject and are now
+      accessed via interfaces. This simplifies memory management, because
+      objects are now reference counted and automatically freed when they are
+      no longer used. Unfortunately, this breaks existing code. However it's
+      relatively easy to fix. See the files INTF-CHANGES-0.99.12.txt and
+      INTF-CHANGES-FAQ-0.99.12.txt for details.
+
 0.99.11
  - added ptcgraph - an implementation of FPC's BGI-compatible graph unit on top
    of PTCPas. It should work on all platforms, supported by PTCPas,  except for

+ 3 - 3
packages/ptc/docs/INSTALL.txt

@@ -1,13 +1,13 @@
 The supported platforms are Linux, FreeBSD, Windows, Windows Mobile and DOS.
 
 Generally you need the latest stable version of the Free Pascal Compiler, which
-currently means version 2.4.0. Generally FPC 2.2.4 should also work fine.
+currently means version 2.4.2. Generally FPC 2.2.4 should also work fine.
 
  - Compiling the library:
 Before starting make sure the FPCDIR environment variable is set correctly.
-For example: (windows, fpc version 2.4.0, default install dir)
+For example: (windows, fpc version 2.4.2, default install dir)
 
-  set FPCDIR=c:\fpc\2.4.0
+  set FPCDIR=c:\fpc\2.4.2
 
 To compile the library type:
 

+ 29 - 0
packages/ptc/docs/INTF-CHANGES-0.99.12.txt

@@ -0,0 +1,29 @@
+This document describes the API changes introduced in version 0.99.12 of PTCPas
+
+Since version 0.99.12, most PTCPas classes have been made descendants of
+TInterfacedObject and are now only accessible via interfaces. The reason for
+this change is to provide automatic memory management via reference counting for
+the PTC core objects, so that they are freed automatically, once they're no
+longer in use, without causing any memory leaks.
+
+Unfortunately, this breaks existing code. However, it's relatively easy to fix
+it and the purpose of this document is to explain how. Here's a basic summary
+of the changes that need to be made:
+
+1) in your source code, replace "TPTCSomething.Create" with
+"TPTCSomethingFactory.CreateNew" (where 'Something' will correspond to one of
+those, depending on which objects are used in your program: Area, Color,
+Console, Event, Format, KeyEvent, Mode, MouseButtonEvent, MouseEvent, Palette,
+Surface and Timer)
+2) replace "TPTCSomething" with "IPTCSomething" (where 'Something' = an object
+from point #1)
+3) try compiling your code and you'll probably get some errors. Remove every
+call to .Free that fails with a compilation error, since it's no longer needed -
+PTC core objects are freed automatically when their reference count reaches
+zero.
+4) if you are using FreeAndNil, be extra careful! Since it accepts an untyped
+var parameter (bad design coming from Delphi), it will not cause a compilation
+error, if you are using it on an interface, but you will instead get a crash at
+runtime. Sadly, there's not much you can do, besides checking every call to
+FreeAndNil and replacing it with ":= nil" when you're using it on a PTC core
+object.

+ 77 - 0
packages/ptc/docs/INTF-CHANGES-FAQ-0.99.12.txt

@@ -0,0 +1,77 @@
+Frequently Asked Questions about the changes, introduced in version 0.99.12 of
+PTCPas
+
+Q: My code uses only the ptcgraph and ptccrt (TP7 compatibility) units. Is it
+affected by the changes?
+A: No. Only code that uses the ptc unit is affected.
+
+Q: Were the changes necessary?
+A: Yes, I believe so. Using certain objects like events, areas, and formats was
+tedious because of the need to manually manage and free them. You had to be
+aware of their lifecycle, e.g. when getting an event from the console you had
+to free it once you were done with it, but, when getting the format of the
+console, you had to know that you must not free it and that you must not change
+it with .Assign (otherwise things would break in the console, etc.) With the new
+interfaces, suddenly you don't have to worry about all that. The original C++
+library OpenPTC was much easier to work with, thanks to C++'s automatic
+destructors, copy constructors and operator overloading. However, when PTC was
+ported to Pascal for the first time, FPC was still at version 1.0.x and didn't
+support interfaces, so it wasn't possible to do at the time. I was planning this
+change for a long time but I kept postponing it until now :)
+
+Q: Are you planning any more changes that break backward compatibility?
+A: No. I promise :)
+
+Q: Was it possible to introduce the changes, without breaking backward
+compatibility with existing code?
+A: Basically, it was hard to do without having to maintain a full set of wrapper
+classes with the old interface for compatibility and that is a lot of extra work
+I wouldn't like to do. I'd rather help people migrate their code to the new
+version. :)
+I documented the necessary changes, most of them can be done with just a series
+of search & replace and you can also email me your code if you have any
+trouble - I'll do my best to help.
+
+Q: How about keeping the old methods around and adding new overloaded ones?
+A: I considered doing that for a while, but I figured it wasn't an option,
+because it would make a huge mess out of PTCPas' interface. Also mixing
+reference counted interfaces and ordinary classes is dangerous and prone to
+errors (see next question for details). If you really cannot justify the effort
+needed to upgrade your code, old PTCPas versions are available on sourceforge
+forever. :)
+
+Q: Why were constructors replaced with factory methods?
+A: I wanted to hide the old classes completely and force the use of interfaces
+everywhere. One of the reason for this was that if I kept the old classes, in
+certain cases, that would make old code compile without errors, but crash at
+runtime. Here's an example:
+
+var
+  format: TPTCFormat;  // bug: this should be changed to IPTCFormat
+  ...
+begin
+  format := TPTCFormat.Create(8);  // object created, ref count = 0
+
+  surface := TPTCSurface.Create(320, 200, format);  // the reference count of
+  // 'format' is increased (i.e. becomes 1) before the constructor call and
+  // decreased afterwards back to 0 and then the object is freed.
+  // If format was declared of type IPTCFormat, it would keep the reference
+  // count one higher until the variable goes out of scope.
+
+  console := TPTCConsole.Create;
+  console.Open(320, 200, format);  // here we try to use format for the second
+  // time, but it has been freed and we get a crash at runtime :(
+  ...
+end.
+
+The problem occurs when mixing direct references to the object with access to it
+via interfaces. To prevent this type of error, I made the TPTCFormat type
+private (i.e. hidden in the implementation part of the ptc unit) and only
+IPTCFormat public. This way, when trying to compile this code, you'll get a
+compile error in the declaration of 'format: TPTCFormat;' and you'll know that
+you need to fix it (by changing the type to IPTCFormat). However, this has the
+side effect of making the call to TPTCFormat.Create impossible (because
+TPTCFormat isn't public). IPTCFormat.Create sounds like the logical alternative,
+but it's also not possible, as IPTCFormat is an interface and interfaces cannot
+have constructors. That's why I introduced factory class methods (in this case:
+TPTCFormatFactory.CreateNew)

+ 41 - 25
packages/ptc/docs/INTRO.txt

@@ -1,40 +1,56 @@
-For more complete documentation please see the API reference found in the
+For more detailed documentation please see the API reference found in the
 'api-reference' directory (as well as online on the official PTCPas website).
 
 This will explain the basics of creating a simple graphics application using
 PTC for FPC. :)
 
-(If you aren't familiar with Delphi classes, please refer to the Free Pascal
-Reference guide, Chapter 5 - Classes.)
+(If you aren't familiar with Delphi classes and interfaces, please refer to the
+Free Pascal Reference guide, Chapter 6 - Classes and Chapter 7 - Interfaces.)
 
-There are 3 classes you should get familiar with: TPTCFormat, TPTCSurface and
-TPTCConsole.
+There are 3 interfaces you should get familiar with: IPTCFormat, IPTCSurface
+and IPTCConsole.
 
-Ok, what is TPTCFormat? It basically describes a pixel format. To create a
+Ok, what is IPTCFormat? It basically describes a pixel format. To create a
 pixel format for 32 bpp use:
-  Format := TPTCFormat.Create(32, $FF0000, $FF00, $FF);
+
+var
+  Format: IPTCFormat;
+begin
+  Format := TPTCFormatFactory.CreateNew(32, $FF0000, $FF00, $FF);
 
 32 is the number of bits per pixel. (Only formats with 8, 16, 24 and 32 bits
 per pixel are supported). $FF0000, $FF00 and $FF are the red, green and blue
 masks.
 
+Note that you no longer need to call Format.Free. The object is reference
+counted and will be freed automatically when it's no longer needed. This also
+applies to all the other PTC objects created by their TPTC*Factory.CreateNew
+methods.
+
 
 Now, when you have created your favourite pixel format, you should create a
 surface:
-  Surface := TPTCSurface.Create(320, 200, Format);
+var
+  Surface: IPTCSurface;
+begin
+  Surface := TPTCSurfaceFactory.CreateNew(320, 200, Format);
 
 This will create a buffer in RAM to hold a single 320x200 frame in the given
-format. Note that TPTCSurface is always created in normal RAM, not video RAM,
-so it's not a problem if your video card doesn't have enough memory for it,
-or doesn't support e.g. 320x200x32bpp. You just create a TPTCConsole and open
-it in whatever mode is supported by the hardware and then PTC will blit the
-image stored in TPTCSurface to the console, doing any conversions that are
-necessary (i.e. converting to another pixel format, stretching the image to
-another resolution, etc...).
-
-
-How to use this TPTCConsole? Easy! First create it:
-  Console := TPTCConsole.Create;
+format. Note that surfaces, created by TPTCSurfaceFactory.CreateNew are always
+created in normal RAM, not video RAM, so it's not a problem if your video card
+doesn't have enough memory for it, or doesn't support the exact resolution that
+you requested. You just create a IPTCConsole and open it in whatever mode is
+supported by the hardware and then PTC will blit the image stored in
+IPTCSurface to the console, doing any conversions that are necessary (i.e.
+converting to another pixel format, stretch the image to another resolution,
+etc...).
+
+
+How to use this IPTCConsole? Easy! First create it:
+var
+  Console: IPTCConsole;
+begin
+  Console := TPTCConsoleFactory.CreateNew;
 
 This still doesn't do anything, just allocates memory and initializes stuff.
 Then you switch to the desired mode:
@@ -46,9 +62,9 @@ to switch to the best mode. If (for example) your card doesn't support
 To see the actual mode that PTC has set use these properties:
   Console.Width Console.Height and Console.Format
 
-Ok, now that you have created a TPTCSurface and opened a TPTCConsole, what to
-do next? Draw stuff... The lock function of TPTCSurface will give you a pointer
-to the internal buffer.
+Ok, now that you have created an IPTCSurface and opened an IPTCConsole, what to
+do next? Draw stuff... The lock function of IPTCSurface will give you a pointer
+to its internal buffer.
   ptr := Surface.Lock;
 
 Now you can draw your frame in the buffer, pointed by ptr. Note that this buffer
@@ -61,10 +77,10 @@ When you're done you have to unlock the surface and copy it to the console:
 
 The Surface.Copy(Console) will do the conversion (if necessary) to the actual
 mode. Console.Update will actually show the new frame (if the console driver
-supports multiple pages and you have enough video RAM for that, etc... :) ).
+supports multiple video pages and you have enough video RAM for that, etc... :) ).
 
-See the example programs for additional details. (keyboard input, high
-resolution timers - they're pretty much straightforward)
+See the example programs for additional details. (keyboard and mouse input,
+high resolution timers - they're pretty much straightforward)
 
 Enjoy!
 

+ 8 - 8
packages/ptc/docs/README.txt

@@ -1,4 +1,4 @@
-PTCPas 0.99.11
+PTCPas 0.99.12
 Nikolay Nikolov ([email protected])
 
 PTCPas is a free, portable framebuffer library, written in Free Pascal. It is
@@ -18,13 +18,13 @@ OpenPTC C++ library. Since then, OpenPTC development has stalled and PTCPas
 lives on as a fully independent Object Pascal project.
 
 Supported consoles:
-  DirectX 3+ (should work on all x86 Windows versions since Windows 95, except
-              Windows CE. This currently means 95/98/ME/NT4/2000/XP/2003/Vista.
-              It is compatible with the x64 editions of XP and 2003 (although
-              only as a 32-bit application, no native win64 yet). On NT4 you
-              need SP3 or later. Also some very ancient versions of Windows 95
-              do not have any DirectX preinstalled, so it has to be installed
-              separately.)
+  DirectX 3+ (should work on all x86 and x64 Windows versions since Windows 95,
+              except Windows CE. This currently means 95/98/ME/NT4/2000/XP/2003/
+              Vista/2008/7. It is compatible with the x64 editions of XP, 2003,
+              Vista, 2008 and 7 (both as a 32-bit application and native win64).
+              On NT4 you need SP3 or later. Ancient versions of Windows 95 come
+              without any DirectX version by default, so you may have to install
+              it.)
   Win32 GDI (no fullscreen support. Slower than DirectX, but maybe more
              compatible.)
   X11 (on linux and other unix-like OSes, supports XRandR, XF86VidMode, XShm

+ 10 - 13
packages/ptc/examples/area.pp

@@ -16,33 +16,33 @@ uses
   ptc;
 
 var
-  console: TPTCConsole = nil;
-  format: TPTCFormat = nil;
-  surface: TPTCSurface = nil;
+  console: IPTCConsole;
+  format: IPTCFormat;
+  surface: IPTCSurface;
   pixels: PDWord;
   width, height: Integer;
   i: Integer;
   x, y, r, g, b: Integer;
-  area: TPTCArea = nil;
+  area: IPTCArea;
 begin
   try
     try
       { create console }
-      console := TPTCConsole.Create;
+      console := TPTCConsoleFactory.CreateNew;
 
       { create format }
-      format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
+      format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
 
       { create console }
       console.open('Area example', format);
 
       { create surface half the size of the console }
-      surface := TPTCSurface.Create(console.width div 2, console.height div 2, format);
+      surface := TPTCSurfaceFactory.CreateNew(console.width div 2, console.height div 2, format);
 
       { setup destination area }
       x := console.width div 4;
       y := console.height div 4;
-      area := TPTCArea.Create(x, y, x + surface.width, y + surface.height);
+      area := TPTCAreaFactory.CreateNew(x, y, x + surface.width, y + surface.height);
 
       { loop until a key is pressed }
       while not console.KeyPressed do
@@ -81,11 +81,8 @@ begin
         console.update;
       end;
     finally
-      console.close;
-      console.Free;
-      surface.Free;
-      format.Free;
-      area.Free;
+      if Assigned(console) then
+        console.close;
     end;
   except
     on error: TPTCError do

+ 11 - 15
packages/ptc/examples/buffer.pp

@@ -16,9 +16,8 @@ uses
   ptc;
 
 var
-  console: TPTCConsole = nil;
-  format: TPTCFormat = nil;
-  palette: TPTCPalette = nil;
+  console: IPTCConsole;
+  format: IPTCFormat;
   width, height: Integer;
   pixels: PUint32 = nil;
   x, y, r, g, b: Integer;
@@ -27,22 +26,21 @@ begin
   try
     try
       { create console }
-      console := TPTCConsole.Create;
+      console := TPTCConsoleFactory.CreateNew;
 
       { create format }
-      format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
+      format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
 
       { open the console }
-      console.open('Buffer example', format);
+      console.Open('Buffer example', format);
 
       { get console dimensions }
-      width := console.width;
-      height := console.height;
+      width := console.Width;
+      height := console.Height;
 
       { allocate a buffer of pixels }
       pixels := GetMem(width * height * SizeOf(Uint32));
       FillChar(pixels^, width * height * SizeOf(Uint32), 0);
-      palette := TPTCPalette.Create;
 
       { loop until a key is pressed }
       while not console.KeyPressed do
@@ -64,18 +62,16 @@ begin
         end;
 
         { load pixels to console }
-        console.load(pixels, width, height, width * 4, format, palette);
+        console.Load(pixels, width, height, width * 4, format, TPTCPaletteFactory.CreateNew);
 
         { update console }
-        console.update;
+        console.Update;
       end;
     finally
       { free pixels buffer }
       FreeMem(pixels);
-      console.close;
-      palette.Free;
-      format.Free;
-      console.Free;
+      if Assigned(console) then
+        console.close;
     end;
   except
     on error: TPTCError do

+ 20 - 27
packages/ptc/examples/clear.pp

@@ -16,27 +16,27 @@ uses
   SysUtils, ptc;
 
 var
-  console: TPTCConsole = nil;
-  format: TPTCFormat = nil;
-  surface: TPTCSurface = nil;
+  console: IPTCConsole;
+  format: IPTCFormat;
+  surface: IPTCSurface;
   width, height: Integer;
   x, y: Integer;
   size: Integer;
-  area: TPTCArea = nil;
-  color: TPTCColor = nil;
+  area: IPTCArea;
+  color: IPTCColor;
 begin
   try
     { create console }
-    console := TPTCConsole.Create;
+    console := TPTCConsoleFactory.CreateNew;
 
     { create format }
-    format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
+    format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
 
     { open the console }
     console.open('Clear example', format);
 
     { create surface matching console dimensions }
-    surface := TPTCSurface.Create(console.width, console.height, format);
+    surface := TPTCSurfaceFactory.CreateNew(console.width, console.height, format);
 
     { loop until a key is pressed }
     while not console.KeyPressed do
@@ -52,30 +52,23 @@ begin
       { get random area size }
       size := Random(width div 8);
 
-      try
-        { setup clear area }
-        area := TPTCArea.Create(x-size, y-size, x+size, y+size);
+      { setup clear area }
+      area := TPTCAreaFactory.CreateNew(x-size, y-size, x+size, y+size);
 
-        { create random color }
-        color := TPTCColor.Create(Random, Random, Random);
+      { create random color }
+      color := TPTCColorFactory.CreateNew(Random, Random, Random);
 
-        { clear surface area with color }
-        surface.clear(color, area);
+      { clear surface area with color }
+      surface.clear(color, area);
 
-        { copy to console }
-        surface.copy(console);
+      { copy to console }
+      surface.copy(console);
 
-        { update console }
-        console.update;
-      finally
-        FreeAndNil(area);
-        FreeAndNil(color);
-      end;
+      { update console }
+      console.update;
     end;
-    console.close;
-    console.Free;
-    surface.Free;
-    format.Free;
+    if Assigned(console) then
+      console.close;
   except
     on error: TPTCError do
       { report error }

+ 10 - 19
packages/ptc/examples/clip.pp

@@ -16,10 +16,9 @@ uses
   ptc;
 
 var
-  console: TPTCConsole = nil;
-  surface: TPTCSurface = nil;
-  format: TPTCFormat = nil;
-  area: TPTCArea;
+  console: IPTCConsole;
+  surface: IPTCSurface;
+  format: IPTCFormat;
   x1, y1, x2, y2: Integer;
   pixels: PUint32;
   width, height: Integer;
@@ -29,16 +28,16 @@ begin
   try
     try
       { create console }
-      console := TPTCConsole.Create;
+      console := TPTCConsoleFactory.CreateNew;
 
       { create format }
-      format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
+      format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
 
       { open the console }
       console.open('Clip example', format);
 
       { create surface matching console dimensions }
-      surface := TPTCSurface.Create(console.width, console.height, format);
+      surface := TPTCSurfaceFactory.CreateNew(console.width, console.height, format);
 
       { calculate clip coordinates }
       x1 := console.width div 4;
@@ -46,14 +45,8 @@ begin
       x2 := console.width - x1;
       y2 := console.height - y1;
 
-      { setup clip area }
-      area := TPTCArea.Create(x1, y1, x2, y2);
-      try
-        { set clip area }
-        console.clip(area);
-      finally
-        area.Free;
-      end;
+      { set clip area }
+      console.clip(TPTCAreaFactory.CreateNew(x1, y1, x2, y2));
 
       { loop until a key is pressed }
       while not console.KeyPressed do
@@ -92,10 +85,8 @@ begin
         console.update;
       end;
     finally
-      console.close;
-      console.Free;
-      surface.Free;
-      format.Free;
+      if Assigned(console) then
+        console.close;
     end;
   except
     on error: TPTCError do

+ 5 - 5
packages/ptc/examples/con_info.pp

@@ -15,7 +15,7 @@ program InfoExample;
 uses
   ptc;
 
-procedure print(const format: TPTCFormat);
+procedure print(format: IPTCFormat);
 begin
   { check format type }
   if format.direct then
@@ -32,7 +32,7 @@ begin
 end;
 
 var
-  console: TPTCConsole = nil;
+  console: IPTCConsole;
 begin
   try
     try
@@ -42,7 +42,7 @@ begin
       Writeln;
 
       { create console }
-      console := TPTCConsole.Create;
+      console := TPTCConsoleFactory.CreateNew;
 
       { open the console }
       console.open('Info example');
@@ -64,8 +64,8 @@ begin
       Writeln('[console information]');
       Writeln(console.information);
     finally
-      console.close;
-      console.Free;
+      if Assigned(console) then
+        console.close;
     end;
   except
     on error: TPTCError do

+ 12 - 13
packages/ptc/examples/console.pp

@@ -16,13 +16,13 @@ uses
   ptc;
 
 var
-  console: TPTCConsole = nil;
-  palette: TPTCPalette = nil;
+  console: IPTCConsole;
+  palette: IPTCPalette;
   data: array [0..255] of DWord;
   i: Integer;
   pixels: PByte;
   width, height, pitch: Integer;
-  format: TPTCFormat;
+  format: IPTCFormat;
   bits, bytes: Integer;
   x, y: Integer;
   color: DWord;
@@ -32,29 +32,29 @@ begin
   try
     try
       { create console }
-      console := TPTCConsole.Create;
+      console := TPTCConsoleFactory.CreateNew;
 
       { open the console with one page }
       console.open('Console example', 1);
 
       { create palette }
-      palette := TPTCPalette.Create;
+      palette := TPTCPaletteFactory.CreateNew;
 
       { generate palette }
       for i := 0 to 255 do
         data[i] := i;
 
       { load palette data }
-      palette.load(data);
+      palette.Load(data);
 
       { set console palette }
-      console.palette(palette);
+      console.Palette(palette);
 
       { loop until a key is pressed }
       while not console.KeyPressed do
       begin
         { lock console }
-        pixels := console.lock;
+        pixels := console.Lock;
 
         try
           { get console dimensions }
@@ -104,16 +104,15 @@ begin
           end;
         finally
           { unlock console }
-          console.unlock;
+          console.Unlock;
         end;
 
         { update console }
-        console.update;
+        console.Update;
       end;
     finally
-      palette.Free;
-      console.close;
-      console.Free;
+      if Assigned(console) then
+        console.Close;
     end;
   except
     on error: TPTCError do

+ 15 - 18
packages/ptc/examples/fire.pp

@@ -21,13 +21,13 @@ begin
   pack := (r shl 16) or (g shl 8) or b;
 end;
 
-procedure generate(palette: TPTCPalette);
+procedure generate(palette: IPTCPalette);
 var
   data: PUint32;
   i, c: Integer;
 begin
   { lock palette data }
-  data := palette.lock;
+  data := palette.Lock;
 
   try
     { black to red }
@@ -67,15 +67,15 @@ begin
 
   finally
     { unlock palette }
-    palette.unlock;
+    palette.Unlock;
   end;
 end;
 
 var
-  format: TPTCFormat = nil;
-  console: TPTCConsole = nil;
-  surface: TPTCSurface = nil;
-  palette: TPTCPalette = nil;
+  format: IPTCFormat;
+  console: IPTCConsole;
+  surface: IPTCSurface;
+  palette: IPTCPalette;
   state: Integer;
   intensity: Single;
   pixels, pixel, p: PUint8;
@@ -84,24 +84,24 @@ var
   top, bottom, c1, c2: Uint32;
   generator: PUint8;
   color: Integer;
-  area: TPTCArea = nil;
+  area: IPTCArea;
 begin
   try
     try
       { create format }
-      format := TPTCFormat.Create(8);
+      format := TPTCFormatFactory.CreateNew(8);
 
       { create console }
-      console := TPTCConsole.Create;
+      console := TPTCConsoleFactory.CreateNew;
 
       { open console }
       console.open('Fire demo', 320, 200, format);
 
       { create surface }
-      surface := TPTCSurface.Create(320, 208, format);
+      surface := TPTCSurfaceFactory.CreateNew(320, 208, format);
 
       { create palette }
-      palette := TPTCPalette.Create;
+      palette := TPTCPaletteFactory.CreateNew;
 
       { generate palette }
       generate(palette);
@@ -117,7 +117,7 @@ begin
       intensity := 0;
 
       { setup copy area }
-      area := TPTCArea.Create(0, 0, 320, 200);
+      area := TPTCAreaFactory.CreateNew(0, 0, 320, 200);
 
       { main loop }
       repeat
@@ -242,11 +242,8 @@ begin
       until False;
 
     finally
-      console.Free;
-      surface.Free;
-      format.Free;
-      palette.Free;
-      area.Free;
+      if Assigned(console) then
+        console.Close;
     end;
   except
     on error: TPTCError do

+ 16 - 22
packages/ptc/examples/flower.pp

@@ -21,7 +21,7 @@ begin
   pack := (r shl 16) or (g shl 8) or b;
 end;
 
-procedure generate_flower(flower: TPTCSurface);
+procedure generate_flower(flower: IPTCSurface);
 var
   data: PUint8;
   x, y, fx, fy, fx2, fy2: Integer;
@@ -56,13 +56,13 @@ begin
   end;
 end;
 
-procedure generate(palette: TPTCPalette);
+procedure generate(palette: IPTCPalette);
 var
   data: PUint32;
   i, c: Integer;
 begin
   { lock palette data }
-  data := palette.lock;
+  data := palette.Lock;
 
   try
     { black to yellow }
@@ -103,17 +103,17 @@ begin
     end;
   finally
     { unlock palette }
-    palette.unlock;
+    palette.Unlock;
   end;
 end;
 
 var
-  console: TPTCConsole = nil;
-  format: TPTCFormat = nil;
-  flower_surface: TPTCSurface = nil;
-  surface: TPTCSurface = nil;
-  palette: TPTCPalette = nil;
-  area: TPTCArea = nil;
+  console: IPTCConsole;
+  format: IPTCFormat;
+  flower_surface: IPTCSurface;
+  surface: IPTCSurface;
+  palette: IPTCPalette;
+  area: IPTCArea;
   time, delta: Single;
   scr, map: PUint8;
   width, height, mapWidth: Integer;
@@ -124,13 +124,13 @@ begin
   try
     try
       { create format }
-      format := TPTCFormat.Create(8);
+      format := TPTCFormatFactory.CreateNew(8);
 
       { create console }
-      console := TPTCConsole.Create;
+      console := TPTCConsoleFactory.CreateNew;
 
       { create flower surface }
-      flower_surface := TPTCSurface.Create(640, 400, format);
+      flower_surface := TPTCSurfaceFactory.CreateNew(640, 400, format);
 
       { generate flower }
       generate_flower(flower_surface);
@@ -139,10 +139,10 @@ begin
       console.open('Flower demo', 320, 200, format);
 
       { create surface }
-      surface := TPTCSurface.Create(320, 200, format);
+      surface := TPTCSurfaceFactory.CreateNew(320, 200, format);
 
       { create palette }
-      palette := TPTCPalette.Create;
+      palette := TPTCPaletteFactory.CreateNew;
 
       { generate palette }
       generate(palette);
@@ -154,7 +154,7 @@ begin
       surface.palette(palette);
 
       { setup copy area }
-      area := TPTCArea.Create(0, 0, 320, 200);
+      area := TPTCAreaFactory.CreateNew(0, 0, 320, 200);
 
       { time data }
       time := 0;
@@ -213,12 +213,6 @@ begin
     finally
       if Assigned(console) then
         console.close;
-      area.Free;
-      format.Free;
-      palette.Free;
-      surface.Free;
-      flower_surface.Free;
-      console.Free;
     end;
   except
     on error: TPTCError do

+ 8 - 10
packages/ptc/examples/hicolor.pp

@@ -16,9 +16,9 @@ uses
   ptc;
 
 var
-  console: TPTCConsole = nil;
-  surface: TPTCSurface = nil;
-  format: TPTCFormat = nil;
+  console: IPTCConsole;
+  surface: IPTCSurface;
+  format: IPTCFormat;
   pixels: PUint16;
   width, height: Integer;
   i: Integer;
@@ -27,16 +27,16 @@ begin
   try
     try
       { create console }
-      console := TPTCConsole.Create;
+      console := TPTCConsoleFactory.CreateNew;
 
       { create format }
-      format := TPTCFormat.Create(16, $F800, $07E0, $001F);
+      format := TPTCFormatFactory.CreateNew(16, $F800, $07E0, $001F);
 
       { open the console }
       console.open('HiColor example', format);
 
       { create surface matching console dimensions }
-      surface := TPTCSurface.Create(console.width, console.height, format);
+      surface := TPTCSurfaceFactory.CreateNew(console.width, console.height, format);
 
       { loop until a key is pressed }
       while not console.KeyPressed do
@@ -77,10 +77,8 @@ begin
         console.update;
       end;
     finally
-      console.close;
-      console.Free;
-      surface.Free;
-      format.Free;
+      if Assigned(console) then
+        console.close;
     end;
   except
     on error: TPTCError do

+ 13 - 21
packages/ptc/examples/image.pp

@@ -15,14 +15,13 @@ program ImageExample;
 uses
   SysUtils, ptc;
 
-procedure load(surface: TPTCSurface; filename: String);
+procedure load(surface: IPTCSurface; filename: String);
 var
   F: File;
   width, height: Integer;
   pixels: PByte = nil;
   y: Integer;
-  img_format: TPTCFormat = nil;
-  img_palette: TPTCPalette = nil;
+  img_format: IPTCFormat;
 begin
   { open image file }
   AssignFile(F, filename);
@@ -45,36 +44,32 @@ begin
 
     { load pixels to surface }
     {$IFDEF FPC_LITTLE_ENDIAN}
-    img_format := TPTCFormat.Create(24, $00FF0000, $0000FF00, $000000FF);
+    img_format := TPTCFormatFactory.CreateNew(24, $00FF0000, $0000FF00, $000000FF);
     {$ELSE FPC_LITTLE_ENDIAN}
-    img_format := TPTCFormat.Create(24, $000000FF, $0000FF00, $00FF0000);
+    img_format := TPTCFormatFactory.CreateNew(24, $000000FF, $0000FF00, $00FF0000);
     {$ENDIF FPC_LITTLE_ENDIAN}
-    img_palette := TPTCPalette.Create;
-    surface.load(pixels, width, height, width * 3, img_format, img_palette);
+    surface.Load(pixels, width, height, width * 3, img_format, TPTCPaletteFactory.CreateNew);
 
   finally
     CloseFile(F);
 
     { free image pixels }
     FreeMem(pixels);
-
-    img_palette.Free;
-    img_format.Free;
   end;
 end;
 
 var
-  console: TPTCConsole = nil;
-  format: TPTCFormat = nil;
-  surface: TPTCSurface = nil;
+  console: IPTCConsole;
+  format: IPTCFormat;
+  surface: IPTCSurface;
 begin
   try
     try
       { create console }
-      console := TPTCConsole.Create;
+      console := TPTCConsoleFactory.CreateNew;
 
       { create format }
-      format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
+      format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
 
       try
         { try to open the console matching the image resolution }
@@ -86,7 +81,7 @@ begin
       end;
 
       { create surface }
-      surface := TPTCSurface.Create(320, 200, format);
+      surface := TPTCSurfaceFactory.CreateNew(320, 200, format);
 
       { load image to surface }
       load(surface, 'image.tga');
@@ -102,11 +97,8 @@ begin
 
     finally
       { close console }
-      console.close;
-
-      console.Free;
-      surface.Free;
-      format.Free;
+      if Assigned(console) then
+        console.close;
     end;
   except
     on error: TPTCError do

+ 13 - 27
packages/ptc/examples/keyboard.pp

@@ -16,38 +16,34 @@ uses
   ptc;
 
 var
-  console: TPTCConsole = nil;
-  surface: TPTCSurface = nil;
-  format: TPTCFormat = nil;
-  color: TPTCColor = nil;
-  key: TPTCKeyEvent = nil;
-  area: TPTCArea;
+  console: IPTCConsole;
+  surface: IPTCSurface;
+  format: IPTCFormat;
+  color: IPTCColor;
+  key: IPTCKeyEvent;
   x, y: Integer;
   size: Integer;
   delta: Integer;
 begin
   try
     try
-      { create key }
-      key := TPTCKeyEvent.Create;
-
       { create console }
-      console := TPTCConsole.Create;
+      console := TPTCConsoleFactory.CreateNew;
 
       { create format }
-      format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
+      format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
 
       { open the console }
       console.open('Keyboard example', format);
 
       { create surface matching console dimensions }
-      surface := TPTCSurface.Create(console.width, console.height, format);
+      surface := TPTCSurfaceFactory.CreateNew(console.width, console.height, format);
 
       { setup cursor data }
       x := surface.width div 2;
       y := surface.height div 2;
       size := surface.width div 10;
-      color := TPTCColor.Create(1, 1, 1);
+      color := TPTCColorFactory.CreateNew(1, 1, 1);
 
       { main loop }
       repeat
@@ -79,14 +75,8 @@ begin
         { clear surface }
         surface.clear;
 
-        { setup cursor area }
-        area := TPTCArea.Create(x - size, y - size, x + size, y + size);
-        try
-          { draw cursor as a quad }
-          surface.clear(color, area);
-        finally
-          area.Free;
-        end;
+        { draw cursor as a quad }
+        surface.clear(color, TPTCAreaFactory.CreateNew(x - size, y - size, x + size, y + size));
 
         { copy to console }
         surface.copy(console);
@@ -95,12 +85,8 @@ begin
         console.update;
       until False;
     finally
-      color.Free;
-      console.close;
-      console.Free;
-      surface.Free;
-      key.Free;
-      format.Free;
+      if Assigned(console) then
+        console.close;
     end;
   except
     on error: TPTCError do

+ 15 - 30
packages/ptc/examples/keyboard2.pp

@@ -11,13 +11,12 @@ uses
   ptc;
 
 var
-  console: TPTCConsole = nil;
-  surface: TPTCSurface = nil;
-  format: TPTCFormat = nil;
-  color: TPTCColor = nil;
-  timer: TPTCTimer = nil;
-  key: TPTCKeyEvent = nil;
-  area: TPTCArea;
+  console: IPTCConsole;
+  surface: IPTCSurface;
+  format: IPTCFormat;
+  color: IPTCColor;
+  timer: IPTCTimer;
+  key: IPTCKeyEvent;
   x, y, delta: Real;
   left, right, up, down: Boolean;
   size: Integer;
@@ -29,32 +28,29 @@ begin
   down := False;
   try
     try
-      { create key }
-      key := TPTCKeyEvent.Create;
-
       { create console }
-      console := TPTCConsole.Create;
+      console := TPTCConsoleFactory.CreateNew;
 
       { enable key release events }
       console.KeyReleaseEnabled := True;
 
       { create format }
-      format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
+      format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
 
       { open the console }
       console.open('Keyboard example 2', format);
 
       { create timer }
-      timer := TPTCTimer.Create;
+      timer := TPTCTimerFactory.CreateNew;
 
       { create surface matching console dimensions }
-      surface := TPTCSurface.Create(console.width, console.height, format);
+      surface := TPTCSurfaceFactory.CreateNew(console.width, console.height, format);
 
       { setup cursor data }
       x := surface.width div 2;
       y := surface.height div 2;
       size := surface.width div 10;
-      color := TPTCColor.Create(1, 1, 1);
+      color := TPTCColorFactory.CreateNew(1, 1, 1);
 
       { start timer }
       timer.start;
@@ -92,14 +88,8 @@ begin
         { clear surface }
         surface.clear;
 
-        { setup cursor area }
-        area := TPTCArea.Create(Trunc(x) - size, Trunc(y) - size, Trunc(x) + size, Trunc(y) + size);
-        try
-          { draw cursor as a quad }
-          surface.clear(color, area);
-        finally
-          area.Free;
-        end;
+        { draw cursor as a quad }
+        surface.clear(color, TPTCAreaFactory.CreateNew(Trunc(x) - size, Trunc(y) - size, Trunc(x) + size, Trunc(y) + size));
 
         { copy to console }
         surface.copy(console);
@@ -108,13 +98,8 @@ begin
         console.update;
       until Done;
     finally
-      color.Free;
-      console.close;
-      console.Free;
-      surface.Free;
-      key.Free;
-      timer.Free;
-      format.Free;
+      if Assigned(console) then
+        console.close;
     end;
   except
     on error: TPTCError do

+ 11 - 16
packages/ptc/examples/land.pp

@@ -270,11 +270,11 @@ begin
 end;
 
 var
-  format: TPTCFormat = nil;
-  console: TPTCConsole = nil;
-  surface: TPTCSurface = nil;
-  timer: TPTCTimer = nil;
-  key: TPTCKeyEvent = nil;
+  format: IPTCFormat;
+  console: IPTCConsole;
+  surface: IPTCSurface;
+  timer: IPTCTimer;
+  key: IPTCKeyEvent;
   pixels: PUint32;
   Done: Boolean;
 
@@ -286,11 +286,10 @@ begin
   Done := False;
   try
     try
-      key := TPTCKeyEvent.Create;
-      format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
-      console := TPTCConsole.Create;
+      format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
+      console := TPTCConsoleFactory.CreateNew;
       console.open('Land demo', SCREENWIDTH, SCREENHEIGHT, format);
-      surface := TPTCSurface.Create(SCREENWIDTH, SCREENHEIGHT, format);
+      surface := TPTCSurfaceFactory.CreateNew(SCREENWIDTH, SCREENHEIGHT, format);
 
       { Compute the height map }
       ComputeMap;
@@ -309,7 +308,7 @@ begin
       scale := 20;
 
       { create timer }
-      timer := TPTCTimer.Create;
+      timer := TPTCTimerFactory.CreateNew;
 
       { start timer }
       timer.start;
@@ -372,12 +371,8 @@ begin
         Inc(y0, Trunc(CurrentSpeed * SinT[index]) div 256);
       until Done;
     finally
-      console.close;
-      console.Free;
-      surface.Free;
-      timer.Free;
-      format.Free;
-      key.Free;
+      if Assigned(console) then
+        console.close;
     end;
   except
     on error: TPTCError do

+ 12 - 15
packages/ptc/examples/lights.pp

@@ -28,10 +28,10 @@ begin
 end;
 
 var
-  console: TPTCConsole = nil;
-  surface: TPTCSurface = nil;
-  format: TPTCFormat = nil;
-  palette: TPTCPalette = nil;
+  console: IPTCConsole;
+  surface: IPTCSurface;
+  format: IPTCFormat;
+  palette: IPTCPalette;
   dx, dy: Integer;
   divisor: Single;
   data: PUint32;
@@ -54,15 +54,15 @@ begin
   try
     try
       { create console }
-      console := TPTCConsole.Create;
+      console := TPTCConsoleFactory.CreateNew;
 
-      format := TPTCFormat.Create(8);
+      format := TPTCFormatFactory.CreateNew(8);
 
       { open console }
       console.open('Lights demo', 320, 200, format);
 
       { create surface }
-      surface := TPTCSurface.Create(320, 200, format);
+      surface := TPTCSurfaceFactory.CreateNew(320, 200, format);
 
       { setup intensity table }
       for dy := 0 to 199 do
@@ -75,15 +75,15 @@ begin
         end;
 
       { create palette }
-      palette := TPTCPalette.Create;
+      palette := TPTCPaletteFactory.CreateNew;
 
       { generate greyscale palette }
-      data := palette.lock;
+      data := palette.Lock;
       try
         for i := 0 to 255 do
           data[i] := (i shl 16) or (i shl 8) or i;
       finally
-        palette.unlock;
+        palette.Unlock;
       end;
 
       { set console palette }
@@ -271,11 +271,8 @@ begin
         console.update;
       end;
     finally
-      console.close;
-      surface.Free;
-      console.Free;
-      palette.Free;
-      format.Free;
+      if Assigned(console) then
+        console.close;
     end;
   except
     on error: TPTCError do

+ 21 - 31
packages/ptc/examples/modes.pp

@@ -15,7 +15,7 @@ program ModesExample;
 uses
   ptc;
 
-procedure print(const format: TPTCFormat);
+procedure print(format: IPTCFormat);
 begin
   { check format type }
   if format.direct then
@@ -31,7 +31,7 @@ begin
     Write('Format(', format.bits:2, ')');
 end;
 
-procedure print(const mode: TPTCMode);
+procedure print(mode: IPTCMode);
 begin
   { print mode width and height }
   Write(' ', mode.width:4, ' x ', mode.height);
@@ -51,42 +51,32 @@ begin
 end;
 
 var
-  console: TPTCConsole = nil;
-  modes: PPTCMode;
+  console: IPTCConsole;
+  modes: TPTCModeList;
   index: Integer;
 begin
   try
-    try
-      { create console }
-      console := TPTCConsole.Create;
+    { create console }
+    console := TPTCConsoleFactory.CreateNew;
 
-      { get list of console modes }
-      modes := console.modes;
+    { get list of console modes }
+    modes := console.modes;
 
-      { check for empty list }
-      if not modes[0].valid then
-        { the console mode list was empty }
-        Writeln('[console mode list is not available]')
-      else
-      begin
-        { print mode list header }
-        Writeln('[console modes]');
-
-        { mode index }
-        index := 0;
-
-        { iterate through all modes }
-        while modes[index].valid do
-        begin
-          { print mode }
-          print(modes[index]);
+    { check for empty list }
+    if Length(modes) = 0 then
+      { the console mode list was empty }
+      Writeln('[console mode list is not available]')
+    else
+    begin
+      { print mode list header }
+      Writeln('[console modes]');
 
-          { next mode }
-          Inc(index);
-        end;
+      { iterate through all modes }
+      for index := Low(modes) to High(modes) do
+      begin
+        { print mode }
+        print(modes[index]);
       end;
-    finally
-      console.Free;
     end;
   except
     on error: TPTCError do

+ 9 - 11
packages/ptc/examples/mojo.pp

@@ -520,7 +520,7 @@ begin
   frandtab_seed := (frandtab_seed + 1) and $FFFF;
 end;
 
-procedure VLightPart(console: TPTCConsole; surface: TPTCSurface);
+procedure VLightPart(console: IPTCConsole; surface: IPTCSurface);
 var
   vl: VLight = nil;
   vl2: VLight = nil;
@@ -705,33 +705,31 @@ begin
 end;
 
 var
-  format: TPTCFormat = nil;
-  console: TPTCConsole = nil;
-  surface: TPTCSurface = nil;
+  format: IPTCFormat;
+  console: IPTCConsole;
+  surface: IPTCSurface;
 begin
   try
     try
       { create format }
-      format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
+      format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
 
       { create console }
-      console := TPTCConsole.Create;
+      console := TPTCConsoleFactory.CreateNew;
 
       { open console }
       console.open('mojo by statix', 320, 200, format);
 
       { create main drawing surface }
-      surface := TPTCSurface.Create(320, 200, format);
+      surface := TPTCSurfaceFactory.CreateNew(320, 200, format);
 
       { do the light effect }
       VLightPart(console, surface);
 
     finally
       { close console }
-      console.close;
-      console.Free;
-      surface.Free;
-      format.Free;
+      if Assigned(console) then
+        console.close;
     end;
 
     { print message to stdout }

+ 16 - 19
packages/ptc/examples/mouse.pp

@@ -8,13 +8,13 @@ program MouseExample;
 {$MODE objfpc}
 
 uses
-  ptc;
+  ptc, SysUtils;
 
 var
-  console: TPTCConsole = nil;
-  surface: TPTCSurface = nil;
-  format: TPTCFormat = nil;
-  event: TPTCEvent = nil;
+  console: IPTCConsole;
+  surface: IPTCSurface;
+  format: IPTCFormat;
+  event: IPTCEvent;
   pixels: PUint32;
   color: Uint32;
   width, height: Integer;
@@ -26,10 +26,10 @@ begin
   try
     try
       { create console }
-      console := TPTCConsole.Create;
+      console := TPTCConsoleFactory.CreateNew;
 
       { create format }
-      format := TPTCFormat.Create(32, $FF0000, $FF00, $FF);
+      format := TPTCFormatFactory.CreateNew(32, $FF0000, $FF00, $FF);
 
       { open the console }
       console.open('Mouse example', format);
@@ -38,7 +38,7 @@ begin
       console.option('hide cursor');
 
       { create surface matching console dimensions }
-      surface := TPTCSurface.Create(console.width, console.height, format);
+      surface := TPTCSurfaceFactory.CreateNew(console.width, console.height, format);
 
       { initialization }
       X := 0;
@@ -49,20 +49,20 @@ begin
         console.NextEvent(event, True, PTCAnyEvent);
 
         { handle mouse events }
-        if event is TPTCMouseEvent then
+        if Supports(event, IPTCMouseEvent) then
         begin
           { if there's more than one mouse event, process them all... }
           repeat
-            X := (event as TPTCMouseEvent).X;
-            Y := (event as TPTCMouseEvent).Y;
-            button := PTCMouseButton1 in (event as TPTCMouseEvent).ButtonState;
+            X := (event as IPTCMouseEvent).X;
+            Y := (event as IPTCMouseEvent).Y;
+            button := PTCMouseButton1 in (event as IPTCMouseEvent).ButtonState;
           until not console.NextEvent(event, False, [PTCMouseEvent]);
         end;
 
         { handle keyboard events }
-        if (event is TPTCKeyEvent) and (event as TPTCKeyEvent).Press then
+        if Supports(event, IPTCKeyEvent) and (event as IPTCKeyEvent).Press then
         begin
-          case (event as TPTCKeyEvent).Code of
+          case (event as IPTCKeyEvent).Code of
             PTCKEY_G: console.Option('grab mouse');
             PTCKEY_U: console.Option('ungrab mouse');
             PTCKEY_ESCAPE: Done := True;
@@ -114,11 +114,8 @@ begin
 
       until Done;
     finally
-      console.close;
-      console.Free;
-      surface.Free;
-      format.Free;
-      event.Free;
+      if Assigned(console) then
+        console.close;
     end;
   except
     on error: TPTCError do

+ 13 - 16
packages/ptc/examples/palette.pp

@@ -16,10 +16,10 @@ uses
   ptc;
 
 var
-  console: TPTCConsole = nil;
-  surface: TPTCSurface = nil;
-  format: TPTCFormat = nil;
-  palette: TPTCPalette = nil;
+  console: IPTCConsole;
+  surface: IPTCSurface;
+  format: IPTCFormat;
+  palette: IPTCPalette;
   data: array [0..255] of Uint32;
   pixels: PUint8;
   width, height: Integer;
@@ -29,32 +29,32 @@ begin
   try
     try
       { create console }
-      console := TPTCConsole.Create;
+      console := TPTCConsoleFactory.CreateNew;
 
       { create format }
-      format := TPTCFormat.Create(8);
+      format := TPTCFormatFactory.CreateNew(8);
 
       { open console }
       console.open('Palette example', format);
 
       { create surface }
-      surface := TPTCSurface.Create(console.width, console.height, format);
+      surface := TPTCSurfaceFactory.CreateNew(console.width, console.height, format);
 
       { create palette }
-      palette := TPTCPalette.Create;
+      palette := TPTCPaletteFactory.CreateNew;
 
       { generate palette }
       for i := 0 to 255 do
         data[i] := i;
 
       { load palette data }
-      palette.load(data);
+      palette.Load(data);
 
       { set console palette }
-      console.palette(palette);
+      console.Palette(palette);
 
       { set surface palette }
-      surface.palette(palette);
+      surface.Palette(palette);
 
       { loop until a key is pressed }
       while not console.KeyPressed do
@@ -92,11 +92,8 @@ begin
         console.update;
       end;
     finally
-      console.close;
-      console.Free;
-      surface.Free;
-      palette.Free;
-      format.Free;
+      if Assigned(console) then
+        console.close;
     end;
   except
     on error: TPTCError do

+ 9 - 11
packages/ptc/examples/pixel.pp

@@ -15,7 +15,7 @@ program PixelExample;
 uses
   ptc;
 
-procedure putpixel(surface: TPTCSurface; x, y: Integer; r, g, b: Uint8);
+procedure putpixel(surface: IPTCSurface; x, y: Integer; r, g, b: Uint8);
 var
   pixels: PUint32;
   color: Uint32;
@@ -35,23 +35,23 @@ begin
 end;
 
 var
-  console: TPTCConsole = nil;
-  surface: TPTCSurface = nil;
-  format: TPTCFormat = nil;
+  console: IPTCConsole;
+  surface: IPTCSurface;
+  format: IPTCFormat;
 begin
   try
     try
       { create console }
-      console := TPTCConsole.Create;
+      console := TPTCConsoleFactory.CreateNew;
 
       { create format }
-      format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
+      format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
 
       { open the console }
       console.open('Pixel example', format);
 
       { create surface matching console dimensions }
-      surface := TPTCSurface.Create(console.width, console.height, format);
+      surface := TPTCSurfaceFactory.CreateNew(console.width, console.height, format);
 
       { plot a white pixel in the middle of the surface }
       putpixel(surface, surface.width div 2, surface.height div 2, 255, 255, 255);
@@ -65,10 +65,8 @@ begin
       { read key }
       console.ReadKey;
     finally
-      console.close;
-      console.Free;
-      surface.Free;
-      format.Free;
+      if Assigned(console) then
+        console.close;
     end;
   except
     on error: TPTCError do

+ 8 - 10
packages/ptc/examples/random.pp

@@ -16,9 +16,9 @@ uses
   ptc;
 
 var
-  console: TPTCConsole = nil;
-  surface: TPTCSurface = nil;
-  format: TPTCFormat = nil;
+  console: IPTCConsole;
+  surface: IPTCSurface;
+  format: IPTCFormat;
   pixels: PUint32;
   width, height: Integer;
   i: Integer;
@@ -27,16 +27,16 @@ begin
   try
     try
       { create console }
-      console := TPTCConsole.Create;
+      console := TPTCConsoleFactory.CreateNew;
 
       { create format }
-      format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
+      format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
 
       { open the console }
       console.open('Random example', format);
 
       { create surface matching console dimensions }
-      surface := TPTCSurface.Create(console.width, console.height, format);
+      surface := TPTCSurfaceFactory.CreateNew(console.width, console.height, format);
 
       { loop until a key is pressed }
       while not console.KeyPressed do
@@ -75,10 +75,8 @@ begin
         console.update;
       end;
     finally
-      console.close;
-      console.Free;
-      surface.Free;
-      format.Free;
+      if Assigned(console) then
+        console.close;
     end;
   except
     on error: TPTCError do

+ 19 - 29
packages/ptc/examples/save.pp

@@ -15,15 +15,14 @@ program SaveExample;
 uses
   ptc, Math;
 
-procedure save(surface: TPTCSurface; filename: string);
+procedure save(surface: IPTCSurface; filename: string);
 var
   F: File;
   width, height: Integer;
   size: Integer;
   y: Integer;
   pixels: PUint8 = nil;
-  format: TPTCFormat = nil;
-  palette: TPTCPalette = nil;
+  format: IPTCFormat;
   { generate the header for a true color targa image }
   header: array [0..17] of Uint8 =
     (0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
@@ -58,14 +57,13 @@ begin
     pixels := GetMem(size);
 
     {$IFDEF FPC_LITTLE_ENDIAN}
-    format := TPTCFormat.Create(24, $00FF0000, $0000FF00, $000000FF);
+    format := TPTCFormatFactory.CreateNew(24, $00FF0000, $0000FF00, $000000FF);
     {$ELSE FPC_LITTLE_ENDIAN}
-    format := TPTCFormat.Create(24, $000000FF, $0000FF00, $00FF0000);
+    format := TPTCFormatFactory.CreateNew(24, $000000FF, $0000FF00, $00FF0000);
     {$ENDIF FPC_LITTLE_ENDIAN}
-    palette := TPTCPalette.Create;
 
     { save surface to image pixels }
-    surface.save(pixels, width, height, width * 3, format, palette);
+    surface.save(pixels, width, height, width * 3, format, TPTCPaletteFactory.CreateNew);
 
     { write image pixels one line at a time }
     for y := height - 1 DownTo 0 do
@@ -75,9 +73,6 @@ begin
     { free image pixels }
     FreeMem(pixels);
 
-    palette.Free;
-    format.Free;
-
     CloseFile(F);
   end;
 end;
@@ -128,7 +123,7 @@ begin
   calculate := 0;
 end;
 
-procedure mandelbrot(console: TPTCConsole; surface: TPTCSurface;
+procedure mandelbrot(console: IPTCConsole; surface: IPTCSurface;
                      x1, y1, x2, y2: Single);
 const
   { constant values }
@@ -149,7 +144,7 @@ var
   count: Integer;
   index: Integer;
   color: Uint32;
-  area: TPTCArea;
+  area: IPTCArea;
 begin
   { generate fractal color table }
   for i := 0 to entries - 1 do
@@ -219,13 +214,10 @@ begin
       imaginary := imaginary + dy;
 
       { setup line area }
-      area := TPTCArea.Create(0, y, width, y + 1);
-      try
-        { copy surface area to console }
-        surface.copy(console, area, area);
-      finally
-        area.Free;
-      end;
+      area := TPTCAreaFactory.CreateNew(0, y, width, y + 1);
+
+      { copy surface area to console }
+      surface.copy(console, area, area);
 
       { update console area }
       console.update;
@@ -237,24 +229,24 @@ begin
 end;
 
 var
-  console: TPTCConsole = nil;
-  surface: TPTCSurface = nil;
-  format: TPTCFormat = nil;
+  console: IPTCConsole;
+  surface: IPTCSurface;
+  format: IPTCFormat;
   x1, y1, x2, y2: Single;
 begin
   try
     try
       { create console }
-      console := TPTCConsole.Create;
+      console := TPTCConsoleFactory.CreateNew;
 
       { create format }
-      format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
+      format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
 
       { open the console with a single page }
       console.open('Save example', format, 1);
 
       { create surface matching console dimensions }
-      surface := TPTCSurface.Create(console.width, console.height, format);
+      surface := TPTCSurfaceFactory.CreateNew(console.width, console.height, format);
 
       { setup viewing area }
       x1 := -2.00;
@@ -271,10 +263,8 @@ begin
       { read key }
       console.ReadKey;
     finally
-      console.close;
-      console.Free;
-      surface.Free;
-      format.Free;
+      if Assigned(console) then
+        console.close;
     end;
   except
     on error: TPTCError do

+ 29 - 49
packages/ptc/examples/stretch.pp

@@ -15,14 +15,13 @@ program StretchExample;
 uses
   ptc;
 
-procedure load(surface: TPTCSurface; filename: String);
+procedure load(surface: IPTCSurface; filename: string);
 var
   F: File;
   width, height: Integer;
   pixels: PByte = nil;
   y: Integer;
-  tmp: TPTCFormat;
-  tmp2: TPTCPalette;
+  format: IPTCFormat;
 begin
   { open image file }
   AssignFile(F, filename);
@@ -45,20 +44,11 @@ begin
 
     { load pixels to surface }
     {$IFDEF FPC_LITTLE_ENDIAN}
-    tmp := TPTCFormat.Create(24, $00FF0000, $0000FF00, $000000FF);
+    format := TPTCFormatFactory.CreateNew(24, $00FF0000, $0000FF00, $000000FF);
     {$ELSE FPC_LITTLE_ENDIAN}
-    tmp := TPTCFormat.Create(24, $000000FF, $0000FF00, $00FF0000);
+    format := TPTCFormatFactory.CreateNew(24, $000000FF, $0000FF00, $00FF0000);
     {$ENDIF FPC_LITTLE_ENDIAN}
-    try
-      tmp2 := TPTCPalette.Create;
-      try
-        surface.load(pixels, width, height, width * 3, tmp, tmp2);
-      finally
-        tmp2.Free;
-      end;
-    finally
-      tmp.Free;
-    end;
+    surface.Load(pixels, width, height, width * 3, format, TPTCPaletteFactory.CreateNew);
   finally
     { free image pixels }
     FreeMem(pixels);
@@ -69,13 +59,12 @@ begin
 end;
 
 var
-  console: TPTCConsole = nil;
-  surface: TPTCSurface = nil;
-  image: TPTCSurface = nil;
-  format: TPTCFormat = nil;
-  timer: TPTCTimer = nil;
-  area: TPTCArea = nil;
-  color: TPTCColor = nil;
+  console: IPTCConsole;
+  surface: IPTCSurface;
+  image: IPTCSurface;
+  format: IPTCFormat;
+  timer: IPTCTimer;
+  area: IPTCArea;
   time: Double;
   zoom: Single;
   x, y, x1, y1, x2, y2, dx, dy: Integer;
@@ -83,19 +72,19 @@ begin
   try
     try
       { create console }
-      console := TPTCConsole.Create;
+      console := TPTCConsoleFactory.CreateNew;
 
       { create format }
-      format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
+      format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
 
       { open the console }
       console.open('Stretch example', format);
 
       { create surface matching console dimensions }
-      surface := TPTCSurface.Create(console.width, console.height, format);
+      surface := TPTCSurfaceFactory.CreateNew(console.width, console.height, format);
 
       { create image surface }
-      image := TPTCSurface.Create(320, 140, format);
+      image := TPTCSurfaceFactory.CreateNew(320, 140, format);
 
       { load image to surface }
       load(image, 'stretch.tga');
@@ -107,11 +96,10 @@ begin
       dy := surface.height div 3;
 
       { create timer }
-      timer := TPTCTimer.Create;
+      timer := TPTCTimerFactory.CreateNew;
 
       { start timer }
       timer.start;
-      color := TPTCColor.Create(1, 1, 1);
 
       { loop until a key is pressed }
       while not console.KeyPressed do
@@ -120,7 +108,7 @@ begin
         time := timer.time;
 
         { clear surface to white background }
-        surface.clear(color);
+        surface.clear(TPTCColorFactory.CreateNew(1, 1, 1));
 
         { calculate zoom factor at current time }
         zoom := 2.5 * (1 - cos(time));
@@ -132,28 +120,20 @@ begin
         y2 := Trunc(y + zoom * dy);
 
         { setup image copy area }
-        area := TPTCArea.Create(x1, y1, x2, y2);
-        try
-          { copy and stretch image to surface }
-          image.copy(surface, image.area, area);
-
-          { copy surface to console }
-          surface.copy(console);
-
-          { update console }
-          console.update;
-        finally
-          area.Free;
-        end;
+        area := TPTCAreaFactory.CreateNew(x1, y1, x2, y2);
+
+        { copy and stretch image to surface }
+        image.copy(surface, image.area, area);
+
+        { copy surface to console }
+        surface.copy(console);
+
+        { update console }
+        console.update;
       end;
     finally
-      console.close;
-      console.Free;
-      surface.Free;
-      format.Free;
-      image.Free;
-      color.Free;
-      timer.Free;
+      if Assigned(console) then
+        console.close;
     end;
   except
     on error: TPTCError do

+ 12 - 15
packages/ptc/examples/texwarp.pp

@@ -22,7 +22,7 @@ const
   green_balance: Uint32 = 3;
   blue_balance: Uint32 = 1;
 
-procedure blur(s: TPTCSurface);
+procedure blur(s: IPTCSurface);
 var
   d: PUint8;
   pitch: Integer;
@@ -60,7 +60,7 @@ begin
   end;
 end;
 
-procedure generate(surface: TPTCSurface);
+procedure generate(surface: IPTCSurface);
 var
   dest: PUint32;
   i: Integer;
@@ -256,10 +256,10 @@ begin
 end;
 
 var
-  format: TPTCFormat = nil;
-  texture: TPTCSurface = nil;
-  surface: TPTCSurface = nil;
-  console: TPTCConsole = nil;
+  format: IPTCFormat;
+  texture: IPTCSurface;
+  surface: IPTCSurface;
+  console: IPTCConsole;
   lighttable: PUint8 = nil;
   { texture grid }
   grid: array [0..41*26*3-1] of Uint32;
@@ -270,10 +270,10 @@ begin
   try
     try
       { create format }
-      format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
+      format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
 
       { create texture surface }
-      texture := TPTCSurface.Create(256, 256, format);
+      texture := TPTCSurfaceFactory.CreateNew(256, 256, format);
 
       { create texture }
       generate(texture);
@@ -283,13 +283,13 @@ begin
       make_light_table(lighttable);
 
       { create console }
-      console := TPTCConsole.Create;
+      console := TPTCConsoleFactory.CreateNew;
 
       { open console }
       console.open('Warp demo', 320, 200, format);
 
       { create drawing surface }
-      surface := TPTCSurface.Create(320, 200, format);
+      surface := TPTCSurfaceFactory.CreateNew(320, 200, format);
 
       { control values }
       xbase := 0;
@@ -365,11 +365,8 @@ begin
         end;
       end;
     finally
-      console.close;
-      console.Free;
-      surface.Free;
-      texture.Free;
-      format.Free;
+      if Assigned(console) then
+        console.close;
       FreeMem(lighttable);
     end;
   except

+ 10 - 13
packages/ptc/examples/timer.pp

@@ -16,10 +16,10 @@ uses
   ptc;
 
 var
-  console: TPTCConsole = nil;
-  format: TPTCFormat = nil;
-  surface: TPTCSurface = nil;
-  timer: TPTCTimer = nil;
+  console: IPTCConsole;
+  format: IPTCFormat;
+  surface: IPTCSurface;
+  timer: IPTCTimer;
   time, t: Double;
   pixels: PDWord;
   width, height: Integer;
@@ -29,19 +29,19 @@ begin
   try
     try
       { create console }
-      console := TPTCConsole.Create;
+      console := TPTCConsoleFactory.CreateNew;
 
       { create format }
-      format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
+      format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
 
       { open the console }
       console.open('Timer example', format);
 
       { create surface matching console dimensions }
-      surface := TPTCSurface.Create(console.width, console.height, format);
+      surface := TPTCSurfaceFactory.CreateNew(console.width, console.height, format);
 
       { create timer }
-      timer := TPTCTimer.Create;
+      timer := TPTCTimerFactory.CreateNew;
 
       { start timer }
       timer.start;
@@ -97,11 +97,8 @@ begin
         console.update;
       end;
     finally
-      timer.Free;
-      surface.Free;
-      console.close;
-      console.Free;
-      format.Free;
+      if Assigned(console) then
+        console.close;
     end;
   except
     on error: TPTCError do

+ 8 - 10
packages/ptc/examples/tunnel.pp

@@ -117,9 +117,9 @@ begin
 end;
 
 var
-  format: TPTCFormat = nil;
-  console: TPTCConsole = nil;
-  surface: TPTCSurface = nil;
+  format: IPTCFormat;
+  console: IPTCConsole;
+  surface: IPTCSurface;
   TheTunnel: TTunnel = nil;
   time, delta: Single;
   buffer: PUint32;
@@ -127,16 +127,16 @@ begin
   try
     try
       { create format }
-      format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
+      format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
 
       { create console }
-      console := TPTCConsole.Create;
+      console := TPTCConsoleFactory.CreateNew;
 
       { open console }
       console.open('Tunnel demo', 320, 200, format);
 
       { create surface }
-      surface := TPTCSurface.Create(320, 200, format);
+      surface := TPTCSurfaceFactory.CreateNew(320, 200, format);
 
       { create tunnel }
       TheTunnel := TTunnel.Create;
@@ -169,10 +169,8 @@ begin
       end;
     finally
       TheTunnel.Free;
-      surface.Free;
-      console.close;
-      console.Free;
-      format.Free;
+      if Assigned(console) then
+        console.close;
     end;
   except
     on error: TPTCError do

+ 20 - 22
packages/ptc/examples/tunnel3d.pp

@@ -343,7 +343,7 @@ begin
 
         { Calculate texture index at intersection point (cylindrical mapping) }
         { try and adjust the 0.2 to stretch/shrink the texture }
-        u_array[(j shl 6) + i] := Trunc(intsc[2] * 0.2) shl 16;
+        u_array[(j shl 6) + i] := Integer(Trunc(intsc[2] * 0.2) shl 16);
         v_array[(j shl 6) + i] := Trunc(abs(arctan2(intsc[1], intsc[0]) * 256 / pi)) shl 16;
 
         { Calculate the dotproduct between the normal vector and the vector }
@@ -396,23 +396,23 @@ begin
 
       { Set up gradients }
       lu := u_array[iadr]; ru := u_array[iadr + 1];
-      liu := (u_array[iadr + 64] - lu) shr 3;
-      riu := (u_array[iadr + 65] - ru) shr 3;
+      liu := (u_array[iadr + 64] - lu) div 8;
+      riu := (u_array[iadr + 65] - ru) div 8;
 
       lv := v_array[iadr]; rv := v_array[iadr + 1];
-      liv := (v_array[iadr + 64] - lv) shr 3;
-      riv := (v_array[iadr + 65] - rv) shr 3;
+      liv := (v_array[iadr + 64] - lv) div 8;
+      riv := (v_array[iadr + 65] - rv) div 8;
 
       ll := l_array[iadr]; rl := l_array[iadr + 1];
-      lil := (l_array[iadr + 64] - ll) shr 3;
-      ril := (l_array[iadr + 65] - rl) shr 3;
+      lil := (l_array[iadr + 64] - ll) div 8;
+      ril := (l_array[iadr + 65] - rl) div 8;
 
       for y := 0 to 7 do
       begin
-        iu := (ru - lu) shr 3;
-        iv := (rv - lv) shr 3;
+        iu := (ru - lu) div 8;
+        iv := (rv - lv) div 8;
         l := ll;
-        il := (rl - ll) shr 3;
+        il := (rl - ll) div 8;
 
         { Mess up everything for the sake of cache optimised mapping :) }
         til_u := DWord(((lu shl 8) and $F8000000) or ((lu shr 1) and $00007FFF) or (lu and $00070000));
@@ -426,8 +426,8 @@ begin
         for x := 0 to 7 do
         begin
           { Interpolate texture u,v and light }
-          Inc(til_u, til_iu);
-          Inc(til_v, til_iv);
+          til_u := DWord(til_u + til_iu);
+          til_v := DWord(til_v + til_iv);
           Inc(l, il);
 
           adr := adr shr 16;
@@ -502,9 +502,9 @@ begin
 end;
 
 var
-  console: TPTCConsole = nil;
-  surface: TPTCSurface = nil;
-  format: TPTCFormat = nil;
+  console: IPTCConsole;
+  surface: IPTCSurface;
+  format: IPTCFormat;
   tunnel: TRayTunnel = nil;
   posz, phase_x, phase_y: Single;
   angle_x, angle_y: Integer;
@@ -512,12 +512,12 @@ var
 begin
   try
     try
-      format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
+      format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
 
-      console := TPTCConsole.create;
+      console := TPTCConsoleFactory.CreateNew;
       console.open('Tunnel3D demo', 320, 200, format);
 
-      surface := TPTCSurface.create(320, 200, format);
+      surface := TPTCSurfaceFactory.CreateNew(320, 200, format);
 
       { Create a tunnel, radius=700 }
       tunnel := TRayTunnel.Create(700);
@@ -554,11 +554,9 @@ begin
         phase_y := phase_y + 0.1;
       end;
     finally
-      console.close;
-      console.Free;
-      surface.Free;
+      if Assigned(console) then
+        console.close;
       tunnel.Free;
-      format.Free;
     end;
   except
     on error: TPTCError do

+ 19 - 13
packages/ptc/src/core/aread.inc

@@ -31,21 +31,27 @@
 }
 
 type
-  TPTCArea = class
-  private
-    FLeft, FTop, FRight, FBottom: Integer;
+  IPTCArea = interface
+    function GetLeft: Integer;
+    function GetTop: Integer;
+    function GetRight: Integer;
+    function GetBottom: Integer;
     function GetWidth: Integer;
     function GetHeight: Integer;
-  public
-    constructor Create;
-    constructor Create(ALeft, ATop, ARight, ABottom: Integer);
-    constructor Create(const AArea: TPTCArea);
-    procedure Assign(const AArea: TPTCArea);
-    function Equals(const AArea: TPTCArea): Boolean;
-    property Left: Integer read FLeft;
-    property Top: Integer read FTop;
-    property Right: Integer read FRight;
-    property Bottom: Integer read FBottom;
+
+    function Equals(AArea: IPTCArea): Boolean;
+
+    property Left: Integer read GetLeft;
+    property Top: Integer read GetTop;
+    property Right: Integer read GetRight;
+    property Bottom: Integer read GetBottom;
     property Width: Integer read GetWidth;
     property Height: Integer read GetHeight;
   end;
+
+  TPTCAreaFactory = class
+  public
+    class function CreateNew: IPTCArea;
+    class function CreateNew(ALeft, ATop, ARight, ABottom: Integer): IPTCArea;
+    class function CreateNew(AArea: IPTCArea): IPTCArea;
+  end;

+ 59 - 15
packages/ptc/src/core/areai.inc

@@ -30,6 +30,38 @@
     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 }
 
+type
+  TPTCArea = class(TInterfacedObject, IPTCArea)
+  private
+    FLeft, FTop, FRight, FBottom: Integer;
+    function GetLeft: Integer;
+    function GetTop: Integer;
+    function GetRight: Integer;
+    function GetBottom: Integer;
+    function GetWidth: Integer;
+    function GetHeight: Integer;
+    function Equals(AArea: IPTCArea): Boolean;
+  public
+    constructor Create;
+    constructor Create(ALeft, ATop, ARight, ABottom: Integer);
+    constructor Create(AArea: IPTCArea);
+  end;
+
+class function TPTCAreaFactory.CreateNew: IPTCArea;
+begin
+  Result := TPTCArea.Create;
+end;
+
+class function TPTCAreaFactory.CreateNew(ALeft, ATop, ARight, ABottom: Integer): IPTCArea;
+begin
+  Result := TPTCArea.Create(ALeft, ATop, ARight, ABottom);
+end;
+
+class function TPTCAreaFactory.CreateNew(AArea: IPTCArea): IPTCArea;
+begin
+  Result := TPTCArea.Create(AArea);
+end;
+
 constructor TPTCArea.Create(ALeft, ATop, ARight, ABottom: Integer);
 begin
   if ALeft < ARight then
@@ -62,28 +94,40 @@ begin
   FBottom := 0;
 end;
 
-constructor TPTCArea.Create(const AArea: TPTCArea);
+constructor TPTCArea.Create(AArea: IPTCArea);
+begin
+  FLeft   := AArea.Left;
+  FTop    := AArea.Top;
+  FRight  := AArea.Right;
+  FBottom := AArea.Bottom;
+end;
+
+function TPTCArea.Equals(AArea: IPTCArea): Boolean;
+begin
+  Result := (FLeft   = AArea.Left) and
+            (FTop    = AArea.Top) and
+            (FRight  = AArea.Right) and
+            (FBottom = AArea.Bottom);
+end;
+
+function TPTCArea.GetLeft: Integer;
+begin
+  Result := FLeft;
+end;
+
+function TPTCArea.GetTop: Integer;
 begin
-  FLeft   := AArea.FLeft;
-  FTop    := AArea.FTop;
-  FRight  := AArea.FRight;
-  FBottom := AArea.FBottom;
+  Result := FTop;
 end;
 
-procedure TPTCArea.Assign(const AArea: TPTCArea);
+function TPTCArea.GetRight: Integer;
 begin
-  FLeft   := AArea.FLeft;
-  FTop    := AArea.FTop;
-  FRight  := AArea.FRight;
-  FBottom := AArea.FBottom;
+  Result := FRight;
 end;
 
-function TPTCArea.Equals(const AArea: TPTCArea): Boolean;
+function TPTCArea.GetBottom: Integer;
 begin
-  Result := (FLeft   = AArea.FLeft) and
-            (FTop    = AArea.FTop) and
-            (FRight  = AArea.FRight) and
-            (FBottom = AArea.FBottom);
+  Result := FBottom;
 end;
 
 function TPTCArea.GetWidth: Integer;

+ 27 - 27
packages/ptc/src/core/baseconsoled.inc

@@ -31,43 +31,43 @@
 }
 
 type
-  TPTCBaseConsole = class(TPTCBaseSurface)
-  private
-    FReleaseEnabled: Boolean;
-    function GetPages: Integer; virtual; abstract;
-    function GetName: string; virtual; abstract;
-    function GetTitle: string; virtual; abstract;
-    function GetInformation: string; virtual; abstract;
-  public
-    constructor Create; virtual;
-    procedure Configure(const AFileName: String); virtual; abstract;
-    function Modes: PPTCMode; virtual; abstract;
-    procedure Open(const ATitle: string; APages: Integer = 0); overload; virtual; abstract;
-    procedure Open(const ATitle: string; const AFormat: TPTCFormat;
-                   APages: Integer = 0); overload; virtual; abstract;
+  IPTCConsole = interface(IPTCSurface)
+    function GetPages: Integer;
+    function GetName: string;
+    function GetTitle: string;
+    function GetInformation: string;
+
+    procedure Configure(const AFileName: string);
+    function Modes: TPTCModeList;
+    procedure Open(const ATitle: string; APages: Integer = 0); overload;
+    procedure Open(const ATitle: string; AFormat: IPTCFormat;
+                   APages: Integer = 0); overload;
     procedure Open(const ATitle: string; AWidth, AHeight: Integer;
-                   const AFormat: TPTCFormat; APages: Integer = 0); overload; virtual; abstract;
-    procedure Open(const ATitle: string; const AMode: TPTCMode;
-                   APages: Integer = 0); overload; virtual; abstract;
-    procedure Close; virtual; abstract;
-    procedure Flush; virtual; abstract;
-    procedure Finish; virtual; abstract;
-    procedure Update; virtual; abstract;
-    procedure Update(const AArea: TPTCArea); virtual; abstract;
+                   AFormat: IPTCFormat; APages: Integer = 0); overload;
+    procedure Open(const ATitle: string; AMode: IPTCMode;
+                   APages: Integer = 0); overload;
+    procedure Close;
+    procedure Flush;
+    procedure Finish;
+    procedure Update;
+    procedure Update(AArea: IPTCArea);
 
     { event handling }
-    function NextEvent(var AEvent: TPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean; virtual; abstract;
-    function PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): TPTCEvent; virtual; abstract;
+    function NextEvent(out AEvent: IPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean;
+    function PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): IPTCEvent;
 
     { key handling }
     function KeyPressed: Boolean;
-    function PeekKey(var AKey: TPTCKeyEvent): Boolean;
-    procedure ReadKey(var AKey: TPTCKeyEvent);
+    function PeekKey(out AKey: IPTCKeyEvent): Boolean;
+    procedure ReadKey(out AKey: IPTCKeyEvent);
     procedure ReadKey;
-    property KeyReleaseEnabled: Boolean read FReleaseEnabled write FReleaseEnabled;
+    procedure SetKeyReleaseEnabled(AValue: Boolean);
+    function GetKeyReleaseEnabled: Boolean;
+    property KeyReleaseEnabled: Boolean read GetKeyReleaseEnabled write SetKeyReleaseEnabled;
 
     property Pages: Integer read GetPages;
     property Name: string read GetName;
     property Title: string read GetTitle;
     property Information: string read GetInformation;
   end;
+

+ 120 - 32
packages/ptc/src/core/baseconsolei.inc

@@ -30,6 +30,98 @@
     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 }
 
+type
+  TPTCBaseConsole = class(TInterfacedObject, IPTCConsole, IPTCSurface)
+  private
+    FReleaseEnabled: Boolean;
+
+    procedure SetKeyReleaseEnabled(AValue: Boolean);
+    function GetKeyReleaseEnabled: Boolean;
+
+    function GetWidth: Integer; virtual; abstract;
+    function GetHeight: Integer; virtual; abstract;
+    function GetPitch: Integer; virtual; abstract;
+    function GetArea: IPTCArea; virtual; abstract;
+    function GetFormat: IPTCFormat; virtual; abstract;
+
+    function GetPages: Integer; virtual; abstract;
+    function GetName: string; virtual; abstract;
+    function GetTitle: string; virtual; abstract;
+    function GetInformation: string; virtual; abstract;
+  public
+    constructor Create; virtual;
+
+    procedure Copy(ASurface: IPTCSurface); virtual; abstract;
+    procedure Copy(ASurface: IPTCSurface;
+                   ASource, ADestination: IPTCArea); virtual; abstract;
+    function Lock: Pointer; virtual; abstract;
+    procedure Unlock; virtual; abstract;
+    procedure Load(const APixels: Pointer;
+                   AWidth, AHeight, APitch: Integer;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette); virtual; abstract;
+    procedure Load(const APixels: Pointer;
+                   AWidth, AHeight, APitch: Integer;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette;
+                   ASource, ADestination: IPTCArea); virtual; abstract;
+    procedure Save(APixels: Pointer;
+                   AWidth, AHeight, APitch: Integer;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette); virtual; abstract;
+    procedure Save(APixels: Pointer;
+                   AWidth, AHeight, APitch: Integer;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette;
+                   ASource, ADestination: IPTCArea); virtual; abstract;
+    procedure Clear; virtual; abstract;
+    procedure Clear(AColor: IPTCColor); virtual; abstract;
+    procedure Clear(AColor: IPTCColor;
+                    AArea: IPTCArea); virtual; abstract;
+    procedure Palette(APalette: IPTCPalette); virtual; abstract;
+    procedure Clip(AArea: IPTCArea); virtual; abstract;
+    function Option(const AOption: String): Boolean; virtual; abstract;
+    function Clip: IPTCArea; virtual; abstract;
+    function Palette: IPTCPalette; virtual; abstract;
+
+    procedure Configure(const AFileName: String); virtual; abstract;
+    function Modes: TPTCModeList; virtual; abstract;
+    procedure Open(const ATitle: string; APages: Integer = 0); overload; virtual; abstract;
+    procedure Open(const ATitle: string; AFormat: IPTCFormat;
+                   APages: Integer = 0); overload; virtual; abstract;
+    procedure Open(const ATitle: string; AWidth, AHeight: Integer;
+                   AFormat: IPTCFormat; APages: Integer = 0); overload; virtual; abstract;
+    procedure Open(const ATitle: string; AMode: IPTCMode;
+                   APages: Integer = 0); overload; virtual; abstract;
+    procedure Close; virtual; abstract;
+    procedure Flush; virtual; abstract;
+    procedure Finish; virtual; abstract;
+    procedure Update; virtual; abstract;
+    procedure Update(AArea: IPTCArea); virtual; abstract;
+
+    { event handling }
+    function NextEvent(out AEvent: IPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean; virtual; abstract;
+    function PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): IPTCEvent; virtual; abstract;
+
+    { key handling }
+    function KeyPressed: Boolean;
+    function PeekKey(out AKey: IPTCKeyEvent): Boolean;
+    procedure ReadKey(out AKey: IPTCKeyEvent);
+    procedure ReadKey;
+    property KeyReleaseEnabled: Boolean read GetKeyReleaseEnabled write SetKeyReleaseEnabled;
+
+    property Pages: Integer read GetPages;
+    property Name: string read GetName;
+    property Title: string read GetTitle;
+    property Information: string read GetInformation;
+
+    property Width: Integer read GetWidth;
+    property Height: Integer read GetHeight;
+    property Pitch: Integer read GetPitch;
+    property Area: IPTCArea read GetArea;
+    property Format: IPTCFormat read GetFormat;
+  end;
+
 constructor TPTCBaseConsole.Create;
 begin
   FReleaseEnabled := False;
@@ -37,38 +129,29 @@ end;
 
 function TPTCBaseConsole.KeyPressed: Boolean;
 var
-  k, kpeek: TPTCEvent;
+  k, kpeek: IPTCEvent;
 begin
-  k := nil;
-  try
-    repeat
-      kpeek := PeekEvent(False, [PTCKeyEvent]);
-      if kpeek = nil then
-        exit(False);
-      if FReleaseEnabled or (kpeek As TPTCKeyEvent).Press then
-        exit(True);
-      NextEvent(k, False, [PTCKeyEvent]);
-    until False;
-  finally
-    k.Free;
-  end;
+  repeat
+    kpeek := PeekEvent(False, [PTCKeyEvent]);
+    if kpeek = nil then
+      exit(False);
+    if FReleaseEnabled or (kpeek as IPTCKeyEvent).Press then
+      exit(True);
+    NextEvent(k, False, [PTCKeyEvent]);
+  until False;
 end;
 
-procedure TPTCBaseConsole.ReadKey(var AKey: TPTCKeyEvent);
+procedure TPTCBaseConsole.ReadKey(out AKey: IPTCKeyEvent);
 var
-  ev: TPTCEvent;
+  ev: IPTCEvent;
 begin
-  ev := AKey;
-  try
-    repeat
-      NextEvent(ev, True, [PTCKeyEvent]);
-    until FReleaseEnabled or (ev As TPTCKeyEvent).Press;
-  finally
-    AKey := ev As TPTCKeyEvent;
-  end;
+  repeat
+    NextEvent(ev, True, [PTCKeyEvent]);
+  until FReleaseEnabled or (ev as IPTCKeyEvent).Press;
+  AKey := ev as IPTCKeyEvent;
 end;
 
-function TPTCBaseConsole.PeekKey(var AKey: TPTCKeyEvent): Boolean;
+function TPTCBaseConsole.PeekKey(out AKey: IPTCKeyEvent): Boolean;
 begin
   if KeyPressed then
   begin
@@ -81,12 +164,17 @@ end;
 
 procedure TPTCBaseConsole.ReadKey;
 var
-  k: TPTCKeyEvent;
+  k: IPTCKeyEvent;
 begin
-  k := TPTCKeyEvent.Create;
-  try
-    ReadKey(k);
-  finally
-    k.Free;
-  end;
+  ReadKey(k);
+end;
+
+procedure TPTCBaseConsole.SetKeyReleaseEnabled(AValue: Boolean);
+begin
+  FReleaseEnabled := AValue;
+end;
+
+function TPTCBaseConsole.GetKeyReleaseEnabled: Boolean;
+begin
+  Result := FReleaseEnabled;
 end;

+ 33 - 34
packages/ptc/src/core/basesurfaced.inc

@@ -31,49 +31,48 @@
 }
 
 type
-  TPTCBaseSurface = class
-  private
-    function GetWidth: Integer; virtual; abstract;
-    function GetHeight: Integer; virtual; abstract;
-    function GetPitch: Integer; virtual; abstract;
-    function GetArea: TPTCArea; virtual; abstract;
-    function GetFormat: TPTCFormat; virtual; abstract;
-  public
-    procedure Copy(ASurface: TPTCBaseSurface); virtual; abstract;
-    procedure Copy(ASurface: TPTCBaseSurface;
-                   const ASource, ADestination: TPTCArea); virtual; abstract;
-    function Lock: Pointer; virtual; abstract;
-    procedure Unlock; virtual; abstract;
+  IPTCSurface = interface
+    function GetWidth: Integer;
+    function GetHeight: Integer;
+    function GetPitch: Integer;
+    function GetArea: IPTCArea;
+    function GetFormat: IPTCFormat;
+
+    procedure Copy(ASurface: IPTCSurface);
+    procedure Copy(ASurface: IPTCSurface;
+                   ASource, ADestination: IPTCArea);
+    function Lock: Pointer;
+    procedure Unlock;
     procedure Load(const APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette); virtual; abstract;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette);
     procedure Load(const APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette;
-                   const ASource, ADestination: TPTCArea); virtual; abstract;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette;
+                   ASource, ADestination: IPTCArea);
     procedure Save(APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette); virtual; abstract;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette);
     procedure Save(APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette;
-                   const ASource, ADestination: TPTCArea); virtual; abstract;
-    procedure Clear; virtual; abstract;
-    procedure Clear(const AColor: TPTCColor); virtual; abstract;
-    procedure Clear(const AColor: TPTCColor;
-                    const AArea: TPTCArea); virtual; abstract;
-    procedure Palette(const APalette: TPTCPalette); virtual; abstract;
-    procedure Clip(const AArea: TPTCArea); virtual; abstract;
-    function Option(const AOption: String): Boolean; virtual; abstract;
-    function Clip: TPTCArea; virtual; abstract;
-    function Palette: TPTCPalette; virtual; abstract;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette;
+                   ASource, ADestination: IPTCArea);
+    procedure Clear;
+    procedure Clear(AColor: IPTCColor);
+    procedure Clear(AColor: IPTCColor;
+                    AArea: IPTCArea);
+    procedure Palette(APalette: IPTCPalette);
+    procedure Clip(AArea: IPTCArea);
+    function Option(const AOption: String): Boolean;
+    function Clip: IPTCArea;
+    function Palette: IPTCPalette;
     property Width: Integer read GetWidth;
     property Height: Integer read GetHeight;
     property Pitch: Integer read GetPitch;
-    property Area: TPTCArea read GetArea;
-    property Format: TPTCFormat read GetFormat;
+    property Area: IPTCArea read GetArea;
+    property Format: IPTCFormat read GetFormat;
   end;

+ 3 - 3
packages/ptc/src/core/cleard.inc

@@ -34,12 +34,12 @@ type
   TPTCClear = class
   private
     FHandle: THermesClearerHandle;
-    FFormat: TPTCFormat;
+    FFormat: IPTCFormat;
   public
     constructor Create;
     destructor Destroy; override;
-    procedure Request(const AFormat: TPTCFormat);
+    procedure Request(AFormat: IPTCFormat);
     procedure Clear(APixels: Pointer;
                     AX, AY, AWidth, AHeight, APitch: Integer;
-                    const AColor: TPTCColor);
+                    AColor: IPTCColor);
   end;

+ 4 - 6
packages/ptc/src/core/cleari.inc

@@ -32,7 +32,6 @@
 
 constructor TPTCClear.Create;
 begin
-  FFormat := nil;
   { initialize hermes }
   if not Hermes_Init then
     raise TPTCError.Create('could not initialize hermes');
@@ -50,7 +49,6 @@ destructor TPTCClear.Destroy;
 begin
   { return the clearer instance }
   Hermes_ClearerReturn(FHandle);
-  FFormat.Free;
 
   { free hermes }
   Hermes_Done;
@@ -58,20 +56,20 @@ begin
   inherited Destroy;
 end;
 
-procedure TPTCClear.Request(const AFormat: TPTCFormat);
+procedure TPTCClear.Request(AFormat: IPTCFormat);
 var
   hermes_format: PHermesFormat;
 begin
-  hermes_format := @AFormat.FFormat;
+  hermes_format := AFormat.GetHermesFormat;
   { request surface clear for this format }
   if not Hermes_ClearerRequest(FHandle, hermes_format) then
     raise TPTCError.Create('unsupported clear format');
 
   { update current format }
-  FFormat.Assign(AFormat);
+  FFormat := AFormat;
 end;
 
-procedure TPTCClear.Clear(APixels: Pointer; AX, AY, AWidth, AHeight, APitch: Integer; const AColor: TPTCColor);
+procedure TPTCClear.Clear(APixels: Pointer; AX, AY, AWidth, AHeight, APitch: Integer; AColor: IPTCColor);
 var
   r, g, b, a: LongInt;
   index: LongInt;

+ 5 - 4
packages/ptc/src/core/clipperd.inc

@@ -34,9 +34,10 @@ type
   TPTCClipper = class
   public
     { clip a single area against clip area }
-    class function Clip(const AArea, AClip: TPTCArea): TPTCArea;
+    class function Clip(AArea, AClip: IPTCArea): IPTCArea;
     { clip source and destination areas against source and destination clip areas }
-    class procedure Clip(const ASource, AClipSource, AClippedSource,
-                         ADestination, AClipDestination,
-                         AClippedDestination: TPTCArea);
+    class procedure Clip(ASource, AClipSource: IPTCArea;
+                         out AClippedSource: IPTCArea;
+                         ADestination, AClipDestination: IPTCArea;
+                         out AClippedDestination: IPTCArea);
   end;

+ 110 - 121
packages/ptc/src/core/clipperi.inc

@@ -32,7 +32,7 @@
 
 {$INLINE ON}
 
-class function TPTCClipper.Clip(const AArea, AClip: TPTCArea): TPTCArea;
+class function TPTCClipper.Clip(AArea, AClip: IPTCArea): IPTCArea;
 var
   left, top, right, bottom: Integer;
   clip_left, clip_top, clip_right, clip_bottom: Integer;
@@ -103,7 +103,7 @@ begin
 end;
 
 { clip floating point area against clip area }
-procedure TPTCClipper_clip(var left, top, right, bottom: Real; const _clip: TPTCArea); Inline;
+procedure TPTCClipper_clip(var left, top, right, bottom: Real; const _clip: IPTCArea); Inline;
 var
   clip_left, clip_top, clip_right, clip_bottom: Real;
 begin
@@ -125,11 +125,11 @@ begin
   bottom := Round(bottom);
 end;
 
-class procedure TPTCClipper.Clip(const ASource, AClipSource, AClippedSource,
-                                 ADestination, AClipDestination,
-                                 AClippedDestination: TPTCArea);
+class procedure TPTCClipper.Clip(ASource, AClipSource: IPTCArea;
+                                 out AClippedSource: IPTCArea;
+                                 ADestination, AClipDestination: IPTCArea;
+                                 out AClippedDestination: IPTCArea);
 var
-  tmp1, tmp2: TPTCArea;
   source_left, source_top, source_right, source_bottom: Real;
   clipped_source_left, clipped_source_top, clipped_source_right,
   clipped_source_bottom: Real;
@@ -148,120 +148,109 @@ var
   adjusted_source_left, adjusted_source_top, adjusted_source_right,
   adjusted_source_bottom: Real;
 begin
-  tmp1 := nil;
-  tmp2 := nil;
-  try
-    { expand source area to floating point }
-    source_left   := ASource.Left;
-    source_top    := ASource.Top;
-    source_right  := ASource.Right;
-    source_bottom := ASource.Bottom;
-
-    { setup clipped source area }
-    clipped_source_left := source_left;
-    clipped_source_top := source_top;
-    clipped_source_right := source_right;
-    clipped_source_bottom := source_bottom;
-
-    { perform clipping on floating point source area }
-    TPTCClipper_clip(clipped_source_left, clipped_source_top, clipped_source_right,
-                     clipped_source_bottom, AClipSource);
-
-    { check for early source area clipping exit }
-    if (clipped_source_left = clipped_source_right) or
-       (clipped_source_top = clipped_source_bottom) then
-    begin
-      { clipped area is zero }
-      tmp1 := TPTCArea.Create(0, 0, 0, 0);
-      AClippedSource.Assign(tmp1);
-      AClippedDestination.Assign(tmp1);
-      exit;
-    end;
-
-    { calculate deltas in source clip }
-    source_delta_left := clipped_source_left - source_left;
-    source_delta_top := clipped_source_top - source_top;
-    source_delta_right := clipped_source_right - source_right;
-    source_delta_bottom := clipped_source_bottom - source_bottom;
-
-    { calculate ratio of source area to destination area }
-    source_to_destination_x := ADestination.Width / ASource.Width;
-    source_to_destination_y := ADestination.Height / ASource.Height;
-
-    { expand destination area to floating point }
-    destination_left   := ADestination.Left;
-    destination_top    := ADestination.Top;
-    destination_right  := ADestination.Right;
-    destination_bottom := ADestination.Bottom;
-
-    { calculate adjusted destination area }
-    adjusted_destination_left := destination_left + source_delta_left * source_to_destination_x;
-    adjusted_destination_top := destination_top + source_delta_top * source_to_destination_y;
-    adjusted_destination_right := destination_right + source_delta_right * source_to_destination_x;
-    adjusted_destination_bottom := destination_bottom + source_delta_bottom * source_to_destination_y;
-
-    { setup clipped destination area }
-    clipped_destination_left := adjusted_destination_left;
-    clipped_destination_top := adjusted_destination_top;
-    clipped_destination_right := adjusted_destination_right;
-    clipped_destination_bottom := adjusted_destination_bottom;
-
-    { perform clipping on floating point destination area }
-    TPTCClipper_clip(clipped_destination_left, clipped_destination_top,
-                     clipped_destination_right, clipped_destination_bottom, AClipDestination);
-
-    { check for early destination area clipping exit }
-    if (clipped_destination_left = clipped_destination_right) or
-       (clipped_destination_top = clipped_destination_bottom) then
-    begin
-      { clipped area is zero }
-      tmp1 := TPTCArea.Create(0, 0, 0, 0);
-      AClippedSource.Assign(tmp1);
-      AClippedDestination.Assign(tmp1);
-      exit;
-    end;
-
-    { calculate deltas in destination clip }
-    destination_delta_left := clipped_destination_left - adjusted_destination_left;
-    destination_delta_top := clipped_destination_top - adjusted_destination_top;
-    destination_delta_right := clipped_destination_right - adjusted_destination_right;
-    destination_delta_bottom := clipped_destination_bottom - adjusted_destination_bottom;
-
-    { calculate ratio of destination area to source area }
-    destination_to_source_x := 1 / source_to_destination_x;
-    destination_to_source_y := 1 / source_to_destination_y;
-
-    { calculate adjusted source area }
-    adjusted_source_left := clipped_source_left + destination_delta_left * destination_to_source_x;
-    adjusted_source_top := clipped_source_top + destination_delta_top * destination_to_source_y;
-    adjusted_source_right := clipped_source_right + destination_delta_right * destination_to_source_x;
-    adjusted_source_bottom := clipped_source_bottom + destination_delta_bottom * destination_to_source_y;
-
-    { assign adjusted source to clipped source }
-    clipped_source_left := adjusted_source_left;
-    clipped_source_top := adjusted_source_top;
-    clipped_source_right := adjusted_source_right;
-    clipped_source_bottom := adjusted_source_bottom;
-
-    { round clipped areas to integer coordinates }
-    TPTCClipper_round(clipped_source_left, clipped_source_top,
-                      clipped_source_right, clipped_source_bottom);
-    TPTCClipper_round(clipped_destination_left, clipped_destination_top,
-                      clipped_destination_right, clipped_destination_bottom);
-
-    { construct clipped area rectangles from rounded floating point areas }
-    tmp1 := TPTCArea.Create(Trunc(clipped_source_left),
-                            Trunc(clipped_source_top),
-                            Trunc(clipped_source_right),
-                            Trunc(clipped_source_bottom));
-    tmp2 := TPTCArea.Create(Trunc(clipped_destination_left),
-                            Trunc(clipped_destination_top),
-                            Trunc(clipped_destination_right),
-                            Trunc(clipped_destination_bottom));
-    AClippedSource.Assign(tmp1);
-    AClippedDestination.Assign(tmp2);
-  finally
-    tmp1.Free;
-    tmp2.Free;
+  { expand source area to floating point }
+  source_left   := ASource.Left;
+  source_top    := ASource.Top;
+  source_right  := ASource.Right;
+  source_bottom := ASource.Bottom;
+
+  { setup clipped source area }
+  clipped_source_left := source_left;
+  clipped_source_top := source_top;
+  clipped_source_right := source_right;
+  clipped_source_bottom := source_bottom;
+
+  { perform clipping on floating point source area }
+  TPTCClipper_clip(clipped_source_left, clipped_source_top, clipped_source_right,
+                   clipped_source_bottom, AClipSource);
+
+  { check for early source area clipping exit }
+  if (clipped_source_left = clipped_source_right) or
+     (clipped_source_top = clipped_source_bottom) then
+  begin
+    { clipped area is zero }
+    AClippedSource := TPTCArea.Create(0, 0, 0, 0);
+    AClippedDestination := AClippedSource;
+    exit;
   end;
+
+  { calculate deltas in source clip }
+  source_delta_left := clipped_source_left - source_left;
+  source_delta_top := clipped_source_top - source_top;
+  source_delta_right := clipped_source_right - source_right;
+  source_delta_bottom := clipped_source_bottom - source_bottom;
+
+  { calculate ratio of source area to destination area }
+  source_to_destination_x := ADestination.Width / ASource.Width;
+  source_to_destination_y := ADestination.Height / ASource.Height;
+
+  { expand destination area to floating point }
+  destination_left   := ADestination.Left;
+  destination_top    := ADestination.Top;
+  destination_right  := ADestination.Right;
+  destination_bottom := ADestination.Bottom;
+
+  { calculate adjusted destination area }
+  adjusted_destination_left := destination_left + source_delta_left * source_to_destination_x;
+  adjusted_destination_top := destination_top + source_delta_top * source_to_destination_y;
+  adjusted_destination_right := destination_right + source_delta_right * source_to_destination_x;
+  adjusted_destination_bottom := destination_bottom + source_delta_bottom * source_to_destination_y;
+
+  { setup clipped destination area }
+  clipped_destination_left := adjusted_destination_left;
+  clipped_destination_top := adjusted_destination_top;
+  clipped_destination_right := adjusted_destination_right;
+  clipped_destination_bottom := adjusted_destination_bottom;
+
+  { perform clipping on floating point destination area }
+  TPTCClipper_clip(clipped_destination_left, clipped_destination_top,
+                   clipped_destination_right, clipped_destination_bottom, AClipDestination);
+
+  { check for early destination area clipping exit }
+  if (clipped_destination_left = clipped_destination_right) or
+     (clipped_destination_top = clipped_destination_bottom) then
+  begin
+    { clipped area is zero }
+    AClippedSource := TPTCArea.Create(0, 0, 0, 0);
+    AClippedDestination := AClippedSource;
+    exit;
+  end;
+
+  { calculate deltas in destination clip }
+  destination_delta_left := clipped_destination_left - adjusted_destination_left;
+  destination_delta_top := clipped_destination_top - adjusted_destination_top;
+  destination_delta_right := clipped_destination_right - adjusted_destination_right;
+  destination_delta_bottom := clipped_destination_bottom - adjusted_destination_bottom;
+
+  { calculate ratio of destination area to source area }
+  destination_to_source_x := 1 / source_to_destination_x;
+  destination_to_source_y := 1 / source_to_destination_y;
+
+  { calculate adjusted source area }
+  adjusted_source_left := clipped_source_left + destination_delta_left * destination_to_source_x;
+  adjusted_source_top := clipped_source_top + destination_delta_top * destination_to_source_y;
+  adjusted_source_right := clipped_source_right + destination_delta_right * destination_to_source_x;
+  adjusted_source_bottom := clipped_source_bottom + destination_delta_bottom * destination_to_source_y;
+
+  { assign adjusted source to clipped source }
+  clipped_source_left := adjusted_source_left;
+  clipped_source_top := adjusted_source_top;
+  clipped_source_right := adjusted_source_right;
+  clipped_source_bottom := adjusted_source_bottom;
+
+  { round clipped areas to integer coordinates }
+  TPTCClipper_round(clipped_source_left, clipped_source_top,
+                    clipped_source_right, clipped_source_bottom);
+  TPTCClipper_round(clipped_destination_left, clipped_destination_top,
+                    clipped_destination_right, clipped_destination_bottom);
+
+  { construct clipped area rectangles from rounded floating point areas }
+  AClippedSource := TPTCArea.Create(Trunc(clipped_source_left),
+                                    Trunc(clipped_source_top),
+                                    Trunc(clipped_source_right),
+                                    Trunc(clipped_source_bottom));
+  AClippedDestination := TPTCArea.Create(Trunc(clipped_destination_left),
+                                         Trunc(clipped_destination_top),
+                                         Trunc(clipped_destination_right),
+                                         Trunc(clipped_destination_bottom));
 end;

+ 25 - 19
packages/ptc/src/core/colord.inc

@@ -31,24 +31,30 @@
 }
 
 type
-  TPTCColor = class
-  private
-    FIndex: Integer;
-    FRed, FGreen, FBlue, FAlpha: Single;
-    FDirect: Boolean;
-    FIndexed: Boolean;
+  IPTCColor = interface
+    function GetIndex: Integer;
+    function GetR: Single;
+    function GetG: Single;
+    function GetB: Single;
+    function GetA: Single;
+    function GetDirect: Boolean;
+    function GetIndexed: Boolean;
+
+    function Equals(AColor: IPTCColor): Boolean;
+
+    property Index: Integer read GetIndex;
+    property R: Single read GetR;
+    property G: Single read GetG;
+    property B: Single read GetB;
+    property A: Single read GetA;
+    property Direct: Boolean read GetDirect;
+    property Indexed: Boolean read GetIndexed;
+  end;
+
+  TPTCColorFactory = class
   public
-    constructor Create;
-    constructor Create(AIndex: Integer);
-    constructor Create(ARed, AGreen, ABlue: Single; AAlpha: Single = 1);
-    constructor Create(const AColor: TPTCColor);
-    procedure Assign(const AColor: TPTCColor);
-    function Equals(const AColor: TPTCColor): Boolean;
-    property Index: Integer read FIndex;
-    property R: Single read FRed;
-    property G: Single read FGreen;
-    property B: Single read FBlue;
-    property A: Single read FAlpha;
-    property Direct: Boolean read FDirect;
-    property Indexed: Boolean read FIndexed;
+    class function CreateNew: IPTCColor;
+    class function CreateNew(AIndex: Integer): IPTCColor;
+    class function CreateNew(ARed, AGreen, ABlue: Single; AAlpha: Single = 1): IPTCColor;
+    class function CreateNew(AColor: IPTCColor): IPTCColor;
   end;

+ 88 - 27
packages/ptc/src/core/colori.inc

@@ -30,10 +30,51 @@
     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 }
 
+type
+  TPTCColor = class(TInterfacedObject, IPTCColor)
+  private
+    FIndex: Integer;
+    FRed, FGreen, FBlue, FAlpha: Single;
+    FIndexed: Boolean;
+
+    function GetIndex: Integer;
+    function GetR: Single;
+    function GetG: Single;
+    function GetB: Single;
+    function GetA: Single;
+    function GetDirect: Boolean;
+    function GetIndexed: Boolean;
+    function Equals(AColor: IPTCColor): Boolean;
+  public
+    constructor Create;
+    constructor Create(AIndex: Integer);
+    constructor Create(ARed, AGreen, ABlue: Single; AAlpha: Single = 1);
+    constructor Create(AColor: IPTCColor);
+  end;
+
+class function TPTCColorFactory.CreateNew: IPTCColor;
+begin
+  Result := TPTCColor.Create;
+end;
+
+class function TPTCColorFactory.CreateNew(AIndex: Integer): IPTCColor;
+begin
+  Result := TPTCColor.Create(AIndex);
+end;
+
+class function TPTCColorFactory.CreateNew(ARed, AGreen, ABlue: Single; AAlpha: Single = 1): IPTCColor;
+begin
+  Result := TPTCColor.Create(ARed, AGreen, ABlue, AAlpha);
+end;
+
+class function TPTCColorFactory.CreateNew(AColor: IPTCColor): IPTCColor;
+begin
+  Result := TPTCColor.Create(AColor);
+end;
+
 constructor TPTCColor.Create;
 begin
   FIndexed := False;
-  FDirect  := False;
   FIndex   := 0;
   FRed     := 0;
   FGreen   := 0;
@@ -44,7 +85,6 @@ end;
 constructor TPTCColor.Create(AIndex: Integer);
 begin
   FIndexed := True;
-  FDirect  := False;
   FIndex   := AIndex;
   FRed     := 0;
   FGreen   := 0;
@@ -55,7 +95,6 @@ end;
 constructor TPTCColor.Create(ARed, AGreen, ABlue: Single; AAlpha: Single = 1);
 begin
   FIndexed := False;
-  FDirect  := True;
   FIndex   := 0;
   FRed     := ARed;
   FGreen   := AGreen;
@@ -63,35 +102,57 @@ begin
   FAlpha   := AAlpha;
 end;
 
-constructor TPTCColor.Create(const AColor: TPTCColor);
+constructor TPTCColor.Create(AColor: IPTCColor);
+begin
+  FIndex   := AColor.Index;
+  FRed     := AColor.R;
+  FGreen   := AColor.G;
+  FBlue    := AColor.B;
+  FAlpha   := AColor.A;
+  FIndexed := AColor.Indexed;
+end;
+
+function TPTCColor.Equals(AColor: IPTCColor): Boolean;
+begin
+  Result := (FIndexed = AColor.Indexed) and
+            (FIndex   = AColor.Index) and
+            (FRed     = AColor.R) and
+            (FGreen   = AColor.G) and
+            (FBlue    = AColor.B) and
+            (FAlpha   = AColor.A);
+end;
+
+function TPTCColor.GetIndex: Integer;
+begin
+  Result := FIndex;
+end;
+
+function TPTCColor.GetR: Single;
+begin
+  Result := FRed;
+end;
+
+function TPTCColor.GetG: Single;
+begin
+  Result := FGreen;
+end;
+
+function TPTCColor.GetB: Single;
+begin
+  Result := FBlue;
+end;
+
+function TPTCColor.GetA: Single;
 begin
-  FIndex   := AColor.FIndex;
-  FRed     := AColor.FRed;
-  FGreen   := AColor.FGreen;
-  FBlue    := AColor.FBlue;
-  FAlpha   := AColor.FAlpha;
-  FDirect  := AColor.FDirect;
-  FIndexed := AColor.FIndexed;
+  Result := FAlpha;
 end;
 
-procedure TPTCColor.Assign(const AColor: TPTCColor);
+function TPTCColor.GetDirect: Boolean;
 begin
-  FIndex   := AColor.FIndex;
-  FRed     := AColor.FRed;
-  FGreen   := AColor.FGreen;
-  FBlue    := AColor.FBlue;
-  FAlpha   := AColor.FAlpha;
-  FDirect  := AColor.FDirect;
-  FIndexed := AColor.FIndexed;
+  Result := not FIndexed;
 end;
 
-function TPTCColor.Equals(const AColor: TPTCColor): Boolean;
+function TPTCColor.GetIndexed: Boolean;
 begin
-  Result := (FIndexed = AColor.FIndexed) and
-            (FDirect  = AColor.FDirect) and
-            (FIndex   = AColor.FIndex) and
-            (FRed     = AColor.FRed) and
-            (FGreen   = AColor.FGreen) and
-            (FBlue    = AColor.FBlue) and
-            (FAlpha   = AColor.FAlpha);
+  Result := FIndexed;
 end;

+ 2 - 74
packages/ptc/src/core/consoled.inc

@@ -31,79 +31,7 @@
 }
 
 type
-  TPTCConsole = class(TPTCBaseConsole)
-  private
-    FConsole: TPTCBaseConsole;
-    FModes: array [0..1023] of TPTCMode;
-    FOptionsQueue: array of string;
-    FHackyOptionConsoleFlag: Boolean;
-
-    function ConsoleCreate(AIndex: Integer): TPTCBaseConsole;
-    function ConsoleCreate(const AName: string): TPTCBaseConsole;
-    procedure Check;
-
-    procedure AddOptionToOptionsQueue(const AOption: string);
-    procedure ExecuteOptionsFromOptionsQueue;
-    procedure ClearOptionsQueue;
+  TPTCConsoleFactory = class
   public
-    constructor Create; override;
-    destructor Destroy; override;
-    procedure Configure(const AFile: string); override;
-    function Option(const AOption: string): Boolean; override;
-    function Modes: PPTCMode; override;
-    procedure Open(const ATitle: string; APages: Integer = 0); overload; override;
-    procedure Open(const ATitle: string; const AFormat: TPTCFormat;
-                   APages: Integer = 0); overload; override;
-    procedure Open(const ATitle: string; AWidth, AHeight: Integer;
-                   const AFormat: TPTCFormat; APages: Integer = 0); overload; override;
-    procedure Open(const ATitle: string; const AMode: TPTCMode;
-                   APages: Integer = 0); overload; override;
-
-    procedure Close; override;
-    procedure Flush; override;
-    procedure Finish; override;
-    procedure Update; override;
-    procedure Update(const AArea: TPTCArea); override;
-    procedure Copy(ASurface: TPTCBaseSurface); override;
-    procedure Copy(ASurface: TPTCBaseSurface;
-                   const ASource, ADestination: TPTCArea); override;
-    function Lock: Pointer; override;
-    procedure Unlock; override;
-    procedure Load(const APixels: Pointer;
-                   AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette); override;
-    procedure Load(const APixels: Pointer;
-                   AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette;
-                   const ASource, ADestination: TPTCArea); override;
-    procedure Save(APixels: Pointer;
-                   AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette); override;
-    procedure Save(APixels: Pointer;
-                   AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette;
-                   const ASource, ADestination: TPTCArea); override;
-    procedure Clear; override;
-    procedure Clear(const AColor: TPTCColor); override;
-    procedure Clear(const AColor: TPTCColor;
-                    const AArea: TPTCArea); override;
-    procedure Palette(const APalette: TPTCPalette); override;
-    function Palette: TPTCPalette; override;
-    procedure Clip(const AArea: TPTCArea); override;
-    function GetWidth: Integer; override;
-    function GetHeight: Integer; override;
-    function GetPitch: Integer; override;
-    function GetPages: Integer; override;
-    function GetArea: TPTCArea; override;
-    function Clip: TPTCArea; override;
-    function GetFormat: TPTCFormat; override;
-    function GetName: string; override;
-    function GetTitle: string; override;
-    function GetInformation: string; override;
-    function NextEvent(var AEvent: TPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean; override;
-    function PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): TPTCEvent; override;
+    class function CreateNew: IPTCConsole;
   end;

+ 140 - 74
packages/ptc/src/core/consolei.inc

@@ -30,6 +30,89 @@
     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 }
 
+type
+  TPTCConsole = class(TPTCBaseConsole)
+  private
+    FConsole: IPTCConsole;
+    FModes: array of IPTCMode;
+    FOptionsQueue: array of string;
+    FHackyOptionConsoleFlag: Boolean;
+
+    function ConsoleCreate(AIndex: Integer): IPTCConsole;
+    function ConsoleCreate(const AName: string): IPTCConsole;
+    procedure Check;
+
+    procedure AddOptionToOptionsQueue(const AOption: string);
+    procedure ExecuteOptionsFromOptionsQueue;
+    procedure ClearOptionsQueue;
+  public
+    constructor Create; override;
+    destructor Destroy; override;
+    procedure Configure(const AFile: string); override;
+    function Option(const AOption: string): Boolean; override;
+    function Modes: TPTCModeList; override;
+    procedure Open(const ATitle: string; APages: Integer = 0); overload; override;
+    procedure Open(const ATitle: string; AFormat: IPTCFormat;
+                   APages: Integer = 0); overload; override;
+    procedure Open(const ATitle: string; AWidth, AHeight: Integer;
+                   AFormat: IPTCFormat; APages: Integer = 0); overload; override;
+    procedure Open(const ATitle: string; AMode: IPTCMode;
+                   APages: Integer = 0); overload; override;
+
+    procedure Close; override;
+    procedure Flush; override;
+    procedure Finish; override;
+    procedure Update; override;
+    procedure Update(AArea: IPTCArea); override;
+    procedure Copy(ASurface: IPTCSurface); override;
+    procedure Copy(ASurface: IPTCSurface;
+                   ASource, ADestination: IPTCArea); override;
+    function Lock: Pointer; override;
+    procedure Unlock; override;
+    procedure Load(const APixels: Pointer;
+                   AWidth, AHeight, APitch: Integer;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette); override;
+    procedure Load(const APixels: Pointer;
+                   AWidth, AHeight, APitch: Integer;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette;
+                   ASource, ADestination: IPTCArea); override;
+    procedure Save(APixels: Pointer;
+                   AWidth, AHeight, APitch: Integer;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette); override;
+    procedure Save(APixels: Pointer;
+                   AWidth, AHeight, APitch: Integer;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette;
+                   ASource, ADestination: IPTCArea); override;
+    procedure Clear; override;
+    procedure Clear(AColor: IPTCColor); override;
+    procedure Clear(AColor: IPTCColor;
+                    AArea: IPTCArea); override;
+    procedure Palette(APalette: IPTCPalette); override;
+    function Palette: IPTCPalette; override;
+    procedure Clip(AArea: IPTCArea); override;
+    function GetWidth: Integer; override;
+    function GetHeight: Integer; override;
+    function GetPitch: Integer; override;
+    function GetPages: Integer; override;
+    function GetArea: IPTCArea; override;
+    function Clip: IPTCArea; override;
+    function GetFormat: IPTCFormat; override;
+    function GetName: string; override;
+    function GetTitle: string; override;
+    function GetInformation: string; override;
+    function NextEvent(out AEvent: IPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean; override;
+    function PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): IPTCEvent; override;
+  end;
+
+class function TPTCConsoleFactory.CreateNew: IPTCConsole;
+begin
+  Result := TPTCConsole.Create;
+end;
+
 const
  {$IFDEF GO32V2}
   ConsoleTypesNumber = 4;
@@ -81,9 +164,6 @@ begin
   inherited Create;
   FConsole := nil;
   FHackyOptionConsoleFlag := False;
-  FillChar(FModes, SizeOf(FModes), 0);
-  for I := Low(FModes) to High(FModes) do
-    FModes[I] := TPTCMode.Create;
 
   {$IFDEF UNIX}
     Configure('/usr/share/ptcpas/ptcpas.conf');
@@ -114,9 +194,7 @@ var
   I: Integer;
 begin
   close;
-  FConsole.Free;
-  for I := Low(FModes) to High(FModes) do
-    FModes[I].Free;
+  FConsole := nil;
   inherited Destroy;
 end;
 
@@ -203,54 +281,42 @@ begin
   end;
 end;
 
-function TPTCConsole.Modes: PPTCMode;
+function TPTCConsole.Modes: TPTCModeList;
 var
-  _console: TPTCBaseConsole;
+  _console: IPTCConsole;
   index, mode: Integer;
   local: Integer;
-  _modes: PPTCMode;
-  tmp: TPTCMode;
+  _modes: TPTCModeList;
 begin
   if Assigned(FConsole) then
     Result := FConsole.Modes
   else
   begin
     _console := nil;
+    SetLength(FModes, 0);
     index := -1;
     mode := 0;
-    try
-      repeat
-        Inc(index);
-        try
-          _console := ConsoleCreate(index);
-        except
-          on TPTCError do begin
-            FreeAndNil(_console);
-            Continue;
-          end;
-        end;
-        if _console = nil then
-          Break;
-        _modes := _console.modes;
-        local := 0;
-        while _modes[local].valid do
-        begin
-          FModes[mode].Assign(_modes[local]);
-          Inc(local);
-          Inc(mode);
+    repeat
+      Inc(index);
+      try
+        _console := ConsoleCreate(index);
+      except
+        on TPTCError do begin
+          _console := nil;
+          continue;
         end;
-        FreeAndNil(_console);
-      until False;
-    finally
-      _console.Free;
-    end;
+      end;
+      if _console = nil then
+        break;
+      _modes := _console.modes;
+      SetLength(FModes, Length(FModes) + Length(_modes));
+      for local := Low(_modes) to High(_modes) do
+      begin
+        FModes[mode] := _modes[local];
+        Inc(mode);
+      end;
+    until False;
     { todo: strip duplicate modes from list? }
-    tmp := TPTCMode.Create;
-    try
-      FModes[mode].Assign(tmp);
-    finally
-      tmp.Free;
-    end;
     Result := FModes;
   end;
 end;
@@ -314,7 +380,7 @@ begin
   end;
 end;
 
-procedure TPTCConsole.Open(const ATitle: string; const AFormat: TPTCFormat;
+procedure TPTCConsole.Open(const ATitle: string; AFormat: IPTCFormat;
                            APages: Integer);
 var
   composite, tmp: TPTCError;
@@ -375,7 +441,7 @@ begin
 end;
 
 procedure TPTCConsole.Open(const ATitle: string; AWidth, AHeight: Integer;
-                           const AFormat: TPTCFormat; APages: Integer);
+                           AFormat: IPTCFormat; APages: Integer);
 var
   composite, tmp: TPTCError;
   index: Integer;
@@ -434,7 +500,7 @@ begin
   end;
 end;
 
-procedure TPTCConsole.Open(const ATitle: string; const AMode: TPTCMode;
+procedure TPTCConsole.Open(const ATitle: string; AMode: IPTCMode;
                            APages: Integer);
 var
   composite, tmp: TPTCError;
@@ -519,20 +585,20 @@ begin
   FConsole.Update;
 end;
 
-procedure TPTCConsole.Update(const AArea: TPTCArea);
+procedure TPTCConsole.Update(AArea: IPTCArea);
 begin
   Check;
   FConsole.Update(AArea);
 end;
 
-procedure TPTCConsole.Copy(ASurface: TPTCBaseSurface);
+procedure TPTCConsole.Copy(ASurface: IPTCSurface);
 begin
   Check;
   FConsole.Copy(ASurface);
 end;
 
-procedure TPTCConsole.Copy(ASurface: TPTCBaseSurface;
-                           const ASource, ADestination: TPTCArea);
+procedure TPTCConsole.Copy(ASurface: IPTCSurface;
+                           ASource, ADestination: IPTCArea);
 begin
   Check;
   FConsole.Copy(ASurface, ASource, ADestination);
@@ -552,8 +618,8 @@ end;
 
 procedure TPTCConsole.Load(const APixels: Pointer;
                            AWidth, AHeight, APitch: Integer;
-                           const AFormat: TPTCFormat;
-                           const APalette: TPTCPalette);
+                           AFormat: IPTCFormat;
+                           APalette: IPTCPalette);
 begin
   Check;
   FConsole.Load(APixels, AWidth, AHeight, APitch, AFormat, APalette);
@@ -561,9 +627,9 @@ end;
 
 procedure TPTCConsole.Load(const APixels: Pointer;
                            AWidth, AHeight, APitch: Integer;
-                           const AFormat: TPTCFormat;
-                           const APalette: TPTCPalette;
-                           const ASource, ADestination: TPTCArea);
+                           AFormat: IPTCFormat;
+                           APalette: IPTCPalette;
+                           ASource, ADestination: IPTCArea);
 begin
   Check;
   FConsole.Load(APixels, AWidth, AHeight, APitch, AFormat, APalette,
@@ -572,8 +638,8 @@ end;
 
 procedure TPTCConsole.Save(Apixels: Pointer;
                            AWidth, AHeight, APitch: Integer;
-                           const AFormat: TPTCFormat;
-                           const APalette: TPTCPalette);
+                           AFormat: IPTCFormat;
+                           APalette: IPTCPalette);
 begin
   Check;
   FConsole.Save(APixels, AWidth, AHeight, APitch, AFormat, APalette);
@@ -581,9 +647,9 @@ end;
 
 procedure TPTCConsole.Save(APixels: Pointer;
                            AWidth, AHeight, APitch: Integer;
-                           const AFormat: TPTCFormat;
-                           const APalette: TPTCPalette;
-                           const ASource, ADestination: TPTCArea);
+                           AFormat: IPTCFormat;
+                           APalette: IPTCPalette;
+                           ASource, ADestination: IPTCArea);
 begin
   Check;
   FConsole.Save(APixels, AWidth, AHeight, APitch, AFormat, APalette,
@@ -593,35 +659,35 @@ end;
 procedure TPTCConsole.Clear;
 begin
   Check;
-  FConsole.clear;
+  FConsole.Clear;
 end;
 
-procedure TPTCConsole.Clear(const AColor: TPTCColor);
+procedure TPTCConsole.Clear(AColor: IPTCColor);
 begin
   Check;
-  FConsole.clear(AColor);
+  FConsole.Clear(AColor);
 end;
 
-procedure TPTCConsole.Clear(const AColor: TPTCColor;
-                           const AArea: TPTCArea);
+procedure TPTCConsole.Clear(AColor: IPTCColor;
+                            AArea: IPTCArea);
 begin
   Check;
-  FConsole.clear(AColor, AArea);
+  FConsole.Clear(AColor, AArea);
 end;
 
-procedure TPTCConsole.Palette(const APalette: TPTCPalette);
+procedure TPTCConsole.Palette(APalette: IPTCPalette);
 begin
   Check;
   FConsole.Palette(APalette);
 end;
 
-function TPTCConsole.Palette: TPTCPalette;
+function TPTCConsole.Palette: IPTCPalette;
 begin
   Check;
   Result := FConsole.Palette;
 end;
 
-procedure TPTCConsole.Clip(const AArea: TPTCArea);
+procedure TPTCConsole.Clip(AArea: IPTCArea);
 begin
   Check;
   FConsole.Clip(AArea);
@@ -651,19 +717,19 @@ begin
   Result := FConsole.GetPages;
 end;
 
-function TPTCConsole.GetArea: TPTCArea;
+function TPTCConsole.GetArea: IPTCArea;
 begin
   Check;
   Result := FConsole.GetArea;
 end;
 
-function TPTCConsole.Clip: TPTCArea;
+function TPTCConsole.Clip: IPTCArea;
 begin
   Check;
   Result := FConsole.Clip;
 end;
 
-function TPTCConsole.GetFormat: TPTCFormat;
+function TPTCConsole.GetFormat: IPTCFormat;
 begin
   Check;
   Result := FConsole.GetFormat;
@@ -701,19 +767,19 @@ begin
   Result := FConsole.GetInformation;
 end;
 
-function TPTCConsole.NextEvent(var AEvent: TPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean;
+function TPTCConsole.NextEvent(out AEvent: IPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean;
 begin
   Check;
   Result := FConsole.NextEvent(AEvent, AWait, AEventMask);
 end;
 
-function TPTCConsole.PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): TPTCEvent;
+function TPTCConsole.PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): IPTCEvent;
 begin
   Check;
   Result := FConsole.PeekEvent(AWait, AEventMask);
 end;
 
-function TPTCConsole.ConsoleCreate(AIndex: Integer): TPTCBaseConsole;
+function TPTCConsole.ConsoleCreate(AIndex: Integer): IPTCConsole;
 begin
   Result := nil;
   if (AIndex >= Low(ConsoleTypes)) and (AIndex <= High(ConsoleTypes)) then
@@ -723,7 +789,7 @@ begin
     Result.KeyReleaseEnabled := KeyReleaseEnabled;
 end;
 
-function TPTCConsole.ConsoleCreate(const AName: string): TPTCBaseConsole;
+function TPTCConsole.ConsoleCreate(const AName: string): IPTCConsole;
 var
   I, J: Integer;
 begin

+ 2 - 2
packages/ptc/src/core/copyd.inc

@@ -40,8 +40,8 @@ type
   public
     constructor Create;
     destructor Destroy; override;
-    procedure Request(const ASource, ADestination: TPTCFormat);
-    procedure Palette(const ASource, ADestination: TPTCPalette);
+    procedure Request(ASource, ADestination: IPTCFormat);
+    procedure Palette(ASource, ADestination: IPTCPalette);
     procedure Copy(const ASourcePixels: Pointer; ASourceX, ASourceY,
                    ASourceWidth, ASourceHeight, ASourcePitch: Integer;
                    ADestinationPixels: Pointer; ADestinationX, ADestinationY,

+ 7 - 7
packages/ptc/src/core/copyi.inc

@@ -47,25 +47,25 @@ begin
   inherited Destroy;
 end;
 
-procedure TPTCCopy.Request(const ASource, ADestination: TPTCFormat);
+procedure TPTCCopy.Request(ASource, ADestination: IPTCFormat);
 var
   hermes_source_format, hermes_destination_format: PHermesFormat;
 begin
-  hermes_source_format := @ASource.FFormat;
-  hermes_destination_format := @ADestination.FFormat;
+  hermes_source_format := ASource.GetHermesFormat;
+  hermes_destination_format := ADestination.GetHermesFormat;
   if not Hermes_ConverterRequest(FHandle, hermes_source_format,
      hermes_destination_format) then
     raise TPTCError.Create('unsupported hermes pixel format conversion');
 end;
 
-procedure TPTCCopy.Palette(const ASource, ADestination: TPTCPalette);
+procedure TPTCCopy.Palette(ASource, ADestination: IPTCPalette);
 begin
-  if not Hermes_ConverterPalette(FHandle, ASource.FHandle,
-         ADestination.FHandle) then
+  if not Hermes_ConverterPalette(FHandle, ASource.GetHermesPaletteHandle,
+         ADestination.GetHermesPaletteHandle) then
     raise TPTCError.Create('could not set hermes conversion palettes');
 end;
 
-procedure TPTCCopy.copy(const ASourcePixels: Pointer; ASourceX, ASourceY,
+procedure TPTCCopy.Copy(const ASourcePixels: Pointer; ASourceX, ASourceY,
                    ASourceWidth, ASourceHeight, ASourcePitch: Integer;
                    ADestinationPixels: Pointer; ADestinationX, ADestinationY,
                    ADestinationWidth, ADestinationHeight, ADestinationPitch: Integer);

+ 4 - 11
packages/ptc/src/core/eventd.inc

@@ -33,18 +33,11 @@
 type
   TPTCEventType = (PTCKeyEvent, PTCMouseEvent{, PTCExposeEvent});
   TPTCEventMask = set of TPTCEventType;
-  TPTCEvent = class
-  protected
-    function GetType: TPTCEventType; virtual; abstract;
-  public
-    property EventType: TPTCEventType read GetType;
+  IPTCEvent = interface
+    ['{1D5A6831-6648-47B6-83D5-10E65FDB72AD}']
+    function GetEventType: TPTCEventType;
+    property EventType: TPTCEventType read GetEventType;
   end;
 
 const
   PTCAnyEvent: TPTCEventMask = [PTCKeyEvent, PTCMouseEvent{, PTCExposeEvent}];
-
-{type
-  TPTCExposeEvent = Class(TPTCEvent)
-  protected
-    function GetType: TPTCEventType; override;
-  End;}

+ 20 - 8
packages/ptc/src/core/eventi.inc

@@ -36,9 +36,20 @@ begin
 end;}
 
 type
+  TPTCEvent = class(TInterfacedObject, IPTCEvent)
+  protected
+    function GetEventType: TPTCEventType; virtual; abstract;
+  public
+    property EventType: TPTCEventType read GetEventType;
+  end;
+{  TPTCExposeEvent = Class(TPTCEvent)
+  protected
+    function GetType: TPTCEventType; override;
+  End;}
+
   PEventLinkedList = ^TEventLinkedList;
   TEventLinkedList = record
-    Event: TPTCEvent;
+    Event: IPTCEvent;
     Next: PEventLinkedList;
   end;
   TEventQueue = class
@@ -47,9 +58,9 @@ type
   public
     constructor Create;
     destructor Destroy; override;
-    procedure AddEvent(event: TPTCEvent);
-    function PeekEvent(const EventMask: TPTCEventMask): TPTCEvent;
-    function NextEvent(const EventMask: TPTCEventMask): TPTCEvent;
+    procedure AddEvent(const event: IPTCEvent);
+    function PeekEvent(const EventMask: TPTCEventMask): IPTCEvent;
+    function NextEvent(const EventMask: TPTCEventMask): IPTCEvent;
   end;
 
 constructor TEventQueue.Create;
@@ -65,7 +76,7 @@ begin
   p := FHead;
   while p <> nil do
   begin
-    FreeAndNil(p^.Event);
+    p^.Event := nil;
     pnext := p^.Next;
     Dispose(p);
     p := pnext;
@@ -73,7 +84,7 @@ begin
   inherited Destroy;
 end;
 
-procedure TEventQueue.AddEvent(event: TPTCEvent);
+procedure TEventQueue.AddEvent(const event: IPTCEvent);
 var
   tmp: PEventLinkedList;
 begin
@@ -94,7 +105,7 @@ begin
   end;
 end;
 
-function TEventQueue.PeekEvent(const EventMask: TPTCEventMask): TPTCEvent;
+function TEventQueue.PeekEvent(const EventMask: TPTCEventMask): IPTCEvent;
 var
   p: PEventLinkedList;
 begin
@@ -112,7 +123,7 @@ begin
   Result := nil;
 end;
 
-function TEventQueue.NextEvent(const EventMask: TPTCEventMask): TPTCEvent;
+function TEventQueue.NextEvent(const EventMask: TPTCEventMask): IPTCEvent;
 var
   prev, p: PEventLinkedList;
 begin
@@ -123,6 +134,7 @@ begin
     if p^.Event.EventType In EventMask then
     begin
       Result := p^.Event;
+      p^.Event := nil;
 
       { delete the element from the linked list }
       if prev <> nil then

+ 28 - 19
packages/ptc/src/core/formatd.inc

@@ -31,27 +31,36 @@
 }
 
 type
-  TPTCFormat = class
-  private
-    FFormat: THermesFormat;
+  IPTCFormat = interface
+    function GetHermesFormat: PHermesFormat;
+
+    function Equals(AFormat: IPTCFormat): Boolean;
+
+    function GetR: Uint32;
+    function GetG: Uint32;
+    function GetB: Uint32;
+    function GetA: Uint32;
+    function GetBits: Integer;
+    function GetIndexed: Boolean;
     function GetDirect: Boolean;
     function GetBytes: Integer;
-  public
-    constructor Create;
-    constructor Create(ABits: Integer);
-    constructor Create(ABits: Integer;
-                       ARedMask, AGreenMask, ABlueMask: Uint32;
-                       AAlphaMask: Uint32 = 0);
-    constructor Create(const format: TPTCFormat);
-    destructor Destroy; override;
-    procedure Assign(const format: TPTCFormat);
-    function Equals(const format: TPTCFormat): Boolean;
-    property R: Uint32 read FFormat.r;
-    property G: Uint32 read FFormat.g;
-    property B: Uint32 read FFormat.b;
-    property A: Uint32 read FFormat.a;
-    property Bits: Integer read FFormat.bits;
-    property Indexed: Boolean read FFormat.indexed;
+
+    property R: Uint32 read GetR;
+    property G: Uint32 read GetG;
+    property B: Uint32 read GetB;
+    property A: Uint32 read GetA;
+    property Bits: Integer read GetBits;
+    property Indexed: Boolean read GetIndexed;
     property Direct: Boolean read GetDirect;
     property Bytes: Integer read GetBytes;
   end;
+
+  TPTCFormatFactory = class
+  public
+    class function CreateNew: IPTCFormat;
+    class function CreateNew(ABits: Integer): IPTCFormat;
+    class function CreateNew(ABits: Integer;
+                             ARedMask, AGreenMask, ABlueMask: Uint32;
+                             AAlphaMask: Uint32 = 0): IPTCFormat;
+    class function CreateNew(AFormat: IPTCFormat): IPTCFormat;
+  end;

+ 95 - 5
packages/ptc/src/core/formati.inc

@@ -30,6 +30,61 @@
     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 }
 
+type
+  TPTCFormat = class(TInterfacedObject, IPTCFormat)
+  private
+    FFormat: THermesFormat;
+    function GetHermesFormat: PHermesFormat;
+    function Equals(AFormat: IPTCFormat): Boolean;
+    function GetR: Uint32;
+    function GetG: Uint32;
+    function GetB: Uint32;
+    function GetA: Uint32;
+    function GetBits: Integer;
+    function GetIndexed: Boolean;
+    function GetDirect: Boolean;
+    function GetBytes: Integer;
+  public
+    constructor Create;
+    constructor Create(ABits: Integer);
+    constructor Create(ABits: Integer;
+                       ARedMask, AGreenMask, ABlueMask: Uint32;
+                       AAlphaMask: Uint32 = 0);
+    constructor Create(AFormat: IPTCFormat);
+    destructor Destroy; override;
+//    procedure Assign(const format: TPTCFormat);
+{    property R: Uint32 read GetR;
+    property G: Uint32 read GetG;
+    property B: Uint32 read GetB;
+    property A: Uint32 read GetA;
+    property Bits: Integer read GetBits;
+    property Indexed: Boolean read GetIndexed;
+    property Direct: Boolean read GetDirect;
+    property Bytes: Integer read GetBytes;}
+  end;
+
+class function TPTCFormatFactory.CreateNew: IPTCFormat;
+begin
+  Result := TPTCFormat.Create;
+end;
+
+class function TPTCFormatFactory.CreateNew(ABits: Integer): IPTCFormat;
+begin
+  Result := TPTCFormat.Create(ABits);
+end;
+
+class function TPTCFormatFactory.CreateNew(ABits: Integer;
+                                           ARedMask, AGreenMask, ABlueMask: Uint32;
+                                           AAlphaMask: Uint32 = 0): IPTCFormat;
+begin
+  Result := TPTCFormat.Create(ABits, ARedMask, AGreenMask, ABlueMask, AAlphaMask);
+end;
+
+class function TPTCFormatFactory.CreateNew(AFormat: IPTCFormat): IPTCFormat;
+begin
+  Result := TPTCFormat.Create(AFormat);
+end;
+
 constructor TPTCFormat.Create;
 begin
   { defaults }
@@ -85,13 +140,13 @@ begin
     raise TPTCError.Create('could not initialize hermes');
 end;
 
-constructor TPTCFormat.Create(const format: TPTCFormat);
+constructor TPTCFormat.Create(AFormat: IPTCFormat);
 begin
   { initialize hermes }
   if not Hermes_Init then
     raise TPTCError.Create('could not initialize hermes');
 
-  Hermes_FormatCopy(@format.FFormat, @FFormat)
+  Hermes_FormatCopy(AFormat.GetHermesFormat, @FFormat)
 end;
 
 {$INFO TODO: check what happens if Hermes_Init blows up in the constructor...}
@@ -102,16 +157,51 @@ begin
   inherited Destroy;
 end;
 
-procedure TPTCFormat.Assign(const format: TPTCFormat);
+function TPTCFormat.GetHermesFormat: PHermesFormat;
+begin
+  Result := @Fformat;
+end;
+
+{procedure TPTCFormat.Assign(const format: TPTCFormat);
 begin
   if Self = format then
     exit;
   Hermes_FormatCopy(@format.Fformat, @Fformat);
+end;}
+
+function TPTCFormat.Equals(AFormat: IPTCFormat): Boolean;
+begin
+  Result := Hermes_FormatEquals(AFormat.GetHermesFormat, @FFormat);
+end;
+
+function TPTCFormat.GetR: Uint32;
+begin
+  Result := FFormat.r;
+end;
+
+function TPTCFormat.GetG: Uint32;
+begin
+  Result := FFormat.g;
+end;
+
+function TPTCFormat.GetB: Uint32;
+begin
+  Result := FFormat.b;
+end;
+
+function TPTCFormat.GetA: Uint32;
+begin
+  Result := FFormat.a;
+end;
+
+function TPTCFormat.GetBits: Integer;
+begin
+  Result := FFormat.bits;
 end;
 
-function TPTCFormat.Equals(const format: TPTCFormat): Boolean;
+function TPTCFormat.GetIndexed: Boolean;
 begin
-  Result := Hermes_FormatEquals(@format.FFormat, @FFormat);
+  Result := FFormat.indexed;
 end;
 
 function TPTCFormat.GetDirect: Boolean;

+ 32 - 31
packages/ptc/src/core/keyeventd.inc

@@ -31,41 +31,42 @@
 }
 
 type
-  TPTCKeyEvent = class(TPTCEvent)
-  private
-    FCode: Integer;
-    FUnicode: Integer;
-    FAlt: Boolean;
-    FShift: Boolean;
-    FControl: Boolean;
-    FPress: Boolean;
-
+  IPTCKeyEvent = interface(IPTCEvent)
+    ['{9BD1CD41-1DF6-4392-99DC-885EADB6D85A}']
+    function GetCode: Integer;
+    function GetUnicode: Integer;
+    function GetAlt: Boolean;
+    function GetShift: Boolean;
+    function GetControl: Boolean;
+    function GetPress: Boolean;
     function GetRelease: Boolean;
-  protected
-    function GetType: TPTCEventType; override;
-  public
-    constructor Create;
-    constructor Create(ACode: Integer);
-    constructor Create(ACode, AUnicode: Integer);
-    constructor Create(ACode, AUnicode: Integer; APress: Boolean);
-    constructor Create(ACode: Integer; AAlt, AShift, AControl: Boolean);
-    constructor Create(ACode: Integer; AAlt, AShift, AControl, APress: Boolean);
-    constructor Create(ACode, AUnicode: Integer;
-                       AAlt, AShift, AControl: Boolean);
-    constructor Create(ACode, AUnicode: Integer;
-                       AAlt, AShift, AControl, APress: Boolean);
-    constructor Create(const AKey: TPTCKeyEvent);
-    procedure Assign(const AKey: TPTCKeyEvent);
-    function Equals(const AKey: TPTCKeyEvent): Boolean;
-    property Code: Integer read FCode;
-    property Unicode: Integer read FUnicode;
-    property Alt: Boolean read FAlt;
-    property Shift: Boolean read FShift;
-    property Control: Boolean read FControl;
-    property Press: Boolean read FPress;
+
+//    function Equals(AKey: IPTCKeyEvent): Boolean;
+
+    property Code: Integer read GetCode;
+    property Unicode: Integer read GetUnicode;
+    property Alt: Boolean read GetAlt;
+    property Shift: Boolean read GetShift;
+    property Control: Boolean read GetControl;
+    property Press: Boolean read GetPress;
     property Release: Boolean read GetRelease;
   end;
 
+  TPTCKeyEventFactory = class
+  public
+    class function CreateNew: IPTCKeyEvent;
+    class function CreateNew(ACode: Integer): IPTCKeyEvent;
+    class function CreateNew(ACode, AUnicode: Integer): IPTCKeyEvent;
+    class function CreateNew(ACode, AUnicode: Integer; APress: Boolean): IPTCKeyEvent;
+    class function CreateNew(ACode: Integer; AAlt, AShift, AControl: Boolean): IPTCKeyEvent;
+    class function CreateNew(ACode: Integer; AAlt, AShift, AControl, APress: Boolean): IPTCKeyEvent;
+    class function CreateNew(ACode, AUnicode: Integer;
+                             AAlt, AShift, AControl: Boolean): IPTCKeyEvent;
+    class function CreateNew(ACode, AUnicode: Integer;
+                             AAlt, AShift, AControl, APress: Boolean): IPTCKeyEvent;
+    class function CreateNew(AKey: IPTCKeyEvent): IPTCKeyEvent;
+  end;
+
 const
   PTCKEY_UNDEFINED    = $00;
   PTCKEY_CANCEL       = $03;

+ 121 - 3
packages/ptc/src/core/keyeventi.inc

@@ -29,8 +29,96 @@
     License along with this library; if not, write to the Free Software
     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 }
+type
+  TPTCKeyEvent = class(TPTCEvent, IPTCKeyEvent)
+  private
+    FCode: Integer;
+    FUnicode: Integer;
+    FAlt: Boolean;
+    FShift: Boolean;
+    FControl: Boolean;
+    FPress: Boolean;
 
-function TPTCKeyEvent.GetType: TPTCEventType;
+    function GetCode: Integer;
+    function GetUnicode: Integer;
+    function GetAlt: Boolean;
+    function GetShift: Boolean;
+    function GetControl: Boolean;
+    function GetPress: Boolean;
+    function GetRelease: Boolean;
+  protected
+    function GetEventType: TPTCEventType; override;
+  public
+    constructor Create;
+    constructor Create(ACode: Integer);
+    constructor Create(ACode, AUnicode: Integer);
+    constructor Create(ACode, AUnicode: Integer; APress: Boolean);
+    constructor Create(ACode: Integer; AAlt, AShift, AControl: Boolean);
+    constructor Create(ACode: Integer; AAlt, AShift, AControl, APress: Boolean);
+    constructor Create(ACode, AUnicode: Integer;
+                       AAlt, AShift, AControl: Boolean);
+    constructor Create(ACode, AUnicode: Integer;
+                       AAlt, AShift, AControl, APress: Boolean);
+    constructor Create(AKey: IPTCKeyEvent);
+{    procedure Assign(const AKey: TPTCKeyEvent);
+    function Equals(const AKey: TPTCKeyEvent): Boolean;
+    property Code: Integer read GetCode;
+    property Unicode: Integer read GetUnicode;
+    property Alt: Boolean read GetAlt;
+    property Shift: Boolean read GetShift;
+    property Control: Boolean read GetControl;
+    property Press: Boolean read GetPress;
+    property Release: Boolean read GetRelease;}
+  end;
+
+class function TPTCKeyEventFactory.CreateNew: IPTCKeyEvent;
+begin
+  Result := TPTCKeyEvent.Create;
+end;
+
+class function TPTCKeyEventFactory.CreateNew(ACode: Integer): IPTCKeyEvent;
+begin
+  Result := TPTCKeyEvent.Create(ACode);
+end;
+
+class function TPTCKeyEventFactory.CreateNew(ACode, AUnicode: Integer): IPTCKeyEvent;
+begin
+  Result := TPTCKeyEvent.Create(ACode, AUnicode);
+end;
+
+class function TPTCKeyEventFactory.CreateNew(ACode, AUnicode: Integer; APress: Boolean): IPTCKeyEvent;
+begin
+  Result := TPTCKeyEvent.Create(ACode, AUnicode, APress);
+end;
+
+class function TPTCKeyEventFactory.CreateNew(ACode: Integer; AAlt, AShift, AControl: Boolean): IPTCKeyEvent;
+begin
+  Result := TPTCKeyEvent.Create(ACode, AAlt, AShift, AControl);
+end;
+
+class function TPTCKeyEventFactory.CreateNew(ACode: Integer; AAlt, AShift, AControl, APress: Boolean): IPTCKeyEvent;
+begin
+  Result := TPTCKeyEvent.Create(ACode, AAlt, AShift, AControl, APress);
+end;
+
+class function TPTCKeyEventFactory.CreateNew(ACode, AUnicode: Integer;
+                                             AAlt, AShift, AControl: Boolean): IPTCKeyEvent;
+begin
+  Result := TPTCKeyEvent.Create(ACode, AUnicode, AAlt, AShift, AControl);
+end;
+
+class function TPTCKeyEventFactory.CreateNew(ACode, AUnicode: Integer;
+                                             AAlt, AShift, AControl, APress: Boolean): IPTCKeyEvent;
+begin
+  Result := TPTCKeyEvent.Create(ACode, AUnicode, AAlt, AShift, AControl, APress);
+end;
+
+class function TPTCKeyEventFactory.CreateNew(AKey: IPTCKeyEvent): IPTCKeyEvent;
+begin
+  Result := TPTCKeyEvent.Create(AKey);
+end;
+
+function TPTCKeyEvent.GetEventType: TPTCEventType;
 begin
   Result := PTCKeyEvent;
 end;
@@ -116,7 +204,7 @@ begin
   FPress   := APress;
 end;
 
-constructor TPTCKeyEvent.Create(const AKey: TPTCKeyEvent);
+constructor TPTCKeyEvent.Create(AKey: IPTCKeyEvent);
 begin
   FCode    := AKey.Code;
   FUnicode := AKey.Unicode;
@@ -126,7 +214,7 @@ begin
   FPress   := AKey.Press;
 end;
 
-procedure TPTCKeyEvent.Assign(const AKey: TPTCKeyEvent);
+{procedure TPTCKeyEvent.Assign(const AKey: TPTCKeyEvent);
 begin
   FCode    := AKey.Code;
   FUnicode := AKey.Unicode;
@@ -144,6 +232,36 @@ begin
             (FShift   = AKey.FShift) and
             (FControl = AKey.FControl) and
             (FPress   = AKey.FPress);
+end;}
+
+function TPTCKeyEvent.GetCode: Integer;
+begin
+  Result := FCode;
+end;
+
+function TPTCKeyEvent.GetUnicode: Integer;
+begin
+  Result := FUnicode;
+end;
+
+function TPTCKeyEvent.GetAlt: Boolean;
+begin
+  Result := FAlt;
+end;
+
+function TPTCKeyEvent.GetShift: Boolean;
+begin
+  Result := FShift;
+end;
+
+function TPTCKeyEvent.GetControl: Boolean;
+begin
+  Result := FControl;
+end;
+
+function TPTCKeyEvent.GetPress: Boolean;
+begin
+  Result := FPress;
 end;
 
 function TPTCKeyEvent.GetRelease: Boolean;

+ 1 - 1
packages/ptc/src/core/log.inc

@@ -174,7 +174,7 @@ begin
   LOG_close;
 end;
 
-procedure LOG(const message: string; data: TPTCFormat);
+procedure LOG(const message: string; data: IPTCFormat);
 begin
   if not LOG_enabled then
     exit;

+ 21 - 17
packages/ptc/src/core/moded.inc

@@ -31,22 +31,26 @@
 }
 
 type
-  PPTCMode=^TPTCMode;
-  TPTCMode = class
-  private
-    FValid: Boolean;
-    FWidth: Integer;
-    FHeight: Integer;
-    FFormat: TPTCFormat;
+  IPTCMode = interface
+    function GetValid: Boolean;
+    function GetWidth: Integer;
+    function GetHeight: Integer;
+    function GetFormat: IPTCFormat;
+
+    function Equals(AMode: IPTCMode): Boolean;
+
+    property Valid: Boolean read GetValid;
+    property Width: Integer read GetWidth;
+    property Height: Integer read GetHeight;
+    property Format: IPTCFormat read GetFormat;
+  end;
+
+  TPTCModeList = array of IPTCMode;
+
+  TPTCModeFactory = class
   public
-    constructor Create;
-    constructor Create(AWidth, AHeight: Integer; const AFormat: TPTCFormat);
-    constructor Create(const mode: TPTCMode);
-    destructor Destroy; override;
-    procedure Assign(const mode: TPTCMode);
-    function Equals(const mode: TPTCMode): Boolean;
-    property Valid: Boolean read FValid;
-    property Width: Integer read FWidth;
-    property Height: Integer read FHeight;
-    property Format: TPTCFormat read FFormat;
+    class function CreateNew: IPTCMode;
+    class function CreateNew(AWidth, AHeight: Integer; AFormat: IPTCFormat): IPTCMode;
+    class function CreateNew(AMode: IPTCMode): IPTCMode;
   end;
+

+ 67 - 18
packages/ptc/src/core/modei.inc

@@ -31,7 +31,42 @@
 }
 
 type
-  TPTCModeDynArray = array of TPTCMode;
+  TPTCMode = class(TInterfacedObject, IPTCMode)
+  private
+    FValid: Boolean;
+    FWidth: Integer;
+    FHeight: Integer;
+    FFormat: IPTCFormat;
+    function GetValid: Boolean;
+    function GetWidth: Integer;
+    function GetHeight: Integer;
+    function GetFormat: IPTCFormat;
+  public
+    constructor Create;
+    constructor Create(AWidth, AHeight: Integer; AFormat: IPTCFormat);
+    constructor Create(AMode: IPTCMode);
+{    procedure Assign(const mode: TPTCMode);}
+    function Equals(AMode: IPTCMode): Boolean;
+{    property Valid: Boolean read GetValid;
+    property Width: Integer read GetWidth;
+    property Height: Integer read GetHeight;
+    property Format: IPTCFormat read GetFormat;}
+  end;
+
+class function TPTCModeFactory.CreateNew: IPTCMode;
+begin
+  Result := TPTCMode.Create;
+end;
+
+class function TPTCModeFactory.CreateNew(AWidth, AHeight: Integer; AFormat: IPTCFormat): IPTCMode;
+begin
+  Result := TPTCMode.Create(AWidth, AHeight, AFormat);
+end;
+
+class function TPTCModeFactory.CreateNew(AMode: IPTCMode): IPTCMode;
+begin
+  Result := TPTCMode.Create(AMode);
+end;
 
 constructor TPTCMode.Create;
 begin
@@ -41,40 +76,54 @@ begin
   FValid := False;
 end;
 
-constructor TPTCMode.Create(AWidth, AHeight: Integer; const AFormat: TPTCFormat);
+constructor TPTCMode.Create(AWidth, AHeight: Integer; AFormat: IPTCFormat);
 begin
-  FFormat := TPTCFormat.Create(AFormat);
+  FFormat := AFormat;
   FWidth := AWidth;
   FHeight := AHeight;
   FValid := True;
 end;
 
-constructor TPTCMode.Create(const mode: TPTCMode);
+constructor TPTCMode.Create(AMode: IPTCMode);
 begin
-  FFormat := TPTCFormat.Create(mode.FFormat);
+  FFormat := AMode.Format;
+  FWidth := AMode.Width;
+  FHeight := AMode.Height;
+  FValid := AMode.Valid;
+end;
+
+{procedure TPTCMode.Assign(const mode: TPTCMode);
+begin
+  FFormat := mode.FFormat;
   FWidth := mode.FWidth;
   FHeight := mode.FHeight;
   FValid := mode.FValid;
 end;
+}
+function TPTCMode.Equals(AMode: IPTCMode): Boolean;
+begin
+  Result := (FValid = AMode.Valid) and
+            (FWidth = AMode.Width) and
+            (FHeight = AMode.Height) and
+             FFormat.Equals(AMode.Format);
+end;
 
-destructor TPTCMode.Destroy;
+function TPTCMode.GetValid: Boolean;
 begin
-  FFormat.Free;
-  inherited Destroy;
+  Result := FValid;
 end;
 
-procedure TPTCMode.Assign(const mode: TPTCMode);
+function TPTCMode.GetWidth: Integer;
 begin
-  FFormat.Assign(mode.FFormat);
-  FWidth := mode.FWidth;
-  FHeight := mode.FHeight;
-  FValid := mode.FValid;
+  Result := FWidth;
+end;
+
+function TPTCMode.GetHeight: Integer;
+begin
+  Result := FHeight;
 end;
 
-function TPTCMode.Equals(const mode: TPTCMode): Boolean;
+function TPTCMode.GetFormat: IPTCFormat;
 begin
-  Result := (FValid = mode.FValid) and
-            (FWidth = mode.FWidth) and
-            (FHeight = mode.FHeight) and
-             FFormat.Equals(mode.FFormat);
+  Result := FFormat;
 end;

+ 30 - 23
packages/ptc/src/core/mouseeventd.inc

@@ -39,30 +39,37 @@ type
                      PTCMouseButton3, { middle mouse button }
                      PTCMouseButton4,
                      PTCMouseButton5);
-  TPTCMouseButtonState = Set of TPTCMouseButton;
-  TPTCMouseEvent = Class(TPTCEvent)
-  private
-    FX, FY: Integer;
-    FDeltaX, FDeltaY: Integer;
-    FButtonState: TPTCMouseButtonState;
-  protected
-    function GetType: TPTCEventType; override;
-  public
-    constructor Create(AX, AY, ADeltaX, ADeltaY: Integer; AButtonState: TPTCMouseButtonState);
-    property X: Integer read FX;
-    property Y: Integer read FY;
-    property DeltaX: Integer read FDeltaX;
-    property DeltaY: Integer read FDeltaY;
-    property ButtonState: TPTCMouseButtonState read FButtonState;
+  TPTCMouseButtonState = set of TPTCMouseButton;
+  IPTCMouseEvent = interface(IPTCEvent)
+    ['{4D093608-6F27-4578-B41E-3492A4C7FEED}']
+    function GetX: Integer;
+    function GetY: Integer;
+    function GetDeltaX: Integer;
+    function GetDeltaY: Integer;
+    function GetButtonState: TPTCMouseButtonState;
+
+    property X: Integer read GetX;
+    property Y: Integer read GetY;
+    property DeltaX: Integer read GetDeltaX;
+    property DeltaY: Integer read GetDeltaY;
+    property ButtonState: TPTCMouseButtonState read GetButtonState;
   end;
-  TPTCMouseButtonEvent = Class(TPTCMouseEvent)
-  private
-    FPress: Boolean;
-    FButton: TPTCMouseButton;
+  IPTCMouseButtonEvent = interface(IPTCMouseEvent)
+    ['{363B9ACC-4DEB-4031-8BD9-0B6788C6CFA7}']
+    function GetPress: Boolean;
     function GetRelease: Boolean;
-  public
-    constructor Create(AX, AY, ADeltaX, ADeltaY: Integer; AButtonState: TPTCMouseButtonState; APress: Boolean; AButton: TPTCMouseButton);
-    property Press: Boolean read FPress;
+    function GetButton: TPTCMouseButton;
+
+    property Press: Boolean read GetPress;
     property Release: Boolean read GetRelease;
-    property Button: TPTCMouseButton read FButton;
+    property Button: TPTCMouseButton read GetButton;
+  end;
+
+  TPTCMouseEventFactory = class
+  public
+    class function CreateNew(AX, AY, ADeltaX, ADeltaY: Integer; AButtonState: TPTCMouseButtonState): IPTCMouseEvent;
+  end;
+  TPTCMouseButtonEventFactory = class
+  public
+    class function CreateNew(AX, AY, ADeltaX, ADeltaY: Integer; AButtonState: TPTCMouseButtonState; APress: Boolean; AButton: TPTCMouseButton): IPTCMouseButtonEvent;
   end;

+ 81 - 1
packages/ptc/src/core/mouseeventi.inc

@@ -30,7 +30,52 @@
     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 }
 
-function TPTCMouseEvent.GetType: TPTCEventType;
+type
+  TPTCMouseEvent = class(TPTCEvent, IPTCMouseEvent)
+  private
+    FX, FY: Integer;
+    FDeltaX, FDeltaY: Integer;
+    FButtonState: TPTCMouseButtonState;
+    function GetX: Integer;
+    function GetY: Integer;
+    function GetDeltaX: Integer;
+    function GetDeltaY: Integer;
+    function GetButtonState: TPTCMouseButtonState;
+  protected
+    function GetEventType: TPTCEventType; override;
+  public
+    constructor Create(AX, AY, ADeltaX, ADeltaY: Integer; AButtonState: TPTCMouseButtonState);
+{    property X: Integer read GetX;
+    property Y: Integer read GetY;
+    property DeltaX: Integer read GetDeltaX;
+    property DeltaY: Integer read GetDeltaY;
+    property ButtonState: TPTCMouseButtonState read GetButtonState;}
+  end;
+  TPTCMouseButtonEvent = class(TPTCMouseEvent, IPTCMouseButtonEvent)
+  private
+    FPress: Boolean;
+    FButton: TPTCMouseButton;
+    function GetPress: Boolean;
+    function GetRelease: Boolean;
+    function GetButton: TPTCMouseButton;
+  public
+    constructor Create(AX, AY, ADeltaX, ADeltaY: Integer; AButtonState: TPTCMouseButtonState; APress: Boolean; AButton: TPTCMouseButton);
+{    property Press: Boolean read GetPress;
+    property Release: Boolean read GetRelease;
+    property Button: TPTCMouseButton read GetButton;}
+  end;
+
+class function TPTCMouseEventFactory.CreateNew(AX, AY, ADeltaX, ADeltaY: Integer; AButtonState: TPTCMouseButtonState): IPTCMouseEvent;
+begin
+  Result := TPTCMouseEvent.Create(AX, AY, ADeltaX, ADeltaY, AButtonState);
+end;
+
+class function TPTCMouseButtonEventFactory.CreateNew(AX, AY, ADeltaX, ADeltaY: Integer; AButtonState: TPTCMouseButtonState; APress: Boolean; AButton: TPTCMouseButton): IPTCMouseButtonEvent;
+begin
+  Result := TPTCMouseButtonEvent.Create(AX, AY, ADeltaX, ADeltaY, AButtonState, APress, AButton);
+end;
+
+function TPTCMouseEvent.GetEventType: TPTCEventType;
 begin
   Result := PTCMouseEvent;
 end;
@@ -44,6 +89,31 @@ begin
   FButtonState := AButtonState;
 end;
 
+function TPTCMouseEvent.GetX: Integer;
+begin
+  Result := FX;
+end;
+
+function TPTCMouseEvent.GetY: Integer;
+begin
+  Result := FY;
+end;
+
+function TPTCMouseEvent.GetDeltaX: Integer;
+begin
+  Result := FDeltaX;
+end;
+
+function TPTCMouseEvent.GetDeltaY: Integer;
+begin
+  Result := FDeltaY;
+end;
+
+function TPTCMouseEvent.GetButtonState: TPTCMouseButtonState;
+begin
+  Result := FButtonState;
+end;
+
 constructor TPTCMouseButtonEvent.Create(AX, AY, ADeltaX, ADeltaY: Integer; AButtonState: TPTCMouseButtonState; APress: Boolean; AButton: TPTCMouseButton);
 begin
   if APress xor (AButton In AButtonState) then
@@ -55,7 +125,17 @@ begin
   FButton := AButton;
 end;
 
+function TPTCMouseButtonEvent.GetPress: Boolean;
+begin
+  Result := FPress;
+end;
+
 function TPTCMouseButtonEvent.GetRelease: Boolean;
 begin
   Result := not FPress;
 end;
+
+function TPTCMouseButtonEvent.GetButton: TPTCMouseButton;
+begin
+  Result := FButton;
+end;

+ 9 - 11
packages/ptc/src/core/paletted.inc

@@ -31,17 +31,7 @@
 }
 
 type
-  TPTCPalette = class
-  private
-    FLocked: Boolean;
-    FHandle: THermesPaletteHandle;
-  public
-    constructor Create;
-    constructor Create(const AData: array of Uint32);
-    constructor Create(const APalette: TPTCPalette);
-    destructor Destroy; override;
-    procedure Assign(const APalette: TPTCPalette);
-    function Equals(const APalette: TPTCPalette): Boolean;
+  IPTCPalette = interface
     function Lock: PUint32;
     procedure Unlock;
     procedure Load(const AData: array of Uint32);
@@ -49,4 +39,12 @@ type
     procedure Save(var AData: array of Uint32);
     procedure Save(AData: Pointer);
     function Data: PUint32;
+    function GetHermesPaletteHandle: THermesPaletteHandle;
+  end;
+
+  TPTCPaletteFactory = class
+  public
+    class function CreateNew: IPTCPalette;
+    class function CreateNew(const AData: array of Uint32): IPTCPalette;
+    class function CreateNew(APalette: IPTCPalette): IPTCPalette;
   end;

+ 46 - 4
packages/ptc/src/core/palettei.inc

@@ -30,6 +30,43 @@
     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 }
 
+type
+  TPTCPalette = class(TInterfacedObject, IPTCPalette)
+  private
+    FLocked: Boolean;
+    FHandle: THermesPaletteHandle;
+  public
+    constructor Create;
+    constructor Create(const AData: array of Uint32);
+    constructor Create(APalette: IPTCPalette);
+    destructor Destroy; override;
+//    procedure Assign(const APalette: TPTCPalette);
+//    function Equals(const APalette: TPTCPalette): Boolean;
+    function Lock: PUint32;
+    procedure Unlock;
+    procedure Load(const AData: array of Uint32);
+    procedure Load(AData: Pointer);
+    procedure Save(var AData: array of Uint32);
+    procedure Save(AData: Pointer);
+    function Data: PUint32;
+    function GetHermesPaletteHandle: THermesPaletteHandle;
+  end;
+
+class function TPTCPaletteFactory.CreateNew: IPTCPalette;
+begin
+  Result := TPTCPalette.Create;
+end;
+
+class function TPTCPaletteFactory.CreateNew(const AData: array of Uint32): IPTCPalette;
+begin
+  Result := TPTCPalette.Create(AData);
+end;
+
+class function TPTCPaletteFactory.CreateNew(APalette: IPTCPalette): IPTCPalette;
+begin
+  Result := TPTCPalette.Create(APalette);
+end;
+
 constructor TPTCPalette.Create;
 var
   zero: array [0..255] of Uint32;
@@ -55,7 +92,7 @@ begin
   Load(AData);
 end;
 
-constructor TPTCPalette.Create(const APalette: TPTCPalette);
+constructor TPTCPalette.Create(APalette: IPTCPalette);
 begin
   FLocked := False;
   if not Hermes_Init then
@@ -63,7 +100,7 @@ begin
   FHandle := Hermes_PaletteInstance;
   if FHandle = nil then
     raise TPTCError.Create('could not create hermes palette instance');
-  Assign(APalette);
+  Hermes_PaletteSet(FHandle, Hermes_PaletteGet(APalette.GetHermesPaletteHandle));
 end;
 
 destructor TPTCPalette.Destroy;
@@ -75,7 +112,7 @@ begin
   inherited Destroy;
 end;
 
-procedure TPTCPalette.Assign(const APalette: TPTCPalette);
+{procedure TPTCPalette.Assign(const APalette: TPTCPalette);
 begin
   if Self = APalette then
     exit;
@@ -87,7 +124,7 @@ function TPTCPalette.Equals(const APalette: TPTCPalette): Boolean;
 begin
   Equals := CompareDWord(Hermes_PaletteGet(FHandle)^, Hermes_PaletteGet(APalette.FHandle)^, 1024 div 4) = 0;
 end;
-
+}
 function TPTCPalette.Lock: PUint32;
 begin
   if FLocked then
@@ -127,3 +164,8 @@ function TPTCPalette.Data: PUint32;
 begin
   Result := Hermes_PaletteGet(FHandle);
 end;
+
+function TPTCPalette.GetHermesPaletteHandle: THermesPaletteHandle;
+begin
+  Result := FHandle;
+end;

+ 2 - 53
packages/ptc/src/core/surfaced.inc

@@ -31,58 +31,7 @@
 }
 
 type
-  TPTCSurface = class(TPTCBaseSurface)
-  private
-    {data}
-    FWidth: Integer;
-    FHeight: Integer;
-    FPitch: Integer;
-    FArea: TPTCArea;
-    FClip: TPTCArea;
-    FFormat: TPTCFormat;
-    FLocked: Boolean;
-    FPixels: Pointer;
-    {objects}
-    FCopy: TPTCCopy;
-    FClear: TPTCClear;
-    FPalette: TPTCPalette;
+  TPTCSurfaceFactory = class
   public
-    constructor Create(AWidth, AHeight: Integer; const AFormat: TPTCFormat);
-    destructor Destroy; override;
-    procedure Copy(ASurface: TPTCBaseSurface); override;
-    procedure Copy(ASurface: TPTCBaseSurface;
-                   const ASource, ADestination: TPTCArea); override;
-    function Lock: Pointer; override;
-    procedure Unlock; override;
-    procedure Load(const APixels: Pointer;
-                   AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette); override;
-    procedure Load(const APixels: Pointer;
-                   AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette;
-                   const ASource, ADestination: TPTCArea); override;
-    procedure Save(APixels: Pointer;
-                   AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette); override;
-    procedure Save(APixels: Pointer;
-                   AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette;
-                   const ASource, ADestination: TPTCArea); override;
-    procedure Clear; override;
-    procedure Clear(const AColor: TPTCColor); override;
-    procedure Clear(const AColor: TPTCColor; const AArea: TPTCArea); override;
-    procedure Palette(const APalette: TPTCPalette); override;
-    function Palette: TPTCPalette; override;
-    procedure Clip(const AArea: TPTCArea); override;
-    function GetWidth: Integer; override;
-    function GetHeight: Integer; override;
-    function GetPitch: Integer; override;
-    function GetArea: TPTCArea; override;
-    function Clip: TPTCArea; override;
-    function GetFormat: TPTCFormat; override;
-    function Option(const AOption: string): Boolean; override;
+    class function CreateNew(AWidth, AHeight: Integer; AFormat: IPTCFormat): IPTCSurface;
   end;

+ 129 - 111
packages/ptc/src/core/surfacei.inc

@@ -30,7 +30,75 @@
     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 }
 
-constructor TPTCSurface.Create(AWidth, AHeight: Integer; const AFormat: TPTCFormat);
+type
+  TPTCSurface = class(TInterfacedObject, IPTCSurface)
+  private
+    {data}
+    FWidth: Integer;
+    FHeight: Integer;
+    FPitch: Integer;
+    FArea: IPTCArea;
+    FClip: IPTCArea;
+    FFormat: IPTCFormat;
+    FLocked: Boolean;
+    FPixels: Pointer;
+    {objects}
+    FCopy: TPTCCopy;
+    FClear: TPTCClear;
+    FPalette: IPTCPalette;
+  public
+    constructor Create(AWidth, AHeight: Integer; AFormat: IPTCFormat);
+    destructor Destroy; override;
+    procedure Copy(ASurface: IPTCSurface);
+    procedure Copy(ASurface: IPTCSurface;
+                   ASource, ADestination: IPTCArea);
+    function Lock: Pointer;
+    procedure Unlock;
+    procedure Load(const APixels: Pointer;
+                   AWidth, AHeight, APitch: Integer;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette);
+    procedure Load(const APixels: Pointer;
+                   AWidth, AHeight, APitch: Integer;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette;
+                   ASource, ADestination: IPTCArea);
+    procedure Save(APixels: Pointer;
+                   AWidth, AHeight, APitch: Integer;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette);
+    procedure Save(APixels: Pointer;
+                   AWidth, AHeight, APitch: Integer;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette;
+                   ASource, ADestination: IPTCArea);
+    procedure Clear;
+    procedure Clear(AColor: IPTCColor);
+    procedure Clear(AColor: IPTCColor; AArea: IPTCArea);
+    procedure Palette(APalette: IPTCPalette);
+    function Palette: IPTCPalette;
+    procedure Clip(AArea: IPTCArea);
+    function GetWidth: Integer;
+    function GetHeight: Integer;
+    function GetPitch: Integer;
+    function GetArea: IPTCArea;
+    function Clip: IPTCArea;
+    function GetFormat: IPTCFormat;
+    function Option(const AOption: string): Boolean;
+
+    property Width: Integer read GetWidth;
+    property Height: Integer read GetHeight;
+    property Pitch: Integer read GetPitch;
+    property Area: IPTCArea read GetArea;
+    property Format: IPTCFormat read GetFormat;
+  end;
+
+class function TPTCSurfaceFactory.CreateNew(AWidth, AHeight: Integer; AFormat: IPTCFormat): IPTCSurface;
+begin
+  Result := TPTCSurface.Create(AWidth, AHeight, AFormat);
+end;
+
+constructor TPTCSurface.Create(AWidth, AHeight: Integer; AFormat: IPTCFormat);
 var
   size: Integer;
 begin
@@ -41,7 +109,7 @@ begin
   LOG('format', AFormat);
   FWidth := AWidth;
   FHeight := AHeight;
-  FFormat := TPTCFormat.Create(AFormat);
+  FFormat := AFormat;
   FArea := TPTCArea.Create(0, 0, AWidth, AHeight);
   FClip := TPTCArea.Create(FArea);
   FPitch := AWidth * AFormat.Bytes;
@@ -63,21 +131,17 @@ begin
   end;
   FCopy.Free;
   FClear.Free;
-  FPalette.Free;
-  FClip.Free;
-  FArea.Free;
-  FFormat.Free;
   FreeMem(FPixels);
   inherited Destroy;
 end;
 
-procedure TPTCSurface.Copy(ASurface: TPTCBaseSurface);
+procedure TPTCSurface.Copy(ASurface: IPTCSurface);
 begin
   ASurface.Load(FPixels, FWidth, FHeight, FPitch, FFormat, FPalette);
 end;
 
-procedure TPTCSurface.Copy(ASurface: TPTCBaseSurface;
-                           const ASource, ADestination: TPTCArea);
+procedure TPTCSurface.Copy(ASurface: IPTCSurface;
+                           ASource, ADestination: IPTCArea);
 begin
   ASurface.Load(FPixels, FWidth, FHeight, FPitch, FFormat, FPalette,
                 ASource, ADestination);
@@ -100,10 +164,8 @@ end;
 
 procedure TPTCSurface.Load(const APixels: Pointer;
                            AWidth, AHeight, APitch: Integer;
-                           const AFormat: TPTCFormat;
-                           const APalette: TPTCPalette);
-var
-  Area_: TPTCArea;
+                           AFormat: IPTCFormat;
+                           APalette: IPTCPalette);
 begin
   if FClip.Equals(FArea) then
   begin
@@ -113,51 +175,35 @@ begin
                FWidth, FHeight, FPitch);
   end
   else
-  begin
-    Area_ := TPTCArea.Create(0, 0, AWidth, AHeight);
-    try
-      Load(APixels, AWidth, AHeight, APitch, AFormat, APalette, Area_, FArea);
-    finally
-      Area_.Free;
-    end;
-  end;
+    Load(APixels, AWidth, AHeight, APitch, AFormat, APalette,
+         TPTCArea.Create(0, 0, AWidth, AHeight), FArea);
 end;
 
 procedure TPTCSurface.Load(const APixels: Pointer;
                            AWidth, AHeight, APitch: Integer;
-                           const AFormat: TPTCFormat;
-                           const APalette: TPTCPalette;
-                           const ASource, ADestination: TPTCArea);
+                           AFormat: IPTCFormat;
+                           APalette: IPTCPalette;
+                           ASource, ADestination: IPTCArea);
 var
-  clipped_source: TPTCArea = nil;
-  clipped_destination: TPTCArea = nil;
-  area_: TPTCArea = nil;
+  clipped_source: IPTCArea;
+  clipped_destination: IPTCArea;
 begin
-  try
-    clipped_source := TPTCArea.Create;
-    clipped_destination := TPTCArea.Create;
-    area_ := TPTCArea.Create(0, 0, AWidth, AHeight);
-    TPTCClipper.Clip(ASource, area_, clipped_source, ADestination, FClip,
-                     clipped_destination);
-    FCopy.Request(AFormat, FFormat);
-    FCopy.Palette(APalette, FPalette);
-    FCopy.Copy(APixels, clipped_source.left, clipped_source.top,
-               clipped_source.width, clipped_source.height, APitch,
-               FPixels, clipped_destination.left, clipped_destination.top,
-               clipped_destination.width, clipped_destination.height, FPitch);
-  finally
-    clipped_source.Free;
-    clipped_destination.Free;
-    area_.Free;
-  end;
+  TPTCClipper.Clip(ASource, TPTCArea.Create(0, 0, AWidth, AHeight),
+                   clipped_source,
+                   ADestination, FClip,
+                   clipped_destination);
+  FCopy.Request(AFormat, FFormat);
+  FCopy.Palette(APalette, FPalette);
+  FCopy.Copy(APixels, clipped_source.left, clipped_source.top,
+             clipped_source.width, clipped_source.height, APitch,
+             FPixels, clipped_destination.left, clipped_destination.top,
+             clipped_destination.width, clipped_destination.height, FPitch);
 end;
 
 procedure TPTCSurface.Save(APixels: Pointer;
                            AWidth, AHeight, APitch: Integer;
-                           const AFormat: TPTCFormat;
-                           const APalette: TPTCPalette);
-var
-  area_: TPTCArea;
+                           AFormat: IPTCFormat;
+                           APalette: IPTCPalette);
 begin
   if FClip.Equals(FArea) then
   begin
@@ -167,99 +213,71 @@ begin
                AWidth, AHeight, APitch);
   end
   else
-  begin
-    area_ := TPTCArea.Create(0, 0, width, height);
-    try
-      Save(APixels, AWidth, AHeight, APitch, AFormat, APalette, FArea, area_);
-    finally
-      area_.Free;
-    end;
-  end;
+    Save(APixels, AWidth, AHeight, APitch, AFormat, APalette,
+         FArea, TPTCArea.Create(0, 0, width, height));
 end;
 
 procedure TPTCSurface.Save(APixels: Pointer;
                            AWidth, AHeight, APitch: Integer;
-                           const AFormat: TPTCFormat;
-                           const APalette: TPTCPalette;
-                           const ASource, ADestination: TPTCArea);
+                           AFormat: IPTCFormat;
+                           APalette: IPTCPalette;
+                           ASource, ADestination: IPTCArea);
 var
-  clipped_source: TPTCArea = nil;
-  clipped_destination: TPTCArea = nil;
-  area_: TPTCArea = nil;
+  clipped_source: IPTCArea;
+  clipped_destination: IPTCArea;
 begin
-  try
-    clipped_source := TPTCArea.Create;
-    clipped_destination := TPTCArea.Create;
-    area_ := TPTCArea.Create(0, 0, AWidth, AHeight);
-    TPTCClipper.Clip(ASource, FClip, clipped_source, ADestination, area_,
-                     clipped_destination);
-    FCopy.Request(FFormat, AFormat);
-    FCopy.Palette(FPalette, APalette);
-    FCopy.Copy(FPixels, clipped_source.left, clipped_source.top,
-               clipped_source.width, clipped_source.height, FPitch,
-               APixels, clipped_destination.left, clipped_destination.top,
-               clipped_destination.width, clipped_destination.height, APitch);
-  finally
-    clipped_source.Free;
-    clipped_destination.Free;
-    area_.Free;
-  end;
+  TPTCClipper.Clip(ASource, FClip,
+                   clipped_source,
+                   ADestination, TPTCArea.Create(0, 0, AWidth, AHeight),
+                   clipped_destination);
+  FCopy.Request(FFormat, AFormat);
+  FCopy.Palette(FPalette, APalette);
+  FCopy.Copy(FPixels, clipped_source.left, clipped_source.top,
+             clipped_source.width, clipped_source.height, FPitch,
+             APixels, clipped_destination.left, clipped_destination.top,
+             clipped_destination.width, clipped_destination.height, APitch);
 end;
 
 procedure TPTCSurface.Clear;
 var
-  Color: TPTCColor;
+  Color: IPTCColor;
 begin
   if Format.Direct then
     Color := TPTCColor.Create(0, 0, 0, 0)
   else
     Color := TPTCColor.Create(0);
-  try
-    Clear(Color);
-  finally
-    Color.Free;
-  end;
+
+  Clear(Color);
 end;
 
-procedure TPTCSurface.Clear(const AColor: TPTCColor);
+procedure TPTCSurface.Clear(AColor: IPTCColor);
 begin
   Clear(AColor, FArea);
 end;
 
-procedure TPTCSurface.Clear(const AColor: TPTCColor; const AArea: TPTCArea);
+procedure TPTCSurface.Clear(AColor: IPTCColor; AArea: IPTCArea);
 var
-  clipped_area: TPTCArea;
+  clipped_area: IPTCArea;
 begin
-  clipped_area := TPTCClipper.clip(AArea, FClip);
-  try
-    FClear.Request(FFormat);
-    FClear.Clear(FPixels, clipped_area.left, clipped_area.top,
-                 clipped_area.width, clipped_area.height, FPitch, AColor);
-  finally
-    clipped_area.Free;
-  end;
+  clipped_area := TPTCClipper.Clip(AArea, FClip);
+  FClear.Request(FFormat);
+  FClear.Clear(FPixels, clipped_area.left, clipped_area.top,
+               clipped_area.width, clipped_area.height, FPitch, AColor);
 end;
 
-procedure TPTCSurface.Palette(const APalette: TPTCPalette);
+procedure TPTCSurface.Palette(APalette: IPTCPalette);
 begin
-  FPalette.Load(APalette.data^);
+  FPalette.Load(APalette.Data^);
 end;
 
-function TPTCSurface.Palette: TPTCPalette;
+function TPTCSurface.Palette: IPTCPalette;
 begin
   Result := FPalette;
 end;
 
-procedure TPTCSurface.Clip(const AArea: TPTCArea);
-var
-  tmp: TPTCArea;
+procedure TPTCSurface.Clip(AArea: IPTCArea);
 begin
-  tmp := TPTCClipper.Clip(AArea, FArea);
-  try
-    FClip.Assign(tmp);
-  finally
-    tmp.Free;
-  end;
+  FClip := TPTCClipper.Clip(AArea, FArea);
 end;
 
 function TPTCSurface.GetWidth: Integer;
@@ -277,17 +295,17 @@ begin
   Result := FPitch;
 end;
 
-function TPTCSurface.GetArea: TPTCArea;
+function TPTCSurface.GetArea: IPTCArea;
 begin
   Result := FArea;
 end;
 
-function TPTCSurface.Clip: TPTCArea;
+function TPTCSurface.Clip: IPTCArea;
 begin
   Result := FClip;
 end;
 
-function TPTCSurface.GetFormat: TPTCFormat;
+function TPTCSurface.GetFormat: IPTCFormat;
 begin
   Result := FFormat;
 end;

+ 7 - 19
packages/ptc/src/core/timerd.inc

@@ -31,25 +31,7 @@
 }
 
 type
-  TPTCTimer = class
-  private
-    FOld: Double;
-    FTime: Double;
-    FStart: Double;
-    FCurrent: Double;
-    FRunning: Boolean;
-    {$IF defined(WIN32) OR defined(WIN64)}
-    FFrequency: QWord;
-    {$ENDIF defined(WIN32) OR defined(WIN64)}
-    function Clock: Double;
-    procedure internal_init_timer;
-  public
-    constructor Create;
-    constructor Create(ATime: Double);
-    constructor Create(const ATimer: TPTCTimer);
-    destructor Destroy; override;
-    procedure Assign(const ATimer: TPTCTimer);
-    function Equals(const ATimer: TPTCTimer): Boolean;
+  IPTCTimer = interface
     procedure SetTime(ATime: Double); {was 'set' in the C++ version}
     procedure Start;
     procedure Stop;
@@ -57,3 +39,9 @@ type
     function Delta: Double;
     function Resolution: Double;
   end;
+
+  TPTCTimerFactory = class
+  public
+    class function CreateNew: IPTCTimer;
+    class function CreateNew(ATime: Double): IPTCTimer;
+  end;

+ 48 - 5
packages/ptc/src/core/timeri.inc

@@ -30,6 +30,44 @@
     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 }
 
+type
+  TPTCTimer = class(TInterfacedObject, IPTCTimer)
+  private
+    FOld: Double;
+    FTime: Double;
+    FStart: Double;
+    FCurrent: Double;
+    FRunning: Boolean;
+    {$IF defined(WIN32) OR defined(WIN64)}
+    FFrequency: QWord;
+    {$ENDIF defined(WIN32) OR defined(WIN64)}
+    function Clock: Double;
+    procedure internal_init_timer;
+  public
+    constructor Create;
+    constructor Create(ATime: Double);
+//    constructor Create(ATimer: IPTCTimer);
+    destructor Destroy; override;
+{    procedure Assign(const ATimer: TPTCTimer);
+    function Equals(const ATimer: TPTCTimer): Boolean;}
+    procedure SetTime(ATime: Double); {was 'set' in the C++ version}
+    procedure Start;
+    procedure Stop;
+    function Time: Double;
+    function Delta: Double;
+    function Resolution: Double;
+  end;
+
+class function TPTCTimerFactory.CreateNew: IPTCTimer;
+begin
+  Result := TPTCTimer.Create;
+end;
+
+class function TPTCTimerFactory.CreateNew(ATime: Double): IPTCTimer;
+begin
+  Result := TPTCTimer.Create(ATime);
+end;
+
 {Function timeGetTime: DWord; StdCall; external 'WINMM' name 'timeGetTime';}
 
 constructor TPTCTimer.Create;
@@ -53,19 +91,24 @@ begin
   SetTime(ATime);
 end;
 
-constructor TPTCTimer.Create(const ATimer: TPTCTimer);
+{constructor TPTCTimer.Create(ATimer: IPTCTimer);
 begin
   internal_init_timer;
-  Assign(ATimer);
-end;
 
+  FOld := ATimer.FOld;
+  FTime := ATimer.FTime;
+  FStart := ATimer.FStart;
+  FCurrent := ATimer.FCurrent;
+  FRunning := ATimer.FRunning;
+end;
+}
 destructor TPTCTimer.Destroy;
 begin
   Stop;
   inherited Destroy;
 end;
 
-procedure TPTCTimer.Assign(const ATimer: TPTCTimer);
+{procedure TPTCTimer.Assign(const ATimer: TPTCTimer);
 begin
   if Self = ATimer then
     exit;
@@ -83,7 +126,7 @@ begin
             (FStart = ATimer.FStart) and (FCurrent = ATimer.FCurrent) and
             (FRunning = ATimer.FRunning);
 end;
-
+}
 procedure TPTCTimer.SetTime(ATime: Double);
 begin
   FCurrent := ATime;

+ 36 - 36
packages/ptc/src/dos/cga/cgaconsoled.inc

@@ -30,10 +30,10 @@
 }
 
 type
-  TCGAConsole = Class(TPTCBaseConsole)
+  TCGAConsole = class(TPTCBaseConsole)
   private
     { data }
-    m_modes: array [0..255] of TPTCMode;
+    m_modes: array of IPTCMode;
     m_title: string;
     m_information: string;
 
@@ -44,7 +44,7 @@ type
     { option data }
     m_default_width: Integer;
     m_default_height: Integer;
-    m_default_format: TPTCFormat;
+    m_default_format: IPTCFormat;
 
     { objects }
     m_copy: TPTCCopy;
@@ -58,9 +58,9 @@ type
     m_primary: TPTCSurface;
 
     { internal console management routines }
-    procedure internal_pre_open_setup(const _title: String);
+    procedure internal_pre_open_setup(const _title: string);
     procedure internal_open_fullscreen_start;
-    procedure internal_open_fullscreen(_width, _height: Integer; const _format: TPTCFormat);
+    procedure internal_open_fullscreen(_width, _height: Integer; const _format: IPTCFormat);
     procedure internal_open_fullscreen_finish(_pages: Integer);
     procedure internal_post_open_setup;
     procedure internal_reset;
@@ -74,61 +74,61 @@ type
   public
     constructor Create; override;
     destructor Destroy; override;
-    procedure Configure(const AFileName: String); override;
-    function option(const _option: String): Boolean; override;
-    function modes: PPTCMode; override;
+    procedure Configure(const AFileName: string); override;
+    function option(const _option: string): Boolean; override;
+    function modes: TPTCModeList; override;
     procedure open(const _title: string; _pages: Integer); overload; override;
-    procedure open(const _title: string; const _format: TPTCFormat;
+    procedure open(const _title: string; _format: IPTCFormat;
                    _pages: Integer); overload; override;
     procedure open(const _title: string; _width, _height: Integer;
-                   const _format: TPTCFormat; _pages: Integer); overload; override;
-    procedure open(const _title: string; const _mode: TPTCMode;
+                   _format: IPTCFormat; _pages: Integer); overload; override;
+    procedure open(const _title: string; _mode: IPTCMode;
                    _pages: Integer); overload; override;
     procedure close; override;
     procedure flush; override;
     procedure finish; override;
     procedure update; override;
-    procedure update(const _area: TPTCArea); override;
-    procedure copy(surface: TPTCBaseSurface); override;
-    procedure copy(surface: TPTCBaseSurface;
-                   const source, destination: TPTCArea); override;
+    procedure update(_area: IPTCArea); override;
+    procedure copy(surface: IPTCSurface); override;
+    procedure copy(surface: IPTCSurface;
+                   source, destination: IPTCArea); override;
     function lock: Pointer; override;
     procedure unlock; override;
     procedure load(const pixels: Pointer;
                    _width, _height, _pitch: Integer;
-                   const _format: TPTCFormat;
-                   const _palette: TPTCPalette); override;
+                   _format: IPTCFormat;
+                   _palette: IPTCPalette); override;
     procedure load(const pixels: Pointer;
                    _width, _height, _pitch: Integer;
-                   const _format: TPTCFormat;
-                   const _palette: TPTCPalette;
-                   const source, destination: TPTCArea); override;
+                   _format: IPTCFormat;
+                   _palette: IPTCPalette;
+                   source, destination: IPTCArea); override;
     procedure save(pixels: Pointer;
                    _width, _height, _pitch: Integer;
-                   const _format: TPTCFormat;
-                   const _palette: TPTCPalette); override;
+                   _format: IPTCFormat;
+                   _palette: IPTCPalette); override;
     procedure save(pixels: Pointer;
                    _width, _height, _pitch: Integer;
-                   const _format: TPTCFormat;
-                   const _palette: TPTCPalette;
-                   const source, destination: TPTCArea); override;
+                   _format: IPTCFormat;
+                   _palette: IPTCPalette;
+                   source, destination: IPTCArea); override;
     procedure clear; override;
-    procedure clear(const color: TPTCColor); override;
-    procedure clear(const color: TPTCColor;
-                    const _area: TPTCArea); override;
-    procedure Palette(const _palette: TPTCPalette); override;
-    function Palette: TPTCPalette; override;
-    procedure Clip(const _area: TPTCArea); override;
+    procedure clear(color: IPTCColor); override;
+    procedure clear(color: IPTCColor;
+                    _area: IPTCArea); override;
+    procedure Palette(_palette: IPTCPalette); override;
+    function Palette: IPTCPalette; override;
+    procedure Clip(_area: IPTCArea); override;
     function GetWidth: Integer; override;
     function GetHeight: Integer; override;
     function GetPitch: Integer; override;
     function GetPages: Integer; override;
-    function GetArea: TPTCArea; override;
-    function Clip: TPTCArea; override;
-    function GetFormat: TPTCFormat; override;
+    function GetArea: IPTCArea; override;
+    function Clip: IPTCArea; override;
+    function GetFormat: IPTCFormat; override;
     function GetName: string; override;
     function GetTitle: string; override;
     function GetInformation: string; override;
-    function NextEvent(var event: TPTCEvent; wait: Boolean; const EventMask: TPTCEventMask): Boolean; override;
-    function PeekEvent(wait: Boolean; const EventMask: TPTCEventMask): TPTCEvent; override;
+    function NextEvent(out event: IPTCEvent; wait: Boolean; const EventMask: TPTCEventMask): Boolean; override;
+    function PeekEvent(wait: Boolean; const EventMask: TPTCEventMask): IPTCEvent; override;
   end;

+ 52 - 199
packages/ptc/src/dos/cga/cgaconsolei.inc

@@ -36,54 +36,37 @@
 {$DEFINE DEFAULT_FORMAT:=TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF)}
 
 constructor TCGAConsole.Create;
-
-var
-  I: Integer;
-
 begin
   inherited Create;
 
   m_open := False;
   m_locked := False;
-  FillChar(m_modes, SizeOf(m_modes), 0);
   m_title := '';
   m_information := '';
   m_default_width := DEFAULT_WIDTH;
   m_default_height := DEFAULT_HEIGHT;
   m_default_format := DEFAULT_FORMAT;
 
-  for I := 0 to 255 do
-    m_modes[I] := TPTCMode.Create;
-
   m_copy := TPTCCopy.Create;
   m_clear := TPTCClear.Create;
   Configure('ptcpas.cfg');
 end;
 
 destructor TCGAConsole.Destroy;
-
-var
-  I: Integer;
-
 begin
   close;
-  for I := 0 to 255 do
-    m_modes[I].Free;
   m_keyboard.Free;
   FMouse.Free;
   FEventQueue.Free;
   m_copy.Free;
   m_clear.Free;
-  m_default_format.Free;
   inherited Destroy;
 end;
 
-procedure TCGAConsole.Configure(const AFileName: String);
-
+procedure TCGAConsole.Configure(const AFileName: string);
 var
-  F: Text;
+  F: TextFile;
   S: string;
-
 begin
   AssignFile(F, AFileName);
   {$push}{$I-}
@@ -103,8 +86,7 @@ begin
   CloseFile(F);
 end;
 
-function TCGAConsole.option(const _option: String): Boolean;
-
+function TCGAConsole.option(const _option: string): Boolean;
 begin
   {...}
   if _option = 'enable logging' then
@@ -123,47 +105,33 @@ begin
   Result := m_copy.option(_option);
 end;
 
-function TCGAConsole.modes: PPTCMode;
-
+function TCGAConsole.modes: TPTCModeList;
 begin
-  Result := @m_modes;
+  Result := m_modes;
 end;
 
-procedure TCGAConsole.open(const _title: string; _pages: Integer); overload;
-
+procedure TCGAConsole.Open(const _title: string; _pages: Integer); overload;
 begin
   open(_title, m_default_format, _pages);
 end;
 
-procedure TCGAConsole.open(const _title: string; const _format: TPTCFormat;
+procedure TCGAConsole.open(const _title: string; _format: IPTCFormat;
                            _pages: Integer); overload;
-
 begin
   open(_title, m_default_width, m_default_height, _format, _pages);
 end;
 
 procedure TCGAConsole.open(const _title: string; _width, _height: Integer;
-                           const _format: TPTCFormat; _pages: Integer); overload;
-
-var
-  m: TPTCMode;
-
+                           _format: IPTCFormat; _pages: Integer); overload;
 begin
-  m := TPTCMode.Create(_width, _height, _format);
-  try
-    open(_title, m, _pages);
-  finally
-    m.Free;
-  end;
+  open(_title, TPTCMode.Create(_width, _height, _format), _pages);
 end;
 
-procedure TCGAConsole.open(const _title: string; const _mode: TPTCMode;
+procedure TCGAConsole.open(const _title: string; _mode: IPTCMode;
                            _pages: Integer); overload;
-
 var
   _width, _height: Integer;
-  _format: TPTCFormat;
-
+  _format: IPTCFormat;
 begin
   if not _mode.valid then
     raise TPTCError.Create('invalid mode');
@@ -180,7 +148,6 @@ begin
 end;
 
 procedure TCGAConsole.close;
-
 begin
   if m_open then
   begin
@@ -194,24 +161,20 @@ begin
 end;
 
 procedure TCGAConsole.flush;
-
 begin
   check_open;
   check_unlocked;
 end;
 
 procedure TCGAConsole.finish;
-
 begin
   check_open;
   check_unlocked;
 end;
 
 procedure TCGAConsole.update;
-
 var
   framebuffer: PByte;
-
 begin
   check_open;
   check_unlocked;
@@ -224,17 +187,14 @@ begin
   end;
 end;
 
-procedure TCGAConsole.update(const _area: TPTCArea);
-
+procedure TCGAConsole.update(_area: IPTCArea);
 begin
   update;
 end;
 
-procedure TCGAConsole.copy(surface: TPTCBaseSurface);
-
+procedure TCGAConsole.Copy(surface: IPTCSurface);
 var
   pixels: Pointer;
-
 begin
   check_open;
   check_unlocked;
@@ -248,16 +208,13 @@ begin
   except
     on error: TPTCError do
       raise TPTCError.Create('failed to copy console to surface', error);
-
   end;
 end;
 
-procedure TCGAConsole.copy(surface: TPTCBaseSurface;
-                           const source, destination: TPTCArea);
-
+procedure TCGAConsole.Copy(surface: IPTCSurface;
+                           source, destination: IPTCArea);
 var
   pixels: Pointer;
-
 begin
   check_open;
   check_unlocked;
@@ -276,10 +233,8 @@ begin
 end;
 
 function TCGAConsole.lock: Pointer;
-
 var
   pixels: Pointer;
-
 begin
   check_open;
   if m_locked then
@@ -291,7 +246,6 @@ begin
 end;
 
 procedure TCGAConsole.unlock;
-
 begin
   check_open;
   if not m_locked then
@@ -303,12 +257,10 @@ end;
 
 procedure TCGAConsole.load(const pixels: Pointer;
                            _width, _height, _pitch: Integer;
-                           const _format: TPTCFormat;
-                           const _palette: TPTCPalette);
+                           _format: IPTCFormat;
+                           _palette: IPTCPalette);
 var
-  Area_: TPTCArea;
   console_pixels: Pointer;
-
 begin
   check_open;
   check_unlocked;
@@ -327,70 +279,46 @@ begin
     except
       on error: TPTCError do
         raise TPTCError.Create('failed to load pixels to console', error);
-
     end;
   end
   else
-  begin
-    Area_ := TPTCArea.Create(0, 0, width, height);
-    try
-      load(pixels, _width, _height, _pitch, _format, _palette, Area_, area);
-    finally
-      Area_.Free;
-    end;
-  end;
+    Load(pixels, _width, _height, _pitch, _format, _palette, TPTCArea.Create(0, 0, width, height), area);
 end;
 
 procedure TCGAConsole.load(const pixels: Pointer;
                            _width, _height, _pitch: Integer;
-                           const _format: TPTCFormat;
-                           const _palette: TPTCPalette;
-                           const source, destination: TPTCArea);
+                           _format: IPTCFormat;
+                           _palette: IPTCPalette;
+                           source, destination: IPTCArea);
 var
   console_pixels: Pointer;
-  clipped_source, clipped_destination: TPTCArea;
-  tmp: TPTCArea;
-
+  clipped_source, clipped_destination: IPTCArea;
 begin
   check_open;
   check_unlocked;
-  clipped_source := nil;
-  clipped_destination := nil;
   try
     console_pixels := lock;
     try
-      clipped_source := TPTCArea.Create;
-      clipped_destination := TPTCArea.Create;
-      tmp := TPTCArea.Create(0, 0, _width, _height);
-      try
-        TPTCClipper.clip(source, tmp, clipped_source, destination, clip, clipped_destination);
-      finally
-        tmp.Free;
-      end;
+      TPTCClipper.clip(source, TPTCArea.Create(0, 0, _width, _height), clipped_source, destination, clip, clipped_destination);
       m_copy.request(_format, format);
       m_copy.palette(_palette, palette);
       m_copy.copy(pixels, clipped_source.left, clipped_source.top, clipped_source.width, clipped_source.height, _pitch,
                   console_pixels, clipped_destination.left, clipped_destination.top, clipped_destination.width, clipped_destination.height, pitch);
     finally
       unlock;
-      clipped_source.Free;
-      clipped_destination.Free;
     end;
   except
     on error:TPTCError do
       raise TPTCError.Create('failed to load pixels to console area', error);
-
   end;
 end;
 
 procedure TCGAConsole.save(pixels: Pointer;
                            _width, _height, _pitch: Integer;
-                           const _format: TPTCFormat;
-                           const _palette: TPTCPalette);
+                           _format: IPTCFormat;
+                           _palette: IPTCPalette);
 var
-  Area_: TPTCArea;
   console_pixels: Pointer;
-
 begin
   check_open;
   check_unlocked;
@@ -413,105 +341,66 @@ begin
     end;
   end
   else
-  begin
-    Area_ := TPTCArea.Create(0, 0, width, height);
-    try
-      save(pixels, _width, _height, _pitch, _format, _palette, area, Area_);
-    finally
-      Area_.Free;
-    end;
-  end;
+    Save(pixels, _width, _height, _pitch, _format, _palette, area, TPTCArea.Create(0, 0, width, height));
 end;
 
 procedure TCGAConsole.save(pixels: Pointer;
                            _width, _height, _pitch: Integer;
-                           const _format: TPTCFormat;
-                           const _palette: TPTCPalette;
-                           const source, destination: TPTCArea);
+                           _format: IPTCFormat;
+                           _palette: IPTCPalette;
+                           source, destination: IPTCArea);
 var
   console_pixels: Pointer;
-  clipped_source, clipped_destination: TPTCArea;
-  tmp: TPTCArea;
-
+  clipped_source, clipped_destination: IPTCArea;
 begin
   check_open;
   check_unlocked;
-  clipped_source := nil;
-  clipped_destination := nil;
   try
     console_pixels := lock;
     try
-      clipped_source := TPTCArea.Create;
-      clipped_destination := TPTCArea.Create;
-      tmp := TPTCArea.Create(0, 0, _width, _height);
-      try
-        TPTCClipper.clip(source, clip, clipped_source, destination, tmp, clipped_destination);
-      finally
-        tmp.Free;
-      end;
+      TPTCClipper.clip(source, clip, clipped_source, destination, TPTCArea.Create(0, 0, _width, _height), clipped_destination);
       m_copy.request(format, _format);
       m_copy.palette(palette, _palette);
       m_copy.copy(console_pixels, clipped_source.left, clipped_source.top, clipped_source.width, clipped_source.height, pitch,
                   pixels, clipped_destination.left, clipped_destination.top, clipped_destination.width, clipped_destination.height, _pitch);
     finally
       unlock;
-      clipped_source.Free;
-      clipped_destination.Free;
     end;
   except
     on error:TPTCError do
       raise TPTCError.Create('failed to save console area pixels', error);
-
   end;
 end;
 
-procedure TCGAConsole.clear;
-
+procedure TCGAConsole.Clear;
 var
-  tmp: TPTCColor;
-
+  Color: IPTCColor;
 begin
   check_open;
   check_unlocked;
   if format.direct then
-    tmp := TPTCColor.Create(0, 0, 0, 0)
+    Color := TPTCColor.Create(0, 0, 0, 0)
   else
-    tmp := TPTCColor.Create(0);
-  try
-    clear(tmp);
-  finally
-    tmp.Free;
-  end;
+    Color := TPTCColor.Create(0);
+  Clear(Color);
 end;
 
-procedure TCGAConsole.clear(const color: TPTCColor);
-
-var
-  tmp: TPTCArea;
-
+procedure TCGAConsole.Clear(color: IPTCColor);
 begin
   check_open;
   check_unlocked;
-  tmp := TPTCArea.Create;
-  try
-    clear(color, tmp);
-  finally
-    tmp.Free;
-  end;
+  Clear(color, TPTCArea.Create);
 end;
 
-procedure TCGAConsole.clear(const color: TPTCColor;
-                            const _area: TPTCArea);
-
+procedure TCGAConsole.clear(color: IPTCColor;
+                            _area: IPTCArea);
 var
   pixels: Pointer;
-  clipped_area: TPTCArea;
-
+  clipped_area: IPTCArea;
 begin
   check_open;
   check_unlocked;
   try
-    clipped_area := nil;
     pixels := lock;
     try
       clipped_area := TPTCClipper.clip(_area, clip);
@@ -519,139 +408,111 @@ begin
       m_clear.clear(pixels, clipped_area.left, clipped_area.right, clipped_area.width, clipped_area.height, pitch, color);
     finally
       unlock;
-      clipped_area.Free;
     end;
   except
     on error: TPTCError do
       raise TPTCError.Create('failed to clear console area', error);
-
   end;
 end;
 
-procedure TCGAConsole.Palette(const _palette: TPTCPalette);
-
+procedure TCGAConsole.Palette(_palette: IPTCPalette);
 begin
   check_open;
   m_primary.palette(_palette);
 end;
 
-function TCGAConsole.Palette: TPTCPalette;
-
+function TCGAConsole.Palette: IPTCPalette;
 begin
   check_open;
   Result := m_primary.palette;
 end;
 
-procedure TCGAConsole.Clip(const _area: TPTCArea);
-
+procedure TCGAConsole.Clip(_area: IPTCArea);
 begin
   check_open;
   m_primary.clip(_area);
 end;
 
 function TCGAConsole.GetWidth: Integer;
-
 begin
   check_open;
   Result := m_primary.width;
 end;
 
 function TCGAConsole.GetHeight: Integer;
-
 begin
   check_open;
   Result := m_primary.height;
 end;
 
 function TCGAConsole.GetPitch: Integer;
-
 begin
   check_open;
   Result := m_primary.pitch;
 end;
 
 function TCGAConsole.GetPages: Integer;
-
 begin
   check_open;
   Result := 2;
 end;
 
-function TCGAConsole.GetArea: TPTCArea;
-
+function TCGAConsole.GetArea: IPTCArea;
 begin
   check_open;
   Result := m_primary.area;
 end;
 
-function TCGAConsole.Clip: TPTCArea;
-
+function TCGAConsole.Clip: IPTCArea;
 begin
   check_open;
   Result := m_primary.clip;
 end;
 
-function TCGAConsole.GetFormat: TPTCFormat;
-
+function TCGAConsole.GetFormat: IPTCFormat;
 begin
   check_open;
   Result := m_primary.format;
 end;
 
 function TCGAConsole.GetName: string;
-
 begin
   Result := 'CGA';
 end;
 
 function TCGAConsole.GetTitle: string;
-
 begin
   Result := m_title;
 end;
 
 function TCGAConsole.GetInformation: string;
-
 begin
   Result := m_information;
 end;
 
-procedure TCGAConsole.internal_pre_open_setup(const _title: String);
-
+procedure TCGAConsole.internal_pre_open_setup(const _title: string);
 begin
   m_title := _title;
 end;
 
 procedure TCGAConsole.internal_open_fullscreen_start;
-
-var
-  f: TPTCFormat;
-
 begin
   CGAPrecalc;
 
-  f := TPTCFormat.Create(32, $FF0000, $00FF00, $0000FF);
-  try
-    m_primary := TPTCSurface.Create(320, 200, f);
-  finally
-    f.Free;
-  end;
+  m_primary := TPTCSurface.Create(320, 200, TPTCFormat.Create(32, $FF0000, $00FF00, $0000FF));
 
   CGA320;
 end;
 
-procedure TCGAConsole.internal_open_fullscreen(_width, _height: Integer; const _format: TPTCFormat);
-
+procedure TCGAConsole.internal_open_fullscreen(_width, _height: Integer; const _format: IPTCFormat);
 begin
 end;
 
 procedure TCGAConsole.internal_open_fullscreen_finish(_pages: Integer);
-
 begin
 end;
 
 procedure TCGAConsole.internal_post_open_setup;
-
 begin
   FreeAndNil(m_keyboard);
   FreeAndNil(FMouse);
@@ -668,7 +529,6 @@ begin
 end;
 
 procedure TCGAConsole.internal_reset;
-
 begin
   FreeAndNil(m_primary);
   FreeAndNil(m_keyboard);
@@ -677,7 +537,6 @@ begin
 end;
 
 procedure TCGAConsole.internal_close;
-
 begin
   FreeAndNil(m_primary);
   FreeAndNil(m_keyboard);
@@ -688,18 +547,15 @@ begin
 end;
 
 procedure TCGAConsole.HandleEvents;
-
 begin
   m_keyboard.GetPendingEvents(FEventQueue);
   FMouse.GetPendingEvents(FEventQueue);
 end;
 
-function TCGAConsole.NextEvent(var event: TPTCEvent; wait: Boolean; const EventMask: TPTCEventMask): Boolean;
-
+function TCGAConsole.NextEvent(out event: IPTCEvent; wait: Boolean; const EventMask: TPTCEventMask): Boolean;
 begin
   check_open;
 
-  FreeAndNil(event);
   repeat
     { get events }
     HandleEvents;
@@ -710,8 +566,7 @@ begin
   Result := event <> nil;
 end;
 
-function TCGAConsole.PeekEvent(wait: Boolean; const EventMask: TPTCEventMask): TPTCEvent;
-
+function TCGAConsole.PeekEvent(wait: Boolean; const EventMask: TPTCEventMask): IPTCEvent;
 begin
   check_open;
 
@@ -725,14 +580,12 @@ begin
 end;
 
 procedure TCGAConsole.check_open;
-
 begin
   if not m_open then
     raise TPTCError.Create('console is not open');
 end;
 
 procedure TCGAConsole.check_unlocked;
-
 begin
   if m_locked then
     raise TPTCError.Create('console is not unlocked');

+ 2 - 2
packages/ptc/src/dos/textfx2/textfx2.pp

@@ -69,7 +69,7 @@ const
   use_charset: Pbyte = @charset_b7asc;
   { Character set to use. Can be changed run-time. }
 
-  colmap: PSmallInt = nil;
+  colmap: PWord = nil;
 
 procedure set80x43; { Sets up 80x43, no blink, no cursor. }
 procedure set80x50; { Sets up 80x50, no blink, no cursor. }
@@ -447,7 +447,7 @@ begin
   if colmap <> nil then
     FreeMem(colmap);
   f := 64.0 / COLMAPDIM;
-  colmap := GetMem(SizeOf(SmallInt) * COLMAPDIM * COLMAPDIM * COLMAPDIM);
+  colmap := GetMem(SizeOf(Word) * COLMAPDIM * COLMAPDIM * COLMAPDIM);
   for r := 0 to COLMAPDIM - 1 do
   begin
     for g := 0 to COLMAPDIM - 1 do

+ 46 - 46
packages/ptc/src/dos/textfx2/textfx2consoled.inc

@@ -30,38 +30,38 @@
 }
 
 type
-  TTextFX2Console = Class(TPTCBaseConsole)
+  TTextFX2Console = class(TPTCBaseConsole)
   private
     { data }
-    m_modes: array [0..255] of TPTCMode;
-    m_title: string;
-    m_information: string;
+    FModes: array of IPTCMode;
+    FTitle: string;
+    FInformation: string;
 
     { flags }
-    m_open: Boolean;
-    m_locked: Boolean;
+    FOpen: Boolean;
+    FLocked: Boolean;
 
     { option data }
-    m_default_width: Integer;
-    m_default_height: Integer;
-    m_default_format: TPTCFormat;
+    FDefaultWidth: Integer;
+    FDefaultHeight: Integer;
+    FDefaultFormat: IPTCFormat;
 
     { objects }
-    m_copy: TPTCCopy;
-    m_clear: TPTCClear;
+    FCopy: TPTCCopy;
+    FClear: TPTCClear;
 
     FEventQueue: TEventQueue;
 
     { Dos objects }
-    m_keyboard: TDosKeyboard;
+    FKeyboard: TDosKeyboard;
     FMouse: TDosMouse;
-    m_primary: TPTCSurface;
-    m_160x100buffer: TPTCSurface;
+    FPrimary: TPTCSurface;
+    F160x100buffer: TPTCSurface;
 
     { internal console management routines }
     procedure internal_pre_open_setup(const _title: String);
     procedure internal_open_fullscreen_start;
-    procedure internal_open_fullscreen(_width, _height: Integer; const _format: TPTCFormat);
+    procedure internal_open_fullscreen(_width, _height: Integer; _format: IPTCFormat);
     procedure internal_open_fullscreen_finish(_pages: Integer);
     procedure internal_post_open_setup;
     procedure internal_reset;
@@ -75,61 +75,61 @@ type
   public
     constructor Create; override;
     destructor Destroy; override;
-    procedure Configure(const AFileName: String); override;
-    function option(const _option: String): Boolean; override;
-    function modes: PPTCMode; override;
+    procedure Configure(const AFileName: string); override;
+    function Option(const AOption: string): Boolean; override;
+    function Modes: TPTCModeList; override;
     procedure open(const _title: string; _pages: Integer); overload; override;
-    procedure open(const _title: string; const _format: TPTCFormat;
+    procedure open(const _title: string; _format: IPTCFormat;
                    _pages: Integer); overload; override;
     procedure open(const _title: string; _width, _height: Integer;
-                   const _format: TPTCFormat; _pages: Integer); overload; override;
-    procedure open(const _title: string; const _mode: TPTCMode;
+                   _format: IPTCFormat; _pages: Integer); overload; override;
+    procedure open(const _title: string; _mode: IPTCMode;
                    _pages: Integer); overload; override;
     procedure close; override;
     procedure flush; override;
     procedure finish; override;
     procedure update; override;
-    procedure update(const _area: TPTCArea); override;
-    procedure copy(surface: TPTCBaseSurface); override;
-    procedure copy(surface: TPTCBaseSurface;
-                   const source, destination: TPTCArea); override;
+    procedure update(_area: IPTCArea); override;
+    procedure copy(surface: IPTCSurface); override;
+    procedure copy(surface: IPTCSurface;
+                   source, destination: IPTCArea); override;
     function lock: Pointer; override;
     procedure unlock; override;
     procedure load(const pixels: Pointer;
                    _width, _height, _pitch: Integer;
-                   const _format: TPTCFormat;
-                   const _palette: TPTCPalette); override;
+                   _format: IPTCFormat;
+                   _palette: IPTCPalette); override;
     procedure load(const pixels: Pointer;
                    _width, _height, _pitch: Integer;
-                   const _format: TPTCFormat;
-                   const _palette: TPTCPalette;
-                   const source, destination: TPTCArea); override;
+                   _format: IPTCFormat;
+                   _palette: IPTCPalette;
+                   source, destination: IPTCArea); override;
     procedure save(pixels: Pointer;
                    _width, _height, _pitch: Integer;
-                   const _format: TPTCFormat;
-                   const _palette: TPTCPalette); override;
+                   _format: IPTCFormat;
+                   _palette: IPTCPalette); override;
     procedure save(pixels: Pointer;
                    _width, _height, _pitch: Integer;
-                   const _format: TPTCFormat;
-                   const _palette: TPTCPalette;
-                   const source, destination: TPTCArea); override;
+                   _format: IPTCFormat;
+                   _palette: IPTCPalette;
+                   source, destination: IPTCArea); override;
     procedure clear; override;
-    procedure clear(const color: TPTCColor); override;
-    procedure clear(const color: TPTCColor;
-                    const _area: TPTCArea); override;
-    procedure Palette(const _palette: TPTCPalette); override;
-    function Palette: TPTCPalette; override;
-    procedure Clip(const _area: TPTCArea); override;
+    procedure Clear(AColor: IPTCColor); override;
+    procedure clear(color: IPTCColor;
+                    _area: IPTCArea); override;
+    procedure Palette(_palette: IPTCPalette); override;
+    function Palette: IPTCPalette; override;
+    procedure Clip(_area: IPTCArea); override;
     function GetWidth: Integer; override;
     function GetHeight: Integer; override;
     function GetPitch: Integer; override;
     function GetPages: Integer; override;
-    function GetArea: TPTCArea; override;
-    function Clip: TPTCArea; override;
-    function GetFormat: TPTCFormat; override;
+    function GetArea: IPTCArea; override;
+    function Clip: IPTCArea; override;
+    function GetFormat: IPTCFormat; override;
     function GetName: string; override;
     function GetTitle: string; override;
     function GetInformation: string; override;
-    function NextEvent(var event: TPTCEvent; wait: Boolean; const EventMask: TPTCEventMask): Boolean; override;
-    function PeekEvent(wait: Boolean; const EventMask: TPTCEventMask): TPTCEvent; override;
+    function NextEvent(out event: IPTCEvent; wait: Boolean; const EventMask: TPTCEventMask): Boolean; override;
+    function PeekEvent(wait: Boolean; const EventMask: TPTCEventMask): IPTCEvent; override;
   end;

+ 138 - 285
packages/ptc/src/dos/textfx2/textfx2consolei.inc

@@ -36,61 +36,44 @@
 {$DEFINE DEFAULT_FORMAT:=TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF)}
 
 constructor TTextFX2Console.Create;
-
-var
-  I: Integer;
-
 begin
   inherited Create;
 
-  m_open := False;
-  m_locked := False;
-  FillChar(m_modes, SizeOf(m_modes), 0);
-  m_title := '';
-  m_information := '';
-  m_default_width := DEFAULT_WIDTH;
-  m_default_height := DEFAULT_HEIGHT;
-  m_default_format := DEFAULT_FORMAT;
-
-  for I := Low(m_modes) to High(m_modes) do
-    m_modes[I] := TPTCMode.Create;
+  FOpen := False;
+  FLocked := False;
+  FTitle := '';
+  FInformation := '';
+  FDefaultWidth := DEFAULT_WIDTH;
+  FDefaultHeight := DEFAULT_HEIGHT;
+  FDefaultFormat := DEFAULT_FORMAT;
 
   calcpal := @calcpal_colorbase;
   use_charset := @charset_b7asc;
   build_colormap(0);
-  m_copy := TPTCCopy.Create;
-  m_clear := TPTCClear.Create;
-  configure('ptcpas.cfg');
+  FCopy := TPTCCopy.Create;
+  FClear := TPTCClear.Create;
+  Configure('ptcpas.cfg');
 end;
 
 destructor TTextFX2Console.Destroy;
-
-var
-  I: Integer;
-
 begin
-  close;
-  m_160x100buffer.Free;
-  m_primary.Free;
+  Close;
+  F160x100buffer.Free;
+  FPrimary.Free;
 
-  for I := Low(m_modes) to High(m_modes) do
-    m_modes[I].Free;
-  m_keyboard.Free;
+  FKeyboard.Free;
   FMouse.Free;
   FEventQueue.Free;
-  m_copy.Free;
-  m_clear.Free;
-  m_default_format.Free;
+  FCopy.Free;
+  FClear.Free;
   dispose_colormap;
   inherited Destroy;
 end;
 
-procedure TTextFX2Console.Configure(const AFileName: String);
-
+procedure TTextFX2Console.Configure(const AFileName: string);
 var
-  F: Text;
+  F: TextFile;
   S: string;
-
 begin
   AssignFile(F, AFileName);
   {$push}{$I-}
@@ -110,111 +93,96 @@ begin
   CloseFile(F);
 end;
 
-function TTextFX2Console.option(const _option: String): Boolean;
-
+function TTextFX2Console.Option(const AOption: string): Boolean;
 begin
   {...}
   Result := True;
-  if _option = 'charset_b8ibm' then
+  if AOption = 'charset_b8ibm' then
   begin
     use_charset := @charset_b8ibm;
     exit;
   end;
-  if _option = 'charset_b7asc' then
+  if AOption = 'charset_b7asc' then
   begin
     use_charset := @charset_b7asc;
     exit;
   end;
-  if _option = 'charset_b7sml' then
+  if AOption = 'charset_b7sml' then
   begin
     use_charset := @charset_b7sml;
     exit;
   end;
-  if _option = 'charset_b8gry' then
+  if AOption = 'charset_b8gry' then
   begin
     use_charset := @charset_b8gry;
     exit;
   end;
-  if _option = 'charset_b7nws' then
+  if AOption = 'charset_b7nws' then
   begin
     use_charset := @charset_b7nws;
     exit;
   end;
-  if _option = 'calcpal_colorbase' then
+  if AOption = 'calcpal_colorbase' then
   begin
     calcpal := @calcpal_colorbase;
     build_colormap(0);
     exit;
   end;
-  if _option = 'calcpal_lightbase' then
+  if AOption = 'calcpal_lightbase' then
   begin
     calcpal := @calcpal_lightbase;
     build_colormap(0);
     exit;
   end;
-  if _option = 'calcpal_lightbase_g' then
+  if AOption = 'calcpal_lightbase_g' then
   begin
     calcpal := @calcpal_lightbase_g;
     build_colormap(0);
     exit;
   end;
-  if _option = 'enable logging' then
+  if AOption = 'enable logging' then
   begin
     LOG_enabled := True;
     Result := True;
     exit;
   end;
-  if _option = 'disable logging' then
+  if AOption = 'disable logging' then
   begin
     LOG_enabled := False;
     Result := True;
     exit;
   end;
 
-  Result := m_copy.option(_option);
+  Result := FCopy.Option(AOption);
 end;
 
-function TTextFX2Console.modes: PPTCMode;
-
+function TTextFX2Console.Modes: TPTCModeList;
 begin
-  Result := @m_modes;
+  Result := FModes;
 end;
 
-procedure TTextFX2Console.open(const _title: string; _pages: Integer); overload;
-
+procedure TTextFX2Console.Open(const _title: string; _pages: Integer); overload;
 begin
-  open(_title, m_default_format, _pages);
+  Open(_title, FDefaultFormat, _pages);
 end;
 
-procedure TTextFX2Console.open(const _title: string; const _format: TPTCFormat;
+procedure TTextFX2Console.open(const _title: string; _format: IPTCFormat;
                                _pages: Integer); overload;
-
 begin
-  open(_title, m_default_width, m_default_height, _format, _pages);
+  open(_title, FDefaultWidth, FDefaultHeight, _format, _pages);
 end;
 
 procedure TTextFX2Console.open(const _title: string; _width, _height: Integer;
-                               const _format: TPTCFormat; _pages: Integer); overload;
-
-var
-  m: TPTCMode;
-
+                               _format: IPTCFormat; _pages: Integer); overload;
 begin
-  m := TPTCMode.Create(_width, _height, _format);
-  try
-    open(_title, m, _pages);
-  finally
-    m.Free;
-  end;
+  open(_title, TPTCMode.Create(_width, _height, _format), _pages);
 end;
 
-procedure TTextFX2Console.open(const _title: string; const _mode: TPTCMode;
+procedure TTextFX2Console.open(const _title: string; _mode: IPTCMode;
                                _pages: Integer); overload;
-
 var
   _width, _height: Integer;
-  _format: TPTCFormat;
-
+  _format: IPTCFormat;
 begin
   if not _mode.valid then
     raise TPTCError.Create('invalid mode');
@@ -228,61 +196,53 @@ begin
   internal_post_open_setup;
 end;
 
-procedure TTextFX2Console.close;
-
+procedure TTextFX2Console.Close;
 begin
-  if m_open then
+  if FOpen then
   begin
-    if m_locked then
+    if FLocked then
       raise TPTCError.Create('console is still locked');
     {flush all key presses}
     while KeyPressed do ReadKey;
     internal_close;
-    m_open := False;
+    FOpen := False;
   end;
 end;
 
 procedure TTextFX2Console.flush;
-
 begin
   check_open;
   check_unlocked;
 end;
 
 procedure TTextFX2Console.finish;
-
 begin
   check_open;
   check_unlocked;
 end;
 
 procedure TTextFX2Console.update;
-
 var
   framebuffer: PInteger;
-
 begin
   check_open;
   check_unlocked;
 
-  m_primary.copy(m_160x100buffer);
-  framebuffer := m_160x100buffer.lock;
+  FPrimary.copy(F160x100buffer);
+  framebuffer := F160x100buffer.Lock;
   vrc;
   dump_160x(0, 50, framebuffer);
-  m_160x100buffer.unlock;
+  F160x100buffer.Unlock;
 end;
 
-procedure TTextFX2Console.update(const _area: TPTCArea);
-
+procedure TTextFX2Console.update(_area: IPTCArea);
 begin
   update;
 end;
 
-procedure TTextFX2Console.copy(surface: TPTCBaseSurface);
-
+procedure TTextFX2Console.copy(surface: IPTCSurface);
 var
   pixels: Pointer;
-
 begin
   check_open;
   check_unlocked;
@@ -296,16 +256,13 @@ begin
   except
     on error: TPTCError do
       raise TPTCError.Create('failed to copy console to surface', error);
-
   end;
 end;
 
-procedure TTextFX2Console.copy(surface: TPTCBaseSurface;
-                               const source, destination: TPTCArea);
-
+procedure TTextFX2Console.copy(surface: IPTCSurface;
+                               source, destination: IPTCArea);
 var
   pixels: Pointer;
-
 begin
   check_open;
   check_unlocked;
@@ -319,43 +276,37 @@ begin
   except
     on error: TPTCError do
       raise TPTCError.Create('failed to copy console to surface', error);
-
   end;
 end;
 
-function TTextFX2Console.lock: Pointer;
-
+function TTextFX2Console.Lock: Pointer;
 var
   pixels: Pointer;
-
 begin
   check_open;
-  if m_locked then
+  if FLocked then
     raise TPTCError.Create('console is already locked');
-  pixels := m_primary.lock;
-  m_locked := True;
+  pixels := FPrimary.lock;
+  FLocked := True;
   Result := pixels;
 end;
 
-procedure TTextFX2Console.unlock;
-
+procedure TTextFX2Console.Unlock;
 begin
   check_open;
-  if not m_locked then
+  if not FLocked then
     raise TPTCError.Create('console is not locked');
 
-  m_primary.unlock;
-  m_locked := False;
+  FPrimary.unlock;
+  FLocked := False;
 end;
 
-procedure TTextFX2Console.load(const pixels: Pointer;
+procedure TTextFX2Console.Load(const pixels: Pointer;
                                _width, _height, _pitch: Integer;
-                               const _format: TPTCFormat;
-                               const _palette: TPTCPalette);
+                               _format: IPTCFormat;
+                               _palette: IPTCPalette);
 var
-  Area_: TPTCArea;
   console_pixels: Pointer;
-
 begin
   check_open;
   check_unlocked;
@@ -364,9 +315,9 @@ begin
     try
       console_pixels := lock;
       try
-        m_copy.request(_format, format);
-        m_copy.palette(_palette, palette);
-        m_copy.copy(pixels, 0, 0, _width, _height, _pitch, console_pixels, 0, 0,
+        FCopy.request(_format, format);
+        FCopy.palette(_palette, palette);
+        FCopy.copy(pixels, 0, 0, _width, _height, _pitch, console_pixels, 0, 0,
                     width, height, pitch);
       finally
         unlock;
@@ -374,54 +325,33 @@ begin
     except
       on error: TPTCError do
         raise TPTCError.Create('failed to load pixels to console', error);
-
     end;
   end
   else
-  begin
-    Area_ := TPTCArea.Create(0, 0, width, height);
-    try
-      load(pixels, _width, _height, _pitch, _format, _palette, Area_, area);
-    finally
-      Area_.Free;
-    end;
-  end;
+    Load(pixels, _width, _height, _pitch, _format, _palette, TPTCArea.Create(0, 0, width, height), area);
 end;
 
 procedure TTextFX2Console.load(const pixels: Pointer;
                                _width, _height, _pitch: Integer;
-                               const _format: TPTCFormat;
-                               const _palette: TPTCPalette;
-                               const source, destination: TPTCArea);
+                               _format: IPTCFormat;
+                               _palette: IPTCPalette;
+                               source, destination: IPTCArea);
 var
   console_pixels: Pointer;
-  clipped_source, clipped_destination: TPTCArea;
-  tmp: TPTCArea;
-
+  clipped_source, clipped_destination: IPTCArea;
 begin
   check_open;
   check_unlocked;
-  clipped_source := nil;
-  clipped_destination := nil;
   try
     console_pixels := lock;
     try
-      clipped_source := TPTCArea.Create;
-      clipped_destination := TPTCArea.Create;
-      tmp := TPTCArea.Create(0, 0, _width, _height);
-      try
-        TPTCClipper.clip(source, tmp, clipped_source, destination, clip, clipped_destination);
-      finally
-        tmp.Free;
-      end;
-      m_copy.request(_format, format);
-      m_copy.palette(_palette, palette);
-      m_copy.copy(pixels, clipped_source.left, clipped_source.top, clipped_source.width, clipped_source.height, _pitch,
+      TPTCClipper.clip(source, TPTCArea.Create(0, 0, _width, _height), clipped_source, destination, clip, clipped_destination);
+      FCopy.request(_format, format);
+      FCopy.palette(_palette, palette);
+      FCopy.copy(pixels, clipped_source.left, clipped_source.top, clipped_source.width, clipped_source.height, _pitch,
                   console_pixels, clipped_destination.left, clipped_destination.top, clipped_destination.width, clipped_destination.height, pitch);
     finally
       unlock;
-      clipped_source.Free;
-      clipped_destination.Free;
     end;
   except
     on error:TPTCError do
@@ -432,12 +362,10 @@ end;
 
 procedure TTextFX2Console.save(pixels: Pointer;
                                _width, _height, _pitch: Integer;
-                               const _format: TPTCFormat;
-                               const _palette: TPTCPalette);
+                               _format: IPTCFormat;
+                               _palette: IPTCPalette);
 var
-  Area_: TPTCArea;
   console_pixels: Pointer;
-
 begin
   check_open;
   check_unlocked;
@@ -446,9 +374,9 @@ begin
     try
       console_pixels := lock;
       try
-        m_copy.request(format, _format);
-        m_copy.palette(palette, _palette);
-        m_copy.copy(console_pixels, 0, 0, width, height, pitch, pixels, 0, 0,
+        FCopy.request(format, _format);
+        FCopy.palette(palette, _palette);
+        FCopy.copy(console_pixels, 0, 0, width, height, pitch, pixels, 0, 0,
                     _width, _height, _pitch);
       finally
         unlock;
@@ -456,117 +384,76 @@ begin
     except
       on error: TPTCError do
         raise TPTCError.Create('failed to save console pixels', error);
-
     end;
   end
   else
-  begin
-    Area_ := TPTCArea.Create(0, 0, width, height);
-    try
-      save(pixels, _width, _height, _pitch, _format, _palette, area, Area_);
-    finally
-      Area_.Free;
-    end;
-  end;
+    Save(pixels, _width, _height, _pitch, _format, _palette, area, TPTCArea.Create(0, 0, width, height));
 end;
 
 procedure TTextFX2Console.save(pixels: Pointer;
                                _width, _height, _pitch: Integer;
-                               const _format: TPTCFormat;
-                               const _palette: TPTCPalette;
-                               const source, destination: TPTCArea);
+                               _format: IPTCFormat;
+                               _palette: IPTCPalette;
+                               source, destination: IPTCArea);
 var
   console_pixels: Pointer;
-  clipped_source, clipped_destination: TPTCArea;
-  tmp: TPTCArea;
-
+  clipped_source, clipped_destination: IPTCArea;
 begin
   check_open;
   check_unlocked;
-  clipped_source := nil;
-  clipped_destination := nil;
   try
     console_pixels := lock;
     try
-      clipped_source := TPTCArea.Create;
-      clipped_destination := TPTCArea.Create;
-      tmp := TPTCArea.Create(0, 0, _width, _height);
-      try
-        TPTCClipper.clip(source, clip, clipped_source, destination, tmp, clipped_destination);
-      finally
-        tmp.Free;
-      end;
-      m_copy.request(format, _format);
-      m_copy.palette(palette, _palette);
-      m_copy.copy(console_pixels, clipped_source.left, clipped_source.top, clipped_source.width, clipped_source.height, pitch,
-                  pixels, clipped_destination.left, clipped_destination.top, clipped_destination.width, clipped_destination.height, _pitch);
+      TPTCClipper.clip(source, clip, clipped_source, destination, TPTCArea.Create(0, 0, _width, _height), clipped_destination);
+      FCopy.request(format, _format);
+      FCopy.palette(palette, _palette);
+      FCopy.copy(console_pixels, clipped_source.left, clipped_source.top, clipped_source.width, clipped_source.height, pitch,
+                 pixels, clipped_destination.left, clipped_destination.top, clipped_destination.width, clipped_destination.height, _pitch);
     finally
       unlock;
-      clipped_source.Free;
-      clipped_destination.Free;
     end;
   except
     on error:TPTCError do
       raise TPTCError.Create('failed to save console area pixels', error);
-
   end;
 end;
 
 procedure TTextFX2Console.clear;
-
 var
-  tmp: TPTCColor;
-
+  Color: IPTCColor;
 begin
   check_open;
   check_unlocked;
   if format.direct then
-    tmp := TPTCColor.Create(0, 0, 0, 0)
+    Color := TPTCColor.Create(0, 0, 0, 0)
   else
-    tmp := TPTCColor.Create(0);
-  try
-    clear(tmp);
-  finally
-    tmp.Free;
-  end;
+    Color := TPTCColor.Create(0);
+  Clear(Color);
 end;
 
-procedure TTextFX2Console.clear(const color: TPTCColor);
-
-var
-  tmp: TPTCArea;
-
+procedure TTextFX2Console.Clear(AColor: IPTCColor);
 begin
   check_open;
   check_unlocked;
-  tmp := TPTCArea.Create;
-  try
-    clear(color, tmp);
-  finally
-    tmp.Free;
-  end;
+  Clear(AColor, TPTCArea.Create);
 end;
 
-procedure TTextFX2Console.clear(const color: TPTCColor;
-                                const _area: TPTCArea);
-
+procedure TTextFX2Console.clear(color: IPTCColor;
+                                _area: IPTCArea);
 var
   pixels: Pointer;
-  clipped_area: TPTCArea;
-
+  clipped_area: IPTCArea;
 begin
   check_open;
   check_unlocked;
   try
-    clipped_area := nil;
     pixels := lock;
     try
       clipped_area := TPTCClipper.clip(_area, clip);
-      m_clear.request(format);
-      m_clear.clear(pixels, clipped_area.left, clipped_area.right, clipped_area.width, clipped_area.height, pitch, color);
+      FClear.request(format);
+      FClear.clear(pixels, clipped_area.left, clipped_area.right, clipped_area.width, clipped_area.height, pitch, color);
     finally
       unlock;
-      clipped_area.Free;
     end;
   except
     on error: TPTCError do
@@ -575,176 +462,145 @@ begin
   end;
 end;
 
-procedure TTextFX2Console.Palette(const _palette: TPTCPalette);
-
+procedure TTextFX2Console.Palette(_palette: IPTCPalette);
 begin
   check_open;
-  m_primary.palette(_palette);
+  FPrimary.palette(_palette);
 end;
 
-function TTextFX2Console.Palette: TPTCPalette;
-
+function TTextFX2Console.Palette: IPTCPalette;
 begin
   check_open;
-  Result := m_primary.palette;
+  Result := FPrimary.palette;
 end;
 
-procedure TTextFX2Console.Clip(const _area: TPTCArea);
-
+procedure TTextFX2Console.Clip(_area: IPTCArea);
 begin
   check_open;
-  m_primary.clip(_area);
+  FPrimary.clip(_area);
 end;
 
 function TTextFX2Console.GetWidth: Integer;
-
 begin
   check_open;
-  Result := m_primary.width;
+  Result := FPrimary.width;
 end;
 
 function TTextFX2Console.GetHeight: Integer;
-
 begin
   check_open;
-  Result := m_primary.height;
+  Result := FPrimary.height;
 end;
 
 function TTextFX2Console.GetPitch: Integer;
-
 begin
   check_open;
-  Result := m_primary.pitch;
+  Result := FPrimary.pitch;
 end;
 
 function TTextFX2Console.GetPages: Integer;
-
 begin
   check_open;
-  Result := 2;{m_primary.pages;}
+  Result := 2;{FPrimary.pages;}
 end;
 
-function TTextFX2Console.GetArea: TPTCArea;
-
+function TTextFX2Console.GetArea: IPTCArea;
 begin
   check_open;
-  Result := m_primary.area;
+  Result := FPrimary.area;
 end;
 
-function TTextFX2Console.Clip: TPTCArea;
-
+function TTextFX2Console.Clip: IPTCArea;
 begin
   check_open;
-  Result := m_primary.clip;
+  Result := FPrimary.clip;
 end;
 
-function TTextFX2Console.GetFormat: TPTCFormat;
-
+function TTextFX2Console.GetFormat: IPTCFormat;
 begin
   check_open;
-  Result := m_primary.format;
+  Result := FPrimary.format;
 end;
 
 function TTextFX2Console.GetName: string;
-
 begin
   Result := 'TextFX2';
 end;
 
 function TTextFX2Console.GetTitle: string;
-
 begin
-  Result := m_title;
+  Result := FTitle;
 end;
 
 function TTextFX2Console.GetInformation: string;
-
 begin
-  Result := m_information;
+  Result := FInformation;
 end;
 
-procedure TTextFX2Console.internal_pre_open_setup(const _title: String);
-
+procedure TTextFX2Console.internal_pre_open_setup(const _title: string);
 begin
-  m_title := _title;
+  FTitle := _title;
 end;
 
 procedure TTextFX2Console.internal_open_fullscreen_start;
-
-var
-  f: TPTCFormat;
-
 begin
-  f := TPTCFormat.Create(32, $0000FF, $00FF00, $FF0000);
-  try
-    m_160x100buffer := TPTCSurface.Create(160, 100, f);
-  finally
-    f.Free;
-  end;
+  F160x100buffer := TPTCSurface.Create(160, 100, TPTCFormat.Create(32, $0000FF, $00FF00, $FF0000));
   set80x50;
 end;
 
-procedure TTextFX2Console.internal_open_fullscreen(_width, _height: Integer; const _format: TPTCFormat);
-
+procedure TTextFX2Console.internal_open_fullscreen(_width, _height: Integer; _format: IPTCFormat);
 begin
-  m_primary := TPTCSurface.Create(_width, _height, _format);
+  FPrimary := TPTCSurface.Create(_width, _height, _format);
 end;
 
 procedure TTextFX2Console.internal_open_fullscreen_finish(_pages: Integer);
-
 begin
 end;
 
 procedure TTextFX2Console.internal_post_open_setup;
-
 begin
-  FreeAndNil(m_keyboard);
+  FreeAndNil(FKeyboard);
   FreeAndNil(FMouse);
   FreeAndNil(FEventQueue);
-  m_keyboard := TDosKeyboard.Create;
-  FMouse := TDosMouse.Create(m_primary.width, m_primary.height);
+  FKeyboard := TDosKeyboard.Create;
+  FMouse := TDosMouse.Create(FPrimary.width, FPrimary.height);
   FEventQueue := TEventQueue.Create;
 
   { temporary platform dependent information fudge }
-  m_information := 'dos version x.xx.x, TextFX2, ...';
+  FInformation := 'dos version x.xx.x, TextFX2, ...';
 
   { set open flag }
-  m_open := True;
+  FOpen := True;
 end;
 
 procedure TTextFX2Console.internal_reset;
-
 begin
-  FreeAndNil(m_primary);
-  FreeAndNil(m_keyboard);
+  FreeAndNil(FPrimary);
+  FreeAndNil(FKeyboard);
   FreeAndNil(FMouse);
   FreeAndNil(FEventQueue);
 end;
 
 procedure TTextFX2Console.internal_close;
-
 begin
-  FreeAndNil(m_primary);
-  FreeAndNil(m_160x100buffer);
-  FreeAndNil(m_keyboard);
+  FreeAndNil(FPrimary);
+  FreeAndNil(F160x100buffer);
+  FreeAndNil(FKeyboard);
   FreeAndNil(FMouse);
   FreeAndNil(FEventQueue);
   set80x25;
 end;
 
 procedure TTextFX2Console.HandleEvents;
-
 begin
-  m_keyboard.GetPendingEvents(FEventQueue);
+  FKeyboard.GetPendingEvents(FEventQueue);
   FMouse.GetPendingEvents(FEventQueue);
 end;
 
-function TTextFX2Console.NextEvent(var event: TPTCEvent; wait: Boolean; const EventMask: TPTCEventMask): Boolean;
-
+function TTextFX2Console.NextEvent(out event: IPTCEvent; wait: Boolean; const EventMask: TPTCEventMask): Boolean;
 begin
   check_open;
 
-  FreeAndNil(event);
   repeat
     { get events }
     HandleEvents;
@@ -755,8 +611,7 @@ begin
   Result := event <> nil;
 end;
 
-function TTextFX2Console.PeekEvent(wait: Boolean; const EventMask: TPTCEventMask): TPTCEvent;
-
+function TTextFX2Console.PeekEvent(wait: Boolean; const EventMask: TPTCEventMask): IPTCEvent;
 begin
   check_open;
 
@@ -770,15 +625,13 @@ begin
 end;
 
 procedure TTextFX2Console.check_open;
-
 begin
-  if not m_open then
+  if not FOpen then
     raise TPTCError.Create('console is not open');
 end;
 
 procedure TTextFX2Console.check_unlocked;
-
 begin
-  if m_locked then
+  if FLocked then
     raise TPTCError.Create('console is not unlocked');
 end;

+ 38 - 38
packages/ptc/src/dos/vesa/vesaconsoled.inc

@@ -33,7 +33,7 @@ type
   TVESAConsole = class(TPTCBaseConsole)
   private
     { data }
-    FModes: array of TPTCMode;
+    FModes: array of IPTCMode;
     FModesLast: Integer;
     FModesN: array of record
       Index: Integer;
@@ -66,16 +66,16 @@ type
     FDefaultWidth: Integer;
     FDefaultHeight: Integer;
 //    FDefaultPages: Integer;
-    FDefaultFormat: TPTCFormat;
+    FDefaultFormat: IPTCFormat;
 
     { objects }
     FCopy: TPTCCopy;
-    FArea: TPTCArea;
-    FClip: TPTCArea;
-//    FFormat: TPTCFormat;
+    FArea: IPTCArea;
+    FClip: IPTCArea;
+//    FFormat: IPTCFormat;
 
 //    FClear: TPTCClear;
-    FPalette: TPTCPalette;
+    FPalette: IPTCPalette;
 
     FEventQueue: TEventQueue;
 
@@ -85,7 +85,7 @@ type
 
     { internal console management routines }
     procedure internal_close;
-    function FindBestMode(const AMode: TPTCMode): Integer;
+    function FindBestMode(const AMode: IPTCMode): Integer;
     
     procedure UpdateModeList;
     procedure EnableLFB;
@@ -101,61 +101,61 @@ type
   public
     constructor Create; override;
     destructor Destroy; override;
-    procedure Configure(const AFileName: String); override;
-    function Option(const AOption: String): Boolean; override;
-    function Modes: PPTCMode; override;
+    procedure Configure(const AFileName: string); override;
+    function Option(const AOption: string): Boolean; override;
+    function Modes: TPTCModeList; override;
     procedure Open(const ATitle: string; APages: Integer); overload; override;
-    procedure Open(const ATitle: string; const AFormat: TPTCFormat;
+    procedure Open(const ATitle: string; AFormat: IPTCFormat;
                    APages: Integer); overload; override;
     procedure Open(const ATitle: string; AWidth, AHeight: Integer;
-                   const AFormat: TPTCFormat; APages: Integer); overload; override;
-    procedure Open(const ATitle: string; const AMode: TPTCMode;
+                   AFormat: IPTCFormat; APages: Integer); overload; override;
+    procedure Open(const ATitle: string; AMode: IPTCMode;
                    APages: Integer); overload; override;
     procedure Close; override;
     procedure Flush; override;
     procedure Finish; override;
     procedure Update; override;
-    procedure Update(const AArea: TPTCArea); override;
-    procedure Copy(ASurface: TPTCBaseSurface); override;
-    procedure Copy(ASurface: TPTCBaseSurface;
-                   const ASource, ADestination: TPTCArea); override;
+    procedure Update(AArea: IPTCArea); override;
+    procedure Copy(ASurface: IPTCSurface); override;
+    procedure Copy(ASurface: IPTCSurface;
+                   ASource, ADestination: IPTCArea); override;
     function Lock: Pointer; override;
     procedure Unlock; override;
     procedure Load(const APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette); override;
     procedure Load(const APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette;
-                   const ASource, ADestination: TPTCArea); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette;
+                   ASource, ADestination: IPTCArea); override;
     procedure Save(APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette); override;
     procedure Save(APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette;
-                   const ASource, ADestination: TPTCArea); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette;
+                   ASource, ADestination: IPTCArea); override;
     procedure Clear; override;
-    procedure Clear(const AColor: TPTCColor); override;
-    procedure Clear(const AColor: TPTCColor;
-                    const AArea: TPTCArea); override;
-    procedure Palette(const APalette: TPTCPalette); override;
-    function Palette: TPTCPalette; override;
-    procedure Clip(const AArea: TPTCArea); override;
+    procedure Clear(AColor: IPTCColor); override;
+    procedure Clear(AColor: IPTCColor;
+                    AArea: IPTCArea); override;
+    procedure Palette(APalette: IPTCPalette); override;
+    function Palette: IPTCPalette; override;
+    procedure Clip(AArea: IPTCArea); override;
     function GetWidth: Integer; override;
     function GetHeight: Integer; override;
     function GetPitch: Integer; override;
     function GetPages: Integer; override;
-    function GetArea: TPTCArea; override;
-    function Clip: TPTCArea; override;
-    function GetFormat: TPTCFormat; override;
+    function GetArea: IPTCArea; override;
+    function Clip: IPTCArea; override;
+    function GetFormat: IPTCFormat; override;
     function GetName: string; override;
     function GetTitle: string; override;
     function GetInformation: string; override;
-    function NextEvent(var AEvent: TPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean; override;
-    function PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): TPTCEvent; override;
+    function NextEvent(out AEvent: IPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean; override;
+    function PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): IPTCEvent; override;
   end;

+ 95 - 201
packages/ptc/src/dos/vesa/vesaconsolei.inc

@@ -73,24 +73,13 @@ begin
 end;
 
 destructor TVESAConsole.Destroy;
-var
-  I: Integer;
 begin
   Close;
   
-  for I := Low(FModes) to High(FModes) do
-    FreeAndNil(FModes[I]);
-  SetLength(FModes, 0);
-  SetLength(FModesN, 0);
-  
   FKeyboard.Free;
   FMouse.Free;
   FEventQueue.Free;
   FCopy.Free;
-  FDefaultFormat.Free;
-  FPalette.Free;
-  FArea.Free;
-  FClip.Free;
   inherited Destroy;
 end;
 
@@ -99,10 +88,7 @@ var
   I, J: Integer;
   r, g, b, a: DWord;
   tmpbpp: Integer;
-  tmp: TPTCFormat;
 begin
-  for I := Low(FModes) to High(FModes) do
-    FreeAndNil(FModes[I]);
   SetLength(FModes, 0);
   SetLength(FModesN, 0);
 
@@ -134,8 +120,8 @@ begin
             (BitsPerPixel = 24) or
             (BitsPerPixel = 32)) then
         begin
-	  if FTryWindowed and SupportsWindowed then
-	  begin
+          if FTryWindowed and SupportsWindowed then
+          begin
             Inc(J);
             r := MakeMask(WindowedRedMaskSize, WindowedRedFieldPosition);
             g := MakeMask(WindowedGreenMaskSize, WindowedGreenFieldPosition);
@@ -146,17 +132,12 @@ begin
               tmpbpp := 16
             else
               tmpbpp := BitsPerPixel;
-            tmp := TPTCFormat.Create(tmpbpp, r, g, b, a);
-            try
-	      SetLength(FModes, J + 1);
-	      SetLength(FModesN, J + 1);
-              FModes[J] := TPTCMode.Create(XResolution, YResolution, tmp);
-              FModesN[J].Index := I;
-	      FModesN[J].SupportsWindowed := true;
-            finally
-              tmp.Free;
-            end;
-	  end;
+            SetLength(FModes, J + 1);
+            SetLength(FModesN, J + 1);
+            FModes[J] := TPTCMode.Create(XResolution, YResolution, TPTCFormat.Create(tmpbpp, r, g, b, a));
+            FModesN[J].Index := I;
+            FModesN[J].SupportsWindowed := true;
+          end;
 	  
 	  if FTryLFB and SupportsLFB then
 	  begin
@@ -184,16 +165,11 @@ begin
                 tmpbpp := 16
               else
                 tmpbpp := BitsPerPixel;
-              tmp := TPTCFormat.Create(tmpbpp, r, g, b, a);
-              try
-	        SetLength(FModes, J + 1);
-	        SetLength(FModesN, J + 1);
-                FModes[J] := TPTCMode.Create(XResolution, YResolution, tmp);
-                FModesN[J].Index := I;
-  	        FModesN[J].SupportsLFB := true;
-              finally
-                tmp.Free;
-              end;
+              SetLength(FModes, J + 1);
+              SetLength(FModesN, J + 1);
+              FModes[J] := TPTCMode.Create(XResolution, YResolution, TPTCFormat.Create(tmpbpp, r, g, b, a));
+              FModesN[J].Index := I;
+              FModesN[J].SupportsLFB := true;
 	    end;
 	  end;
 {          Inc(FModesLast)}
@@ -202,38 +178,28 @@ begin
           if (MemoryModel = vmmmPackedPixel) and (BitsPerPixel = 8) then
           begin
             Inc(J);
-            tmp := TPTCFormat.Create(8);
-            try
-              SetLength(FModes, J + 1);
-              SetLength(FModesN, J + 1);
-              FModes[J] := TPTCMode.Create(XResolution, YResolution, tmp);
-              FModesN[J].Index := I;
-	      FModesN[J].SupportsWindowed := FTryWindowed and SupportsWindowed;
-	      FModesN[J].SupportsLFB := FTryLFB and SupportsLFB;
-            finally
-              tmp.Free;
-            end;
+            SetLength(FModes, J + 1);
+            SetLength(FModesN, J + 1);
+            FModes[J] := TPTCMode.Create(XResolution, YResolution, TPTCFormat.Create(8));
+            FModesN[J].Index := I;
+            FModesN[J].SupportsWindowed := FTryWindowed and SupportsWindowed;
+            FModesN[J].SupportsLFB := FTryLFB and SupportsLFB;
             Inc(J);
-            tmp := TPTCFormat.Create(8, $E0, $1C, $03); {RGB 332}
-            try
-              SetLength(FModes, J + 1);
-              SetLength(FModesN, J + 1);
-              FModes[J] := TPTCMode.Create(XResolution, YResolution, tmp);
-              FModesN[J].Index := I;
-	      FModesN[J].SupportsWindowed := FTryWindowed and SupportsWindowed;
-	      FModesN[J].SupportsLFB := FTryLFB and SupportsLFB;
-            finally
-              tmp.Free;
-            end;
+			
+            {RGB 332}
+            SetLength(FModes, J + 1);
+            SetLength(FModesN, J + 1);
+            FModes[J] := TPTCMode.Create(XResolution, YResolution, TPTCFormat.Create(8, $E0, $1C, $03));
+            FModesN[J].Index := I;
+            FModesN[J].SupportsWindowed := FTryWindowed and SupportsWindowed;
+            FModesN[J].SupportsLFB := FTryLFB and SupportsLFB;
 {           Inc(FModesLast, 2);}
           end;
 
   FModesLast := J;
-  SetLength(FModes, FModesLast + 2);
-  FModes[FModesLast + 1] := TPTCMode.Create; {mark end of list!}
 end;
 
-procedure TVESAConsole.Configure(const AFileName: String);
+procedure TVESAConsole.Configure(const AFileName: string);
 var
   F: TextFile;
   S: string;
@@ -394,12 +360,12 @@ begin
   Result := FCopy.Option(AOption);
 end;
 
-function TVESAConsole.Modes: PPTCMode;
+function TVESAConsole.Modes: TPTCModeList;
 begin
-  Result := @FModes[0];
+  Result := FModes;
 end;
 
-function TVESAConsole.FindBestMode(const AMode: TPTCMode): Integer;
+function TVESAConsole.FindBestMode(const AMode: IPTCMode): Integer;
 var
   I: Integer;
   modefound, bestmodefound: Integer;
@@ -491,26 +457,19 @@ begin
   Open(ATitle, FDefaultFormat, APages);
 end;
 
-procedure TVESAConsole.Open(const ATitle: string; const AFormat: TPTCFormat;
+procedure TVESAConsole.Open(const ATitle: string; AFormat: IPTCFormat;
                             APages: Integer); overload;
 begin
   Open(ATitle, FDefaultWidth, FDefaultHeight, AFormat, APages);
 end;
 
 procedure TVESAConsole.Open(const ATitle: string; AWidth, AHeight: Integer;
-                            const AFormat: TPTCFormat; APages: Integer); overload;
-var
-  m: TPTCMode;
+                            AFormat: IPTCFormat; APages: Integer); overload;
 begin
-  m := TPTCMode.Create(AWidth, AHeight, AFormat);
-  try
-    Open(ATitle, m, APages);
-  finally
-    m.Free;
-  end;
+  Open(ATitle, TPTCMode.Create(AWidth, AHeight, AFormat), APages);
 end;
 
-procedure TVESAConsole.Open(const ATitle: string; const AMode: TPTCMode;
+procedure TVESAConsole.Open(const ATitle: string; AMode: IPTCMode;
                             APages: Integer); overload;
 
   procedure SetRGB332Palette;
@@ -556,7 +515,6 @@ procedure TVESAConsole.Open(const ATitle: string; const AMode: TPTCMode;
 
 var
   ModeFound: Integer;
-  tmpa: TPTCArea;
 begin
   ModeFound := FindBestMode(AMode);
 
@@ -592,7 +550,7 @@ begin
   
   FVESACurrentMode := FModesN[ModeFound].Index;
 
-  with FModes[FCurrentMode].FFormat do
+  with FModes[FCurrentMode].Format do
     if (Bits = 8) and Direct and (R = $E0) and (G = $1C) and (B = $03) then
       SetRGB332Palette;
 
@@ -622,13 +580,8 @@ begin
     else
       FNextVideoPage := 0;
   end;
-  tmpa := TPTCArea.Create(0, 0, FWidth, FHeight);
-  try
-    FArea.Assign(tmpa);
-    FClip.Assign(tmpa);
-  finally
-    tmpa.Free;
-  end;
+  FArea := TPTCArea.Create(0, 0, FWidth, FHeight);
+  FClip := FArea;
 
   FLFBNearPtrAccessAvailable := LFBNearPtrAccessAvailable;
   if not FLFBNearPtrAccessAvailable then
@@ -710,12 +663,12 @@ begin
   end;
 end;
 
-procedure TVESAConsole.Update(const AArea: TPTCArea);
+procedure TVESAConsole.Update(AArea: IPTCArea);
 begin
   Update;
 end;
 
-procedure TVESAConsole.Copy(ASurface: TPTCBaseSurface);
+procedure TVESAConsole.Copy(ASurface: IPTCSurface);
 var
   Pixels: Pointer;
 begin
@@ -734,8 +687,8 @@ begin
   end;
 end;
 
-procedure TVESAConsole.Copy(ASurface: TPTCBaseSurface;
-                            const ASource, ADestination: TPTCArea);
+procedure TVESAConsole.Copy(ASurface: IPTCSurface;
+                            ASource, ADestination: IPTCArea);
 var
   pixels: Pointer;
 begin
@@ -780,10 +733,9 @@ end;
 
 procedure TVESAConsole.Load(const APixels: Pointer;
                             AWidth, AHeight, APitch: Integer;
-                            const AFormat: TPTCFormat;
-                            const APalette: TPTCPalette);
+                            AFormat: IPTCFormat;
+                            APalette: IPTCPalette);
 var
-  Area_: TPTCArea;
   ConsolePixels: Pointer;
 begin
   check_open;
@@ -808,66 +760,44 @@ begin
     end;
   end
   else
-  begin
-    Area_ := TPTCArea.Create(0, 0, Width, Height);
-    try
-      Load(APixels, AWidth, AHeight, APitch, AFormat, APalette, Area_, Area);
-    finally
-      Area_.Free;
-    end;
-  end;
+    Load(APixels, AWidth, AHeight, APitch, AFormat, APalette, TPTCArea.Create(0, 0, Width, Height), Area);
 end;
 
 procedure TVESAConsole.Load(const APixels: Pointer;
                             AWidth, AHeight, APitch: Integer;
-                            const AFormat: TPTCFormat;
-                            const APalette: TPTCPalette;
-                            const ASource, ADestination: TPTCArea);
+                            AFormat: IPTCFormat;
+                            APalette: IPTCPalette;
+                            ASource, ADestination: IPTCArea);
 var
   ConsolePixels: Pointer;
-  ClippedSource: TPTCArea = nil;
-  ClippedDestination: TPTCArea = nil;
-  tmp: TPTCArea;
+  ClippedSource, ClippedDestination: IPTCArea;
 begin
   check_open;
   check_unlocked;
+  ConsolePixels := Lock;
   try
-    ClippedSource := TPTCArea.Create;
-    ClippedDestination := TPTCArea.Create;
-    ConsolePixels := Lock;
     try
-      try
-        tmp := TPTCArea.Create(0, 0, AWidth, AHeight);
-        try
-          TPTCClipper.Clip(ASource, tmp, ClippedSource, ADestination, Clip, ClippedDestination);
-        finally
-          tmp.Free;
-        end;
-        FCopy.Request(AFormat, Format);
-        FCopy.Palette(APalette, Palette);
-        FCopy.Copy(APixels, ClippedSource.Left, ClippedSource.Top, ClippedSource.Width, ClippedSource.Height, APitch,
-                    ConsolePixels, ClippedDestination.Left, ClippedDestination.Top, ClippedDestination.Width, ClippedDestination.Height, Pitch);
-      except
-        on error:TPTCError do
-        begin
-          raise TPTCError.Create('failed to load pixels to console area', error);
-        end;
+      TPTCClipper.Clip(ASource, TPTCArea.Create(0, 0, AWidth, AHeight), ClippedSource, ADestination, Clip, ClippedDestination);
+      FCopy.Request(AFormat, Format);
+      FCopy.Palette(APalette, Palette);
+      FCopy.Copy(APixels, ClippedSource.Left, ClippedSource.Top, ClippedSource.Width, ClippedSource.Height, APitch,
+                 ConsolePixels, ClippedDestination.Left, ClippedDestination.Top, ClippedDestination.Width, ClippedDestination.Height, Pitch);
+    except
+      on error:TPTCError do
+      begin
+        raise TPTCError.Create('failed to load pixels to console area', error);
       end;
-    finally
-      Unlock;
     end;
   finally
-    ClippedSource.Free;
-    ClippedDestination.Free;
+    Unlock;
   end;
 end;
 
 procedure TVESAConsole.Save(APixels: Pointer;
                             AWidth, AHeight, APitch: Integer;
-                            const AFormat: TPTCFormat;
-                            const APalette: TPTCPalette);
+                            AFormat: IPTCFormat;
+                            APalette: IPTCPalette);
 var
-  Area_: TPTCArea;
   ConsolePixels: Pointer;
 begin
   check_open;
@@ -892,100 +822,70 @@ begin
     end;
   end
   else
-  begin
-    Area_ := TPTCArea.Create(0, 0, Width, Height);
-    try
-      Save(APixels, AWidth, AHeight, APitch, AFormat, APalette, Area, Area_);
-    finally
-      Area_.Free;
-    end;
-  end;
+    Save(APixels, AWidth, AHeight, APitch, AFormat, APalette, Area, TPTCArea.Create(0, 0, Width, Height));
 end;
 
 procedure TVESAConsole.Save(APixels: Pointer;
                             AWidth, AHeight, APitch: Integer;
-                            const AFormat: TPTCFormat;
-                            const APalette: TPTCPalette;
-                            const ASource, ADestination: TPTCArea);
+                            AFormat: IPTCFormat;
+                            APalette: IPTCPalette;
+                            ASource, ADestination: IPTCArea);
 var
   ConsolePixels: Pointer;
-  ClippedSource: TPTCArea = nil;
-  ClippedDestination: TPTCArea = nil;
-  tmp: TPTCArea;
+  ClippedSource, ClippedDestination: IPTCArea;
 begin
   check_open;
   check_unlocked;
+  ConsolePixels := Lock;
   try
-    ClippedSource := TPTCArea.Create;
-    ClippedDestination := TPTCArea.Create;
-    ConsolePixels := Lock;
     try
-      try
-        tmp := TPTCArea.Create(0, 0, AWidth, AHeight);
-        try
-          TPTCClipper.Clip(ASource, Clip, ClippedSource, ADestination, tmp, ClippedDestination);
-        finally
-          tmp.Free;
-        end;
-        FCopy.Request(Format, AFormat);
-        FCopy.Palette(Palette, APalette);
-        FCopy.Copy(ConsolePixels, ClippedSource.Left, ClippedSource.Top, ClippedSource.Width, ClippedSource.Height, Pitch,
-                    APixels, ClippedDestination.Left, ClippedDestination.Top, ClippedDestination.Width, ClippedDestination.Height, APitch);
-      except
-        on error:TPTCError do
-        begin
-          raise TPTCError.Create('failed to save console area pixels', error);
-        end;
+      TPTCClipper.Clip(ASource, Clip, ClippedSource, ADestination, TPTCArea.Create(0, 0, AWidth, AHeight), ClippedDestination);
+      FCopy.Request(Format, AFormat);
+      FCopy.Palette(Palette, APalette);
+      FCopy.Copy(ConsolePixels, ClippedSource.Left, ClippedSource.Top, ClippedSource.Width, ClippedSource.Height, Pitch,
+                 APixels, ClippedDestination.Left, ClippedDestination.Top, ClippedDestination.Width, ClippedDestination.Height, APitch);
+    except
+      on error:TPTCError do
+      begin
+        raise TPTCError.Create('failed to save console area pixels', error);
       end;
-    finally
-      Unlock;
     end;
   finally
-    ClippedSource.Free;
-    ClippedDestination.Free;
+    Unlock;
   end;
 end;
 
 procedure TVESAConsole.Clear;
 var
-  tmp: TPTCColor;
+  Color: IPTCColor;
 begin
   check_open;
   check_unlocked;
   if Format.Direct then
-    tmp := TPTCColor.Create(0, 0, 0, 0)
+    Color := TPTCColor.Create(0, 0, 0, 0)
   else
-    tmp := TPTCColor.Create(0);
-  try
-    Clear(tmp);
-  finally
-    tmp.Free;
-  end;
+    Color := TPTCColor.Create(0);
+  Clear(Color);
 end;
 
-procedure TVESAConsole.Clear(const AColor: TPTCColor);
+procedure TVESAConsole.Clear(AColor: IPTCColor);
 var
   tmp: TPTCArea;
 begin
   check_open;
   check_unlocked;
-  tmp := TPTCArea.Create;
-  try
-    Clear(AColor, tmp);
-  finally
-    tmp.Free;
-  end;
+  Clear(AColor, TPTCArea.Create);
 end;
 
-procedure TVESAConsole.Clear(const AColor: TPTCColor;
-                             const AArea: TPTCArea);
+procedure TVESAConsole.Clear(AColor: IPTCColor;
+                             AArea: IPTCArea);
 begin
   check_open;
   check_unlocked;
   {...}
 end;
 
-procedure TVESAConsole.Palette(const APalette: TPTCPalette);
+procedure TVESAConsole.Palette(APalette: IPTCPalette);
 begin
   check_open;
 
@@ -996,23 +896,18 @@ begin
   end;
 end;
 
-function TVESAConsole.Palette: TPTCPalette;
+function TVESAConsole.Palette: IPTCPalette;
 begin
   check_open;
   Result := FPalette;
 end;
 
-procedure TVESAConsole.Clip(const AArea: TPTCArea);
+procedure TVESAConsole.Clip(AArea: IPTCArea);
 var
   tmp: TPTCArea;
 begin
   check_open;
-  tmp := TPTCClipper.Clip(AArea, FArea);
-  try
-    FClip.Assign(tmp);
-  finally
-    tmp.Free;
-  end;
+  FClip := TPTCClipper.Clip(AArea, FArea);
 end;
 
 function TVESAConsole.GetWidth: Integer;
@@ -1039,19 +934,19 @@ begin
   Result := 2;{FPrimary.pages;}
 end;
 
-function TVESAConsole.GetArea: TPTCArea;
+function TVESAConsole.GetArea: IPTCArea;
 begin
   check_open;
   Result := FArea;
 end;
 
-function TVESAConsole.Clip: TPTCArea;
+function TVESAConsole.Clip: IPTCArea;
 begin
   check_open;
   Result := FClip;
 end;
 
-function TVESAConsole.GetFormat: TPTCFormat;
+function TVESAConsole.GetFormat: IPTCFormat;
 begin
   check_open;
   Result := FModes[FCurrentMode].Format;
@@ -1087,11 +982,10 @@ begin
   FMouse.GetPendingEvents(FEventQueue);
 end;
 
-function TVESAConsole.NextEvent(var AEvent: TPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean;
+function TVESAConsole.NextEvent(out AEvent: IPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean;
 begin
   check_open;
 
-  FreeAndNil(AEvent);
   repeat
     { get events }
     HandleEvents;
@@ -1102,7 +996,7 @@ begin
   Result := AEvent <> nil;
 end;
 
-function TVESAConsole.PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): TPTCEvent;
+function TVESAConsole.PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): IPTCEvent;
 begin
   check_open;
 

+ 38 - 38
packages/ptc/src/dos/vga/vgaconsoled.inc

@@ -30,10 +30,10 @@
 }
 
 type
-  TVGAConsole = Class(TPTCBaseConsole)
+  TVGAConsole = class(TPTCBaseConsole)
   private
     { data }
-    m_modes: array [0..31{255}] of TPTCMode;
+    m_modes: array of IPTCMode;
     m_title: string;
     m_information: string;
     m_CurrentMode: Integer;
@@ -48,15 +48,15 @@ type
     { option data }
     m_default_width: Integer;
     m_default_height: Integer;
-    m_default_format: TPTCFormat;
+    m_default_format: IPTCFormat;
 
     { objects }
     m_copy: TPTCCopy;
-    m_area: TPTCArea;
-    m_clip: TPTCArea;
+    m_area: IPTCArea;
+    m_clip: IPTCArea;
 
     m_clear: TPTCClear;
-    m_palette: TPTCPalette;
+    m_palette: IPTCPalette;
 
     FEventQueue: TEventQueue;
 
@@ -65,7 +65,7 @@ type
     FMouse: TDosMouse;
 
     { internal console management routines }
-    procedure internal_pre_open_setup(const _title: String);
+    procedure internal_pre_open_setup(const _title: string);
     procedure internal_open_fullscreen_start;
     procedure internal_open_fullscreen(ModeType: Integer);
     procedure internal_open_fullscreen_finish(_pages: Integer);
@@ -85,61 +85,61 @@ type
   public
     constructor Create; override;
     destructor Destroy; override;
-    procedure Configure(const AFileName: String); override;
-    function option(const _option: String): Boolean; override;
-    function modes: PPTCMode; override;
+    procedure Configure(const AFileName: string); override;
+    function Option(const _option: string): Boolean; override;
+    function modes: TPTCModeList; override;
     procedure open(const _title: string; _pages: Integer); overload; override;
-    procedure open(const _title: string; const _format: TPTCFormat;
+    procedure open(const _title: string; _format: IPTCFormat;
                    _pages: Integer); overload; override;
     procedure open(const _title: string; _width, _height: Integer;
-                   const _format: TPTCFormat; _pages: Integer); overload; override;
-    procedure open(const _title: string; const _mode: TPTCMode;
+                   _format: IPTCFormat; _pages: Integer); overload; override;
+    procedure open(const _title: string; _mode: IPTCMode;
                    _pages: Integer); overload; override;
     procedure close; override;
     procedure flush; override;
     procedure finish; override;
     procedure update; override;
-    procedure update(const _area: TPTCArea); override;
-    procedure copy(surface: TPTCBaseSurface); override;
-    procedure copy(surface: TPTCBaseSurface;
-                   const source, destination: TPTCArea); override;
+    procedure update(_area: IPTCArea); override;
+    procedure copy(surface: IPTCSurface); override;
+    procedure copy(surface: IPTCSurface;
+                   source, destination: IPTCArea); override;
     function lock: Pointer; override;
     procedure unlock; override;
     procedure load(const pixels: Pointer;
                    _width, _height, _pitch: Integer;
-                   const _format: TPTCFormat;
-                   const _palette: TPTCPalette); override;
+                   _format: IPTCFormat;
+                   _palette: IPTCPalette); override;
     procedure load(const pixels: Pointer;
                    _width, _height, _pitch: Integer;
-                   const _format: TPTCFormat;
-                   const _palette: TPTCPalette;
-                   const source, destination: TPTCArea); override;
+                   _format: IPTCFormat;
+                   _palette: IPTCPalette;
+                   source, destination: IPTCArea); override;
     procedure save(pixels: Pointer;
                    _width, _height, _pitch: Integer;
-                   const _format: TPTCFormat;
-                   const _palette: TPTCPalette); override;
+                   _format: IPTCFormat;
+                   _palette: IPTCPalette); override;
     procedure save(pixels: Pointer;
                    _width, _height, _pitch: Integer;
-                   const _format: TPTCFormat;
-                   const _palette: TPTCPalette;
-                   const source, destination: TPTCArea); override;
+                   _format: IPTCFormat;
+                   _palette: IPTCPalette;
+                   source, destination: IPTCArea); override;
     procedure clear; override;
-    procedure clear(const color: TPTCColor); override;
-    procedure clear(const color: TPTCColor;
-                    const _area: TPTCArea); override;
-    procedure Palette(const _palette: TPTCPalette); override;
-    function Palette: TPTCPalette; override;
-    procedure Clip(const _area: TPTCArea); override;
+    procedure Clear(AColor: IPTCColor); override;
+    procedure clear(color: IPTCColor;
+                    _area: IPTCArea); override;
+    procedure Palette(_palette: IPTCPalette); override;
+    function Palette: IPTCPalette; override;
+    procedure Clip(_area: IPTCArea); override;
     function GetWidth: Integer; override;
     function GetHeight: Integer; override;
     function GetPitch: Integer; override;
     function GetPages: Integer; override;
-    function GetArea: TPTCArea; override;
-    function Clip: TPTCArea; override;
-    function GetFormat: TPTCFormat; override;
+    function GetArea: IPTCArea; override;
+    function Clip: IPTCArea; override;
+    function GetFormat: IPTCFormat; override;
     function GetName: string; override;
     function GetTitle: string; override;
     function GetInformation: string; override;
-    function NextEvent(var event: TPTCEvent; wait: Boolean; const EventMask: TPTCEventMask): Boolean; override;
-    function PeekEvent(wait: Boolean; const EventMask: TPTCEventMask): TPTCEvent; override;
+    function NextEvent(out event: IPTCEvent; wait: Boolean; const EventMask: TPTCEventMask): Boolean; override;
+    function PeekEvent(wait: Boolean; const EventMask: TPTCEventMask): IPTCEvent; override;
   end;

+ 60 - 234
packages/ptc/src/dos/vga/vgaconsolei.inc

@@ -38,10 +38,6 @@
 {$ASMMODE intel}
 
 constructor TVGAConsole.Create;
-
-var
-  fmt1, fmt2, fmt3: TPTCFormat;
-
 begin
   inherited Create;
 
@@ -61,29 +57,17 @@ begin
   m_clear := TPTCClear.Create;
   m_palette := TPTCPalette.Create;
 
-  fmt1 := nil;
-  fmt2 := nil;
-  fmt3 := nil;
-  try
-    fmt1 := TPTCFormat.Create(8);
-    fmt2 := TPTCFormat.Create(8, $E0, $1C, $03);
-    fmt3 := TPTCFormat.Create(16, $F800, $7E0, $1F);
-    m_modes[0] := TPTCMode.Create(320, 200, fmt1);
-    m_modes[1] := TPTCMode.Create(320, 200, fmt2);
-    m_modes[2] := TPTCMode.Create(320, 200, fmt3);
-    m_modes[3] := TPTCMode.Create;
-  finally
-    fmt1.Free;
-    fmt2.Free;
-    fmt3.Free;
-  end;
+  SetLength(m_modes, 3);
+  m_modes[0] := TPTCMode.Create(320, 200, TPTCFormat.Create(8));
+  m_modes[1] := TPTCMode.Create(320, 200, TPTCFormat.Create(8, $E0, $1C, $03));
+  m_modes[2] := TPTCMode.Create(320, 200, TPTCFormat.Create(16, $F800, $7E0, $1F));
+
   m_faketype := FAKEMODE2A;
 
   configure('ptcpas.cfg');
 end;
 
 destructor TVGAConsole.Destroy;
-
 begin
   close;
   internal_clear_mode_list;
@@ -92,19 +76,13 @@ begin
   FEventQueue.Free;
   m_copy.Free;
   m_clear.Free;
-  m_default_format.Free;
-  m_palette.Free;
-  m_clip.Free;
-  m_area.Free;
   inherited Destroy;
 end;
 
-procedure TVGAConsole.Configure(const AFileName: String);
-
+procedure TVGAConsole.Configure(const AFileName: string);
 var
-  F: Text;
+  F: TextFile;
   S: string;
-
 begin
   AssignFile(F, AFileName);
   {$push}{$I-}
@@ -124,8 +102,7 @@ begin
   CloseFile(F);
 end;
 
-function TVGAConsole.option(const _option: String): Boolean;
-
+function TVGAConsole.option(const _option: string): Boolean;
 begin
   {...}
   if (System.Copy(_option, 1, 8) = 'FAKEMODE') and (Length(_option) = 10) and
@@ -169,62 +146,37 @@ begin
 end;
 
 procedure TVGAConsole.internal_clear_mode_list;
-
-var
-  I: Integer;
-  Done: Boolean;
-
 begin
-  I := 0;
-  Done := False;
-  repeat
-    Done := not m_modes[I].valid;
-    m_modes[I].Free;
-    Inc(I);
-  until Done;
+  SetLength(m_modes, 0);
 end;
 
-function TVGAConsole.modes: PPTCMode;
-
+function TVGAConsole.modes: TPTCModeList;
 begin
   Result := m_modes;
 end;
 
 procedure TVGAConsole.open(const _title: string; _pages: Integer); overload;
-
 begin
   open(_title, m_default_format, _pages);
 end;
 
-procedure TVGAConsole.open(const _title: string; const _format: TPTCFormat;
+procedure TVGAConsole.open(const _title: string; _format: IPTCFormat;
                            _pages: Integer); overload;
-
 begin
   open(_title, m_default_width, m_default_height, _format, _pages);
 end;
 
 procedure TVGAConsole.open(const _title: string; _width, _height: Integer;
-                           const _format: TPTCFormat; _pages: Integer); overload;
-
-var
-  m: TPTCMode;
-
+                           _format: IPTCFormat; _pages: Integer); overload;
 begin
-  m := TPTCMode.Create(_width, _height, _format);
-  try
-    open(_title, m, _pages);
-  finally
-    m.Free;
-  end;
+  open(_title, TPTCMode.Create(_width, _height, _format), _pages);
 end;
 
-procedure TVGAConsole.open(const _title: string; const _mode: TPTCMode;
+procedure TVGAConsole.open(const _title: string; _mode: IPTCMode;
                            _pages: Integer); overload;
-
 var
   I: Integer;
   modetype: Integer;
-
 begin
   if not _mode.valid then
     raise TPTCError.Create('invalid mode');
@@ -243,7 +195,6 @@ begin
 end;
 
 procedure TVGAConsole.close;
-
 begin
   if m_open then
   begin
@@ -257,22 +208,19 @@ begin
 end;
 
 procedure TVGAConsole.flush;
-
 begin
   check_open;
   check_unlocked;
 end;
 
 procedure TVGAConsole.finish;
-
 begin
   check_open;
   check_unlocked;
 end;
 
-procedure TVGAConsole.vga_load(data: Pointer); ASSembler; Register;
-
-Asm
+procedure TVGAConsole.vga_load(data: Pointer); ASSembler; register;
+asm
   push es
   mov esi, data
   mov ax, fs
@@ -285,7 +233,6 @@ Asm
 end;
 
 procedure TVGAConsole.update;
-
 begin
   check_open;
   check_unlocked;
@@ -299,17 +246,14 @@ begin
   end;
 end;
 
-procedure TVGAConsole.update(const _area: TPTCArea);
-
+procedure TVGAConsole.update(_area: IPTCArea);
 begin
   update;
 end;
 
-procedure TVGAConsole.copy(surface: TPTCBaseSurface);
-
+procedure TVGAConsole.copy(surface: IPTCSurface);
 var
   pixels: Pointer;
-
 begin
   check_open;
   check_unlocked;
@@ -323,16 +267,13 @@ begin
   except
     on error: TPTCError do
       raise TPTCError.Create('failed to copy console to surface', error);
-
   end;
 end;
 
-procedure TVGAConsole.copy(surface: TPTCBaseSurface;
-                           const source, destination: TPTCArea);
-
+procedure TVGAConsole.copy(surface: IPTCSurface;
+                           source, destination: IPTCArea);
 var
   pixels: Pointer;
-
 begin
   check_open;
   check_unlocked;
@@ -346,12 +287,10 @@ begin
   except
     on error: TPTCError do
       raise TPTCError.Create('failed to copy console to surface', error);
-
   end;
 end;
 
 function TVGAConsole.lock: Pointer;
-
 begin
   check_open;
   if m_locked then
@@ -362,7 +301,6 @@ begin
 end;
 
 procedure TVGAConsole.unlock;
-
 begin
   check_open;
   if not m_locked then
@@ -371,14 +309,12 @@ begin
   m_locked := False;
 end;
 
-procedure TVGAConsole.load(const pixels: Pointer;
+procedure TVGAConsole.Load(const pixels: Pointer;
                            _width, _height, _pitch: Integer;
-                           const _format: TPTCFormat;
-                           const _palette: TPTCPalette);
+                           _format: IPTCFormat;
+                           _palette: IPTCPalette);
 var
-  Area_: TPTCArea;
   console_pixels: Pointer;
-
 begin
   check_open;
   check_unlocked;
@@ -397,70 +333,46 @@ begin
     except
       on error: TPTCError do
         raise TPTCError.Create('failed to load pixels to console', error);
-
     end;
   end
   else
-  begin
-    Area_ := TPTCArea.Create(0, 0, width, height);
-    try
-      load(pixels, _width, _height, _pitch, _format, _palette, Area_, area);
-    finally
-      Area_.Free;
-    end;
-  end;
+    Load(pixels, _width, _height, _pitch, _format, _palette, TPTCArea.Create(0, 0, width, height), area);
 end;
 
-procedure TVGAConsole.load(const pixels: Pointer;
+procedure TVGAConsole.Load(const pixels: Pointer;
                            _width, _height, _pitch: Integer;
-                           const _format: TPTCFormat;
-                           const _palette: TPTCPalette;
-                           const source, destination: TPTCArea);
+                           _format: IPTCFormat;
+                           _palette: IPTCPalette;
+                           source, destination: IPTCArea);
 var
   console_pixels: Pointer;
-  clipped_source, clipped_destination: TPTCArea;
-  tmp: TPTCArea;
-
+  clipped_source, clipped_destination: IPTCArea;
 begin
   check_open;
   check_unlocked;
-  clipped_source := nil;
-  clipped_destination := nil;
   try
     console_pixels := lock;
     try
-      clipped_source := TPTCArea.Create;
-      clipped_destination := TPTCArea.Create;
-      tmp := TPTCArea.Create(0, 0, _width, _height);
-      try
-        TPTCClipper.clip(source, tmp, clipped_source, destination, clip, clipped_destination);
-      finally
-        tmp.Free;
-      end;
+      TPTCClipper.clip(source, TPTCArea.Create(0, 0, _width, _height), clipped_source, destination, clip, clipped_destination);
       m_copy.request(_format, format);
       m_copy.palette(_palette, palette);
       m_copy.copy(pixels, clipped_source.left, clipped_source.top, clipped_source.width, clipped_source.height, _pitch,
                   console_pixels, clipped_destination.left, clipped_destination.top, clipped_destination.width, clipped_destination.height, pitch);
     finally
       unlock;
-      clipped_source.Free;
-      clipped_destination.Free;
     end;
   except
     on error:TPTCError do
       raise TPTCError.Create('failed to load pixels to console area', error);
-
   end;
 end;
 
 procedure TVGAConsole.save(pixels: Pointer;
                            _width, _height, _pitch: Integer;
-                           const _format: TPTCFormat;
-                           const _palette: TPTCPalette);
+                           _format: IPTCFormat;
+                           _palette: IPTCPalette);
 var
-  Area_: TPTCArea;
   console_pixels: Pointer;
-
 begin
   check_open;
   check_unlocked;
@@ -479,109 +391,69 @@ begin
     except
       on error: TPTCError do
         raise TPTCError.Create('failed to save console pixels', error);
-
     end;
   end
   else
-  begin
-    Area_ := TPTCArea.Create(0, 0, width, height);
-    try
-      save(pixels, _width, _height, _pitch, _format, _palette, area, Area_);
-    finally
-      Area_.Free;
-    end;
-  end;
+    Save(pixels, _width, _height, _pitch, _format, _palette, area, TPTCArea.Create(0, 0, width, height));
 end;
 
-procedure TVGAConsole.save(pixels: Pointer;
+procedure TVGAConsole.Save(pixels: Pointer;
                            _width, _height, _pitch: Integer;
-                           const _format: TPTCFormat;
-                           const _palette: TPTCPalette;
-                           const source, destination: TPTCArea);
+                           _format: IPTCFormat;
+                           _palette: IPTCPalette;
+                           source, destination: IPTCArea);
 var
   console_pixels: Pointer;
-  clipped_source, clipped_destination: TPTCArea;
-  tmp: TPTCArea;
-
+  clipped_source, clipped_destination: IPTCArea;
 begin
   check_open;
   check_unlocked;
-  clipped_source := nil;
-  clipped_destination := nil;
   try
     console_pixels := lock;
     try
-      clipped_source := TPTCArea.Create;
-      clipped_destination := TPTCArea.Create;
-      tmp := TPTCArea.Create(0, 0, _width, _height);
-      try
-        TPTCClipper.clip(source, clip, clipped_source, destination, tmp, clipped_destination);
-      finally
-        tmp.Free;
-      end;
+      TPTCClipper.clip(source, clip, clipped_source, destination, TPTCArea.Create(0, 0, _width, _height), clipped_destination);
       m_copy.request(format, _format);
       m_copy.palette(palette, _palette);
       m_copy.copy(console_pixels, clipped_source.left, clipped_source.top, clipped_source.width, clipped_source.height, pitch,
                   pixels, clipped_destination.left, clipped_destination.top, clipped_destination.width, clipped_destination.height, _pitch);
     finally
       unlock;
-      clipped_source.Free;
-      clipped_destination.Free;
     end;
   except
     on error:TPTCError do
       raise TPTCError.Create('failed to save console area pixels', error);
-
   end;
 end;
 
 procedure TVGAConsole.clear;
-
 var
-  tmp: TPTCColor;
-
+  Color: IPTCColor;
 begin
   check_open;
   check_unlocked;
   if format.direct then
-    tmp := TPTCColor.Create(0, 0, 0, 0)
+    Color := TPTCColor.Create(0, 0, 0, 0)
   else
-    tmp := TPTCColor.Create(0);
-  try
-    clear(tmp);
-  finally
-    tmp.Free;
-  end;
+    Color := TPTCColor.Create(0);
+  Clear(Color);
 end;
 
-procedure TVGAConsole.clear(const color: TPTCColor);
-
-var
-  tmp: TPTCArea;
-
+procedure TVGAConsole.clear(AColor: IPTCColor);
 begin
   check_open;
   check_unlocked;
-  tmp := TPTCArea.Create;
-  try
-    clear(color, tmp);
-  finally
-    tmp.Free;
-  end;
+  Clear(AColor, TPTCArea.Create);
 end;
 
-procedure TVGAConsole.clear(const color: TPTCColor;
-                            const _area: TPTCArea);
-
+procedure TVGAConsole.clear(color: IPTCColor;
+                            _area: IPTCArea);
 var
   pixels: Pointer;
-  clipped_area: TPTCArea;
-
+  clipped_area: IPTCArea;
 begin
   check_open;
   check_unlocked;
   try
-    clipped_area := nil;
     pixels := lock;
     try
       clipped_area := TPTCClipper.clip(_area, clip);
@@ -589,17 +461,14 @@ begin
       m_clear.clear(pixels, clipped_area.left, clipped_area.right, clipped_area.width, clipped_area.height, pitch, color);
     finally
       unlock;
-      clipped_area.Free;
     end;
   except
     on error: TPTCError do
       raise TPTCError.Create('failed to clear console area', error);
-
   end;
 end;
 
-procedure TVGAConsole.Palette(const _palette: TPTCPalette);
-
+procedure TVGAConsole.Palette(_palette: IPTCPalette);
 begin
   check_open;
   if format.indexed then
@@ -609,111 +478,85 @@ begin
   end;
 end;
 
-function TVGAConsole.Palette: TPTCPalette;
-
+function TVGAConsole.Palette: IPTCPalette;
 begin
   check_open;
   Result := m_palette;
 end;
 
-procedure TVGAConsole.Clip(const _area: TPTCArea);
-
-var
-  tmp: TPTCArea;
-
+procedure TVGAConsole.Clip(_area: IPTCArea);
 begin
   check_open;
-  tmp := TPTCClipper.clip(_area, m_area);
-  try
-    m_clip.Assign(tmp);
-  finally
-    tmp.Free;
-  end;
+  m_clip := TPTCClipper.clip(_area, m_area);
 end;
 
 function TVGAConsole.GetWidth: Integer;
-
 begin
   check_open;
   Result := m_width;
 end;
 
 function TVGAConsole.GetHeight: Integer;
-
 begin
   check_open;
   Result := m_height;
 end;
 
 function TVGAConsole.GetPitch: Integer;
-
 begin
   check_open;
   Result := m_pitch;
 end;
 
 function TVGAConsole.GetPages: Integer;
-
 begin
   check_open;
   Result := 2;
 end;
 
-function TVGAConsole.GetArea: TPTCArea;
-
+function TVGAConsole.GetArea: IPTCArea;
 begin
   check_open;
   Result := m_area;
 end;
 
-function TVGAConsole.Clip: TPTCArea;
-
+function TVGAConsole.Clip: IPTCArea;
 begin
   check_open;
   Result := m_clip;
 end;
 
-function TVGAConsole.GetFormat: TPTCFormat;
-
+function TVGAConsole.GetFormat: IPTCFormat;
 begin
   check_open;
   Result := m_modes[m_CurrentMode].format;
 end;
 
 function TVGAConsole.GetName: string;
-
 begin
   Result := 'VGA';
 end;
 
 function TVGAConsole.GetTitle: string;
-
 begin
   Result := m_title;
 end;
 
 function TVGAConsole.GetInformation: string;
-
 begin
   Result := m_information;
 end;
 
-procedure TVGAConsole.internal_pre_open_setup(const _title: String);
-
+procedure TVGAConsole.internal_pre_open_setup(const _title: string);
 begin
   m_title := _title;
 end;
 
 procedure TVGAConsole.internal_open_fullscreen_start;
-
 begin
 end;
 
 procedure TVGAConsole.internal_open_fullscreen(ModeType: Integer);
-
-var
-  tmp: TPTCArea;
-
 begin
   VGASetMode(320, 200, ModeType, m_faketype);
   case ModeType of
@@ -733,17 +576,11 @@ begin
   m_width := 320;
   m_height := 200;
 
-  tmp := TPTCArea.Create(0, 0, m_width, m_height);
-  try
-    m_area.Assign(tmp);
-    m_clip.Assign(tmp);
-  finally
-    tmp.Free;
-  end;
+  m_area := TPTCArea.Create(0, 0, m_width, m_height);
+  m_clip := m_area;
 end;
 
 procedure TVGAConsole.internal_open_fullscreen_finish(_pages: Integer);
-
 begin
   FreeMemAndNil(m_primary);
   m_primary := GetMem(m_height * m_pitch);
@@ -751,7 +588,6 @@ begin
 end;
 
 procedure TVGAConsole.internal_post_open_setup;
-
 begin
   FreeAndNil(m_keyboard);
   FreeAndNil(FMouse);
@@ -768,7 +604,6 @@ begin
 end;
 
 procedure TVGAConsole.internal_reset;
-
 begin
   FreeMemAndNil(m_primary);
   FreeAndNil(m_keyboard);
@@ -777,7 +612,6 @@ begin
 end;
 
 procedure TVGAConsole.internal_close;
-
 begin
   FreeMemAndNil(m_primary);
   FreeAndNil(m_keyboard);
@@ -788,11 +622,9 @@ begin
 end;
 
 procedure TVGAConsole.internal_SetPalette(data: PUint32);
-
 var
   i: Integer;
   c: DWord;
-
 begin
   outportb($3C8, 0);
   for i := 0 to 255 do
@@ -806,18 +638,15 @@ begin
 end;
 
 procedure TVGAConsole.HandleEvents;
-
 begin
   m_keyboard.GetPendingEvents(FEventQueue);
   FMouse.GetPendingEvents(FEventQueue);
 end;
 
-function TVGAConsole.NextEvent(var event: TPTCEvent; wait: Boolean; const EventMask: TPTCEventMask): Boolean;
-
+function TVGAConsole.NextEvent(out event: IPTCEvent; wait: Boolean; const EventMask: TPTCEventMask): Boolean;
 begin
   check_open;
 
-  FreeAndNil(event);
   repeat
     { get events }
     HandleEvents;
@@ -828,8 +657,7 @@ begin
   Result := event <> nil;
 end;
 
-function TVGAConsole.PeekEvent(wait: Boolean; const EventMask: TPTCEventMask): TPTCEvent;
-
+function TVGAConsole.PeekEvent(wait: Boolean; const EventMask: TPTCEventMask): IPTCEvent;
 begin
   check_open;
 
@@ -843,14 +671,12 @@ begin
 end;
 
 procedure TVGAConsole.check_open;
-
 begin
   if not m_open then
     raise TPTCError.Create('console is not open');
 end;
 
 procedure TVGAConsole.check_unlocked;
-
 begin
   if m_locked then
     raise TPTCError.Create('console is not unlocked');

+ 1 - 1
packages/ptc/src/ptc.pp

@@ -60,7 +60,7 @@ uses
 {$ENDIF FPDOC}
 
 const
-  PTCPAS_VERSION = 'PTCPas 0.99.11';
+  PTCPAS_VERSION = 'PTCPas 0.99.12';
 
 type
   PUint8  = ^Uint8;

+ 9 - 8
packages/ptc/src/ptcwrapper/ptceventqueue.pp

@@ -42,7 +42,7 @@ uses
 type
   PEventLinkedList = ^TEventLinkedList;
   TEventLinkedList = record
-    Event: TPTCEvent;
+    Event: IPTCEvent;
     Next: PEventLinkedList;
   end;
   TEventQueue = class
@@ -51,9 +51,9 @@ type
   public
     constructor Create;
     destructor Destroy; override;
-    procedure AddEvent(event: TPTCEvent);
-    function PeekEvent(const EventMask: TPTCEventMask): TPTCEvent;
-    function NextEvent(const EventMask: TPTCEventMask): TPTCEvent;
+    procedure AddEvent(const event: IPTCEvent);
+    function PeekEvent(const EventMask: TPTCEventMask): IPTCEvent;
+    function NextEvent(const EventMask: TPTCEventMask): IPTCEvent;
   end;
 
 implementation
@@ -71,7 +71,7 @@ begin
   p := FHead;
   while p <> nil do
   begin
-    FreeAndNil(p^.Event);
+    p^.Event := nil;
     pnext := p^.Next;
     Dispose(p);
     p := pnext;
@@ -79,7 +79,7 @@ begin
   inherited Destroy;
 end;
 
-procedure TEventQueue.AddEvent(event: TPTCEvent);
+procedure TEventQueue.AddEvent(const event: IPTCEvent);
 var
   tmp: PEventLinkedList;
 begin
@@ -100,7 +100,7 @@ begin
   end;
 end;
 
-function TEventQueue.PeekEvent(const EventMask: TPTCEventMask): TPTCEvent;
+function TEventQueue.PeekEvent(const EventMask: TPTCEventMask): IPTCEvent;
 var
   p: PEventLinkedList;
 begin
@@ -118,7 +118,7 @@ begin
   Result := nil;
 end;
 
-function TEventQueue.NextEvent(const EventMask: TPTCEventMask): TPTCEvent;
+function TEventQueue.NextEvent(const EventMask: TPTCEventMask): IPTCEvent;
 var
   prev, p: PEventLinkedList;
 begin
@@ -129,6 +129,7 @@ begin
     if p^.Event.EventType In EventMask then
     begin
       Result := p^.Event;
+      p^.Event := nil;
 
       { delete the element from the linked list }
       if prev <> nil then

+ 28 - 35
packages/ptc/src/ptcwrapper/ptcwrapper.pp

@@ -60,8 +60,8 @@ type
     Title: string;
     SurfaceWidth, SurfaceHeight: Integer;
     Width, Height: Integer;
-    Format: TPTCFormat;
-    Mode: TPTCMode;
+    Format: IPTCFormat;
+    Mode: IPTCMode;
     VirtualPages: Integer;
     Pages: Integer;
 
@@ -84,14 +84,14 @@ type
   TPTCWrapperGetModesRequest = record
     Processed: Boolean;
 
-    Result: PPTCMode;
+    Result: TPTCModeList;
   end;
 
   TPTCWrapperThread = class(TThread)
   private
-    FConsole: TPTCConsole;
-    FSurface: array of TPTCSurface;
-    FPalette: TPTCPalette;
+    FConsole: IPTCConsole;
+    FSurface: array of IPTCSurface;
+    FPalette: IPTCPalette;
     FSurfaceCriticalSection: TCriticalSection;
     FNeedsUpdate: Boolean;
     FPaletteNeedsUpdate: Boolean;
@@ -116,14 +116,14 @@ type
     destructor Destroy; override;
 
     procedure Open(const ATitle: string; AVirtualPages: Integer; APages: Integer = 0);
-    procedure Open(const ATitle: string; AFormat: TPTCFormat; AVirtualPages: Integer; APages: Integer = 0);
-    procedure Open(const ATitle: string; ASurfaceWidth, ASurfaceHeight, AWidth, AHeight: Integer; AFormat: TPTCFormat; AVirtualPages: Integer; APages: Integer = 0);
-    procedure Open(const ATitle: string; ASurfaceWidth, ASurfaceHeight: Integer; AMode: TPTCMode; AVirtualPages: Integer; APages: Integer = 0);
+    procedure Open(const ATitle: string; AFormat: IPTCFormat; AVirtualPages: Integer; APages: Integer = 0);
+    procedure Open(const ATitle: string; ASurfaceWidth, ASurfaceHeight, AWidth, AHeight: Integer; AFormat: IPTCFormat; AVirtualPages: Integer; APages: Integer = 0);
+    procedure Open(const ATitle: string; ASurfaceWidth, ASurfaceHeight: Integer; AMode: IPTCMode; AVirtualPages: Integer; APages: Integer = 0);
     procedure Close;
 
     function Option(const AOption: string): Boolean;
 
-    function Modes: PPTCMode;
+    function Modes: TPTCModeList;
 
     procedure SetVisualPage(AVisualPage: Integer);
 
@@ -133,8 +133,8 @@ type
     function PaletteLock: Pointer;
     procedure PaletteUnlock;
 
-    function NextEvent(var AEvent: TPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean;
-    function PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): TPTCEvent;
+    function NextEvent(out AEvent: IPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean;
+    function PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): IPTCEvent;
 
     property IsOpen: Boolean read FOpen;
   end;
@@ -174,16 +174,13 @@ end;
 procedure TPTCWrapperThread.Execute;
   procedure GetEvents;
   var
-    Event: TPTCEvent = nil;
+    Event: IPTCEvent;
     NextEventAvailable: Boolean;
   begin
     repeat
       NextEventAvailable := FConsole.NextEvent(Event, False, PTCAnyEvent);
       if NextEventAvailable then
-      begin
         FEventQueue.AddEvent(Event);
-        Event := nil;
-      end;
     until not NextEventAvailable;
   end;
 
@@ -194,7 +191,7 @@ procedure TPTCWrapperThread.Execute;
     if not FOpenRequest.Processed then
     begin
       for I := Low(FSurface) to High(FSurface) do
-        FreeAndNil(FSurface[I]);
+        FSurface[I] := nil;
       with FOpenRequest do
       begin
         SetLength(FSurface, VirtualPages);
@@ -203,25 +200,25 @@ procedure TPTCWrapperThread.Execute;
             begin
               FConsole.Open(Title, Pages);
               for I := Low(FSurface) to High(FSurface) do
-                FSurface[I] := TPTCSurface.Create(FConsole.Width, FConsole.Height, FConsole.Format);
+                FSurface[I] := TPTCSurfaceFactory.CreateNew(FConsole.Width, FConsole.Height, FConsole.Format);
             end;
           pwotFormat:
             begin
               FConsole.Open(Title, Format, Pages);
               for I := Low(FSurface) to High(FSurface) do
-                FSurface[I] := TPTCSurface.Create(FConsole.Width, FConsole.Height, Format);
+                FSurface[I] := TPTCSurfaceFactory.CreateNew(FConsole.Width, FConsole.Height, Format);
             end;
           pwotWidthHeightFormat:
             begin
               FConsole.Open(Title, Width, Height, Format, Pages);
               for I := Low(FSurface) to High(FSurface) do
-                FSurface[I] := TPTCSurface.Create(SurfaceWidth, SurfaceHeight, Format);
+                FSurface[I] := TPTCSurfaceFactory.CreateNew(SurfaceWidth, SurfaceHeight, Format);
             end;
           pwotMode:
             begin
               FConsole.Open(Title, Mode, Pages);
               for I := Low(FSurface) to High(FSurface) do
-                FSurface[I] := TPTCSurface.Create(SurfaceWidth, SurfaceHeight, Mode.Format);
+                FSurface[I] := TPTCSurfaceFactory.CreateNew(SurfaceWidth, SurfaceHeight, Mode.Format);
             end;
         end;
       end;
@@ -241,7 +238,7 @@ procedure TPTCWrapperThread.Execute;
     begin
       FConsole.Close;
       for I := Low(FSurface) to High(FSurface) do
-        FreeAndNil(FSurface[I]);
+        FSurface[I] := nil;
       SetLength(FSurface, 0);
       SetLength(FPixels, 0);
       FOpen := False;
@@ -266,10 +263,10 @@ var
   I: Integer;
 begin
   try
-    FConsole := TPTCConsole.Create;
+    FConsole := TPTCConsoleFactory.CreateNew;
 
     FEventQueue := TEventQueue.Create;
-    FPalette := TPTCPalette.Create;
+    FPalette := TPTCPaletteFactory.CreateNew;
     FPaletteData := FPalette.Data;
 
     FOpen := False;
@@ -312,11 +309,8 @@ begin
     if Assigned(FConsole) then
       FConsole.Close;
 
-    for I := Low(FSurface) to High(FSurface) do
-      FreeAndNil(FSurface[I]);
     SetLength(FSurface, 0);
-    FreeAndNil(FPalette);
-    FreeAndNil(FConsole);
+    FConsole := nil;
   end;
 end;
 
@@ -341,7 +335,7 @@ begin
   until FOpenRequest.Processed;
 end;
 
-procedure TPTCWrapperThread.Open(const ATitle: string; AFormat: TPTCFormat; AVirtualPages: Integer; APages: Integer = 0);
+procedure TPTCWrapperThread.Open(const ATitle: string; AFormat: IPTCFormat; AVirtualPages: Integer; APages: Integer = 0);
 begin
   FSurfaceCriticalSection.Acquire;
   try
@@ -363,7 +357,7 @@ begin
   until FOpenRequest.Processed;
 end;
 
-procedure TPTCWrapperThread.Open(const ATitle: string; ASurfaceWidth, ASurfaceHeight, AWidth, AHeight: Integer; AFormat: TPTCFormat; AVirtualPages: Integer; APages: Integer = 0);
+procedure TPTCWrapperThread.Open(const ATitle: string; ASurfaceWidth, ASurfaceHeight, AWidth, AHeight: Integer; AFormat: IPTCFormat; AVirtualPages: Integer; APages: Integer = 0);
 begin
   FSurfaceCriticalSection.Acquire;
   try
@@ -389,7 +383,7 @@ begin
   until FOpenRequest.Processed;
 end;
 
-procedure TPTCWrapperThread.Open(const ATitle: string; ASurfaceWidth, ASurfaceHeight: Integer; AMode: TPTCMode; AVirtualPages: Integer; APages: Integer = 0);
+procedure TPTCWrapperThread.Open(const ATitle: string; ASurfaceWidth, ASurfaceHeight: Integer; AMode: IPTCMode; AVirtualPages: Integer; APages: Integer = 0);
 begin
   FSurfaceCriticalSection.Acquire;
   try
@@ -449,7 +443,7 @@ begin
   Result := FOptionRequest.Result;
 end;
 
-function TPTCWrapperThread.Modes: PPTCMode;
+function TPTCWrapperThread.Modes: TPTCModeList;
 begin
   FSurfaceCriticalSection.Acquire;
   try
@@ -507,9 +501,8 @@ begin
   FSurfaceCriticalSection.Release;
 end;
 
-function TPTCWrapperThread.NextEvent(var AEvent: TPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean;
+function TPTCWrapperThread.NextEvent(out AEvent: IPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean;
 begin
-  FreeAndNil(AEvent);
   repeat
     ThreadSwitch;
 
@@ -520,7 +513,7 @@ begin
   Result := AEvent <> nil;
 end;
 
-function TPTCWrapperThread.PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): TPTCEvent;
+function TPTCWrapperThread.PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): IPTCEvent;
 begin
   repeat
     ThreadSwitch;

+ 2 - 5
packages/ptc/src/win32/base/kbd.inc

@@ -88,12 +88,9 @@ begin
     exit;
 
   { process key message }
-  if (message = WM_KEYDOWN) or (message = WM_KEYUP) {Or ((message = WM_SYSKEYDOWN) and ((lParam and (1 shl 29)) <> 0))} then
+  if (message = WM_KEYDOWN) or (message = WM_KEYUP) or (message = WM_SYSKEYDOWN) or (message = WM_SYSKEYUP) then
   begin
-    if message = WM_KEYUP then
-      press := False
-    else
-      press := True;
+    press := (message = WM_KEYDOWN) or (message = WM_SYSKEYDOWN);
 
     { update modifiers }
     if wParam = VK_MENU then

+ 16 - 0
packages/ptc/src/win32/base/window.inc

@@ -165,6 +165,14 @@ end;
 function WndProcSingleThreaded(hWnd: HWND; message: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
 begin
   case message of
+    WM_SYSCOMMAND:
+      begin
+        { this fixes the pausing of the application when the Alt or F10 key is pressed }
+        if wParam = SC_KEYMENU then
+	  Result := 0
+	else
+          Result := DefWindowProc(hWnd, message, wParam, lParam);
+      end;
     WM_SETCURSOR: begin
       {$WARNING GCLP_HCURSOR not defined in windows unit}
       if (LOWORD(lParam) = HTCLIENT) and (GetClassLongPtr(hWnd, {GCLP_HCURSOR}GCL_HCURSOR) = 0) then
@@ -188,6 +196,14 @@ function WndProcMultiThreaded(hWnd: HWND; message: UINT; wParam: WPARAM; lParam:
 begin
   Result := 0;
   case message of
+    WM_SYSCOMMAND:
+      begin
+        { this fixes the pausing of the application when the Alt or F10 key is pressed }
+        if wParam = SC_KEYMENU then
+	  Result := 0
+	else
+          Result := DefWindowProc(hWnd, message, wParam, lParam);
+      end;
     WM_SETCURSOR: begin
       {$WARNING GCLP_HCURSOR not defined in windows unit}
       if (LOWORD(lParam) = HTCLIENT) and (GetClassLongPtr(hWnd, {GCLP_HCURSOR}GCL_HCURSOR) = 0) then

+ 40 - 40
packages/ptc/src/win32/directx/directxconsoled.inc

@@ -51,7 +51,7 @@ type
 //    FDefaultPages: Integer;
     FCenterWindow: Boolean;
     FSynchronizedUpdate: Boolean;
-    FDefaultFormat: TPTCFormat;
+    FDefaultFormat: IPTCFormat;
     FOutputMode: (DEFAULT, WINDOWED, FULLSCREEN); {Output}
     FWindowMode: (RESIZABLE, FIXED); {Window}
     FPrimaryModeWindowed: TPrimaryModeEnum; {Primary}
@@ -77,8 +77,8 @@ type
     FPrimary: TDirectXPrimary;
 
     { internal console management routines }
-    procedure internal_open(const _title: string; window: HWND; const mode: TPTCMode; _pages: Integer; exact: Boolean);
-    procedure internal_recycle(const _title: string; window: HWND; const mode: TPTCMode; _pages: Integer; exact: Boolean);
+    procedure internal_open(const _title: string; window: HWND; const mode: IPTCMode; _pages: Integer; exact: Boolean);
+    procedure internal_recycle(const _title: string; window: HWND; const mode: IPTCMode; _pages: Integer; exact: Boolean);
     procedure internal_close;
     procedure internal_shutdown;
 
@@ -88,20 +88,20 @@ type
     procedure internal_open_reset;
 
     { internal fullscreen open routines }
-    procedure internal_open_fullscreen_start(window: HWND; const mode: TPTCMode; exact: Boolean);
-    procedure internal_open_fullscreen_change(const mode: TPTCMode; exact: Boolean);
-    procedure internal_open_fullscreen_surface(const mode: TPTCMode; _pages: Integer);
+    procedure internal_open_fullscreen_start(window: HWND; const mode: IPTCMode; exact: Boolean);
+    procedure internal_open_fullscreen_change(const mode: IPTCMode; exact: Boolean);
+    procedure internal_open_fullscreen_surface(const mode: IPTCMode; _pages: Integer);
     procedure internal_open_fullscreen_finish;
 
     { internal windowed open routines }
-    procedure internal_open_windowed_start(window: HWND; const mode: TPTCMode; exact: Boolean);
-    procedure internal_open_windowed_change(const mode: TPTCMode; exact: Boolean);
-    procedure internal_open_windowed_surface(const mode: TPTCMode; _pages: Integer);
+    procedure internal_open_windowed_start(window: HWND; const mode: IPTCMode; exact: Boolean);
+    procedure internal_open_windowed_change(const mode: IPTCMode; exact: Boolean);
+    procedure internal_open_windowed_surface(const mode: IPTCMode; _pages: Integer);
     procedure internal_open_windowed_finish;
 
     { internal console open recycling routines }
-    procedure internal_recycle_fullscreen(const _title: string; window: HWND; const mode: TPTCMode; _pages: Integer; exact: Boolean);
-    procedure internal_recycle_windowed(const _title: string; window: HWND; const mode: TPTCMode; _pages: Integer; exact: Boolean);
+    procedure internal_recycle_fullscreen(const _title: string; window: HWND; const mode: IPTCMode; _pages: Integer; exact: Boolean);
+    procedure internal_recycle_windowed(const _title: string; window: HWND; const mode: IPTCMode; _pages: Integer; exact: Boolean);
 
 {$IFDEF DEBUG}
     { debug }
@@ -119,59 +119,59 @@ type
     destructor Destroy; override;
     procedure Configure(const AFileName: String); override;
     function Option(const AOption: String): Boolean; override;
-    function Modes: PPTCMode; override;
+    function Modes: TPTCModeList; override;
     procedure Open(const ATitle: string; APages: Integer = 0); overload; override;
-    procedure Open(const ATitle: string; const AFormat: TPTCFormat;
+    procedure Open(const ATitle: string; AFormat: IPTCFormat;
                    APages: Integer = 0); overload; override;
     procedure Open(const ATitle: string; AWidth, AHeight: Integer;
-                   const AFormat: TPTCFormat; APages: Integer = 0); overload; override;
-    procedure Open(const ATitle: string; const AMode: TPTCMode;
+                   AFormat: IPTCFormat; APages: Integer = 0); overload; override;
+    procedure Open(const ATitle: string; AMode: IPTCMode;
                    APages: Integer = 0); overload; override;
     procedure Close; override;
     procedure Flush; override;
     procedure Finish; override;
     procedure Update; override;
-    procedure Update(const AArea: TPTCArea); override;
-    procedure Copy(ASurface: TPTCBaseSurface); override;
-    procedure Copy(ASurface: TPTCBaseSurface;
-                   const ASource, ADestination: TPTCArea); override;
+    procedure Update(AArea: IPTCArea); override;
+    procedure Copy(ASurface: IPTCSurface); override;
+    procedure Copy(ASurface: IPTCSurface;
+                   ASource, ADestination: IPTCArea); override;
     function Lock: Pointer; override;
     procedure Unlock; override;
     procedure Load(const APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette); override;
     procedure Load(const APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette;
-                   const ASource, ADestination: TPTCArea); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette;
+                   ASource, ADestination: IPTCArea); override;
     procedure Save(APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette); override;
     procedure Save(APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette;
-                   const ASource, ADestination: TPTCArea); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette;
+                   ASource, ADestination: IPTCArea); override;
     procedure Clear; override;
-    procedure Clear(const AColor: TPTCColor); override;
-    procedure Clear(const AColor: TPTCColor;
-                    const AArea: TPTCArea); override;
-    procedure Palette(const APalette: TPTCPalette); override;
-    function Palette: TPTCPalette; override;
-    procedure Clip(const AArea: TPTCArea); override;
+    procedure Clear(AColor: IPTCColor); override;
+    procedure Clear(AColor: IPTCColor;
+                    AArea: IPTCArea); override;
+    procedure Palette(APalette: IPTCPalette); override;
+    function Palette: IPTCPalette; override;
+    procedure Clip(AArea: IPTCArea); override;
     function GetWidth: Integer; override;
     function GetHeight: Integer; override;
     function GetPitch: Integer; override;
     function GetPages: Integer; override;
-    function GetArea: TPTCArea; override;
-    function Clip: TPTCArea; override;
-    function GetFormat: TPTCFormat; override;
+    function GetArea: IPTCArea; override;
+    function Clip: IPTCArea; override;
+    function GetFormat: IPTCFormat; override;
     function GetName: string; override;
     function GetTitle: string; override;
     function GetInformation: string; override;
-    function NextEvent(var AEvent: TPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean; override;
-    function PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): TPTCEvent; override;
+    function NextEvent(out AEvent: IPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean; override;
+    function PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): IPTCEvent; override;
   end;

+ 81 - 151
packages/ptc/src/win32/directx/directxconsolei.inc

@@ -135,7 +135,6 @@ begin
   FEventQueue.Free;
   FWin32Cursor.Free;
   FCopy.Free;
-  FDefaultFormat.Free;
   inherited Destroy;
 end;
 
@@ -165,7 +164,6 @@ end;
 function TDirectXConsole.Option(const AOption: String): Boolean;
 var
   tmp, tmp2: Integer;
-  tmpformat: TPTCFormat;
 begin
   LOG('console option', AOption);
   Result := True;
@@ -216,27 +214,17 @@ begin
     begin
       Val(System.Copy(AOption, 13, Length(AOption)-12), tmp, tmp2);
       case tmp of
-        8: tmpformat := TPTCFormat.Create(8);
-        16: tmpformat := TPTCFormat.Create(16, $F800, $07E0, $001F);
-        24: tmpformat := TPTCFormat.Create(24, $00FF0000, $0000FF00, $000000FF);
-        32: tmpformat := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
+        8: FDefaultFormat := TPTCFormat.Create(8);
+        16: FDefaultFormat := TPTCFormat.Create(16, $F800, $07E0, $001F);
+        24: FDefaultFormat := TPTCFormat.Create(24, $00FF0000, $0000FF00, $000000FF);
+        32: FDefaultFormat := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
         else
           exit(False);
       end;
-      try
-        FDefaultFormat.Assign(tmpformat);
-      finally
-        tmpformat.Free;
-      end;
     end
     else
     begin
-      tmpformat := DEFAULT_FORMAT;
-      try
-        FDefaultFormat.Assign(tmpformat);
-      finally
-        tmpformat.Free;
-      end;
+      FDefaultFormat := DEFAULT_FORMAT;
     end;
   end;
   if AOption = 'resizable window' then
@@ -393,7 +381,7 @@ begin
   Result := FCopy.Option(AOption);
 end;
 
-function TDirectXConsole.Modes: PPTCMode;
+function TDirectXConsole.Modes: TPTCModeList;
 begin
   Result := FDisplay.Modes;
 end;
@@ -403,27 +391,23 @@ begin
   Open(ATitle, FDefaultFormat, APages);
 end;
 
-procedure TDirectXConsole.Open(const ATitle: string; const AFormat: TPTCFormat;
+procedure TDirectXConsole.Open(const ATitle: string; AFormat: IPTCFormat;
                            APages: Integer = 0);
 begin
   Open(ATitle, FDefaultWidth, FDefaultHeight, AFormat, APages);
 end;
 
 procedure TDirectXConsole.Open(const ATitle: string; AWidth, AHeight: Integer;
-                           const AFormat: TPTCFormat; APages: Integer = 0);
+                           AFormat: IPTCFormat; APages: Integer = 0);
 var
-  m: TPTCMode;
+  mode: IPTCMode;
 begin
   { internal open nearest mode }
-  m := TPTCMode.Create(AWidth, AHeight, AFormat);
-  try
-    internal_open(ATitle, 0, m, APages, False);
-  finally
-    m.Free;
-  end;
+  mode := TPTCMode.Create(AWidth, AHeight, AFormat);
+  internal_open(ATitle, 0, mode, APages, False);
 end;
 
-procedure TDirectXConsole.Open(const ATitle: string; const AMode: TPTCMode;
+procedure TDirectXConsole.Open(const ATitle: string; AMode: IPTCMode;
                            APages: Integer = 0);
 begin
   { internal open exact mode }
@@ -489,20 +473,19 @@ begin
   FWindow.Update;
 end;
 
-procedure TDirectXConsole.Update(const AArea: TPTCArea);
+procedure TDirectXConsole.Update(AArea: IPTCArea);
 begin
   { update }
   Update;
 end;
 
-function TDirectXConsole.NextEvent(var AEvent: TPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean;
+function TDirectXConsole.NextEvent(out AEvent: IPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean;
 var
   UseGetMessage: Boolean;
 begin
   CHECK_OPEN('TDirectXConsole.NextEvent');
 //  CHECK_LOCK('TDirectXConsole.NextEvent');
 
-  FreeAndNil(AEvent);
   UseGetMessage := False;
   repeat
     FPrimary.Block;
@@ -518,7 +501,7 @@ begin
   Result := AEvent <> nil;
 end;
 
-function TDirectXConsole.PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): TPTCEvent;
+function TDirectXConsole.PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): IPTCEvent;
 var
   UseGetMessage: Boolean;
 begin
@@ -539,7 +522,7 @@ begin
   until (not AWait) or (Result <> nil);
 end;
 
-procedure TDirectXConsole.Copy(ASurface: TPTCBaseSurface);
+procedure TDirectXConsole.Copy(ASurface: IPTCSurface);
 var
   pixels: Pointer;
 begin
@@ -558,8 +541,8 @@ begin
   end;
 end;
 
-procedure TDirectXConsole.Copy(ASurface: TPTCBaseSurface;
-                           const ASource, ADestination: TPTCArea);
+procedure TDirectXConsole.Copy(ASurface: IPTCSurface;
+                               ASource, ADestination: IPTCArea);
 var
   pixels: Pointer;
 begin
@@ -608,10 +591,9 @@ end;
 
 procedure TDirectXConsole.Load(const APixels: Pointer;
                            AWidth, AHeight, APitch: Integer;
-                           const AFormat: TPTCFormat;
-                           const APalette: TPTCPalette);
+                           AFormat: IPTCFormat;
+                           APalette: IPTCPalette);
 var
-  Area_: TPTCArea;
   console_pixels: Pointer;
 begin
   CHECK_OPEN('TDirectXConsole.Load(APixels, AWidth, AHeight, APitch, AFormat, APalette)');
@@ -636,66 +618,44 @@ begin
     end;
   end
   else
-  begin
-    Area_ := TPTCArea.Create(0, 0, Width, Height);
-    try
-      Load(APixels, AWidth, AHeight, APitch, AFormat, APalette, Area_, area);
-    finally
-      Area_.Free;
-    end;
-  end;
+    Load(APixels, AWidth, AHeight, APitch, AFormat, APalette, TPTCArea.Create(0, 0, Width, Height), area);
 end;
 
 procedure TDirectXConsole.Load(const APixels: Pointer;
                                AWidth, AHeight, APitch: Integer;
-                               const AFormat: TPTCFormat;
-                               const APalette: TPTCPalette;
-                               const ASource, ADestination: TPTCArea);
+                               AFormat: IPTCFormat;
+                               APalette: IPTCPalette;
+                               ASource, ADestination: IPTCArea);
 var
   console_pixels: Pointer;
-  clipped_source, clipped_destination: TPTCArea;
-  tmp: TPTCArea;
+  clipped_source, clipped_destination: IPTCArea;
 begin
   CHECK_OPEN('TDirectXConsole.Load(APixels, AWidth, AHeight, APitch, AFormat, APalette, ASource, ADestination)');
   CHECK_LOCK('TDirectXConsole.Load(APixels, AWidth, AHeight, APitch, AFormat, APalette, ASource, ADestination)');
-  clipped_destination := nil;
-  clipped_source := TPTCArea.Create;
+  console_pixels := Lock;
   try
-    clipped_destination := TPTCArea.Create;
-    console_pixels := Lock;
     try
-      try
-        tmp := TPTCArea.Create(0, 0, AWidth, AHeight);
-        try
-          TPTCClipper.clip(ASource, tmp, clipped_source, ADestination, Clip, clipped_destination);
-        finally
-          tmp.Free;
-        end;
-        FCopy.Request(AFormat, Format);
-        FCopy.Palette(APalette, Palette);
-        FCopy.Copy(APixels, clipped_source.Left, clipped_source.Top, clipped_source.Width, clipped_source.Height, APitch,
-                    console_pixels, clipped_destination.Left, clipped_destination.Top, clipped_destination.Width, clipped_destination.Height, Pitch);
-      except
-        on error:TPTCError do
-        begin
-          raise TPTCError.Create('failed to load pixels to console area', error);
-        end;
+      TPTCClipper.Clip(ASource, TPTCArea.Create(0, 0, AWidth, AHeight), clipped_source, ADestination, Clip, clipped_destination);
+      FCopy.Request(AFormat, Format);
+      FCopy.Palette(APalette, Palette);
+      FCopy.Copy(APixels, clipped_source.Left, clipped_source.Top, clipped_source.Width, clipped_source.Height, APitch,
+                 console_pixels, clipped_destination.Left, clipped_destination.Top, clipped_destination.Width, clipped_destination.Height, Pitch);
+    except
+      on error:TPTCError do
+      begin
+        raise TPTCError.Create('failed to load pixels to console area', error);
       end;
-    finally
-      Unlock;
     end;
   finally
-    clipped_source.Free;
-    clipped_destination.Free;
+    Unlock;
   end;
 end;
 
 procedure TDirectXConsole.Save(APixels: Pointer;
                                AWidth, AHeight, APitch: Integer;
-                               const AFormat: TPTCFormat;
-                               const APalette: TPTCPalette);
+                               AFormat: IPTCFormat;
+                               APalette: IPTCPalette);
 var
-  Area_: TPTCArea;
   console_pixels: Pointer;
 begin
   CHECK_OPEN('TDirectXConsole.Save(APixels, AWidth, AHeight, APitch, AFormat, APalette)');
@@ -720,112 +680,82 @@ begin
     end;
   end
   else
-  begin
-    Area_ := TPTCArea.Create(0, 0, Width, Height);
-    try
-      Save(APixels, AWidth, AHeight, APitch, AFormat, APalette, Area, Area_);
-    finally
-      Area_.Free;
-    end;
-  end;
+    Save(APixels, AWidth, AHeight, APitch, AFormat, APalette, Area, TPTCArea.Create(0, 0, Width, Height));
 end;
 
 procedure TDirectXConsole.Save(APixels: Pointer;
                            AWidth, AHeight, APitch: Integer;
-                           const AFormat: TPTCFormat;
-                           const APalette: TPTCPalette;
-                           const ASource, ADestination: TPTCArea);
+                           AFormat: IPTCFormat;
+                           APalette: IPTCPalette;
+                           ASource, ADestination: IPTCArea);
 var
   console_pixels: Pointer;
-  clipped_source, clipped_destination: TPTCArea;
-  tmp: TPTCArea;
+  clipped_source, clipped_destination: IPTCArea;
 begin
   CHECK_OPEN('TDirectXConsole.Save(APixels, AWidth, AHeight, APitch, AFormat, APalette, ASource, ADestination)');
   CHECK_LOCK('TDirectXConsole.Save(APixels, AWidth, AHeight, APitch, AFormat, APalette, ASource, ADestination)');
-  clipped_destination := nil;
-  clipped_source := TPTCArea.Create;
+  console_pixels := Lock;
   try
-    clipped_destination := TPTCArea.Create;
-    console_pixels := Lock;
     try
-      try
-        tmp := TPTCArea.Create(0, 0, AWidth, AHeight);
-        try
-          TPTCClipper.Clip(ASource, Clip, clipped_source, ADestination, tmp, clipped_destination);
-        finally
-          tmp.Free;
-        end;
-        FCopy.Request(Format, AFormat);
-        FCopy.Palette(Palette, APalette);
-        FCopy.Copy(console_pixels, clipped_source.Left, clipped_source.Top, clipped_source.Width, clipped_source.Height, Pitch,
-                    APixels, clipped_destination.Left, clipped_destination.Top, clipped_destination.Width, clipped_destination.Height, APitch);
-      except
-        on error:TPTCError do
-        begin
-          raise TPTCError.Create('failed to save console area pixels', error);
-        end;
+      TPTCClipper.Clip(ASource, Clip, clipped_source, ADestination, TPTCArea.Create(0, 0, AWidth, AHeight), clipped_destination);
+      FCopy.Request(Format, AFormat);
+      FCopy.Palette(Palette, APalette);
+      FCopy.Copy(console_pixels, clipped_source.Left, clipped_source.Top, clipped_source.Width, clipped_source.Height, Pitch,
+                 APixels, clipped_destination.Left, clipped_destination.Top, clipped_destination.Width, clipped_destination.Height, APitch);
+    except
+      on error:TPTCError do
+      begin
+        raise TPTCError.Create('failed to save console area pixels', error);
       end;
-    finally
-      Unlock;
     end;
   finally
-    clipped_source.Free;
-    clipped_destination.Free;
+    Unlock;
   end;
 end;
 
 procedure TDirectXConsole.Clear;
 var
-  tmp: TPTCColor;
+  Color: IPTCColor;
 begin
   CHECK_OPEN('TDirectXConsole.Clear');
   CHECK_LOCK('TDirectXConsole.Clear');
   if format.direct then
-    tmp := TPTCColor.Create(0, 0, 0, 0)
+    Color := TPTCColor.Create(0, 0, 0, 0)
   else
-    tmp := TPTCColor.Create(0);
-  try
-    Clear(tmp);
-  finally
-    tmp.Free;
-  end;
+    Color := TPTCColor.Create(0);
+  Clear(Color);
 end;
 
-procedure TDirectXConsole.Clear(const AColor: TPTCColor);
+procedure TDirectXConsole.Clear(AColor: IPTCColor);
 var
   tmp: TPTCArea;
 begin
   CHECK_OPEN('TDirectXConsole.Clear(AColor)');
   CHECK_LOCK('TDirectXConsole.Clear(AColor)');
-  tmp := TPTCArea.Create;
-  try
-    Clear(AColor, tmp);
-  finally
-    tmp.Free;
-  end;
+  Clear(AColor, TPTCArea.Create);
 end;
 
-procedure TDirectXConsole.Clear(const AColor: TPTCColor;
-                                const AArea: TPTCArea);
+procedure TDirectXConsole.Clear(AColor: IPTCColor;
+                                AArea: IPTCArea);
 begin
   CHECK_OPEN('TDirectXConsole.Clear(AColor, AArea)');
   CHECK_LOCK('TDirectXConsole.Clear(AColor, AArea)');
   FPrimary.Clear(AColor, AArea);
 end;
 
-procedure TDirectXConsole.palette(const APalette: TPTCPalette);
+procedure TDirectXConsole.palette(APalette: IPTCPalette);
 begin
   CHECK_OPEN('TDirectXConsole.Palette(APalette)');
   FPrimary.Palette(APalette);
 end;
 
-function TDirectXConsole.Palette: TPTCPalette;
+function TDirectXConsole.Palette: IPTCPalette;
 begin
   CHECK_OPEN('TDirectXConsole.Palette');
   Result := FPrimary.palette;
 end;
 
-procedure TDirectXConsole.Clip(const AArea: TPTCArea);
+procedure TDirectXConsole.Clip(AArea: IPTCArea);
 begin
   CHECK_OPEN('TDirectXConsole.Clip(AArea)');
   FPrimary.Clip(AArea);
@@ -855,19 +785,19 @@ begin
   Result := FPrimary.pages;
 end;
 
-function TDirectXConsole.GetArea: TPTCArea;
+function TDirectXConsole.GetArea: IPTCArea;
 begin
   CHECK_OPEN('TDirectXConsole.GetArea');
-  Result := FPrimary.area;
+  Result := FPrimary.Area;
 end;
 
-function TDirectXConsole.Clip: TPTCArea;
+function TDirectXConsole.Clip: IPTCArea;
 begin
   CHECK_OPEN('TDirectXConsole.Clip');
-  Result := FPrimary.clip;
+  Result := FPrimary.Clip;
 end;
 
-function TDirectXConsole.GetFormat: TPTCFormat;
+function TDirectXConsole.GetFormat: IPTCFormat;
 begin
   CHECK_OPEN('TDirectXConsole.GetFormat');
   Result := FPrimary.format;
@@ -890,10 +820,10 @@ begin
   Result := FDisplay.information;
 end;
 
-procedure TDirectXConsole.internal_open(const _title: string; window: HWND; const mode: TPTCMode; _pages: Integer; exact: Boolean);
+procedure TDirectXConsole.internal_open(const _title: string; window: HWND; const mode: IPTCMode; _pages: Integer; exact: Boolean);
 var
   _width, _height: Integer;
-  _format: TPTCFormat;
+  _format: IPTCFormat;
 begin
   try
     { recycle an already open console }
@@ -982,7 +912,7 @@ begin
   internal_open_finish;
 end;
 
-procedure TDirectXConsole.internal_recycle(const _title: string; window: HWND; const mode: TPTCMode; _pages: Integer; exact: Boolean);
+procedure TDirectXConsole.internal_recycle(const _title: string; window: HWND; const mode: IPTCMode; _pages: Integer; exact: Boolean);
 begin
   { Check if the console is open }
   if not FOpen then
@@ -1075,7 +1005,7 @@ begin
   FDisplay.restore;
 end;
 
-procedure TDirectXConsole.internal_open_fullscreen_start(window: HWND; const mode: TPTCMode; exact: Boolean);
+procedure TDirectXConsole.internal_open_fullscreen_start(window: HWND; const mode: IPTCMode; exact: Boolean);
 begin
   FFullscreen := True;
 
@@ -1118,13 +1048,13 @@ begin
   FDisplay.cooperative(FWindow.handle, True);
 end;
 
-procedure TDirectXConsole.internal_open_fullscreen_change(const mode: TPTCMode; exact: Boolean);
+procedure TDirectXConsole.internal_open_fullscreen_change(const mode: IPTCMode; exact: Boolean);
 begin
   FDisplay.open(mode, exact, FFrequency);
   FPrimary.blocking(True);
 end;
 
-procedure TDirectXConsole.internal_open_fullscreen_surface(const mode: TPTCMode; _pages: Integer);
+procedure TDirectXConsole.internal_open_fullscreen_surface(const mode: IPTCMode; _pages: Integer);
 var
   primary: Boolean;
   _secondary: Boolean;
@@ -1196,7 +1126,7 @@ begin
   FHook := TDirectXHook.Create(Self, FWindow.handle, GetCurrentThreadId, FCursor, FWindow.managed, True);
 end;
 
-procedure TDirectXConsole.internal_open_windowed_start(window: HWND; const mode: TPTCMode; exact: Boolean);
+procedure TDirectXConsole.internal_open_windowed_start(window: HWND; const mode: IPTCMode; exact: Boolean);
 var
   extended: Integer;
 begin
@@ -1226,7 +1156,7 @@ begin
   FDisplay.cooperative(FWindow.handle, False);
 end;
 
-procedure TDirectXConsole.internal_open_windowed_change(const mode: TPTCMode; exact: Boolean);
+procedure TDirectXConsole.internal_open_windowed_change(const mode: IPTCMode; exact: Boolean);
 begin
   FDisplay.open;
   if FPrimaryModeWindowed = DIRECT then
@@ -1235,7 +1165,7 @@ begin
     FPrimary.blocking(False);
 end;
 
-procedure TDirectXConsole.internal_open_windowed_surface(const mode: TPTCMode; _pages: Integer);
+procedure TDirectXConsole.internal_open_windowed_surface(const mode: IPTCMode; _pages: Integer);
 begin
   FPrimary.initialize(FWindow, FLibrary.lpDD2);
   FPrimary.primary(1, False, False, False, False);
@@ -1251,7 +1181,7 @@ begin
   FHook := TDirectXHook.Create(Self, FWindow.handle, GetCurrentThreadId, FCursor, FWindow.managed, False);
 end;
 
-procedure TDirectXConsole.internal_recycle_fullscreen(const _title: string; window: HWND; const mode: TPTCMode; _pages: Integer; exact: Boolean);
+procedure TDirectXConsole.internal_recycle_fullscreen(const _title: string; window: HWND; const mode: IPTCMode; _pages: Integer; exact: Boolean);
 begin
   LOG('fullscreen open recycle');
   FPrimary.close;
@@ -1259,7 +1189,7 @@ begin
   internal_open_fullscreen_surface(mode, _pages);
 end;
 
-procedure TDirectXConsole.internal_recycle_windowed(const _title: string; window: HWND; const mode: TPTCMode; _pages: Integer; exact: Boolean);
+procedure TDirectXConsole.internal_recycle_windowed(const _title: string; window: HWND; const mode: IPTCMode; _pages: Integer; exact: Boolean);
 begin
   LOG('windowed open recycle');
   FPrimary.close;

+ 89 - 143
packages/ptc/src/win32/directx/display.inc

@@ -33,7 +33,6 @@
 constructor TDirectXDisplay.Create;
 begin
   FInformation := '';
-  FMode := nil;
   FCursorsaved := False;
   FOpen := False;
   FFullscreen := False;
@@ -42,43 +41,31 @@ begin
 //  FForeground := 0;
 //  FillChar(FModes, SizeOf(FModes), 0);
 //  FillChar(FResolutions, SizeOf(FResolutions), 0);
-  FMode := TPTCMode.Create;
 end;
 
 destructor TDirectXDisplay.Destroy;
 begin
   close;
-  FMode.Free;
   internal_dispose_modes;
   internal_dispose_resolutions;
   inherited Destroy;
 end;
 
 procedure TDirectXDisplay.internal_dispose_modes;
-var
-  i: Integer;
 begin
-  for i := Low(FModes) to High(FModes) do
-    FreeAndNil(FModes[i]);
-
   SetLength(FModes, 0);
   FModesCount := 0;
 end;
 
 procedure TDirectXDisplay.internal_dispose_resolutions;
-var
-  i: Integer;
 begin
-  for i := Low(FResolutions) to High(FResolutions) do
-    FreeAndNil(FResolutions[i]);
-
   SetLength(FResolutions, 0);
 end;
 
 function TDirectXDisplay_callback(descriptor: PDDSurfaceDesc {TODO: TDDSurfaceDesc vs. TDDSurfaceDesc2???}; Context: Pointer): HRESULT; stdcall;
 var
   display: TDirectXDisplay;
-  tmp: TPTCFormat;
+  format: IPTCFormat;
 begin
   if (descriptor = nil) or (Context = nil) then
   begin
@@ -90,15 +77,11 @@ begin
      ((descriptor^.dwFlags and DDSD_HEIGHT) <> 0) and
      ((descriptor^.dwFlags and DDSD_PIXELFORMAT) <> 0) then
   begin
-    tmp := DirectXTranslate(descriptor^.ddpfPixelFormat);
-    try
-      Inc(display.FModesCount);
-      SetLength(display.FModes, display.FModesCount);
-      display.FModes[display.FModesCount - 1] :=
-        TPTCMode.Create(descriptor^.dwWidth, descriptor^.dwHeight, tmp);
-    finally
-      tmp.Free;
-    end;
+    format := DirectXTranslate(descriptor^.ddpfPixelFormat);
+    Inc(display.FModesCount);
+    SetLength(display.FModes, display.FModesCount);
+    display.FModes[display.FModesCount - 1] :=
+      TPTCMode.Create(descriptor^.dwWidth, descriptor^.dwHeight, format);
   end
   else
   begin
@@ -112,8 +95,8 @@ var
   version: TOSVERSIONINFO;
   match, found: Boolean;
   i, j: Integer;
-  temp: TPTCMode;
-  temp2: TPTCFormat;
+  tempMode: IPTCMode;
+  format8: IPTCFormat;
 begin
   LOG('setting up display lpDD2');
   FDDraw := ADDraw;
@@ -127,36 +110,32 @@ begin
     if version.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS then
     begin
       LOG('detected windows 95/98');
-      temp2 := TPTCFormat.Create(8);
-      try
-        found := False;
-        for i := 0 to FModesCount - 1 do
-          if (FModes[i].Width = 320) and (FModes[i].Height = 200) and
-              FModes[i].Format.Equals(temp2) then
-            found := True;
-        if not found then
-        begin
-          LOG('adding 320x200x8 to mode list');
-	  
-	  Inc(FModesCount);
-          SetLength(FModes, FModesCount);
-          FModes[FModesCount - 1] := TPTCMode.Create(320, 200, temp2);
-        end;
-        found := False;
-        for i := 0 to FModesCount - 1 do
-          if (FModes[i].Width = 320) and (FModes[i].Height = 240) and
-              FModes[i].Format.Equals(temp2) then
-            found := True;
-        if not found then
-        begin
-          LOG('adding 320x240x8 to mode list');
+      format8 := TPTCFormat.Create(8);
+      found := False;
+      for i := 0 to FModesCount - 1 do
+        if (FModes[i].Width = 320) and (FModes[i].Height = 200) and
+            FModes[i].Format.Equals(format8) then
+          found := True;
+      if not found then
+      begin
+        LOG('adding 320x200x8 to mode list');
+	
+	Inc(FModesCount);
+        SetLength(FModes, FModesCount);
+        FModes[FModesCount - 1] := TPTCMode.Create(320, 200, format8);
+      end;
+      found := False;
+      for i := 0 to FModesCount - 1 do
+        if (FModes[i].Width = 320) and (FModes[i].Height = 240) and
+            FModes[i].Format.Equals(format8) then
+          found := True;
+      if not found then
+      begin
+        LOG('adding 320x240x8 to mode list');
 
-	  Inc(FModesCount);
-          SetLength(FModes, FModesCount);
-          FModes[FModesCount - 1] := TPTCMode.Create(320, 240, temp2);
-        end;
-      finally
-        temp2.Free;
+	Inc(FModesCount);
+        SetLength(FModes, FModesCount);
+        FModes[FModesCount - 1] := TPTCMode.Create(320, 240, format8);
       end;
     end
     else
@@ -167,9 +146,6 @@ begin
   end;
   LOG('number of display modes', FModesCount);
 
-  SetLength(FModes, FModesCount + 1);
-  FModes[FModesCount] := TPTCMode.Create;
-  
   internal_dispose_resolutions;
   for i := 0 to FModesCount - 1 do
   begin
@@ -185,11 +161,9 @@ begin
     begin
       Inc(FResolutionsCount);
       SetLength(FResolutions, FResolutionsCount);
-      FResolutions[FResolutionsCount - 1] := TPTCMode.Create(FModes[i]);
+      FResolutions[FResolutionsCount - 1] := FModes[i];
     end;
   end;
-  SetLength(FResolutions, FResolutionsCount + 1);
-  FResolutions[FResolutionsCount] := TPTCMode.Create;
 
   { kludge sort... :) }
   for i := 0 to FResolutionsCount - 1 do
@@ -197,9 +171,9 @@ begin
       if (FResolutions[i].Width > FResolutions[j].Width) or
          (FResolutions[i].Height > FResolutions[j].Height) then
       begin
-        temp := FResolutions[i];
+        tempMode := FResolutions[i];
         FResolutions[i] := FResolutions[j];
-        FResolutions[j] := temp;
+        FResolutions[j] := tempMode;
       end;
   LOG('number of unique resolutions', FResolutionsCount);
   for i := 0 to FResolutionsCount - 1 do
@@ -208,12 +182,12 @@ begin
   end;
 end;
 
-function TDirectXDisplay.GetModes: PPTCMode;
+function TDirectXDisplay.GetModes: TPTCModeList;
 begin
-  Result := @FModes[0];
+  Result := FModes;
 end;
 
-function TDirectXDisplay.Test(const AMode: TPTCMode; AExact: Boolean): Boolean;
+function TDirectXDisplay.Test(const AMode: IPTCMode; AExact: Boolean): Boolean;
 var
   i: Integer;
 begin
@@ -271,7 +245,7 @@ begin
   LOG('opening windowed display');
 end;
 
-procedure TDirectXDisplay.Open(const AMode: TPTCMode; AExact: Boolean; AFrequency: Integer);
+procedure TDirectXDisplay.Open(const AMode: IPTCMode; AExact: Boolean; AFrequency: Integer);
 begin
   if AExact then
   begin
@@ -338,7 +312,7 @@ begin
   End;}
 end;
 
-procedure TDirectXDisplay.internal_open(const AMode: TPTCMode; AExact: Boolean; AFrequency: Integer);
+procedure TDirectXDisplay.internal_open(const AMode: IPTCMode; AExact: Boolean; AFrequency: Integer);
 begin
   LOG('internal display open');
   LOG('mode width', AMode.Width);
@@ -381,25 +355,20 @@ begin
         raise TPTCError.Create('unsupported pixel format');
     end;
   LOG('internal display open success');
-  FreeAndNil(FMode);
   FMode := TPTCMode.Create(AMode);
   FOpen := True;
 end;
 
-procedure TDirectXDisplay.internal_open_nearest(const AMode: TPTCMode; AExact: Boolean; AFrequency: Integer);
+procedure TDirectXDisplay.internal_open_nearest(const AMode: IPTCMode; AExact: Boolean; AFrequency: Integer);
 
   function Pass2TryMode(TryWidth, TryHeight: Integer): Boolean;
   var
-    tmp: TPTCMode;
+    tmpMode: IPTCMode;
   begin
     if (AMode.Width <= TryWidth) and (AMode.Height <= TryHeight) then
     try
-      tmp := TPTCMode.Create(TryWidth, TryHeight, AMode.Format);
-      try
-        internal_open(tmp, AExact, AFrequency);
-      finally
-        tmp.Free;
-      end;
+      tmpMode := TPTCMode.Create(TryWidth, TryHeight, AMode.Format);
+      internal_open(tmpMode, AExact, AFrequency);
       exit(true);
     except
       on TPTCError do;
@@ -409,8 +378,7 @@ procedure TDirectXDisplay.internal_open_nearest(const AMode: TPTCMode; AExact: B
 
 var
   index: Integer;
-  match, match_exact: TPTCMode;
-  tmp: TPTCMode;
+  match, match_exact: IPTCMode;
   i: Integer;
   width, height: Integer;
   dx1, dy1, dx2, dy2: Integer;
@@ -420,87 +388,65 @@ begin
     LOG('searching for nearest mode in resolutions list');
     index := 0;
     match_exact := nil;
-    match := TPTCMode.Create;
-    try
-      match_exact := TPTCMode.Create;
-      for i := 0 to FResolutionsCount - 1 do
+    match := nil;
+    for i := 0 to FResolutionsCount - 1 do
+    begin
+      width := FResolutions[i].Width;
+      height := FResolutions[i].Height;
+      if (AMode.Width <= width) and (AMode.Height <= height) then
       begin
-        width := FResolutions[i].Width;
-        height := FResolutions[i].Height;
-        if (AMode.Width <= width) and (AMode.Height <= height) then
+        if (width = AMode.Width) and (height = AMode.Height) then
         begin
-          if (width = AMode.Width) and (height = AMode.Height) then
-          begin
-            LOG('found an exact match');
-            tmp := TPTCMode.Create(width, height, AMode.Format);
-            try
-              match_exact.Assign(tmp);
-            finally
-              tmp.Free;
-            end;
-          end;
-          if match.valid then
-          begin
-            dx1 := match.Width - AMode.Width;
-            dy1 := match.Height - AMode.Height;
-            dx2 := width - AMode.Width;
-            dy2 := height - AMode.Height;
-            if (dx2 <= dx1) and (dy2 <= dy1) then
-            begin
-              LOG('found a better nearest match');
-              tmp := TPTCMode.Create(width, height, AMode.Format);
-              try
-                match.Assign(tmp);
-              finally
-                tmp.Free;
-              end;
-            end;
-          end
-          else
+          LOG('found an exact match');
+          match_exact := TPTCMode.Create(width, height, AMode.Format);
+        end;
+        if match <> nil then
+        begin
+          dx1 := match.Width - AMode.Width;
+          dy1 := match.Height - AMode.Height;
+          dx2 := width - AMode.Width;
+          dy2 := height - AMode.Height;
+          if (dx2 <= dx1) and (dy2 <= dy1) then
           begin
-            LOG('found first nearest match');
-            tmp := TPTCMode.Create(width, height, AMode.Format);
-            try
-              match.Assign(tmp);
-            finally
-              tmp.Free;
-            end;
+            LOG('found a better nearest match');
+            match := TPTCMode.Create(width, height, AMode.Format);
           end;
         end
         else
         begin
-{          LOG('stopping nearest mode search');
-          Break;}
+          LOG('found first nearest match');
+          match := TPTCMode.Create(width, height, AMode.Format);
         end;
+      end
+      else
+      begin
+{        LOG('stopping nearest mode search');
+        Break;}
       end;
+    end;
 
-      if match_exact.valid then
-      try
-        LOG('trying an exact match');
-        internal_open(match_exact, AExact, AFrequency);
-        exit;
-      except
-        on TPTCError do;
-      end;
-      if match.valid then
-      try
-        LOG('trying nearest match');
-        internal_open(match, AExact, AFrequency);
-        exit;
-      except
-        on TPTCError do;
-      end;
-    finally
-      match.Free;
-      match_exact.Free;
+    if match_exact <> nil then
+    try
+      LOG('trying an exact match');
+      internal_open(match_exact, AExact, AFrequency);
+      exit;
+    except
+      on TPTCError do;
+    end;
+    if match <> nil then
+    try
+      LOG('trying nearest match');
+      internal_open(match, AExact, AFrequency);
+      exit;
+    except
+      on TPTCError do;
     end;
   end
   else
   begin
     LOG('no resolutions list for nearest mode search');
   end;
-{  match.Free;
-  match_exact.Free;}
+
   LOG('could not find a nearest match in first pass');
   try
     LOG('trying requested mode first');

+ 10 - 10
packages/ptc/src/win32/directx/displayd.inc

@@ -35,13 +35,13 @@ type
   private
     FOpen: Boolean;
     FFullscreen: Boolean;
-    FMode: TPTCMode;
+    FMode: IPTCMode;
     FWindow: HWND;
     FDDraw: IDirectDraw2;
     FModesCount: Integer;
     FResolutionsCount: Integer;
-    FModes: array of TPTCMode;
-    FResolutions: array of TPTCMode;
+    FModes: array of IPTCMode;
+    FResolutions: array of IPTCMode;
     FInformation: string;
 
     FCursorSaved: Boolean;
@@ -50,25 +50,25 @@ type
     FForegroundRect: RECT;
     FForegroundPlacement: WINDOWPLACEMENT;}
 
-    procedure internal_open(const AMode: TPTCMode; AExact: Boolean; AFrequency: Integer);
-    procedure internal_open_nearest(const AMode: TPTCMode; AExact: Boolean; AFrequency: Integer);
+    procedure internal_open(const AMode: IPTCMode; AExact: Boolean; AFrequency: Integer);
+    procedure internal_open_nearest(const AMode: IPTCMode; AExact: Boolean; AFrequency: Integer);
     procedure internal_dispose_modes;
     procedure internal_dispose_resolutions;
 
-    function GetModes: PPTCMode;
+    function GetModes: TPTCModeList;
   public
     constructor Create;
     destructor Destroy; override;
     procedure Setup(const ADDraw: IDirectDraw2);
-    function Test(const AMode: TPTCMode; AExact: Boolean): Boolean;
+    function Test(const AMode: IPTCMode; AExact: Boolean): Boolean;
     procedure Cooperative(AWindow: HWND; AFullscreen: Boolean);
     procedure Open;
-    procedure Open(const AMode: TPTCMode; AExact: Boolean; AFrequency: Integer);
+    procedure Open(const AMode: IPTCMode; AExact: Boolean; AFrequency: Integer);
     procedure Close;
     procedure Save;
     procedure Restore;
-    property Modes: PPTCMode read GetModes;
-    property Mode: TPTCMode read FMode;
+    property Modes: TPTCModeList read GetModes;
+    property Mode: IPTCMode read FMode;
     property Fullscreen: Boolean read FFullscreen;
     property Information: string read FInformation;
   end;

+ 1 - 1
packages/ptc/src/win32/directx/hook.inc

@@ -73,7 +73,7 @@ begin
 
       { get window message data }
       active := wParam <> 0;
-      thread := lParam;
+      thread := DWord(lParam);
 
       { check active flag }
       if active = False then

+ 30 - 83
packages/ptc/src/win32/directx/primary.inc

@@ -49,11 +49,7 @@ destructor TDirectXPrimary.Destroy;
 begin
   { close }
   Close;
-  FArea.Free;
-  FClip.Free;
-  FFormat.Free;
   FClear.Free;
-  FPalette.Free;
   inherited Destroy;
 end;
 
@@ -71,7 +67,6 @@ var
   descriptor: TDDSURFACEDESC;
   ddpf: TDDPIXELFORMAT;
   capabilities: TDDSCAPS;
-  tmp: TPTCPalette;
   I: Integer;
   rectangle: TRECT;
 begin
@@ -132,7 +127,6 @@ begin
     FPages := APages;
     FWidth := descriptor.dwWidth;
     FHeight := descriptor.dwHeight;
-    FreeAndNil(FFormat);
     FFormat := DirectXTranslate(ddpf);
     LOG('primary width', FWidth);
     LOG('primary height', FHeight);
@@ -141,12 +135,7 @@ begin
     if APalette then
     begin
       LOG('clearing primary palette');
-      tmp := TPTCPalette.Create;
-      try
-        palette(tmp);
-      finally
-        tmp.Free;
-      end;
+      palette(TPTCPalette.Create);
     end;
     if attach_primary_pages then
     begin
@@ -189,9 +178,7 @@ begin
       FWidth := rectangle.right;
       FHeight := rectangle.bottom;
     end;
-    FreeAndNil(FArea);
     FArea := TPTCArea.Create(0, 0, FWidth, FHeight);
-    FreeAndNil(FClip);
     FClip := TPTCArea.Create(FArea);
     if APages > 1 then
     begin
@@ -275,9 +262,7 @@ begin
   end;
   FWidth := AWidth;
   FHeight := AHeight;
-  FreeAndNil(FArea);
   FArea := TPTCArea.Create(0, 0, FWidth, FHeight);
-  FreeAndNil(FClip);
   FClip := TPTCArea.Create(FArea);
   FSecondaryWidth := FWidth;
   FSecondaryHeight := FHeight;
@@ -334,7 +319,6 @@ procedure TDirectXPrimary.Close;
 var
   i: Integer;
   lost: Boolean;
-  tmp: TPTCPalette;
 begin
   try
     LOG('closing primary surface');
@@ -345,13 +329,7 @@ begin
       lost := True;
     if (FBack <> nil) and (FDDSPrimary <> nil) and FFullscreen and (not lost) then
     begin
-      tmp := TPTCPalette.Create;
-      try
-        LOG('clearing primary palette');
-        palette(tmp);
-      finally
-        tmp.Free;
-      end;
+      palette(TPTCPalette.Create);
       LOG('clearing primary pages');
       for i := 0 to FPages - 1 do
       begin
@@ -443,7 +421,6 @@ end;
 procedure TDirectXPrimary.Clear;
 var
   fx: TDDBLTFX;
-  tmp: TPTCColor;
 begin
   Block;
   if FFullscreen or (FBack = FDDSSecondary) then
@@ -456,29 +433,15 @@ begin
   begin
     { todo: replace with hardware clear! }
     if Format.Direct then
-    begin
-      tmp := TPTCColor.Create(0, 0, 0, 0);
-      try
-        Clear(tmp, FArea);
-      finally
-        tmp.Free;
-      end;
-    end
+      Clear(TPTCColor.Create(0, 0, 0, 0), FArea)
     else
-    begin
-      tmp := TPTCColor.Create(0);
-      try
-        Clear(tmp, FArea);
-      finally
-        tmp.Free;
-      end;
-    end;
+      Clear(TPTCColor.Create(0), FArea);
   end;
 end;
 
-procedure TDirectXPrimary.Clear(const AColor: TPTCColor; const AArea: TPTCArea);
+procedure TDirectXPrimary.Clear(AColor: IPTCColor; const AArea: IPTCArea);
 var
-  clipped, clipped_area: TPTCArea;
+  clipped, clipped_area: IPTCArea;
   clear_color: DWord;
   rct: RECT;
   fx: TDDBLTFX;
@@ -488,47 +451,38 @@ begin
   if FFullscreen or (FBack = FDDSSecondary) then
   begin
     clipped := TPTCClipper.clip(AArea, FClip);
-    try
-      clear_color := Pack(AColor, FFormat);
-      with rct do
-      begin
-        left := clipped.left;
-        top := clipped.top;
-        right := clipped.right;
-        bottom := clipped.bottom;
-      end;
-      fx.dwSize := SizeOf(fx);
-      fx.dwFillColor := clear_color;
-      DirectXCheck(FBack.Blt(@rct, nil, nil, DDBLT_COLORFILL or DDBLT_WAIT, @fx), 'FBack.Blt(rect) failed in TDirectXPrimary.Clear');
-    finally
-      clipped.Free;
+    clear_color := Pack(AColor, FFormat);
+    with rct do
+    begin
+      left := clipped.left;
+      top := clipped.top;
+      right := clipped.right;
+      bottom := clipped.bottom;
     end;
+    fx.dwSize := SizeOf(fx);
+    fx.dwFillColor := clear_color;
+    DirectXCheck(FBack.Blt(@rct, nil, nil, DDBLT_COLORFILL or DDBLT_WAIT, @fx), 'FBack.Blt(rect) failed in TDirectXPrimary.Clear');
   end
   else
   begin
     { todo: replace with accelerated clearing code! }
     pixels := Lock;
-    clipped_area := nil;
     try
-      try
-        clipped_area := TPTCClipper.clip(AArea, Clip);
-        FClear.Request(Format);
-        FClear.Clear(pixels, clipped_area.left, clipped_area.right, clipped_area.width, clipped_area.height, Pitch, AColor);
+      clipped_area := TPTCClipper.clip(AArea, Clip);
+      FClear.Request(Format);
+      FClear.Clear(pixels, clipped_area.left, clipped_area.right, clipped_area.width, clipped_area.height, Pitch, AColor);
+      Unlock;
+    except
+      on error: TPTCError do
+      begin
         Unlock;
-      except
-        on error: TPTCError do
-        begin
-          Unlock;
-          raise TPTCError.Create('failed to clear console area', error);
-        end;
+        raise TPTCError.Create('failed to clear console area', error);
       end;
-    finally
-      clipped_area.Free;
     end;
   end;
 end;
 
-procedure TDirectXPrimary.Palette(const APalette: TPTCPalette);
+procedure TDirectXPrimary.Palette(APalette: IPTCPalette);
 var
   data: PUint32;
   temp: array [0..255] of PALETTEENTRY;
@@ -562,16 +516,9 @@ begin
   end;
 end;
 
-procedure TDirectXPrimary.Clip(const AArea: TPTCArea);
-var
-  tmp: TPTCArea;
+procedure TDirectXPrimary.Clip(const AArea: IPTCArea);
 begin
-  tmp := TPTCClipper.clip(AArea, FArea);
-  try
-    FClip.Assign(tmp);
-  finally
-    tmp.Free;
-  end;
+  FClip := TPTCClipper.clip(AArea, FArea);
 end;
 
 function TDirectXPrimary.GetPitch: Integer;
@@ -799,7 +746,7 @@ begin
   FBlocking := ABlocking;
 end;
 
-function TDirectXPrimary.Pack(const AColor: TPTCColor; const AFormat: TPTCFormat): Uint32;
+function TDirectXPrimary.Pack(const AColor: IPTCColor; const AFormat: IPTCFormat): Uint32;
 var
   r_base, g_base, b_base, a_base: Integer;
   r_size, g_size, b_size, a_size: Integer;
@@ -845,12 +792,12 @@ begin
   end;
 end;
 
-function TDirectXPrimary.Palette: TPTCPalette;
+function TDirectXPrimary.Palette: IPTCPalette;
 begin
   Result := FPalette;
 end;
 
-function TDirectXPrimary.Clip: TPTCArea;
+function TDirectXPrimary.Clip: IPTCArea;
 begin
   Result := FClip;
 end;

+ 12 - 12
packages/ptc/src/win32/directx/primaryd.inc

@@ -36,9 +36,9 @@ type
     FWidth: Integer;
     FHeight: Integer;
     FPages: Integer;
-    FArea: TPTCArea;
-    FClip: TPTCArea;
-    FFormat: TPTCFormat;
+    FArea: IPTCArea;
+    FClip: IPTCArea;
+    FFormat: IPTCFormat;
 
     FActive: Boolean;
     FBlocking: Boolean;
@@ -52,7 +52,7 @@ type
 
     FLocked: Pointer;
 
-    FPalette: TPTCPalette;
+    FPalette: IPTCPalette;
 
     FPrimaryWidth: Integer;
     FPrimaryHeight: Integer;
@@ -72,7 +72,7 @@ type
 
     FBack, FFront: IDirectDrawSurface;
 
-    function Pack(const AColor: TPTCColor; const AFormat: TPTCFormat): Uint32;
+    function Pack(const AColor: IPTCColor; const AFormat: IPTCFormat): Uint32;
     procedure Analyse(AMask: Uint32; out ABase, ASize: Integer);
     function GetDDS: IDirectDrawSurface;
     function GetPitch: Integer;
@@ -93,20 +93,20 @@ type
     procedure Unlock;
 
     procedure Clear;
-    procedure Clear(const AColor: TPTCColor; const AArea: TPTCArea);
+    procedure Clear(AColor: IPTCColor; const AArea: IPTCArea);
 
-    procedure Palette(const APalette: TPTCPalette);
-    function Palette: TPTCPalette;
+    procedure Palette(APalette: IPTCPalette);
+    function Palette: IPTCPalette;
 
-    procedure Clip(const AArea: TPTCArea);
+    procedure Clip(const AArea: IPTCArea);
 
     property Width: Integer read FWidth;
     property Height: Integer read FHeight;
     property Pages: Integer read FPages;
     property Pitch: Integer read GetPitch;
-    property Area: TPTCArea read FArea;
-    function Clip: TPTCArea;
-    property Format: TPTCFormat read FFormat;
+    property Area: IPTCArea read FArea;
+    function Clip: IPTCArea;
+    property Format: IPTCFormat read FFormat;
 
     property DDS: IDirectDrawSurface read GetDDS;
     property DDSPrimary: IDirectDrawSurface read FDDSPrimary;

+ 1 - 1
packages/ptc/src/win32/directx/translate.inc

@@ -30,7 +30,7 @@
     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 }
 
-function DirectXTranslate(const ddpf: TDDPIXELFORMAT): TPTCFormat;
+function DirectXTranslate(const ddpf: TDDPIXELFORMAT): IPTCFormat;
 begin
   if (ddpf.dwFlags and DDPF_PALETTEINDEXED8) <> 0 then
     exit(TPTCFormat.Create(8))

+ 34 - 34
packages/ptc/src/win32/gdi/gdiconsoled.inc

@@ -41,10 +41,10 @@ type
     FCopy: TPTCCopy;
     FClear: TPTCClear;
     FEventQueue: TEventQueue;
-    FArea: TPTCArea;
-    FClip: TPTCArea;
-    FPalette: TPTCPalette;
-    FModes: array [0..1] of TPTCMode;
+    FArea: IPTCArea;
+    FClip: IPTCArea;
+    FPalette: IPTCPalette;
+    FModes: array of IPTCMode;
 
     FOpen: Boolean;
     FLocked: Boolean;
@@ -58,15 +58,15 @@ type
 
     FDefaultWidth: Integer;
     FDefaultHeight: Integer;
-    FDefaultFormat: TPTCFormat;
+    FDefaultFormat: IPTCFormat;
 
     procedure UpdateCursor;
 
     function GetWidth: Integer; override;
     function GetHeight: Integer; override;
     function GetPitch: Integer; override;
-    function GetArea: TPTCArea; override;
-    function GetFormat: TPTCFormat; override;
+    function GetArea: IPTCArea; override;
+    function GetFormat: IPTCFormat; override;
     function GetPages: Integer; override;
     function GetName: string; override;
     function GetTitle: string; override;
@@ -79,59 +79,59 @@ type
     destructor Destroy; override;
 
     procedure Open(const ATitle: string; APages: Integer = 0); overload; override;
-    procedure Open(const ATitle: string; const AFormat: TPTCFormat;
+    procedure Open(const ATitle: string; AFormat: IPTCFormat;
                    APages: Integer = 0); overload; override;
     procedure Open(const ATitle: string; AWidth, AHeight: Integer;
-                   const AFormat: TPTCFormat; APages: Integer = 0); overload; override;
-    procedure Open(const ATitle: string; const AMode: TPTCMode;
+                   AFormat: IPTCFormat; APages: Integer = 0); overload; override;
+    procedure Open(const ATitle: string; AMode: IPTCMode;
                    APages: Integer = 0); overload; override;
     procedure Close; override;
 
-    procedure Copy(ASurface: TPTCBaseSurface); override;
-    procedure Copy(ASurface: TPTCBaseSurface;
-                   const ASource, ADestination: TPTCArea); override;
+    procedure Copy(ASurface: IPTCSurface); override;
+    procedure Copy(ASurface: IPTCSurface;
+                   ASource, ADestination: IPTCArea); override;
 
     procedure Load(const APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette); override;
     procedure Load(const APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette;
-                   const ASource, ADestination: TPTCArea); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette;
+                   ASource, ADestination: IPTCArea); override;
     procedure Save(APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette); override;
     procedure Save(APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette;
-                   const ASource, ADestination: TPTCArea); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette;
+                   ASource, ADestination: IPTCArea); override;
 
     function Lock: Pointer; override;
     procedure Unlock; override;
 
     procedure Clear; override;
-    procedure Clear(const AColor: TPTCColor); override;
-    procedure Clear(const AColor: TPTCColor;
-                    const AArea: TPTCArea); override;
+    procedure Clear(AColor: IPTCColor); override;
+    procedure Clear(AColor: IPTCColor;
+                    AArea: IPTCArea); override;
 
     procedure Configure(const AFileName: String); override;
     function Option(const AOption: String): Boolean; override;
 
-    procedure Palette(const APalette: TPTCPalette); override;
-    procedure Clip(const AArea: TPTCArea); override;
-    function Clip: TPTCArea; override;
-    function Palette: TPTCPalette; override;
-    function Modes: PPTCMode; override;
+    procedure Palette(APalette: IPTCPalette); override;
+    procedure Clip(AArea: IPTCArea); override;
+    function Clip: IPTCArea; override;
+    function Palette: IPTCPalette; override;
+    function Modes: TPTCModeList; override;
 
     procedure Flush; override;
     procedure Finish; override;
     procedure Update; override;
-    procedure Update(const AArea: TPTCArea); override;
+    procedure Update(AArea: IPTCArea); override;
 
-    function NextEvent(var AEvent: TPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean; override;
-    function PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): TPTCEvent; override;
+    function NextEvent(out AEvent: IPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean; override;
+    function PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): IPTCEvent; override;
   end;

+ 39 - 76
packages/ptc/src/win32/gdi/gdiconsolei.inc

@@ -50,8 +50,8 @@ begin
 
   FOpen := False;
 
+  SetLength(FModes, 1);
   FModes[0] := TPTCMode.Create(FDisplayWidth, FDisplayHeight, FDefaultFormat);
-  FModes[1] := TPTCMode.Create;
 
   { configure console }
   Configure('ptcpas.cfg');
@@ -67,16 +67,9 @@ begin
 
   FWin32DIB.Free;
   FWindow.Free;
-  FPalette.Free;
   FEventQueue.Free;
   FCopy.Free;
   FClear.Free;
-  FArea.Free;
-  FClip.Free;
-  FDefaultFormat.Free;
-
-  for I := Low(FModes) to High(FModes) do
-    FModes[I].Free;
 
   inherited Destroy;
 end;
@@ -86,22 +79,22 @@ begin
   Open(ATitle, FDefaultFormat, APages);
 end;
 
-procedure TGDIConsole.Open(const ATitle: string; const AFormat: TPTCFormat;
+procedure TGDIConsole.Open(const ATitle: string; AFormat: IPTCFormat;
                APages: Integer = 0);
 begin
   Open(ATitle, FDefaultWidth, FDefaultHeight, AFormat, APages);
 end;
 
-procedure TGDIConsole.Open(const ATitle: string; const AMode: TPTCMode;
+procedure TGDIConsole.Open(const ATitle: string; AMode: IPTCMode;
                            APages: Integer = 0);
 begin
   Open(ATitle, AMode.Width, AMode.Height, AMode.Format, APages);
 end;
 
 procedure TGDIConsole.Open(const ATitle: string; AWidth, AHeight: Integer;
-               const AFormat: TPTCFormat; APages: Integer = 0);
+                           AFormat: IPTCFormat; APages: Integer = 0);
 var
-  tmp: TPTCArea;
+  tmpArea: IPTCArea;
 begin
   if FOpen then
     Close;
@@ -145,13 +138,9 @@ begin
   FKeyboard := TWin32Keyboard.Create(FWindow.Handle, FWindow.Thread, False, FEventQueue);
   FMouse := TWin32Mouse.Create(FWindow.Handle, FWindow.Thread, False, FEventQueue, {FFullScreen}False, AWidth, AHeight);
 
-  tmp := TPTCArea.Create(0, 0, AWidth, AHeight);
-  try
-    FArea.Assign(tmp);
-    FClip.Assign(tmp);
-  finally
-    tmp.Free;
-  end;
+  tmpArea := TPTCArea.Create(0, 0, AWidth, AHeight);
+  FArea := tmpArea;
+  FClip := tmpArea;
 
   FWindow.Update;
 
@@ -180,23 +169,22 @@ begin
   FOpen := False;
 end;
 
-procedure TGDIConsole.Copy(ASurface: TPTCBaseSurface);
+procedure TGDIConsole.Copy(ASurface: IPTCSurface);
 begin
   // todo...
 end;
 
-procedure TGDIConsole.Copy(ASurface: TPTCBaseSurface;
-                           const ASource, ADestination: TPTCArea);
+procedure TGDIConsole.Copy(ASurface: IPTCSurface;
+                           ASource, ADestination: IPTCArea);
 begin
   // todo...
 end;
 
 procedure TGDIConsole.Load(const APixels: Pointer;
                            AWidth, AHeight, APitch: Integer;
-                           const AFormat: TPTCFormat;
-                           const APalette: TPTCPalette);
+                           AFormat: IPTCFormat;
+                           APalette: IPTCPalette);
 var
-  Area_: TPTCArea;
   console_pixels: Pointer;
 begin
   CheckOpen(    'TGDIConsole.Load(APixels, AWidth, AHeight, APitch, AFormat, APalette)');
@@ -219,49 +207,30 @@ begin
     end;
   end
   else
-  begin
-    Area_ := TPTCArea.Create(0, 0, width, height);
-    try
-      Load(APixels, AWidth, AHeight, APitch, AFormat, APalette, Area_, Area);
-    finally
-      Area_.Free;
-    end;
-  end;
+    Load(APixels, AWidth, AHeight, APitch, AFormat, APalette, TPTCArea.Create(0, 0, width, height), Area);
 end;
 
 procedure TGDIConsole.Load(const APixels: Pointer;
                            AWidth, AHeight, APitch: Integer;
-                           const AFormat: TPTCFormat;
-                           const APalette: TPTCPalette;
-                           const ASource, ADestination: TPTCArea);
+                           AFormat: IPTCFormat;
+                           APalette: IPTCPalette;
+                           ASource, ADestination: IPTCArea);
 var
   console_pixels: Pointer;
-  clipped_source, clipped_destination: TPTCArea;
-  tmp: TPTCArea;
+  clipped_source, clipped_destination: IPTCArea;
 begin
   CheckOpen(    'TGDIConsole.Load(APixels, AWidth, AHeight, APitch, AFormat, APalette, ASource, ADestination)');
   CheckUnlocked('TGDIConsole.Load(APixels, AWidth, AHeight, APitch, AFormat, APalette, ASource, ADestination)');
-  clipped_source := nil;
-  clipped_destination := nil;
   try
     console_pixels := Lock;
     try
-      clipped_source := TPTCArea.Create;
-      clipped_destination := TPTCArea.Create;
-      tmp := TPTCArea.Create(0, 0, AWidth, AHeight);
-      try
-        TPTCClipper.Clip(ASource, tmp, clipped_source, ADestination, Clip, clipped_destination);
-      finally
-        tmp.Free;
-      end;
+      TPTCClipper.Clip(ASource, TPTCArea.Create(0, 0, AWidth, AHeight), clipped_source, ADestination, Clip, clipped_destination);
       FCopy.request(AFormat, Format);
       FCopy.palette(APalette, Palette);
       FCopy.copy(APixels, clipped_source.left, clipped_source.top, clipped_source.width, clipped_source.height, APitch,
                  console_pixels, clipped_destination.left, clipped_destination.top, clipped_destination.width, clipped_destination.height, Pitch);
     finally
       Unlock;
-      clipped_source.Free;
-      clipped_destination.Free;
     end;
   except
     on error: TPTCError do
@@ -271,17 +240,17 @@ end;
 
 procedure TGDIConsole.Save(APixels: Pointer;
                            AWidth, AHeight, APitch: Integer;
-                           const AFormat: TPTCFormat;
-                           const APalette: TPTCPalette);
+                           AFormat: IPTCFormat;
+                           APalette: IPTCPalette);
 begin
   // todo...
 end;
 
 procedure TGDIConsole.Save(APixels: Pointer;
                            AWidth, AHeight, APitch: Integer;
-                           const AFormat: TPTCFormat;
-                           const APalette: TPTCPalette;
-                           const ASource, ADestination: TPTCArea);
+                           AFormat: IPTCFormat;
+                           APalette: IPTCPalette;
+                           ASource, ADestination: IPTCArea);
 begin
   // todo...
 end;
@@ -327,13 +296,13 @@ begin
   // todo...
 end;
 
-procedure TGDIConsole.Clear(const AColor: TPTCColor);
+procedure TGDIConsole.Clear(AColor: IPTCColor);
 begin
   // todo...
 end;
 
-procedure TGDIConsole.Clear(const AColor: TPTCColor;
-                            const AArea: TPTCArea);
+procedure TGDIConsole.Clear(AColor: IPTCColor;
+                            AArea: IPTCArea);
 begin
   // todo...
 end;
@@ -397,40 +366,35 @@ begin
   Result := FCopy.Option(AOption);
 end;
 
-procedure TGDIConsole.Palette(const APalette: TPTCPalette);
+procedure TGDIConsole.Palette(APalette: IPTCPalette);
 begin
   // todo...
 end;
 
-procedure TGDIConsole.Clip(const AArea: TPTCArea);
+procedure TGDIConsole.Clip(AArea: IPTCArea);
 var
   tmp: TPTCArea;
 begin
   CheckOpen('TGDIConsole.Clip(AArea)');
 
-  tmp := TPTCClipper.Clip(AArea, FArea);
-  try
-    FClip.Assign(tmp);
-  finally
-    tmp.Free;
-  end;
+  FClip := TPTCClipper.Clip(AArea, FArea);
 end;
 
-function TGDIConsole.Clip: TPTCArea;
+function TGDIConsole.Clip: IPTCArea;
 begin
   CheckOpen('TGDIConsole.Clip');
   Result := FClip;
 end;
 
-function TGDIConsole.Palette: TPTCPalette;
+function TGDIConsole.Palette: IPTCPalette;
 begin
   CheckOpen('TGDIConsole.Palette');
   Result := FPalette;
 end;
 
-function TGDIConsole.Modes: PPTCMode;
+function TGDIConsole.Modes: TPTCModeList;
 begin
-  Result := @FModes[0];
+  Result := FModes;
 end;
 
 procedure TGDIConsole.Flush;
@@ -478,19 +442,18 @@ begin
   end;
 end;
 
-procedure TGDIConsole.Update(const AArea: TPTCArea);
+procedure TGDIConsole.Update(AArea: IPTCArea);
 begin
   Update;
 end;
 
-function TGDIConsole.NextEvent(var AEvent: TPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean;
+function TGDIConsole.NextEvent(out AEvent: IPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean;
 var
   UseGetMessage: Boolean;
 begin
   CheckOpen('TGDIConsole.NextEvent');
 //  CheckUnlocked('TGDIConsole.NextEvent');
 
-  FreeAndNil(AEvent);
   UseGetMessage := False;
   repeat
     { update window }
@@ -504,7 +467,7 @@ begin
   Result := AEvent <> nil;
 end;
 
-function TGDIConsole.PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): TPTCEvent;
+function TGDIConsole.PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): IPTCEvent;
 var
   UseGetMessage: Boolean;
 begin
@@ -541,13 +504,13 @@ begin
   Result := FWin32DIB.Pitch;
 end;
 
-function TGDIConsole.GetArea: TPTCArea;
+function TGDIConsole.GetArea: IPTCArea;
 begin
   CheckOpen('TGDIConsole.GetArea');
   Result := FArea;
 end;
 
-function TGDIConsole.GetFormat: TPTCFormat;
+function TGDIConsole.GetFormat: IPTCFormat;
 begin
   CheckOpen('TGDIConsole.GetFormat');
   Result := FWin32DIB.Format;

+ 2 - 2
packages/ptc/src/win32/gdi/win32dibd.inc

@@ -34,7 +34,7 @@ type
   private
     FBitmapInfo: PBITMAPINFO;
     FPixels: Pointer;
-    FFormat: TPTCFormat;
+    FFormat: IPTCFormat;
     FWidth, FHeight, FPitch: Integer;
   public
     constructor Create(AWidth, AHeight: Integer);
@@ -43,6 +43,6 @@ type
     property Width: Integer read FWidth;
     property Height: Integer read FHeight;
     property Pitch: Integer read FPitch;
-    property Format: TPTCFormat read FFormat;
+    property Format: IPTCFormat read FFormat;
     property Pixels: Pointer read FPixels;
   end;

+ 0 - 1
packages/ptc/src/win32/gdi/win32dibi.inc

@@ -69,6 +69,5 @@ destructor TWin32DIB.Destroy;
 begin
   FreeMem(FPixels);
   FreeMem(FBitmapInfo);
-  FFormat.Free;
   inherited Destroy;
 end;

+ 38 - 38
packages/ptc/src/wince/gapi/wincegapiconsoled.inc

@@ -30,7 +30,7 @@
 }
 
 type
-  TWinCEGAPIConsole = Class(TPTCBaseConsole)
+  TWinCEGAPIConsole = class(TPTCBaseConsole)
   private
     FWindow: TWinCEWindow;
     FKeyboard: TWinCEKeyboard;
@@ -40,10 +40,10 @@ type
 
     FCopy: TPTCCopy;
     FClear: TPTCClear;
-    FArea: TPTCArea;
-    FClip: TPTCArea;
+    FArea: IPTCArea;
+    FClip: IPTCArea;
     FEventQueue: TEventQueue;
-    FModes: array [0..1] of TPTCMode;
+    FModes: array of IPTCMode;
 
     FOpen: Boolean;
     FLocked: Boolean;
@@ -55,80 +55,80 @@ type
     FDisplayWidth: Integer;
     FDisplayHeight: Integer;
     FDisplayPitch: Integer;
-    FDisplayFormat: TPTCFormat;
+    FDisplayFormat: IPTCFormat;
 
     function WndProc(Ahwnd: HWND; AuMsg: UINT; AwParam: WPARAM; AlParam: LPARAM): LRESULT;
 
     function GetWidth: Integer; override;
     function GetHeight: Integer; override;
     function GetPitch: Integer; override;
-    function GetArea: TPTCArea; override;
-    function GetFormat: TPTCFormat; override;
+    function GetArea: IPTCArea; override;
+    function GetFormat: IPTCFormat; override;
     function GetPages: Integer; override;
     function GetName: string; override;
     function GetTitle: string; override;
     function GetInformation: string; override;
 
-    procedure CheckOpen(    AMessage: String);
-    procedure CheckUnlocked(AMessage: String);
+    procedure CheckOpen(    AMessage: string);
+    procedure CheckUnlocked(AMessage: string);
   public
     constructor Create; override;
     destructor Destroy; override;
 
     procedure Open(const ATitle: string; APages: Integer = 0); overload; override;
-    procedure Open(const ATitle: string; const AFormat: TPTCFormat;
+    procedure Open(const ATitle: string; AFormat: IPTCFormat;
                    APages: Integer = 0); overload; override;
     procedure Open(const ATitle: string; AWidth, AHeight: Integer;
-                   const AFormat: TPTCFormat; APages: Integer = 0); overload; override;
-    procedure Open(const ATitle: string; const AMode: TPTCMode;
+                   AFormat: IPTCFormat; APages: Integer = 0); overload; override;
+    procedure Open(const ATitle: string; AMode: IPTCMode;
                    APages: Integer = 0); overload; override;
     procedure Close; override;
 
-    procedure Copy(ASurface: TPTCBaseSurface); override;
-    procedure Copy(ASurface: TPTCBaseSurface;
-                   const ASource, ADestination: TPTCArea); override;
+    procedure Copy(ASurface: IPTCSurface); override;
+    procedure Copy(ASurface: IPTCSurface;
+                   ASource, ADestination: IPTCArea); override;
 
     procedure Load(const APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette); override;
     procedure Load(const APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette;
-                   const ASource, ADestination: TPTCArea); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette;
+                   ASource, ADestination: IPTCArea); override;
     procedure Save(APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette); override;
     procedure Save(APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette;
-                   const ASource, ADestination: TPTCArea); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette;
+                   ASource, ADestination: IPTCArea); override;
 
     function Lock: Pointer; override;
     procedure Unlock; override;
 
     procedure Clear; override;
-    procedure Clear(const AColor: TPTCColor); override;
-    procedure Clear(const AColor: TPTCColor;
-                    const AArea: TPTCArea); override;
+    procedure Clear(AColor: IPTCColor); override;
+    procedure Clear(AColor: IPTCColor;
+                    AArea: IPTCArea); override;
 
-    procedure Configure(const AFileName: String); override;
-    function Option(const AOption: String): Boolean; override;
+    procedure Configure(const AFileName: string); override;
+    function Option(const AOption: string): Boolean; override;
 
-    procedure Palette(const APalette: TPTCPalette); override;
-    procedure Clip(const AArea: TPTCArea); override;
-    function Clip: TPTCArea; override;
-    function Palette: TPTCPalette; override;
-    function Modes: PPTCMode; override;
+    procedure Palette(APalette: IPTCPalette); override;
+    procedure Clip(AArea: IPTCArea); override;
+    function Clip: IPTCArea; override;
+    function Palette: IPTCPalette; override;
+    function Modes: TPTCModeList; override;
 
     procedure Flush; override;
     procedure Finish; override;
     procedure Update; override;
-    procedure Update(const AArea: TPTCArea); override;
+    procedure Update(AArea: IPTCArea); override;
 
-    function NextEvent(var AEvent: TPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean; override;
-    function PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): TPTCEvent; override;
+    function NextEvent(out AEvent: IPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean; override;
+    function PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): IPTCEvent; override;
   end;

+ 41 - 130
packages/ptc/src/wince/gapi/wincegapiconsolei.inc

@@ -30,7 +30,6 @@
 }
 
 constructor TWinCEGAPIConsole.Create;
-
 begin
   inherited Create;
 
@@ -51,7 +50,6 @@ begin
   FDisplayWidth := FGXDisplayProperties.cxWidth;
   FDisplayHeight := FGXDisplayProperties.cyHeight;
   FDisplayPitch := FGXDisplayProperties.cbyPitch;
-  FDisplayFormat := nil;
 
   if (FGXDisplayProperties.ffFormat and kfDirect565) <> 0 then
     FDisplayFormat := TPTCFormat.Create(FGXDisplayProperties.cBPP,
@@ -72,56 +70,39 @@ begin
   if FDisplayFormat = nil then
     raise TPTCError.Create('GAPI: Unknown/unsupported pixel format');
 
+  SetLength(FModes, 1);
   FModes[0] := TPTCMode.Create(FDisplayWidth, FDisplayHeight, FDisplayFormat);
-  FModes[1] := TPTCMode.Create;
 end;
 
 destructor TWinCEGAPIConsole.Destroy;
-
-var
-  I: Integer;
-
 begin
   Close;
 
   FCopy.Free;
   FClear.Free;
-  FArea.Free;
-  FClip.Free;
-  FDisplayFormat.Free;
-
-  for I := Low(FModes) to High(FModes) do
-    FModes[I].Free;
 
   inherited Destroy;
 end;
 
 procedure TWinCEGAPIConsole.Open(const ATitle: string; APages: Integer = 0);
-
 begin
   Open(ATitle, FDisplayFormat, APages);
 end;
 
-procedure TWinCEGAPIConsole.Open(const ATitle: string; const AFormat: TPTCFormat;
+procedure TWinCEGAPIConsole.Open(const ATitle: string; AFormat: IPTCFormat;
                                  APages: Integer = 0);
-
 begin
   Open(ATitle, FDisplayWidth, FDisplayHeight, AFormat, APages);
 end;
 
-procedure TWinCEGAPIConsole.Open(const ATitle: string; const AMode: TPTCMode;
+procedure TWinCEGAPIConsole.Open(const ATitle: string; AMode: IPTCMode;
                                  APages: Integer = 0);
-
 begin
   Open(ATitle, AMode.Width, AMode.Height, AMode.Format, APages);
 end;
 
 procedure TWinCEGAPIConsole.Open(const ATitle: string; AWidth, AHeight: Integer;
-                                 const AFormat: TPTCFormat; APages: Integer = 0);
-
-var
-  tmp: TPTCArea;
-
+                                 AFormat: IPTCFormat; APages: Integer = 0);
 begin
   LOG('TWinCEGAPIConsole.Open');
 
@@ -146,13 +127,8 @@ begin
     else
       raise TPTCError.Create('could not open display');
 
-    tmp := TPTCArea.Create(0, 0, FDisplayWidth, FDisplayHeight);
-    try
-      FArea.Assign(tmp);
-      FClip.Assign(tmp);
-    finally
-      tmp.Free;
-    end;
+	FArea := TPTCArea.Create(0, 0, FDisplayWidth, FDisplayHeight);
+	FClip := FArea;
 
     FEventQueue := TEventQueue.Create;
     FKeyboard := TWinCEKeyboard.Create(FEventQueue);
@@ -168,13 +144,12 @@ begin
     on error: TObject do
     begin
       Close;
-      Raise;
+      raise;
     end;
   end;
 end;
 
 procedure TWinCEGAPIConsole.Close;
-
 begin
   LOG('TWinCEGAPIConsole.Close');
 
@@ -190,25 +165,21 @@ begin
   FOpen := False;
 end;
 
-procedure TWinCEGAPIConsole.Copy(ASurface: TPTCBaseSurface);
-
+procedure TWinCEGAPIConsole.Copy(ASurface: IPTCSurface);
 begin
 end;
 
-procedure TWinCEGAPIConsole.Copy(ASurface: TPTCBaseSurface;
-                                 const ASource, ADestination: TPTCArea);
-
+procedure TWinCEGAPIConsole.Copy(ASurface: IPTCSurface;
+                                 ASource, ADestination: IPTCArea);
 begin
 end;
 
 procedure TWinCEGAPIConsole.Load(const APixels: Pointer;
                                  AWidth, AHeight, APitch: Integer;
-                                 const AFormat: TPTCFormat;
-                                 const APalette: TPTCPalette);
+                                 AFormat: IPTCFormat;
+                                 APalette: IPTCPalette);
 var
-  Area_: TPTCArea;
   console_pixels: Pointer;
-
 begin
   CheckOpen(    'TWinCEGAPIConsole.Load(APixels, AWidth, AHeight, APitch, AFormat, APalette)');
   CheckUnlocked('TWinCEGAPIConsole.Load(APixels, AWidth, AHeight, APitch, AFormat, APalette)');
@@ -230,26 +201,17 @@ begin
     end;
   end
   else
-  begin
-    Area_ := TPTCArea.Create(0, 0, width, height);
-    try
-      Load(APixels, AWidth, AHeight, APitch, AFormat, APalette, Area_, Area);
-    finally
-      Area_.Free;
-    end;
-  end;
+    Load(APixels, AWidth, AHeight, APitch, AFormat, APalette, TPTCArea.Create(0, 0, width, height), Area);
 end;
 
 procedure TWinCEGAPIConsole.Load(const APixels: Pointer;
                                  AWidth, AHeight, APitch: Integer;
-                                 const AFormat: TPTCFormat;
-                                 const APalette: TPTCPalette;
-                                 const ASource, ADestination: TPTCArea);
+                                 AFormat: IPTCFormat;
+                                 APalette: IPTCPalette;
+                                 ASource, ADestination: IPTCArea);
 var
   console_pixels: Pointer;
-  clipped_source, clipped_destination: TPTCArea;
-  tmp: TPTCArea;
-
+  clipped_source, clipped_destination: IPTCArea;
 begin
   CheckOpen(    'TWinCEGAPIConsole.Load(APixels, AWidth, AHeight, APitch, AFormat, APalette, ASource, ADestination)');
   CheckUnlocked('TWinCEGAPIConsole.Load(APixels, AWidth, AHeight, APitch, AFormat, APalette, ASource, ADestination)');
@@ -258,22 +220,13 @@ begin
   try
     console_pixels := Lock;
     try
-      clipped_source := TPTCArea.Create;
-      clipped_destination := TPTCArea.Create;
-      tmp := TPTCArea.Create(0, 0, AWidth, AHeight);
-      try
-        TPTCClipper.Clip(ASource, tmp, clipped_source, ADestination, Clip, clipped_destination);
-      finally
-        tmp.Free;
-      end;
+      TPTCClipper.Clip(ASource, TPTCArea.Create(0, 0, AWidth, AHeight), clipped_source, ADestination, Clip, clipped_destination);
       FCopy.request(AFormat, Format);
       FCopy.palette(APalette, Palette);
       FCopy.copy(APixels, clipped_source.left, clipped_source.top, clipped_source.width, clipped_source.height, APitch,
                  console_pixels, clipped_destination.left, clipped_destination.top, clipped_destination.width, clipped_destination.height, Pitch);
     finally
       Unlock;
-      clipped_source.Free;
-      clipped_destination.Free;
     end;
   except
     on error: TPTCError do
@@ -283,23 +236,20 @@ end;
 
 procedure TWinCEGAPIConsole.Save(APixels: Pointer;
                                  AWidth, AHeight, APitch: Integer;
-                                 const AFormat: TPTCFormat;
-                                 const APalette: TPTCPalette);
-
+                                 AFormat: IPTCFormat;
+                                 APalette: IPTCPalette);
 begin
 end;
 
 procedure TWinCEGAPIConsole.Save(APixels: Pointer;
                                  AWidth, AHeight, APitch: Integer;
-                                 const AFormat: TPTCFormat;
-                                 const APalette: TPTCPalette;
-                                 const ASource, ADestination: TPTCArea);
-
+                                 AFormat: IPTCFormat;
+                                 APalette: IPTCPalette;
+                                 ASource, ADestination: IPTCArea);
 begin
 end;
 
 function TWinCEGAPIConsole.Lock: Pointer;
-
 begin
   CheckUnlocked('display already locked');
   Result := GXBeginDraw;
@@ -311,7 +261,6 @@ begin
 end;
 
 procedure TWinCEGAPIConsole.Unlock;
-
 begin
   if not FLocked then
     raise TPTCError.Create('display is not locked');
@@ -323,27 +272,22 @@ begin
 end;
 
 procedure TWinCEGAPIConsole.Clear;
-
 begin
 end;
 
-procedure TWinCEGAPIConsole.Clear(const AColor: TPTCColor);
-
+procedure TWinCEGAPIConsole.Clear(AColor: IPTCColor);
 begin
 end;
 
-procedure TWinCEGAPIConsole.Clear(const AColor: TPTCColor;
-                    const AArea: TPTCArea);
-
+procedure TWinCEGAPIConsole.Clear(AColor: IPTCColor;
+                                  AArea: IPTCArea);
 begin
 end;
 
-procedure TWinCEGAPIConsole.Configure(const AFileName: String);
-
+procedure TWinCEGAPIConsole.Configure(const AFileName: string);
 var
   F: Text;
   S: string;
-
 begin
   AssignFile(F, AFileName);
   {$push}{$I-}
@@ -357,14 +301,13 @@ begin
     Readln(F, S);
     {$pop}
     if IOResult <> 0 then
-      Break;
+      break;
     Option(S);
   end;
   CloseFile(F);
 end;
 
-function TWinCEGAPIConsole.Option(const AOption: String): Boolean;
-
+function TWinCEGAPIConsole.Option(const AOption: string): Boolean;
 begin
   LOG('console option', AOption);
 
@@ -373,47 +316,33 @@ begin
   Result := FCopy.Option(AOption);
 end;
 
-procedure TWinCEGAPIConsole.Palette(const APalette: TPTCPalette);
-
+procedure TWinCEGAPIConsole.Palette(APalette: IPTCPalette);
 begin
 end;
 
-procedure TWinCEGAPIConsole.Clip(const AArea: TPTCArea);
-
-var
-  tmp: TPTCArea;
-
+procedure TWinCEGAPIConsole.Clip(AArea: IPTCArea);
 begin
   CheckOpen('TWinCEGAPIConsole.Clip(AArea)');
 
-  tmp := TPTCClipper.Clip(AArea, FArea);
-  try
-    FClip.Assign(tmp);
-  finally
-    tmp.Free;
-  end;
+  FClip := TPTCClipper.Clip(AArea, FArea);
 end;
 
-function TWinCEGAPIConsole.Clip: TPTCArea;
-
+function TWinCEGAPIConsole.Clip: IPTCArea;
 begin
   CheckOpen('TWinCEGAPIConsole.Clip');
   Result := FClip;
 end;
 
-function TWinCEGAPIConsole.Palette: TPTCPalette;
-
+function TWinCEGAPIConsole.Palette: IPTCPalette;
 begin
 end;
 
-function TWinCEGAPIConsole.Modes: PPTCMode;
-
+function TWinCEGAPIConsole.Modes: TPTCModeList;
 begin
-  Result := @FModes[0];
+  Result := FModes;
 end;
 
 function TWinCEGAPIConsole.WndProc(Ahwnd: HWND; AuMsg: UINT; AwParam: WPARAM; AlParam: LPARAM): LRESULT;
-
 begin
   case AuMsg of
   WM_CLOSE: begin
@@ -458,7 +387,6 @@ begin
 end;
 
 procedure TWinCEGAPIConsole.Flush;
-
 begin
   CheckOpen    ('TWinCEGAPIConsole.Flush');
   CheckUnlocked('TWinCEGAPIConsole.Flush');
@@ -467,7 +395,6 @@ begin
 end;
 
 procedure TWinCEGAPIConsole.Finish;
-
 begin
   CheckOpen    ('TWinCEGAPIConsole.Finish');
   CheckUnlocked('TWinCEGAPIConsole.Finish');
@@ -476,7 +403,6 @@ begin
 end;
 
 procedure TWinCEGAPIConsole.Update;
-
 begin
   CheckOpen    ('TWinCEGAPIConsole.Update');
   CheckUnlocked('TWinCEGAPIConsole.Update');
@@ -484,19 +410,16 @@ begin
   FWindow.Update;
 end;
 
-procedure TWinCEGAPIConsole.Update(const AArea: TPTCArea);
-
+procedure TWinCEGAPIConsole.Update(AArea: IPTCArea);
 begin
   Update;
 end;
 
-function TWinCEGAPIConsole.NextEvent(var AEvent: TPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean;
-
+function TWinCEGAPIConsole.NextEvent(out AEvent: IPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean;
 begin
   CheckOpen('TWinCEGAPIConsole.NextEvent');
 //  CheckUnlocked('TWinCEGAPIConsole.NextEvent');
 
-  FreeAndNil(AEvent);
   repeat
     { update window }
     FWindow.Update;
@@ -507,8 +430,7 @@ begin
   Result := AEvent <> nil;
 end;
 
-function TWinCEGAPIConsole.PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): TPTCEvent;
-
+function TWinCEGAPIConsole.PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): IPTCEvent;
 begin
   CheckOpen('TWinCEGAPIConsole.PeekEvent');
 //  CheckUnlocked('TWinCEGAPIConsole.PeekEvent');
@@ -523,68 +445,58 @@ begin
 end;
 
 function TWinCEGAPIConsole.GetWidth: Integer;
-
 begin
   CheckOpen('TWinCEGAPIConsole.GetWidth');
   Result := FDisplayWidth;
 end;
 
 function TWinCEGAPIConsole.GetHeight: Integer;
-
 begin
   CheckOpen('TWinCEGAPIConsole.GetHeight');
   Result := FDisplayHeight;
 end;
 
 function TWinCEGAPIConsole.GetPitch: Integer;
-
 begin
   CheckOpen('TWinCEGAPIConsole.GetPitch');
   Result := FDisplayPitch;
 end;
 
-function TWinCEGAPIConsole.GetArea: TPTCArea;
-
+function TWinCEGAPIConsole.GetArea: IPTCArea;
 begin
   CheckOpen('TWinCEGAPIConsole.GetArea');
   Result := FArea;
 end;
 
-function TWinCEGAPIConsole.GetFormat: TPTCFormat;
-
+function TWinCEGAPIConsole.GetFormat: IPTCFormat;
 begin
   CheckOpen('TWinCEGAPIConsole.GetFormat');
   Result := FDisplayFormat;
 end;
 
 function TWinCEGAPIConsole.GetPages: Integer;
-
 begin
   CheckOpen('TWinCEGAPIConsole.GetPages');
   Result := 1; {???}
 end;
 
 function TWinCEGAPIConsole.GetName: string;
-
 begin
   Result := 'GAPI';
 end;
 
 function TWinCEGAPIConsole.GetTitle: string;
-
 begin
   CheckOpen('TWinCEGAPIConsole.GetTitle');
   Result := FTitle;
 end;
 
 function TWinCEGAPIConsole.GetInformation: string;
-
 begin
   Result := ''; // todo...
 end;
 
 procedure TWinCEGAPIConsole.CheckOpen(    AMessage: String);
-
 begin
   if not FOpen then
   try
@@ -596,7 +508,6 @@ begin
 end;
 
 procedure TWinCEGAPIConsole.CheckUnlocked(AMessage: String);
-
 begin
   if FLocked then
   try

+ 3 - 3
packages/ptc/src/wince/gdi/wincebitmapinfod.inc

@@ -30,11 +30,11 @@
 }
 
 type
-  TWinCEBitmapInfo = Class(TObject)
+  TWinCEBitmapInfo = class(TObject)
   private
     FBitmapInfo: PBITMAPINFO;
 //    FPixels: Pointer;
-    FFormat: TPTCFormat;
+    FFormat: IPTCFormat;
     FWidth, FHeight, FPitch: Integer;
   public
     constructor Create(AWidth, AHeight: Integer);
@@ -43,6 +43,6 @@ type
     property Width: Integer read FWidth;
     property Height: Integer read FHeight;
     property Pitch: Integer read FPitch;
-    property Format: TPTCFormat read FFormat;
+    property Format: IPTCFormat read FFormat;
 //    property Pixels: Pointer read FPixels;
   end;

+ 0 - 3
packages/ptc/src/wince/gdi/wincebitmapinfoi.inc

@@ -33,7 +33,6 @@
 {TODO: create DIBs with the same color depth as the desktop}
 
 constructor TWinCEBitmapInfo.Create(AWidth, AHeight: Integer);
-
 begin
   FBitmapInfo := GetMem(SizeOf(BITMAPINFOHEADER) + 12);
 
@@ -67,10 +66,8 @@ begin
 end;
 
 destructor TWinCEBitmapInfo.Destroy;
-
 begin
 //  FreeMem(FPixels);
   FreeMem(FBitmapInfo);
-  FFormat.Free;
   inherited Destroy;
 end;

+ 38 - 38
packages/ptc/src/wince/gdi/wincegdiconsoled.inc

@@ -30,7 +30,7 @@
 }
 
 type
-  TWinCEGDIConsole = Class(TPTCBaseConsole)
+  TWinCEGDIConsole = class(TPTCBaseConsole)
   private
     FWindow: TWinCEWindow;
     FBitmap: HBitmap;
@@ -41,10 +41,10 @@ type
 
     FCopy: TPTCCopy;
     FClear: TPTCClear;
-    FArea: TPTCArea;
-    FClip: TPTCArea;
+    FArea: IPTCArea;
+    FClip: IPTCArea;
     FEventQueue: TEventQueue;
-    FModes: array [0..1] of TPTCMode;
+    FModes: array of IPTCMode;
 
     FOpen: Boolean;
     FLocked: Boolean;
@@ -53,80 +53,80 @@ type
 
     FDisplayWidth: Integer;
     FDisplayHeight: Integer;
-    FDefaultFormat: TPTCFormat;
+    FDefaultFormat: IPTCFormat;
 
     function WndProc(Ahwnd: HWND; AuMsg: UINT; AwParam: WPARAM; AlParam: LPARAM): LRESULT;
 
     function GetWidth: Integer; override;
     function GetHeight: Integer; override;
     function GetPitch: Integer; override;
-    function GetArea: TPTCArea; override;
-    function GetFormat: TPTCFormat; override;
+    function GetArea: IPTCArea; override;
+    function GetFormat: IPTCFormat; override;
     function GetPages: Integer; override;
     function GetName: string; override;
     function GetTitle: string; override;
     function GetInformation: string; override;
 
-    procedure CheckOpen(    AMessage: String);
-    procedure CheckUnlocked(AMessage: String);
+    procedure CheckOpen(    AMessage: string);
+    procedure CheckUnlocked(AMessage: string);
   public
     constructor Create; override;
     destructor Destroy; override;
 
     procedure Open(const ATitle: string; APages: Integer = 0); overload; override;
-    procedure Open(const ATitle: string; const AFormat: TPTCFormat;
+    procedure Open(const ATitle: string; AFormat: IPTCFormat;
                    APages: Integer = 0); overload; override;
     procedure Open(const ATitle: string; AWidth, AHeight: Integer;
-                   const AFormat: TPTCFormat; APages: Integer = 0); overload; override;
-    procedure Open(const ATitle: string; const AMode: TPTCMode;
+                   AFormat: IPTCFormat; APages: Integer = 0); overload; override;
+    procedure Open(const ATitle: string; AMode: IPTCMode;
                    APages: Integer = 0); overload; override;
     procedure Close; override;
 
-    procedure Copy(ASurface: TPTCBaseSurface); override;
-    procedure Copy(ASurface: TPTCBaseSurface;
-                   const ASource, ADestination: TPTCArea); override;
+    procedure Copy(ASurface: IPTCSurface); override;
+    procedure Copy(ASurface: IPTCSurface;
+                   ASource, ADestination: IPTCArea); override;
 
     procedure Load(const APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette); override;
     procedure Load(const APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette;
-                   const ASource, ADestination: TPTCArea); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette;
+                   ASource, ADestination: IPTCArea); override;
     procedure Save(APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette); override;
     procedure Save(APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette;
-                   const ASource, ADestination: TPTCArea); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette;
+                   ASource, ADestination: IPTCArea); override;
 
     function Lock: Pointer; override;
     procedure Unlock; override;
 
     procedure Clear; override;
-    procedure Clear(const AColor: TPTCColor); override;
-    procedure Clear(const AColor: TPTCColor;
-                    const AArea: TPTCArea); override;
+    procedure Clear(AColor: IPTCColor); override;
+    procedure Clear(AColor: IPTCColor;
+                    AArea: IPTCArea); override;
 
-    procedure Configure(const AFileName: String); override;
-    function Option(const AOption: String): Boolean; override;
+    procedure Configure(const AFileName: string); override;
+    function Option(const AOption: string): Boolean; override;
 
-    procedure Palette(const APalette: TPTCPalette); override;
-    procedure Clip(const AArea: TPTCArea); override;
-    function Clip: TPTCArea; override;
-    function Palette: TPTCPalette; override;
-    function Modes: PPTCMode; override;
+    procedure Palette(APalette: IPTCPalette); override;
+    procedure Clip(AArea: IPTCArea); override;
+    function Clip: IPTCArea; override;
+    function Palette: IPTCPalette; override;
+    function Modes: TPTCModeList; override;
 
     procedure Flush; override;
     procedure Finish; override;
     procedure Update; override;
-    procedure Update(const AArea: TPTCArea); override;
+    procedure Update(AArea: IPTCArea); override;
 
-    function NextEvent(var AEvent: TPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean; override;
-    function PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): TPTCEvent; override;
+    function NextEvent(out AEvent: IPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean; override;
+    function PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): IPTCEvent; override;
   end;

+ 46 - 136
packages/ptc/src/wince/gdi/wincegdiconsolei.inc

@@ -30,7 +30,6 @@
 }
 
 constructor TWinCEGDIConsole.Create;
-
 begin
   inherited Create;
 
@@ -43,15 +42,11 @@ begin
   FArea := TPTCArea.Create;
   FClip := TPTCArea.Create;
 
+  SetLength(FModes, 1);
   FModes[0] := TPTCMode.Create(FDisplayWidth, FDisplayHeight, FDefaultFormat);
-  FModes[1] := TPTCMode.Create;
 end;
 
 destructor TWinCEGDIConsole.Destroy;
-
-var
-  I: Integer;
-
 begin
   Close;
 
@@ -60,43 +55,31 @@ begin
   FEventQueue.Free;
   FCopy.Free;
   FClear.Free;
-  FArea.Free;
-  FClip.Free;
-  FDefaultFormat.Free;
-
-  for I := Low(FModes) to High(FModes) do
-    FModes[I].Free;
 
   inherited Destroy;
 end;
 
 procedure TWinCEGDIConsole.Open(const ATitle: string; APages: Integer = 0);
-
 begin
   Open(ATitle, FDefaultFormat, APages);
 end;
 
-procedure TWinCEGDIConsole.Open(const ATitle: string; const AFormat: TPTCFormat;
+procedure TWinCEGDIConsole.Open(const ATitle: string; AFormat: IPTCFormat;
                                 APages: Integer = 0);
-
 begin
   Open(ATitle, FDisplayWidth, FDisplayHeight, AFormat, APages);
 end;
 
-procedure TWinCEGDIConsole.Open(const ATitle: string; const AMode: TPTCMode;
+procedure TWinCEGDIConsole.Open(const ATitle: string; AMode: IPTCMode;
                                 APages: Integer = 0);
-
 begin
   Open(ATitle, AMode.Width, AMode.Height, AMode.Format, APages);
 end;
 
 procedure TWinCEGDIConsole.Open(const ATitle: string; AWidth, AHeight: Integer;
-                                const AFormat: TPTCFormat; APages: Integer = 0);
-
+                                AFormat: IPTCFormat; APages: Integer = 0);
 var
   DeviceContext: HDC;
-  tmp: TPTCArea;
-
 begin
   LOG('TWinCEGDIConsole.Open');
 
@@ -137,13 +120,8 @@ begin
   if FBitmap = 0 then
     raise TPTCError.Create('could not create dib section');
 
-  tmp := TPTCArea.Create(0, 0, FDisplayWidth, FDisplayHeight);
-  try
-    FArea.Assign(tmp);
-    FClip.Assign(tmp);
-  finally
-    tmp.Free;
-  end;
+  FArea := TPTCArea.Create(0, 0, FDisplayWidth, FDisplayHeight);
+  FClip := FArea;
 
   FEventQueue := TEventQueue.Create;
   FKeyboard := TWinCEKeyboard.Create(FEventQueue);
@@ -157,7 +135,6 @@ begin
 end;
 
 procedure TWinCEGDIConsole.Close;
-
 begin
   LOG('TWinCEGDIConsole.Close');
 
@@ -179,27 +156,23 @@ begin
   FOpen := False;
 end;
 
-procedure TWinCEGDIConsole.Copy(ASurface: TPTCBaseSurface);
-
+procedure TWinCEGDIConsole.Copy(ASurface: IPTCSurface);
 begin
   {todo...}
 end;
 
-procedure TWinCEGDIConsole.Copy(ASurface: TPTCBaseSurface;
-                                const ASource, ADestination: TPTCArea);
-
+procedure TWinCEGDIConsole.Copy(ASurface: IPTCSurface;
+                                ASource, ADestination: IPTCArea);
 begin
   {todo...}
 end;
 
 procedure TWinCEGDIConsole.Load(const APixels: Pointer;
                                 AWidth, AHeight, APitch: Integer;
-                                const AFormat: TPTCFormat;
-                                const APalette: TPTCPalette);
+                                AFormat: IPTCFormat;
+                                APalette: IPTCPalette);
 var
-  Area_: TPTCArea;
   console_pixels: Pointer;
-
 begin
   CheckOpen(    'TWinCEGDIConsole.Load(APixels, AWidth, AHeight, APitch, AFormat, APalette)');
   CheckUnlocked('TWinCEGDIConsole.Load(APixels, AWidth, AHeight, APitch, AFormat, APalette)');
@@ -221,50 +194,30 @@ begin
     end;
   end
   else
-  begin
-    Area_ := TPTCArea.Create(0, 0, width, height);
-    try
-      Load(APixels, AWidth, AHeight, APitch, AFormat, APalette, Area_, Area);
-    finally
-      Area_.Free;
-    end;
-  end;
+    Load(APixels, AWidth, AHeight, APitch, AFormat, APalette, TPTCArea.Create(0, 0, width, height), Area);
 end;
 
 procedure TWinCEGDIConsole.Load(const APixels: Pointer;
                                 AWidth, AHeight, APitch: Integer;
-                                const AFormat: TPTCFormat;
-                                const APalette: TPTCPalette;
-                                const ASource, ADestination: TPTCArea);
+                                AFormat: IPTCFormat;
+                                APalette: IPTCPalette;
+                                ASource, ADestination: IPTCArea);
 var
   console_pixels: Pointer;
-  clipped_source, clipped_destination: TPTCArea;
-  tmp: TPTCArea;
-
+  clipped_source, clipped_destination: IPTCArea;
 begin
   CheckOpen(    'TWinCEGDIConsole.Load(APixels, AWidth, AHeight, APitch, AFormat, APalette, ASource, ADestination)');
   CheckUnlocked('TWinCEGDIConsole.Load(APixels, AWidth, AHeight, APitch, AFormat, APalette, ASource, ADestination)');
-  clipped_source := nil;
-  clipped_destination := nil;
   try
     console_pixels := Lock;
     try
-      clipped_source := TPTCArea.Create;
-      clipped_destination := TPTCArea.Create;
-      tmp := TPTCArea.Create(0, 0, AWidth, AHeight);
-      try
-        TPTCClipper.Clip(ASource, tmp, clipped_source, ADestination, Clip, clipped_destination);
-      finally
-        tmp.Free;
-      end;
+      TPTCClipper.Clip(ASource, TPTCArea.Create(0, 0, AWidth, AHeight), clipped_source, ADestination, Clip, clipped_destination);
       FCopy.request(AFormat, Format);
       FCopy.palette(APalette, Palette);
       FCopy.copy(APixels, clipped_source.left, clipped_source.top, clipped_source.width, clipped_source.height, APitch,
                  console_pixels, clipped_destination.left, clipped_destination.top, clipped_destination.width, clipped_destination.height, Pitch);
     finally
       Unlock;
-      clipped_source.Free;
-      clipped_destination.Free;
     end;
   except
     on error: TPTCError do
@@ -274,61 +227,52 @@ end;
 
 procedure TWinCEGDIConsole.Save(APixels: Pointer;
                                 AWidth, AHeight, APitch: Integer;
-                                const AFormat: TPTCFormat;
-                                const APalette: TPTCPalette);
-
+                                AFormat: IPTCFormat;
+                                APalette: IPTCPalette);
 begin
   {todo...}
 end;
 
 procedure TWinCEGDIConsole.Save(APixels: Pointer;
                                 AWidth, AHeight, APitch: Integer;
-                                const AFormat: TPTCFormat;
-                                const APalette: TPTCPalette;
-                                const ASource, ADestination: TPTCArea);
-
+                                AFormat: IPTCFormat;
+                                APalette: IPTCPalette;
+                                ASource, ADestination: IPTCArea);
 begin
   {todo...}
 end;
 
 function TWinCEGDIConsole.Lock: Pointer;
-
 begin
   Result := FBitmapPixels; // todo...
   FLocked := True;
 end;
 
 procedure TWinCEGDIConsole.Unlock;
-
 begin
   FLocked := False;
 end;
 
 procedure TWinCEGDIConsole.Clear;
-
 begin
   {todo...}
 end;
 
-procedure TWinCEGDIConsole.Clear(const AColor: TPTCColor);
-
+procedure TWinCEGDIConsole.Clear(AColor: IPTCColor);
 begin
   {todo...}
 end;
 
-procedure TWinCEGDIConsole.Clear(const AColor: TPTCColor;
-                                 const AArea: TPTCArea);
-
+procedure TWinCEGDIConsole.Clear(AColor: IPTCColor;
+                                 AArea: IPTCArea);
 begin
   {todo...}
 end;
 
-procedure TWinCEGDIConsole.Configure(const AFileName: String);
-
+procedure TWinCEGDIConsole.Configure(const AFileName: string);
 var
   F: Text;
   S: string;
-
 begin
   AssignFile(F, AFileName);
   {$push}{$I-}
@@ -348,8 +292,7 @@ begin
   CloseFile(F);
 end;
 
-function TWinCEGDIConsole.Option(const AOption: String): Boolean;
-
+function TWinCEGDIConsole.Option(const AOption: string): Boolean;
 begin
   LOG('console option', AOption);
 
@@ -358,61 +301,45 @@ begin
   Result := FCopy.Option(AOption);
 end;
 
-procedure TWinCEGDIConsole.Palette(const APalette: TPTCPalette);
-
+procedure TWinCEGDIConsole.Palette(APalette: IPTCPalette);
 begin
   {todo...}
 end;
 
-procedure TWinCEGDIConsole.Clip(const AArea: TPTCArea);
-
-var
-  tmp: TPTCArea;
-
+procedure TWinCEGDIConsole.Clip(AArea: IPTCArea);
 begin
   CheckOpen('TWinCEGDIConsole.Clip(AArea)');
 
-  tmp := TPTCClipper.Clip(AArea, FArea);
-  try
-    FClip.Assign(tmp);
-  finally
-    tmp.Free;
-  end;
+  FClip := TPTCClipper.Clip(AArea, FArea);
 end;
 
-function TWinCEGDIConsole.Clip: TPTCArea;
-
+function TWinCEGDIConsole.Clip: IPTCArea;
 begin
   CheckOpen('TWinCEGDIConsole.Clip');
   Result := FClip;
 end;
 
-function TWinCEGDIConsole.Palette: TPTCPalette;
-
+function TWinCEGDIConsole.Palette: IPTCPalette;
 begin
   {todo...}
 end;
 
-function TWinCEGDIConsole.Modes: PPTCMode;
-
+function TWinCEGDIConsole.Modes: TPTCModeList;
 begin
-  Result := @FModes[0];
+  Result := FModes;
 end;
 
 procedure TWinCEGDIConsole.Flush;
-
 begin
   {todo...}
 end;
 
 procedure TWinCEGDIConsole.Finish;
-
 begin
   {todo...}
 end;
 
 function TWinCEGDIConsole.WndProc(Ahwnd: HWND; AuMsg: UINT; AwParam: WPARAM; AlParam: LPARAM): LRESULT;
-
 begin
   case AuMsg of
   WM_CLOSE: begin
@@ -442,11 +369,9 @@ begin
 end;
 
 procedure TWinCEGDIConsole.Update;
-
 var
   ClientRect: RECT;
   DeviceContext, DeviceContext2: HDC;
-
 begin
   CheckOpen(    'TWinCEGDIConsole.Update');
   CheckUnlocked('TWinCEGDIConsole.Update');
@@ -464,11 +389,11 @@ begin
       begin
         SelectObject(DeviceContext2, FBitmap);
 
-	StretchBlt(DeviceContext,
-	           0, 0, ClientRect.right, ClientRect.bottom,
-		   DeviceContext2,
-		   0, 0, FBitmapInfo.Width, FBitmapInfo.Height,
-		   SRCCOPY);
+        StretchBlt(DeviceContext,
+                   0, 0, ClientRect.right, ClientRect.bottom,
+                   DeviceContext2,
+                   0, 0, FBitmapInfo.Width, FBitmapInfo.Height,
+                   SRCCOPY);
 
         DeleteDC(DeviceContext2);
       end;
@@ -478,20 +403,17 @@ begin
   end;
 end;
 
-procedure TWinCEGDIConsole.Update(const AArea: TPTCArea);
-
+procedure TWinCEGDIConsole.Update(AArea: IPTCArea);
 begin
   {todo...}
   Update;
 end;
 
-function TWinCEGDIConsole.NextEvent(var AEvent: TPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean;
-
+function TWinCEGDIConsole.NextEvent(out AEvent: IPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean;
 begin
   CheckOpen('TWinCEGDIConsole.NextEvent');
 //  CheckUnlocked('TWinCEGDIConsole.NextEvent');
 
-  FreeAndNil(AEvent);
   repeat
     { update window }
     FWindow.Update;
@@ -502,8 +424,7 @@ begin
   Result := AEvent <> nil;
 end;
 
-function TWinCEGDIConsole.PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): TPTCEvent;
-
+function TWinCEGDIConsole.PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): IPTCEvent;
 begin
   CheckOpen('TWinCEGDIConsole.PeekEvent');
 //  CheckUnlocked('TWinCEGDIConsole.PeekEvent');
@@ -518,69 +439,59 @@ begin
 end;
 
 function TWinCEGDIConsole.GetWidth: Integer;
-
 begin
   CheckOpen('TWinCEGDIConsole.GetWidth');
   Result := FBitmapInfo.Width;
 end;
 
 function TWinCEGDIConsole.GetHeight: Integer;
-
 begin
   CheckOpen('TWinCEGDIConsole.GetHeight');
   Result := FBitmapInfo.Height;
 end;
 
 function TWinCEGDIConsole.GetPitch: Integer;
-
 begin
   CheckOpen('TWinCEGDIConsole.GetPitch');
   Result := FBitmapInfo.Pitch;
 end;
 
-function TWinCEGDIConsole.GetFormat: TPTCFormat;
-
+function TWinCEGDIConsole.GetFormat: IPTCFormat;
 begin
   CheckOpen('TWinCEGDIConsole.GetFormat');
   Result := FBitmapInfo.Format;
 end;
 
-function TWinCEGDIConsole.GetArea: TPTCArea;
-
+function TWinCEGDIConsole.GetArea: IPTCArea;
 begin
   CheckOpen('TWinCEGDIConsole.GetArea');
   Result := FArea;
 end;
 
 function TWinCEGDIConsole.GetPages: Integer;
-
 begin
   CheckOpen('TWinCEGDIConsole.GetPages');
   Result := 2;
 end;
 
 function TWinCEGDIConsole.GetName: string;
-
 begin
   Result := 'WinCE';
 end;
 
 function TWinCEGDIConsole.GetTitle: string;
-
 begin
   CheckOpen('TWinCEGDIConsole.GetTitle');
   Result := FTitle;
 end;
 
 function TWinCEGDIConsole.GetInformation: string;
-
 begin
   CheckOpen('TWinCEGDIConsole.GetInformation');
   Result := ''; // todo...
 end;
 
-procedure TWinCEGDIConsole.CheckOpen(AMessage: String);
-
+procedure TWinCEGDIConsole.CheckOpen(AMessage: string);
 begin
   if not FOpen then
   try
@@ -591,8 +502,7 @@ begin
   end;
 end;
 
-procedure TWinCEGDIConsole.CheckUnlocked(AMessage: String);
-
+procedure TWinCEGDIConsole.CheckUnlocked(AMessage: string);
 begin
   if FLocked then
   try

+ 30 - 30
packages/ptc/src/x11/x11consoled.inc

@@ -36,7 +36,7 @@ type
     FX11Display: TX11Display;
     FTitle: string;
     FFlags: TX11Flags;
-    FModes: array of TPTCMode;
+    FModes: array of IPTCMode;
 
     procedure UpdateCursor;
     procedure UpdateMouseGrab;
@@ -46,8 +46,8 @@ type
     function GetWidth: Integer; override;
     function GetHeight: Integer; override;
     function GetPitch: Integer; override;
-    function GetArea: TPTCArea; override;
-    function GetFormat: TPTCFormat; override;
+    function GetArea: IPTCArea; override;
+    function GetFormat: IPTCFormat; override;
     function GetPages: Integer; override;
     function GetName: string; override;
     function GetTitle: string; override;
@@ -57,59 +57,59 @@ type
     destructor Destroy; override;
 
     procedure Open(const ATitle: string; APages: Integer = 0); overload; override;
-    procedure Open(const ATitle: string; const AFormat: TPTCFormat;
+    procedure Open(const ATitle: string; AFormat: IPTCFormat;
                    APages: Integer = 0); overload; override;
     procedure Open(const ATitle: string; AWidth, AHeight: Integer;
-                   const AFormat: TPTCFormat; APages: Integer = 0); overload; override;
-    procedure Open(const ATitle: string; const AMode: TPTCMode;
+                   AFormat: IPTCFormat; APages: Integer = 0); overload; override;
+    procedure Open(const ATitle: string; AMode: IPTCMode;
                    APages: Integer = 0); overload; override;
     procedure Close; override;
 
-    procedure Copy(ASurface: TPTCBaseSurface); override;
-    procedure Copy(ASurface: TPTCBaseSurface;
-                   const ASource, ADestination: TPTCArea); override;
+    procedure Copy(ASurface: IPTCSurface); override;
+    procedure Copy(ASurface: IPTCSurface;
+                   ASource, ADestination: IPTCArea); override;
 
     procedure Load(const APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette); override;
     procedure Load(const APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette;
-                   const ASource, ADestination: TPTCArea); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette;
+                   ASource, ADestination: IPTCArea); override;
     procedure Save(APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette); override;
     procedure Save(APixels: Pointer;
                    AWidth, AHeight, APitch: Integer;
-                   const AFormat: TPTCFormat;
-                   const APalette: TPTCPalette;
-                   const ASource, ADestination: TPTCArea); override;
+                   AFormat: IPTCFormat;
+                   APalette: IPTCPalette;
+                   ASource, ADestination: IPTCArea); override;
 
     function Lock: Pointer; override;
     procedure Unlock; override;
 
     procedure Clear; override;
-    procedure Clear(const AColor: TPTCColor); override;
-    procedure Clear(const AColor: TPTCColor;
-                    const AArea: TPTCArea); override;
+    procedure Clear(AColor: IPTCColor); override;
+    procedure Clear(AColor: IPTCColor;
+                    AArea: IPTCArea); override;
 
     procedure Configure(const AFileName: String); override;
     function Option(const AOption: String): Boolean; override;
 
-    procedure Palette(const APalette: TPTCPalette); override;
-    procedure Clip(const AArea: TPTCArea); override;
-    function Clip: TPTCArea; override;
-    function Palette: TPTCPalette; override;
-    function Modes: PPTCMode; override;
+    procedure Palette(APalette: IPTCPalette); override;
+    procedure Clip(AArea: IPTCArea); override;
+    function Clip: IPTCArea; override;
+    function Palette: IPTCPalette; override;
+    function Modes: TPTCModeList; override;
 
     procedure Flush; override;
     procedure Finish; override;
     procedure Update; override;
-    procedure Update(const AArea: TPTCArea); override;
+    procedure Update(AArea: IPTCArea); override;
 
-    function NextEvent(var AEvent: TPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean; override;
-    function PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): TPTCEvent; override;
+    function NextEvent(out AEvent: IPTCEvent; AWait: Boolean; const AEventMask: TPTCEventMask): Boolean; override;
+    function PeekEvent(AWait: Boolean; const AEventMask: TPTCEventMask): IPTCEvent; override;
   end;

Alguns arquivos não foram mostrados porque muitos arquivos mudaram nesse diff