Added pixel texture element

This commit is contained in:
2026-06-04 23:46:34 +01:00
parent 55241b9c93
commit e7418fde08
9 changed files with 567 additions and 30 deletions
+138
View File
@@ -177,6 +177,83 @@ 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);
@@ -216,6 +293,28 @@ 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);
@@ -335,6 +434,45 @@ 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;