Chris Taylor / Mbed 2 deprecated RETRO-CityRally

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Bitmap4bpp.cpp Source File

Bitmap4bpp.cpp

00001 #include "mbed.h"
00002 #include "Bitmap4bpp.h"
00003 
00004 Bitmap4bpp::Bitmap4bpp(uint16_t width, uint16_t height) :
00005     _width(width), 
00006     _height(height),
00007     _stride((width >> 1) + (width & 0x01)),
00008     _pBitmapData(new uint8_t[_stride * height])    
00009 {
00010     
00011 }
00012 
00013 Bitmap4bpp::~Bitmap4bpp()
00014 {
00015     if (_pBitmapData != NULL)
00016     {
00017         delete []_pBitmapData;
00018         _pBitmapData = NULL;
00019     }
00020 }
00021 
00022 void Bitmap4bpp::clear()
00023 {
00024     memset(_pBitmapData, 0, _stride * _height);
00025 }
00026 
00027 void Bitmap4bpp::setPixel(int16_t x, int16_t y, uint16_t color)
00028 {
00029     if (x < 0 || x >= _width || y < 0 || y >= _height) return;
00030     
00031     int bit = (x % 2) << 2;
00032     uint8_t mask = (color  & 0x0f) << (4 - bit);
00033     uint8_t *p = _pBitmapData + ((y * _stride) + (x / 2));
00034     
00035     *p &= ~mask; 
00036     *p |= mask;
00037 }
00038