Waving Flag Animation
21 Dec 2024Introduction
In a previous post we made a simple water droplet demonstration. This is all built on the vga work that we’ve already done.
In today’s post, we’ll use this information again and put a waving flag demo together.
The effect that we’re looking to produce should look something like this:
The Idea
The waving flag effect simulates a piece of fabric rippling in the wind. Here’s the high-level approach:
- Flag Bitmap: Create a bitmap with alternating horizontal stripes of green and white.
- Wave Dynamics: Use trigonometric functions to displace pixels horizontally and vertically, simulating waves.
- Buffering: Use an offscreen buffer to avoid flickering during rendering.
Building the Flag
The flag is a simple bitmap composed of horizontal stripes alternating between green and white. Here’s how it’s generated:
Each stripe spans 10 rows, and the alternating colors give the flag a distinctive look.
Adding the Wave Effect
The waving effect is achieved by modifying the position of each pixel based on sine and cosine functions. Here’s the core logic:
Key Features:
- Wave Dynamics: The
wave_offset
creates a horizontal ripple effect based onsin(theta + xx * 0.1)
. A secondary vertical ripple adds realism. - Boundary Checks: Ensures pixels remain within the screen bounds.
- Direct Pixel Copy: Pixels are copied from the flag bitmap to the appropriate buffer position.
- Redundant Pixel Render: We make sure we render to all surrounding cells so we don’t experience tearing
Main Loop
The main loop ties everything together, handling synchronization, rendering, and input:
Highlights:
- Synchronization: The
wait_vsync()
call ensures smooth updates. - Animation: The
theta
value incrementally changes, creating continuous movement. - Keyboard Interrupt: The
kbhit()
function allows the user to exit gracefully.
Conclusion
This waving flag effect combines simple algorithms with creative use of VGA mode 13h to create a visually stunning effect. By leveraging trigonometry, palette manipulation, and efficient buffer handling, we replicate the mesmerizing motion of a flag in the wind.
You can find the complete code on GitHub as a gist.
Try it out, tweak the parameters, and share your own effects! There’s a lot of joy in creating beautiful visuals with minimal resources.