OP
Very Senior Member
Joined: Feb 2014
Posts: 1,102 Likes: 173 |
I was thinking about how it'd be interesting to write a "printer" simulator in lua so I could render various printer outputs that have different ESC codes... like Imagewriter ESC codes, 'cuz I'm dying to print dot matrix output in color. I made a luaprinter class and I've got it so it can render to a bitmap. Just a proof of concept. class luaprinter {
public:
luaprinter(){};
const static int BUFFERSIZE = 128000; // a nice big size so we shouldn't have to worry
int m_printerbuffer[BUFFERSIZE];
int m_head = 0;
int m_tail = 0;
bitmap_rgb32 *m_bitmap; // pointer to bitmap
int m_xpos = 0;
int m_ypos = 0;
virtual void clearpage(){ m_bitmap->fill(0);};
virtual void drawpixel(int x, int y, int pixelval){m_bitmap->pix32(y,x) = pixelval; }; // row major
virtual int getpixel(int x, int y){return m_bitmap->pix32(y,x);};
virtual void setheadpos(int x, int y){m_xpos=x;m_ypos=y;};
virtual void savepage(){};
virtual std::tuple<int*,int,int> getbuffer() { return std::make_tuple(m_printerbuffer,m_head,m_tail);}; // returns the entire buffer as an integer a>
virtual int getnextchar(){if (m_head==m_tail) return -1; else { int retval = m_printerbuffer[m_tail++]; m_tail %= BUFFERSIZE; return retval;}};
virtual void putnextchar(int c){m_printerbuffer[m_head++]=c; m_head %= BUFFERSIZE;};
void register_bitmap(bitmap_rgb32 &mybitmap){m_bitmap = &mybitmap;} //
void testmodifybuffer(){ for (int i=0;i<10;i++){ m_printerbuffer[i]=i; m_head++; }; };
std::tuple<int,int>getheadpos() { return std::make_tuple(m_xpos,m_ypos); };
static std::vector<luaprinter *> luaprinterlist;
};
and then hack on centronics/printer.cpp to inherit from luaprinter, and do a little bit of hookups in luaengine.
auto luaprinter_type = sol().registry().create_simple_usertype<luaprinter>("new", sol::no_constructor);
luaprinter_type.set("clearpage", &luaprinter::clearpage);
luaprinter_type.set("drawpixel", &luaprinter::drawpixel);
luaprinter_type.set("getpixel", &luaprinter::getpixel);
luaprinter_type.set("setheadpos", &luaprinter::setheadpos);
luaprinter_type.set("savepage", &luaprinter::savepage);
luaprinter_type.set("getbuffer", &luaprinter::getbuffer);
luaprinter_type.set("getnextchar", &luaprinter::getnextchar);
luaprinter_type.set("getheadpos", &luaprinter::getheadpos);
sol().registry().set_usertype("luaprinter", luaprinter_type);
machine_type.set("luaprinters", sol::property([this](running_machine &m) {
sol::table table = sol().create_table();
int i=0;
for(luaprinter* p : luaprinter::luaprinterlist)
{
table[i++] = &(*p);
};
return table;
}));
I haven't worked it all out yet and I'm figuring out all this cpp stuff (and sometimes not figuring it out 8-) but I did get it to draw on my bitmap:
for i=0,255 do for p=0,7 do
if (2^p) & i ~= 0 then
manager:machine().luaprinters[0]:drawpixel(i,20+p,0)
manager:machine().luaprinters[0]:setheadpos(i,20)
end end end
And it wouldn't be hard at all to do color:
function iff(a,b,c) if a~=0 then return b else return c end end
for i=0,255 do
for p=0,7 do
if (2^p) & i ~= 0 then
manager:machine().luaprinters[0]:drawpixel(i,20+p,iff((2^1)&p,0xff0000,0)|iff((2^2)&p,0xff00,0)|iff((2^3)&p,0xff,0))
manager:machine().luaprinters[0]:setheadpos(i,20)
end end end
|