Copy of emulator-template with the Cyberflythrough sketch (pseudo-3D grid flight with tracing entities). Host includes the 60 fps pacing and delta-time Demo sample; APP defaults to Cyberflythrough.
74 lines
2.6 KiB
C++
74 lines
2.6 KiB
C++
/**
|
|
* Demo sketch — replace this file with your own experiment.
|
|
*
|
|
* Contract with the emulator host:
|
|
* - implement setup() — called once at launch
|
|
* - implement loop() — called once per frame (host paces at ~60 fps)
|
|
* - write pixels into emuGfx::fb[] (320x240, ABGR8888 uint32)
|
|
* - set emuGfx::dirty = true each frame you want presented
|
|
* - read keys from emu::keyboard (ASCII queue, filled from SDL events)
|
|
*
|
|
* Update state with DELTA TIME, not per-frame constants: measure the real
|
|
* seconds since the previous loop() call (see loop() below) and scale all
|
|
* speeds in "units per second" by it. Then the sketch runs at the same
|
|
* speed no matter what fps the host (or the real hardware) manages.
|
|
*
|
|
* This sample just bounces a colored box around — enough to prove the
|
|
* pipeline works. Delete everything and start from the structure below.
|
|
*/
|
|
|
|
#include "shim.h"
|
|
|
|
// Emulator state provided by the host
|
|
extern uint32_t emuGfx::fb[320 * 240];
|
|
extern bool emuGfx::dirty;
|
|
extern std::deque<uint8_t> emu::keyboard;
|
|
|
|
static constexpr int W = 320;
|
|
static constexpr int H = 240;
|
|
|
|
// --- Demo state -----------------------------------------------------------
|
|
static float boxX = 40, boxY = 30;
|
|
static float velX = 100.0f, velY = 70.0f; // pixels per second
|
|
static unsigned long lastMs = 0;
|
|
static constexpr float BOX = 24.0f;
|
|
|
|
void setup()
|
|
{
|
|
randomSeed(millis());
|
|
velX = 60.0f + random(100) * 1.2f; // 60..180 px/s
|
|
velY = 60.0f + random(100) * 1.2f;
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
// Delta time: seconds since the previous frame.
|
|
unsigned long now = millis();
|
|
float dt = lastMs ? (now - lastMs) / 1000.0f : 1.0f / 60.0f;
|
|
lastMs = now;
|
|
if (dt > 0.1f) dt = 0.1f; // clamp after stalls (drag, breakpoint)
|
|
|
|
// Move and bounce
|
|
boxX += velX * dt;
|
|
boxY += velY * dt;
|
|
if (boxX < 0 || boxX + BOX > W) { velX = -velX; boxX += velX * dt; }
|
|
if (boxY < 0 || boxY + BOX > H) { velY = -velY; boxY += velY * dt; }
|
|
|
|
// Clear the framebuffer (opaque black, alpha = 0xFF)
|
|
for (int i = 0; i < W * H; i++) emuGfx::fb[i] = 0xFF000000;
|
|
|
|
// Draw a filled box with a color that drifts over time
|
|
uint8_t r = (uint8_t)(128 + 127 * sinf(millis() / 900.0f));
|
|
uint32_t color = 0xFF000000u | ((uint32_t)r << 16) | 0x0000CC66u;
|
|
for (int y = (int)boxY; y < (int)(boxY + BOX); y++) {
|
|
if (y < 0 || y >= H) continue;
|
|
for (int x = (int)boxX; x < (int)(boxX + BOX); x++) {
|
|
if (x < 0 || x >= W) continue;
|
|
emuGfx::fb[y * W + x] = color;
|
|
}
|
|
}
|
|
|
|
// Present the frame
|
|
emuGfx::dirty = true;
|
|
}
|