foxygit / fake-08-xbox360 Log in
commit fa33cc9c9377358347e3e89dedabf87b5266fcee
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Thu Apr 23 16:41:54 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Thu Apr 23 16:41:54 2020 -0600

    Implement clip()
---
 source/Readme.md      |  2 +-
 source/console.cpp    |  1 +
 source/graphics.cpp   | 44 +++++++++++++++++++++++++++++++++++---------
 source/graphics.h     | 10 ++++++++++
 source/picoluaapi.cpp | 32 ++++++++++++++++++++++++--------
 source/picoluaapi.h   |  1 +
 6 files changed, 72 insertions(+), 18 deletions(-)

diff --git a/source/Readme.md b/source/Readme.md
index ec045c1..d82d974 100644
--- a/source/Readme.md
+++ b/source/Readme.md
@@ -12,7 +12,7 @@ Basic Roadmap:
 -finish basic graphics api support
 (x) implement overloads of existing graphics API
 (x) Add camera() support
-( ) Add clip() support
+(x) Add clip() support
 ( ) Add map(), mget(), mset() support
 ( ) Add pal() support
 ( ) Add palt() support
diff --git a/source/console.cpp b/source/console.cpp
index 991a1d8..d36b987 100644
--- a/source/console.cpp
+++ b/source/console.cpp
@@ -76,6 +76,7 @@ void Console::LoadCart(std::string filename){
     lua_register(_luaState, "sget", sget);
     lua_register(_luaState, "sset", sset);
     lua_register(_luaState, "camera", camera);
+    lua_register(_luaState, "clip", clip);

     //input
     lua_register(_luaState, "btn", btn);
diff --git a/source/graphics.cpp b/source/graphics.cpp
index 0116383..ea9082c 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -36,6 +36,9 @@ const Color PaletteColors[] = {
 Graphics::Graphics(std::string fontdata) {
 	this->_gfxState_color = 7;

+	this->_gfxState_clip_w = 127;
+	this->_gfxState_clip_h = 127;
+
 	copy_data_to_sprites(fontSpriteData, fontdata);
 }

@@ -181,9 +184,30 @@ void Graphics::sortCoordsForRect(short *x1, short *y1, short *x2, short *y2){
 	}
 }

+bool Graphics::canDrawAtPoint(short x, short y) {
+	return this->isOnScreen(x, y) && this->isWithinClip(x, y);
+}
+
 bool Graphics::isOnScreen(short x, short y) {
 	return x >= 0 && x <= 127 && y >= 0 && y <= 127;
 }
+
+bool Graphics::isWithinClip(short x, short y) {
+	return
+		x >= _gfxState_clip_x &&
+		x <= _gfxState_clip_x + _gfxState_clip_w &&
+		y >= _gfxState_clip_y &&
+		y <= _gfxState_clip_y + _gfxState_clip_h;
+}
+
+void Graphics::_private_pset(short x, short y, uint8_t col) {
+	x = x - _gfxState_camera_x;
+	y = y - _gfxState_camera_y;
+
+	if (canDrawAtPoint(x, y)){
+		_pico8_fb[(x * 128) + y] = col;
+	}
+}
 //end helper methods

 void Graphics::cls() {
@@ -207,15 +231,6 @@ void Graphics::pset(short x, short y, uint8_t col){
 	_private_pset(x, y, col);
 }

-void Graphics::_private_pset(short x, short y, uint8_t col) {
-	x = x - _gfxState_camera_x;
-	y = y - _gfxState_camera_y;
-
-	if (isOnScreen(x, y)){
-		_pico8_fb[(x * 128) + y] = col;
-	}
-}
-
 uint8_t Graphics::pget(short x, short y){
 	if (isOnScreen(x, y)){
 		return _pico8_fb[(x * 128) + y];
@@ -518,6 +533,17 @@ void Graphics::camera(short x, short y) {
 	_gfxState_camera_y = y;
 }

+void Graphics::clip() {
+	this->clip(0, 0, 127, 127);
+}
+
+void Graphics::clip(short x, short y, short w, short h) {
+	_gfxState_clip_x = x;
+	_gfxState_clip_y = y;
+	_gfxState_clip_w = w;
+	_gfxState_clip_h = h;
+}
+
 void Graphics::flipBuffer(uint8_t* fb) {
 	short x, y;
     for(x = 0; x < 400; x++) {
diff --git a/source/graphics.h b/source/graphics.h
index a9407b2..d75ae69 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -46,6 +46,11 @@ class Graphics {
 	short _gfxState_camera_x;
 	short _gfxState_camera_y;

+	short _gfxState_clip_x;
+	short _gfxState_clip_y;
+	short _gfxState_clip_w;
+	short _gfxState_clip_h;
+
 	//not actually part of graphics state memory?
 	short _gfxState_line_x;
 	short _gfxState_line_y;
@@ -82,7 +87,9 @@ class Graphics {

 	void sortCoordsForRect(short *x1, short *y1, short *x2, short *y2);

+	bool canDrawAtPoint(short x, short y);
 	bool isOnScreen(short x, short y);
+	bool isWithinClip(short x, short y);

 	void _private_pset(short x, short y, uint8_t col);

@@ -156,6 +163,9 @@ class Graphics {
 	void camera();
 	void camera(short x, short y);

+	void clip();
+	void clip(short x, short y, short w, short h);
+

 	void flipBuffer(uint8_t* fb);

diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index b4a91fe..edb11f3 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -349,8 +349,8 @@ int fset(lua_State *L) {
 }

 int sget(lua_State *L) {
-    int x = lua_tointeger(L,1);
-    int y = lua_tointeger(L,2);
+    short x = lua_tonumber(L,1);
+    short y = lua_tonumber(L,2);
     uint8_t result = _graphicsForLuaApi->sget((uint8_t)x, (uint8_t)y);
     lua_pushinteger(L, result);

@@ -358,10 +358,10 @@ int sget(lua_State *L) {
 }

 int sset(lua_State *L) {
-    int x = lua_tointeger(L,1);
-    int y = lua_tointeger(L,2);
-    int c = lua_tointeger(L,3);
-    _graphicsForLuaApi->sset((uint8_t)x, (uint8_t)y, (uint8_t)c);
+    short x = lua_tonumber(L,1);
+    short y = lua_tonumber(L,2);
+    uint8_t c = lua_tonumber(L,3);
+    _graphicsForLuaApi->sset(x, y, c);

     return 0;
 }
@@ -370,10 +370,10 @@ int camera(lua_State *L) {
     short x = 0;
     short y = 0;
     if (lua_gettop(L) > 0) {
-        x = lua_tointeger(L,1);
+        x = lua_tonumber(L,1);
     }
     if (lua_gettop(L) > 1) {
-        y = lua_tointeger(L,2);
+        y = lua_tonumber(L,2);
     }

     _graphicsForLuaApi->camera(x, y);
@@ -381,6 +381,22 @@ int camera(lua_State *L) {
     return 0;
 }

+int clip(lua_State *L) {
+    if (lua_gettop(L) >= 4) {
+        short x = lua_tonumber(L,1);
+        short y = lua_tonumber(L,2);
+        short w = lua_tonumber(L,3);
+        short h = lua_tonumber(L,4);
+
+        _graphicsForLuaApi->clip(x, y, w, h);
+    }
+    else {
+        _graphicsForLuaApi->clip();
+    }
+
+    return 0;
+}
+
 //Input

 //input api
diff --git a/source/picoluaapi.h b/source/picoluaapi.h
index 701cb0a..6123e02 100644
--- a/source/picoluaapi.h
+++ b/source/picoluaapi.h
@@ -32,6 +32,7 @@ int fset(lua_State *L);
 int sget(lua_State *L);
 int sset(lua_State *L);
 int camera(lua_State *L);
+int clip(lua_State *L);

 //input api
 int btn(lua_State *L);