cool-bun-demo/bun.c

323 lines
8.1 KiB
C
Raw Normal View History

2024-06-02 18:37:37 +00:00
#include <math.h>
2024-05-29 11:57:08 +00:00
// Custom
#include <hardware/custom.h>
// WaitBlit()
#include <clib/graphics_protos.h>
// AllocMem(), FreeMem()
#include <clib/exec_protos.h>
#include <exec/memory.h>
2024-05-28 12:02:28 +00:00
#include "screen.h"
#include "bun.h"
2024-05-29 11:57:08 +00:00
#include "system/system.h"
2024-05-28 12:02:28 +00:00
2024-06-02 18:42:36 +00:00
#define COOL_BUN_WIDTH (32)
#define COOL_BUN_WIDTH_BYTES (COOL_BUN_WIDTH / 8)
#define COOL_BUN_HEIGHT (32)
#define COOL_BUN_PLANES (2)
#define COOL_BUN_LAST_ROW (COOL_BUN_HEIGHT - 1)
#define COOL_BUN_LAST_ROW_BYTES (COOL_BUN_LAST_ROW * COOL_BUN_WIDTH_BYTES)
#define COOL_BUN_PLANE_SIZE (COOL_BUN_WIDTH_BYTES * COOL_BUN_HEIGHT)
#define COOL_BUN_MEMORY_SIZE (COOL_BUN_PLANE_SIZE * COOL_BUN_PLANES)
#define BUN_MAX_RANGE (31 + 320)
#define BUN_COUNT (12)
#define BUN_SPEED (1)
#define BUN_HORIZ_DISTANCE_BETWEEN_BUNS ((BUN_MAX_RANGE / 4) - COOL_BUN_WIDTH)
#define BUN_TOTAL_HORIZ_DISTANCE (BUN_HORIZ_DISTANCE_BETWEEN_BUNS + COOL_BUN_WIDTH)
#define BUN_ROW_START (30)
#define BUN_VERT_DISTANCE_BETWEEN_BUNS (20)
#define BUN_TOTAL_VERT_DISTANCE (COOL_BUN_HEIGHT + BUN_VERT_DISTANCE_BETWEEN_BUNS)
#define FRAME_MAX (BUN_TOTAL_HORIZ_DISTANCE / BUN_SPEED)
#define FRAMES_FOR_SCREEN (90)
#define BUN_WAVE_LENGTH (FRAMES_FOR_SCREEN / 2)
2024-05-29 11:57:08 +00:00
// linked as raw bytes in assembler
2024-05-28 12:02:28 +00:00
extern unsigned char far coolbun[];
2024-06-02 20:03:16 +00:00
unsigned char *coolbunArea = 0;
2024-05-28 12:02:28 +00:00
2024-05-29 11:57:08 +00:00
#define BLTCON0( \
minterm, aChan, bChan, cChan, dChan, shift \
) (minterm + (aChan << 11) + (bChan << 10) + (cChan << 9) + (dChan << 8) + (shift << 12))
#define BLTCON1(descending, shift) ((descending << 1) + (shift << 12))
extern struct Custom far custom;
2024-06-02 18:42:36 +00:00
void setupBun(void) {
2024-05-28 12:02:28 +00:00
unsigned char *currentCoolBunArea, *currentCoolBun;
coolbunArea = AllocMem(COOL_BUN_MEMORY_SIZE, MEMF_CHIP | MEMF_CLEAR);
currentCoolBunArea = coolbunArea;
2024-05-29 11:57:08 +00:00
currentCoolBun = coolbun;
2024-05-28 12:02:28 +00:00
2024-05-29 11:57:08 +00:00
CopyMem(coolbun, coolbunArea, COOL_BUN_MEMORY_SIZE);
2024-05-28 12:02:28 +00:00
}
2024-06-02 18:42:36 +00:00
void teardownBun(void) {
2024-06-02 20:03:16 +00:00
if (coolbunArea) {
FreeMem(coolbunArea, COOL_BUN_MEMORY_SIZE);
}
2024-05-28 12:02:28 +00:00
}
2024-06-01 11:42:11 +00:00
void bun_offRightSide(
int plusXValue,
int y,
struct ScreenSetup *screenSetup,
struct CurrentScreen *currentScreen
) {
2024-05-28 12:02:28 +00:00
uint8_t i, plane = 0;
uint8_t shift = plusXValue & 15;
uint8_t wordShift = (plusXValue >> 4);
uint16_t bltalwm;
for (plane = 0; plane < 2; ++plane) {
2024-05-29 11:57:08 +00:00
custom.bltcon0 = BLTCON0(0xc0, 0, 1, 0, 1, shift);
custom.bltcon1 = BLTCON1(0, shift);
2024-05-28 12:02:28 +00:00
custom.bltadat = 0xffff;
2024-05-29 11:57:08 +00:00
custom.bltbpt = coolbunArea +
plane * COOL_BUN_PLANE_SIZE;
custom.bltdpt = currentScreen->planes[plane] +
2024-06-01 11:42:11 +00:00
(y * screenSetup->byteWidth) +
screenSetup->byteWidth - COOL_BUN_WIDTH_BYTES +
2024-05-29 11:57:08 +00:00
wordShift * 2;
2024-05-28 12:02:28 +00:00
custom.bltafwm = 0xffff;
bltalwm = 0x0000;
2024-06-01 12:19:10 +00:00
for (i = 0; i <= 15 - shift; ++i) {
2024-05-28 12:02:28 +00:00
bltalwm += (1 << (15 - i));
}
custom.bltalwm = bltalwm;
custom.bltbmod = wordShift * 2;
2024-06-01 11:42:11 +00:00
custom.bltdmod = screenSetup->byteWidth - COOL_BUN_WIDTH_BYTES +
2024-05-29 11:57:08 +00:00
wordShift * 2;
2024-05-28 12:02:28 +00:00
custom.bltsize = 2 - wordShift + (COOL_BUN_HEIGHT << 6);
WaitBlit();
}
}
2024-06-01 11:42:11 +00:00
void bun_offLeftSide(
int minusXValue,
int y,
struct ScreenSetup *screenSetup,
struct CurrentScreen *currentScreen
) {
2024-05-28 12:02:28 +00:00
unsigned char plane;
uint8_t shift = minusXValue & 15;
uint8_t wordShift = (minusXValue >> 4);
uint8_t i;
uint16_t bltalwm;
// y can't be 0 otherwise we will corrupt memory for now
if (y == 0) return;
for (plane = 0; plane < 2; ++plane) {
// shift left, so descending
custom.bltcon0 = BLTCON0(0xc0, 0, 1, 0, 1, shift);
custom.bltcon1 = BLTCON1(1, shift);
// a has a mask we're shifting
custom.bltadat = 0xffff;
// b has bun data
2024-05-29 11:57:08 +00:00
custom.bltbpt = coolbunArea + 2 +
COOL_BUN_LAST_ROW_BYTES +
plane * COOL_BUN_PLANE_SIZE;
2024-05-28 12:02:28 +00:00
// d is the part on screen
2024-05-29 11:57:08 +00:00
custom.bltdpt = currentScreen->planes[plane] +
2024-06-01 11:42:11 +00:00
(screenSetup->byteWidth * (y + COOL_BUN_LAST_ROW)) +
2024-05-29 11:57:08 +00:00
2 -
wordShift * 2;
2024-05-28 12:02:28 +00:00
custom.bltafwm = 0xffff;
bltalwm = 0x0000;
2024-06-01 12:19:10 +00:00
for (i = 0; i <= 15 - shift; ++i) {
2024-05-28 12:02:28 +00:00
bltalwm += (1 << i);
}
custom.bltalwm = bltalwm;
custom.bltbmod = wordShift * 2;
2024-06-01 11:42:11 +00:00
custom.bltdmod = screenSetup->byteWidth - 4 + wordShift * 2;
2024-05-28 12:02:28 +00:00
custom.bltsize = 2 - wordShift + (COOL_BUN_HEIGHT << 6);
WaitBlit();
}
}
2024-06-01 11:42:11 +00:00
void bun_anywhere(
int x,
int y,
struct ScreenSetup *screenSetup,
struct CurrentScreen *currentScreen
) {
2024-05-28 12:02:28 +00:00
uint8_t plane;
uint8_t shift = x & 15;
uint8_t needsExtraWord = shift != 0;
for (plane = 0; plane < 2; ++plane) {
// if we extend the bun area by a word, we only need one write
// buns will never interfere with a background so they don't need a mask
// they do need the scratch area though
2024-05-29 11:57:08 +00:00
custom.bltcon0 = BLTCON0(0xc0, 0, 1, 0, 1, shift);
custom.bltcon1 = BLTCON1(0, shift);
2024-05-28 12:02:28 +00:00
custom.bltadat = 0xffff;
custom.bltbpt = coolbunArea + plane * COOL_BUN_PLANE_SIZE;
2024-05-29 11:57:08 +00:00
custom.bltdpt = currentScreen->planes[plane] +
2024-06-01 11:42:11 +00:00
WORD_ALIGNED_BYTE_POSITION(screenSetup->width, x, y);
2024-05-28 12:02:28 +00:00
// custom.bltdpt = screenSetup.memoryStart;
custom.bltafwm = 0xffff;
if (needsExtraWord) {
custom.bltalwm = 0x0000;
} else {
custom.bltalwm = 0xffff;
}
2024-05-29 11:57:08 +00:00
2024-05-28 12:02:28 +00:00
custom.bltbmod = -(needsExtraWord * 2);
2024-06-01 11:42:11 +00:00
custom.bltdmod = screenSetup->byteWidth - COOL_BUN_WIDTH_BYTES - (needsExtraWord * 2);
2024-05-28 12:02:28 +00:00
custom.bltsize = (2 + needsExtraWord) + (COOL_BUN_HEIGHT << 6);
WaitBlit();
}
}
2024-06-01 11:42:11 +00:00
void renderBun(
int x,
int y,
struct ScreenSetup *screenSetup,
struct CurrentScreen *currentScreen
) {
2024-05-28 12:02:28 +00:00
/**
* Conditions that will cause the program to crash if met. If your bun
* isn't rendering, it's due to here.
*
* TODO: Handle top/bottom off-screen as well.
*/
if (x < -31) return;
2024-06-01 11:42:11 +00:00
if (x > screenSetup->width + 31) return;
2024-05-28 12:02:28 +00:00
if (y < 1) return;
2024-06-01 11:42:11 +00:00
if (y > screenSetup->height- COOL_BUN_HEIGHT - 1) return;
2024-05-28 12:02:28 +00:00
if (x < 0) {
2024-06-01 11:42:11 +00:00
bun_offLeftSide(-x, y, screenSetup, currentScreen);
} else if (x > screenSetup->width - COOL_BUN_WIDTH) {
bun_offRightSide(x - (screenSetup->width - COOL_BUN_WIDTH), y, screenSetup, currentScreen);
2024-05-28 12:02:28 +00:00
} else {
2024-06-01 11:42:11 +00:00
bun_anywhere(x, y, screenSetup, currentScreen);
2024-05-28 12:02:28 +00:00
}
}
2024-06-02 18:37:37 +00:00
#define MAX_SINE_WAVE_CHANGE (20)
int bunAngleAdjustments[BUN_COUNT];
void calculateBunPositions(
uint16_t frame,
short int bunPositions[BUN_COUNT][2],
struct ScreenSetup *screenSetup
) {
int x, y, row, column, current;
float angle, startAngle;
frame %= FRAMES_FOR_SCREEN;
startAngle = (float)(frame % BUN_WAVE_LENGTH) * 360 / BUN_WAVE_LENGTH;
for (current = 0; current < BUN_COUNT; ++current) {
row = current / 4;
column = current % 4;
angle = startAngle + bunAngleAdjustments[current];
x = column * BUN_TOTAL_HORIZ_DISTANCE + ((float)frame * BUN_MAX_RANGE / FRAMES_FOR_SCREEN);
if (row == 1) {
x += BUN_TOTAL_HORIZ_DISTANCE / 2;
}
x %= BUN_MAX_RANGE;
x -= 31;
angle = angle * PI / 180;
y = BUN_ROW_START +
row * BUN_TOTAL_VERT_DISTANCE +
sin(angle) * MAX_SINE_WAVE_CHANGE;
bunPositions[current][0] = x;
bunPositions[current][1] = y;
}
}
2024-06-02 18:42:36 +00:00
short int allBunPositionsByFrame[FRAMES_FOR_SCREEN][BUN_COUNT][2];
void calculateAllBunPositions(
struct ScreenSetup *screenSetup
) {
int frame;
for (frame = 0; frame < FRAMES_FOR_SCREEN; ++frame) {
calculateBunPositions(
frame,
allBunPositionsByFrame[frame],
screenSetup
);
}
}
void buildBunAngleAdjustments() {
int current, angleAdjustment;
for (current = 0; current < BUN_COUNT; ++current) {
angleAdjustment = 0;
if (current % 2 == 1) angleAdjustment += 180;
if (current / 4 == 1) angleAdjustment += 90;
bunAngleAdjustments[current] = angleAdjustment;
}
}
2024-06-02 18:37:37 +00:00
void setupBunRenderer(
struct BunRenderer *bunRenderer,
struct ScreenSetup *screenSetup,
struct CurrentScreen *currentScreen) {
bunRenderer->screenSetup = screenSetup;
bunRenderer->currentScreen = currentScreen;
setupBun();
buildBunAngleAdjustments();
calculateAllBunPositions(screenSetup);
}
void renderBunFrame(
int frame,
struct BunRenderer *bunRenderer
) {
int bun;
frame %= FRAMES_FOR_SCREEN;
for (bun = 0; bun < BUN_COUNT; ++bun) {
renderBun(
allBunPositionsByFrame[frame][bun][0],
allBunPositionsByFrame[frame][bun][1],
bunRenderer->screenSetup,
bunRenderer->currentScreen
);
}
}
void teardownBunRenderer() {
teardownBun();
}