set/get pixel
This commit is contained in:
parent
ee01eee844
commit
3faeca5c9a
|
@ -582,6 +582,57 @@ img_t gfx_display_alpha(img_t src)
|
|||
return res;
|
||||
}
|
||||
|
||||
int gfx_get_pixel(img_t src, int x, int y, color_t *color)
|
||||
{
|
||||
Uint8 r, g, b, a;
|
||||
Uint32 col;
|
||||
Uint32 *ptr;
|
||||
SDL_Surface *img = Surf(src);
|
||||
if (!img)
|
||||
return -1;
|
||||
|
||||
if (x >= img->w || y >= img->h || x < 0 || y < 0)
|
||||
return -1;
|
||||
|
||||
if (SDL_LockSurface(img))
|
||||
return -1;
|
||||
ptr = (Uint32*)img->pixels;
|
||||
ptr += y * img->w + x;
|
||||
col = *ptr;
|
||||
SDL_UnlockSurface(img);
|
||||
if (color)
|
||||
SDL_GetRGBA(col, img->format, &r, &g, &b, &a);
|
||||
|
||||
if (color) {
|
||||
color->r = r;
|
||||
color->g = g;
|
||||
color->b = b;
|
||||
color->a = a;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int gfx_set_pixel(img_t src, int x, int y, color_t color)
|
||||
{
|
||||
Uint32 col;
|
||||
Uint32 *ptr;
|
||||
SDL_Surface *img = Surf(src);
|
||||
if (!img)
|
||||
return -1;
|
||||
|
||||
if (x >= img->w || y >= img->h || x < 0 || y < 0)
|
||||
return -1;
|
||||
|
||||
if (SDL_LockSurface(img))
|
||||
return -1;
|
||||
ptr = (Uint32*)img->pixels;
|
||||
ptr += y * img->w + x;
|
||||
col = SDL_MapRGBA(img->format, color.r, color.g, color.b, color.a);
|
||||
*ptr = col;
|
||||
SDL_UnlockSurface(img);
|
||||
return 0;
|
||||
}
|
||||
|
||||
img_t gfx_alpha_img(img_t src, int alpha)
|
||||
{
|
||||
Uint32 *ptr;
|
||||
|
|
|
@ -26,6 +26,7 @@ typedef struct {
|
|||
int r;
|
||||
int g;
|
||||
int b;
|
||||
int a;
|
||||
} color_t;
|
||||
|
||||
#define ALIGN_LEFT 1
|
||||
|
@ -99,6 +100,8 @@ extern void gfx_change_screen(img_t src, int steps);
|
|||
extern int gfx_fading(void);
|
||||
extern void gfx_img_fill(img_t img, int x, int y, int w, int h, color_t col);
|
||||
extern void gfx_fill(int x, int y, int w, int h, color_t col);
|
||||
extern int gfx_get_pixel(img_t src, int x, int y, color_t *color);
|
||||
extern int gfx_set_pixel(img_t src, int x, int y, color_t color);
|
||||
|
||||
extern fnt_t fnt_load(const char *fname, int size);
|
||||
extern void fnt_free(fnt_t);
|
||||
|
|
|
@ -1196,7 +1196,7 @@ static int luaB_fill_sprite(lua_State *L) {
|
|||
int y = luaL_optnumber(L, 3, 0);
|
||||
int w = luaL_optnumber(L, 4, -1);
|
||||
int h = luaL_optnumber(L, 5, -1);
|
||||
const char *color = luaL_optstring(L, 6, 0);
|
||||
const char *color = luaL_optstring(L, 6, NULL);
|
||||
int xoff = 0, yoff = 0;
|
||||
color_t col = { .r = game_theme.bgcol.r, .g = game_theme.bgcol.g, .b = game_theme.bgcol.b };
|
||||
if (!dst)
|
||||
|
@ -1232,6 +1232,55 @@ static int luaB_fill_sprite(lua_State *L) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
static int luaB_pixel_sprite(lua_State *L) {
|
||||
img_t d;
|
||||
float v;
|
||||
int rc;
|
||||
color_t col = { .r = game_theme.bgcol.r, .g = game_theme.bgcol.g, .b = game_theme.bgcol.b, .a = 255 };
|
||||
const char *dst = luaL_optstring(L, 1, NULL);
|
||||
int x = luaL_optnumber(L, 2, 0);
|
||||
int y = luaL_optnumber(L, 3, 0);
|
||||
const char *color = luaL_optstring(L, 4, NULL);
|
||||
int alpha = luaL_optnumber(L, 5, 255);
|
||||
int xoff = 0, yoff = 0;
|
||||
|
||||
if (!dst)
|
||||
return 0;
|
||||
|
||||
d = grab_sprite(dst, &xoff, &yoff);
|
||||
|
||||
if (color)
|
||||
gfx_parse_color(color, &col);
|
||||
|
||||
if (!d)
|
||||
return 0;
|
||||
|
||||
v = game_theme.scale;
|
||||
|
||||
if (v != 1.0f) {
|
||||
x *= v;
|
||||
y *= v;
|
||||
}
|
||||
if (color) {
|
||||
game_pict_modify(d);
|
||||
gfx_clip(game_theme.xoff, game_theme.yoff, game_theme.w - 2*game_theme.xoff, game_theme.h - 2*game_theme.yoff);
|
||||
col.a = alpha;
|
||||
rc = gfx_set_pixel(d, x + xoff, y + yoff, col);
|
||||
gfx_noclip();
|
||||
} else {
|
||||
rc = gfx_get_pixel(d, x + xoff, y + yoff, &col);
|
||||
}
|
||||
|
||||
if (rc)
|
||||
return 0;
|
||||
|
||||
lua_pushnumber(L, col.r);
|
||||
lua_pushnumber(L, col.g);
|
||||
lua_pushnumber(L, col.b);
|
||||
lua_pushnumber(L, col.a);
|
||||
return 4;
|
||||
}
|
||||
|
||||
static int _free_sprite(const char *key)
|
||||
{
|
||||
_spr_t *sp;
|
||||
|
@ -1391,6 +1440,7 @@ static const luaL_Reg base_funcs[] = {
|
|||
{"sprite_scale", luaB_scale_sprite},
|
||||
{"sprite_rotate", luaB_rotate_sprite},
|
||||
{"sprite_text_size", luaB_text_size},
|
||||
{"sprite_pixel", luaB_pixel_sprite},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
|
|
@ -12,6 +12,7 @@ stead.sprite_text_size = sprite_text_size
|
|||
stead.sprite_draw = sprite_draw
|
||||
stead.sprite_copy = sprite_copy
|
||||
stead.sprite_fill = sprite_fill
|
||||
stead.sprite_pixel = sprite_pixel
|
||||
stead.sprite_load = sprite_load
|
||||
stead.sprite_free = sprite_free
|
||||
stead.sprite_size = sprite_size
|
||||
|
@ -81,6 +82,9 @@ sprite = {
|
|||
end
|
||||
return stead.sprite_fill(d, x, y, w, h, col);
|
||||
end;
|
||||
pixel = function(d, x, y, col)
|
||||
return stead.sprite_pixel(d, x, y, col)
|
||||
end;
|
||||
load = function(fname)
|
||||
return stead.sprite_load(fname);
|
||||
end;
|
||||
|
|
Loading…
Reference in a new issue