Source

vectorscope / spacerocks.c

Diff from to

spacerocks.c

 #include <inttypes.h>
 #include "sin_table.h"
 
-#define rand() lrand48()
+#ifdef __i386__
+#define fastrand() lrand48()
+#else
+#include <avr/io.h>
+#include "vector.h"
+#include "bits.h"
+
+static inline uint16_t
+fastrand(void)
+{
+	return TCNT1 ^ (TCNT1 << 1);
+}
+#endif
 
 #define STARTING_FUEL 65535
 #define STARTING_AMMO 65535
 	int16_t dx = p->x - q->x;
 	int16_t dy = p->y - q->y;
 
-	if (0) fprintf(stderr, "%d,%d -> %d,%d => %d,%d\n",
-		p->x, p->y,
-		q->x, q->y,
-		dx, dy
-	);
-
 	if (-radius < dx && dx < radius
 	&&  -radius < dy && dy < radius)
 		return 1;
 	b->p.vx = s->ax * BULLET_VEL + s->p.vx; // in the direction of the ship
 	b->p.vy = s->ay * BULLET_VEL + s->p.vy; // in the direction of the ship
 
+#ifdef __i386__
 	fprintf(stderr, "fire: vx=%d vy=%d\n", b->p.vx, b->p.vy);
+#endif
 
 	s->shots--;
 }
 		r->size = size;
 		r->p.x = x;
 		r->p.y = y;
-		r->p.vx = rand() % ROCK_VEL;
-		r->p.vy = rand() % ROCK_VEL;
-		r->type = rand() % (NUM_ROCK_TYPES * 8);
+		r->p.vx = fastrand() % ROCK_VEL;
+		r->p.vy = fastrand() % ROCK_VEL;
+		r->type = fastrand() % (NUM_ROCK_TYPES * 8);
 		return;
 	}
 }
 		uint8_t rock_dead = 0;
 
 		// check for bullet collision
-//fprintf(stderr, "Bullet check\n");
 		for (uint8_t j = 0 ; j < MAX_BULLETS ; j++)
 		{
 			bullet_t * const b = &bullets[j];
 				continue;
 			if (collide(&r->p, &b->p, r->size))
 			{
-				fprintf(stderr, "rock %d is dead\n", i);
-
 				uint16_t new_size = r->size / 2;
 				if (new_size > 256)
 				{
 		if (rock_dead)
 			continue;
 
-//fprintf(stderr, "Ship check\n");
 		 if (collide(&r->p, &s->p, r->size))
 			s->dead = 1;
 	}
 		if (b->age != 0)
 		{
 			point_update(&b->p);
-			fprintf(stderr, "%d,%d\n", b->p.x/256, b->p.y/256);
 		} else
 		if (fire)
 		{
 	}
 
 	if (fire)
+	{
+#ifdef __i386__
 		fprintf(stderr, "no bullets\n");
+#endif
+	}
 }
 
 
 	s->p.y = 0;
 	s->p.vx = 0;
 	s->p.vy = 0;
-	s->angle = rand();
+	s->angle = fastrand();
 	s->dead = 0;
 	s->fuel = STARTING_FUEL;
 	s->shots = STARTING_AMMO;
 	for (uint8_t i = 0 ; i < num ; i++)
 	{
 		// Make sure that there is space around the center
-		int16_t x = rand();
-		int16_t y = rand();
-		uint16_t size = (rand() % 32) * 256 + 512;
+		int16_t x = fastrand();
+		int16_t y = fastrand();
+		uint16_t size = (fastrand() % 32) * 256 + 512;
 		if (0 <= x)
 			x += MIN_RADIUS;
 		else
 	// If we hit something, start over
 	if (g->s.dead)
 	{
+#ifdef __i386__
 		fprintf(stderr, "game over\n");
+#endif
 		game_init(g);
 	}
 }
 	uint8_t n
 )
 {
+#ifdef __i386__
 	for (uint8_t i = 0 ; i < n ; i++)
 	{
 		uint8_t px = x + p[2*i+0];
 	}
 
 	printf("\n");
+#else
+	uint8_t ox = x + p[0];
+	uint8_t oy = y + p[1];
+	for (uint8_t i = 1 ; i < n ; i++)
+	{
+		const uint8_t px = x + p[2*i+0];
+		const uint8_t py = y + p[2*i+1];
+
+		line(ox, oy, px, py);
+		ox = px;
+		oy = py;
+	}
+#endif
 }
 
 
 }
 
 
+#ifdef __i386__
 int main(void)
 {
 	srand(getpid());
 		);
 	}
 }
+
+#else
+
+static game_t g;
+
+int main(void)
+{
+	// set for 16 MHz clock
+#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
+	CPU_PRESCALE(0);
+
+	game_init(&g);
+
+	while (1)
+	{
+		game_vectors(&g);
+
+		int8_t rot = ADC - 128;
+		uint8_t thrust = ADC;
+		uint8_t fire = in(0xB7);
+
+		game_update(
+			&g,
+			rot,
+			thrust,
+			fire
+		);
+	}
+}
+#endif
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.