Use OpenGL renderer (#72)

This commit is contained in:
Alexander Batalov 2022-07-12 10:10:23 +03:00 committed by GitHub
parent c562d83020
commit bbfd7785b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 91 additions and 26 deletions

View File

@ -358,8 +358,10 @@ int gKeyboardLayout;
unsigned char gPressedPhysicalKeysCount; unsigned char gPressedPhysicalKeysCount;
SDL_Window* gSdlWindow = NULL; SDL_Window* gSdlWindow = NULL;
SDL_Surface* gSdlWindowSurface = NULL;
SDL_Surface* gSdlSurface = NULL; SDL_Surface* gSdlSurface = NULL;
SDL_Renderer* gSdlRenderer = NULL;
SDL_Texture* gSdlTexture = NULL;
SDL_Surface* gSdlTextureSurface = NULL;
// 0x4C8A70 // 0x4C8A70
int coreInit(int a1) int coreInit(int a1)
@ -1270,7 +1272,6 @@ void _GNW95_process_message()
case SDL_WINDOWEVENT_SIZE_CHANGED: case SDL_WINDOWEVENT_SIZE_CHANGED:
// TODO: Recreate gSdlSurface in case size really changed (i.e. // TODO: Recreate gSdlSurface in case size really changed (i.e.
// not alt-tabbing in fullscreen mode). // not alt-tabbing in fullscreen mode).
gSdlWindowSurface = SDL_GetWindowSurface(gSdlWindow);
break; break;
case SDL_WINDOWEVENT_FOCUS_GAINED: case SDL_WINDOWEVENT_FOCUS_GAINED:
gProgramIsActive = true; gProgramIsActive = true;
@ -2014,20 +2015,45 @@ int _init_vesa_mode(int width, int height)
int _GNW95_init_window(int width, int height, bool fullscreen) int _GNW95_init_window(int width, int height, bool fullscreen)
{ {
if (gSdlWindow == NULL) { if (gSdlWindow == NULL) {
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
if (SDL_Init(SDL_INIT_VIDEO) != 0) { if (SDL_Init(SDL_INIT_VIDEO) != 0) {
return -1; return -1;
} }
gSdlWindow = SDL_CreateWindow(gProgramWindowTitle, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, fullscreen ? SDL_WINDOW_FULLSCREEN : 0); Uint32 windowFlags = SDL_WINDOW_OPENGL;
if (fullscreen) {
windowFlags |= SDL_WINDOW_FULLSCREEN;
}
gSdlWindow = SDL_CreateWindow(gProgramWindowTitle, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, windowFlags);
if (gSdlWindow == NULL) { if (gSdlWindow == NULL) {
return -1; return -1;
} }
gSdlWindowSurface = SDL_GetWindowSurface(gSdlWindow); gSdlRenderer = SDL_CreateRenderer(gSdlWindow, -1, 0);
if (gSdlWindowSurface == NULL) { if (gSdlRenderer == NULL) {
SDL_DestroyWindow(gSdlWindow); goto err;
gSdlWindow = NULL; }
return -1;
if (SDL_RenderSetLogicalSize(gSdlRenderer, width, height) != 0) {
goto err;
}
gSdlTexture = SDL_CreateTexture(gSdlRenderer, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_STREAMING, width, height);
if (gSdlTexture == NULL) {
goto err;
}
Uint32 format;
if (SDL_QueryTexture(gSdlTexture, &format, NULL, NULL, NULL) != 0) {
goto err;
}
gSdlTextureSurface = SDL_CreateRGBSurfaceWithFormat(0, width, height, SDL_BITSPERPIXEL(format), format);
if (gSdlTextureSurface == NULL) {
goto err;
} }
#if _WIN32 #if _WIN32
@ -2035,9 +2061,7 @@ int _GNW95_init_window(int width, int height, bool fullscreen)
SDL_VERSION(&info.version); SDL_VERSION(&info.version);
if (!SDL_GetWindowWMInfo(gSdlWindow, &info)) { if (!SDL_GetWindowWMInfo(gSdlWindow, &info)) {
SDL_DestroyWindow(gSdlWindow); goto err;
gSdlWindow = NULL;
return -1;
} }
// Needed for DirectSound. // Needed for DirectSound.
@ -2046,6 +2070,24 @@ int _GNW95_init_window(int width, int height, bool fullscreen)
} }
return 0; return 0;
err:
if (gSdlTexture != NULL) {
SDL_DestroyTexture(gSdlTexture);
gSdlTexture = NULL;
}
if (gSdlRenderer != NULL) {
SDL_DestroyRenderer(gSdlRenderer);
gSdlRenderer = NULL;
}
if (gSdlWindow != NULL) {
SDL_DestroyWindow(gSdlWindow);
gSdlWindow = NULL;
}
return -1;
} }
// calculate shift for mask // calculate shift for mask
@ -2147,8 +2189,11 @@ void directDrawSetPaletteInRange(unsigned char* palette, int start, int count)
} }
SDL_SetPaletteColors(gSdlSurface->format->palette, colors, start, count); SDL_SetPaletteColors(gSdlSurface->format->palette, colors, start, count);
SDL_BlitSurface(gSdlSurface, NULL, gSdlWindowSurface, NULL); SDL_BlitSurface(gSdlSurface, NULL, gSdlTextureSurface, NULL);
SDL_UpdateWindowSurface(gSdlWindow); SDL_UpdateTexture(gSdlTexture, NULL, gSdlTextureSurface->pixels, gSdlTextureSurface->pitch);
SDL_RenderClear(gSdlRenderer);
SDL_RenderCopy(gSdlRenderer, gSdlTexture, NULL, NULL);
SDL_RenderPresent(gSdlRenderer);
} else { } else {
for (int index = start; index < start + count; index++) { for (int index = start; index < start + count; index++) {
unsigned short r = palette[0] << 2; unsigned short r = palette[0] << 2;
@ -2191,8 +2236,11 @@ void directDrawSetPalette(unsigned char* palette)
} }
SDL_SetPaletteColors(gSdlSurface->format->palette, colors, 0, 256); SDL_SetPaletteColors(gSdlSurface->format->palette, colors, 0, 256);
SDL_BlitSurface(gSdlSurface, NULL, gSdlWindowSurface, NULL); SDL_BlitSurface(gSdlSurface, NULL, gSdlTextureSurface, NULL);
SDL_UpdateWindowSurface(gSdlWindow); SDL_UpdateTexture(gSdlTexture, NULL, gSdlTextureSurface->pixels, gSdlTextureSurface->pitch);
SDL_RenderClear(gSdlRenderer);
SDL_RenderCopy(gSdlRenderer, gSdlTexture, NULL, NULL);
SDL_RenderPresent(gSdlRenderer);
} else { } else {
for (int index = 0; index < 256; index++) { for (int index = 0; index < 256; index++) {
unsigned short r = palette[index * 3] << 2; unsigned short r = palette[index * 3] << 2;
@ -2272,8 +2320,11 @@ void _GNW95_ShowRect(unsigned char* src, int srcPitch, int a3, int srcX, int src
SDL_Rect destRect; SDL_Rect destRect;
destRect.x = destX; destRect.x = destX;
destRect.y = destY; destRect.y = destY;
SDL_BlitSurface(gSdlSurface, &srcRect, gSdlWindowSurface, &destRect); SDL_BlitSurface(gSdlSurface, &srcRect, gSdlTextureSurface, &destRect);
SDL_UpdateWindowSurface(gSdlWindow); SDL_UpdateTexture(gSdlTexture, NULL, gSdlTextureSurface->pixels, gSdlTextureSurface->pitch);
SDL_RenderClear(gSdlRenderer);
SDL_RenderCopy(gSdlRenderer, gSdlTexture, NULL, NULL);
SDL_RenderPresent(gSdlRenderer);
} }
// 0x4CB93C // 0x4CB93C
@ -2312,8 +2363,11 @@ void _GNW95_MouseShowRect16(unsigned char* src, int srcPitch, int a3, int srcX,
SDL_Rect destRect; SDL_Rect destRect;
destRect.x = destX; destRect.x = destX;
destRect.y = destY; destRect.y = destY;
SDL_BlitSurface(gSdlSurface, &srcRect, gSdlWindowSurface, &destRect); SDL_BlitSurface(gSdlSurface, &srcRect, gSdlTextureSurface, &destRect);
SDL_UpdateWindowSurface(gSdlWindow); SDL_UpdateTexture(gSdlTexture, NULL, gSdlTextureSurface->pixels, gSdlTextureSurface->pitch);
SDL_RenderClear(gSdlRenderer);
SDL_RenderCopy(gSdlRenderer, gSdlTexture, NULL, NULL);
SDL_RenderPresent(gSdlRenderer);
} }
// 0x4CBA44 // 0x4CBA44
@ -2360,8 +2414,11 @@ void _GNW95_MouseShowTransRect16(unsigned char* src, int srcPitch, int a3, int s
SDL_Rect destRect; SDL_Rect destRect;
destRect.x = destX; destRect.x = destX;
destRect.y = destY; destRect.y = destY;
SDL_BlitSurface(gSdlSurface, &srcRect, gSdlWindowSurface, &destRect); SDL_BlitSurface(gSdlSurface, &srcRect, gSdlTextureSurface, &destRect);
SDL_UpdateWindowSurface(gSdlWindow); SDL_UpdateTexture(gSdlTexture, NULL, gSdlTextureSurface->pixels, gSdlTextureSurface->pitch);
SDL_RenderClear(gSdlRenderer);
SDL_RenderCopy(gSdlRenderer, gSdlTexture, NULL, NULL);
SDL_RenderPresent(gSdlRenderer);
} }
// Clears drawing surface. // Clears drawing surface.
@ -2383,8 +2440,11 @@ void _GNW95_zero_vid_mem()
SDL_UnlockSurface(gSdlSurface); SDL_UnlockSurface(gSdlSurface);
SDL_BlitSurface(gSdlSurface, NULL, gSdlWindowSurface, NULL); SDL_BlitSurface(gSdlSurface, NULL, gSdlTextureSurface, NULL);
SDL_UpdateWindowSurface(gSdlWindow); SDL_UpdateTexture(gSdlTexture, NULL, gSdlTextureSurface->pixels, gSdlTextureSurface->pitch);
SDL_RenderClear(gSdlRenderer);
SDL_RenderCopy(gSdlRenderer, gSdlTexture, NULL, NULL);
SDL_RenderPresent(gSdlRenderer);
} }
// 0x4CBC90 // 0x4CBC90

View File

@ -522,8 +522,10 @@ extern int gKeyboardLayout;
extern unsigned char gPressedPhysicalKeysCount; extern unsigned char gPressedPhysicalKeysCount;
extern SDL_Window* gSdlWindow; extern SDL_Window* gSdlWindow;
extern SDL_Surface* gSdlWindowSurface;
extern SDL_Surface* gSdlSurface; extern SDL_Surface* gSdlSurface;
extern SDL_Renderer* gSdlRenderer;
extern SDL_Texture* gSdlTexture;
extern SDL_Surface* gSdlTextureSurface;
int coreInit(int a1); int coreInit(int a1);
void coreExit(); void coreExit();

View File

@ -262,8 +262,11 @@ static void movieDirectImpl(SDL_Surface* surface, int srcWidth, int srcHeight, i
// backbuffer surface (with palette set), all we get is shiny white box. // backbuffer surface (with palette set), all we get is shiny white box.
SDL_SetSurfacePalette(surface, gSdlSurface->format->palette); SDL_SetSurfacePalette(surface, gSdlSurface->format->palette);
SDL_BlitSurface(surface, &srcRect, gSdlSurface, &destRect); SDL_BlitSurface(surface, &srcRect, gSdlSurface, &destRect);
SDL_BlitSurface(gSdlSurface, NULL, gSdlWindowSurface, NULL); SDL_BlitSurface(gSdlSurface, NULL, gSdlTextureSurface, NULL);
SDL_UpdateWindowSurface(gSdlWindow); SDL_UpdateTexture(gSdlTexture, NULL, gSdlTextureSurface->pixels, gSdlTextureSurface->pitch);
SDL_RenderClear(gSdlRenderer);
SDL_RenderCopy(gSdlRenderer, gSdlTexture, NULL, NULL);
SDL_RenderPresent(gSdlRenderer);
} }
// 0x486900 // 0x486900