-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbouncer6.cpp
More file actions
238 lines (202 loc) · 6.43 KB
/
Copy pathbouncer6.cpp
File metadata and controls
238 lines (202 loc) · 6.43 KB
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
/* Vectrex Tutorial Bouncer6 by Joakim Larsson Edström, (c) 2018, GPLv3 */
/*
* Bouncer6 improves/changes Bouncer5 by:
* - adding depth dimension
* - no turtles more boxes
* - a few bugs fixed
* - added support for BIOS calls with return values, e.g. Random()
*/
#include "vectrex.h"
#include "bouncer.h"
/*
* The vectorlist constructor takes a parameter which should be a Vectrex vectorlist and calculates the maximum
* distance from the drawing start point of the object to the edges in all four direction. This is needed to
* be able to calculate the bounces to the walls.
* Profile ROM: 6 RAM: 0 STACK: 4 CYCLES: Bouncer5.vectorlist: On init
*/
vectorlist::vectorlist(int8_t *sym)
{
this->init(sym);
}
/* Because the gcc 3.4.x produces multiple constructors for each class and this one is rather big
* reducing the constructor to just calling init saves 80+ bytes. Weird but that is C++.
*/
void vectorlist::init(int8_t *sym)
{
int8_t c, i, px, py;
c = sym[0]; /* First byte of a Vectrex vector list is the number of vectors - 1 */
c++; /* So compensate to simplify the loop through */
px = py = 0;
i = 1; /* Start from first coordinate pair of vector list */
bright = bleft = bup = bdown = 0;
vec = sym;
while (c--)
{
py += sym[i++];
if ( py > bup) bup = py;
if (-py > bdown) bdown = -py;
px += sym[i++];
if ( px > bright) bright = px;
if (-px > bleft) bleft = -px;
}
}
/*
* Operator overload to avoid the need of the C++ standard libraries when using the new operators below
* This particular function only supports the creation of instances of class vectorlist.
* Profile ROM: 38 RAM: 13 STACK: 2 Bouncer5.new: On init
*/
#define VLISTS 1
#define VEXPRS VEXPRITES
#define NULL ((void *) 0L)
static int vli = 0;
static int vxi = 0;
static int8_t vlists[sizeof(vectorlist) * VLISTS];
static int8_t vexprite3ds[sizeof(vexprite3D) * VEXPRS];
void* operator new(long unsigned int size)
{
void *v = NULL;
switch (size)
{
case sizeof(vectorlist):
if (vli < VLISTS * sizeof(vectorlist))
{
v = (void *) &vlists[vli];
vli += sizeof(vectorlist);
}
break;
case sizeof(vexprite3D):
if (vxi < VEXPRS * sizeof(vexprite3D))
{
v = (void *) &vexprite3ds[vxi];
vxi += sizeof(vexprite3D);
}
break;
default: break;
}
return v;
}
/* The constructor sets up the basic data for the object
* Profile ROM: 13 RAM: 0 STACK: 0 Bouncer5.vexprite: On init
*/
vexprite3D::vexprite3D()
{
/* Setup start position and directions */
ox = oy = oz = 0;
xdir = 1;
ydir = 3;
zdir = -1;
}
/* The draw method draws all vectors for a specific object
* Profile ROM: 23 RAM: 0 STACK: 3 Bouncer5.vexprite.draw: On Update
*/
void vexprite3D::draw()
{
int px, py;
int i;
unsigned long tmp;
VIA_t1_cnt_lo = SCALE - 2 * (oz + ZSIZE / 2);
/* Move beam (origin of vexprite) to new position in screen coordinates (Vectrex BIOS) */
Moveto_d(ox, oy);
/* Draw Symbol (Vectrex BIOS) */
if (sym != NULL)
Draw_VLc(sym->vec);
}
/* The move method moves the object and checks for bounces against the bounding box
* Profile ROM: 199 RAM: 0 STACK: 0 Bouncer5.vexprite.move: On Update
*/
void vexprite3D::move()
{
/* Update position of vexprite */
ox += xdir;
oy += ydir;
oz += zdir;
/* Check boundaries and bounce if needed, along X axis */
if (ox > (XSIZE / 2 - sym->bright)){
ox = (XSIZE / 2 - sym->bright) - (ox - (XSIZE / 2 - sym->bright));
xdir = -xdir;
}else if
(ox < -(XSIZE / 2 - sym->bleft)){
ox = -(XSIZE / 2 - sym->bleft) + ( -(XSIZE / 2 - sym->bleft) - ox);
xdir = -xdir;
}
/* Check boundaries and bounce if needed, along Y axis */
if (oy > (YSIZE / 2 - sym->bup)){
oy = (YSIZE / 2 - sym->bup) - (oy - (YSIZE / 2 - sym->bup));
ydir = -ydir;
}else if
(oy < -(YSIZE / 2 - sym->bdown)){
oy = -(YSIZE / 2 - sym->bdown) + ( - (YSIZE / 2 - sym->bdown) - oy);
ydir = -ydir;
}
/* Check boundaries and bounce if needed, along Z axis */
if (oz > (ZSIZE / 2)){
oz = (ZSIZE / 2) - (oz - (ZSIZE / 2));
zdir = -zdir;
}else if
(oz < -(ZSIZE / 2)){
oz = -(ZSIZE / 2) + ( - (ZSIZE / 2) - oz);
zdir = -zdir;
}
}
/*
* main() inits the objects and drives the update loop
* Profile ROM: 324 RAM: 0 STACK: 36 Bouncer5.main: On Update
*/
main()
{
class vexprite3D *vexprites[VEXPRITES];
int8_t i;
int8_t speed;
class vectorlist *v1, *v2;
/* Setup scale and beam intensity */
Intensity_a(0x5f);
VIA_t1_cnt_lo = SCALE;
/* Initiate the vexprite vector lists */
v1 = new vectorlist(box_line_list);
// v2 = new vectorlist(turtle_line_list);
/* Create each vexprite and make it a little different */
for (i = 0; i < VEXPRITES; i++){
/* Creation */
vexprites[i] = new vexprite3D();
/* Vectors */
vexprites[i]->sym = v1;
/* Different speed and directions */
vexprites[i]->xdir = Random() / 64 - 2;
vexprites[i]->ydir = Random() / 64 - 2;
vexprites[i]->zdir = Random() / 64 - 2;
vexprites[i]->ox = Random() - XSIZE / 2;
vexprites[i]->oy = Random() - YSIZE / 2;
vexprites[i]->oz = Random() - ZSIZE / 2;
}
/* Set up the animation speed */
speed = SPEED;
volatile uint8_t *sp = &Vec_Seed_Ptr;
*sp++ = 0x11;
*sp++ = 0x22;
*sp++ = 0x45;
while (1) {
/* Wait for frame boundary (one frame = 30,000 CPU cyles@50fps) */
Wait_Recal();
#if 0 // Disabled frame to avoid burnin on real Vectrex when left on for a long time.
/* Reset pen and scale */
Reset0Ref();
VIA_t1_cnt_lo = SCALE;
/* Draw boundary box */
Moveto_d(XSIZE / 2, YSIZE / 2);
Draw_Line_d(-XSIZE, 0);
Draw_Line_d( 0, -YSIZE );
Draw_Line_d( XSIZE, 0);
Draw_Line_d( 0, YSIZE );
#endif
for (int j = 0; j < VEXPRITES; j++)
{
/* Reset pen and scale */
Reset0Ref();
/* Draw Vexprite */
vexprites[j]->draw();
/* Move Vexprite */
vexprites[j]->move();
}
}
return 0; /* Will never happen due to the while (1) statement */
}