fixed concurrent updates of _hackyBitmap; added save-to-.bmp

This commit is contained in:
Jorj Bauer 2018-02-19 15:48:50 -05:00
parent 5b6760d33a
commit ba0593bb70
2 changed files with 59 additions and 1 deletions

View File

@ -1,4 +1,5 @@
#include "sdl-printer.h" #include "sdl-printer.h"
#include <stdio.h>
#define WINDOWNAME "printer" #define WINDOWNAME "printer"
@ -19,15 +20,25 @@ SDLPrinter::SDLPrinter()
window = NULL; window = NULL;
renderer = NULL; renderer = NULL;
printerMutex = SDL_CreateMutex();
currentPageNumber = 0;
} }
SDLPrinter::~SDLPrinter() SDLPrinter::~SDLPrinter()
{ {
SDL_DestroyMutex(printerMutex);
} }
void SDLPrinter::update() void SDLPrinter::update()
{ {
if (isDirty) { 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 isDirty = false; // set early in case there's a race
if (!window) { if (!window) {
@ -50,6 +61,7 @@ void SDLPrinter::update()
SDL_RenderDrawPoint(renderer, x, y); SDL_RenderDrawPoint(renderer, x, y);
} }
} }
SDL_UnlockMutex(printerMutex);
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
} }
@ -57,6 +69,7 @@ void SDLPrinter::update()
void SDLPrinter::addLine(uint8_t *rowOfBits) void SDLPrinter::addLine(uint8_t *rowOfBits)
{ {
SDL_LockMutex(printerMutex);
for (int yoff=0; yoff<9; yoff++) { for (int yoff=0; yoff<9; yoff++) {
// 960 pixels == 120 bytes -- FIXME // 960 pixels == 120 bytes -- FIXME
for (int i=0; i<(NATIVEWIDTH/8); i++) { for (int i=0; i<(NATIVEWIDTH/8); i++) {
@ -76,16 +89,56 @@ void SDLPrinter::addLine(uint8_t *rowOfBits)
} }
isDirty = true; isDirty = true;
SDL_UnlockMutex(printerMutex);
} }
void SDLPrinter::moveDownPixels(uint8_t p) void SDLPrinter::moveDownPixels(uint8_t p)
{ {
SDL_LockMutex(printerMutex);
ypos+= p; ypos+= p;
if (ypos >= HEIGHT) { if (ypos >= HEIGHT) {
savePageAsBitmap(++currentPageNumber);
// clear page & restart // clear page & restart
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
for (int y=0; y<HEIGHT; y++) {
for (int x=0; x<WIDTH; x++) {
_hackyBitmap[y*WIDTH+x] = 0;
}
}
isDirty = true; isDirty = true;
ypos = 0; ypos = 0;
} }
SDL_UnlockMutex(printerMutex);
} }
void SDLPrinter::savePageAsBitmap(uint32_t pageno)
{
SDL_Surface* saveSurface = NULL;
char buf[255];
sprintf(buf, "page-%d.bmp", pageno);
saveSurface = SDL_CreateRGBSurfaceFrom((void *)_hackyBitmap,
WIDTH,
HEIGHT,
8, // bpp
WIDTH * 1, // bytes per row
0xE0, // rmask
0x1C, // gmask
0x03, // bmask
0x00 // amask
);
if (saveSurface) {
SDL_SaveBMP(saveSurface, buf);
SDL_FreeSurface(saveSurface);
} else {
printf("Failed to create saveSurface\n");
}
}

View File

@ -28,13 +28,18 @@ class SDLPrinter : public PhysicalPrinter {
virtual void moveDownPixels(uint8_t p); virtual void moveDownPixels(uint8_t p);
void savePageAsBitmap(uint32_t pageno);
private: private:
bool isDirty; bool isDirty;
uint16_t ypos; uint16_t ypos;
SDL_Window *window; SDL_Window *window;
SDL_Renderer *renderer; SDL_Renderer *renderer;
bool _hackyBitmap[WIDTH * HEIGHT]; volatile uint8_t _hackyBitmap[WIDTH * HEIGHT];
SDL_mutex *printerMutex;
uint32_t currentPageNumber;
}; };
#endif #endif