cool-bun-demo/bun.c

558 lines
15 KiB
C

#include <math.h>
// Custom
#include <hardware/custom.h>
// WaitBlit()
#include <clib/graphics_protos.h>
// AllocMem(), FreeMem()
#include <clib/exec_protos.h>
#include <exec/memory.h>
#include "screen.h"
#include "bun.h"
#include "system/system.h"
#include "system/blitter.h"
#include "types.h"
#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_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 (50)
#define BUN_VERT_DISTANCE_BETWEEN_BUNS (30)
#define BUN_TOTAL_VERT_DISTANCE (COOL_BUN_HEIGHT + BUN_VERT_DISTANCE_BETWEEN_BUNS)
#define FRAME_MAX (BUN_TOTAL_HORIZ_DISTANCE / BUN_SPEED)
#define BUN_WAVE_LENGTH (FRAMES_FOR_SCREEN / 2)
// linked as raw bytes in assembler
extern unsigned char chip coolbun[];
extern struct Custom far custom;
struct BunClear {
uint16_t memoryStartOffsetBytes;
uint16_t widthWords;
uint16_t heightRows;
uint8_t direction;
};
#define BUN_OFF_RIGHT_SIDE (0)
#define BUN_OFF_LEFT_SIDE (1)
#define BUN_ANYWHERE (2)
uint8_t *coolbunPlaneStarts[3][2];
uint16_t bunRightSide_bltalwmPrecalc[16];
uint16_t bunLeftSide_bltalwmPrecalc[16];
void precalculateBunBLTALWM() {
int shift, j;
uint16_t bltalwm;
for (shift = 0; shift < 16; ++shift) {
// right
bltalwm = 0x0000;
for (j = 0; j < 15 - shift; ++j) {
bltalwm += (1 << (15 - j));
}
bunRightSide_bltalwmPrecalc[shift] = bltalwm;
//left
bltalwm = 0x0000;
for (j = 0; j <= 15 - shift; ++j) {
bltalwm += (1 << j);
}
bunLeftSide_bltalwmPrecalc[shift] = bltalwm;
}
}
struct PrecalcBunInfo {
uint32_t memoryStartOffsetBytes;
uint16_t bltcon0;
uint16_t bltcon1;
uint16_t bltdmod;
};
struct PrecalcBunInfo precalcBunInfo[FRAMES_FOR_SCREEN][BUN_COUNT];
void bun_offRightSide(
int plusXValue,
int y,
struct ScreenDefinition *screenDefinition,
struct ActiveScreenBufferDetails *activeScreenBufferDetails,
struct BunClear *bunClear,
struct PrecalcBunInfo *precalcBunInfo
) {
uint8_t i, plane = 0;
uint8_t shift = plusXValue & 15;
uint8_t wordShift = (plusXValue >> 4);
uint16_t bltalwm, bltcon0, bltcon1, bltdmod, bltsize;
/*
bunClear->memoryStartOffsetBytes = (y * screenDefinition->byteWidth) +
screenDefinition->byteWidth - COOL_BUN_WIDTH_BYTES +
wordShift * 2;
*/
bunClear->memoryStartOffsetBytes = precalcBunInfo->memoryStartOffsetBytes;
bunClear->heightRows = COOL_BUN_HEIGHT;
bunClear->widthWords = 2 - wordShift;
bunClear->direction = BLITTER_ASCENDING;
bltalwm = bunRightSide_bltalwmPrecalc[shift];
/*
bltalwm = 0x0000;
for (i = 0; i <= 15 - shift; ++i) {
bltalwm += (1 << (15 - i));
}
*/
//bltcon0 = BLTCON0(0xf0, 1, 0, 0, 1, shift);
//bltcon1 = BLTCON1(bunClear->direction, shift);
//bltdmod = screenDefinition->byteWidth - COOL_BUN_WIDTH_BYTES +
// wordShift * 2;
bltcon0 = precalcBunInfo->bltcon0;
bltcon1 = precalcBunInfo->bltcon1;
bltdmod = precalcBunInfo->bltdmod;
bltsize = bunClear->widthWords + (bunClear->heightRows << 6);
for (plane = 0; plane < 2; ++plane) {
custom.bltcon0 = bltcon0;
custom.bltcon1 = bltcon1;
custom.bltapt = coolbunPlaneStarts[BUN_OFF_RIGHT_SIDE][plane];
custom.bltdpt = activeScreenBufferDetails->planes[plane] +
bunClear->memoryStartOffsetBytes;
custom.bltafwm = 0xffff;
custom.bltalwm = bltalwm;
custom.bltamod = wordShift * 2;
custom.bltdmod = bltdmod;
custom.bltsize = bltsize;
WaitBlit();
}
}
void bun_offLeftSide(
int minusXValue,
int y,
struct ScreenDefinition *screenDefinition,
struct ActiveScreenBufferDetails *activeScreenBufferDetails,
struct BunClear *bunClear,
struct PrecalcBunInfo *precalcBunInfo
) {
unsigned char plane;
uint8_t shift = minusXValue & 15;
uint8_t wordShift = (minusXValue >> 4);
uint8_t i;
uint16_t bltalwm, bltcon0, bltcon1, bltdmod, bltsize;
// y can't be 0 otherwise we will corrupt memory for now
if (y == 0) return;
/*
bunClear->memoryStartOffsetBytes = (screenDefinition->byteWidth * (y + COOL_BUN_LAST_ROW)) +
2 - wordShift * 2;
*/
bunClear->memoryStartOffsetBytes = precalcBunInfo->memoryStartOffsetBytes;
bunClear->heightRows = COOL_BUN_HEIGHT;
bunClear->widthWords = 2 - wordShift;
bunClear->direction = BLITTER_DESCENDING;
bltalwm = bunLeftSide_bltalwmPrecalc[shift];
/*
bltalwm = 0x0000;
for (i = 0; i <= 15 - shift; ++i) {
bltalwm += (1 << i);
}
*/
//bltcon0 = BLTCON0(0xf0, 1, 0, 0, 1, shift);
//bltcon1 = BLTCON1(bunClear->direction, shift);
//bltdmod = screenDefinition->byteWidth - 4 + wordShift * 2;
bltcon0 = precalcBunInfo->bltcon0;
bltcon1 = precalcBunInfo->bltcon1;
bltdmod = precalcBunInfo->bltdmod;
bltsize = bunClear->widthWords + (bunClear->heightRows << 6);
for (plane = 0; plane < 2; ++plane) {
// shift left, so descending
custom.bltcon0 = bltcon0;
custom.bltcon1 = bltcon1;
// a has bun data
custom.bltapt = coolbunPlaneStarts[BUN_OFF_LEFT_SIDE][plane];
// d is the part on screen
custom.bltdpt = activeScreenBufferDetails->planes[plane] + bunClear->memoryStartOffsetBytes;
custom.bltafwm = 0xffff;
custom.bltalwm = bltalwm;
custom.bltamod = wordShift * 2;
custom.bltdmod = bltdmod;
custom.bltsize = bltsize;
WaitBlit();
}
}
void bun_anywhere(
int x,
int y,
struct ScreenDefinition *screenDefinition,
struct ActiveScreenBufferDetails *activeScreenBufferDetails,
struct BunClear *bunClear,
struct PrecalcBunInfo *precalcBunInfo
) {
uint8_t plane;
uint8_t shift = x & 15;
uint8_t needsExtraWord = shift != 0;
uint16_t bltcon0, bltcon1, bltalwm, bltamod, bltdmod, bltsize;
//bunClear->memoryStartOffsetBytes = WORD_ALIGNED_BYTE_POSITION(screenDefinition->width, x, y);
bunClear->memoryStartOffsetBytes = precalcBunInfo->memoryStartOffsetBytes;
bunClear->heightRows = COOL_BUN_HEIGHT;
bunClear->widthWords = 2 + needsExtraWord;
bunClear->direction = BLITTER_ASCENDING;
// buns will never interfere with a background so they don't need a mask
//bltcon0 = BLTCON0(0xf0, 1, 0, 0, 1, shift);
//bltcon1 = BLTCON1(bunClear->direction, shift);
//bltdmod = screenDefinition->byteWidth - COOL_BUN_WIDTH_BYTES - (needsExtraWord * 2);
bltcon0 = precalcBunInfo->bltcon0;
bltcon1 = precalcBunInfo->bltcon1;
bltdmod = precalcBunInfo->bltdmod;
if (needsExtraWord) {
bltalwm = 0x0000;
} else {
bltalwm = 0xffff;
}
bltamod = -(needsExtraWord * 2);
bltsize = bunClear->widthWords + (bunClear->heightRows << 6);
for (plane = 0; plane < 2; ++plane) {
custom.bltcon0 = bltcon0;
custom.bltcon1 = bltcon1;
custom.bltapt = coolbunPlaneStarts[BUN_ANYWHERE][plane];
custom.bltdpt = activeScreenBufferDetails->planes[plane] +
bunClear->memoryStartOffsetBytes;
custom.bltafwm = 0xffff;
custom.bltalwm = bltalwm;
custom.bltamod = bltamod;
custom.bltdmod = bltdmod;
custom.bltsize = bltsize;
WaitBlit();
}
}
#define ENABLE_RENDER_BUN_CHECKING (0)
uint16_t screenDefCoolBunWidthCheck;
void renderBun(
int x,
int y,
struct ScreenDefinition *screenDefinition,
struct ActiveScreenBufferDetails *activeScreenBufferDetails,
struct BunClear *bunClear,
struct PrecalcBunInfo *precalcBunInfo
) {
/**
* Conditions that will cause the program to crash if met. If your bun
* isn't rendering, it's due to here. Turn off checking once you know
* everything is solid to save some CPU.
*
* TODO: Handle top/bottom off-screen as well.
*/
if (ENABLE_RENDER_BUN_CHECKING) {
if (x < -31) return;
if (x > screenDefinition->width + 31) return;
if (y < 1) return;
if (y > screenDefinition->height - COOL_BUN_HEIGHT - 1) return;
}
if (x < 0) {
bun_offLeftSide(
-x,
y,
screenDefinition,
activeScreenBufferDetails,
bunClear,
precalcBunInfo
);
//} else if (x > screenDefinition->width - COOL_BUN_WIDTH) {
} else if (x > screenDefCoolBunWidthCheck) {
bun_offRightSide(
//x - (screenDefinition->width - COOL_BUN_WIDTH),
x - screenDefCoolBunWidthCheck,
y,
screenDefinition,
activeScreenBufferDetails,
bunClear,
precalcBunInfo
);
} else {
bun_anywhere(
x,
y,
screenDefinition,
activeScreenBufferDetails,
bunClear,
precalcBunInfo
);
}
}
#define MAX_SINE_WAVE_CHANGE (20)
int bunAngleAdjustments[BUN_COUNT];
void calculateBunPositions(
uint16_t frame,
short int bunPositions[BUN_COUNT][2],
struct ScreenDefinition *screenDefinition
) {
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;
}
}
short int allBunPositionsByFrame[FRAMES_FOR_SCREEN][BUN_COUNT][2];
void precalculateBunInfoForFrames(
struct ScreenDefinition *screenDefinition
) {
int frame, bun;
int x, y;
uint8_t wordShift, shift, needsExtraWord;
screenDefCoolBunWidthCheck = screenDefinition->width - COOL_BUN_WIDTH;
for (frame = 0; frame < FRAMES_FOR_SCREEN; ++frame) {
for (bun = 0; bun < BUN_COUNT; ++bun) {
x = allBunPositionsByFrame[frame][bun][0];
y = allBunPositionsByFrame[frame][bun][1];
// off left
if (x < 0) {
x = -x;
shift = x & 15;
wordShift = x >> 4;
precalcBunInfo[frame][bun].memoryStartOffsetBytes =
(screenDefinition->byteWidth * (y + COOL_BUN_LAST_ROW)) +
2 - wordShift * 2;
precalcBunInfo[frame][bun].bltcon0 = BLTCON0(0xf0, 1, 0, 0, 1, shift);
precalcBunInfo[frame][bun].bltcon1 = BLTCON1(BLITTER_DESCENDING, shift);
precalcBunInfo[frame][bun].bltdmod =
screenDefinition->byteWidth - 4 +
wordShift * 2;
// off right
} else if (x > screenDefinition->width - COOL_BUN_WIDTH) {
x = x - (screenDefinition->width - COOL_BUN_WIDTH),
wordShift = x >> 4;
shift = x & 15;
precalcBunInfo[frame][bun].memoryStartOffsetBytes =
(y * screenDefinition->byteWidth) +
screenDefinition->byteWidth - COOL_BUN_WIDTH_BYTES +
wordShift * 2;
precalcBunInfo[frame][bun].bltcon0 = BLTCON0(0xf0, 1, 0, 0, 1, shift);
precalcBunInfo[frame][bun].bltcon1 = BLTCON1(BLITTER_ASCENDING, shift);
precalcBunInfo[frame][bun].bltdmod =
screenDefinition->byteWidth - COOL_BUN_WIDTH_BYTES +
wordShift * 2;
// anywhere
} else {
shift = x & 15;
needsExtraWord = shift != 0;
precalcBunInfo[frame][bun].memoryStartOffsetBytes =
WORD_ALIGNED_BYTE_POSITION(screenDefinition->width, x, y);
precalcBunInfo[frame][bun].bltcon0 = BLTCON0(0xf0, 1, 0, 0, 1, shift);
precalcBunInfo[frame][bun].bltcon1 = BLTCON1(BLITTER_ASCENDING, shift);
precalcBunInfo[frame][bun].bltdmod =
screenDefinition->byteWidth - COOL_BUN_WIDTH_BYTES - (needsExtraWord * 2);
}
}
}
}
void calculateAllBunPositions(
struct ScreenDefinition *screenDefinition
) {
int frame;
for (frame = 0; frame < FRAMES_FOR_SCREEN; ++frame) {
calculateBunPositions(
frame,
allBunPositionsByFrame[frame],
screenDefinition
);
}
}
void buildBunAngleAdjustments(void) {
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;
}
}
void precalculateBunRenderInfo(void) {
int plane;
for (plane = 0; plane < 2; ++plane) {
coolbunPlaneStarts[BUN_OFF_RIGHT_SIDE][plane] = coolbun + plane * COOL_BUN_PLANE_SIZE;
coolbunPlaneStarts[BUN_ANYWHERE][plane] = coolbun + plane * COOL_BUN_PLANE_SIZE;
coolbunPlaneStarts[BUN_OFF_LEFT_SIDE][plane] = coolbun + 2 +
COOL_BUN_LAST_ROW_BYTES +
plane * COOL_BUN_PLANE_SIZE;
}
}
void setupBunRenderer(
struct BunRenderer *bunRenderer,
struct ScreenDefinition *screenDefinition,
struct ActiveScreenBufferDetails *activeScreenBufferDetails) {
bunRenderer->screenDefinition = screenDefinition;
bunRenderer->activeScreenBufferDetails = activeScreenBufferDetails;
buildBunAngleAdjustments();
calculateAllBunPositions(screenDefinition);
precalculateBunRenderInfo();
precalculateBunBLTALWM();
precalculateBunInfoForFrames(screenDefinition);
}
/**
* Is there bun clear information for this frame? This is not true for the
* first frame in the demo.
*/
int hasBunClear[2] = {0,0};
struct BunClear bunClearForScreen[2][BUN_COUNT];
void clearCurrentBuns(
struct BunRenderer *bunRenderer
) {
int bun, plane;
struct BunClear *currentBunClear;
uint16_t bltcon0, bltcon1, bltdmod, bltsize;
if (!hasBunClear[bunRenderer->activeScreenBufferDetails->currentBuffer]) return;
bltcon0 = BLTCON0(0xc0, 0, 0, 0, 1, 0);
for (bun = 0; bun < BUN_COUNT; ++bun) {
currentBunClear = &bunClearForScreen[bunRenderer->activeScreenBufferDetails->currentBuffer][bun];
bltcon1 = BLTCON1(currentBunClear->direction, 0);
bltdmod = bunRenderer->screenDefinition->byteWidth - (currentBunClear->widthWords * 2);
bltsize = currentBunClear->widthWords + (currentBunClear->heightRows << 6);
for (plane = 0; plane < 2; ++plane) {
custom.bltcon0 = bltcon0;
custom.bltcon1 = bltcon1;
custom.bltadat = 0x0000;
custom.bltafwm = 0xffff;
custom.bltalwm = 0xffff;
custom.bltdpt = bunRenderer->activeScreenBufferDetails->planes[plane] +
currentBunClear->memoryStartOffsetBytes;
custom.bltdmod = bltdmod;
custom.bltsize = bltsize;
WaitBlit();
}
}
}
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->screenDefinition,
bunRenderer->activeScreenBufferDetails,
&bunClearForScreen[bunRenderer->activeScreenBufferDetails->currentBuffer][bun],
&precalcBunInfo[frame][bun]
);
}
hasBunClear[bunRenderer->activeScreenBufferDetails->currentBuffer] = 1;
}
void teardownBunRenderer() {
}