summaryrefslogtreecommitdiff
path: root/packages/ptc/examples/stretch.pp
blob: f7cf768b0d7b884072e4bfab371d31d60e82d739 (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
{
Ported to FPC by Nikolay Nikolov (nickysn@users.sourceforge.net)
}

{
 Stretch example for OpenPTC 1.0 C++ Implementation
 Copyright (c) Glenn Fiedler (ptc@gaffer.org)
 This source code is in the public domain
}

Program StretchExample;

{$MODE objfpc}

Uses
  ptc;

Procedure load(surface : TPTCSurface; filename : String);

Var
  F : File;
  width, height : Integer;
  pixels : PByte;
  y : Integer;
  tmp : TPTCFormat;
  tmp2 : TPTCPalette;

Begin
  { open image file }
  ASSign(F, filename);
  Reset(F, 1);

  { skip header }
  Seek(F, 18);

  { get surface dimensions }
  width := surface.width;
  height := surface.height;

  { allocate image pixels }
  pixels := GetMem(width * height * 3);
  Try
    { read image pixels one line at a time }
    For y := height - 1 DownTo 0 Do
      BlockRead(F, pixels[width * y * 3], width * 3);

    { load pixels to surface }
    tmp := TPTCFormat.Create(24, $00FF0000, $0000FF00, $000000FF);
    Try
      tmp2 := TPTCPalette.Create;
      Try
        surface.load(pixels, width, height, width * 3, tmp, tmp2);
      Finally
        tmp2.Free;
      End;
    Finally
      tmp.Free;
    End;
  Finally
    { free image pixels }
    FreeMem(pixels);
  End;
End;

Var
  console : TPTCConsole;
  surface : TPTCSurface;
  image : TPTCSurface;
  format : TPTCFormat;
  timer : TPTCTimer;
  area : TPTCArea;
  color : TPTCColor;
  time : Double;
  zoom : Single;
  x, y, x1, y1, x2, y2, dx, dy : Integer;

Begin
  format := Nil;
  color := Nil;
  timer := Nil;
  image := Nil;
  surface := Nil;
  console := Nil;
  Try
    Try
      { create console }
      console := TPTCConsole.Create;

      { create format }
      format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);

      { open the console }
      console.open('Stretch example', format);

      { create surface matching console dimensions }
      surface := TPTCSurface.Create(console.width, console.height, format);

      { create image surface }
      image := TPTCSurface.Create(320, 140, format);

      { load image to surface }
      load(image, 'stretch.tga');

      { setup stretching parameters }
      x := surface.width Div 2;
      y := surface.height Div 2;
      dx := surface.width Div 2;
      dy := surface.height Div 3;

      { create timer }
      timer := TPTCTimer.Create;

      { start timer }
      timer.start;
      color := TPTCColor.Create(1, 1, 1);

      { loop until a key is pressed }
      While Not console.KeyPressed Do
      Begin
        { get current time from timer }
        time := timer.time;

        { clear surface to white background }
        surface.clear(color);

        { calculate zoom factor at current time }
        zoom := 2.5 * (1 - cos(time));

        { calculate zoomed image coordinates }
        x1 := Trunc(x - zoom * dx);
        y1 := Trunc(y - zoom * dy);
        x2 := Trunc(x + zoom * dx);
        y2 := Trunc(y + zoom * dy);

        { setup image copy area }
        area := TPTCArea.Create(x1, y1, x2, y2);
	Try
          { copy and stretch image to surface }
          image.copy(surface, image.area, area);

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

          { update console }
          console.update;
	Finally
          area.Free;
	End;
      End;
    Finally
      console.close;
      console.Free;
      surface.Free;
      format.Free;
      image.Free;
      color.Free;
      timer.Free;
    End;
  Except
    On error : TPTCError Do
      { report error }
      error.report;
  End;
End.