Trees in Pascal

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

fpc trees.pp

and enjoy!


Program Trees;

uses
  brick, ctypes;

{ Constants for the program }
const
  SCREEN_W = 384;
  SCREEN_H = 240;
  LAYER_CT = 5;
  TREE_CT = 4;


{ A couple of useful functions - first, a random number function with a lower and upper limit }
function myRandom(min, max:integer):integer;
  begin
    myRandom := min + random(max - min + 1)
  end;

{ And second, a function to limit an integer to a range }
function limit(min, val, max:integer):integer;
  begin
    if val < min then
      limit := min
    else
      begin
        if val > max then
          limit := max
        else
          limit := val
      end
  end;


var
  { counters }
  i, j : integer;

  { the different layers used to simulate depth-of-field }
  layers : array[1..LAYER_CT] of integer;

  { trees and mountain, and a temp sprite used in placing the scenery }
  sprites : array[1..TREE_CT+1] of Pbr_sprite;
  sprite : Pbr_sprite;

  { the file names of the trees }
  tree_srcs : array[1..TREE_CT] of Pchar = ('tree1.png', 'tree2.png', 'tree3.png', 'tree4.png');
  tree_amt : array[1..TREE_CT+1] of integer = (0, 12, 24, 60, 120);
  mtn_src : PChar = 'mountain1.png';

  { the camera position }
  cx : integer = 0;
  cy : integer = 0;

  { the input record }
  io : Tbr_input;


begin
  { Fire up the engine! }
  InitBrick;

  { Let's load each png - mountain first .. }
  sprites[TREE_CT+1] := SpriteCreate;
  SpriteAddFrame(sprites[TREE_CT+1], FrameConvert( FrameFromDisk( mtn_src, Nil), FRAME_LT, Nil));

  { then the trees. }
  for i := 1 to TREE_CT do
    begin
      sprites[i] := SpriteCreate;
      SpriteAddFrame(sprites[i], FrameConvert( FrameFromDisk( tree_srcs[i], Nil), FRAME_LT, Nil));
    end;


  { Let's make some layers - each layer after the mountain layer has trees }
  for i := 1 to LAYER_CT do
    begin

      layers[i] := LayerAdd;

      { place a mountain on the first layer .. }
      if i = 1 then
        begin
          sprite := SpriteCopy(sprites[TREE_CT+1]);
          SpriteSetPosition(sprite, 0, 80);
          ListAdd( LayerGetSpriteList(layers[i]), sprite)
        end
      else
        begin

          { all other layers get trees }
          for j := 1 to tree_amt[i] do
            begin
              sprite := SpriteCopy(sprites[myRandom(1, TREE_CT)]);
              SpriteSetPosition(sprite, myRandom(20, (i-1)*600), myRandom( (i-1)*10+90, (i-1)*20+120));
              ListAdd( LayerGetSpriteList(layers[i]), sprite)
            end;

        end;

    end;


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

  while true do
    begin

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

      { Update the camera position }
      cx := cx + io.hat[0].horiz;
      cy := cy + io.hat[0].vert;
      cx := limit(20, cx, 200);

      { And scroll the layers .. move the mountain at half speed and the rest
        at multiples of the camera speed, to give a nice parallax effect }
      LayerSetCamera(layers[1], cx div 2, 0);

      for i := 2 to LAYER_CT do
        LayerSetCamera(layers[i], cx*(i), 0);

      { 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/trees_in_pascal.txt · Last modified: 2010/06/07 22:00 by steve