Commits

Trammell Hudson committed 3886873

Stars work

  • Participants
  • Parent commits df34b90

Comments (0)

Files changed (2)

 #include "app.h"
 
 
+extern const app_t stars_app;
 extern const app_t qr_app;
 extern const app_t life_app;
 extern const app_t powersave_app;
 extern const app_t plaid_app;
 
 static const app_t * apps[] = {
-	&qr_app,
-	&life_app,
 	&powersave_app,
+	&stars_app,
+	//&qr_app,
+	//&life_app,
 	&swarm_app,
-	&plaid_app,
+	//&plaid_app,
 };
 
 static uint8_t current;
 
 #define MAX_STARS 64
 
+
 struct star
 {
-	uint8_t x; // scaled by 2
-	uint8_t y;
+	int16_t x; // scaled by 4
+	int16_t y;
 	int8_t dx;
 	int8_t dy;
-	//uint8_t brightness;
+	uint8_t brightness;
 } __attribute__((packed));
 
 
 struct stars_app
 {
+	int8_t rotate_sin;
+	int8_t rotate_cos;
 	struct star stars[MAX_STARS];
 } __attribute__((packed));
 
+#define APP ((struct stars_app *) app_common_data)
+
+// Ensure that the size of the struct is smaller than common block
+enum { test_size = 1 / (sizeof(*APP) <= APP_COMMON_SIZE) };
+
 
 static void
 star_create(
 	struct star * const star
 )
 {
-	uint8_t theta = rand() % 0xFF;
-	star->sin_t = sin_table(theta);
+	uint8_t theta = rand() & 0xFF;
+	int32_t vel = rand() & 0xFF;
+	star->dx = (sin_lookup(theta) * vel) / 1024;
+	star->dy = (cos_lookup(theta) * vel) / 1024;
 
-	star->x = SCREEN_WIDTH; // scaled by 2 == center
-	star->y = SCREEN_HEIGHT;
+	star->x = 0;
+	star->y = 0;
 
-	//star->brightness = rand() % 0xFF;
+	// Brighter stars are faster
+	star->brightness = vel;
 }
 
 
 static void
 stars_init(void)
 {
-	for (int i=0 ; i < NUM_STARS ; i++)
+	pulse_blank_canvas();
+
+	for (int i=0 ; i < MAX_STARS ; i++)
 		star_create(&APP->stars[i]);
+
+	// Pick a slow rotate speed
+	uint8_t rotate = 1; // (rand() & 0xF) - 8;
+	APP->rotate_sin = sin_lookup(rotate);
+	APP->rotate_cos = cos_lookup(rotate);
 }
 
 
 static void
+star_draw_point(
+	const struct star * const star,
+	color24_t color
+)
+{
+	int16_t x = star->x / 4 + SCREEN_WIDTH / 2;
+	int16_t y = star->y / 4 + SCREEN_HEIGHT / 2;
+
+	pulse_set_draw_window(x, y, x, y);
+	pulse_draw_point24(color);
+}
+	
+
+
+static void
 star_draw(
 	struct star * const star
 )
 	// Check for age
 	int32_t x = star->x + star->dx;
 	int32_t y = star->y + star->dy;
-	if (x < 0 || x >= SCREEN_WIDTH
-	||  y < 0 || y >= SCREEN_HEIGHT)
+
+	int32_t rx = (APP->rotate_cos * x - APP->rotate_sin * y) / 128;
+	int32_t ry = (APP->rotate_sin * x + APP->rotate_cos * y) / 128;
+
+	if (rx <= -SCREEN_WIDTH * 2 || rx >= SCREEN_WIDTH * 2
+	||  ry <= -SCREEN_HEIGHT * 2 || ry >= SCREEN_HEIGHT * 2)
 	{
 		star_create(star);
 		return;
 	}
+		
 
-	// Update the new location
-	star->x = x;
-	star->y = y;
+	star->x = rx;
+	star->y = ry;
 
-	star_draw_point(star, COLOR_WHITE24);
+	color24_t color = {
+		.red	= star->brightness,
+		.blue	= star->brightness,
+		.green	= star->brightness,
+	};
+
+	star_draw_point(star, color);
 }
 
 
 	const struct pulse_time_tm * const now
 )
 {
-	for (int i=0 ; i < NUM_STARS ; i++)
+	for (int i=0 ; i < MAX_STARS ; i++)
 		star_draw(&APP->stars[i]);
 }