cool-bun-demo/main.c

231 lines
6.5 KiB
C
Raw Normal View History

2024-04-30 02:12:48 +00:00
#include <stdio.h>
#include <clib/exec_protos.h>
#include <clib/graphics_protos.h>
#include <exec/exec.h>
#include <hardware/custom.h>
#include <hardware/dmabits.h>
2024-06-11 02:04:40 +00:00
#include <hardware/cia.h>
2024-04-30 02:12:48 +00:00
2024-05-27 18:58:13 +00:00
#include <graphics/gfx.h>
2024-05-27 20:50:56 +00:00
#include "system/blitter.h"
#include "system/copper.h"
#include "system/system.h"
2024-05-02 16:52:06 +00:00
#include "screen.h"
#include "types.h"
2024-05-28 12:02:28 +00:00
#include "bun.h"
2024-04-30 02:12:48 +00:00
2024-05-02 16:52:06 +00:00
extern struct Custom far custom;
2024-06-11 02:04:40 +00:00
extern struct CIA far ciaa;
2024-05-27 18:58:13 +00:00
// this should be large enough to hold one bitplane of the largest object
// you are blitting, plus one additional word on each side
#define SCRATCH_AREA_WIDTH_BYTES (8)
#define SCRATCH_AREA_HEIGHT_ROWS (34)
#define SCRATCH_AREA_MEMORY_SIZE (SCRATCH_AREA_WIDTH_BYTES * SCRATCH_AREA_HEIGHT_ROWS)
volatile short *dbg = (volatile short *)0x100;
unsigned char *scratchArea;
struct ScreenSetup screenSetup;
2024-05-28 12:02:28 +00:00
struct CurrentScreen currentScreen;
2024-06-01 11:42:11 +00:00
void *copperlistBitplanePointers[8][2];
void *copperlistSpritePointers[8];
2024-05-27 18:58:13 +00:00
2024-05-28 12:02:28 +00:00
#define offsetof(s, m) &((struct s *)0)->m
uint16_t custom_color = (uint16_t)offsetof(Custom, color);
2024-06-01 11:42:11 +00:00
uint16_t custom_sprite = (uint16_t)offsetof(Custom, sprpt);
2024-06-02 20:03:16 +00:00
uint16_t custom_sprite_control = (uint16_t)offsetof(Custom, spr);
#define SPRPOS(x, y) (((y & 0xff) << 8) + ((x & 0x1fe) >> 1))
#define SPRCTL(x, y, height) ( \
((height & 0xff) << 8) + \
((y & 0x100) >> 6) + \
((height & 0x100) >> 7) + \
(x & 1) \
)
2024-05-27 18:58:13 +00:00
2024-06-11 02:04:40 +00:00
extern uint8_t chip TopazBitplanes[];
extern uint16_t chip CopperColors[];
extern uint16_t chip SpriteCopperlist[];
extern uint16_t chip SpriteData[];
2024-07-17 11:08:59 +00:00
extern uint8_t chip MaskBitplane[];
2024-06-11 02:04:40 +00:00
2024-06-13 16:30:39 +00:00
#define TOPAZ_WIDTH_PIXELS (160)
#define TOPAZ_WIDTH_BYTES (TOPAZ_WIDTH_PIXELS / 8)
#define TOPAZ_WIDTH_WORDS (TOPAZ_WIDTH_PIXELS / 16)
void renderTopaz(void) {
int plane;
uint16_t bltcon0, bltcmod;
uint8_t *bltbpt;
bltcon0 = 0xca + (1 << 8) + (1 << 9) + (1 << 10) + (1 << 11);
2024-06-13 16:30:39 +00:00
bltcmod = screenSetup.byteWidth - TOPAZ_WIDTH_BYTES;
bltbpt = TopazBitplanes;
for (plane = 0; plane < 3; ++plane) {
custom.bltcon0 = bltcon0;
custom.bltcon1 = 0;
custom.bltafwm = 0xffff;
custom.bltalwm = 0xffff;
custom.bltapt = MaskBitplane;
custom.bltbpt = bltbpt;
2024-06-13 16:30:39 +00:00
custom.bltcpt = currentScreen.planes[plane] + 8;
custom.bltdpt = currentScreen.planes[plane] + 8;
custom.bltamod = 0;
custom.bltbmod = 0;
custom.bltcmod = bltcmod;
custom.bltdmod = bltcmod;
2024-06-13 16:30:39 +00:00
custom.bltsize = TOPAZ_WIDTH_WORDS + (256 << 6);
2024-06-13 16:30:39 +00:00
bltbpt += TOPAZ_WIDTH_BYTES * 256;
WaitBlit();
}
}
2024-04-30 02:12:48 +00:00
int main(void) {
2024-06-11 02:04:40 +00:00
uint16_t *copperlist, *currentCopperlist, *currentCopperColors, result, *currentSpriteCopperlist;
uint32_t spriteDataPointer;
2024-06-02 20:03:16 +00:00
int i, x, y, height, plane;
2024-05-27 18:58:13 +00:00
2024-06-02 18:37:37 +00:00
struct BunRenderer bunRenderer;
2024-06-14 13:16:01 +00:00
printf("\nCool bun blitter, copper, and sprite demo by John Bintz\n");
printf("\n");
printf("This is my first real go at writing graphics code for the Amiga.\n");
printf("It's a combination of C (SAS/C) and Assembler (DevPac), with a Ruby\n");
printf("script to do some image processing. The screen only uses three\n");
printf("bitplanes -- one color is the background, two colors are the flying\n");
printf("cool bun logo white and black, and the remaining 5 colors are used\n");
printf("by the Topaz smiling art. The Ruby script will process the PNG file\n");
printf("and figure out the best 5 colors (plus b&w) for that line of the\n");
printf("image, setting the colors via copperlist for that line.\n");
printf("It also builds the clipping mask for the blitter.\n\n");
printf("And, I'm also using sprites to add more colors to the Topaz art.\n");
printf("The Ruby script will figure out what small areas of each line weren't\n");
printf("covered by the bitplane colors and use the 8 available sprites,\n");
printf("changing the colors on each line via copper, to fill in the gaps.\n");
printf("Cool buns are animated via blitter rather than sprites, but I\n");
printf("managed to get it to run at a decent speed regardless.\n");
printf("\nEnjoy, and thanks for watching!\n");
printf("John (theindustriousrabbit.com)\n\n");
2024-06-02 18:37:37 +00:00
2024-05-28 12:02:28 +00:00
setupScreen(&screenSetup, SCREEN_WIDTH, SCREEN_HEIGHT, 3);
setupInitialCurrentScreen(&screenSetup, &currentScreen);
2024-04-30 02:12:48 +00:00
2024-06-11 02:04:40 +00:00
setupBunRenderer(&bunRenderer, &screenSetup, &currentScreen);
2024-06-02 18:37:37 +00:00
2024-04-30 02:12:48 +00:00
// blitter copy the first bitplane row down to the second
2024-05-02 16:52:06 +00:00
copperlist = prepareNewCopperlist();
2024-04-30 02:12:48 +00:00
2024-06-01 11:42:11 +00:00
currentCopperlist = addDisplayToCopperlist(
copperlist,
&screenSetup,
&currentScreen,
&copperlistBitplanePointers
);
2024-06-11 02:04:40 +00:00
currentCopperColors = CopperColors;
currentSpriteCopperlist = SpriteCopperlist;
2024-05-28 12:02:28 +00:00
*(currentCopperlist++) = custom_color + 2;
*(currentCopperlist++) = 0x000;
*(currentCopperlist++) = custom_color + 4;
*(currentCopperlist++) = 0xfff;
2024-06-11 02:04:40 +00:00
for (i = 0; i < 8; ++i) {
spriteDataPointer = (uint32_t)&SpriteData;
spriteDataPointer += ((256 + 2) * 4) * i;
2024-06-02 20:03:16 +00:00
2024-06-11 02:04:40 +00:00
*(currentCopperlist++) = custom_sprite + i * 4;
*(currentCopperlist++) = (spriteDataPointer >> 16);
2024-06-02 20:03:16 +00:00
2024-06-11 02:04:40 +00:00
*(currentCopperlist++) = custom_sprite + 2 + i * 4;
*(currentCopperlist++) = (spriteDataPointer & 0xffff);
}
2024-06-02 20:03:16 +00:00
2024-06-11 02:04:40 +00:00
*(currentCopperlist++) = custom_color;
*(currentCopperlist++) = 0x3a6;
2024-06-02 20:03:16 +00:00
2024-05-28 12:02:28 +00:00
for (y = 0; y < 256; ++y) {
2024-06-11 02:04:40 +00:00
for (i = 0; i < 8; ++i) {
2024-06-13 16:30:39 +00:00
// set sprite color
2024-06-11 02:04:40 +00:00
*(currentCopperlist++) = custom_color + 32 + (
// sprite color group
(i / 2) * 4 +
// 0 is transparent
1 +
i % 2
) * 2;
*(currentCopperlist++) = *(currentSpriteCopperlist++);
2024-06-03 16:46:32 +00:00
2024-06-13 16:30:39 +00:00
// set sprite position
2024-06-11 02:04:40 +00:00
*(currentCopperlist++) = custom_sprite_control + i * 8;
*(currentCopperlist++) = *(currentSpriteCopperlist++);
2024-06-03 16:46:32 +00:00
2024-06-11 02:04:40 +00:00
*(currentCopperlist++) = custom_sprite_control + i * 8 + 2;
*(currentCopperlist++) = *(currentSpriteCopperlist++);
}
2024-06-03 16:46:32 +00:00
2024-06-11 02:04:40 +00:00
for (i = 3; i < 8; ++i) {
*(currentCopperlist++) = custom_color + (i * 2);
*(currentCopperlist++) = *(currentCopperColors++);
2024-06-03 16:46:32 +00:00
}
2024-06-02 20:03:16 +00:00
2024-06-11 02:04:40 +00:00
*(currentCopperlist++) = 1 + ((31 + (256 / 4)) << 1) + ((44 + y) << 8);
2024-05-28 12:02:28 +00:00
*(currentCopperlist++) = 0xFFFE;
}
2024-05-02 16:52:06 +00:00
endCopperlist(currentCopperlist);
2024-06-14 13:16:01 +00:00
takeOverSystem();
setCopperlist(copperlist);
setUpDisplay((uint32_t)screenSetup.bitplanes);
2024-06-11 02:04:40 +00:00
i = 0;
while (1) {
2024-06-01 11:42:11 +00:00
swapCurrentScreenBuffer(&screenSetup, &currentScreen);
2024-05-28 12:02:28 +00:00
2024-06-13 09:55:29 +00:00
clearCurrentBuns(&bunRenderer);
2024-06-11 02:04:40 +00:00
renderBunFrame(i, &bunRenderer);
renderTopaz();
2024-06-01 11:42:11 +00:00
updateDisplayInCopperList(
&screenSetup,
&currentScreen,
copperlistBitplanePointers
);
2024-06-11 02:04:40 +00:00
2024-06-14 13:16:01 +00:00
WaitBOF(250);
2024-06-13 16:30:39 +00:00
2024-06-11 02:04:40 +00:00
if ((ciaa.ciapra >> 6) != 3) break;
i++;
i %= FRAMES_FOR_SCREEN;
2024-05-28 12:02:28 +00:00
}
2024-05-27 18:58:13 +00:00
2024-05-02 16:52:06 +00:00
giveBackSystem();
2024-04-30 02:12:48 +00:00
2024-06-11 02:04:40 +00:00
teardownScreen(&screenSetup);
2024-05-02 16:52:06 +00:00
freeCopperlist(copperlist);
2024-04-30 02:12:48 +00:00
2024-06-02 18:42:36 +00:00
teardownBunRenderer();
2024-06-01 12:19:10 +00:00
2024-04-30 02:12:48 +00:00
return 0;
}