commit b39956d9d99a4279186cbdf451c931da137f2b4a
Author: Jon Bell <jondbell@protonmail.com>
AuthorDate: Fri Apr 17 12:51:58 2020 -0600
Commit: Jon Bell <jondbell@protonmail.com>
CommitDate: Fri Apr 17 12:51:58 2020 -0600
Encapsulate graphics calls in Graphics class
---
source/console.cpp | 12 ++++++++-
source/console.h | 3 +++
source/fakecart.cpp | 2 +-
source/graphics.cpp | 64 ++++++++++++++++++++++--------------------------
source/graphics.h | 51 +++++++++++++++++++++++++++++---------
source/main.cpp | 4 +--
source/picoglobalapi.cpp | 41 +++++++++++++++++++++++++++++++
source/picoglobalapi.h | 20 +++++++++++++++
8 files changed, 146 insertions(+), 51 deletions(-)
diff --git a/source/console.cpp b/source/console.cpp
index 7a458c3..0b8049b 100644
--- a/source/console.cpp
+++ b/source/console.cpp
@@ -4,11 +4,17 @@
#include "graphics.h"
#include "fontdata.h"
#include "cart.h"
+#include "picoglobalapi.h"
Console::Console(){
auto fontdata = get_font_data();
- initPico8Graphics(fontdata);
+ Graphics* graphics = new Graphics(fontdata);
+
+ _graphics = graphics;
+
+ //this can probably go away when I'm loading actual carts and just have to expose api to lua
+ initGlobalApi(_graphics);
}
void Console::LoadCart(std::string filename){
@@ -17,3 +23,7 @@ void Console::LoadCart(std::string filename){
_loadedCart = &cart;
}
+
+void Console::FlipBuffer(uint8_t* fb){
+ _graphics->flipBuffer(fb);
+}
diff --git a/source/console.h b/source/console.h
index 8624bcc..6acf543 100644
--- a/source/console.h
+++ b/source/console.h
@@ -4,10 +4,13 @@
class Console {
Cart* _loadedCart;
+ Graphics* _graphics;
public:
Console();
void LoadCart(std::string filename);
+
+ void FlipBuffer(uint8_t* fb);
};
diff --git a/source/fakecart.cpp b/source/fakecart.cpp
index 84e4b75..b069b05 100644
--- a/source/fakecart.cpp
+++ b/source/fakecart.cpp
@@ -1,5 +1,5 @@
-#include "graphics.h"
+#include "picoglobalapi.h"
float r1x0 = 10;
diff --git a/source/graphics.cpp b/source/graphics.cpp
index 9e5637a..b386272 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -29,11 +29,6 @@ const Color PaletteColors[] = {
};
-uint8_t _pico8_fb[128*128];
-
-static GraphicsState _graphicsState;
-
-SpriteSheet _fontSpriteSheet;
void copy_data_to_sprites(SpriteSheet *sprites, std::string data) {
uint16_t i = 0;
@@ -54,13 +49,15 @@ void copy_data_to_sprites(SpriteSheet *sprites, std::string data) {
//call initialize to make sure defaults are correct
-void initPico8Graphics(std::string fontdata) {
-
- copy_data_to_sprites(&_fontSpriteSheet, fontdata);
+Graphics::Graphics(std::string fontdata) {
+ SpriteSheet spritesheet = {};
+ _fontSpriteSheet = & spritesheet;
+ copy_data_to_sprites(_fontSpriteSheet, fontdata);
}
+//start helper methods
//based on tac08 implementation of blitter()
-void copySpriteToScreen(
+void Graphics::copySpriteToScreen(
uint8_t spritebuffer[],
short scr_x,
short scr_y,
@@ -90,9 +87,7 @@ void copySpriteToScreen(
}
}
-
-//start helper methods
-void swap(short *x, short *y) {
+void Graphics::swap(short *x, short *y) {
short temp;
temp = *x;
*x = *y;
@@ -101,14 +96,14 @@ void swap(short *x, short *y) {
return;
}
-void sortPointsLtoR(short *x1, short *y1, short *x2, short *y2){
+void Graphics::sortPointsLtoR(short *x1, short *y1, short *x2, short *y2){
if (*x1 > *x2) {
swap(x1, x2);
swap(y1, y2);
}
}
-void sortCoordsForRect(short *x1, short *y1, short *x2, short *y2){
+void Graphics::sortCoordsForRect(short *x1, short *y1, short *x2, short *y2){
if (*x1 > *x2) {
swap(x1, x2);
}
@@ -118,22 +113,22 @@ void sortCoordsForRect(short *x1, short *y1, short *x2, short *y2){
}
}
-bool isOnScreen(short *x, short* y) {
+bool Graphics::isOnScreen(short *x, short* y) {
return *x >= 0 && *x < 127 && *y >= 0 && *y < 127;
}
//end helper methods
-void cls() {
- memset(_pico8_fb, _graphicsState.bgColor, sizeof(_pico8_fb));
+void Graphics::cls() {
+ memset(_pico8_fb, _graphicsState->bgColor, sizeof(_pico8_fb));
}
-void pset(short x, short y, uint8_t col){
+void Graphics::pset(short x, short y, uint8_t col){
if (isOnScreen(&x, &y)){
_pico8_fb[(x * 128) + y] = col;
}
}
-uint8_t pget(short x, short y){
+uint8_t Graphics::pget(short x, short y){
if (isOnScreen(&x, &y)){
return _pico8_fb[(x * 128) + y];
}
@@ -141,12 +136,11 @@ uint8_t pget(short x, short y){
return 0;
}
-
-void color(uint8_t col){
- _graphicsState.color = col;
+void Graphics::color(uint8_t col){
+ _graphicsState->color = col;
}
-void line (short x1, short y1, short x2, short y2, uint8_t col) {
+void Graphics::line (short x1, short y1, short x2, short y2, uint8_t col) {
sortPointsLtoR(&x1, &y1, &x2, &y2);
float run = x2 - x1;
@@ -172,7 +166,7 @@ void line (short x1, short y1, short x2, short y2, uint8_t col) {
}
}
-void circ(short ox, short oy, short r, uint8_t col){
+void Graphics::circ(short ox, short oy, short r, uint8_t col){
short x = r;
short y = 0;
short decisionOver2 = 1-x;
@@ -200,7 +194,7 @@ void circ(short ox, short oy, short r, uint8_t col){
}
-void circfill(short ox, short oy, short r, uint8_t col){
+void Graphics::circfill(short ox, short oy, short r, uint8_t col){
if (r == 0) {
pset(ox, oy, col);
}
@@ -224,7 +218,7 @@ void circfill(short ox, short oy, short r, uint8_t col){
}
-void rect(short x1, short y1, short x2, short y2, uint8_t col) {
+void Graphics::rect(short x1, short y1, short x2, short y2, uint8_t col) {
sortCoordsForRect(&x1, &y1, &x2, &y2);
for (short i = x1; i <= x2; i++) {
@@ -237,7 +231,7 @@ void rect(short x1, short y1, short x2, short y2, uint8_t col) {
}
-void rectfill(short x1, short y1, short x2, short y2, uint8_t col) {
+void Graphics::rectfill(short x1, short y1, short x2, short y2, uint8_t col) {
sortCoordsForRect(&x1, &y1, &x2, &y2);
for (short i = x1; i <= x2; i++) {
@@ -248,32 +242,32 @@ void rectfill(short x1, short y1, short x2, short y2, uint8_t col) {
}
//tac08
-short print(std::string str, short x, short y, uint16_t c) {
- _graphicsState.text_x = x;
+short Graphics::print(std::string str, short x, short y, uint16_t c) {
+ _graphicsState->text_x = x;
for (size_t n = 0; n < str.length(); n++) {
uint8_t ch = str[n];
if (ch >= 0x10 && ch < 0x80) {
short index = ch - 0x10;
- copySpriteToScreen(_fontSpriteSheet.sprite_data, x, y, (index % 16) * 8, (index / 16) * 8, 4, 5);
+ copySpriteToScreen(_fontSpriteSheet->sprite_data, x, y, (index % 16) * 8, (index / 16) * 8, 4, 5);
x += 4;
} else if (ch >= 0x80) {
short index = ch - 0x80;
- copySpriteToScreen(_fontSpriteSheet.sprite_data, x, y, (index % 16) * 8, (index / 16) * 8 + 56, 8, 5);
+ copySpriteToScreen(_fontSpriteSheet->sprite_data, x, y, (index % 16) * 8, (index / 16) * 8 + 56, 8, 5);
x += 8;
} else if (ch == '\n') {
- x = _graphicsState.text_x;
+ x = _graphicsState->text_x;
y += 6;
}
}
- _graphicsState.text_x = 0;
- _graphicsState.text_y = y + 6;
+ _graphicsState->text_x = 0;
+ _graphicsState->text_y = y + 6;
return x;
}
-void flipBuffer(uint8_t* fb) {
+void Graphics::flipBuffer(uint8_t* fb) {
short x, y;
for(x = 0; x < 400; x++) {
for(y = 0; y < 240; y++) {
diff --git a/source/graphics.h b/source/graphics.h
index 6d7b333..c72eb84 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -42,23 +42,50 @@ struct SpriteSheet{
uint8_t flags[256];
};
-void initPico8Graphics(std::string fontdata);
+class Graphics {
+ uint8_t _pico8_fb[128*128];
+ GraphicsState* _graphicsState;
+ SpriteSheet* _fontSpriteSheet;
-void cls();
+ void copySpriteToScreen(
+ uint8_t spritebuffer[],
+ short scr_x,
+ short scr_y,
+ short spr_x,
+ short spr_y,
+ short spr_w,
+ short spr_h,
+ bool flip_x,
+ bool flip_y);
-void pset(short x, short y, uint8_t col);
-uint8_t pget(short x, short y);
+ void swap(short *x, short *y);
-void color(uint8_t c);
+ void sortPointsLtoR(short *x1, short *y1, short *x2, short *y2);
-void line (short x1, short y1, short x2, short y2, uint8_t col);
+ void sortCoordsForRect(short *x1, short *y1, short *x2, short *y2);
-void circ(short ox, short oy, short r, uint8_t col);
-void circfill(short ox, short oy, short r, uint8_t col);
+ bool isOnScreen(short *x, short* y);
-void rect(short x1, short y1, short x2, short y2, uint8_t col);
-void rectfill(short x1, short y1, short x2, short y2, uint8_t col);
+ public:
+ Graphics(std::string fontdata);
+ void cls();
-short print(std::string str, short x, short y, uint16_t c);
+ void pset(short x, short y, uint8_t col);
+ uint8_t pget(short x, short y);
+
+ void color(uint8_t c);
+
+ void line (short x1, short y1, short x2, short y2, uint8_t col);
+
+ void circ(short ox, short oy, short r, uint8_t col);
+ void circfill(short ox, short oy, short r, uint8_t col);
+
+ void rect(short x1, short y1, short x2, short y2, uint8_t col);
+ void rectfill(short x1, short y1, short x2, short y2, uint8_t col);
+
+ short print(std::string str, short x, short y, uint16_t c);
+
+ void flipBuffer(uint8_t* fb);
+
+};
-void flipBuffer(uint8_t* fb);
\ No newline at end of file
diff --git a/source/main.cpp b/source/main.cpp
index 6efcfca..c82c062 100644
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -29,7 +29,7 @@ int main(int argc, char* argv[])
console.LoadCart("testcart.p8");
#else
- loadcart("testcart.p8");
+ console.LoadCart("testcart.p8");
int bgcolor = 0;
#endif
@@ -64,7 +64,7 @@ int main(int argc, char* argv[])
_draw();
//send pico 8 screen to framebuffer
- flipBuffer(fb);
+ console.FlipBuffer(fb);
frames++;
}
diff --git a/source/picoglobalapi.cpp b/source/picoglobalapi.cpp
new file mode 100644
index 0000000..061931e
--- /dev/null
+++ b/source/picoglobalapi.cpp
@@ -0,0 +1,41 @@
+
+#include <string>
+
+#include "graphics.h"
+
+Graphics* _graphicsForApi;
+
+void initGlobalApi(Graphics* graphics){
+ _graphicsForApi = graphics;
+}
+
+void cls(){
+ _graphicsForApi->cls();
+}
+void pset(short x, short y, uint8_t col){
+ _graphicsForApi->pset(x, y, col);
+}
+uint8_t pget(short x, short y){
+ return _graphicsForApi->pget(x, y);
+}
+void color(uint8_t c){
+ _graphicsForApi->color(c);
+}
+void line (short x1, short y1, short x2, short y2, uint8_t col){
+ _graphicsForApi->line(x1, y1, x2, y2, col);
+}
+void circ(short ox, short oy, short r, uint8_t col){
+ _graphicsForApi->circ(ox, oy, r, col);
+}
+void circfill(short ox, short oy, short r, uint8_t col){
+ _graphicsForApi->circfill(ox, oy, r, col);
+}
+void rect(short x1, short y1, short x2, short y2, uint8_t col){
+ _graphicsForApi->rect(x1, y1, x2, y2, col);
+}
+void rectfill(short x1, short y1, short x2, short y2, uint8_t col){
+ _graphicsForApi->rectfill(x1, y1, x2, y2, col);
+}
+short print(std::string str, short x, short y, uint16_t c){
+ return _graphicsForApi->print(str, x, y, c);
+}
diff --git a/source/picoglobalapi.h b/source/picoglobalapi.h
new file mode 100644
index 0000000..b2db7ff
--- /dev/null
+++ b/source/picoglobalapi.h
@@ -0,0 +1,20 @@
+#pragma once
+
+#include <string>
+
+#include "graphics.h"
+
+//this can probably go away when I'm loading actual carts and just have to expose api to lua
+void initGlobalApi(Graphics* graphics);
+
+void cls();
+void pset(short x, short y, uint8_t col);
+uint8_t pget(short x, short y);
+void color(uint8_t c);
+void line (short x1, short y1, short x2, short y2, uint8_t col);
+void circ(short ox, short oy, short r, uint8_t col);
+void circfill(short ox, short oy, short r, uint8_t col);
+void rect(short x1, short y1, short x2, short y2, uint8_t col);
+void rectfill(short x1, short y1, short x2, short y2, uint8_t col);
+short print(std::string str, short x, short y, uint16_t c);
+