From 98b2b4c64dc3eaa20eabc5a640e9e430d1cc3805 Mon Sep 17 00:00:00 2001 From: John Lorentzson Date: Sat, 5 Apr 2025 19:51:34 +0200 Subject: [PATCH] Make compatible with the Sun's pixel format --- src/Backends/Platform/X11.cpp | 5 ++- .../Rendering/Window/Software/X11.cpp | 42 +++++++------------ 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/src/Backends/Platform/X11.cpp b/src/Backends/Platform/X11.cpp index 8d7ed2b4..1f0cafcf 100644 --- a/src/Backends/Platform/X11.cpp +++ b/src/Backends/Platform/X11.cpp @@ -27,8 +27,9 @@ static unsigned long startTime; bool Backend_Init(void (*drag_and_drop_callback)(const char *path), void (*window_focus_callback)(bool focus)) { // we're ignoring the hell out of focus and drag & drop xDisplay = XOpenDisplay(NULL); - //XMatchVisualInfo(xDisplay, DefaultScreen(xDisplay), 24, DirectColor, &xvisinfo); - //xVisual = xvisinfo.visual; + XMatchVisualInfo(xDisplay, DefaultScreen(xDisplay), 24, TrueColor, &xvisinfo); + xVisual = xvisinfo.visual; + printf("Using X visual with ID 0x%lx\n", xVisual->visualid); struct timeval time; // time.tv_usec is microseconds gettimeofday(&time, NULL); diff --git a/src/Backends/Rendering/Window/Software/X11.cpp b/src/Backends/Rendering/Window/Software/X11.cpp index d715710b..468103dd 100644 --- a/src/Backends/Rendering/Window/Software/X11.cpp +++ b/src/Backends/Rendering/Window/Software/X11.cpp @@ -12,7 +12,7 @@ static Window window; static XImage* framebufferImage; static char* framebufferPixels; static int framebufferPitch; -static char* xfriendlyFB; +static unsigned char* xfriendlyFB; static GC gc; static int screenWidth; static int screenHeight; @@ -26,8 +26,9 @@ bool WindowBackend_Software_CreateWindow(const char *window_title, size_t screen int screen = DefaultScreen(xDisplay); Window rootWindow = RootWindow(xDisplay, screen); - //window = XCreateWindow(display, rootWindow, 0, 0, screen_width, screen_height, 0, 16, InputOutput, CopyFromParent, 0, NULL); - window = XCreateSimpleWindow(xDisplay, rootWindow, 0, 0, screen_width, screen_height, 1, 0, 0); + + window = XCreateWindow(xDisplay, rootWindow, 0, 0, screen_width, screen_height, 0, 24, InputOutput, xVisual, 0, NULL); + //window = XCreateSimpleWindow(xDisplay, rootWindow, 0, 0, screen_width, screen_height, 1, 0, 0); XSelectInput(xDisplay, window, ExposureMask | KeyPressMask | KeyReleaseMask); XMapRaised(xDisplay, window); XStoreName(xDisplay, window, window_title); @@ -45,10 +46,10 @@ bool WindowBackend_Software_CreateWindow(const char *window_title, size_t screen // Setting up the framebuffer framebufferPixels = (char*)malloc(screen_width * screen_height * 3); - xfriendlyFB = (char*)malloc(screen_width * screen_height * 4); - framebufferImage = XCreateImage(xDisplay, DefaultVisual(xDisplay, DefaultScreen(xDisplay)), + xfriendlyFB = (unsigned char*)malloc(screen_width * screen_height * 4); + framebufferImage = XCreateImage(xDisplay, xVisual,//DefaultVisual(xDisplay, DefaultScreen(xDisplay)), depth, ZPixmap, 0, - xfriendlyFB, screen_width, screen_height, + (char*)xfriendlyFB, screen_width, screen_height, 32, 0); if(framebufferImage == 0) { return false; @@ -76,28 +77,17 @@ void WindowBackend_Software_HandleWindowResize(size_t width, size_t height) { } void WindowBackend_Software_Display(void) { + // Convert Cave Story's framebuffer format to the Sun's + // This is the exact reverse of how it is on modern + // computers. Yup, it's endianess. int srci = 0; - for(int i = 0; i < screenWidth * screenHeight * 4; i++) { + for(int i = 0; i < screenWidth * screenHeight * 4; i += 4) { int src; - switch(i % 4) { - case 0: - src = srci + 2; - break; - case 1: - src = srci + 1; - break; - case 2: - src = srci + 0; - break; - case 3: - srci += 3; - } - xfriendlyFB[i] = framebufferPixels[src]; - // if(i % 4 != 3) { - // xfriendlyFB[i] = framebufferPixels[srci]; - // srci++; - // } + xfriendlyFB[i + 0] = 0; // UNUSED + xfriendlyFB[i + 1] = framebufferPixels[srci + 2]; // BLUE + xfriendlyFB[i + 2] = framebufferPixels[srci + 1]; // GREEN + xfriendlyFB[i + 3] = framebufferPixels[srci + 0]; // RED + srci += 3; } XPutImage(xDisplay, window, gc, framebufferImage, 0, 0, 0, 0, screenWidth, screenHeight); - //XCopyArea(xDisplay, intermediate, window, gc, 0, 0, screenWidth, screenHeight, 0, 0); }