Brick Breaker
Dependencies: 4DGL-uLCD-SE mbed-rtos mbed
Graphics.h@0:099e4258aba4, 2014-10-21 (annotated)
- Committer:
- hotwheelharry
- Date:
- Tue Oct 21 16:14:11 2014 +0000
- Revision:
- 0:099e4258aba4
Brick Breaker! It's okay
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hotwheelharry | 0:099e4258aba4 | 1 | #include <vector> |
hotwheelharry | 0:099e4258aba4 | 2 | #include <string> |
hotwheelharry | 0:099e4258aba4 | 3 | #include <list> |
hotwheelharry | 0:099e4258aba4 | 4 | |
hotwheelharry | 0:099e4258aba4 | 5 | struct vec2 { |
hotwheelharry | 0:099e4258aba4 | 6 | float x; |
hotwheelharry | 0:099e4258aba4 | 7 | float y; |
hotwheelharry | 0:099e4258aba4 | 8 | }; |
hotwheelharry | 0:099e4258aba4 | 9 | |
hotwheelharry | 0:099e4258aba4 | 10 | class Physics; |
hotwheelharry | 0:099e4258aba4 | 11 | class Renderer; |
hotwheelharry | 0:099e4258aba4 | 12 | |
hotwheelharry | 0:099e4258aba4 | 13 | class Color{ |
hotwheelharry | 0:099e4258aba4 | 14 | public: |
hotwheelharry | 0:099e4258aba4 | 15 | unsigned char r; |
hotwheelharry | 0:099e4258aba4 | 16 | unsigned char g; |
hotwheelharry | 0:099e4258aba4 | 17 | unsigned char b; |
hotwheelharry | 0:099e4258aba4 | 18 | |
hotwheelharry | 0:099e4258aba4 | 19 | Color() : r(0), g(0), b(0) |
hotwheelharry | 0:099e4258aba4 | 20 | {} |
hotwheelharry | 0:099e4258aba4 | 21 | Color(unsigned char r, unsigned char g, unsigned char b) : r(r), g(g), b(b) |
hotwheelharry | 0:099e4258aba4 | 22 | {} |
hotwheelharry | 0:099e4258aba4 | 23 | Color(int c) : r( (c & 0xff0000) >> 16), g( (c & 0xff00) >> 8), b(c & 0xff) |
hotwheelharry | 0:099e4258aba4 | 24 | {} |
hotwheelharry | 0:099e4258aba4 | 25 | |
hotwheelharry | 0:099e4258aba4 | 26 | operator int(){ |
hotwheelharry | 0:099e4258aba4 | 27 | int c = 0; |
hotwheelharry | 0:099e4258aba4 | 28 | return c | (r << 2*8) | (g << 8) | b; |
hotwheelharry | 0:099e4258aba4 | 29 | } |
hotwheelharry | 0:099e4258aba4 | 30 | }; |
hotwheelharry | 0:099e4258aba4 | 31 | |
hotwheelharry | 0:099e4258aba4 | 32 | |
hotwheelharry | 0:099e4258aba4 | 33 | class Block{ |
hotwheelharry | 0:099e4258aba4 | 34 | friend Physics; |
hotwheelharry | 0:099e4258aba4 | 35 | friend Renderer; |
hotwheelharry | 0:099e4258aba4 | 36 | |
hotwheelharry | 0:099e4258aba4 | 37 | private: |
hotwheelharry | 0:099e4258aba4 | 38 | |
hotwheelharry | 0:099e4258aba4 | 39 | |
hotwheelharry | 0:099e4258aba4 | 40 | vec2 next_coord; |
hotwheelharry | 0:099e4258aba4 | 41 | vec2 next_size; |
hotwheelharry | 0:099e4258aba4 | 42 | vec2 next_velocity; |
hotwheelharry | 0:099e4258aba4 | 43 | |
hotwheelharry | 0:099e4258aba4 | 44 | public: |
hotwheelharry | 0:099e4258aba4 | 45 | bool updated; |
hotwheelharry | 0:099e4258aba4 | 46 | bool locked; //fixed position |
hotwheelharry | 0:099e4258aba4 | 47 | bool interactive; //collidable in physics |
hotwheelharry | 0:099e4258aba4 | 48 | bool render; |
hotwheelharry | 0:099e4258aba4 | 49 | vec2 coord; |
hotwheelharry | 0:099e4258aba4 | 50 | vec2 size; |
hotwheelharry | 0:099e4258aba4 | 51 | vec2 velocity; |
hotwheelharry | 0:099e4258aba4 | 52 | Color color; |
hotwheelharry | 0:099e4258aba4 | 53 | |
hotwheelharry | 0:099e4258aba4 | 54 | vec2 prev_coord; |
hotwheelharry | 0:099e4258aba4 | 55 | vec2 prev_size; |
hotwheelharry | 0:099e4258aba4 | 56 | |
hotwheelharry | 0:099e4258aba4 | 57 | Block() : updated(true), locked(false), interactive(true), render(true) |
hotwheelharry | 0:099e4258aba4 | 58 | { |
hotwheelharry | 0:099e4258aba4 | 59 | coord.x = 0; |
hotwheelharry | 0:099e4258aba4 | 60 | coord.y = 0; |
hotwheelharry | 0:099e4258aba4 | 61 | velocity.x = 0; |
hotwheelharry | 0:099e4258aba4 | 62 | velocity.y = 0; |
hotwheelharry | 0:099e4258aba4 | 63 | color = RED; |
hotwheelharry | 0:099e4258aba4 | 64 | size.x = 2; |
hotwheelharry | 0:099e4258aba4 | 65 | size.y = 2; |
hotwheelharry | 0:099e4258aba4 | 66 | } |
hotwheelharry | 0:099e4258aba4 | 67 | |
hotwheelharry | 0:099e4258aba4 | 68 | virtual bool touching(const Block& b){ |
hotwheelharry | 0:099e4258aba4 | 69 | if ( coord.x < b.coord.x + b.size.x && |
hotwheelharry | 0:099e4258aba4 | 70 | coord.x + size.x > b.coord.x && |
hotwheelharry | 0:099e4258aba4 | 71 | coord.y < b.coord.y + b.size.y && |
hotwheelharry | 0:099e4258aba4 | 72 | size.y + coord.y > b.coord.y |
hotwheelharry | 0:099e4258aba4 | 73 | ) |
hotwheelharry | 0:099e4258aba4 | 74 | { |
hotwheelharry | 0:099e4258aba4 | 75 | if(this == &b){ |
hotwheelharry | 0:099e4258aba4 | 76 | return false; |
hotwheelharry | 0:099e4258aba4 | 77 | } |
hotwheelharry | 0:099e4258aba4 | 78 | return true; |
hotwheelharry | 0:099e4258aba4 | 79 | } |
hotwheelharry | 0:099e4258aba4 | 80 | return false; |
hotwheelharry | 0:099e4258aba4 | 81 | } |
hotwheelharry | 0:099e4258aba4 | 82 | virtual void next_swap(){ |
hotwheelharry | 0:099e4258aba4 | 83 | coord = next_coord; |
hotwheelharry | 0:099e4258aba4 | 84 | size = next_size; |
hotwheelharry | 0:099e4258aba4 | 85 | velocity = next_velocity; |
hotwheelharry | 0:099e4258aba4 | 86 | } |
hotwheelharry | 0:099e4258aba4 | 87 | void store_prev(){ |
hotwheelharry | 0:099e4258aba4 | 88 | prev_coord = coord; |
hotwheelharry | 0:099e4258aba4 | 89 | prev_size = size; |
hotwheelharry | 0:099e4258aba4 | 90 | } |
hotwheelharry | 0:099e4258aba4 | 91 | void copy(){ |
hotwheelharry | 0:099e4258aba4 | 92 | prev_coord = coord; |
hotwheelharry | 0:099e4258aba4 | 93 | prev_size = size; |
hotwheelharry | 0:099e4258aba4 | 94 | |
hotwheelharry | 0:099e4258aba4 | 95 | next_coord = coord; |
hotwheelharry | 0:099e4258aba4 | 96 | next_size = size; |
hotwheelharry | 0:099e4258aba4 | 97 | next_velocity = velocity; |
hotwheelharry | 0:099e4258aba4 | 98 | } |
hotwheelharry | 0:099e4258aba4 | 99 | }; |
hotwheelharry | 0:099e4258aba4 | 100 | |
hotwheelharry | 0:099e4258aba4 | 101 | class Text{ |
hotwheelharry | 0:099e4258aba4 | 102 | public: |
hotwheelharry | 0:099e4258aba4 | 103 | bool updated; |
hotwheelharry | 0:099e4258aba4 | 104 | std::string message; |
hotwheelharry | 0:099e4258aba4 | 105 | vec2 coord; |
hotwheelharry | 0:099e4258aba4 | 106 | Color color; |
hotwheelharry | 0:099e4258aba4 | 107 | int font; |
hotwheelharry | 0:099e4258aba4 | 108 | Text() : color(0xffffff), updated(true) |
hotwheelharry | 0:099e4258aba4 | 109 | { |
hotwheelharry | 0:099e4258aba4 | 110 | } |
hotwheelharry | 0:099e4258aba4 | 111 | }; |
hotwheelharry | 0:099e4258aba4 | 112 | class Scene{ |
hotwheelharry | 0:099e4258aba4 | 113 | public: |
hotwheelharry | 0:099e4258aba4 | 114 | int lives; |
hotwheelharry | 0:099e4258aba4 | 115 | int score; |
hotwheelharry | 0:099e4258aba4 | 116 | bool beat; |
hotwheelharry | 0:099e4258aba4 | 117 | |
hotwheelharry | 0:099e4258aba4 | 118 | Color bg; |
hotwheelharry | 0:099e4258aba4 | 119 | |
hotwheelharry | 0:099e4258aba4 | 120 | Block paddle; |
hotwheelharry | 0:099e4258aba4 | 121 | Block ball; |
hotwheelharry | 0:099e4258aba4 | 122 | std::list<Block> blocks; |
hotwheelharry | 0:099e4258aba4 | 123 | std::vector<Text> text; |
hotwheelharry | 0:099e4258aba4 | 124 | }; |
hotwheelharry | 0:099e4258aba4 | 125 | |
hotwheelharry | 0:099e4258aba4 | 126 | |
hotwheelharry | 0:099e4258aba4 | 127 | class Renderer{ |
hotwheelharry | 0:099e4258aba4 | 128 | private: |
hotwheelharry | 0:099e4258aba4 | 129 | public: |
hotwheelharry | 0:099e4258aba4 | 130 | uLCD_4DGL& g; |
hotwheelharry | 0:099e4258aba4 | 131 | Color background; |
hotwheelharry | 0:099e4258aba4 | 132 | vec2 screen; |
hotwheelharry | 0:099e4258aba4 | 133 | |
hotwheelharry | 0:099e4258aba4 | 134 | Renderer(uLCD_4DGL& gfx, vec2 screen) : g(gfx), screen(screen) |
hotwheelharry | 0:099e4258aba4 | 135 | { |
hotwheelharry | 0:099e4258aba4 | 136 | } |
hotwheelharry | 0:099e4258aba4 | 137 | void render(Scene s){ |
hotwheelharry | 0:099e4258aba4 | 138 | //g.cls(); |
hotwheelharry | 0:099e4258aba4 | 139 | |
hotwheelharry | 0:099e4258aba4 | 140 | // draw blocks |
hotwheelharry | 0:099e4258aba4 | 141 | for(std::list<Block>::iterator b = s.blocks.begin(); b != s.blocks.end(); b++){ |
hotwheelharry | 0:099e4258aba4 | 142 | if(b->updated){ |
hotwheelharry | 0:099e4258aba4 | 143 | //draw over where it was |
hotwheelharry | 0:099e4258aba4 | 144 | //g.filled_rectangle(b->prev_coord.x, b->prev_coord.y, b->prev_coord.x+b->prev_size.x, b->prev_coord.y+b->prev_size.y, background); //x1,y1,x2,y2,color |
hotwheelharry | 0:099e4258aba4 | 145 | //draw new location |
hotwheelharry | 0:099e4258aba4 | 146 | g.filled_rectangle(b->coord.x, b->coord.y, b->coord.x+b->size.x, b->coord.y+b->size.y, b->color); //x1,y1,x2,y2,color |
hotwheelharry | 0:099e4258aba4 | 147 | |
hotwheelharry | 0:099e4258aba4 | 148 | b->updated = false; |
hotwheelharry | 0:099e4258aba4 | 149 | } |
hotwheelharry | 0:099e4258aba4 | 150 | } |
hotwheelharry | 0:099e4258aba4 | 151 | |
hotwheelharry | 0:099e4258aba4 | 152 | |
hotwheelharry | 0:099e4258aba4 | 153 | |
hotwheelharry | 0:099e4258aba4 | 154 | //draw paddle |
hotwheelharry | 0:099e4258aba4 | 155 | { |
hotwheelharry | 0:099e4258aba4 | 156 | Block* b = &(s.paddle); |
hotwheelharry | 0:099e4258aba4 | 157 | if(b->updated){ |
hotwheelharry | 0:099e4258aba4 | 158 | //draw over where it was |
hotwheelharry | 0:099e4258aba4 | 159 | g.filled_rectangle(b->prev_coord.x, b->prev_coord.y, b->prev_coord.x+b->prev_size.x, b->prev_coord.y+b->prev_size.y, background); //x1,y1,x2,y2,color |
hotwheelharry | 0:099e4258aba4 | 160 | //draw new location |
hotwheelharry | 0:099e4258aba4 | 161 | g.filled_rectangle(b->coord.x, b->coord.y, b->coord.x+b->size.x, b->coord.y+b->size.y, b->color); //x1,y1,x2,y2,color |
hotwheelharry | 0:099e4258aba4 | 162 | |
hotwheelharry | 0:099e4258aba4 | 163 | b->updated = false; |
hotwheelharry | 0:099e4258aba4 | 164 | } |
hotwheelharry | 0:099e4258aba4 | 165 | } |
hotwheelharry | 0:099e4258aba4 | 166 | |
hotwheelharry | 0:099e4258aba4 | 167 | |
hotwheelharry | 0:099e4258aba4 | 168 | //draw ball |
hotwheelharry | 0:099e4258aba4 | 169 | { |
hotwheelharry | 0:099e4258aba4 | 170 | Block* b = &(s.ball); |
hotwheelharry | 0:099e4258aba4 | 171 | if(b->updated){ |
hotwheelharry | 0:099e4258aba4 | 172 | //draw over where it was |
hotwheelharry | 0:099e4258aba4 | 173 | g.filled_rectangle(b->prev_coord.x, b->prev_coord.y, b->prev_coord.x+b->prev_size.x, b->prev_coord.y+b->prev_size.y, background); //x1,y1,x2,y2,color |
hotwheelharry | 0:099e4258aba4 | 174 | //draw new location |
hotwheelharry | 0:099e4258aba4 | 175 | g.filled_rectangle(b->coord.x, b->coord.y, b->coord.x+b->size.x, b->coord.y+b->size.y, b->color); //x1,y1,x2,y2,color |
hotwheelharry | 0:099e4258aba4 | 176 | |
hotwheelharry | 0:099e4258aba4 | 177 | b->updated = false; |
hotwheelharry | 0:099e4258aba4 | 178 | } |
hotwheelharry | 0:099e4258aba4 | 179 | } |
hotwheelharry | 0:099e4258aba4 | 180 | |
hotwheelharry | 0:099e4258aba4 | 181 | |
hotwheelharry | 0:099e4258aba4 | 182 | //draw text |
hotwheelharry | 0:099e4258aba4 | 183 | for(size_t i = 0; i < s.text.size(); i++){ |
hotwheelharry | 0:099e4258aba4 | 184 | Text& t = s.text[i]; |
hotwheelharry | 0:099e4258aba4 | 185 | if(t.updated){ |
hotwheelharry | 0:099e4258aba4 | 186 | g.set_font(t.font); |
hotwheelharry | 0:099e4258aba4 | 187 | g.color(t.color); |
hotwheelharry | 0:099e4258aba4 | 188 | g.locate(t.coord.x, t.coord.y); |
hotwheelharry | 0:099e4258aba4 | 189 | g.printf(t.message.c_str()); |
hotwheelharry | 0:099e4258aba4 | 190 | t.updated = false; |
hotwheelharry | 0:099e4258aba4 | 191 | } |
hotwheelharry | 0:099e4258aba4 | 192 | } |
hotwheelharry | 0:099e4258aba4 | 193 | |
hotwheelharry | 0:099e4258aba4 | 194 | } |
hotwheelharry | 0:099e4258aba4 | 195 | }; |