-
Notifications
You must be signed in to change notification settings - Fork 0
/
ff_patch.pas
151 lines (133 loc) · 3.97 KB
/
ff_patch.pas
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
//------------------------------------------------------------------------------
//
// DD_FONT: Doom Font Creator
// Copyright (C) 2021-2022 by Jim Valavanis
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
// DESCRIPTION:
// Doom patch creation
//
//------------------------------------------------------------------------------
// E-Mail: [email protected]
// Site : https://sourceforge.net/projects/dd-font/
//------------------------------------------------------------------------------
unit ff_patch;
interface
uses
Windows, SysUtils, Classes, Graphics;
type
patchheader_t = packed record
width: smallint; // bounding box size
height: smallint;
leftoffset: smallint; // pixels to the left of origin
topoffset: smallint; // pixels below the origin
end;
column_t = packed record
topdelta: byte; // -1 is the last post in a column
length: byte; // length data bytes follows
end;
Pcolumn_t = ^column_t;
procedure FF_CreateDoomPatch(const img: PByteArray; const width, height: integer;
const solid: boolean; out p: pointer; out size: integer; const offsx: integer = -255; const offsy: integer = -255);
implementation
uses
ff_utils;
procedure FF_CreateDoomPatch(const img: PByteArray; const width, height: integer;
const solid: boolean; out p: pointer; out size: integer; const offsx: integer = -255; const offsy: integer = -255);
var
x, y: integer;
c: LongWord;
m, fs: TMemoryStream;
patch: patchheader_t;
column: column_t;
columnofs: TDNumberList;
columndata: TDByteList;
i: integer;
procedure flashcolumnend;
begin
column.topdelta := 255;
column.length := 0;
m.Write(column, SizeOf(column_t));
end;
procedure flashcolumndata;
var
bb: byte;
begin
if columndata.Count > 0 then
begin
column.topdelta := y - columndata.Count;
column.length := columndata.Count;
m.Write(column, SizeOf(column_t));
bb := 0;
m.Write(bb, SizeOf(bb));
m.Write(columndata.List^, columndata.Count);
m.Write(bb, SizeOf(bb));
columndata.FastClear;
end;
end;
begin
m := TMemoryStream.Create;
fs := TMemoryStream.Create;
columnofs := TDNumberList.Create;
columndata := TDByteList.Create;
try
patch.width := width;
patch.height := height;
if offsx = -255 then
patch.leftoffset := width div 2
else
patch.leftoffset := offsx;
if offsy = -255 then
patch.topoffset := height
else
patch.topoffset := offsy;
fs.Write(patch, SizeOf(patchheader_t));
for x := 0 to width - 1 do
begin
columnofs.Add(m.Position + SizeOf(patchheader_t) + width * SizeOf(integer));
columndata.FastClear;
for y := 0 to height - 1 do
begin
c := img[x + y * width];
if not solid then
if c = 0 then
begin
flashcolumndata;
continue;
end;
columndata.Add(c);
end;
flashcolumndata;
flashcolumnend;
end;
for i := 0 to columnofs.Count - 1 do
begin
x := columnofs.Numbers[i];
fs.Write(x, SizeOf(integer));
end;
size := fs.Size + m.Size;
GetMem(p, size);
memcpy(p, fs.Memory, fs.Size);
memcpy(pointer(integer(p) + fs.Size), m.Memory, m.Size);
finally
m.Free;
columnofs.Free;
columndata.Free;
fs.Free;
end;
end;
end.