Yang Junyao 201090191
Bitmap.cpp@38:92fad278c2c3, 2017-03-08 (annotated)
- Committer:
- valavanisalex
- Date:
- Wed Mar 08 14:12:42 2017 +0000
- Revision:
- 38:92fad278c2c3
- Child:
- 40:c9262294f2e1
Added Bitmap class to simplify sprite support
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
valavanisalex | 38:92fad278c2c3 | 1 | #include "Bitmap.h" |
valavanisalex | 38:92fad278c2c3 | 2 | |
valavanisalex | 38:92fad278c2c3 | 3 | #include <iostream> |
valavanisalex | 38:92fad278c2c3 | 4 | |
valavanisalex | 38:92fad278c2c3 | 5 | Bitmap::Bitmap(std::vector<int> const &contents, |
valavanisalex | 38:92fad278c2c3 | 6 | unsigned int const height, |
valavanisalex | 38:92fad278c2c3 | 7 | unsigned int const width) |
valavanisalex | 38:92fad278c2c3 | 8 | : |
valavanisalex | 38:92fad278c2c3 | 9 | _contents(contents), |
valavanisalex | 38:92fad278c2c3 | 10 | _height(height), |
valavanisalex | 38:92fad278c2c3 | 11 | _width(width) |
valavanisalex | 38:92fad278c2c3 | 12 | { |
valavanisalex | 38:92fad278c2c3 | 13 | // Perform a quick sanity check of the dimensions |
valavanisalex | 38:92fad278c2c3 | 14 | if (contents.size() != height * height) { |
valavanisalex | 38:92fad278c2c3 | 15 | std::cerr << "Contents of bitmap has size " << contents.size() |
valavanisalex | 38:92fad278c2c3 | 16 | << " pixels, but its dimensions were specified as " |
valavanisalex | 38:92fad278c2c3 | 17 | << width << " * " << height << " = " << width * height; |
valavanisalex | 38:92fad278c2c3 | 18 | } |
valavanisalex | 38:92fad278c2c3 | 19 | } |
valavanisalex | 38:92fad278c2c3 | 20 | |
valavanisalex | 38:92fad278c2c3 | 21 | int Bitmap::get_pixel(unsigned int const row, |
valavanisalex | 38:92fad278c2c3 | 22 | unsigned int const column) const |
valavanisalex | 38:92fad278c2c3 | 23 | { |
valavanisalex | 38:92fad278c2c3 | 24 | // First check that row and column indices are within bounds |
valavanisalex | 38:92fad278c2c3 | 25 | if(column >= _width || row >= _height) |
valavanisalex | 38:92fad278c2c3 | 26 | { |
valavanisalex | 38:92fad278c2c3 | 27 | std::cerr << "The requested pixel with index " << row << "," << column |
valavanisalex | 38:92fad278c2c3 | 28 | << "is outside the bitmap dimensions: " << _width << "," |
valavanisalex | 38:92fad278c2c3 | 29 | << _height; |
valavanisalex | 38:92fad278c2c3 | 30 | } |
valavanisalex | 38:92fad278c2c3 | 31 | |
valavanisalex | 38:92fad278c2c3 | 32 | // Now return the pixel value, using row-major indexing |
valavanisalex | 38:92fad278c2c3 | 33 | return _contents[row * _width + column]; |
valavanisalex | 38:92fad278c2c3 | 34 | } |