-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkiss_general.pas
278 lines (233 loc) · 7.63 KB
/
kiss_general.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
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
unit kiss_general;
{<
This unit is part of the C to Free Pascal conversion project kiss_sdl4fp.
It converts the
kiss_sdl widget toolkit
Copyright (c) 2016 Tarvo Korrovits <tkorrovi@mail.com>.
The original files are:
kiss_sdl.h (version 1.2.0, in parts automatically converted by H2Pas 1.0.0)
kiss_posix.c (version 1.2.0)
kiss_general.c (version 1.2.0)
kiss_widgets.c (version 1.2.4)
kiss_draw.c (version 1.2.4).
For more information on kiss_sdl4fp, visit:
https://github.com/Free-Pascal-meets-SDL-Website/kiss_sdl4fp
Copyright (c) 2020 Matthias J. Molski
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
}
{$I kiss_sdl.inc}
interface
uses
SDL2,
SysUtils;
const
{$IFDEF WINDOWS}
External_library='kernel32'; {Setup as you need}
{$ELSE}
External_library='';
{$ENDIF}
const
KISS_MAX_LENGTH = 200;
KISS_MIN_LENGTH = 10;
KISS_MAX_LABEL = 500;
KISS_MAGIC = 12345;
const
OTHER_TYPE = 1;
WINDOW_TYPE = 2;
RENDERER_TYPE = 3;
TEXTURE_TYPE = 4;
SURFACE_TYPE = 5;
FONT_TYPE = 6;
STRING_TYPE = 7;
ARRAY_TYPE = 8;
type
Pkiss_array = ^kiss_array;
kiss_array = record
data : Ppointer;
id : PLongInt;
length : LongInt; { --> obsolete in Free Pascal, bc. contained in Length(arr), not true bc. array system of FP not used here }
size : LongInt; { --> obsolete in Free Pascal, bc. automatically managed FP, not true bc. array system of FP not used here }
ref : LongInt; { single ref. value for entire array, store in 0th element? }
end;
Tkiss_array = kiss_array;
function kiss_makerect(rect:PSDL_Rect; x:LongInt; y:LongInt; w:LongInt; h:LongInt):LongInt;
function kiss_pointinrect(x:LongInt; y:LongInt; rect:PSDL_Rect):LongInt;
function kiss_string_copy(var dest:string; const size: size_t;
const str1:string; const str2:string):string;
function kiss_string_compare(const a:string; const b:string):LongInt;
function kiss_backspace(var str:string):string;
function kiss_array_new(a: Pkiss_array):LongInt;
function kiss_array_data(a:Pkiss_array; index:LongInt):pointer;
function kiss_array_id(var a:Pkiss_array; index:LongInt):LongInt;
//function kiss_array_assign(var a:kiss_array; index:LongInt; id:LongInt; data:pointer):LongInt;cdecl;external External_library name 'kiss_array_assign';
function kiss_array_append(a:Pkiss_array; id:LongInt; data:pointer):LongInt;
function kiss_array_appendstring(a:Pkiss_array; id:LongInt; text1:string; text2:string):LongInt;
//function kiss_array_insert(var a:kiss_array; index:LongInt; id:LongInt; data:pointer):LongInt;cdecl;external External_library name 'kiss_array_insert';
//function kiss_array_remove(var a:kiss_array; index:LongInt):LongInt;cdecl;external External_library name 'kiss_array_remove';
function kiss_array_free(a:Pkiss_array):LongInt;
implementation
function kiss_makerect(rect: PSDL_Rect; x: LongInt; y: LongInt; w: LongInt;
h: LongInt): LongInt;
begin
if not Assigned(rect) then
Exit(-1);
rect^.x := x;
rect^.y := y;
rect^.w := w;
rect^.h := h;
Result := 0;
end;
function kiss_pointinrect(x: LongInt; y: LongInt; rect: PSDL_Rect): LongInt;
begin
Result := 0;
if (x >= rect^.x) and (x < rect^.x + rect^.w) and (y >= rect^.y) and
(y < rect^.y + rect^.h) then
Result := 1;
end;
function kiss_string_copy(var dest: string; str1: string;
str2: string): string;
begin
dest := '';
if (str1 <> '') then
dest := dest + str1;
if not(str2 <> '') then
begin
Result := dest;
Exit;
end;
dest := dest + str2;
Result := dest;
end;
function kiss_string_copy(var dest: string; const size: size_t;
const str1: string; const str2: string): string;
var
len: LongInt;
p: string;
begin
dest := '';
if (size < 1) then
Exit(dest);
dest := Copy(str1, 1, size); // Conv.: Copy truncates string if size larger
// than length of string. strncpy pads with
// 0-chars in this case.
len := Length(dest);
if (not(str2 <> '') or (size <= len)) then
Exit(dest);
dest := dest + Copy(str2, 1, size - len);
Result := dest;
end;
function kiss_string_compare(const a: string; const b: string): LongInt;
begin
Result := CompareStr(a, b);
end;
function kiss_backspace(var str: string): string;
begin
if (str = '') then
begin
Result := str;
Exit;
end;
SetLength(str, Length(str)-1);
Result := str;
end;
function kiss_array_new(a: Pkiss_array): LongInt;
begin
if not Assigned(a) then
Exit(-1);
a^.size := KISS_MIN_LENGTH;
a^.length := 0;
a^.ref := 1;
a^.data := GetMem(KISS_MIN_LENGTH * SizeOf(pointer));
a^.id := GetMem(KISS_MIN_LENGTH * SizeOf(LongInt));
end;
function kiss_array_data(a: Pkiss_array; index: LongInt): pointer;
begin
if (index < 0) or (index >= a^.size) or (not Assigned(a)) then
Result := nil
else
Result := a^.data[index];
end;
function kiss_array_id(var a: Pkiss_array; index: LongInt): LongInt;
begin
if (index < 0) or (index >= a^.size) or (not Assigned(a)) then
Result := 0
else
Result := a^.id[index];
end;
function kiss_array_append(a: Pkiss_array; id: LongInt; data: pointer): LongInt;
var
i: LongInt;
begin
if not Assigned(a) then
Exit(-1);
if a^.length >= a^.size then
begin
a^.size := a^.size * 2;
a^.data := ReAllocMem(a^.data, a^.size * SizeOf(pointer));
a^.id := ReAllocMem(a^.id, a^.size * SizeOf(LongInt));
for i := a^.length to a^.size-1 do
begin
a^.data[i] := nil;
a^.id[i] := 0;
end;
end;
a^.data[a^.length] := data;
a^.id[a^.length] := id;
Inc(a^.length);
Result := 0;
end;
function kiss_array_appendstring(a: Pkiss_array; id: LongInt;
text1: string; text2: string): LongInt;
var
p: PString;
begin
if (not Assigned(a)) then
Exit(-1);
New(p);
kiss_string_copy(p^, KISS_MAX_LENGTH, text1, text2);
kiss_array_append(a, id, p);
Result := 0;
end;
function kiss_array_free(a: Pkiss_array): LongInt;
var
i: LongInt;
begin
if (not Assigned(a)) or (a^.ref = 0) then
Exit(-1);
if a^.ref > 1 then
begin
Dec(a^.ref);
Exit(0);
end;
if a^.length > 0 then
begin
for i := 0 to a^.length-1 do
if a^.id[i] = STRING_TYPE then // Conv.: necessary for string feature
Dispose(PString(a^.data[i]))
else
FreeMem(a^.data[i]);
end;
FreeMem(a^.data);
FreeMem(a^.id);
a^.data := nil;
a^.id := nil;
a^.size := 0;
a^.length := 0;
a^.ref := 0;
Result := 0;
end;
end.