summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/ptc/examples/land.pp
blob: 1abe255d1f739dbd0cc4820587995b0f5d7d62bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
{
Ported to FPC by Nikolay Nikolov (nickysn@users.sourceforge.net)
}

{
 Land demo for OpenPTC 1.0 C++ API

 Based on Heightmap example from Hornet (RIP)
 PTC version Copyright (c) 1998 Marcus Fletcher (cus@commsat.demon.co.uk)

 Updated to OpenPTC 1.0 by Glenn Fiedler (ptc@gaffer.org)

 Cursor keys to move, <Pause> to brake and <Esc> to quit
}

program Land;

{$MODE objfpc}

uses
  ptc;

const
  SCREENWIDTH = 320;
  SCREENHEIGHT = 200;

  FOV: Integer = 256; { half of the xy field of view (This is based on the 0-2048 convention) }

var
  HMap: array [0..256*256 - 1] of Uint8; { Height field }
  CMap: array [0..256*256 - 1] of Uint8; { Color map }

  lasty, { Last pixel drawn on a given column }
  lastc: array [0..SCREENWIDTH - 1] of Integer; { Color of last pixel on a column }
  CosT, SinT: array [0..2047] of Integer; { Cosine and Sine tables }

{ Reduces a value to 0..255 (used in height field computation) }
function Clamp(x: Integer): Integer;
begin
  if x < 0 then
    Result := 0
  else
    if x > 255 then
      Result := 255
    else
      Result := x;
end;

{ Heightfield and colormap computation }
procedure ComputeMap;
var
  p, i, j, k, k2, p2, a, b, c, d: Integer;
begin
  { Start from a plasma clouds fractal }
  HMap[0] := 128;
  p := 256;
  while p > 1 do
  begin
    p2 := p shr 1;
    k := p * 8 + 20;
    k2 := k shr 1;
    i := 0;
    while i < 256 do
    begin
      j := 0;
      while j < 256 do
      begin
        a := HMap[(i shl 8) + j];
        b := HMap[(((i + p) and 255) shl 8) + j];
        c := HMap[(i shl 8) + ((j + p) and 255)];
        d := HMap[(((i + p) and 255) shl 8) + ((j + p) and 255)];

        HMap[(i shl 8) + ((j + p2) and 255)] :=
          Clamp(((a + c) shr 1) + (Random(k) - k2));
        HMap[(((i + p2) and 255) shl 8) + ((j + p2) and 255)] :=
          Clamp(((a + b + c + d) shr 2) + (Random(k) - k2));
        HMap[(((i + p2) and 255) shl 8) + j] :=
          Clamp(((a + b) shr 1) + (Random(k) - k2));
        Inc(j, p);
      end;
      Inc(i, p);
    end;
    p := p2;
  end;

  { Smoothing }
  for k := 0 to 2 do
  begin
    i := 0;
    while i < 256*256 do
    begin
      for j := 0 to 255 do
        HMap[i + j] := (HMap[((i + 256) and $FF00) + j] +
                        HMap[i + ((j + 1) and $FF)] +
                        HMap[((i - 256) and $FF00) + j] +
                        HMap[i + ((j - 1) and $FF)]) shr 2;
      Inc(i, 256);
    end;
  end;

  { Color computation (derivative of the height field) }
  i := 0;
  while i < 256*256 do
  begin
    for j := 0 to 255 do
    begin
      k := 128 + (HMap[((i + 256) and $FF00) + ((j + 1) and 255)] - HMap[i + j])*4;
      if k < 0 then
        k := 0;
      if k > 255 then
        k := 255;
      CMap[i + j] := k;
    end;
    Inc(i, 256);
  end;
end;

{ Calculate the lookup tables }
procedure InitTables;
var
  a: Integer;
  result: Single;
begin
  for a := 0 to 2047 do
  begin
    { Precalculate cosine }
    result := cos(a * PI / 1024) * 256;
    CosT[a] := Trunc(result);

    { and sine }
    result := sin(a * PI / 1024) * 256;
    SinT[a] := Trunc(result);
  end;
end;

{
 Draw a "section" of the landscape; x0,y0 and x1,y1 and the xy coordinates
 on the height field, hy is the viewpoint height, s is the scaling factor
 for the distance. x0,y0,x1,y1 are 16.16 fixed point numbers and the
 scaling factor is a 16.8 fixed point value.
}
procedure Line(x0, y0, x1, y1, hy, s: Integer; surface_buffer: PUint32; fadeout: Integer);
var
  sx, sy, i, a, b, u0, u1, v0, v1, h0, h1, h2, h3, h, c, y: Integer;
  coord_x, coord_y, sc, cc, currentColor: Integer;
  pixel: PUint32;
begin
  { Compute xy speed }
  sx := (x1 - x0) div SCREENWIDTH;
  sy := (y1 - y0) div SCREENWIDTH;

  for i := 0 to SCREENWIDTH - 1 do
  begin
    { Compute the xy coordinates; a and b will be the position inside the }
    { single map cell (0..255). }
    a := (x0 shr 8) and $FF;
    b := (y0 shr 8) and $FF;

    u0 := (x0 shr 16) and $FF;
    u1 := (u0 + 1) and $FF;
    v0 := (y0 shr 8) and $FF00;
    v1 := (v0 + 256) and $FF00;

    { Fetch the height at the four corners of the square the point is in }
    h0 := HMap[u0 + v0];
    h1 := HMap[u1 + v0];
    h2 := HMap[u0 + v1];
    h3 := HMap[u1 + v1];

    { Compute the height using bilinear interpolation }
    h0 := (h0 shl 8) + a * (h1 - h0);
    h2 := (h2 shl 8) + a * (h3 - h2);
    h := ((h0 shl 8) + b * (h2 - h0)) shr 16;

    { Fetch the color at the centre of the square the point is in }
    h0 := CMap[u0 + v0];
    h1 := CMap[u1 + v0];
    h2 := CMap[u0 + v1];
    h3 := CMap[u1 + v1];

    { Compute the color using bilinear interpolation (in 16.16) }
    h0 := (h0 shl 8) + a * (h1 - h0);
    h2 := (h2 shl 8) + a * (h3 - h2);
    c := ((h0 shl 8) + b * (h2 - h0));

    { Compute screen height using the scaling factor }
    y := (((h - hy) * s) shr 11) + (SCREENHEIGHT shr 1);

    { Draw the column }
    a := lasty[i];
    if y < a then
    begin
      coord_x := i;
      coord_y := a;
      if lastc[i] = -1 then
        lastc[i] := c;

      sc := (c - lastc[i]) div (a - y);
      cc := lastc[i];

      if a > (SCREENHEIGHT - 1) then
      begin
        Dec(coord_y, a - (SCREENHEIGHT - 1));
        a := SCREENHEIGHT - 1;
      end;
      if y < 0 then
        y := 0;

      while y < a do
      begin
        currentColor := cc shr 18;
        pixel := surface_buffer + (coord_y * SCREENWIDTH) + coord_x;
        pixel^ := ((currentColor shl 2) * (150 - fadeout) div 150) shl 8;
        Inc(cc, sc);
        Dec(coord_y);
        Dec(a);
      end;
      lasty[i] := y;
    end;
    lastc[i] := c;

    { Advance to next xy position }
    Inc(x0, sx); Inc(y0, sy);
  end;
end;

{ Draw the view from the point x0,y0 (16.16) looking at angle a }
procedure View(x0, y0, angle, height: Integer; surface_buffer: PUint32);
var
  d, u0, a, v0, u1, v1, h0, h1, h2, h3: Integer;
begin
  { Initialize last-y and last-color arrays }
  for d := 0 to SCREENWIDTH - 1 do
  begin
    lasty[d] := SCREENHEIGHT;
    lastc[d] := -1;
  end;

  { Compute the xy coordinates; a and b will be the position inside the }
  { single map cell (0..255). }
  u0 := (x0 shr 16) and $FF;
  a := (x0 shr 8) and $FF;
  v0 := (y0 shr 8) and $FF00;
  u1 := (u0 + 1) and $FF;
  v1 := (v0 + 256) and $FF00;

  { Fetch the height at the four corners of the square the point is in }
  h0 := HMap[u0 + v0];
  h1 := HMap[u1 + v0];
  h2 := HMap[u0 + v1];
  h3 := HMap[u1 + v1];

  { Compute the height using bilinear interpolation }
  h0 := (h0 shl 8) + a * (h1 - h0);
  h2 := (h2 shl 8) + a * (h3 - h2);

  { Draw the landscape from near to far without overdraw }
  d := 0;
  while d < 150 do
  begin
    Line(x0 + (d shl 8)*CosT[(angle - FOV) and $7FF],
         y0 + (d shl 8)*SinT[(angle - FOV) and $7FF],
         x0 + (d shl 8)*CosT[(angle + FOV) and $7FF],
         y0 + (d shl 8)*SinT[(angle + FOV) and $7FF],
         height, (100 shl 8) div (d + 1),
         surface_buffer,
         d);
    Inc(d, 1 + (d shr 6));
  end;
end;

var
  format: IPTCFormat;
  console: IPTCConsole;
  surface: IPTCSurface;
  timer: IPTCTimer;
  key: IPTCKeyEvent;
  pixels: PUint32;
  Done: Boolean;

  x0, y0: Integer;
  height: Integer;
  angle, deltaAngle, deltaSpeed, CurrentSpeed, scale, delta: Double;
  index: Integer;
begin
  Done := False;
  try
    try
      format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
      console := TPTCConsoleFactory.CreateNew;
      console.open('Land demo', SCREENWIDTH, SCREENHEIGHT, format);
      surface := TPTCSurfaceFactory.CreateNew(SCREENWIDTH, SCREENHEIGHT, format);

      { Compute the height map }
      ComputeMap;
      InitTables;

      x0 := 0;
      y0 := 0;

      height := -200;
      angle := 0;
      deltaAngle := 0;
      deltaSpeed := 4096;
      CurrentSpeed := deltaSpeed * 10;

      { time scaling constant }
      scale := 20;

      { create timer }
      timer := TPTCTimerFactory.CreateNew;

      { start timer }
      timer.start;

      { main loop }
      repeat
        { get time delta between frames }
        delta := timer.delta;

        { clear surface }
        surface.clear;

        { lock surface pixels }
        pixels := surface.lock;
        try
          { draw current landscape view }
          View(x0, y0, Trunc(angle), height, pixels);
        finally
          { unlock surface }
          surface.unlock;
        end;

        { copy surface to console }
        surface.copy(console);

        { update console }
        console.update;

        { check key press }
        while console.KeyPressed do
        begin
          { read key press }
          console.ReadKey(key);

          { handle key press }
          case key.code of
                        { increase speed }
            PTCKEY_UP: CurrentSpeed := CurrentSpeed + deltaSpeed * delta * scale;
                        { decrease speed }
            PTCKEY_DOWN: CurrentSpeed := CurrentSpeed - deltaSpeed * delta * scale;
                        { turn to the left }
            PTCKEY_LEFT: deltaAngle := deltaAngle - 1;
                        { turn to the right }
            PTCKEY_RIGHT: deltaAngle := deltaAngle + 1;
            PTCKEY_SPACE: begin
              { stop moving }
              CurrentSpeed := 0;
              deltaAngle := 0;
            end;
                           { exit }
            PTCKEY_ESCAPE: Done := True;
          end;
        end;

        { Update position/angle }
        angle := angle + deltaAngle * delta * scale;

        index := Trunc(angle) and $7FF;
        Inc(x0, Trunc(CurrentSpeed * CosT[index]) div 256);
        Inc(y0, Trunc(CurrentSpeed * SinT[index]) div 256);
      until Done;
    finally
      if Assigned(console) then
        console.close;
    end;
  except
    on error: TPTCError do
      { report error }
      error.report;
  end;
end.