Race around the city collecting the flags while avoiding those that stand in the way of your mission. Make no mistake you will need to be quick to outwit your opponents, they are smart and will try to box you in. I wrote this game to prove that writing a game with scrolling scenery is possible even with the limited 6kB of RAM available. I had to compromise sound effects for features, I wanted multiple opponents, I wanted to be able to drop smoke bombs to trap the opponents but all this required memory so the sound effects had to take a back seat.

Dependencies:   mbed

LCD_ST7735/Canvas.h

Committer:
taylorza
Date:
2015-02-01
Revision:
1:1b8125937f28
Parent:
0:d85c449aca6d

File content as of revision 1:1b8125937f28:

#include "Base.h"

#ifndef __CANVAS_H__
#define __CANVAS_H__

template <typename T>
class Canvas
{
    public:
        Canvas(T *pSurface)
        {
            _pSurface = pSurface;
        }
        
        inline void clear()
        {
            _pSurface->clear();
        }
        
        inline void setPixel(int x, int y, uint16_t color)
        {
            _pSurface->setPixel(x, y, color);
        }
        
        void drawLine(int x1, int y1, int x2, int y2, uint16_t color)
        {
            int dx = abs(x2 - x1);
            int dy = abs(y2 - y1);
            
            if (dx == 0) 
            {
                if (y1 > y2) swap(y1, y2);
                drawVertLine(x1, y1, y2, color);
                return;
            }
            else if(dy == 0)
            {
                if (x1 > x2) swap(x1, x2);
                drawHorizLine(x1, y1, x2, color);
                return;
            }
           
            int sx = (x1 < x2) ? 1 : -1;
            int sy = (y1 < y2) ? 1 : -1;
            int err = dx - dy;
            while(x1 != x2 || y1 != y2)
            {
                setPixel(x1, y1, color);
                int e2 = err << 1;
                if (e2 > -dy)
                {
                    err -= dy;
                    x1 += sx;            
                }
                if (e2 < dx)
                {
                    err += dx;
                    y1 += sy;
                }
            }
            setPixel(x2, y2, color);
        }
        
        void drawRect(int x1, int y1, int x2, int y2, uint16_t color)
        {
            if (x1 > x2) swap(x1, x2);
            if (y1 > y2) swap(y1, y2);
            
            drawHorizLine(x1, y1, x2, color);
            drawHorizLine(x1, y2, x2, color);
            drawVertLine(x1, y1, y2, color);
            drawVertLine(x2, y1, y2, color);
        }
        
        void fillRect(int x1, int y1, int x2, int y2, uint16_t color)
        {
            if (x1 > x2) swap(x1, x2);
            if (y1 > y2) swap(y1, y2);
            
            for (int x = x1; x <= x2; ++x)
            {
                _pSurface->fastVLine(y1, y2, x, color);
            }
        }
        
        void drawBitmap(int x, int y, T &bmp, int srcX, int srcY, int srcWidth, int srcHeight, bool transparent)
        {
            _pSurface->drawBitmap(x, y, bmp, srcX, srcY, srcWidth, srcHeight, transparent);
        }
    
    private:
        void drawHorizLine(int x1, int y1, int x2, uint16_t color)
        {            
            for(int x = x1; x <= x2; ++x) setPixel(x, y1, color);
        }
        
        void drawVertLine(int x1, int y1, int y2, uint16_t color)
        {            
            for(int y = y1; y <= y2; ++y) setPixel(x1, y, color);
        }
           
    private:
        T *_pSurface;
};

#endif // __CANVAS_H__