Skip to content

Commit

Permalink
sdl: add blitting to avoid event handling issues
Browse files Browse the repository at this point in the history
We encountered some problems related to updating screens and handling events.
To solve these problems we started with a generic implementation, now, we 
always design on a virtual surface and use `SDL_BlitSurface` to overlay the 
previous one.

Author: @acmlira and @flsobral
  • Loading branch information
acmlira committed Oct 29, 2020
1 parent 9f4ab7b commit 026921b
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions TotalCrossVM/src/init/tcsdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ bool TCSDL_Init(ScreenSurface screen, const char* title, bool fullScreen) {
std::cerr << "SDL_CreateTexturet(): " << SDL_GetError() << '\n';
return false;
}
} else {
surface = SDL_GetWindowSurface(window);
}

// Get pixel format struct
Expand All @@ -161,14 +159,9 @@ bool TCSDL_Init(ScreenSurface screen, const char* title, bool fullScreen) {
// pixel order
screen->pixelformat = windowPixelFormat;

if (usesTexture) {
// Adjusts screen's pixel surface
if (IS_NULL(screen->pixels = (uint8*) malloc(screen->pitch * screen->screenH))) {
std::cerr << "Failed to alloc " << (screen->pitch * screen->screenH) << " bytes for pixel surface" << '\n';
return false;
}
}else {
screen->pixels = (uint8*) surface->pixels;
if (IS_NULL(screen->pixels = (uint8*) malloc(screen->pitch * screen->screenH))) {
std::cerr << "Failed to alloc " << (screen->pitch * screen->screenH) << " bytes for pixel surface" << '\n';
return false;
}

if (IS_NULL(screen->extension = (ScreenSurfaceEx) malloc(sizeof(TScreenSurfaceEx)))) {
Expand All @@ -178,6 +171,17 @@ bool TCSDL_Init(ScreenSurface screen, const char* title, bool fullScreen) {
std::cerr << "Failed to alloc TScreenSurfaceEx of " << sizeof(TScreenSurfaceEx) << " bytes" << '\n';
return false;
}

if(!usesTexture) {
surface = SDL_CreateRGBSurfaceWithFormatFrom(
screen->pixels,
screen->screenW,
screen->screenH,
screen->bpp,
screen->pitch,
windowPixelFormat);
}

SCREEN_EX(screen)->window = window;
SCREEN_EX(screen)->renderer = renderer;
SCREEN_EX(screen)->texture = texture;
Expand All @@ -200,6 +204,9 @@ void TCSDL_UpdateTexture(int w, int h, int pitch, void* pixels) {
if(usesTexture) {
// Update the given texture rectangle with new pixel data.
SDL_UpdateTexture(texture, NULL, pixels, pitch);
} else {
// Copy virtual surface to window surface
SDL_BlitSurface(surface, NULL, SDL_GetWindowSurface(window), NULL);
}
// Call SDL render present
TCSDL_Present();
Expand Down

0 comments on commit 026921b

Please sign in to comment.