home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Pascal / HISOFTPASCAL2,0-1.DMS / in.adf / HSPascal / AmigaDemos / Init.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-05-20  |  12.1 KB  |  383 lines

  1. {--------------------------------------------------------------------------
  2.  
  3.                      HighSpeed Pascal for the Amiga
  4.  
  5.                    ROUTINES FOR DEMO INITIALIZATION
  6.  
  7.                   Programmed by Martin Eskildsen 1991
  8.  
  9.                   Copyright (c) 1991 by D-House I ApS
  10.                          All rights reserved
  11.  
  12.  
  13.   Version : Date (dd.mm.yy) : Comment
  14.   -----------------------------------
  15.     1.00 : 06.11.91 : First version
  16.     1.01 : 16.12.91 : System unit names updated
  17.     1.10 : 22.04.92 : Updated for new units, Wait() fixed
  18.  
  19. --------------------------------------------------------------------------}
  20.  
  21. unit Init;
  22.  
  23. INTERFACE
  24.  
  25. uses Exec, Intuition, Graphics, Amiga;
  26.  
  27. const
  28.   ScrWidth  = 640;                  { Size of custom screen }
  29.   ScrHeight = 200;                  { Should not be changed! }
  30.  
  31. function PrepareEnvironment(s : string) : boolean;
  32.                                     { Prepare an environment for the demo }
  33. procedure CloseDown;                { De-init environment }
  34.  
  35. procedure Inform(s : string);       { Write message }
  36. procedure Message(s : string);      { Write message and wait for acknowledement }
  37. procedure WaitMessageClose;         { Wait for msg. window close gadget }
  38.  
  39. function Panic(condition : boolean; s : string) : boolean;
  40.                                     { Write panic message s if condition is }
  41.                                     { TRUE and return condition as result }
  42.  
  43. procedure WaitClose(var w : pWindow);   { Wait for user to click window w's close gadget }
  44. procedure EnableClose(var w : pWindow); { Enable Close messages }
  45. procedure DisableClose(var w : pWindow);{ Disable Close messages }
  46.  
  47. procedure OpenOutputWindow;         { Create a standard demo window }
  48. procedure CloseOutputWindow;        { Remove it again }
  49. procedure ClearOutputWindow;        { Clear work area }
  50.  
  51. function CStrConstPtr(s : string) : pointer;
  52. { This makes s a "C" string, allocates a chunck of heap large enough for s
  53.   to reside therein, puts s in the new memory and returns a pointer to it.
  54.   Please note that the memory is never released again in the program's
  55.   lifespan. This avoids global variables containing screen/window titles. }
  56.  
  57. function RetrieveStr(p : pointer) : string;
  58. { Pick a "C" string from memory pointed at by p and make it Pascal string }
  59.  
  60. function Max(a, b : integer) : integer;
  61. function Min(a, b : integer) : integer;
  62. procedure SwapMin(var a, b : integer);    { Make sure a <= b }
  63.  
  64. function LegalPosition(x, y : integer) : boolean; { Is (x,y) inside output? }
  65.  
  66. function Binary(s : string) : integer;  { Make binary value from s }
  67. { s can contain any character but only 0 and 1 are used for evauation }
  68.  
  69.  
  70. PROCEDURE W(CH:CHAR);
  71.  
  72.  
  73. var
  74.   BaseScreen   : pScreen;
  75.   TopOffset    : integer;           { First raster line usable by demo }
  76.   OutputWinDef : tNewWindow;        { Definition of output window }
  77.                                     { The variable is set up in the }
  78.                                     { unit but made global so the user can }
  79.                                     { alter it before calling }
  80.                                     { OpenOutputWindow. }
  81.   OutputWindow : pWindow;           { The actual output window }
  82.   OutputTitle  : String;            { Output window's title }
  83.   WorkArea     : record             { Actual usable area in window }
  84.                    minX, maxX,
  85.                    minY, maxY  : integer
  86.                  end;
  87.  
  88. IMPLEMENTATION
  89.  
  90.  
  91. PROCEDURE W(CH:CHAR); BEGIN WRITE(CH) END;
  92.  
  93.  
  94. const
  95.   IRev         = 0;                 { Required Intuition revision }
  96.   GRev         = 0;                 { Required Graphics revision }
  97.   Detail       = 0;
  98.   Block        = 1;
  99.  
  100. var
  101.   MsgWindow    : pWindow;           { The message window }
  102.   FontInfo     : tTextAttr;
  103.  
  104. procedure CloseEnvironment;
  105. var junk: Boolean;
  106. begin
  107.   CloseWindow(MsgWindow);                 { Remove the message window }
  108.   {$ifdef WORKBENCH_2}
  109.   junk :=
  110.   {$endif}
  111.   CloseScreen(BaseScreen);                { and the screen }
  112.   CloseLibrary(pLibrary(IntuitionBase));  { Close Intuition }
  113.   CloseLibrary(pLibrary(GfxBase))         { and Graphics }
  114. end;
  115.  
  116. function PrepareEnvironment(s : string) : boolean;
  117. label 1;                         { Disaster termination point }
  118. var
  119.   status        : boolean;       { TRUE = everything went ok }
  120.   BaseScreenDef : tNewScreen;    { Record defining the custom screen }
  121.   MsgWindowDef  : tNewWindow;    { Record defining the message window }
  122.  
  123.   procedure DefineStdOutputWindow;
  124.   begin
  125.     with OutputWinDef do begin
  126.       LeftEdge    := 10;
  127.       TopEdge     := TopOffset;
  128.       Width       := ScrWidth - 2 * LeftEdge;
  129.       Height      := ScrHeight - TopOffset - 5;
  130.       DetailPen   := Detail;
  131.       BlockPen    := Block;
  132.       Title       := @OutputTitle[1];
  133.       Flags       := WINDOWCLOSE or SMART_REFRESH or WINDOWDEPTH or NOCAREREFRESH;
  134.       IDCMPflags  := CLOSEWINDOW_;
  135.       Type_       := CUSTOMSCREEN;
  136.       FirstGadget := NIL;
  137.       CheckMark   := NIL;
  138.       Screen      := BaseScreen;
  139.       BitMap      := NIL;
  140.       MinWidth    := Width;
  141.       MinHeight   := Height;
  142.       MaxWidth    := MinWidth;
  143.       MaxHeight   := MaxHeight
  144.     end;
  145.     OutputTitle   := 'Output'#0;
  146.     OutputWindow  := NIL
  147.   end;
  148.  
  149. begin
  150.   status := FALSE;
  151.   TopOffset := 0;
  152.  
  153.   IntuitionBase := pIntuitionBase(OpenLibrary('intuition.library', IRev));
  154.                                  { Note the typecast pIntuitionBase(... }
  155.   if Panic(IntuitionBase = NIL, 'intuition.library could not be opened') then goto 1;
  156.  
  157.   GfxBase := pGfxBase(OpenLibrary('graphics.library', GRev));
  158.   if Panic(GfxBase = NIL, 'graphics.library could not be opened') then goto 1;
  159.  
  160.   with FontInfo do begin
  161.     ta_Name    := CStrConstPtr('topaz.font');
  162.     ta_YSize   := TOPAZ_EIGHTY;
  163.     ta_Style   := FS_NORMAL;
  164.     ta_Flags   := FPF_ROMFONT
  165.   end;
  166.  
  167.   with BaseScreenDef do begin
  168.     LeftEdge   := 0;                { MUST be 0! }
  169.     TopEdge    := 0;
  170.     Width      := ScrWidth;
  171.     Height     := ScrHeight;
  172.     Depth      := 2;                { Two bit planes = four colors }
  173.     DetailPen  := Detail;           { Color for details }
  174.     BlockPen   := Block;            { and for blocks }
  175.     ViewModes  := HIRES;            { High resolution }
  176.     Type_      := CUSTOMSCREEN;     { Note the underscore '_' }
  177.     Font       := @FontInfo;        { Use the normal Topaz font }
  178.     DefaultTitle := CStrConstPtr('HighSpeed Pascal for the Amiga! Copyright (c) 1991 by D-House I ApS');
  179.     Gadgets      := NIL;            { No gadgets }
  180.     CustomBitMap := NIL             { No bitmap }
  181.   end;
  182.   BaseScreen := OpenScreen(@BaseScreenDef);  { Note the @ operator }
  183.   if Panic(BaseScreen = NIL, 'Could not open demo screen') then begin
  184.     CloseLibrary(pLibrary(IntuitionBase));   { No screen! Close libs and }
  185.     CloseLibrary(pLibrary(GfxBase));         { get out of here! }
  186.     goto 1
  187.   end;
  188.  
  189.   with MsgWindowDef do begin
  190.     LeftEdge      := 10;
  191.     TopEdge       := 15;
  192.     Width         := ScrWidth - 2 * LeftEdge;
  193.     Height        := 28;
  194.     DetailPen     := Detail;
  195.     BlockPen      := Block;
  196.     Title         := CStrConstPtr('Messages. Use the Close gadget to accept/continue');
  197.     Flags         := WINDOWCLOSE or       { Add Close gadget and }
  198.                      WINDOWDEPTH or       { depth arrangement gadgets }
  199.                      SMART_REFRESH or     { Save window in RAM }
  200.                      ACTIVATE or          { Activate it }
  201.                      NOCAREREFRESH;       { Don't wanna hear of refreshes! }
  202.     IDCMPFlags    := CLOSEWINDOW_;        { But of user-clicks on Close }
  203.     Type_         := CUSTOMSCREEN;        { Put window in custom screen }
  204.     FirstGadget   := NIL;                 { No gadgets attached }
  205.     CheckMark     := NIL;                 { Same checkmark as usual }
  206.     Screen        := BaseScreen;          { Use our own custom screen }
  207.     BitMap        := NIL;                 { No bitmap }
  208.     MinWidth      := Width;               { Dummies as we can't resize }
  209.     MinHeight     := Height;              { this window }
  210.     MaxWidth      := MinWidth;
  211.     MaxHeight     := MinHeight
  212.   end;
  213.   MsgWindow := OpenWindow(@MsgWindowDef);
  214.   if Panic(MsgWindow = NIL, 'Can not open message window') then begin
  215.     CloseEnvironment;
  216.     goto 1
  217.   end;
  218.   DisableClose(MsgWindow);                { See WindowDemo.pas for explanation }
  219.  
  220.   status := TRUE;       { No Gurus! (yet...) Announce it to the world }
  221.  
  222.   TopOffset := MsgWindowDef.TopEdge +  { Top of window plus }
  223.                MsgWindowDef.Height  +  { window's height plus }
  224.                10;                     { a margin }
  225.  
  226.   DefineStdOutputWindow;
  227.   Message('Welcome to the ' + s + ' Demo!');
  228.  
  229.   1:                    { Where to go if the world turns against you }
  230.   PrepareEnvironment := status
  231. end;
  232.  
  233. procedure CloseDown;
  234. begin
  235.   if OutputWindow <> NIL then             { <> NIL = window still on screen }
  236.     CloseOutputWindow;                    { so we close it }
  237.   Message('That''s all folks!');
  238.   CloseEnvironment
  239. end;
  240.  
  241. procedure Inform(s : string);
  242. begin
  243.   while length(s) < 73 do s := s + ' ';   { Pad with spaces (simple, eh?) }
  244.   s := copy(s, 1, 73);                    { Truncate string to 73 chars }
  245.   with MsgWindow^ do begin
  246.     Move_(RPort, 20, 20);                 { Put text at (20,20) }
  247.     Text_(RPort, @s[1], length(s))        { Output it }
  248.   end
  249. end;
  250.  
  251. procedure WaitClose(var w : pWindow);              { Wait for the user to }
  252. var dummy : longint;                               { click the Close gadget }
  253. begin                                              { in window w }
  254.   EnableClose(w);
  255.   dummy := Wait(BitMask(w^.UserPort^.MP_SIGBIT));
  256.   DisableClose(w)
  257. end;
  258.  
  259. procedure EnableClose(var w : pWindow);
  260. begin
  261.   with w^ do IDCMPflags := IDCMPflags or CLOSEWINDOW_
  262. end;
  263.  
  264. procedure DisableClose(var w : pWindow);
  265. begin
  266.   with w^ do IDCMPflags := IDCMPflags and not CLOSEWINDOW_
  267. end;
  268.  
  269. procedure Message(s : string);
  270. begin
  271.   Inform(s);                              { Output message }
  272.   WaitClose(MsgWindow)                    { Wait for Close }
  273. end;
  274.  
  275. procedure WaitMessageClose;
  276. begin
  277.   WaitClose(MsgWindow)
  278. end;
  279.  
  280. function Panic(condition : boolean; s : string) : boolean;
  281. begin
  282.   if condition then Message('Demo problem: ' + s + ' - terminating soon.');
  283.   Panic := condition
  284. end;
  285.  
  286. procedure OpenOutputWindow;
  287. begin
  288.   OutputTitle := OutputTitle + #0;        { Just to be sure }
  289.   OutputWinDef.Title := @OutputTitle[1];
  290.   OutputWindow := OpenWindow(@OutputWinDef);
  291.   if Panic(OutputWindow = NIL, 'Can''t open output window') then begin
  292.     CloseDown;
  293.     halt(0)
  294.   end;
  295.   SetApen(OutputWindow^.RPort, 3);
  296.   with OutputWindow^, WorkArea do begin
  297.     minX := BorderLeft;
  298.     minY := BorderTop;
  299.     maxX := Width  - BorderRight;
  300.     maxY := Height - BorderBottom
  301.   end
  302. end;
  303.  
  304. procedure CloseOutputWindow;
  305. begin
  306.   CloseWindow(OutputWindow);
  307.   OutputWindow := NIL
  308. end;
  309.  
  310. procedure ClearOutputWindow;
  311. begin
  312.   with WorkArea, OutputWindow^ do begin
  313.     SetAPen(RPort, 0);
  314.     RectFill(RPort, minX, minY, maxX, maxY);
  315.     SetAPen(RPort, 3)
  316.   end
  317. end;
  318.  
  319. function CStrConstPtr(s : string) : pointer;
  320. type a = packed array [0..255] of char;
  321. var  p : ^a;
  322. begin
  323.   s := s + #0;                         { Make "C" string }
  324.   getmem(p, length(s));                { Get some mem for it }
  325.   move(s[1], p^, length(s));           { Move s into newly alloc'd mem }
  326.   CStrConstPtr := p                    { Return the pointer }
  327. end;
  328.  
  329. function RetrieveStr(p : pointer) : string;
  330. type
  331.   a = packed array [0..255] of char;
  332. var
  333.   i    : integer;
  334.   sptr : ^a;
  335.   s    : string;
  336. begin
  337.   sptr := p;
  338.   s := '';
  339.   i := 0;
  340.   while sptr^[i] <> #0 do begin
  341.     s := s + sptr^[i];
  342.     inc(i)
  343.   end;
  344.   RetrieveStr := s
  345. end;
  346.  
  347. function Max(a, b : integer) : integer;
  348. begin
  349.   if a > b then Max := a else Max := b
  350. end;
  351.  
  352. function Min(a, b : integer) : integer;
  353. begin
  354.   if a < b then Min := a else Min := b
  355. end;
  356.  
  357. procedure SwapMin(var a, b : integer);
  358. var t : integer;
  359. begin
  360.   if a > b then begin
  361.     t := a;
  362.     a := b;
  363.     b := t
  364.   end
  365. end;
  366.  
  367. function LegalPosition(x, y : integer) : boolean;
  368. begin
  369.   with WorkArea do LegalPosition := (x >= minX) and (x <= maxX) and
  370.                                     (y >= minY) and (y <= maxY)
  371. end;
  372.  
  373. function Binary(s : string) : integer;
  374. var i, n : integer;
  375. begin
  376.   n := 0;
  377.   for i := 1 to length(s) do
  378.     if s[i] in ['0', '1'] then n := n*2 + ord(s[i]) - ord('0');
  379.   Binary := n
  380. end;
  381.  
  382. end.
  383.