Mateo Gomez-Randulfe / Mbed 2 deprecated The_Children_of_Cronos

Dependencies:   mbed

Fork of The_Children_of_Cronos_el15mggr by ELEC2645 (2016/17)

Committer:
matirc
Date:
Fri May 05 11:43:35 2017 +0000
Revision:
4:466cda7cae42
FINAL GAME VERSION

Who changed what in which revision?

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