Emulation of the 1970's Chip-8 machine. The emulator has 7 games that are unmodified from the original Chip-8 format.

Dependencies:   mbed

Committer:
taylorza
Date:
Sun Feb 08 01:58:57 2015 +0000
Revision:
0:bc3f11b1b41f
Chip-8 Emulator

Who changed what in which revision?

UserRevisionLine numberNew contents of line
taylorza 0:bc3f11b1b41f 1 #include "Base.h"
taylorza 0:bc3f11b1b41f 2
taylorza 0:bc3f11b1b41f 3 #ifndef __CANVAS_H__
taylorza 0:bc3f11b1b41f 4 #define __CANVAS_H__
taylorza 0:bc3f11b1b41f 5
taylorza 0:bc3f11b1b41f 6 template <typename T>
taylorza 0:bc3f11b1b41f 7 class Canvas
taylorza 0:bc3f11b1b41f 8 {
taylorza 0:bc3f11b1b41f 9 public:
taylorza 0:bc3f11b1b41f 10 Canvas(T &surface) :
taylorza 0:bc3f11b1b41f 11 _surface(surface)
taylorza 0:bc3f11b1b41f 12 {
taylorza 0:bc3f11b1b41f 13
taylorza 0:bc3f11b1b41f 14 }
taylorza 0:bc3f11b1b41f 15
taylorza 0:bc3f11b1b41f 16 inline void clear()
taylorza 0:bc3f11b1b41f 17 {
taylorza 0:bc3f11b1b41f 18 _surface.clear();
taylorza 0:bc3f11b1b41f 19 }
taylorza 0:bc3f11b1b41f 20
taylorza 0:bc3f11b1b41f 21 inline void setPixel(int x, int y, uint16_t color)
taylorza 0:bc3f11b1b41f 22 {
taylorza 0:bc3f11b1b41f 23 _surface.setPixel(x, y, color);
taylorza 0:bc3f11b1b41f 24 }
taylorza 0:bc3f11b1b41f 25
taylorza 0:bc3f11b1b41f 26 inline uint16_t getPixel(int x, int y)
taylorza 0:bc3f11b1b41f 27 {
taylorza 0:bc3f11b1b41f 28 return _surface.getPixel(x, y);
taylorza 0:bc3f11b1b41f 29 }
taylorza 0:bc3f11b1b41f 30
taylorza 0:bc3f11b1b41f 31 void drawLine(int x1, int y1, int x2, int y2, uint16_t color)
taylorza 0:bc3f11b1b41f 32 {
taylorza 0:bc3f11b1b41f 33 int dx = abs(x2 - x1);
taylorza 0:bc3f11b1b41f 34 int dy = abs(y2 - y1);
taylorza 0:bc3f11b1b41f 35
taylorza 0:bc3f11b1b41f 36 if (dx == 0)
taylorza 0:bc3f11b1b41f 37 {
taylorza 0:bc3f11b1b41f 38 if (y1 > y2) swap(y1, y2);
taylorza 0:bc3f11b1b41f 39 _surface.fastVLine(y1, y2, x1, color);
taylorza 0:bc3f11b1b41f 40 return;
taylorza 0:bc3f11b1b41f 41 }
taylorza 0:bc3f11b1b41f 42 else if(dy == 0)
taylorza 0:bc3f11b1b41f 43 {
taylorza 0:bc3f11b1b41f 44 if (x1 > x2) swap(x1, x2);
taylorza 0:bc3f11b1b41f 45 _surface.fastHLine(x1, x2, y1, color);
taylorza 0:bc3f11b1b41f 46 return;
taylorza 0:bc3f11b1b41f 47 }
taylorza 0:bc3f11b1b41f 48
taylorza 0:bc3f11b1b41f 49 int sx = (x1 < x2) ? 1 : -1;
taylorza 0:bc3f11b1b41f 50 int sy = (y1 < y2) ? 1 : -1;
taylorza 0:bc3f11b1b41f 51 int err = dx - dy;
taylorza 0:bc3f11b1b41f 52 while(x1 != x2 || y1 != y2)
taylorza 0:bc3f11b1b41f 53 {
taylorza 0:bc3f11b1b41f 54 setPixel(x1, y1, color);
taylorza 0:bc3f11b1b41f 55 int e2 = err << 1;
taylorza 0:bc3f11b1b41f 56 if (e2 > -dy)
taylorza 0:bc3f11b1b41f 57 {
taylorza 0:bc3f11b1b41f 58 err -= dy;
taylorza 0:bc3f11b1b41f 59 x1 += sx;
taylorza 0:bc3f11b1b41f 60 }
taylorza 0:bc3f11b1b41f 61 if (e2 < dx)
taylorza 0:bc3f11b1b41f 62 {
taylorza 0:bc3f11b1b41f 63 err += dx;
taylorza 0:bc3f11b1b41f 64 y1 += sy;
taylorza 0:bc3f11b1b41f 65 }
taylorza 0:bc3f11b1b41f 66 }
taylorza 0:bc3f11b1b41f 67 setPixel(x2, y2, color);
taylorza 0:bc3f11b1b41f 68 }
taylorza 0:bc3f11b1b41f 69
taylorza 0:bc3f11b1b41f 70 void drawRect(int x1, int y1, int x2, int y2, uint16_t color)
taylorza 0:bc3f11b1b41f 71 {
taylorza 0:bc3f11b1b41f 72 if (x1 > x2) swap(x1, x2);
taylorza 0:bc3f11b1b41f 73 if (y1 > y2) swap(y1, y2);
taylorza 0:bc3f11b1b41f 74
taylorza 0:bc3f11b1b41f 75 _surface.fastHLine(x1, x2, y1, color);
taylorza 0:bc3f11b1b41f 76 _surface.fastHLine(x1, x2, y2, color);
taylorza 0:bc3f11b1b41f 77
taylorza 0:bc3f11b1b41f 78 _surface.fastVLine(y1, y2, x1, color);
taylorza 0:bc3f11b1b41f 79 _surface.fastVLine(y1, y2, x2, color);
taylorza 0:bc3f11b1b41f 80 }
taylorza 0:bc3f11b1b41f 81
taylorza 0:bc3f11b1b41f 82 void fillRect(int x1, int y1, int x2, int y2, uint16_t color)
taylorza 0:bc3f11b1b41f 83 {
taylorza 0:bc3f11b1b41f 84 if (x1 > x2) swap(x1, x2);
taylorza 0:bc3f11b1b41f 85 if (y1 > y2) swap(y1, y2);
taylorza 0:bc3f11b1b41f 86
taylorza 0:bc3f11b1b41f 87 for (int x = x1; x <= x2; ++x)
taylorza 0:bc3f11b1b41f 88 {
taylorza 0:bc3f11b1b41f 89 _surface.fastVLine(y1, y2, x, color);
taylorza 0:bc3f11b1b41f 90 }
taylorza 0:bc3f11b1b41f 91 }
taylorza 0:bc3f11b1b41f 92
taylorza 0:bc3f11b1b41f 93 void drawBitmap(int x, int y, T &bmp, int srcX, int srcY, int srcWidth, int srcHeight, bool transparent)
taylorza 0:bc3f11b1b41f 94 {
taylorza 0:bc3f11b1b41f 95 _surface.drawBitmap(x, y, bmp, srcX, srcY, srcWidth, srcHeight, transparent);
taylorza 0:bc3f11b1b41f 96 }
taylorza 0:bc3f11b1b41f 97
taylorza 0:bc3f11b1b41f 98 private:
taylorza 0:bc3f11b1b41f 99 T &_surface;
taylorza 0:bc3f11b1b41f 100 };
taylorza 0:bc3f11b1b41f 101
taylorza 0:bc3f11b1b41f 102 #endif // __CANVAS_H__