ELEC2645 (2018/19) / Mbed 2 deprecated el17arm

Dependencies:   mbed

Committer:
el17arm
Date:
Wed Mar 20 01:32:31 2019 +0000
Revision:
2:725c213b2396
Collision now detected though sprite does not react to collision yet

Who changed what in which revision?

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