#include "sdl-printer.h" #include #define WINDOWNAME "printer" inline void putpixel(SDL_Renderer *renderer, int x, int y, uint8_t d) { uint8_t v = (d ? 0xFF : 0x00); SDL_SetRenderDrawColor(renderer, v, v, v, 255); SDL_RenderDrawPoint(renderer, x, y); } SDLPrinter::SDLPrinter() { ypos = 0; isDirty = false; memset((void *)_hackyBitmap, 0, sizeof(_hackyBitmap)); 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) { window = SDL_CreateWindow(WINDOWNAME, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN); renderer = SDL_CreateRenderer(window, -1, 0); } for (int y=0; y= HEIGHT) { ypos = 0; } 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