-- File CIFScreen.mesa
-- Written by Martin Newell, June 1980
-- Last updated: November 13, 1980 2:54 PM

DIRECTORY

CIFControlDefs: FROM "CIFControlDefs" USING [DrawCIF],
CIFDevicesDefs: FROM "CIFDevicesDefs" USING [DeviceDescriptor, DeviceDescriptorRecord,
RegisterDevice, MaxLENGTHLayerArray],
CIFOutputDefs: FROM "CIFOutputDefs" USING [SetSorting],
CIFScreenDefs: FROM "CIFScreenDefs",
CIFUtilitiesDefs: FROM "CIFUtilitiesDefs" USING [Rectangle,
GetClipRectangle, SetClipRectangle, DrawClipRectangle, ClearClipRectangle,
GetDisplayContext, GetBaseContext],
Graphics: FROM "Graphics" USING [DisplayContext, SetTexture, Texture, DisplayString,
Scale, PushContext, PopContext, MoveTo],
Real: FROM "Real" USING [Fix];

CIFScreen: PROGRAM
IMPORTS CIFControlDefs, CIFDevicesDefs, CIFOutputDefs, CIFUtilitiesDefs, Graphics, Real
EXPORTS CIFScreenDefs =

BEGIN OPEN CIFControlDefs, CIFDevicesDefs, CIFOutputDefs, CIFUtilitiesDefs, Graphics, Real;


-- Screen procedures

ScreenDeviceRecord: DeviceDescriptorRecord ← [
next:NIL,
name:"screen",
deviceSelect: ScreenSelect,
deviceDrawFrame: ScreenDrawFrame,
deviceSetScale: ScreenSetScale,
deviceSetClipRegion: ScreenSetClipRegion,
deviceOutput: ScreenOutput,
deviceLayer: ScreenLayer,
deviceLoadLayer: ScreenLoadLayer,
deviceRectangle: ScreenRectangle,
deviceStartPoly: ScreenStartPoly,
devicePolyVertex: ScreenPolyVertex,
deviceEndPoly: ScreenEndPoly,
deviceText: ScreenText
];

ScreenSelect: PROCEDURE RETURNS[BOOLEAN] =
BEGIN
--
ScreenSetClipRegion[GetClipRectangle[]];
RETURN[TRUE];
END;

ScreenDrawFrame: PROCEDURE =
BEGIN
DrawClipRectangle[];
END;

ScreenSetScale: PROCEDURE [factor: REAL] =
BEGIN
dc: DisplayContext ← GetDisplayContext[];
PopContext[dc];
PushContext[dc];
Scale[dc, [factor,factor]];
END;

ScreenSetClipRegion: PROCEDURE [rt: Rectangle] =
BEGIN
SetClipRectangle[rt];
END;

ScreenOutput: PROCEDURE =
--expects nothing
BEGIN
r: Rectangle ← GetClipRectangle[];
ClearClipRectangle[];
ScreenDrawFrame[];
SetSorting[none];
ScreenCurrentLayer ← 32000; --i.e. not set
DrawCIF[Fix[r.llx],Fix[r.urx],Fix[r.lly],Fix[r.ury]];
END;

ScreenLayer: PROCEDURE [layer: CARDINAL] =
BEGIN
IF layer#ScreenCurrentLayer THEN
BEGIN
SetTexture[GetDisplayContext[], Stipple[layer]];
ScreenCurrentLayer ← layer;
END;
END;

ScreenLoadLayer: PROCEDURE[layer:CARDINAL, v0,v1,v2,v3: CARDINAL] =
BEGIN
Stipple[layer] ← v0;
END;

--dummies:

ScreenRectangle: PROCEDURE [r: Rectangle] =
BEGIN
END;

ScreenStartPoly: PROCEDURE [x,y: REAL] =
BEGIN
END;

ScreenPolyVertex: PROCEDURE [x,y: REAL] =
BEGIN
END;

ScreenEndPoly: PROCEDURE =
BEGIN
END;

ScreenText: PROCEDURE[text: STRING, x,y: REAL] =
--x,y are in BaseContext
BEGIN
bc: DisplayContext ← GetBaseContext[];
MoveTo[bc, [x,y]];
DisplayString[bc, text];
END;


--Private procedures--

ScreenCurrentLayer: CARDINAL;

Stipple: ARRAY [0..MaxLENGTHLayerArray) OF Texture ← ALL[177777B];
--undef


--set up device context

screenClipRectangle: Rectangle ← GetClipRectangle[];
screenClipRectangle ← [screenClipRectangle.llx+1,screenClipRectangle.lly+1,
screenClipRectangle.urx-1,screenClipRectangle.ury-1];
SetClipRectangle[screenClipRectangle]; --kludge to avoid clipping problems

--Set up default stipples
Stipple[0] ← 000020B;
--implant
Stipple[1] ← 040501B;
--diffusion
Stipple[2] ← 014102B;
--poly
Stipple[3] ← 165627B;
--contact
Stipple[4] ← 000050B;
--metal
Stipple[5] ← 001110B;
--buried
Stipple[6] ← 001122B;
--glass
Stipple[7] ← 177777B;
--undef

RegisterDevice[@ScreenDeviceRecord];

END.