Skip to content

Commit

Permalink
Added Graphics.getPixel.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rinnegatamante committed Jan 16, 2020
1 parent 6eff896 commit 551cad8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
17 changes: 17 additions & 0 deletions doc/luaGraphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ class Graphics {
*/
void drawPixel(number x, number y, int color, int image);

/**
* Get a pixel color from a loaded image.
* \ingroup Graphics
*
* @par Usage example:
* @code
* pixel_color = Graphics.getPixel(5, 25, img)
* @endcode
*
* @param x - X coordinate of the pixel.
* @param y - Y coordinate of the pixel.
* @param img - A valid image id.
*
* @return The pixel color value (See ::Color).
*/
int getPixel(int x, int y, int img);

/**
* Draw a line.
* \ingroup Graphics
Expand Down
18 changes: 18 additions & 0 deletions source/luaGraphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <string.h>
#include <vitasdk.h>
#include <vita2d.h>
#include <utils.h>
#include "include/luaplayer.h"
#include "include/message_dialog.h"

Expand Down Expand Up @@ -561,12 +562,29 @@ static int lua_rescaleroff(lua_State *L) {
return 0;
}

static int lua_gpixel(lua_State *L) {
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc != 3) return luaL_error(L, "wrong number of arguments");
#endif
int x = luaL_checkinteger(L, 1);
int y = luaL_checkinteger(L, 2);
lpp_texture* text = (lpp_texture*)(luaL_checkinteger(L, 3));
#ifndef SKIP_ERROR_HANDLING
if (text->magic != 0xABADBEEF) luaL_error(L, "attempt to access wrong memory block type.");
#endif
uint32_t *buff = (uint32_t*)vita2d_texture_get_datap(text->text);
lua_pushinteger(L, buff[ALIGN(vita2d_texture_get_width(text->text), 8) * y + x]);
return 1;
}

//Register our Graphics Functions
static const luaL_Reg Graphics_functions[] = {
{"initBlend", lua_init},
{"termBlend", lua_term},
{"debugPrint", lua_print},
{"drawPixel", lua_pixel},
{"getPixel", lua_gpixel},
{"drawLine", lua_line},
{"fillRect", lua_rect},
{"fillEmptyRect", lua_emptyrect},
Expand Down

0 comments on commit 551cad8

Please sign in to comment.