Glass in Pascal

Save this code to your computer with an appropriate name (such as glass.pp) and download the glass images to go with it. Make sure you've built the Brick unit, and then compile the glass demo thus:

fpc glass.pp

and enjoy!


Program Glass;

uses
  brick, ctypes;

const
  { screen width and height }
  SCREEN_W = 256;
  SCREEN_H = 180;
  { displacement map dimensions, used in the stained glass panel }
  DISPL_W = 80;
  DISPL_H = 100;


var
  { a few general counters }
  i, j, k : integer;

  { our graphics layer }
  layer : integer;

  { the sprite variables - a stained glass panel and a bear }
  panel, bear : Pbr_sprite;

  { a color key for the bear sprite }
  key : Tbr_color;

  { a buffer to hold the pixel displacement data for the stained glass panel }
  displ : array[1..DISPL_W, 1..DISPL_H, 1..2] of cshort;

  { the bear sprite position }
  bx : integer = 0;
  by : integer = 0;

  { the input record }
  io : Tbr_input;


begin

  { Initialize brick and create the layer }
  InitBrick;

  layer := LayerAdd;
  LayerSetSorting(layer, 1);

  { Now, we're going to create the stained glass effect by combining two sprites:
    a color-adjustment sprite to give the glass some color, and a displacement
    sprite to give it the fuzzy, mottled look.  First we'll create a
    displacement map for the fuzzy effect, by making a list of small random
    pixel displacement values, which will comprise the displacement sprite. }
  for i := 1 to DISPL_W do
    for j := 1 to DISPL_H do
      for k := 1 to 2 do
        displ[i][j][k] := random(8) - 4;


  { Next, we'll load the stained glass. }
  panel := SpriteCreate;
  SpriteAddFrame(panel, FrameConvert(FrameFromDisk('window.png', Nil), FRAME_LT, Nil));
  SpriteAddSubframe(panel, 0, FrameCreate(FRAME_DISPL, DISPL_W, DISPL_H, @displ, Nil));

  { Position the stained glass panel and add it to the sprite list. }
  SpriteSetPosition(panel, 100, 20);
  SpriteSetZHint(panel, 10);
  ListAdd(LayerGetSpriteList(layer), panel);


  { And last, load the bear sprite, and set a color key to make
    sure that the magenta background is made transparent. }
  bear := SpriteCreate;
  with key do
    begin
      r := 255;
      g := 0;
      b := 255;
    end;

  SpriteAddFrame(bear, FrameCopy(FrameFromDisk('bear.png', @key)));
  ListAdd(LayerGetSpriteList(layer), bear);


  { Open the graphics display }
  GraphicsOpen(GRAPHICS_ACCEL, SCREEN_W, SCREEN_H, 0, 3);

  while true do
    begin

      { Read the keyboard/joystick input }
      IoFetch(0, @io);

      { Update the bear's position }
      bx := bx + io.hat[0].horiz;
      by := by + io.hat[0].vert;
      SpriteSetPosition(bear, bx, by);

      { Display the frame and run the delay loop }
      RenderDisplay;
      Delay(50);

      { Quit on escape or application-close }
      if (io.esc = 1) or (IoHasQuit = 1) then
        break;

    end;

end.

 
demos/glass_in_pascal.txt · Last modified: 2010/06/09 20:49 by steve