Mochu Yao explorer game

Dependencies:   mbed

Committer:
el17my
Date:
Wed Apr 15 08:17:18 2020 +0000
Revision:
1:ed745421d8c4
second part of the code 4/13

Who changed what in which revision?

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