Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "stdio.h"
- #include "conio.h"
- #include "windows.h"
- #include "math.h"
- using namespace std;
- #define waitExit() cout << "\n\nPress any key to exit."; getch();
- // Console functions
- class Console
- {
- public:
- HANDLE chandle;
- COORD cpos;
- WORD* cfont;
- Console(WORD* cfnt)
- {
- this->chandle = GetStdHandle(STD_OUTPUT_HANDLE);
- this->cpos = (COORD) {1,1};
- this->cfont = cfnt;
- };
- ~Console()
- {
- SetConsoleTextAttribute(this->chandle,15);
- };
- void setPos(int x, int y)
- {
- this->cpos.X = x;
- this->cpos.Y = y;
- SetConsoleCursorPosition(this->chandle,this->cpos);
- }
- void Plot(int color, int thickness=1)
- {
- if(color < 0 || color > 3) return; // Invalid color!
- SetConsoleTextAttribute(this->chandle, this->cfont[color]);
- for(int i = 0; i < thickness; i++)cout << "#"; // Add space to output buffer.
- }
- void PlotAt(int color, int x, int y, int thickness=1)
- {
- this->setPos(x,y);
- this->Plot(color,thickness);
- }
- void Fill(WORD color, int x, int y, int width, int height)
- {
- for(int yy = 0; yy < height; yy++)
- for(int xx = 0; xx < width; xx++)
- {
- SetConsoleTextAttribute(this->chandle,color);
- this->setPos(x+xx,y+yy);
- cout << " ";
- }
- }
- };
- class ImgBPP
- {
- public:
- char* bin;
- unsigned int fsize;
- ImgBPP(const char* img)
- {
- FILE* fbin = fopen(img,"rb");
- int nbytes=0;
- if(fbin)
- {
- fseek(fbin,0,SEEK_END);
- nbytes = ftell(fbin);
- fseek(fbin,0,SEEK_SET);
- this->bin = (char*) malloc(nbytes);
- if(this->bin) fread(this->bin,1,nbytes,fbin);
- this->fsize = nbytes;
- fclose(fbin);
- }
- if(nbytes == 0) cout << "Failed to load \"" << img << "\" or nothing to load." << endl;
- }
- ~ImgBPP()
- {
- free(this->bin);
- }
- void DrawTile(Console* wobj, int tile=1, int x=1, int y=1)
- {
- int defx = x;
- int defy = y;
- int tile_end = (tile*16)+16;
- if(this->fsize < tile_end) tile_end = this->fsize;
- for(int i = (tile*16); i <= tile_end-1; i += 2)
- {
- char pbyte = *(this->bin+i);
- char sbyte = *(this->bin+i+1);
- unsigned char LSB, MSB, result;
- for(int b = 7; b >= 0; b--)
- {
- MSB = LSB = 0;
- int bit = pow(2,b);
- if(pbyte&bit) LSB = 1;
- if(sbyte&bit) MSB = 2;
- result = LSB+MSB;
- wobj->PlotAt(result,x,y,2);
- x+=2;
- }
- y++;
- x=defx;
- }
- }
- void Draw(Console* wobj, int x=1, int y=1, int tratio=1)
- {
- int defx = x;
- int nextc = 0;
- int maxtiles = this->fsize/16;
- int tilecol = (maxtiles/tratio)*8;
- int tilerow = tratio*16;
- wobj->Fill(0x0011,x-1,y-1,tilerow+2,tilecol+2);
- for(int tilenum = 0; tilenum < maxtiles; tilenum++)
- {
- if(nextc == tratio)
- {
- y+=8;
- x = defx;
- nextc = 0;
- }
- this->DrawTile(wobj,tilenum,x,y);
- x += 16;
- nextc++;
- }
- }
- };
- int main()
- {
- WORD cfont[4] = {0x00FF,0x0088,0x0077,0x0000};
- Console* con = new Console( (WORD*) &cfont );
- ImgBPP* imgtst = new ImgBPP("test.2bpp");
- imgtst->Draw(con,1,1,4);
- delete(imgtst);
- delete(con);
- waitExit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement