Remove element script and allow images to be pixel updated for similar behaviour

This commit is contained in:
2026-06-05 11:00:15 +01:00
parent e7418fde08
commit 419e95ac89
9 changed files with 177 additions and 518 deletions
+110 -144
View File
@@ -57,17 +57,52 @@ Window::~Window() {
}
uint64_t Window::load_image(const char *path, bool pixel_art) {
SDL_Texture *texture = IMG_LoadTexture(renderer, path);
SDL_Surface *surface = IMG_Load(path);
if (!surface) {
fprintf(stderr, "Failed to load image: %s\n", SDL_GetError());
return UINT64_MAX;
}
SDL_Surface *rgba = SDL_ConvertSurface(
surface,
SDL_PIXELFORMAT_RGBA8888
);
SDL_DestroySurface(surface);
if (!rgba) {
fprintf(stderr, "Failed to convert image: %s\n", SDL_GetError());
return UINT64_MAX;
}
SDL_Texture *texture = SDL_CreateTexture(
renderer,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STREAMING,
rgba->w,
rgba->h
);
if (!texture) {
SDL_DestroySurface(rgba);
fprintf(stderr, "Failed to create texture: %s\n", SDL_GetError());
return UINT64_MAX;
}
SDL_UpdateTexture(
texture,
nullptr,
rgba->pixels,
rgba->pitch
);
SDL_DestroySurface(rgba);
if (pixel_art) {
SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
}
if (texture == nullptr) {
fprintf(stderr, "Failed to load image: %s\n", SDL_GetError());
return UINT64_MAX;
}
float w, h;
SDL_GetTextureSize(texture, &w, &h);
@@ -78,6 +113,75 @@ uint64_t Window::load_image(const char *path, bool pixel_art) {
return id;
}
uint64_t Window::create_image(Vec2<float> size, bool pixel_art) {
SDL_Texture *texture = SDL_CreateTexture(
renderer,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STREAMING,
(int)size.x,
(int)size.y
);
if (!texture) {
fprintf(stderr, "Failed to create scripted texture: %s\n", SDL_GetError());
return UINT64_MAX;
}
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
if (pixel_art)
SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
Image *img = new Image{texture, size};
uint64_t id = image_pool.acquire(img);
image_pool.use(id);
char *buffer = (char *)malloc(size.x * size.y * 4 * sizeof(char));
for (unsigned int i = 0; i < (unsigned int)(size.x * size.y * 4); i += 4) {
buffer[i] = 0xFF;
buffer[i + 1] = 0xFF;
buffer[i + 2] = 0xFF;
buffer[i + 3] = 0xFF;
}
SDL_UpdateTexture(
img->texture,
nullptr,
buffer,
(int)(size.x * 4)
);
free(buffer);
return id;
}
bool Window::update_image(uint64_t image_id, Rect content_rect, const char *new_content, uint32_t length) {
if (!image_pool.is_valid(image_id)) {
fprintf(stderr, "Invalid image ID: %llu\n", (unsigned long long)image_id);
return false;
}
Image *img = image_pool[image_id];
SDL_Rect sdl_rect = {
(int)content_rect.x(),
(int)content_rect.y(),
(int)content_rect.w(),
(int)content_rect.h()
};
SDL_UpdateTexture(
img->texture,
&sdl_rect,
new_content,
(int)(content_rect.w() * 4)
);
return true;
};
Vec2<float> Window::get_image_size(uint64_t image_id) {
if (!image_pool.is_valid(image_id)) {
fprintf(stderr, "Invalid image ID: %llu\n", (unsigned long long)image_id);
@@ -177,83 +281,6 @@ bool Window::write(uint64_t text_id, Vec2<float> position, float z, float angle,
return true;
};
uint64_t Window::create_scripted(Vec2<float> size, bool pixel_art) {
SDL_Texture *texture = SDL_CreateTexture(
renderer,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STREAMING,
(int)size.x,
(int)size.y
);
if (!texture) {
fprintf(stderr, "Failed to create scripted texture: %s\n", SDL_GetError());
return UINT64_MAX;
}
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
if (pixel_art)
SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
Scripted *s = new Scripted{texture, size};
uint64_t id = scripted_pool.acquire(s);
scripted_pool.use(id);
char *buffer = (char *)malloc(size.x * size.y * 4 * sizeof(char));
for (unsigned int i = 0; i < (unsigned int)(size.x * size.y * 4); i += 4) {
buffer[i] = 0xFF;
buffer[i + 1] = 0xFF;
buffer[i + 2] = 0xFF;
buffer[i + 3] = 0xFF;
}
SDL_UpdateTexture(
s->texture,
nullptr,
buffer,
(int)(size.x * 4)
);
free(buffer);
return id;
}
Vec2<float> Window::get_scripted_size(uint64_t scripted_id) {
if (!scripted_pool.is_valid(scripted_id)) {
fprintf(stderr, "Invalid scripted ID: %llu\n", (unsigned long long)scripted_id);
return {0, 0};
}
return scripted_pool[scripted_id]->size;
}
bool Window::update_scripted(uint64_t scripted_id, Rect content_rect, const char *new_content, uint32_t length) {
if (!scripted_pool.is_valid(scripted_id)) {
fprintf(stderr, "Invalid scripted ID: %llu\n", (unsigned long long)scripted_id);
return false;
}
Scripted *s = scripted_pool[scripted_id];
SDL_Rect sdl_rect = {
(int)content_rect.x(),
(int)content_rect.y(),
(int)content_rect.w(),
(int)content_rect.h()
};
SDL_UpdateTexture(
s->texture,
&sdl_rect,
new_content,
(int)(content_rect.w() * 4)
);
return true;
};
bool Window::draw(uint64_t image_id, Vec2<float> position, float z, float angle, Vec2<float> pivot, Vec2<float> scale, float alpha) {
if (!image_pool.is_valid(image_id)) {
fprintf(stderr, "Invalid image ID: %llu\n", (unsigned long long)image_id);
@@ -293,28 +320,6 @@ bool Window::draw_rect(Rect rect, Color color, float z, float angle, Vec2<float>
return true;
};
bool Window::draw_scripted(uint64_t scripted_id, Vec2<float> position, float z, float angle, Vec2<float> pivot, Vec2<float> scale, float alpha) {
if (!scripted_pool.is_valid(scripted_id)) {
fprintf(stderr, "Invalid scripted ID: %llu\n", (unsigned long long)scripted_id);
return false;
}
render_instructions.push_back(
{.type = RenderInstruction::DRAW_SCRIPTED,
.z = z,
.angle = angle,
.pivot = pivot,
.scale = scale,
.alpha = alpha,
.draw_scripted = {
.scripted_id = scripted_id,
.position = position,
}}
);
return true;
};
void Window::draw_letterbox() {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
@@ -434,45 +439,6 @@ void Window::render() {
);
} break;
case RenderInstruction::DRAW_SCRIPTED: {
Scripted *scripted = scripted_pool[instruction.draw_scripted.scripted_id];
SDL_Texture *texture = scripted->texture;
SDL_FlipMode flip = SDL_FLIP_NONE;
if (instruction.scale.x < 0)
flip = (SDL_FlipMode)(flip | SDL_FLIP_HORIZONTAL);
if (instruction.scale.y < 0)
flip = (SDL_FlipMode)(flip | SDL_FLIP_VERTICAL);
float scale_x = std::abs(instruction.scale.x) * scale;
float scale_y = std::abs(instruction.scale.y) * scale;
SDL_FRect dst_rect;
dst_rect.x = instruction.draw_scripted.position.x * scale + offset.x;
dst_rect.y = instruction.draw_scripted.position.y * scale + offset.y;
dst_rect.w = scale_x * scripted->size.x;
dst_rect.h = scale_y * scripted->size.y;
SDL_SetTextureAlphaModFloat(texture, instruction.alpha);
SDL_FPoint pivot_point = {
instruction.pivot.x * scale_x,
instruction.pivot.y * scale_y
};
SDL_RenderTextureRotated(
renderer,
texture,
nullptr,
&dst_rect,
instruction.angle,
&pivot_point,
flip
);
} break;
case RenderInstruction::DRAW_RECT: {
SDL_FRect dst_rect;
dst_rect.x = instruction.draw_rect.rect.x() * scale + offset.x;