eco2d/code/game/source/editors/texed_ops.c

159 lines
6.1 KiB
C
Raw Normal View History

2021-05-16 14:11:29 +00:00
static inline
float texed_map_value(float v, float min, float max);
2021-05-15 15:19:50 +00:00
void texed_process_ops(void) {
2021-05-17 05:53:10 +00:00
for (int i = 0; i <= ctx.img_pos; i+=1)
UnloadImage(ctx.img[i]);
ctx.img_pos = -1;
2021-05-15 15:19:50 +00:00
for (int i = 0; i < zpl_array_count(ctx.ops); i += 1) {
td_op *op = &ctx.ops[i];
if (op->is_hidden) continue;
zpl_printf("processing op: %s ... \n", op->name);
switch (op->kind) {
2021-05-17 05:53:10 +00:00
case TOP_PUSH_IMAGE:
2021-05-15 17:17:47 +00:00
case TOP_NEW_IMAGE: {
2021-05-17 05:53:10 +00:00
texed_img_push(op->params[0].i32, op->params[1].i32, op->params[2].color);
}break;
case TOP_POP_IMAGE: {
texed_img_pop(op->params[0].i32,
op->params[1].i32,
op->params[2].i32,
op->params[3].i32,
op->params[4].color);
2021-05-15 15:19:50 +00:00
}break;
case TOP_DRAW_RECT: {
2021-05-17 05:53:10 +00:00
ImageDrawRectangle(&ctx.img[ctx.img_pos],
2021-05-15 15:19:50 +00:00
op->params[0].i32,
op->params[1].i32,
op->params[2].i32,
op->params[3].i32,
op->params[4].color);
}break;
case TOP_DRAW_LINE: {
2021-05-17 05:53:10 +00:00
ImageDrawLine(&ctx.img[ctx.img_pos],
2021-05-15 15:19:50 +00:00
op->params[0].i32,
op->params[1].i32,
op->params[2].i32,
op->params[3].i32,
op->params[4].color);
}break;
case TOP_DITHER: {
2021-05-17 05:53:10 +00:00
ImageDither(&ctx.img[ctx.img_pos],
2021-05-15 15:19:50 +00:00
op->params[0].i32,
op->params[1].i32,
op->params[2].i32,
op->params[3].i32);
}break;
2021-05-16 13:38:30 +00:00
case TOP_DRAW_IMAGE: {
2021-05-15 20:09:25 +00:00
char const *str = zpl_bprintf("art/%s", op->params[0].str);
if (FileExists(str)) {
Image img = LoadImage(str);
2021-05-15 15:19:50 +00:00
int x = op->params[1].i32;
int y = op->params[2].i32;
int w = op->params[3].i32;
int h = op->params[4].i32;
2021-05-16 14:58:08 +00:00
int flip = op->params[5].i32;
int rotate = op->params[6].i32;
2021-05-15 15:19:50 +00:00
2021-05-17 05:53:10 +00:00
if (w != 0 || h != 0) {
ImageResize(&img, w != 0 ? w : img.width, h != 0 ? h : img.height);
2021-05-15 15:19:50 +00:00
}
2021-05-16 14:58:08 +00:00
if (flip == 1) {
ImageFlipVertical(&img);
} else if (flip == 2) {
ImageFlipHorizontal(&img);
}
if (rotate == 1) {
ImageRotateCW(&img);
} else if (rotate == 2) {
ImageRotateCCW(&img);
}
2021-05-17 05:53:10 +00:00
ImageDraw(&ctx.img[ctx.img_pos], img,
2021-05-15 15:19:50 +00:00
(Rectangle){0.0f, 0.0f, img.width, img.height},
(Rectangle){x, y, img.width, img.height},
op->params[5].color);
UnloadImage(img);
} else {
2021-05-15 20:09:25 +00:00
zpl_printf("TOP_LOAD_IMAGE: src %s not found!\n", str);
2021-05-15 15:19:50 +00:00
}
}break;
2021-05-15 16:41:30 +00:00
case TOP_DRAW_TEXT: {
char const *str = op->params[0].str;
int x = op->params[1].i32;
int y = op->params[2].i32;
int size = op->params[3].i32;
Color color = op->params[4].color;
2021-05-17 05:53:10 +00:00
ImageDrawText(&ctx.img[ctx.img_pos], str, x, y, size, color);
2021-05-15 16:41:30 +00:00
}break;
2021-05-15 17:40:27 +00:00
case TOP_RESIZE_IMAGE: {
2021-05-17 05:53:10 +00:00
if (ctx.img[ctx.img_pos].width == 0) break;
2021-05-15 17:40:27 +00:00
int w = op->params[0].i32;
int h = op->params[1].i32;
int mode = op->params[2].i32;
if (mode) {
2021-05-17 05:53:10 +00:00
ImageResize(&ctx.img[ctx.img_pos], w, h);
2021-05-15 17:40:27 +00:00
} else {
2021-05-17 05:53:10 +00:00
ImageResizeNN(&ctx.img[ctx.img_pos], w, h);
2021-05-15 17:40:27 +00:00
}
}break;
2021-05-16 14:22:28 +00:00
case TOP_COLOR_TWEAKS: {
2021-05-17 05:53:10 +00:00
ImageColorContrast(&ctx.img[ctx.img_pos], texed_map_value(op->params[0].flt, -100.0f, 100.0f));
ImageColorBrightness(&ctx.img[ctx.img_pos], (int)texed_map_value(op->params[1].flt, -255.0f, 255.0f));
ImageColorTint(&ctx.img[ctx.img_pos], op->params[2].color);
2021-05-16 14:58:08 +00:00
if (op->params[3].i32) {
2021-05-17 05:53:10 +00:00
ImageColorInvert(&ctx.img[ctx.img_pos]);
2021-05-16 14:58:08 +00:00
}
if (op->params[4].i32) {
2021-05-17 05:53:10 +00:00
ImageColorGrayscale(&ctx.img[ctx.img_pos]);
2021-05-16 14:58:08 +00:00
}
2021-05-16 14:11:29 +00:00
}break;
2021-05-15 15:19:50 +00:00
default: {
zpl_printf("%s\n", "unsupported op!");
}break;
}
}
}
void texed_process_params(void) {
for (int i = 0; i < zpl_array_count(ctx.ops); i += 1) {
td_op *op = &ctx.ops[i];
for (int j = 0; j < op->num_params; j += 1) {
td_param *p = &op->params[j];
switch (p->kind) {
2021-05-16 14:11:29 +00:00
case TPARAM_SLIDER:
2021-05-15 15:19:50 +00:00
case TPARAM_FLOAT: {
p->flt = (float)zpl_str_to_f64(p->str, NULL);
}break;
2021-05-16 14:58:08 +00:00
case TPARAM_INT:
2021-05-15 16:41:30 +00:00
case TPARAM_COORD: {
p->i32 = (int32_t)zpl_str_to_i64(p->str, NULL, 10);
2021-05-15 15:19:50 +00:00
}break;
case TPARAM_COLOR: {
uint32_t color = (uint32_t)zpl_str_to_u64(p->str, NULL, 16);
p->color = GetColor(color);
}break;
case TPARAM_STRING: {
// NOTE(zaklaus): no-op
}break;
default: {
zpl_printf("%s\n", "unsupported param!");
}break;
}
}
}
}
2021-05-16 14:11:29 +00:00
static inline
float texed_map_value(float v, float min, float max) {
float slope = max-min;
return min + zpl_round(slope * v);
}