2017-02-26 16:00:41 +00:00
|
|
|
#include <ctype.h> // isgraph
|
|
|
|
#include "sdl-display.h"
|
|
|
|
|
2018-01-07 13:24:53 +00:00
|
|
|
#include "images.h"
|
2017-02-26 16:00:41 +00:00
|
|
|
|
|
|
|
#include "globals.h"
|
|
|
|
#include "applevm.h"
|
|
|
|
|
2018-01-07 19:43:17 +00:00
|
|
|
#include "apple/appleui.h"
|
2021-01-19 14:35:25 +00:00
|
|
|
// FIXME should be able to omit this include and rely on the xterns, which
|
2021-01-09 12:14:14 +00:00
|
|
|
// would prove it's linking properly
|
|
|
|
#include "apple/font.h"
|
|
|
|
extern const unsigned char ucase_glyphs[512];
|
|
|
|
extern const unsigned char lcase_glyphs[256];
|
|
|
|
extern const unsigned char mousetext_glyphs[256];
|
|
|
|
extern const unsigned char interface_glyphs[256];
|
2018-01-07 19:43:17 +00:00
|
|
|
|
2017-02-26 16:00:41 +00:00
|
|
|
// RGB map of each of the lowres colors
|
|
|
|
const uint8_t loresPixelColors[16][3] = { { 0, 0, 0 }, // black
|
2021-01-18 01:45:44 +00:00
|
|
|
{ 0xAC, 0x12, 0x4C }, // magenta
|
|
|
|
{ 0x00, 0x07, 0x83 }, // dark blue
|
|
|
|
{ 0xAA, 0x1A, 0xD1 }, // purple
|
|
|
|
{ 0x00, 0x83, 0x2F }, // dark green
|
|
|
|
{ 0x9F, 0x97, 0x7E }, // drak grey
|
|
|
|
{ 0x00, 0x8A, 0xB5 }, // medium blue
|
|
|
|
{ 0x9F, 0x9E, 0xFF }, // light blue
|
|
|
|
{ 0x7A, 0x5F, 0x00 }, // brown
|
|
|
|
{ 0xFF, 0x72, 0x47 }, // orange
|
|
|
|
{ 0x78, 0x68, 0x7F }, // light gray
|
|
|
|
{ 0xFF, 0x7A, 0xCF }, // pink
|
|
|
|
{ 0x6F, 0xE6, 0x2C }, // green
|
|
|
|
{ 0xFF, 0xF6, 0x7B }, // yellow
|
|
|
|
{ 0x6C, 0xEE, 0xB2 }, // aqua
|
|
|
|
{ 0xFF, 0xFF, 0xFF } // white
|
2017-02-26 16:00:41 +00:00
|
|
|
};
|
|
|
|
|
2021-01-19 14:35:25 +00:00
|
|
|
#define color16To32(x) ( (((x) & 0xF800) << 8) | (((x) & 0x07E0) << 5) | (((x) & 0x001F)<<3) )
|
|
|
|
#define packColor32(x) ( (x[0] << 16) | (x[1] << 8) | (x[2]) )
|
2021-01-19 23:40:13 +00:00
|
|
|
#define unpackRed(x) ( ((x) & 0xFF0000) >> 16 )
|
|
|
|
#define unpackGreen(x) ( ((x) & 0xFF00) >> 8 )
|
|
|
|
#define unpackBlue(x) ( ((x) & 0xFF) )
|
2021-01-19 14:35:25 +00:00
|
|
|
// FIXME this blend could be optimized - and it's a dumb blend that
|
|
|
|
// just averages RGB values individually, rather than trying to find a
|
|
|
|
// sane blend of 2 pixels. Needs thought.
|
2021-01-19 23:40:13 +00:00
|
|
|
#define blendPackedColor(x,y) ( (((unpackRed(x) + unpackRed(y))/2) << 16) + (((unpackGreen(x) + unpackGreen(y))/2) << 8) + ((unpackBlue(x) + unpackBlue(y))/2) )
|
|
|
|
#define luminanceFromRGB(r,g,b) ( ((r)*0.2126) + ((g)*0.7152) + ((b)*0.0722) )
|
2021-01-19 14:35:25 +00:00
|
|
|
|
2017-02-26 16:00:41 +00:00
|
|
|
SDLDisplay::SDLDisplay()
|
|
|
|
{
|
2018-02-18 01:44:04 +00:00
|
|
|
memset(videoBuffer, 0, sizeof(videoBuffer));
|
|
|
|
|
2017-02-26 16:00:41 +00:00
|
|
|
// FIXME: abstract constants
|
|
|
|
screen = SDL_CreateWindow("Aiie!",
|
|
|
|
SDL_WINDOWPOS_UNDEFINED,
|
|
|
|
SDL_WINDOWPOS_UNDEFINED,
|
2022-01-16 12:14:58 +00:00
|
|
|
800, 480,
|
2021-01-21 01:55:11 +00:00
|
|
|
SDL_WINDOW_SHOWN|SDL_WINDOW_RESIZABLE);
|
2017-02-26 16:00:41 +00:00
|
|
|
|
|
|
|
// SDL_RENDERER_SOFTWARE because, at least on my Mac, this has some
|
|
|
|
// serious issues with hardware accelerated drawing (flashing and crashing).
|
|
|
|
renderer = SDL_CreateRenderer(screen, -1, SDL_RENDERER_SOFTWARE);
|
|
|
|
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); // set to white
|
|
|
|
SDL_RenderClear(renderer); // clear it to the selected color
|
|
|
|
SDL_RenderPresent(renderer); // perform the render
|
2021-01-21 01:55:11 +00:00
|
|
|
|
2022-01-16 12:14:58 +00:00
|
|
|
// ***
|
2021-01-21 01:55:11 +00:00
|
|
|
buffer = SDL_CreateTexture(renderer,
|
|
|
|
SDL_PIXELFORMAT_RGB888,
|
|
|
|
SDL_TEXTUREACCESS_STREAMING,
|
2022-01-16 12:14:58 +00:00
|
|
|
800,
|
|
|
|
480);
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SDLDisplay::~SDLDisplay()
|
|
|
|
{
|
|
|
|
SDL_Quit();
|
|
|
|
}
|
|
|
|
|
2018-02-07 15:20:26 +00:00
|
|
|
void SDLDisplay::flush()
|
|
|
|
{
|
2021-01-19 14:35:25 +00:00
|
|
|
blit();
|
2018-02-07 15:20:26 +00:00
|
|
|
SDL_RenderPresent(renderer);
|
|
|
|
}
|
|
|
|
|
2017-02-26 16:00:41 +00:00
|
|
|
void SDLDisplay::redraw()
|
|
|
|
{
|
|
|
|
// primarily for the device, where it's in and out of the
|
|
|
|
// bios. Draws the background image.
|
2018-01-07 19:43:17 +00:00
|
|
|
g_ui->drawStaticUIElement(UIeOverlay);
|
2017-02-26 16:00:41 +00:00
|
|
|
|
2018-01-07 19:43:17 +00:00
|
|
|
if (g_vm && g_ui) {
|
|
|
|
// determine whether or not a disk is inserted & redraw each drive
|
|
|
|
g_ui->drawOnOffUIElement(UIeDisk1_state, ((AppleVM *)g_vm)->DiskName(0)[0] == '\0');
|
|
|
|
g_ui->drawOnOffUIElement(UIeDisk2_state, ((AppleVM *)g_vm)->DiskName(1)[0] == '\0');
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-07 19:43:17 +00:00
|
|
|
void SDLDisplay::drawImageOfSizeAt(const uint8_t *img,
|
2021-01-21 02:16:32 +00:00
|
|
|
uint16_t sizex, uint16_t sizey,
|
|
|
|
uint16_t wherex, uint16_t wherey)
|
2017-02-26 16:00:41 +00:00
|
|
|
{
|
2021-01-21 02:16:32 +00:00
|
|
|
for (uint16_t y=0; y<sizey; y++) {
|
2022-01-16 12:14:58 +00:00
|
|
|
const uint8_t *p = &img[(y * sizex)*3];
|
2018-01-07 19:43:17 +00:00
|
|
|
for (uint16_t x=0; x<sizex; x++) {
|
2022-01-16 12:14:58 +00:00
|
|
|
videoBuffer[(y+wherey)][(x+wherex)] = packColor32(p);
|
|
|
|
p += 3;
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 01:31:37 +00:00
|
|
|
void SDLDisplay::blit()
|
|
|
|
{
|
2021-01-21 01:55:11 +00:00
|
|
|
uint32_t *pixels = NULL;
|
|
|
|
int pitch = 0;
|
|
|
|
SDL_LockTexture(buffer,
|
|
|
|
NULL, // NULL means the *whole texture* here.
|
|
|
|
(void **)&pixels,
|
|
|
|
&pitch);
|
|
|
|
// FIXME what if pitch isn't as expected? Should be width*4
|
|
|
|
|
|
|
|
memcpy(pixels, videoBuffer, sizeof(videoBuffer));
|
|
|
|
|
|
|
|
SDL_UnlockTexture(buffer);
|
|
|
|
SDL_RenderCopy(renderer, buffer, NULL, NULL);
|
|
|
|
SDL_RenderPresent(renderer);
|
2020-07-07 01:31:37 +00:00
|
|
|
}
|
|
|
|
|
2018-02-18 01:44:04 +00:00
|
|
|
// Blit the videoBuffer to the display device, in the rect given
|
2017-02-26 16:00:41 +00:00
|
|
|
void SDLDisplay::blit(AiieRect r)
|
|
|
|
{
|
2021-01-21 01:55:11 +00:00
|
|
|
blit();
|
|
|
|
/*
|
2017-02-26 16:00:41 +00:00
|
|
|
if (overlayMessage[0]) {
|
|
|
|
drawString(M_SELECTDISABLED, 1, 240 - 16 - 12, overlayMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_RenderPresent(renderer);
|
2021-01-21 01:55:11 +00:00
|
|
|
*/
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void putpixel(SDL_Renderer *renderer, int x, int y, uint8_t r, uint8_t g, uint8_t b)
|
|
|
|
{
|
|
|
|
SDL_SetRenderDrawColor(renderer, r, g, b, 255);
|
|
|
|
SDL_RenderDrawPoint(renderer, x, y);
|
|
|
|
}
|
|
|
|
|
2018-02-18 01:44:04 +00:00
|
|
|
void SDLDisplay::drawUIPixel(uint16_t x, uint16_t y, uint16_t color)
|
|
|
|
{
|
2022-01-16 12:14:58 +00:00
|
|
|
// ***
|
|
|
|
if (x >= 800 || y >= 480) return; // make sure it's onscreen
|
|
|
|
|
|
|
|
videoBuffer[y][x] = color16To32(color);
|
2018-02-18 01:44:04 +00:00
|
|
|
}
|
|
|
|
|
2018-01-07 19:43:17 +00:00
|
|
|
void SDLDisplay::drawPixel(uint16_t x, uint16_t y, uint16_t color)
|
2017-02-26 16:00:41 +00:00
|
|
|
{
|
|
|
|
uint8_t
|
|
|
|
r = (color & 0xF800) >> 8,
|
|
|
|
g = (color & 0x7E0) >> 3,
|
|
|
|
b = (color & 0x1F) << 3;
|
|
|
|
|
2020-07-09 06:38:03 +00:00
|
|
|
// Pixel-doubling vertically and horizontally, based on scale
|
2022-01-16 12:14:58 +00:00
|
|
|
// for (int xoff=0; xoff<SDLDISPLAY_SCALE; xoff++) {
|
|
|
|
// putpixel(renderer, x+xoff, yoff+y*SDLDISPLAY_SCALE, r, g, b);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
for (int yoff=0; yoff<2; yoff++) {
|
|
|
|
putpixel(renderer, x, (y*2)+yoff, r, g, b);
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-07 19:43:17 +00:00
|
|
|
void SDLDisplay::drawPixel(uint16_t x, uint16_t y, uint8_t r, uint8_t g, uint8_t b)
|
2017-02-26 16:00:41 +00:00
|
|
|
{
|
2020-07-09 06:38:03 +00:00
|
|
|
// Pixel-doubling horizontally and vertically, based on scale
|
2022-01-16 12:14:58 +00:00
|
|
|
// ***
|
|
|
|
// for (int yoff=0; yoff<SDLDISPLAY_SCALE; yoff++) {
|
|
|
|
// for (int xoff=0; xoff<SDLDISPLAY_SCALE; xoff++) {
|
|
|
|
// putpixel(renderer, x+xoff, yoff+y*SDLDISPLAY_SCALE, r, g, b);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
for (int yoff=0; yoff<2; yoff++) {
|
|
|
|
putpixel(renderer, x, (y*2)+yoff, r, g, b);
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 02:16:32 +00:00
|
|
|
void SDLDisplay::drawCharacter(uint8_t mode, uint16_t x, uint16_t y, char c)
|
2017-02-26 16:00:41 +00:00
|
|
|
{
|
|
|
|
int8_t xsize = 8,
|
2021-01-09 12:14:14 +00:00
|
|
|
ysize = 0x07;
|
2022-01-16 12:14:58 +00:00
|
|
|
|
2017-02-26 16:00:41 +00:00
|
|
|
uint16_t offPixel, onPixel;
|
|
|
|
switch (mode) {
|
|
|
|
case M_NORMAL:
|
|
|
|
onPixel = 0xFFFF;
|
|
|
|
offPixel = 0x0010;
|
|
|
|
break;
|
|
|
|
case M_SELECTED:
|
|
|
|
onPixel = 0x0000;
|
|
|
|
offPixel = 0xFFFF;
|
|
|
|
break;
|
|
|
|
case M_DISABLED:
|
|
|
|
default:
|
|
|
|
onPixel = 0x7BEF;
|
|
|
|
offPixel = 0x0000;
|
|
|
|
break;
|
|
|
|
case M_SELECTDISABLED:
|
|
|
|
onPixel = 0x7BEF;
|
|
|
|
offPixel = 0xFFE0;
|
|
|
|
break;
|
2021-01-09 17:15:12 +00:00
|
|
|
case M_PLAIN:
|
|
|
|
onPixel = 0xFFFF;
|
|
|
|
offPixel = 0x0000;
|
|
|
|
break;
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
|
2022-01-16 12:14:58 +00:00
|
|
|
// scale up the font
|
2021-01-09 12:14:14 +00:00
|
|
|
const unsigned char *ch = asciiToAppleGlyph(c);
|
2017-02-26 16:00:41 +00:00
|
|
|
for (int8_t y_off = 0; y_off <= ysize; y_off++) {
|
|
|
|
for (int8_t x_off = 0; x_off <= xsize; x_off++) {
|
2021-01-09 12:14:14 +00:00
|
|
|
if (*ch & (1 << (x_off))) {
|
2022-01-16 12:14:58 +00:00
|
|
|
for (int8_t ys=0; ys<2; ys++) {
|
|
|
|
for (int8_t xs=0; xs<2; xs++) {
|
|
|
|
drawUIPixel((x + x_off)*2+xs, (y+y_off)*2 + ys, onPixel);
|
|
|
|
}
|
|
|
|
}
|
2017-02-26 16:00:41 +00:00
|
|
|
} else {
|
2022-01-16 12:14:58 +00:00
|
|
|
for (int8_t ys=0; ys<2; ys++) {
|
|
|
|
for (int8_t xs=0; xs<2; xs++) {
|
|
|
|
drawUIPixel((x + x_off)*2+xs, (y+y_off)*2 + ys, offPixel);
|
|
|
|
}
|
|
|
|
}
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-09 12:14:14 +00:00
|
|
|
ch++;
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 02:16:32 +00:00
|
|
|
void SDLDisplay::drawString(uint8_t mode, uint16_t x, uint16_t y, const char *str)
|
2017-02-26 16:00:41 +00:00
|
|
|
{
|
|
|
|
int8_t xsize = 8; // width of a char in this font
|
|
|
|
|
|
|
|
for (int8_t i=0; i<strlen(str); i++) {
|
|
|
|
drawCharacter(mode, x, y, str[i]);
|
2022-01-16 12:14:58 +00:00
|
|
|
x += xsize;
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-09 12:14:14 +00:00
|
|
|
void SDLDisplay::clrScr(uint8_t coloridx)
|
2017-02-26 16:00:41 +00:00
|
|
|
{
|
2021-01-09 12:14:14 +00:00
|
|
|
const uint8_t *rgbptr = &loresPixelColors[0][0];
|
|
|
|
if (coloridx <= 16)
|
|
|
|
rgbptr = loresPixelColors[coloridx];
|
|
|
|
|
2021-01-21 01:55:11 +00:00
|
|
|
uint32_t packedColor = packColor32(loresPixelColors[coloridx]);
|
|
|
|
|
2022-01-16 12:14:58 +00:00
|
|
|
for (uint16_t y=0; y<480; y++) {
|
|
|
|
for (uint16_t x=0; x<800; x++) {
|
2021-01-21 01:55:11 +00:00
|
|
|
videoBuffer[y][x] = packedColor;
|
2021-01-19 14:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
|
2018-02-18 01:44:04 +00:00
|
|
|
void SDLDisplay::cachePixel(uint16_t x, uint16_t y, uint8_t color)
|
|
|
|
{
|
2022-01-16 12:14:58 +00:00
|
|
|
// ***
|
|
|
|
for (int yoff=0; yoff<2; yoff++) {
|
|
|
|
videoBuffer[(y*2)+SCREENINSET_Y+yoff][x+SCREENINSET_X] = packColor32(loresPixelColors[color]);
|
2021-01-19 20:37:54 +00:00
|
|
|
}
|
2018-02-18 01:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// "DoubleWide" means "please double the X because I'm in low-res width mode"
|
|
|
|
void SDLDisplay::cacheDoubleWidePixel(uint16_t x, uint16_t y, uint8_t color)
|
|
|
|
{
|
2022-01-16 12:14:58 +00:00
|
|
|
// ***
|
|
|
|
for (int yoff=0; yoff<2; yoff++) {
|
|
|
|
for (int xoff=0; xoff<2; xoff++) {
|
|
|
|
videoBuffer[(y*2)+SCREENINSET_Y+yoff][(x*2)+SCREENINSET_X+xoff] = packColor32(loresPixelColors[color]);
|
2021-01-19 14:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SDLDisplay::cacheDoubleWidePixel(uint16_t x, uint16_t y, uint32_t packedColor)
|
|
|
|
{
|
2022-01-16 12:14:58 +00:00
|
|
|
// ***
|
|
|
|
for (int yoff=0; yoff<2; yoff++) {
|
|
|
|
for (int xoff=0; xoff<2; xoff++) {
|
|
|
|
videoBuffer[(y*2)+SCREENINSET_Y+yoff][(x*2)+SCREENINSET_X+xoff] = packedColor;
|
2020-07-09 06:38:03 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-18 01:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SDLDisplay::cache2DoubleWidePixels(uint16_t x, uint16_t y, uint8_t colorB, uint8_t colorA)
|
|
|
|
{
|
2022-01-16 12:14:58 +00:00
|
|
|
// ***
|
|
|
|
for (int yoff=0; yoff<2; yoff++) {
|
|
|
|
for (int xoff=0; xoff<2; xoff++) {
|
|
|
|
videoBuffer[(y*2)+SCREENINSET_Y+yoff][(x*2)+SCREENINSET_X+xoff] = packColor32(loresPixelColors[colorA]);
|
|
|
|
videoBuffer[(y*2)+SCREENINSET_Y+yoff][(x+1)*2+SCREENINSET_X+xoff] = packColor32(loresPixelColors[colorB]);
|
2020-07-09 06:38:03 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-18 01:44:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 01:55:11 +00:00
|
|
|
void SDLDisplay::windowResized(uint32_t w, uint32_t h)
|
|
|
|
{
|
2022-01-16 12:14:58 +00:00
|
|
|
// Preserve the aspect ratio
|
2021-01-21 01:55:11 +00:00
|
|
|
float aspectRatio = (float)w/(float)h;
|
2022-01-16 12:14:58 +00:00
|
|
|
if (aspectRatio != 800.0/480.0) {
|
|
|
|
if (aspectRatio > 800.0/480.0) {
|
|
|
|
h = ((1.f * 480.0) / 800.0) * w;
|
2021-01-21 01:55:11 +00:00
|
|
|
} else {
|
2022-01-16 12:14:58 +00:00
|
|
|
w = (800.0/480.0) * h;
|
2021-01-21 01:55:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_SetWindowSize(screen, w, h);
|
|
|
|
}
|