Game for Leeds University Gamepad for the FRDM-K64F: Game is a RPG Horror Title.
Dependencies: mbed FATFileSystem
N5110_Modded/Bitmap.cpp@21:f3b0ce18b44f, 2019-05-09 (annotated)
- Committer:
- rottenegg
- Date:
- Thu May 09 06:22:53 2019 +0000
- Revision:
- 21:f3b0ce18b44f
- Parent:
- 18:14e5391beccf
- Child:
- 26:716bcd47f3ca
Game_Manager: 20 lines per Function Achevied; Documentation Updated along with Inline Comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rottenegg | 18:14e5391beccf | 1 | #include "Bitmap.h" |
rottenegg | 21:f3b0ce18b44f | 2 | Serial d(USBTX,USBRX); |
rottenegg | 18:14e5391beccf | 3 | |
rottenegg | 18:14e5391beccf | 4 | //Total New Process time = 0.04 Seconds (still a bit long) |
rottenegg | 18:14e5391beccf | 5 | //Image data is Stored in DWORDS (32-bits) not Bytes (8-bits) |
rottenegg | 18:14e5391beccf | 6 | Bitmap::Bitmap(PinName const scePin, |
rottenegg | 18:14e5391beccf | 7 | PinName const rstPin, |
rottenegg | 18:14e5391beccf | 8 | PinName const dcPin, |
rottenegg | 18:14e5391beccf | 9 | PinName const mosiPin, |
rottenegg | 18:14e5391beccf | 10 | PinName const sclkPin, |
rottenegg | 18:14e5391beccf | 11 | PinName const ledPin) |
rottenegg | 18:14e5391beccf | 12 | : |
rottenegg | 18:14e5391beccf | 13 | N5110(scePin, rstPin, dcPin, mosiPin, sclkPin, ledPin) |
rottenegg | 18:14e5391beccf | 14 | {}; |
rottenegg | 18:14e5391beccf | 15 | |
rottenegg | 18:14e5391beccf | 16 | void Bitmap::renderBMP(const char *path, unsigned int const x, unsigned int const y) { |
rottenegg | 18:14e5391beccf | 17 | FILE *bmp = fopen(path,"r"); |
rottenegg | 21:f3b0ce18b44f | 18 | if (bmp == NULL) { |
rottenegg | 21:f3b0ce18b44f | 19 | d.printf("fail"); |
rottenegg | 21:f3b0ce18b44f | 20 | } |
rottenegg | 18:14e5391beccf | 21 | unsigned char *buffer = (unsigned char*)std::malloc(4 * sizeof(unsigned char)); |
rottenegg | 21:f3b0ce18b44f | 22 | this->readDIB(bmp); //Reading DIB header |
rottenegg | 18:14e5391beccf | 23 | unsigned short int dcount = (width / 32) + 1; //Counting DWORDS required per Row of Image |
rottenegg | 18:14e5391beccf | 24 | fseek(bmp,offbits,SEEK_SET); |
rottenegg | 18:14e5391beccf | 25 | std::bitset<32> bits; |
rottenegg | 18:14e5391beccf | 26 | offbits = 0; |
rottenegg | 18:14e5391beccf | 27 | row = 0; |
rottenegg | 18:14e5391beccf | 28 | colomn = 0; |
rottenegg | 18:14e5391beccf | 29 | for (unsigned int dwcount = 1; dwcount < ((dcount*height) + 1); dwcount++) { |
rottenegg | 18:14e5391beccf | 30 | this->swapEndian(bmp, buffer, bits); //Correcing Machine Endiness |
rottenegg | 18:14e5391beccf | 31 | if (dwcount % dcount == 0) { |
rottenegg | 18:14e5391beccf | 32 | this->halfLoop(x,y,bits); |
rottenegg | 18:14e5391beccf | 33 | } else { |
rottenegg | 18:14e5391beccf | 34 | this->fullLoop(x,y,bits); |
rottenegg | 18:14e5391beccf | 35 | } |
rottenegg | 18:14e5391beccf | 36 | } |
rottenegg | 18:14e5391beccf | 37 | std::fclose(bmp); |
rottenegg | 21:f3b0ce18b44f | 38 | free(buffer); |
rottenegg | 18:14e5391beccf | 39 | } |
rottenegg | 18:14e5391beccf | 40 | |
rottenegg | 18:14e5391beccf | 41 | void Bitmap::readDIB(FILE *bmp) { |
rottenegg | 18:14e5391beccf | 42 | std::fseek(bmp,10,SEEK_SET); |
rottenegg | 21:f3b0ce18b44f | 43 | std::fread(&offbits,4,1,bmp); //Bytes to skip to get to data block |
rottenegg | 18:14e5391beccf | 44 | std::fseek(bmp,18,SEEK_SET); |
rottenegg | 21:f3b0ce18b44f | 45 | std::fread(&width,4,1,bmp); //Width of Image |
rottenegg | 18:14e5391beccf | 46 | std::fseek(bmp,22,SEEK_SET); |
rottenegg | 21:f3b0ce18b44f | 47 | std::fread(&height,4,1,bmp); //Height of image |
rottenegg | 18:14e5391beccf | 48 | } |
rottenegg | 18:14e5391beccf | 49 | |
rottenegg | 18:14e5391beccf | 50 | void Bitmap::swapEndian(FILE *bmp, unsigned char *buffer, std::bitset<32> &bits) { |
rottenegg | 18:14e5391beccf | 51 | std::fread(buffer,1,4,bmp); |
rottenegg | 18:14e5391beccf | 52 | //Endian Swap |
rottenegg | 18:14e5391beccf | 53 | bits = buffer[0]; |
rottenegg | 18:14e5391beccf | 54 | bits = (bits << 8) | (std::bitset<32>)buffer[1]; |
rottenegg | 18:14e5391beccf | 55 | bits = (bits << 8) | (std::bitset<32>)buffer[2]; |
rottenegg | 18:14e5391beccf | 56 | bits = (bits << 8) | (std::bitset<32>)buffer[3]; |
rottenegg | 18:14e5391beccf | 57 | } |
rottenegg | 18:14e5391beccf | 58 | |
rottenegg | 18:14e5391beccf | 59 | void Bitmap::halfLoop(unsigned const int x, unsigned const int y, std::bitset<32> bits) { |
rottenegg | 18:14e5391beccf | 60 | //First Loop only reads remaining bits in a DWORD and skips the Packing bits |
rottenegg | 18:14e5391beccf | 61 | for (offbits = 0; offbits < (width % 32); offbits++){ |
rottenegg | 18:14e5391beccf | 62 | //Bit Loop to print out pixel data but skips the packing bits |
rottenegg | 18:14e5391beccf | 63 | this->renderDWORD(bits,x,y); |
rottenegg | 18:14e5391beccf | 64 | } |
rottenegg | 18:14e5391beccf | 65 | row++; |
rottenegg | 18:14e5391beccf | 66 | colomn = 0; |
rottenegg | 18:14e5391beccf | 67 | } |
rottenegg | 18:14e5391beccf | 68 | |
rottenegg | 18:14e5391beccf | 69 | void Bitmap::fullLoop(unsigned const int x, unsigned const int y, std::bitset<32> bits) { |
rottenegg | 18:14e5391beccf | 70 | //Second Loop reads the full DWORD |
rottenegg | 18:14e5391beccf | 71 | for (offbits = 0; offbits < 32; offbits++) { |
rottenegg | 18:14e5391beccf | 72 | this->renderDWORD(bits,x,y); |
rottenegg | 18:14e5391beccf | 73 | } |
rottenegg | 21:f3b0ce18b44f | 74 | } |
rottenegg | 21:f3b0ce18b44f | 75 | |
rottenegg | 21:f3b0ce18b44f | 76 | //Aiding Funtion for fullloop and halfloop |
rottenegg | 21:f3b0ce18b44f | 77 | void Bitmap::renderDWORD(std::bitset<32> bits, unsigned int const x, unsigned int const y) { |
rottenegg | 21:f3b0ce18b44f | 78 | bool state; |
rottenegg | 21:f3b0ce18b44f | 79 | if (bits[31 - offbits] == 1) { |
rottenegg | 21:f3b0ce18b44f | 80 | state = false; |
rottenegg | 21:f3b0ce18b44f | 81 | } else { |
rottenegg | 21:f3b0ce18b44f | 82 | state = true; |
rottenegg | 21:f3b0ce18b44f | 83 | } |
rottenegg | 21:f3b0ce18b44f | 84 | this->setPixel((colomn + x) ,(row + y),state); |
rottenegg | 21:f3b0ce18b44f | 85 | colomn++; |
rottenegg | 18:14e5391beccf | 86 | } |