Advertisement
Redxone

[C++] Gameboy Sprite Drawer (windows.h)

Jan 5th, 2019
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1. #include <iostream>
  2. #include "stdio.h"
  3. #include "conio.h"
  4. #include "windows.h"
  5. #include "math.h"
  6.  
  7. using namespace std;
  8.  
  9. #define waitExit() cout << "\n\nPress any key to exit."; getch();
  10.  
  11. // Console functions
  12. class Console
  13. {
  14. public:
  15.     HANDLE chandle;
  16.     COORD  cpos;
  17.     WORD* cfont;
  18.      Console(WORD* cfnt)
  19.      {
  20.         this->chandle = GetStdHandle(STD_OUTPUT_HANDLE);
  21.         this->cpos = (COORD) {1,1};
  22.         this->cfont = cfnt;
  23.      };
  24.     ~Console()
  25.     {
  26.         SetConsoleTextAttribute(this->chandle,15);
  27.     };
  28.     void setPos(int x, int y)
  29.     {
  30.         this->cpos.X = x;
  31.         this->cpos.Y = y;
  32.         SetConsoleCursorPosition(this->chandle,this->cpos);
  33.     }
  34.     void Plot(int color, int thickness=1)
  35.     {
  36.         if(color < 0 || color > 3) return; // Invalid color!
  37.         SetConsoleTextAttribute(this->chandle, this->cfont[color]);
  38.         for(int i = 0; i < thickness; i++)cout << "#"; // Add space to output buffer.
  39.     }
  40.     void PlotAt(int color, int x, int y, int thickness=1)
  41.     {
  42.         this->setPos(x,y);
  43.         this->Plot(color,thickness);
  44.     }
  45.     void Fill(WORD color, int x, int y, int width, int height)
  46.     {
  47.         for(int yy = 0; yy < height; yy++)
  48.             for(int xx = 0; xx < width; xx++)
  49.             {
  50.                 SetConsoleTextAttribute(this->chandle,color);
  51.                 this->setPos(x+xx,y+yy);
  52.                 cout << " ";
  53.             }
  54.     }
  55. };
  56.  
  57. class ImgBPP
  58. {
  59. public:
  60.     char* bin;
  61.     unsigned int fsize;
  62.     ImgBPP(const char* img)
  63.     {
  64.         FILE* fbin = fopen(img,"rb");
  65.         int nbytes=0;
  66.         if(fbin)
  67.         {
  68.             fseek(fbin,0,SEEK_END);
  69.             nbytes = ftell(fbin);
  70.             fseek(fbin,0,SEEK_SET);
  71.             this->bin = (char*) malloc(nbytes);
  72.             if(this->bin) fread(this->bin,1,nbytes,fbin);
  73.             this->fsize = nbytes;
  74.             fclose(fbin);
  75.         }
  76.         if(nbytes == 0) cout << "Failed to load \"" << img << "\" or nothing to load." << endl;
  77.     }
  78.     ~ImgBPP()
  79.     {
  80.         free(this->bin);
  81.     }
  82.     void DrawTile(Console* wobj, int tile=1, int x=1, int y=1)
  83.     {
  84.         int defx = x;
  85.         int defy = y;
  86.         int tile_end = (tile*16)+16;
  87.         if(this->fsize < tile_end) tile_end = this->fsize;
  88.         for(int i = (tile*16); i <= tile_end-1; i += 2)
  89.         {
  90.             char pbyte = *(this->bin+i);
  91.             char sbyte = *(this->bin+i+1);
  92.             unsigned char LSB, MSB, result;
  93.             for(int b = 7; b >= 0; b--)
  94.             {
  95.                 MSB = LSB = 0;
  96.                 int bit = pow(2,b);
  97.                 if(pbyte&bit) LSB = 1;
  98.                 if(sbyte&bit) MSB = 2;
  99.                 result = LSB+MSB;
  100.                 wobj->PlotAt(result,x,y,2);
  101.                 x+=2;
  102.             }
  103.             y++;
  104.             x=defx;
  105.         }
  106.     }
  107.     void Draw(Console* wobj, int x=1, int y=1, int tratio=1)
  108.     {
  109.         int defx = x;
  110.         int nextc = 0;
  111.         int maxtiles = this->fsize/16;
  112.         int tilecol = (maxtiles/tratio)*8;
  113.         int tilerow =  tratio*16;
  114.         wobj->Fill(0x0011,x-1,y-1,tilerow+2,tilecol+2);
  115.         for(int tilenum = 0; tilenum < maxtiles; tilenum++)
  116.         {
  117.             if(nextc == tratio)
  118.             {
  119.                 y+=8;
  120.                 x = defx;
  121.                 nextc = 0;
  122.             }
  123.             this->DrawTile(wobj,tilenum,x,y);
  124.             x += 16;
  125.             nextc++;
  126.         }
  127.     }
  128. };
  129.  
  130.  
  131. int main()
  132. {
  133.     WORD cfont[4] = {0x00FF,0x0088,0x0077,0x0000};
  134.     Console* con = new Console( (WORD*) &cfont );
  135.     ImgBPP* imgtst = new ImgBPP("test.2bpp");
  136.     imgtst->Draw(con,1,1,4);
  137.     delete(imgtst);
  138.     delete(con);
  139.     waitExit();
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement