“Race Collision” is a one player game in which a truck has to avoid “particles” that appear on the road. By the use of the joystick, the player can guide themselves through the menu system to start the game. The truck is the main element of the game and it can be moved from side to side with the joystick. The road curves randomly from time to time and the player has to be careful to keep the truck within the road boundaries. Particles appear on the screen at random positions and 4 collisions lead to the end of the game.
Dependencies: ELEC2645_JoystickLCD_LPC1768_2021
lib/Bitmap.cpp@9:6f060f495536, 2021-05-06 (annotated)
- Committer:
- alex_20
- Date:
- Thu May 06 12:04:45 2021 +0000
- Revision:
- 9:6f060f495536
- Parent:
- 0:be41a15e7a86
Race Collision for ELEC2645
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 0:be41a15e7a86 | 1 | #include "Bitmap.h" |
eencae | 0:be41a15e7a86 | 2 | |
eencae | 0:be41a15e7a86 | 3 | #include <iostream> |
eencae | 0:be41a15e7a86 | 4 | |
eencae | 0:be41a15e7a86 | 5 | #include "N5110.h" |
eencae | 0:be41a15e7a86 | 6 | |
eencae | 0:be41a15e7a86 | 7 | Bitmap::Bitmap(int const *contents, |
eencae | 0:be41a15e7a86 | 8 | unsigned int const height, |
eencae | 0:be41a15e7a86 | 9 | unsigned int const width) |
eencae | 0:be41a15e7a86 | 10 | : |
eencae | 0:be41a15e7a86 | 11 | _contents(std::vector<int>(height*width)), |
eencae | 0:be41a15e7a86 | 12 | _height(height), |
eencae | 0:be41a15e7a86 | 13 | _width(width) |
eencae | 0:be41a15e7a86 | 14 | { |
eencae | 0:be41a15e7a86 | 15 | // Perform a quick sanity check of the dimensions |
eencae | 0:be41a15e7a86 | 16 | if (_contents.size() != height * width) { |
eencae | 0:be41a15e7a86 | 17 | std::cerr << "Contents of bitmap has size " << _contents.size() |
eencae | 0:be41a15e7a86 | 18 | << " pixels, but its dimensions were specified as " |
eencae | 0:be41a15e7a86 | 19 | << width << " * " << height << " = " << width * height << std::endl; |
eencae | 0:be41a15e7a86 | 20 | } |
eencae | 0:be41a15e7a86 | 21 | |
eencae | 0:be41a15e7a86 | 22 | for(unsigned int i = 0; i < height*width; ++i) _contents[i] = contents[i]; |
eencae | 0:be41a15e7a86 | 23 | } |
eencae | 0:be41a15e7a86 | 24 | |
eencae | 0:be41a15e7a86 | 25 | /** |
eencae | 0:be41a15e7a86 | 26 | * @returns the value of the pixel at the given position |
eencae | 0:be41a15e7a86 | 27 | */ |
eencae | 0:be41a15e7a86 | 28 | int Bitmap::get_pixel(unsigned int const row, |
eencae | 0:be41a15e7a86 | 29 | unsigned int const column) const |
eencae | 0:be41a15e7a86 | 30 | { |
eencae | 0:be41a15e7a86 | 31 | // First check that row and column indices are within bounds |
eencae | 0:be41a15e7a86 | 32 | if(column >= _width || row >= _height) |
eencae | 0:be41a15e7a86 | 33 | { |
eencae | 0:be41a15e7a86 | 34 | std::cerr << "The requested pixel with index " << row << "," << column |
eencae | 0:be41a15e7a86 | 35 | << "is outside the bitmap dimensions: " << _width << "," |
eencae | 0:be41a15e7a86 | 36 | << _height << std::endl; |
eencae | 0:be41a15e7a86 | 37 | } |
eencae | 0:be41a15e7a86 | 38 | |
eencae | 0:be41a15e7a86 | 39 | // Now return the pixel value, using row-major indexing |
eencae | 0:be41a15e7a86 | 40 | return _contents[row * _width + column]; |
eencae | 0:be41a15e7a86 | 41 | } |
eencae | 0:be41a15e7a86 | 42 | |
eencae | 0:be41a15e7a86 | 43 | /** |
eencae | 0:be41a15e7a86 | 44 | * @brief Prints the contents of the bitmap to the terminal |
eencae | 0:be41a15e7a86 | 45 | */ |
eencae | 0:be41a15e7a86 | 46 | void Bitmap::print() const |
eencae | 0:be41a15e7a86 | 47 | { |
eencae | 0:be41a15e7a86 | 48 | for (unsigned int row = 0; row < _height; ++row) |
eencae | 0:be41a15e7a86 | 49 | { |
eencae | 0:be41a15e7a86 | 50 | // Print each element of the row |
eencae | 0:be41a15e7a86 | 51 | for (unsigned int column = 0; column < _width; ++column) |
eencae | 0:be41a15e7a86 | 52 | { |
eencae | 0:be41a15e7a86 | 53 | int pixel = get_pixel(row, column); |
eencae | 0:be41a15e7a86 | 54 | std::cout << pixel; |
eencae | 0:be41a15e7a86 | 55 | } |
eencae | 0:be41a15e7a86 | 56 | |
eencae | 0:be41a15e7a86 | 57 | // And then terminate with a new-line character |
eencae | 0:be41a15e7a86 | 58 | std::cout << std::endl; |
eencae | 0:be41a15e7a86 | 59 | } |
eencae | 0:be41a15e7a86 | 60 | } |
eencae | 0:be41a15e7a86 | 61 | |
eencae | 0:be41a15e7a86 | 62 | /** |
eencae | 0:be41a15e7a86 | 63 | * @brief Renders the contents of the bitmap onto an N5110 screen |
eencae | 0:be41a15e7a86 | 64 | * |
eencae | 0:be41a15e7a86 | 65 | * @param[in] lcd The screen to use for rendering |
eencae | 0:be41a15e7a86 | 66 | * @param[in] x0 The horizontal position in pixels at which to render the bitmap |
eencae | 0:be41a15e7a86 | 67 | * @param[in] y0 The vertical position in pixels at which to render the bitmap |
eencae | 0:be41a15e7a86 | 68 | * |
eencae | 0:be41a15e7a86 | 69 | * @details Note that x0, y0 gives the location of the top-left of the bitmap on |
eencae | 0:be41a15e7a86 | 70 | * the screen. |
eencae | 0:be41a15e7a86 | 71 | * This function only updates the buffer on the screen. You still need |
eencae | 0:be41a15e7a86 | 72 | * to refresh the screen in order to actually see the bitmap. |
eencae | 0:be41a15e7a86 | 73 | */ |
eencae | 0:be41a15e7a86 | 74 | void Bitmap::render(N5110 &lcd, |
eencae | 0:be41a15e7a86 | 75 | unsigned int const x0, |
eencae | 0:be41a15e7a86 | 76 | unsigned int const y0) const |
eencae | 0:be41a15e7a86 | 77 | { |
eencae | 0:be41a15e7a86 | 78 | // Loop through each row of the bitmap image |
eencae | 0:be41a15e7a86 | 79 | for (unsigned int bitmap_row = 0; bitmap_row < _height; ++bitmap_row) |
eencae | 0:be41a15e7a86 | 80 | { |
eencae | 0:be41a15e7a86 | 81 | // Row index on the screen for rendering the row of pixels |
eencae | 0:be41a15e7a86 | 82 | unsigned int screen_row = y0 + bitmap_row; |
eencae | 0:be41a15e7a86 | 83 | |
eencae | 0:be41a15e7a86 | 84 | // Render each pixel in the row |
eencae | 0:be41a15e7a86 | 85 | for (unsigned int bitmap_col = 0; bitmap_col < _width; ++bitmap_col) |
eencae | 0:be41a15e7a86 | 86 | { |
eencae | 0:be41a15e7a86 | 87 | // Column index on the screen for rendering this pixel |
eencae | 0:be41a15e7a86 | 88 | int screen_col = x0 + bitmap_col; |
eencae | 0:be41a15e7a86 | 89 | |
eencae | 0:be41a15e7a86 | 90 | // Find the required value of the pixel at the given location within |
eencae | 0:be41a15e7a86 | 91 | // the bitmap data and then write it to the LCD screen |
eencae | 0:be41a15e7a86 | 92 | int pixel = get_pixel(bitmap_row, bitmap_col); |
eencae | 0:be41a15e7a86 | 93 | lcd.setPixel(screen_col, screen_row, pixel); |
eencae | 0:be41a15e7a86 | 94 | } |
eencae | 0:be41a15e7a86 | 95 | } |
eencae | 0:be41a15e7a86 | 96 | } |