diff --git a/sdl/sdl-printer.cpp b/sdl/sdl-printer.cpp index 7b9abc4..9fc1a5b 100644 --- a/sdl/sdl-printer.cpp +++ b/sdl/sdl-printer.cpp @@ -1,4 +1,5 @@ #include "sdl-printer.h" +#include #define WINDOWNAME "printer" @@ -19,15 +20,25 @@ SDLPrinter::SDLPrinter() window = NULL; renderer = NULL; + + printerMutex = SDL_CreateMutex(); + currentPageNumber = 0; } SDLPrinter::~SDLPrinter() { + SDL_DestroyMutex(printerMutex); } void SDLPrinter::update() { if (isDirty) { + // If SDL_TryLockMutex returns 0, then it locked the mutex and + // we'll continue. Otherwise, assume we're drawing something + // complicated and don't gum up the works... + if (SDL_TryLockMutex(printerMutex)) + return; + isDirty = false; // set early in case there's a race if (!window) { @@ -50,6 +61,7 @@ void SDLPrinter::update() SDL_RenderDrawPoint(renderer, x, y); } } + SDL_UnlockMutex(printerMutex); SDL_RenderPresent(renderer); } @@ -57,6 +69,7 @@ void SDLPrinter::update() void SDLPrinter::addLine(uint8_t *rowOfBits) { + SDL_LockMutex(printerMutex); for (int yoff=0; yoff<9; yoff++) { // 960 pixels == 120 bytes -- FIXME for (int i=0; i<(NATIVEWIDTH/8); i++) { @@ -76,16 +89,56 @@ void SDLPrinter::addLine(uint8_t *rowOfBits) } isDirty = true; + SDL_UnlockMutex(printerMutex); } void SDLPrinter::moveDownPixels(uint8_t p) { + SDL_LockMutex(printerMutex); ypos+= p; if (ypos >= HEIGHT) { + savePageAsBitmap(++currentPageNumber); + // clear page & restart SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); SDL_RenderClear(renderer); + + + for (int y=0; y