-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzxhigfx.h
429 lines (378 loc) · 9.4 KB
/
zxhigfx.h
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/**
* \mainpage
* High Resolution 256x192 Colour Graphics Library for ZX Spectrum
* ---------------------------------------------------------------
*
* \section intro_sec Introduction
* C library providing assembly functions for plotting pixels to the ZX spectrum screen
* and attribute changing (paper, ink) plus some c functions utilising these
* for drawing primitives.
*
* For use with z88dk cross compiler for z80 based machines
* http://www.z88dk.org/forum/
*
* \section testing_sec Testing
* Tested on 48k ZX Spectrum, ZX Spectrum 128k, ZX Spectrum +2, Spectrum +3
* known compatibility issue with 16k ZX Spectrum
*
* \section license_sec License
* Copyright 2013 Matthew Thorne
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
//TODO : Add screen page swapping and sprite handling functionality
#ifndef __ZXHIGFX_H__
#define __ZXHIGFX_H__
#include <stdlib.h>
/**
* Standard spectrum colour black
*/
#define BLACK 0
/**
* Standard spectrum colour blue
*/
#define BLUE 1
/**
* Standard spectrum colour red
*/
#define RED 2
/**
* Standard spectrum colour magenta
*/
#define MAGENTA 3
/**
* Standard spectrum colour green
*/
#define GREEN 4
/**
* Standard spectrum colour cyan
*/
#define CYAN 5
/**
* Standard spectrum colour yellow
*/
#define YELLOW 6
/**
* Standard spectrum colour white
*/
#define WHITE 7
/**
* Spectrum horizontal screen boundary
*/
#define XMAX 255
/**
* Spectrum vertical screen boundary
*/
#define YMAX 191
/**
* Helper function - return the attribute value based on the specified
* ink and paper colours
* @param ink an integer representing the ink colour (0 - 7)
* @param paper an integer representing the paper colour (0 - 7)
* @param bright an integer specifying wether bright is used (0, 1)
* @param flash an integer specifying wether flash is used (0, 1)
* @return an integer representing the attribute value
*/
int attr(int ink, int paper, int bright, int flash){
// Construct the attribute byte
return ink + (8 * paper) + (bright * 64) + (flash * 128);
}
/**
* Set the spectrum border colour
* @param color an integer representing the border colour (0 - 7)
*/
void cBorder(int color){
#asm
LD ix,0
ADD ix,sp
LD A,(ix+2)
CALL 8859
RET
#endasm
}
/**
* plot a pixel to the screen
* @param x an integer specifying the horizontal position of the pixel
* @param y an integer specifying the vertical position of the pixel
* @param attr an integer specifying the attribute value of the pixel
* @see attr(int ink, int paper, int bright, int flash)
*/
void cplot(int x, int y, int attr){
//Bounds checking
if (x>XMAX) x=XMAX;
if (y>YMAX) y=YMAX;
if (attr>255) attr=255;
// Parameters -
// (ix+2) - attr
// (ix+4) - y
// (ix+6) - x
// Assembly Pixel Plotting Routine
// From "Advanced Spectrum Machine Language" by David Webb
// Published by Melbourne House 1984
// Page 20
#asm
LD ix,0
ADD ix,sp
LD C,(ix+2)
LD L,(ix+4)
LD H,(ix+6)
PLOT:
LD A,L
AND 0C0H
RRA
SCF
RRA
RRCA
XOR L
AND 0F8H
XOR L
LD D,A
LD A,H
RLCA
RLCA
RLCA
XOR L
AND 0C7H
XOR L
RLCA
RLCA
LD E,A
PUSH DE ;address is stored in DE
; Find attribute address
LD A,D
RRCA
RRCA
RRCA
AND 3
OR 58H
LD D,A
LD B,0
; Change attribute
LD A,(DE)
XOR C
AND B
XOR C
LD (DE),A
; Retrieve D.F. address
POP DE
LD A,H
AND 7
LD B,A
INC B
; B holds target bit number plus 1
LD A,0FEH
; Rotate a window to the target bit
PLOOP:
RRCA
DJNZ PLOOP
LD B,A
LD A,0
LD C,A
; Take byte from D.F
LD A,(DE)
; Check for over 1
BIT 1,C
JR NZ,OVER1
AND B
; Check for inverse 1
OVER1:
BIT 3,C
JR NZ,INV1
XOR B
CPL
INV1:
LD (DE),A
RET
#endasm
}
/**
* Clear screen to specified attribute
* @param attr an integer specifying the attribute value
* @see attr(int ink, int paper, int bright, int flash)
*/
void cls(int attr){
// Assembly Screen Attribute Clearing Routine
// From "Advanced Spectrum Machine Language" by David Webb
// Published by Melbourne House 1984
// Page 11
#asm
LD ix,0
ADD ix,sp
LD A,(ix+2)
CLS:
LD HL,4000H
LD BC,1800H
LD (HL),L
LD D,H
LD E,1
LDIR
LD (HL),A
LD BC,02FFH
LDIR
RET
#endasm
}
/**
* Clear the spectrum displayfile
*/
void clsdf(void){
// Assembly Display File Clearing Routine
// From "Advanced Spectrum Machine Language" by David Webb
// Published by Melbourne House 1984
// Page 07
#asm
CLSDF:
LD HL,4000H
LD BC,17FFH
LD (HL),L
LD D,H
LD E,1
LDIR
RET
#endasm
}
/**
* Draw a rectangle primitive - origin topleft
* @param startx an integer specifying the horizontal origin
* @param starty an integer specifying the vertical origin
* @param endx an integer specifying the horizontal end point
* @param endy an integer specifying the vertical end point
* @param attr an integer specifying the attribute value
* @see attr(int ink, int paper, int bright, int flash)
*/
void cDrawRect(int startx, int starty, int endx, int endy, int attr) {
int x, y;
for(x=startx; x<=endx; x++){
cplot(x, starty, attr);
}
for(y=starty; y<=endy; y++){
cplot(endx, y, attr);
}
for(x=endx; x>=startx; x--){
cplot(x, endy, attr);
}
for(y=endy; y>=starty; y--){
cplot(startx, y, attr);
}
}
/**
* Draw a line primitive
* @param startx an integer specifying the horizontal origin
* @param starty an integer specifying the vertical origin
* @param endx an integer specifying the horizontal end point
* @param endy an integer specifying the vertical end point
* @param attr an integer specifying the attribute value
* @see attr(int ink, int paper, int bright, int flash)
*/
void cDrawLine(int startx, int starty, int endx, int endy, int attr) {
// http://cboard.cprogramming.com/game-programming/67832-line-drawing-algorithm.html
int t, distance;
int xerr=0, yerr=0, delta_x, delta_y;
int incx, incy;
/* compute the distances in both directions */
delta_x=endx-startx;
delta_y=endy-starty;
/* Compute the direction of the increment,
an increment of 0 means either a horizontal or vertical
line.
*/
if(delta_x>0) incx=1;
else if(delta_x==0) incx=0;
else incx=-1;
if(delta_y>0) incy=1;
else if(delta_y==0) incy=0;
else incy=-1;
/* determine which distance is greater */
delta_x=abs(delta_x);
delta_y=abs(delta_y);
if(delta_x>delta_y) distance=delta_x;
else distance=delta_y;
/* draw the line */
for(t=0; t<=distance+1; t++) {
cplot(startx, starty, attr);
xerr+=delta_x;
yerr+=delta_y;
if(xerr>distance) {
xerr-=distance;
startx+=incx;
}
if(yerr>distance) {
yerr-=distance;
starty+=incy;
}
}
}
/**
* Draw a circle primitive - origin centre
* @param x0 an integer specifying the horizontal origin
* @param y0 an integer specifying the vertical origin
* @param radius an integer specifying the radius of the circle
* @param attr an integer specifying the attribute value
* @see attr(int ink, int paper, int bright, int flash)
*/
void cDrawCircle(int x0, int y0, int radius, int attr){
int x, y, radiusError;
x = radius;
y = 0;
radiusError = 1-x;
while(x >= y)
{
cplot(x + x0, y + y0, attr);
cplot(y + x0, x + y0, attr);
cplot(-x + x0, y + y0, attr);
cplot(-y + x0, x + y0, attr);
cplot(-x + x0, -y + y0, attr);
cplot(-y + x0, -x + y0, attr);
cplot(x + x0, -y + y0, attr);
cplot(y + x0, -x + y0, attr);
y++;
if(radiusError<0)
radiusError+=2*y+1;
else
{
x--;
radiusError+=2*(y-x+1);
}
}
}
/**
* Draw a filled circle primitive - origin centre
* @param x0 an integer specifying the horizontal origin
* @param y0 an integer specifying the vertical origin
* @param radius an integer specifying the radius of the circle
* @param attr an integer specifying the attribute value
* @see attr(int ink, int paper, int bright, int flash)
*/
void cDrawFilledCircle(int x0, int y0, int radius, int attr){
int n;
for(n=radius; n>0; n--) cDrawCircle(x0, y0, n, attr);
}
/**
* Draw a filled rectangle primitive - origin topleft
* @param startx an integer specifying the horizontal origin
* @param starty an integer specifying the vertical origin
* @param endx an integer specifying the horizontal end point
* @param endy an integer specifying the vertical end point
* @param attr an integer specifying the attribute value
* @see attr(int ink, int paper, int bright, int flash)
*/
void cDrawFilledRect(int startx, int starty, int endx, int endy, int attr) {
int x, y;
for(y=starty; y<=endy; y++){
for(x=startx; x<=endx; x++){
cplot(x, y, attr);
}
}
}
#endif