'===========================================================================
' Subject: PCX VIEWER FOR BASEC               Date: 03-24-99 (19:07)       
'  Author: The ABC Programmer                 Code: BEC                    
'  Origin: voxel@edmc.net                   Packet: BASEC.ABC
'===========================================================================
' 640x480x256 PCX Viewer  (only standard palette) for BASEC v0.15
' By William Yu,  Code based on Tony Jones' ASIC PCX Viewer.
'
' Actually, a PCX loader is already contained in GRAPHICS.LIB, however,
' there's no way to call it, because I didn't implement it :)

DIM FileName AS STRING
DIM ID AS INTEGER
DIM BCode AS BYTE
DIM NumBytes AS INTEGER
DIM Counter AS LONG
DIM PCXFILE AS INTEGER
DIM X AS INTEGER, Y AS INTEGER
DIM K$ AS STRING

FileName = "PCXFILE.PCX"

PCXFile = FREEFILE

OPEN "R", PCXFile, FileName

'' -- Check to see if the file exist
IF LOF(PCXFile) = 0 THEN
 PRINT "File not found: "; FileName
 CLOSE PCXFile
 KILL FileName
 END
END IF

GET PCXFile,, ID

IF ID <> 1290 THEN
  PRINT "Error in header"
  PRINT FileName; " must be a true 256 PCX file."
  CLOSE PCXFile
  END
END IF

SEEK PCXFile, 129

SCREEN 12

'' Decode the PCX data

Counter = 0
X = 0: Y = 0
WHILE Counter < 640*480

 GET PCXFile,,BCode

 IF BCode > 191 THEN
   '' How many bytes are in the run.
   NumBytes = BCode - 192

   '' Get the actual data for the run
   GET PCXFile,,BCode

   Counter = Counter + NumBytes
   IF NumBytes > 640 - X THEN
     LINE (X, Y)-(639, Y), BCode
     NumBytes = NumBytes - (640 - X)
     X = 0
     Y = Y + 1
     LINE (X, Y)-(X+NumBytes-1, Y), BCode
   ELSE
     LINE (X, Y)-(X+NumBytes, Y), BCode
   END IF

   X = X + NumBytes
   IF X = 640 THEN X = 0: Y = Y + 1

 ELSE
   '' Else just put the data on the screen

   PSET (X,Y), BCode
   Counter = Counter + 1
   X = X + 1
   IF X = 640 THEN X = 0: Y = Y + 1
 END IF

WEND

CLOSE PCXFile

K$ = INPUT$(1)

SCREEN 0

END
