Skip to content

Commit

Permalink
Fix crash when app is sent to background
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipe Manga authored and Felipe Manga committed Dec 13, 2023
1 parent 2239d71 commit 62f3d9c
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/she/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ namespace she {

// Flips all graphics in the surface to the real display.
virtual void flip(const gfx::Rect& bounds) = 0;
virtual void present() {}

virtual void toggleFullscreen(){}
virtual void maximize() = 0;
Expand Down
15 changes: 10 additions & 5 deletions src/she/sdl2/sdl2_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ namespace she {
if (!m_window)
throw DisplayCreationException(SDL_GetError());

auto flags = gpu ? SDL_RENDERER_ACCELERATED : SDL_RENDERER_SOFTWARE;
m_renderer = SDL_CreateRenderer(m_window, -1, flags);
if (gpu)
m_renderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_ACCELERATED);

sdl::windowIdToDisplay[SDL_GetWindowID(m_window)] = this;
SDL_GetWindowSize(m_window, &width, &height);
Expand Down Expand Up @@ -183,7 +183,10 @@ namespace she {
if (!m_dirty)
return;
m_dirty = false;
SDL_UpdateWindowSurface(m_window);
if (m_renderer)
SDL_RenderPresent(m_renderer);
else
SDL_UpdateWindowSurface(m_window);
}

void SDL2Display::flip(const gfx::Rect& bounds)
Expand All @@ -193,9 +196,11 @@ namespace she {
SDL_Rect rect {bounds.x, bounds.y, bounds.w, bounds.h};
if (m_renderer) {
auto texture = static_cast<SDL2Surface*>(m_surface)->getTexture(rect);
SDL_RenderClear(m_renderer);
SDL_Rect dst {
rect.x * m_scale, rect.y * m_scale,
rect.w * m_scale, rect.h * m_scale
};
SDL_RenderCopy(m_renderer, texture, nullptr, nullptr);
SDL_RenderPresent(m_renderer);
} else {
auto nativeSurface = SDL_GetWindowSurface(m_window);
SDL_Rect dst {
Expand Down
5 changes: 5 additions & 0 deletions src/she/sdl2/sdl2_surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,12 @@ namespace she {

SDL_Texture* SDL2Surface::getTexture(SDL_Rect& rect) {
auto pixels = ((uint8_t*)m_bmp->pixels) + m_bmp->pitch * rect.y + m_bmp->format->BytesPerPixel * rect.x;
if (m_texture && m_textureGen != textureGen) {
SDL_DestroyTexture(m_texture);
m_texture = nullptr;
}
if (!m_texture) {
m_textureGen = textureGen;
auto renderer = she::unique_display->renderer();
m_texture = SDL_CreateTexture(renderer,
m_bmp->format->format,
Expand Down
3 changes: 3 additions & 0 deletions src/she/sdl2/sdl2_surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ namespace she {

SDL_Texture* getTexture(SDL_Rect& rect);

static inline unsigned int textureGen;

private:
unsigned int m_textureGen{};
SDL_Texture* m_texture{};
uint32_t m_textureFormat{};
SDL_Surface* m_bmp{};
Expand Down
5 changes: 5 additions & 0 deletions src/she/sdl2/she.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ namespace she {
while (SDL_PollEvent(&sdlEvent)) {
switch (sdlEvent.type) {
case SDL_APP_DIDENTERFOREGROUND:
SDL2Surface::textureGen++;
forceFlip();
continue;

Expand All @@ -261,6 +262,8 @@ namespace she {
case SDL_WINDOWEVENT_EXPOSED:
forceFlip();
continue;
case SDL_WINDOWEVENT_SIZE_CHANGED:
continue;

case SDL_WINDOWEVENT_MAXIMIZED:
sdl::isMaximized = true;
Expand Down Expand Up @@ -297,6 +300,7 @@ namespace she {
Event ev;
ev.setType(Event::MouseLeave);
queue_event(ev);
break;
}
}

Expand Down Expand Up @@ -508,6 +512,7 @@ namespace she {
}

void setGpuAcceleration(bool state) override {
if (!unique_display)
SDL2Display::gpu = state;
}

Expand Down
1 change: 1 addition & 0 deletions src/ui/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ void Manager::flipDisplay()

for (auto& rc : m_dirtyRegion)
m_display->flip(rc);
m_display->present();

m_dirtyRegion.clear();
}
Expand Down

0 comments on commit 62f3d9c

Please sign in to comment.