ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_PROJECT_EL18KK

Dependencies:   mbed

Committer:
KaifK
Date:
Fri Apr 24 22:24:14 2020 +0000
Revision:
0:e47aa16004e6
game idea confirmed and all sprites drawn;

Who changed what in which revision?

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