Skip to content

Latest commit

 

History

History
357 lines (258 loc) · 8.95 KB

tobygrfx.c

File metadata and controls

357 lines (258 loc) · 8.95 KB
 
Apr 22, 2006
Apr 22, 2006
1
2
3
4
5
6
7
8
9
10
11
12
/**********************************************
* Graphical/Video related routines for TOBY. *
**********************************************
* Copyright (c) Ryan C. Gordon, 1995 *
* See copyright info in TOBY.C. *
**********************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "toby.h"
Apr 22, 2006
Apr 22, 2006
13
14
15
16
17
18
19
#ifdef __GNUC__
#include "unixbits.h"
#else
#include <graph.h>
#endif
extern char graphic_flags; /* Bitmap of flags for graphics. */
Apr 22, 2006
Apr 22, 2006
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
extern short turtle_x; /* Turtle's X coordinate. */
extern short turtle_y; /* Turtle's Y coordinate. */
extern short pen_color; /* Current pen color. */
extern short turtle_heading; /* the Turtle's heading : 0-360 deg. */
static struct videoconfig crt_info; /* Information regarding video. */
static void *video_buffer; /* Video buffer. */
static char sidelength; /* length of one side of the Turtle. */
/* functions/procedures... */
void init_crt(void)
/*
* Initializes variables related to graphics and sets up screen for LOGOing.
*
* params : void.
* returns : void.
* also : Video mode changes, memory potentially allocated.
*/
{
int buffer_size; /* Size of a virtual video page. */
int adjust;
if (_setvideomode(_MAXRESMODE) == FALSE)
logofinish("Can't set graphics mode.", ERLV_BAD);
_getvideoconfig(&crt_info);
_settextwindow(crt_info.numtextrows - 5, 0, crt_info.numtextrows,
crt_info.numtextcols);
pen_color = _getcolor();
/* calculate one side of the Turtle's length. */
sidelength = (char) ((crt_info.numxpixels / TURTLERATIO) +
(crt_info.numypixels / TURTLERATIO)) / 2;
adjust = (sidelength / 2) + 5;
buffer_size = _imagesize(100 - adjust, 100 - adjust,
100 + adjust, 100 + adjust);
video_buffer = allocate(NULL, buffer_size);
resetscreen();
} /*init_crt*/
void deinit_crt(void)
/*
* This routine frees any video buffers and returns the screen to default
* video mode.
*
* params : void.
* returns : void.
* also : Memory allocation and screen mode changed.
*/
{
_setvideomode(_DEFAULTMODE);
_clearscreen(_GCLEARSCREEN);
free(video_buffer);
} /*deinit_crt*/
void saveimage(int x, int y)
/*
* Copy image on screen to memory buffer, so drawing the Turtle won't destroy
* the image.
*
* params : x, y == coordinate of center pixel of turtle.
* returns : void.
* also : make sure that there's enough allocated space pointed to by
* video_buffer.
*/
{
int adjust = (sidelength / 2) + 5;
_getimage(x - adjust, y - adjust,
x + adjust, y + adjust, video_buffer);
} /*saveimage*/
void restoreimage(int x, int y)
/*
* Output saved image from video_buffer to the screen at specified
* coordinates.
*
* params : x, y == center coordinates where you want image restored.
* returns : void.
*/
{
int adjust = (sidelength / 2) + 5;
_putimage(x - adjust, y - adjust, video_buffer, _GPSET);
} /*restoreimage*/
void home(void)
/*
* Move the Turtle to the center of the screen, and face him north.
*
* params : void.
* returns : void.
*/
{
restoreimage(turtle_x, turtle_y);
turtle_heading = 0;
turtle_x = (crt_info.numxpixels / 2);
turtle_y = ((crt_info.numypixels / crt_info.numtextrows) *
(crt_info.numtextrows - 5)) / 2;
drawturtle(turtle_x, turtle_y);
} /*home*/
void resetscreen(void)
/*
* This procedure blanks all used pages of video memory, and resets the
* turtle's location to center screen.
*
* params : void.
* returns : void.
*/
{
clean();
home();
} /*resetscreen*/
void clean(void)
/*
* Clear screen without changing turtle...
*
* params : void.
* returns : void.
*/
{
/* Y axis of separator between graphics and text windows... */
int y = ((crt_info.numypixels / crt_info.numtextrows) *
(crt_info.numtextrows - 6) - 1);
_clearscreen(_GVIEWPORT);
/* Clear out buffer. */
saveimage(crt_info.numxpixels / 2, crt_info.numypixels / 2);
_moveto(0, y);
_lineto(crt_info.numxpixels, y);
} /*clean*/
void _outchar(char outthis)
/*
* _outtext() for a single character.
*
* params : outthis == character to output.
* returns : void.
*/
{
char outstr[] = {outthis, '\0'}; /* convert to a null-term'd string. */
_outtext(outstr); /* ...and print it. */
} /*_outchar*/
void drawturtle(short x, short y)
/*
* Draw a 'turtle', that little triangle, on the screen at x, y coordinates.
* params : the x and y is the center pixel of the triangle.
* returns : void.
*/
{
char looper; /* Loop control. */
short temp_heading; /* Calculates each angle of triangle. */
short temp_x; /* Calculate coordinates of... */
short temp_y; /* ..each angle of the triangle. */
saveimage(turtle_x, turtle_y);
if (!(graphic_flags & HIDETURTLE))
{
_setcolor(10); /* green? */
calcline(turtle_heading, (sidelength / 2), x, y, &temp_x, &temp_y);
temp_heading = turtle_heading + 150; /* Set up for... */
_moveto(temp_x, temp_y); /* ...actual drawing. */
for (looper = 1; looper <= 3; looper++)
{
calcline(temp_heading, sidelength, temp_x, temp_y,
&temp_x, &temp_y);
_lineto(temp_x, temp_y);
temp_heading += 120; /* Equilateral triangle; 60 degrees. */
/* (straight line == 60 + 120 == 180.) */
} /*for*/
_setcolor(9); /* Blue? */
calcline(turtle_heading + 180, sidelength, temp_x, temp_y,
&temp_x, &temp_y);
_lineto(temp_x, temp_y);
_setcolor(pen_color);
} /*if*/
} /*drawturtle*/
void moveturtle(short distance)
/*
* Move the Turtle's location to another point in Turtlespace. If the pen
* is down, so to speak, a line is drawn, otherwise, the location of the
* turtle only is changed. If passing the fence should generate an error,
* that will be checked, and runtime is toggled accordingly.
*
* params : distance == distance turtle should move.
* (heading and current location are stored in module-wide
* variables.)
* returns : void.
*/
{
restoreimage(turtle_x, turtle_y);
_moveto(turtle_x, turtle_y);
calcline(turtle_heading, distance, turtle_x, turtle_y,
&turtle_x, &turtle_y);
if (graphic_flags & PEN_UP)
;
else if (graphic_flags & PEN_REVERSE)
/* not yet implemented. */;
else if (graphic_flags & PEN_ERASE)
{
_setcolor(0);
_lineto(turtle_x, turtle_y);
} /*else if*/
else /* Pen is down, and writing... */
{
_setcolor(pen_color);
_lineto(turtle_x, turtle_y);
} /*else*/
/* Has the Turtle overstepped the 'fence'? Should it cause an error?... */
if ((graphic_flags & FENCE) && ((turtle_x < 0) || (turtle_x > crt_info.numxpixels) ||
(turtle_y < 0) || (turtle_y > crt_info.numypixels)))
logo_error("Turtle has passed fence.", RT_PASTFENCE);
drawturtle(turtle_x, turtle_y);
} /*moveturtle*/
void turtleturn(short rotation)
/*
* Rotate the turtle, setting a new heading.
*
* params : rotation == number of degrees, positive or negative, to
* rotate Turtle.
* returns : void. turtle_heading is modified, though.
*/
{
restoreimage(turtle_x, turtle_y);
turtle_heading = turtle_heading + rotation;
while (turtle_heading >= 360)
turtle_heading -= 360;
while (turtle_heading < 0)
turtle_heading += 360;
drawturtle(turtle_x, turtle_y);
} /*turtleturn*/
void calcline(short heading, short distance, short startx, short starty,
short *retx, short *rety)
/*
* This procedure calculates coordinates for a line. No line is actually
* drawn by this procedure, though.
*
* params : heading == 0 - 360 degree direction line goes.
* distance == total space line should cover.
* startx == Starting x coordinate for line.
* starty == Starting y coordinate for line.
* *retx == (Return value) ending x coordinate.
* *rety == (Return value) ending y coordinate.
* returns : officially, void. however, retx and rety are modified.
*/
{
short calcx;
short calcy;
double rad;
heading -= 90; /* Make 0 degrees point north. */
rad = (heading / R_TO_D); /* Radian conversion. */
calcx = (cos(rad) * distance);
calcy = (sin(rad) * distance);
*retx = startx + calcx; /* return values. */
*rety = starty + calcy;
} /*calcline*/