Skip to content

Commit a329e27

Browse files
committed
Use a mutex when painting or changing gfx surface.
1 parent b1e5f20 commit a329e27

File tree

5 files changed

+15
-1
lines changed

5 files changed

+15
-1
lines changed

libgag/include/EventListener.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class EventListener {
4040
static std::condition_variable startedCond;
4141
static std::mutex doneMutex;
4242
static std::condition_variable doneCond;
43+
static std::mutex renderMutex;
4344
void setPainter(std::function<void()> f);
4445
void paint();
4546
private:

libgag/src/EventListener.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ std::mutex EventListener::startMutex;
2525
std::condition_variable EventListener::startedCond;
2626
std::mutex EventListener::doneMutex;
2727
std::condition_variable EventListener::doneCond;
28+
std::mutex EventListener::renderMutex;
2829

2930
#define SIZE_MOVE_TIMER_ID 1
3031
#if defined(_WIN32) || defined(__MINGW32__) || defined(__MINGW64__)
@@ -52,12 +53,15 @@ EventListener::~EventListener()
5253
}
5354
void EventListener::setPainter(std::function<void()> f)
5455
{
56+
std::unique_lock<std::mutex> lock(renderMutex);
5557
painter = f;
5658
}
5759
void EventListener::paint()
5860
{
5961
if (painter)
6062
{
63+
std::unique_lock<std::mutex> lock(renderMutex);
64+
gfx->createGLContext();
6165
painter();
6266
gfx->nextFrame();
6367
}

libgag/src/GraphicContext.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <Toolkit.h>
2222
#include <FileManager.h>
2323
#include <SupportFunctions.h>
24+
#include "EventListener.h"
2425
#include <assert.h>
2526
#include <string>
2627
#include <sstream>
@@ -2060,6 +2061,7 @@ namespace GAGCore
20602061
}
20612062

20622063
SDL_Surface* GraphicContext::getOrCreateSurface(int w, int h, Uint32 flags) {
2064+
std::unique_lock<std::mutex> lock(EventListener::instance()->renderMutex);
20632065
if (flags & USEGPU)
20642066
{
20652067
if (sdlsurface)

src/Engine.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,14 @@ int Engine::run(void)
508508
if (nextGuiStep == 0)
509509
{
510510
// we draw
511+
static bool isPainterSet = false;
512+
if (!isPainterSet)
513+
{
514+
EventListener::instance()->setPainter(std::bind(GameGUI::drawAll, &gui, gui.localTeamNo));
515+
isPainterSet = true;
516+
}
517+
std::unique_lock<std::mutex> lock(EventListener::instance()->renderMutex);
518+
globalContainer->gfx->createGLContext();
511519
gui.drawAll(gui.localTeamNo);
512520
globalContainer->gfx->nextFrame();
513521
}

src/GameGUI.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4380,7 +4380,6 @@ void GameGUI::drawInGameScrollableText(void)
43804380

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

0 commit comments

Comments
 (0)