Monorepo for Aesthetic.Computer aesthetic.computer
4
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 89 lines 1.9 kB view raw
1// Simple animated graphics demo for GameBoy 2// Draws bouncing pixels and simple shapes 3 4#include <gb/gb.h> 5#include <stdint.h> 6 7// Simple tile with a single pixel in top-left corner 8const uint8_t pixel_tile[] = { 9 0xFF, 0xFF, // Row 0: all pixels on 10 0xFF, 0xFF, // Row 1 11 0xFF, 0xFF, // Row 2 12 0xFF, 0xFF, // Row 3 13 0xFF, 0xFF, // Row 4 14 0xFF, 0xFF, // Row 5 15 0xFF, 0xFF, // Row 6 16 0xFF, 0xFF // Row 7 17}; 18 19// Bouncing ball state 20int16_t ball_x = 80; 21int16_t ball_y = 72; 22int8_t ball_dx = 2; 23int8_t ball_dy = 1; 24 25void init_graphics(void) { 26 uint8_t tile_num = 0; 27 28 // Load our pixel tile into VRAM 29 set_bkg_data(0, 1, pixel_tile); 30 31 // Fill background with tile 0 32 for(uint8_t y = 0; y < 18; y++) { 33 for(uint8_t x = 0; x < 20; x++) { 34 set_bkg_tiles(x, y, 1, 1, &tile_num); 35 } 36 } 37 38 // Turn on the background 39 SHOW_BKG; 40 DISPLAY_ON; 41} 42 43void update_ball(void) { 44 // Update position 45 ball_x += ball_dx; 46 ball_y += ball_dy; 47 48 // Bounce off edges 49 if(ball_x <= 0 || ball_x >= 152) { 50 ball_dx = -ball_dx; 51 } 52 if(ball_y <= 0 || ball_y >= 136) { 53 ball_dy = -ball_dy; 54 } 55} 56 57void draw_pattern(void) { 58 // Draw a simple animated pattern 59 static uint8_t frame = 0; 60 uint8_t tile_num = 0; 61 62 // Draw diagonal lines 63 for(uint8_t i = 0; i < 18; i++) { 64 uint8_t x = (i + frame) % 20; 65 set_bkg_tiles(x, i, 1, 1, &tile_num); 66 } 67 68 frame++; 69} 70 71void main(void) { 72 init_graphics(); 73 74 uint8_t counter = 0; 75 76 while(1) { 77 vsync(); // Wait for vertical blank 78 79 // Update every 2 frames 80 if(counter++ & 1) { 81 update_ball(); 82 draw_pattern(); 83 } 84 85 // Scroll the background slightly for visual interest 86 SCX_REG = counter >> 2; 87 SCY_REG = counter >> 3; 88 } 89}