Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of el17ajf by
Graphics/Graphics.cpp@13:59e17cab320a, 2019-03-18 (annotated)
- Committer:
- el17ajf
- Date:
- Mon Mar 18 18:09:57 2019 +0000
- Revision:
- 13:59e17cab320a
- Parent:
- 12:beb0d7632531
- Child:
- 14:53d2167d7c9a
got tetrominos dropping and moving!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17ajf | 5:3efbdcb3efaf | 1 | #include "Graphics.h" |
el17ajf | 5:3efbdcb3efaf | 2 | |
el17ajf | 12:beb0d7632531 | 3 | const int Graphics::SCREEN_HEIGHT = 84; |
el17ajf | 12:beb0d7632531 | 4 | const int Graphics::SCREEN_WIDTH = 48; |
el17ajf | 12:beb0d7632531 | 5 | const int Graphics::BLOCK_SIZE = 4; |
el17ajf | 12:beb0d7632531 | 6 | const int Graphics::BORDER_SIZE = 2; |
el17ajf | 12:beb0d7632531 | 7 | |
el17ajf | 10:5762d7fae033 | 8 | N5110 * Graphics::lcd; |
el17ajf | 10:5762d7fae033 | 9 | |
el17ajf | 8:5066ce13a430 | 10 | void Graphics::init() { |
el17ajf | 8:5066ce13a430 | 11 | lcd = new N5110(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
el17ajf | 9:3a7776a29a11 | 12 | lcd->init(); |
el17ajf | 11:fba7d54fd36b | 13 | lcd->setContrast(0.5); |
el17ajf | 13:59e17cab320a | 14 | lcd->setBrightness(0); |
el17ajf | 8:5066ce13a430 | 15 | } |
el17ajf | 8:5066ce13a430 | 16 | |
el17ajf | 8:5066ce13a430 | 17 | void Graphics::deinit() { |
el17ajf | 8:5066ce13a430 | 18 | delete lcd; |
el17ajf | 8:5066ce13a430 | 19 | } |
el17ajf | 8:5066ce13a430 | 20 | |
el17ajf | 5:3efbdcb3efaf | 21 | void Graphics::clear() { |
el17ajf | 9:3a7776a29a11 | 22 | lcd->clear(); |
el17ajf | 5:3efbdcb3efaf | 23 | } |
el17ajf | 5:3efbdcb3efaf | 24 | |
el17ajf | 5:3efbdcb3efaf | 25 | void Graphics::render() { |
el17ajf | 9:3a7776a29a11 | 26 | lcd->refresh(); |
el17ajf | 8:5066ce13a430 | 27 | } |
el17ajf | 8:5066ce13a430 | 28 | |
el17ajf | 8:5066ce13a430 | 29 | void Graphics::drawPoint(int x, int y) { |
el17ajf | 8:5066ce13a430 | 30 | lcd->setPixel(x, y, true); |
el17ajf | 8:5066ce13a430 | 31 | } |
el17ajf | 8:5066ce13a430 | 32 | |
el17ajf | 8:5066ce13a430 | 33 | void Graphics::drawLine(int x1, int y1, int x2, int y2) { |
el17ajf | 8:5066ce13a430 | 34 | lcd->drawLine(x1, y1, x2, y2, 1); |
el17ajf | 8:5066ce13a430 | 35 | } |
el17ajf | 8:5066ce13a430 | 36 | |
el17ajf | 8:5066ce13a430 | 37 | void Graphics::drawDottedLine(int x1, int y1, int x2, int y2) { |
el17ajf | 11:fba7d54fd36b | 38 | lcd->drawLine( |
el17ajf | 11:fba7d54fd36b | 39 | y1, |
el17ajf | 11:fba7d54fd36b | 40 | x1, |
el17ajf | 11:fba7d54fd36b | 41 | y2, |
el17ajf | 11:fba7d54fd36b | 42 | x2, |
el17ajf | 11:fba7d54fd36b | 43 | 2 // dotted |
el17ajf | 11:fba7d54fd36b | 44 | ); |
el17ajf | 8:5066ce13a430 | 45 | } |
el17ajf | 8:5066ce13a430 | 46 | |
el17ajf | 8:5066ce13a430 | 47 | void Graphics::drawBox(int x1, int y1, int x2, int y2) { |
el17ajf | 8:5066ce13a430 | 48 | Graphics::drawLine(x1, y1, x2, y1); |
el17ajf | 8:5066ce13a430 | 49 | Graphics::drawLine(x2, y1, x2, y2); |
el17ajf | 8:5066ce13a430 | 50 | Graphics::drawLine(x2, y2, x1, y2); |
el17ajf | 8:5066ce13a430 | 51 | Graphics::drawLine(x1, y2, x1, y1); |
el17ajf | 8:5066ce13a430 | 52 | } |
el17ajf | 8:5066ce13a430 | 53 | |
el17ajf | 8:5066ce13a430 | 54 | void Graphics::drawBlock(int grid_x, int grid_y) { |
el17ajf | 11:fba7d54fd36b | 55 | // screen coords |
el17ajf | 11:fba7d54fd36b | 56 | int x = gridYToScreenX(grid_y); |
el17ajf | 11:fba7d54fd36b | 57 | int y = gridXToScreenY(grid_x); |
el17ajf | 11:fba7d54fd36b | 58 | Graphics::drawBox(x, y, x + 3, y - 3); |
el17ajf | 11:fba7d54fd36b | 59 | Graphics::drawPoint(x + 2, y - 2); |
el17ajf | 8:5066ce13a430 | 60 | } |
el17ajf | 8:5066ce13a430 | 61 | |
el17ajf | 8:5066ce13a430 | 62 | void Graphics::drawBorder() { |
el17ajf | 10:5762d7fae033 | 63 | Graphics::drawDottedLine(0, 0, 0, SCREEN_HEIGHT - 1); |
el17ajf | 10:5762d7fae033 | 64 | Graphics::drawDottedLine(1, 1, 1, SCREEN_HEIGHT - 2); |
el17ajf | 5:3efbdcb3efaf | 65 | |
el17ajf | 10:5762d7fae033 | 66 | Graphics::drawDottedLine(0, SCREEN_HEIGHT - 1, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1); |
el17ajf | 10:5762d7fae033 | 67 | Graphics::drawDottedLine(1, SCREEN_HEIGHT - 2, SCREEN_WIDTH - 2, SCREEN_HEIGHT - 2); |
el17ajf | 8:5066ce13a430 | 68 | |
el17ajf | 10:5762d7fae033 | 69 | Graphics::drawDottedLine(SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, SCREEN_WIDTH - 1, 0); |
el17ajf | 10:5762d7fae033 | 70 | Graphics::drawDottedLine(SCREEN_WIDTH - 2, SCREEN_HEIGHT - 2, SCREEN_WIDTH - 2, 1); |
el17ajf | 11:fba7d54fd36b | 71 | } |
el17ajf | 11:fba7d54fd36b | 72 | |
el17ajf | 11:fba7d54fd36b | 73 | int Graphics::gridYToScreenX(int grid_y) { |
el17ajf | 11:fba7d54fd36b | 74 | return grid_y * BLOCK_SIZE + BORDER_SIZE; |
el17ajf | 11:fba7d54fd36b | 75 | } |
el17ajf | 11:fba7d54fd36b | 76 | |
el17ajf | 11:fba7d54fd36b | 77 | int Graphics::gridXToScreenY(int grid_x) { |
el17ajf | 11:fba7d54fd36b | 78 | return SCREEN_WIDTH - (grid_x * BLOCK_SIZE) - BORDER_SIZE; |
el17ajf | 11:fba7d54fd36b | 79 | } |