Skip to content

Instantly share code, notes, and snippets.

@kilbith
Created November 16, 2022 01:12
Show Gist options
  • Save kilbith/513644d5b68043f8cc0d75abddd73e27 to your computer and use it in GitHub Desktop.
Save kilbith/513644d5b68043f8cc0d75abddd73e27 to your computer and use it in GitHub Desktop.
core.get_pixel_color(texture_path, x, y)
commit 4ec99632b153a9a3ecf6cf123353f675ba12b1d6
Author: Jean-Patrick Guerrero <kilbith@users.noreply.github.com>
Date: Wed Nov 16 01:58:15 2022 +0100
Add core.get_pixel_color()
diff --git a/src/script/lua_api/l_server.cpp b/src/script/lua_api/l_server.cpp
index a5daae346..a0b90087c 100644
--- a/src/script/lua_api/l_server.cpp
+++ b/src/script/lua_api/l_server.cpp
@@ -29,6 +29,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "environment.h"
#include "remoteplayer.h"
#include "log.h"
+#include "filesys.h"
+#include "client/renderingengine.h"
#include <algorithm>
// request_shutdown()
@@ -596,6 +598,31 @@ int ModApiServer::l_serialize_roundtrip(lua_State *L)
return 1;
}
+// get_pixel_color()
+int ModApiServer::l_get_pixel_color(lua_State *L)
+{
+ auto driver = RenderingEngine::get_video_driver();
+
+ std::string path = readParam<std::string>(L, 1);
+ video::ITexture *texture = driver->getTexture(path.c_str());
+
+ if (!texture)
+ return 0;
+
+ auto img = driver->createImage(texture, core::position2d<s32>(), texture->getSize());
+ int x = readParam<int>(L, 2);
+ int y = readParam<int>(L, 3);
+
+ video::SColor px = img->getPixel(x, y);
+ char buf[16+1];
+
+ snprintf(buf, sizeof buf, "#%02x%02x%02x", px.getRed(), px.getGreen(), px.getBlue());
+
+ lua_pushstring(L, buf);
+
+ return 1;
+}
+
void ModApiServer::Initialize(lua_State *L, int top)
{
API_FCT(request_shutdown);
@@ -633,6 +660,8 @@ void ModApiServer::Initialize(lua_State *L, int top)
API_FCT(do_async_callback);
API_FCT(register_async_dofile);
API_FCT(serialize_roundtrip);
+
+ API_FCT(get_pixel_color);
}
void ModApiServer::InitializeAsync(lua_State *L, int top)
diff --git a/src/script/lua_api/l_server.h b/src/script/lua_api/l_server.h
index a4f38c34e..4328ed976 100644
--- a/src/script/lua_api/l_server.h
+++ b/src/script/lua_api/l_server.h
@@ -115,6 +115,9 @@ class ModApiServer : public ModApiBase
// serialize_roundtrip(obj)
static int l_serialize_roundtrip(lua_State *L);
+ // get_pixel_color()
+ static int l_get_pixel_color(lua_State *L);
+
public:
static void Initialize(lua_State *L, int top);
static void InitializeAsync(lua_State *L, int top);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment