fix texture functions, add to ffi
This commit is contained in:
parent
9e6462bff4
commit
dbaf482385
|
@ -2651,7 +2651,6 @@ JSC_GET(texture, vram, number)
|
|||
JSC_SCALL(texture_save, texture_save(js2texture(self), str));
|
||||
|
||||
JSC_CCALL(texture_blit,
|
||||
texture *tex = js2texture(self);
|
||||
texture_blit(js2texture(self), js2texture(argv[0]), js2rect(argv[1]), js2rect(argv[2]), js2boolean(argv[3]));
|
||||
)
|
||||
|
||||
|
@ -2662,6 +2661,39 @@ JSC_CCALL(texture_getid,
|
|||
|
||||
JSC_CCALL(texture_inram, return boolean2js(js2texture(self)->data));
|
||||
|
||||
JSC_CCALL(texture_fill,
|
||||
texture_fill(js2texture(self), js2color(argv[0]));
|
||||
)
|
||||
|
||||
JSC_CCALL(texture_fill_rect,
|
||||
texture_fill_rect(js2texture(self), js2rect(argv[0]), js2color(argv[1]));
|
||||
)
|
||||
|
||||
JSC_CCALL(texture_flip,
|
||||
texture_flip(js2texture(self), js2boolean(argv[0]));
|
||||
)
|
||||
|
||||
JSC_CCALL(texture_write_pixel,
|
||||
HMM_Vec2 c = js2vec2(argv[0]);
|
||||
texture_write_pixel(js2texture(self), c.x, c.y, js2color(argv[1]));
|
||||
)
|
||||
|
||||
JSC_CCALL(texture_dup,
|
||||
ret = texture2js(texture_dup(js2texture(self)));
|
||||
)
|
||||
|
||||
JSC_CCALL(texture_scale,
|
||||
ret = texture2js(texture_scale(js2texture(self), js2number(argv[0]), js2number(argv[1])));
|
||||
)
|
||||
|
||||
JSC_CCALL(texture_load_gpu,
|
||||
texture_load_gpu(js2texture(self));
|
||||
)
|
||||
|
||||
JSC_CCALL(texture_offload,
|
||||
texture_offload(js2texture(self));
|
||||
)
|
||||
|
||||
static const JSCFunctionListEntry js_texture_funcs[] = {
|
||||
MIST_GET(texture, width),
|
||||
MIST_GET(texture, height),
|
||||
|
@ -2669,9 +2701,17 @@ static const JSCFunctionListEntry js_texture_funcs[] = {
|
|||
MIST_GET(texture, delays),
|
||||
MIST_GET(texture, vram),
|
||||
MIST_FUNC_DEF(texture, save, 1),
|
||||
MIST_FUNC_DEF(texture, blit, 5),
|
||||
MIST_FUNC_DEF(texture, write_pixel, 2),
|
||||
MIST_FUNC_DEF(texture, fill, 1),
|
||||
MIST_FUNC_DEF(texture, fill_rect, 2),
|
||||
MIST_FUNC_DEF(texture, dup, 0),
|
||||
MIST_FUNC_DEF(texture, scale, 2),
|
||||
MIST_FUNC_DEF(texture, flip, 1),
|
||||
MIST_FUNC_DEF(texture, blit, 4),
|
||||
MIST_FUNC_DEF(texture, getid, 0),
|
||||
MIST_FUNC_DEF(texture, inram, 0),
|
||||
MIST_FUNC_DEF(texture, load_gpu, 0),
|
||||
MIST_FUNC_DEF(texture, offload, 0),
|
||||
};
|
||||
|
||||
JSC_GETSET_CALLBACK(timer, fn)
|
||||
|
|
|
@ -249,7 +249,6 @@ struct texture *texture_empty(int w, int h)
|
|||
tex->data = calloc(w*h*n, sizeof(unsigned char));
|
||||
tex->width = w;
|
||||
tex->height = h;
|
||||
texture_load_gpu(tex);
|
||||
return tex;
|
||||
}
|
||||
|
||||
|
@ -321,17 +320,17 @@ void texture_save(texture *tex, const char *file)
|
|||
// sx and sy are the destination coordinates to copy to
|
||||
// sw the width of the destination to take in pixels
|
||||
// sh the height of the destination to take in pixels
|
||||
int texture_blit(texture *src, texture *dst, rect srcrect, rect dstrect, int tile) {
|
||||
if (!src || !dst || !src->data || !dst->data) return 0;
|
||||
int texture_blit(texture *dst, texture *src, rect dstrect, rect srcrect, int tile) {
|
||||
// if (!src || !dst || !src->data || !dst->data) return 0;
|
||||
|
||||
float scaleX = srcrect.w / dstrect.w;
|
||||
float scaleY = srcrect.h / dstrect.h;
|
||||
|
||||
if (srcrect.x < 0 || srcrect.y < 0 || srcrect.x + srcrect.w > src->width ||
|
||||
/* if (srcrect.x < 0 || srcrect.y < 0 || srcrect.x + srcrect.w > src->width ||
|
||||
dstrect.x < 0 || dstrect.y < 0 || dstrect.x + dstrect.w > dst->width ||
|
||||
srcrect.y + srcrect.h > src->height || dstrect.y + dstrect.h > dst->height) {
|
||||
return false; // Rectangles exceed texture bounds
|
||||
}
|
||||
}*/
|
||||
|
||||
for (int dstY = 0; dstY < dstrect.h; ++dstY) {
|
||||
for (int dstX = 0; dstX < dstrect.w; ++dstX) {
|
||||
|
@ -351,25 +350,28 @@ int texture_blit(texture *src, texture *dst, rect srcrect, rect dstrect, int til
|
|||
|
||||
rgba srccolor = get_pixel(src->data, srcIndex);
|
||||
rgba dstcolor = get_pixel(dst->data, dstIndex);
|
||||
write_pixel(dst->data, dstIndex, blend_colors(srccolor, dstcolor));
|
||||
rgba color = blend_colors(srccolor, dstcolor);
|
||||
write_pixel(dst->data, dstIndex, color);
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int texture_fill_rect(texture *tex, int x, int y, int w, int h, struct rgba color)
|
||||
int texture_fill_rect(texture *tex, struct rect rect, struct rgba color)
|
||||
{
|
||||
if (!tex || !tex->data) return 0;
|
||||
|
||||
int x_end = x+w;
|
||||
int y_end = y+h;
|
||||
int x_end = rect.x+rect.w;
|
||||
int y_end = rect.y+rect.h;
|
||||
|
||||
if (x < 0 || y < 0 || x_end > tex->width || y_end > tex->height) return 0;
|
||||
if (rect.x < 0 || rect.y < 0 || x_end > tex->width || y_end > tex->height) return 0;
|
||||
|
||||
for (int j = y; j < y_end; ++j)
|
||||
for (int i = x; i < x_end; ++i)
|
||||
for (int j = rect.y; j < y_end; ++j)
|
||||
for (int i = rect.x; i < x_end; ++i) {
|
||||
int index = (j*tex->width+i)*4;
|
||||
write_pixel(tex->data, index, color);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
@ -403,16 +405,16 @@ int texture_flip(texture *tex, int y)
|
|||
if (y) {
|
||||
for (int row = 0; row < height / 2; ++row) {
|
||||
for (int col = 0; col < width; ++col) {
|
||||
unsigned char *top = &tex->data[(row*width+col)*4];
|
||||
unsigned char *bottom = &tex->data[((height-row-1)*width+col)*4];
|
||||
unsigned char *top = tex->data+((row*width+col)*4);
|
||||
unsigned char *bottom = tex->data+(((height-row-1)*width+col)*4);
|
||||
swap_pixels(top,bottom);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (int row = 0; row < height; ++row) {
|
||||
for (int col = 0; col < width / 2; ++col) {
|
||||
unsigned char *left = &tex->data[(row*width+col)*4];
|
||||
unsigned char *right = &tex->data[(row*width+(width-col-1))*4];
|
||||
unsigned char *left = tex->data+((row*width+col)*4);
|
||||
unsigned char *right = tex->data+((row*width+(width-col-1))*4);
|
||||
swap_pixels(left,right);
|
||||
}
|
||||
}
|
||||
|
@ -476,6 +478,21 @@ sg_image_data tex_img_data(texture *tex, int mipmaps)
|
|||
}
|
||||
}
|
||||
|
||||
int texture_fill(texture *tex, struct rgba color)
|
||||
{
|
||||
if (!tex || !tex->data) return 0; // Ensure valid texture and pixel data
|
||||
|
||||
// Loop through every pixel in the texture
|
||||
for (int y = 0; y < tex->height; ++y) {
|
||||
for (int x = 0; x < tex->width; ++x) {
|
||||
int index = (y * tex->width + x) * 4;
|
||||
write_pixel(tex->data, index, color);
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void texture_load_gpu(texture *tex)
|
||||
{
|
||||
if (!tex->data) return;
|
||||
|
|
|
@ -53,7 +53,8 @@ void texture_offload(texture *tex); // Remove the data from this texture
|
|||
void texture_load_gpu(texture *tex); // Upload this data to the GPU if it isn't already there. Replace it if it is.
|
||||
|
||||
int texture_write_pixel(texture *tex, int x, int y, struct rgba color);
|
||||
int texture_fill_rect(texture *tex, int x, int y, int w, int h, struct rgba color);
|
||||
int texture_fill(texture *tex, struct rgba color);
|
||||
int texture_fill_rect(texture *tex, struct rect rect, struct rgba color);
|
||||
int texture_blit(texture *dst, texture *src, struct rect dstrect, struct rect srcrect, int tile); // copies src into dst, using their respective squares, scaling if necessary
|
||||
int texture_flip(texture *tex, int y);
|
||||
|
||||
|
|
Loading…
Reference in a new issue