cool-bun-demo/bun.c

196 lines
5.0 KiB
C

// 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"
// linked as raw bytes in assembler
extern unsigned char far coolbun[];
unsigned char *coolbunArea;
#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;
void setupBun() {
unsigned char *currentCoolBunArea, *currentCoolBun;
unsigned int x, y, plane;
coolbunArea = AllocMem(COOL_BUN_MEMORY_SIZE, MEMF_CHIP | MEMF_CLEAR);
currentCoolBunArea = coolbunArea;
currentCoolBun = coolbun;
CopyMem(coolbun, coolbunArea, COOL_BUN_MEMORY_SIZE);
}
void teardownBun() {
FreeMem(coolbunArea, COOL_BUN_MEMORY_SIZE);
}
void bun_offRightSide(
int plusXValue,
int y,
struct ScreenSetup *screenSetup,
struct CurrentScreen *currentScreen
) {
uint8_t i, plane = 0;
uint8_t shift = plusXValue & 15;
uint8_t wordShift = (plusXValue >> 4);
uint16_t bltalwm;
for (plane = 0; plane < 2; ++plane) {
custom.bltcon0 = BLTCON0(0xc0, 0, 1, 0, 1, shift);
custom.bltcon1 = BLTCON1(0, shift);
custom.bltadat = 0xffff;
custom.bltbpt = coolbunArea +
plane * COOL_BUN_PLANE_SIZE;
custom.bltdpt = currentScreen->planes[plane] +
(y * screenSetup->byteWidth) +
screenSetup->byteWidth - COOL_BUN_WIDTH_BYTES +
wordShift * 2;
custom.bltafwm = 0xffff;
bltalwm = 0x0000;
for (i = 0; i <= 15 - shift; ++i) {
bltalwm += (1 << (15 - i));
}
custom.bltalwm = bltalwm;
custom.bltbmod = wordShift * 2;
custom.bltdmod = screenSetup->byteWidth - COOL_BUN_WIDTH_BYTES +
wordShift * 2;
custom.bltsize = 2 - wordShift + (COOL_BUN_HEIGHT << 6);
WaitBlit();
}
}
void bun_offLeftSide(
int minusXValue,
int y,
struct ScreenSetup *screenSetup,
struct CurrentScreen *currentScreen
) {
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
custom.bltbpt = coolbunArea + 2 +
COOL_BUN_LAST_ROW_BYTES +
plane * COOL_BUN_PLANE_SIZE;
// d is the part on screen
custom.bltdpt = currentScreen->planes[plane] +
(screenSetup->byteWidth * (y + COOL_BUN_LAST_ROW)) +
2 -
wordShift * 2;
custom.bltafwm = 0xffff;
bltalwm = 0x0000;
for (i = 0; i <= 15 - shift; ++i) {
bltalwm += (1 << i);
}
custom.bltalwm = bltalwm;
custom.bltbmod = wordShift * 2;
custom.bltdmod = screenSetup->byteWidth - 4 + wordShift * 2;
custom.bltsize = 2 - wordShift + (COOL_BUN_HEIGHT << 6);
WaitBlit();
}
}
void bun_anywhere(
int x,
int y,
struct ScreenSetup *screenSetup,
struct CurrentScreen *currentScreen
) {
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
custom.bltcon0 = BLTCON0(0xc0, 0, 1, 0, 1, shift);
custom.bltcon1 = BLTCON1(0, shift);
custom.bltadat = 0xffff;
custom.bltbpt = coolbunArea + plane * COOL_BUN_PLANE_SIZE;
custom.bltdpt = currentScreen->planes[plane] +
WORD_ALIGNED_BYTE_POSITION(screenSetup->width, x, y);
// custom.bltdpt = screenSetup.memoryStart;
custom.bltafwm = 0xffff;
if (needsExtraWord) {
custom.bltalwm = 0x0000;
} else {
custom.bltalwm = 0xffff;
}
custom.bltbmod = -(needsExtraWord * 2);
custom.bltdmod = screenSetup->byteWidth - COOL_BUN_WIDTH_BYTES - (needsExtraWord * 2);
custom.bltsize = (2 + needsExtraWord) + (COOL_BUN_HEIGHT << 6);
WaitBlit();
}
}
void renderBun(
int x,
int y,
struct ScreenSetup *screenSetup,
struct CurrentScreen *currentScreen
) {
/**
* 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;
if (x > screenSetup->width + 31) return;
if (y < 1) return;
if (y > screenSetup->height- COOL_BUN_HEIGHT - 1) return;
if (x < 0) {
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);
} else {
bun_anywhere(x, y, screenSetup, currentScreen);
}
}