Skip to content

Commit

Permalink
Use a mutex when painting or changing gfx surface.
Browse files Browse the repository at this point in the history
  • Loading branch information
Quipyowert2 committed Mar 24, 2023
1 parent b1e5f20 commit a329e27
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions libgag/include/EventListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class EventListener {
static std::condition_variable startedCond;
static std::mutex doneMutex;
static std::condition_variable doneCond;
static std::mutex renderMutex;
void setPainter(std::function<void()> f);
void paint();
private:
Expand Down
4 changes: 4 additions & 0 deletions libgag/src/EventListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ std::mutex EventListener::startMutex;
std::condition_variable EventListener::startedCond;
std::mutex EventListener::doneMutex;
std::condition_variable EventListener::doneCond;
std::mutex EventListener::renderMutex;

#define SIZE_MOVE_TIMER_ID 1
#if defined(_WIN32) || defined(__MINGW32__) || defined(__MINGW64__)
Expand Down Expand Up @@ -52,12 +53,15 @@ EventListener::~EventListener()
}
void EventListener::setPainter(std::function<void()> f)
{
std::unique_lock<std::mutex> lock(renderMutex);
painter = f;
}
void EventListener::paint()
{
if (painter)
{
std::unique_lock<std::mutex> lock(renderMutex);
gfx->createGLContext();
painter();
gfx->nextFrame();
}
Expand Down
2 changes: 2 additions & 0 deletions libgag/src/GraphicContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <Toolkit.h>
#include <FileManager.h>
#include <SupportFunctions.h>
#include "EventListener.h"
#include <assert.h>
#include <string>
#include <sstream>
Expand Down Expand Up @@ -2060,6 +2061,7 @@ namespace GAGCore
}

SDL_Surface* GraphicContext::getOrCreateSurface(int w, int h, Uint32 flags) {
std::unique_lock<std::mutex> lock(EventListener::instance()->renderMutex);
if (flags & USEGPU)
{
if (sdlsurface)
Expand Down
8 changes: 8 additions & 0 deletions src/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,14 @@ int Engine::run(void)
if (nextGuiStep == 0)
{
// we draw
static bool isPainterSet = false;
if (!isPainterSet)
{
EventListener::instance()->setPainter(std::bind(GameGUI::drawAll, &gui, gui.localTeamNo));
isPainterSet = true;
}
std::unique_lock<std::mutex> lock(EventListener::instance()->renderMutex);
globalContainer->gfx->createGLContext();
gui.drawAll(gui.localTeamNo);
globalContainer->gfx->nextFrame();
}
Expand Down
1 change: 0 additions & 1 deletion src/GameGUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4380,7 +4380,6 @@ void GameGUI::drawInGameScrollableText(void)

void GameGUI::drawAll(int team)
{
EventListener::instance()->setPainter(std::bind(&GameGUI::drawAll, this, team));
// draw the map
Uint32 drawOptions = (drawHealthFoodBar ? Game::DRAW_HEALTH_FOOD_BAR : 0) |
(drawPathLines ? Game::DRAW_PATH_LINE : 0) |
Expand Down

0 comments on commit a329e27

Please sign in to comment.