Final Submission. I have read and agreed with Statement of Academic Integrity.

Dependencies:   mbed

Committer:
Nicholas75179
Date:
Fri May 22 10:28:29 2020 +0000
Revision:
2:bb50c3a9a05c
Parent:
0:fde420d18f42
published 11:27 22/5/2020

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Nicholas75179 0:fde420d18f42 1 #include "Bitmap.h"
Nicholas75179 0:fde420d18f42 2
Nicholas75179 0:fde420d18f42 3 #include <iostream>
Nicholas75179 0:fde420d18f42 4
Nicholas75179 0:fde420d18f42 5 #include "N5110.h"
Nicholas75179 0:fde420d18f42 6
Nicholas75179 0:fde420d18f42 7 Bitmap::Bitmap(int const *contents,
Nicholas75179 0:fde420d18f42 8 unsigned int const height,
Nicholas75179 0:fde420d18f42 9 unsigned int const width)
Nicholas75179 0:fde420d18f42 10 :
Nicholas75179 0:fde420d18f42 11 _contents(std::vector<int>(height*width)),
Nicholas75179 0:fde420d18f42 12 _height(height),
Nicholas75179 0:fde420d18f42 13 _width(width)
Nicholas75179 0:fde420d18f42 14 {
Nicholas75179 0:fde420d18f42 15 // Perform a quick sanity check of the dimensions
Nicholas75179 0:fde420d18f42 16 if (_contents.size() != height * width) {
Nicholas75179 0:fde420d18f42 17 std::cerr << "Contents of bitmap has size " << _contents.size()
Nicholas75179 0:fde420d18f42 18 << " pixels, but its dimensions were specified as "
Nicholas75179 0:fde420d18f42 19 << width << " * " << height << " = " << width * height << std::endl;
Nicholas75179 0:fde420d18f42 20 }
Nicholas75179 0:fde420d18f42 21
Nicholas75179 0:fde420d18f42 22 for(unsigned int i = 0; i < height*width; ++i) _contents[i] = contents[i];
Nicholas75179 0:fde420d18f42 23 }
Nicholas75179 0:fde420d18f42 24
Nicholas75179 0:fde420d18f42 25 /**
Nicholas75179 0:fde420d18f42 26 * @returns the value of the pixel at the given position
Nicholas75179 0:fde420d18f42 27 */
Nicholas75179 0:fde420d18f42 28 int Bitmap::get_pixel(unsigned int const row,
Nicholas75179 0:fde420d18f42 29 unsigned int const column) const
Nicholas75179 0:fde420d18f42 30 {
Nicholas75179 0:fde420d18f42 31 // First check that row and column indices are within bounds
Nicholas75179 0:fde420d18f42 32 if(column >= _width || row >= _height)
Nicholas75179 0:fde420d18f42 33 {
Nicholas75179 0:fde420d18f42 34 std::cerr << "The requested pixel with index " << row << "," << column
Nicholas75179 0:fde420d18f42 35 << "is outside the bitmap dimensions: " << _width << ","
Nicholas75179 0:fde420d18f42 36 << _height << std::endl;
Nicholas75179 0:fde420d18f42 37 }
Nicholas75179 0:fde420d18f42 38
Nicholas75179 0:fde420d18f42 39 // Now return the pixel value, using row-major indexing
Nicholas75179 0:fde420d18f42 40 return _contents[row * _width + column];
Nicholas75179 0:fde420d18f42 41 }
Nicholas75179 0:fde420d18f42 42
Nicholas75179 0:fde420d18f42 43 /**
Nicholas75179 0:fde420d18f42 44 * @brief Prints the contents of the bitmap to the terminal
Nicholas75179 0:fde420d18f42 45 */
Nicholas75179 0:fde420d18f42 46 void Bitmap::print() const
Nicholas75179 0:fde420d18f42 47 {
Nicholas75179 0:fde420d18f42 48 for (unsigned int row = 0; row < _height; ++row)
Nicholas75179 0:fde420d18f42 49 {
Nicholas75179 0:fde420d18f42 50 // Print each element of the row
Nicholas75179 0:fde420d18f42 51 for (unsigned int column = 0; column < _width; ++column)
Nicholas75179 0:fde420d18f42 52 {
Nicholas75179 0:fde420d18f42 53 int pixel = get_pixel(row, column);
Nicholas75179 0:fde420d18f42 54 std::cout << pixel;
Nicholas75179 0:fde420d18f42 55 }
Nicholas75179 0:fde420d18f42 56
Nicholas75179 0:fde420d18f42 57 // And then terminate with a new-line character
Nicholas75179 0:fde420d18f42 58 std::cout << std::endl;
Nicholas75179 0:fde420d18f42 59 }
Nicholas75179 0:fde420d18f42 60 }
Nicholas75179 0:fde420d18f42 61
Nicholas75179 0:fde420d18f42 62 /**
Nicholas75179 0:fde420d18f42 63 * @brief Renders the contents of the bitmap onto an N5110 screen
Nicholas75179 0:fde420d18f42 64 *
Nicholas75179 0:fde420d18f42 65 * @param[in] lcd The screen to use for rendering
Nicholas75179 0:fde420d18f42 66 * @param[in] x0 The horizontal position in pixels at which to render the bitmap
Nicholas75179 0:fde420d18f42 67 * @param[in] y0 The vertical position in pixels at which to render the bitmap
Nicholas75179 0:fde420d18f42 68 *
Nicholas75179 0:fde420d18f42 69 * @details Note that x0, y0 gives the location of the top-left of the bitmap on
Nicholas75179 0:fde420d18f42 70 * the screen.
Nicholas75179 0:fde420d18f42 71 * This function only updates the buffer on the screen. You still need
Nicholas75179 0:fde420d18f42 72 * to refresh the screen in order to actually see the bitmap.
Nicholas75179 0:fde420d18f42 73 */
Nicholas75179 0:fde420d18f42 74 void Bitmap::render(N5110 &lcd,
Nicholas75179 0:fde420d18f42 75 unsigned int const x0,
Nicholas75179 0:fde420d18f42 76 unsigned int const y0) const
Nicholas75179 0:fde420d18f42 77 {
Nicholas75179 0:fde420d18f42 78 // Loop through each row of the bitmap image
Nicholas75179 0:fde420d18f42 79 for (unsigned int bitmap_row = 0; bitmap_row < _height; ++bitmap_row)
Nicholas75179 0:fde420d18f42 80 {
Nicholas75179 0:fde420d18f42 81 // Row index on the screen for rendering the row of pixels
Nicholas75179 0:fde420d18f42 82 unsigned int screen_row = y0 + bitmap_row;
Nicholas75179 0:fde420d18f42 83
Nicholas75179 0:fde420d18f42 84 // Render each pixel in the row
Nicholas75179 0:fde420d18f42 85 for (unsigned int bitmap_col = 0; bitmap_col < _width; ++bitmap_col)
Nicholas75179 0:fde420d18f42 86 {
Nicholas75179 0:fde420d18f42 87 // Column index on the screen for rendering this pixel
Nicholas75179 0:fde420d18f42 88 int screen_col = x0 + bitmap_col;
Nicholas75179 0:fde420d18f42 89
Nicholas75179 0:fde420d18f42 90 // Find the required value of the pixel at the given location within
Nicholas75179 0:fde420d18f42 91 // the bitmap data and then write it to the LCD screen
Nicholas75179 0:fde420d18f42 92 int pixel = get_pixel(bitmap_row, bitmap_col);
Nicholas75179 0:fde420d18f42 93 if(pixel<2)lcd.setPixel(screen_col, screen_row, pixel);
Nicholas75179 0:fde420d18f42 94 //I have modified the code here to skip 2s,
Nicholas75179 0:fde420d18f42 95 //I use this to create transparency in my sprites.
Nicholas75179 0:fde420d18f42 96 }
Nicholas75179 0:fde420d18f42 97 }
Nicholas75179 0:fde420d18f42 98 }