Kern Fowler / Mbed 2 deprecated Donkey_Kong_Game

Dependencies:   mbed

Committer:
Kern_EL17KJTF
Date:
Wed Mar 13 11:12:10 2019 +0000
Revision:
0:0130fd5738f7
initial comment

Who changed what in which revision?

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