Rewrote video rendering code to leverage SDL

Right now the program starts up and immediately exits. That naturally
needs to be fixed. I'll do that soon.
This commit is contained in:
InvisibleUp 2020-07-01 23:13:45 -04:00
parent fa4bbacf3a
commit f485b2ef4c
7 changed files with 86 additions and 1683 deletions

View File

@ -495,33 +495,6 @@ GLOBALPROC ScreenChangedAll(void)
ScreenChangedRight = vMacScreenWidth;
}
GLOBALOSGLUPROC Screen_OutputFrame(uint8_t * screencurrentbuff)
{
int16_t top;
int16_t left;
int16_t bottom;
int16_t right;
if (! EmVideoDisable) {
if (ScreenFindChanges(screencurrentbuff, EmLagTime,
&top, &left, &bottom, &right))
{
if (top < ScreenChangedTop) {
ScreenChangedTop = top;
}
if (bottom > ScreenChangedBottom) {
ScreenChangedBottom = bottom;
}
if (left < ScreenChangedLeft) {
ScreenChangedLeft = left;
}
if (right > ScreenChangedRight) {
ScreenChangedRight = right;
}
}
}
}
#if MayFullScreen
GLOBALVAR uint16_t ViewHSize;
GLOBALVAR uint16_t ViewVSize;

View File

@ -5,7 +5,7 @@
*
*/
#include <SDL2.h> // everything else is deprecated now
#include <SDL.h> // everything else is deprecated now
#include <stdlib.h>
#include <stdint.h>
#include "CONFIGM.h"
@ -18,5 +18,6 @@ void ConfigMode_Tick()
// Get the screen context and just draw something there for now
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDrawRect(renderer, {.x = 16, .y = 16, .w = 128, .h = 128});
const SDL_Rect rect = {.x = 16, .y = 16, .w = 128, .h = 128};
SDL_RenderDrawRect(renderer, &rect);
}

File diff suppressed because it is too large Load Diff

View File

@ -28,43 +28,8 @@ extern bool RunInBackground;
extern bool SpeedStopped;
extern bool WantFullScreen;
/* Macros and such */
enum {
SpclModeNoRom,
SpclModeMessage,
SpclModeControl,
kNumSpclModes
};
#define SpecialModeSet(i) SpecialModes |= (1 << (i))
#define SpecialModeClr(i) SpecialModes &= ~ (1 << (i))
#define SpecialModeTst(i) (0 != (SpecialModes & (1 << (i))))
#define MacMsgDisplayed SpecialModeTst(SpclModeMessage)
/* Message display */
void MacMsgOverride(char *briefMsg, char *longMsg);
void MacMsgDisplayOff(void);
void MacMsgDisplayOn(void);
#if NeedDoMoreCommandsMsg
void DoMoreCommandsMsg(void);
#endif
#if NeedDoAboutMsg
void DoAboutMsg(void);
#endif
/* Screen drawing */
uint8_t * GetCurDrawBuff(void);
/* Things that really don't belong here */
void Keyboard_UpdateKeyMap2(uint8_t key, bool down);
void DisconnectKeyCodes2(void);
/* Public Functions */
void MacMsgOverride(char *title, char *msg);
MacErr_t ROM_IsValid(void);
bool WaitForRom(void);

View File

@ -163,7 +163,8 @@ void DoKeyCode(SDL_Keysym *r, bool down)
{
uint8_t v = SDLScan2MacKeyCode(r->scancode);
if (MKC_None != v) {
Keyboard_UpdateKeyMap2(v, down);
// this probably breaks keyboard support
//Keyboard_UpdateKeyMap2(v, down);
}
}
@ -181,6 +182,6 @@ void ReconnectKeyCodes3(void)
void DisconnectKeyCodes3(void)
{
DisconnectKeyCodes2();
//DisconnectKeyCodes2();
MouseButtonSet(false);
}

View File

@ -149,7 +149,7 @@ LOCALPROC HandleTheEvent(SDL_Event *event)
case SDL_KEYUP:
DoKeyCode(&event->key.keysym, false);
break;
case SDL_MOUSEWHEEL:
/*case SDL_MOUSEWHEEL:
if (event->wheel.x < 0) {
Keyboard_UpdateKeyMap2(MKC_Left, true);
Keyboard_UpdateKeyMap2(MKC_Left, false);
@ -164,7 +164,7 @@ LOCALPROC HandleTheEvent(SDL_Event *event)
Keyboard_UpdateKeyMap2(MKC_Up, true);
Keyboard_UpdateKeyMap2(MKC_Up, false);
}
break;
break;*/
case SDL_DROPFILE:
{
char *s = event->drop.file;
@ -839,20 +839,12 @@ LOCALPROC CheckForSavedTasks(void)
}
}
if (CurSpeedStopped != (SpeedStopped ||
(gBackgroundFlag && ! RunInBackground)))
{
CurSpeedStopped = ! CurSpeedStopped;
if (CurSpeedStopped) {
EnterSpeedStopped();
// TODO: fix this
/*if (CurSpeedStopped != (SpeedStopped ||
(gBackgroundFlag && ! RunInBackground))){
} else {
LeaveSpeedStopped();
}
}
if ((nullpr != SavedBriefMsg) & ! MacMsgDisplayed) {
MacMsgDisplayOn();
}
}*/
#if EnableRecreateW
if (0
@ -1095,10 +1087,6 @@ LOCALFUNC bool InitOSGLU(void)
LOCALPROC UnInitOSGLU(void)
{
if (MacMsgDisplayed) {
MacMsgDisplayOff();
}
RestoreKeyRepeat();
#if MayFullScreen
UngrabMachine();

View File

@ -2,6 +2,7 @@
#include <string.h>
#include <stdio.h>
#include <SDL.h>
#include <SDL_pixels.h>
#include "CNFGRAPI.h"
#include "SYSDEPNS.h"
#include "UTIL/ENDIANAC.h"
@ -29,524 +30,60 @@ SDL_PixelFormat *format = NULL;
uint8_t * ScalingBuff = nullpr;
uint8_t * CLUT_final;
/*
256 possible values of one byte
8 pixels per byte maximum (when black and white)
4 bytes per destination pixel maximum
multiplied by WindowScale if 1
*/
#define ScrnMapr_DoMap UpdateBWDepth3Copy
#define ScrnMapr_Src GetCurDrawBuff()
#define ScrnMapr_Dst ScalingBuff
#define ScrnMapr_SrcDepth 0
#define ScrnMapr_DstDepth 3
#define ScrnMapr_Map CLUT_final
#include "HW/SCREEN/SCRNMAPR.h"
#define ScrnMapr_DoMap UpdateBWDepth4Copy
#define ScrnMapr_Src GetCurDrawBuff()
#define ScrnMapr_Dst ScalingBuff
#define ScrnMapr_SrcDepth 0
#define ScrnMapr_DstDepth 4
#define ScrnMapr_Map CLUT_final
#include "HW/SCREEN/SCRNMAPR.h"
#define ScrnMapr_DoMap UpdateBWDepth5Copy
#define ScrnMapr_Src GetCurDrawBuff()
#define ScrnMapr_Dst ScalingBuff
#define ScrnMapr_SrcDepth 0
#define ScrnMapr_DstDepth 5
#define ScrnMapr_Map CLUT_final
#include "HW/SCREEN/SCRNMAPR.h"
#if 1 && ! UseSDLscaling
#define ScrnMapr_DoMap UpdateBWDepth3ScaledCopy
#define ScrnMapr_Src GetCurDrawBuff()
#define ScrnMapr_Dst ScalingBuff
#define ScrnMapr_SrcDepth 0
#define ScrnMapr_DstDepth 3
#define ScrnMapr_Map CLUT_final
#define ScrnMapr_Scale WindowScale
#include "HW/SCREEN/SCRNMAPR.h"
#define ScrnMapr_DoMap UpdateBWDepth4ScaledCopy
#define ScrnMapr_Src GetCurDrawBuff()
#define ScrnMapr_Dst ScalingBuff
#define ScrnMapr_SrcDepth 0
#define ScrnMapr_DstDepth 4
#define ScrnMapr_Map CLUT_final
#define ScrnMapr_Scale WindowScale
#include "HW/SCREEN/SCRNMAPR.h"
#define ScrnMapr_DoMap UpdateBWDepth5ScaledCopy
#define ScrnMapr_Src GetCurDrawBuff()
#define ScrnMapr_Dst ScalingBuff
#define ScrnMapr_SrcDepth 0
#define ScrnMapr_DstDepth 5
#define ScrnMapr_Map CLUT_final
#define ScrnMapr_Scale WindowScale
#include "HW/SCREEN/SCRNMAPR.h"
#endif /* 1 && ! UseSDLscaling */
#if (0 != vMacScreenDepth) && (vMacScreenDepth < 4)
#define ScrnMapr_DoMap UpdateColorDepth3Copy
#define ScrnMapr_Src GetCurDrawBuff()
#define ScrnMapr_Dst ScalingBuff
#define ScrnMapr_SrcDepth vMacScreenDepth
#define ScrnMapr_DstDepth 3
#define ScrnMapr_Map CLUT_final
#include "HW/SCREEN/SCRNMAPR.h"
#define ScrnMapr_DoMap UpdateColorDepth4Copy
#define ScrnMapr_Src GetCurDrawBuff()
#define ScrnMapr_Dst ScalingBuff
#define ScrnMapr_SrcDepth vMacScreenDepth
#define ScrnMapr_DstDepth 4
#define ScrnMapr_Map CLUT_final
#include "HW/SCREEN/SCRNMAPR.h"
#define ScrnMapr_DoMap UpdateColorDepth5Copy
#define ScrnMapr_Src GetCurDrawBuff()
#define ScrnMapr_Dst ScalingBuff
#define ScrnMapr_SrcDepth vMacScreenDepth
#define ScrnMapr_DstDepth 5
#define ScrnMapr_Map CLUT_final
#include "HW/SCREEN/SCRNMAPR.h"
#if 1 && ! UseSDLscaling
#define ScrnMapr_DoMap UpdateColorDepth3ScaledCopy
#define ScrnMapr_Src GetCurDrawBuff()
#define ScrnMapr_Dst ScalingBuff
#define ScrnMapr_SrcDepth vMacScreenDepth
#define ScrnMapr_DstDepth 3
#define ScrnMapr_Map CLUT_final
#define ScrnMapr_Scale WindowScale
#include "HW/SCREEN/SCRNMAPR.h"
#define ScrnMapr_DoMap UpdateColorDepth4ScaledCopy
#define ScrnMapr_Src GetCurDrawBuff()
#define ScrnMapr_Dst ScalingBuff
#define ScrnMapr_SrcDepth vMacScreenDepth
#define ScrnMapr_DstDepth 4
#define ScrnMapr_Map CLUT_final
#define ScrnMapr_Scale WindowScale
#include "HW/SCREEN/SCRNMAPR.h"
#define ScrnMapr_DoMap UpdateColorDepth5ScaledCopy
#define ScrnMapr_Src GetCurDrawBuff()
#define ScrnMapr_Dst ScalingBuff
#define ScrnMapr_SrcDepth vMacScreenDepth
#define ScrnMapr_DstDepth 5
#define ScrnMapr_Map CLUT_final
#define ScrnMapr_Scale WindowScale
#include "HW/SCREEN/SCRNMAPR.h"
#endif /* 1 && ! UseSDLscaling */
#endif
LOCALPROC HaveChangedScreenBuff(uint16_t top, uint16_t left,
uint16_t bottom, uint16_t right)
// Set the display palette from the Macintosh's memory or w/e
static int SetPalette(SDL_Palette *palette, const SDL_Color *macColors, int ncolors)
{
int i;
int j;
uint8_t *p;
Uint32 pixel;
#if (0 != vMacScreenDepth) && (vMacScreenDepth < 4)
Uint32 CLUT_pixel[CLUT_size];
#endif
Uint32 BWLUT_pixel[2];
uint32_t top2;
uint32_t left2;
uint32_t bottom2;
uint32_t right2;
void *pixels;
int pitch;
SDL_Rect src_rect;
SDL_Rect dst_rect;
int XDest;
int YDest;
int DestWidth;
int DestHeight;
#if 1
if (UseFullScreen)
#endif
#if MayFullScreen
{
if (top < ViewVStart) {
top = ViewVStart;
}
if (left < ViewHStart) {
left = ViewHStart;
}
if (bottom > ViewVStart + ViewVSize) {
bottom = ViewVStart + ViewVSize;
}
if (right > ViewHStart + ViewHSize) {
right = ViewHStart + ViewHSize;
}
if ((top >= bottom) || (left >= right)) {
goto label_exit;
}
}
#endif
XDest = left;
YDest = top;
DestWidth = (right - left);
DestHeight = (bottom - top);
#if 1
if (UseFullScreen)
#endif
#if MayFullScreen
{
XDest -= ViewHStart;
YDest -= ViewVStart;
}
#endif
#if 1
if (UseMagnify) {
XDest *= WindowScale;
YDest *= WindowScale;
DestWidth *= WindowScale;
DestHeight *= WindowScale;
}
#endif
#if 1
if (UseFullScreen)
#endif
#if MayFullScreen
{
XDest += hOffset;
YDest += vOffset;
}
#endif
top2 = top;
left2 = left;
bottom2 = bottom;
right2 = right;
#if 1 && ! UseSDLscaling
if (UseMagnify) {
top2 *= WindowScale;
left2 *= WindowScale;
bottom2 *= WindowScale;
right2 *= WindowScale;
}
#endif
if (0 != SDL_LockTexture(texture, NULL, &pixels, &pitch)) {
return;
}
{
int bpp = format->BytesPerPixel;
uint32_t ExpectedPitch = vMacScreenWidth * bpp;
#if 1 && ! UseSDLscaling
if (UseMagnify) {
ExpectedPitch *= WindowScale;
}
#endif
#if 0 != vMacScreenDepth
if (UseColorMode) {
#if vMacScreenDepth < 4
for (i = 0; i < CLUT_size; ++i) {
CLUT_pixel[i] = SDL_MapRGB(format,
CLUT_reds[i] >> 8,
CLUT_greens[i] >> 8,
CLUT_blues[i] >> 8);
}
#endif
} else
#endif
{
BWLUT_pixel[1] = SDL_MapRGB(format, 0, 0, 0);
/* black */
BWLUT_pixel[0] = SDL_MapRGB(format, 255, 255, 255);
/* white */
}
if ((0 == ((bpp - 1) & bpp)) /* a power of 2 */
&& (pitch == ExpectedPitch)
#if (vMacScreenDepth > 3)
&& ! UseColorMode
#endif
)
{
int k;
Uint32 v;
#if 1 && ! UseSDLscaling
int a;
#endif
int PixPerByte =
#if (0 != vMacScreenDepth) && (vMacScreenDepth < 4)
UseColorMode ? (1 << (3 - vMacScreenDepth)) :
#endif
8;
Uint8 *p4 = (Uint8 *)CLUT_final;
for (i = 0; i < 256; ++i) {
for (k = PixPerByte; --k >= 0; ) {
#if (0 != vMacScreenDepth) && (vMacScreenDepth < 4)
if (UseColorMode) {
v = CLUT_pixel[
#if 3 == vMacScreenDepth
i
#else
(i >> (k << vMacScreenDepth))
& (CLUT_size - 1)
#endif
];
} else
#endif
{
v = BWLUT_pixel[(i >> k) & 1];
}
#if 1 && ! UseSDLscaling
for (a = UseMagnify ? WindowScale : 1; --a >= 0; )
#endif
{
switch (bpp) {
case 1: /* Assuming 8-bpp */
*p4++ = v;
break;
case 2: /* Probably 15-bpp or 16-bpp */
*(Uint16 *)p4 = v;
p4 += 2;
break;
case 4: /* Probably 32-bpp */
*(Uint32 *)p4 = v;
p4 += 4;
break;
}
}
}
}
ScalingBuff = (uint8_t *)pixels;
#if (0 != vMacScreenDepth) && (vMacScreenDepth < 4)
if (UseColorMode) {
#if 1 && ! UseSDLscaling
if (UseMagnify) {
switch (bpp) {
case 1:
UpdateColorDepth3ScaledCopy(
top, left, bottom, right);
break;
case 2:
UpdateColorDepth4ScaledCopy(
top, left, bottom, right);
break;
case 4:
UpdateColorDepth5ScaledCopy(
top, left, bottom, right);
break;
}
} else
#endif
{
switch (bpp) {
case 1:
UpdateColorDepth3Copy(top, left, bottom, right);
break;
case 2:
UpdateColorDepth4Copy(top, left, bottom, right);
break;
case 4:
UpdateColorDepth5Copy(top, left, bottom, right);
break;
}
}
} else
#endif
{
#if 1 && ! UseSDLscaling
if (UseMagnify) {
switch (bpp) {
case 1:
UpdateBWDepth3ScaledCopy(
top, left, bottom, right);
break;
case 2:
UpdateBWDepth4ScaledCopy(
top, left, bottom, right);
break;
case 4:
UpdateBWDepth5ScaledCopy(
top, left, bottom, right);
break;
}
} else
#endif
{
switch (bpp) {
case 1:
UpdateBWDepth3Copy(top, left, bottom, right);
break;
case 2:
UpdateBWDepth4Copy(top, left, bottom, right);
break;
case 4:
UpdateBWDepth5Copy(top, left, bottom, right);
break;
}
}
}
} else {
uint8_t *the_data = (uint8_t *)GetCurDrawBuff();
/* adapted from putpixel in SDL documentation */
for (i = top2; i < bottom2; ++i) {
for (j = left2; j < right2; ++j) {
int i0 = i;
int j0 = j;
Uint8 *bufp = (Uint8 *)pixels
+ i * pitch + j * bpp;
#if 1 && ! UseSDLscaling
if (UseMagnify) {
i0 /= WindowScale;
j0 /= WindowScale;
}
#endif
#if 0 != vMacScreenDepth
if (UseColorMode) {
#if vMacScreenDepth < 4
p = the_data + ((i0 * vMacScreenWidth + j0)
>> (3 - vMacScreenDepth));
{
uint8_t k = (*p >> (((~ j0)
& ((1 << (3 - vMacScreenDepth)) - 1))
<< vMacScreenDepth))
& (CLUT_size - 1);
pixel = CLUT_pixel[k];
}
#elif 4 == vMacScreenDepth
p = the_data + ((i0 * vMacScreenWidth + j0) << 1);
{
uint16_t t0 = do_get_mem_word(p);
pixel = SDL_MapRGB(format,
((t0 & 0x7C00) >> 7)
| ((t0 & 0x7000) >> 12),
((t0 & 0x03E0) >> 2)
| ((t0 & 0x0380) >> 7),
((t0 & 0x001F) << 3)
| ((t0 & 0x001C) >> 2));
}
#elif 5 == vMacScreenDepth
p = the_data + ((i0 * vMacScreenWidth + j0) << 2);
pixel = SDL_MapRGB(format,
p[1],
p[2],
p[3]);
#endif
} else
#endif
{
p = the_data + ((i0 * vMacScreenWidth + j0) / 8);
pixel = BWLUT_pixel[(*p >> ((~ j0) & 0x7)) & 1];
}
switch (bpp) {
case 1: /* Assuming 8-bpp */
*bufp = pixel;
break;
case 2: /* Probably 15-bpp or 16-bpp */
*(Uint16 *)bufp = pixel;
break;
case 3:
/* Slow 24-bpp mode, usually not used */
if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
bufp[0] = (pixel >> 16) & 0xff;
bufp[1] = (pixel >> 8) & 0xff;
bufp[2] = pixel & 0xff;
} else {
bufp[0] = pixel & 0xff;
bufp[1] = (pixel >> 8) & 0xff;
bufp[2] = (pixel >> 16) & 0xff;
}
break;
case 4: /* Probably 32-bpp */
*(Uint32 *)bufp = pixel;
break;
}
}
}
}
}
SDL_UnlockTexture(texture);
src_rect.x = left2;
src_rect.y = top2;
src_rect.w = right2 - left2;
src_rect.h = bottom2 - top2;
dst_rect.x = XDest;
dst_rect.y = YDest;
dst_rect.w = DestWidth;
dst_rect.h = DestHeight;
/* SDL_RenderClear(renderer); */
SDL_RenderCopy(renderer, texture, &src_rect, &dst_rect);
SDL_RenderPresent(renderer);
#if MayFullScreen
label_exit:
;
#endif
return SDL_SetPaletteColors(palette, macColors, 0, ncolors);
}
LOCALPROC DrawChangesAndClear(void)
// Get pixel format for a given screen depth
// Note: this is complete and total guesswork right now. Lol.
uint32_t GetPixFormatFromDepth(int depth)
{
if (ScreenChangedBottom > ScreenChangedTop) {
HaveChangedScreenBuff(ScreenChangedTop, ScreenChangedLeft,
ScreenChangedBottom, ScreenChangedRight);
ScreenClearChanges();
switch(depth)
{
case 1:
return SDL_PIXELFORMAT_INDEX1LSB;
case 4:
return SDL_PIXELFORMAT_INDEX4LSB;
case 8:
return SDL_PIXELFORMAT_INDEX8;
case 16:
return SDL_PIXELFORMAT_RGB332;
case 24:
return SDL_PIXELFORMAT_RGB24;
case 32:
return SDL_PIXELFORMAT_RGBA8888;
default:
return SDL_PIXELFORMAT_UNKNOWN;
}
}
// We aren't being smart about *what* to update, since if we use SDL2 properly
// the update operating is stunningly cheap.
GLOBALOSGLUPROC Screen_OutputFrame(uint8_t * src_ptr)
{
if (EmVideoDisable) { return; }
uint32_t src_format = GetPixFormatFromDepth(vMacScreenDepth);
SDL_Surface *src = SDL_CreateRGBSurfaceWithFormatFrom(
src_ptr,
vMacScreenWidth,
vMacScreenHeight,
vMacScreenDepth,
vMacScreenByteWidth,
src_format
);
SDL_LockTexture(texture, NULL, NULL, NULL);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_UnlockTexture(texture);
SDL_RenderPresent(renderer);
}
GLOBALOSGLUPROC DoneWithDrawingForTick(void)
{
#if EnableFSMouseMotion
if (HaveMouseMotion) {
AutoScrollScreen();
}
#endif
DrawChangesAndClear();
return;
}