f256/f256lib/bitmap.c
2024-01-05 17:21:38 -06:00

175 lines
4.1 KiB
C

/*
* Copyright (c) 2024 Scott Duensing, scott@kangaroopunch.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "bitmap.h"
#include "dma.h"
static int16_t _MAX_X = 320;
static int16_t _MAX_Y = 240;
static uint32_t _BITMAP_BASE = 0x10000;
static byte _color = 255;
char error;
void clearBitmap(void) {
dmaFill(_BITMAP_BASE, (uint32_t)_MAX_X * (uint32_t)_MAX_Y, _color);
}
void defineGraphicsColor(byte slot, byte r, byte g, byte b) {
byte mmu = PEEK(MMU_IO_CTRL); // Get current MMU state.
byte *write;
POKE(MMU_IO_CTRL, MMU_IO_PAGE_1); // Swap I/O page 1 into bank 6.
write = (byte *)VKY_GR_CLUT_0 + slot * 4;
*write++ = b;
*write++ = g;
*write++ = r;
*write++ = 0xff;
POKE(MMU_IO_CTRL, mmu); // Restore MMU state.
}
void getBitmapResolution(int16_t *x, int16_t *y) {
*x = _MAX_X;
*y = _MAX_Y;
}
void line(int16_t x1, int16_t y1, int16_t x2, int16_t y2) {
int16_t x;
int16_t y;
int16_t dx;
int16_t dy;
int16_t incX;
int16_t incY;
int16_t balance;
if (x2 >= x1) {
dx = x2 - x1;
incX = 1;
} else {
dx = x1 - x2;
incX = -1;
}
if (y2 >= y1) {
dy = y2 - y1;
incY = 1;
} else {
dy = y1 - y2;
incY = -1;
}
x = x1;
y = y1;
if (dx >= dy) {
dy <<= 1;
balance = dy - dx;
dx <<= 1;
while (x != x2) {
putpixel(x, y);
if (balance >= 0) {
y += incY;
balance -= dx;
}
balance += dy;
x += incX;
}
putpixel(x, y);
} else {
dx <<= 1;
balance = dx - dy;
dy <<= 1;
while (y != y2) {
putpixel(x, y);
if (balance >= 0) {
x += incX;
balance -= dy;
}
balance += dx;
y += incY;
}
putpixel(x, y);
}
}
void putpixel(int16_t x, int16_t y) {
uint32_t pixelRAM;
byte block;
// We only map 8k of the bitmap into CPU RAM at once.
// We use slot 5 for this. We need to figure out
// where our pixel lands and bring that into RAM.
pixelRAM = _BITMAP_BASE + ((int32_t)y * (int32_t)_MAX_X) + (int32_t)x;
block = pixelRAM / 0x2000;
pixelRAM &= 0x1FFF; // Find offset into this block.
POKE(MMU_MEM_BANK_5, block);
POKE(0xa000 + pixelRAM, _color);
POKE(MMU_MEM_BANK_5, 5);
}
void resetGraphics(void) {
byte mmu = PEEK(MMU_IO_CTRL); // Get current MMU state.
int x;
_MAX_X = 320;
_MAX_Y = 240;
_BITMAP_BASE = 0x10000;
POKE(MMU_IO_CTRL, MMU_IO_PAGE_0); // Swap I/O page 0 into bank 6.
POKE(VKY_LAYER_CTRL_0, 16); // Bitmaps on all layers.
POKE(VKY_LAYER_CTRL_1, 1); // Bitmaps on all layers.
POKE(VKY_BM1_CTRL, 0); // Disable bitmap 1.
POKE(VKY_BM2_CTRL, 0); // Disable bitmap 2.
// Set up bitmap 0.
POKE(VKY_BM0_CTRL, 1); // Enable bitmap 0, GLUT 0.
POKEA(VKY_BM0_ADDR_L, _BITMAP_BASE); // Location of bitmap data.
//POKE(VKY_BM0_ADDR_L, _BITMAP_BASE & 0xFF); // Location of bitmap data.
//POKE(VKY_BM0_ADDR_M, (_BITMAP_BASE >> 8) & 0xFF); // Location of bitmap data.
//POKE(VKY_BM0_ADDR_H, (_BITMAP_BASE >> 16) & 0xFF); // Location of bitmap data.
POKE(MMU_IO_CTRL, mmu); // Restore MMU state.
// Set palette to a gradient so there's at least *something*.
for (x=0; x<256; x++) defineGraphicsColor(x, x, x, x);
_color = 0;
clearBitmap();
_color = 255;
}
void setGraphicsColor(byte c) {
_color = c;
}