'*****************************************************************************
'*                                                                           *
'*                           AC/BASIC COMPILER                               *
'*                          ===================                              *
'*                                                                           *
'* This program does some minor work to get the machine in Hold And Modify   *
'* mode.  After that, it paints the screen full of random colors and then    *
'* undoes all that it did.  If an error occurs, it cleans up any mess it may *
'* have made, announces the error (using SAY as we don't know the state of   *
'* the screen/window system if trouble arises somewhere), and bails out.     *
'*                                                                           *
'* Note: To fully understand Hold And Modify graphics, you should examine    *
'*       the ROM Kernel Manual: Libraries and Devices publication.  This     *
'*       manual discusses the graphics library in detail.  An understanding  * 
'*       of Intution is also essential.  See the Intuition manual.           *
'*                                                                           *
'* Another Note:  This program is reasonably unsophisticated insofar as no   *
'*                routines check for keyboard/mouse activity in the active   *
'*                window/screen.  This means that trying to dump the program *
'*                with control-c won't work.                                 *
'*****************************************************************************
'* 
'* Modification History:
'*
'* 29 Mar 1988  Original version prepared for AC/BASIC v1.3 release        CCG
'*


'----------------------------------------------------
'- Various setup stuff                              -
'----------------------------------------------------

dim newscreen%(15)
dim newwindow%(23)

'--- define all the external stuff required --

declare function OpenScreen&() library
declare function OpenWindow&() library
declare function ViewPortAddress&() library

library "graphics.library"
library "dos.library"
library "intuition.library"


'-----------------------------------------------------
'- Initialization code.  Get the machine in HAM mode -
'-----------------------------------------------------

'--- set up the screen definition ---

'We create our own data structures to create NewWindow/NewScreen 
'definitions.  These are then passed into ROM Kernel routines to create 
'windows and screens that BASIC can't create.
'
'Note: Since AC/BASIC doesn't support structures, we use arrays to achieve
'      the same results.

newscreen%( 2 ) = 320   '-- Screen width
newscreen%( 3 ) = 200   '-- Screen height
newscreen%( 4 ) = 6     '-- Screen depth
newscreen%( 6 ) = &H800 '-- Hold And Modify mode
newscreen%( 7 ) = &Hf   '-- Custom screen

'--- set up the window definition ---

newwindow%( 2 ) = 320 '-- Window width
newwindow%( 3 ) = 200 '-- Window height
newwindow%( 4 ) = &H1 '-- DetailPen, BlockPen
newwindow%( 19) = 320 '-- more width stuff
newwindow%( 20) = 200
newwindow%( 21) = 320
newwindow%( 22) = 200
newwindow%( 23) = &Hf '-- Custom screen

'Create a borderless window..could use other types with various
'gadgets if you need them.  For this example, we shalln't worry about them.

pokel varptr(newwindow%(7)),&H1800 '-- Borderless|Activate

'--- get open the screen and window ---

'Note that we're using ROM Kernel calls to get these guys up, not
'the usual BASIC WINDOW/SCREEN statements.  This means that the
'runtime system (or interpreter environment) doesn't know about them
'and can't do any kind of window/screen management on them.

OurScreen& = OpenScreen&( varptr( newscreen%( 0 )) )
if OurScreen& = 0 then call abort( "error during open screen." )

pokel varptr(newwindow%(15)),OurScreen&
OurWindow& = OpenWindow&( varptr( newwindow%( 0 )) )
if OurWindow& = 0 then call abort( "error during open window." )

'--- get pointers to the viewport and rastport ---

OurViewPort& = ViewPortAddress&( OurWindow& )
OurRastPort& = peekl( OurWindow& + 50 )



'-------------------------------------------------------
'- Use the information we got to draw some lines       -
'- on the HAM screen.                                  -
'-                                                     -
'- Note that normal BASIC graphics commands are not    -
'- appropriate.  You must use ROM service routines.    -
'-------------------------------------------------------

randomize timer
starty% = 1
for startx% = 1 to 199
    y% = starty%
    for x% = startx% to 320
        pen% = int(rnd*31+1)
        call SetAPen( OurRastPort&, pen% )
        call WritePixel(OurRastPort&,x%,y%)
    next x%
    for y% = starty% to 200
        x% = startx% + 1
        pen% = int(rnd*31+1)
        call SetAPen( OurRastPort&,pen% )
        call WritePixel( OurRastPort&,x%,y% )
    next y%
    starty% = starty% + 1
next startx%

'-- All done, pause for about 15 seconds and then exit --
a& = timer
while timer-a& < 15 : wend

'----------------------------------------------------
'- Game Over.  Wrap up everything                   -
'----------------------------------------------------

call CleanUpEverything()
end

sub abort( errormessage$ ) static
'--- Come here if something nasty happens
say translate$( errormessage$ )
call CleanUpEverything()
system
end sub

sub CleanUpEverything() static
shared OurScreen&, OurWindow&
if OurWindow& <> 0 then call CloseWindow( OurWindow& )
if OurScreen& <> 0 then call CloseScreen( OurScreen& )
library close
end sub
