3-D snake game for the mbed application board.

Dependencies:   C12832 MMA7660 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GraphicsDisplay.cpp Source File

GraphicsDisplay.cpp

00001 /* mbed GraphicsDisplay Display Library Base Class
00002  * Copyright (c) 2007-2009 sford
00003  * Released under the MIT License: http://mbed.org/license/mit
00004  */
00005  
00006 #include "GraphicsDisplay.h"
00007 
00008 const unsigned char FONT8x8[97][8] = {
00009 0x08,0x08,0x08,0x00,0x00,0x00,0x00,0x00, // columns, rows, num_bytes_per_char
00010 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // space 0x20
00011 0x30,0x78,0x78,0x30,0x30,0x00,0x30,0x00, // !
00012 0x6C,0x6C,0x6C,0x00,0x00,0x00,0x00,0x00, // "
00013 0x6C,0x6C,0xFE,0x6C,0xFE,0x6C,0x6C,0x00, // #
00014 0x18,0x3E,0x60,0x3C,0x06,0x7C,0x18,0x00, // $
00015 0x00,0x63,0x66,0x0C,0x18,0x33,0x63,0x00, // %
00016 0x1C,0x36,0x1C,0x3B,0x6E,0x66,0x3B,0x00, // &
00017 0x30,0x30,0x60,0x00,0x00,0x00,0x00,0x00, // '
00018 0x0C,0x18,0x30,0x30,0x30,0x18,0x0C,0x00, // (
00019 0x30,0x18,0x0C,0x0C,0x0C,0x18,0x30,0x00, // )
00020 0x00,0x66,0x3C,0xFF,0x3C,0x66,0x00,0x00, // *
00021 0x00,0x30,0x30,0xFC,0x30,0x30,0x00,0x00, // +
00022 0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x30, // ,
00023 0x00,0x00,0x00,0x7E,0x00,0x00,0x00,0x00, // -
00024 0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00, // .
00025 0x03,0x06,0x0C,0x18,0x30,0x60,0x40,0x00, // / (forward slash)
00026 0x3E,0x63,0x63,0x6B,0x63,0x63,0x3E,0x00, // 0 0x30
00027 0x18,0x38,0x58,0x18,0x18,0x18,0x7E,0x00, // 1
00028 0x3C,0x66,0x06,0x1C,0x30,0x66,0x7E,0x00, // 2
00029 0x3C,0x66,0x06,0x1C,0x06,0x66,0x3C,0x00, // 3
00030 0x0E,0x1E,0x36,0x66,0x7F,0x06,0x0F,0x00, // 4
00031 0x7E,0x60,0x7C,0x06,0x06,0x66,0x3C,0x00, // 5
00032 0x1C,0x30,0x60,0x7C,0x66,0x66,0x3C,0x00, // 6
00033 0x7E,0x66,0x06,0x0C,0x18,0x18,0x18,0x00, // 7
00034 0x3C,0x66,0x66,0x3C,0x66,0x66,0x3C,0x00, // 8
00035 0x3C,0x66,0x66,0x3E,0x06,0x0C,0x38,0x00, // 9
00036 0x00,0x18,0x18,0x00,0x00,0x18,0x18,0x00, // :
00037 0x00,0x18,0x18,0x00,0x00,0x18,0x18,0x30, // ;
00038 0x0C,0x18,0x30,0x60,0x30,0x18,0x0C,0x00, // <
00039 0x00,0x00,0x7E,0x00,0x00,0x7E,0x00,0x00, // =
00040 0x30,0x18,0x0C,0x06,0x0C,0x18,0x30,0x00, // >
00041 0x3C,0x66,0x06,0x0C,0x18,0x00,0x18,0x00, // ?
00042 0x3E,0x63,0x6F,0x69,0x6F,0x60,0x3E,0x00, // @ 0x40
00043 0x18,0x3C,0x66,0x66,0x7E,0x66,0x66,0x00, // A
00044 0x7E,0x33,0x33,0x3E,0x33,0x33,0x7E,0x00, // B
00045 0x1E,0x33,0x60,0x60,0x60,0x33,0x1E,0x00, // C
00046 0x7C,0x36,0x33,0x33,0x33,0x36,0x7C,0x00, // D
00047 0x7F,0x31,0x34,0x3C,0x34,0x31,0x7F,0x00, // E
00048 0x7F,0x31,0x34,0x3C,0x34,0x30,0x78,0x00, // F
00049 0x1E,0x33,0x60,0x60,0x67,0x33,0x1F,0x00, // G
00050 0x66,0x66,0x66,0x7E,0x66,0x66,0x66,0x00, // H
00051 0x3C,0x18,0x18,0x18,0x18,0x18,0x3C,0x00, // I
00052 0x0F,0x06,0x06,0x06,0x66,0x66,0x3C,0x00, // J
00053 0x73,0x33,0x36,0x3C,0x36,0x33,0x73,0x00, // K
00054 0x78,0x30,0x30,0x30,0x31,0x33,0x7F,0x00, // L
00055 0x63,0x77,0x7F,0x7F,0x6B,0x63,0x63,0x00, // M
00056 0x63,0x73,0x7B,0x6F,0x67,0x63,0x63,0x00, // N
00057 0x3E,0x63,0x63,0x63,0x63,0x63,0x3E,0x00, // O
00058 0x7E,0x33,0x33,0x3E,0x30,0x30,0x78,0x00, // P 0x50
00059 0x3C,0x66,0x66,0x66,0x6E,0x3C,0x0E,0x00, // Q
00060 0x7E,0x33,0x33,0x3E,0x36,0x33,0x73,0x00, // R
00061 0x3C,0x66,0x30,0x18,0x0C,0x66,0x3C,0x00, // S
00062 0x7E,0x5A,0x18,0x18,0x18,0x18,0x3C,0x00, // T
00063 0x66,0x66,0x66,0x66,0x66,0x66,0x7E,0x00, // U
00064 0x66,0x66,0x66,0x66,0x66,0x3C,0x18,0x00, // V
00065 0x63,0x63,0x63,0x6B,0x7F,0x77,0x63,0x00, // W
00066 0x63,0x63,0x36,0x1C,0x1C,0x36,0x63,0x00, // X
00067 0x66,0x66,0x66,0x3C,0x18,0x18,0x3C,0x00, // Y
00068 0x7F,0x63,0x46,0x0C,0x19,0x33,0x7F,0x00, // Z
00069 0x3C,0x30,0x30,0x30,0x30,0x30,0x3C,0x00, // [
00070 0x60,0x30,0x18,0x0C,0x06,0x03,0x01,0x00, // \ (back slash)
00071 0x3C,0x0C,0x0C,0x0C,0x0C,0x0C,0x3C,0x00, // ]
00072 0x08,0x1C,0x36,0x63,0x00,0x00,0x00,0x00, // ^
00073 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF, // _
00074 0x18,0x18,0x0C,0x00,0x00,0x00,0x00,0x00, // ` 0x60
00075 0x00,0x00,0x3C,0x06,0x3E,0x66,0x3B,0x00, // a
00076 0x70,0x30,0x3E,0x33,0x33,0x33,0x6E,0x00, // b
00077 0x00,0x00,0x3C,0x66,0x60,0x66,0x3C,0x00, // c
00078 0x0E,0x06,0x3E,0x66,0x66,0x66,0x3B,0x00, // d
00079 0x00,0x00,0x3C,0x66,0x7E,0x60,0x3C,0x00, // e
00080 0x1C,0x36,0x30,0x78,0x30,0x30,0x78,0x00, // f
00081 0x00,0x00,0x3B,0x66,0x66,0x3E,0x06,0x7C, // g
00082 0x70,0x30,0x36,0x3B,0x33,0x33,0x73,0x00, // h
00083 0x18,0x00,0x38,0x18,0x18,0x18,0x3C,0x00, // i
00084 0x06,0x00,0x06,0x06,0x06,0x66,0x66,0x3C, // j
00085 0x70,0x30,0x33,0x36,0x3C,0x36,0x73,0x00, // k
00086 0x38,0x18,0x18,0x18,0x18,0x18,0x3C,0x00, // l
00087 0x00,0x00,0x66,0x7F,0x7F,0x6B,0x63,0x00, // m
00088 0x00,0x00,0x7C,0x66,0x66,0x66,0x66,0x00, // n
00089 0x00,0x00,0x3C,0x66,0x66,0x66,0x3C,0x00, // o
00090 0x00,0x00,0x6E,0x33,0x33,0x3E,0x30,0x78, // p
00091 0x00,0x00,0x3B,0x66,0x66,0x3E,0x06,0x0F, // q
00092 0x00,0x00,0x6E,0x3B,0x33,0x30,0x78,0x00, // r
00093 0x00,0x00,0x3E,0x60,0x3C,0x06,0x7C,0x00, // s
00094 0x08,0x18,0x3E,0x18,0x18,0x1A,0x0C,0x00, // t
00095 0x00,0x00,0x66,0x66,0x66,0x66,0x3B,0x00, // u
00096 0x00,0x00,0x66,0x66,0x66,0x3C,0x18,0x00, // v
00097 0x00,0x00,0x63,0x6B,0x7F,0x7F,0x36,0x00, // w
00098 0x00,0x00,0x63,0x36,0x1C,0x36,0x63,0x00, // x
00099 0x00,0x00,0x66,0x66,0x66,0x3E,0x06,0x7C, // y
00100 0x00,0x00,0x7E,0x4C,0x18,0x32,0x7E,0x00, // z
00101 0x0E,0x18,0x18,0x70,0x18,0x18,0x0E,0x00, // {
00102 0x0C,0x0C,0x0C,0x00,0x0C,0x0C,0x0C,0x00, // |
00103 0x70,0x18,0x18,0x0E,0x18,0x18,0x70,0x00, // }
00104 0x3B,0x6E,0x00,0x00,0x00,0x00,0x00,0x00, // ~
00105 0x1C,0x36,0x36,0x1C,0x00,0x00,0x00,0x00}; // DEL
00106     
00107 GraphicsDisplay::GraphicsDisplay(const char *name):TextDisplay(name) {
00108     foreground(0xFFFF);
00109     background(0x0000);
00110 }
00111     
00112 void GraphicsDisplay::character(int column, int row, int value) { 
00113     blitbit(column * 8, row * 8, 8, 8, (char*)&(FONT8x8[value - 0x1F][0]));
00114 }
00115 
00116 void GraphicsDisplay::window(int x, int y, int w, int h) {
00117     // current pixel location
00118     _x = x;
00119     _y = y;
00120     // window settings
00121     _x1 = x;
00122     _x2 = x + w - 1;
00123     _y1 = y;
00124     _y2 = y + h - 1;
00125 }
00126     
00127 void GraphicsDisplay::putp(int colour) {
00128     // put pixel at current pixel location
00129     pixel(_x, _y, colour);
00130     // update pixel location based on window settings
00131     _x++;
00132     if(_x > _x2) {
00133         _x = _x1;
00134         _y++;
00135         if(_y > _y2) {
00136             _y = _y1;
00137         }
00138     }
00139 }
00140 
00141 void GraphicsDisplay::fill(int x, int y, int w, int h, int colour) { 
00142     window(x, y, w, h);
00143     for(int i=0; i<w*h; i++) {
00144         putp(colour);
00145     }
00146 }
00147     
00148 void GraphicsDisplay::cls() {
00149     fill(0, 0, width(), height(), _background);
00150 }
00151     
00152 void GraphicsDisplay::blit(int x, int y, int w, int h, const int *colour) { 
00153     window(x, y, w, h);
00154     for(int i=0; i<w*h; i++) {
00155         putp(colour[i]);
00156     }
00157 }
00158     
00159 void GraphicsDisplay::blitbit(int x, int y, int w, int h, const char* colour) {
00160     window(x, y, w, h);
00161     for(int i = 0; i < w*h; i++) {
00162         char byte = colour[i >> 3];
00163         int offset = i & 0x7;
00164         int c = ((byte << offset) & 0x80) ? _foreground : _background;
00165         putp(c);
00166     }
00167 }
00168     
00169 int GraphicsDisplay::columns() { 
00170     return width() / 8; 
00171 }
00172 
00173 int GraphicsDisplay::rows() { 
00174     return height() / 8; 
00175 }
00176