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
+32 -2
View File
@@ -8,7 +8,8 @@
enum Type { IMAGE,
TEXT,
RECT };
RECT,
SCRIPT };
enum ClickMode { PASS,
BLOCK,
@@ -72,8 +73,9 @@ private:
};
struct ERect : Element {
Vec2<float> shape = {0, 0};
Color color = {255, 255, 255};
Vec2<float> shape = {0, 0};
Vec2<float> size() override;
ERect(Vec2<float> shape, Color color) : Element(Type::RECT), shape(shape), color(color) {}
@@ -81,6 +83,34 @@ struct ERect : Element {
void render(uint64_t) override;
};
struct EScript : Element {
Ruby::Block script;
uint64_t scripted_id = UINT64_MAX;
bool pixel_art = false;
Vec2<float> shape;
EScript(Vec2<float> shape) : Element(Type::SCRIPT), shape(shape) {
scripted_id = window.create_scripted(shape, pixel_art);
}
~EScript() {
if (scripted_id != UINT64_MAX)
scripted_pool.release(scripted_id);
};
Vec2<float> size() override {
return shape;
}
bool update_scripted(Rect content_rect, const char *new_content, uint32_t length) {
return window.update_scripted(scripted_id, content_rect, new_content, length);
}
void render(uint64_t) override {
window.draw_scripted(scripted_id, position, z, rotation, pivot, scale, alpha);
};
};
inline Pool<Element> element_pool;
} // namespace app
+3
View File
@@ -18,6 +18,7 @@ inline struct RubyBindings {
Binding ElementText;
Binding ElementRect;
Binding ElementImage;
Binding ElementScript;
Binding Scene;
} bindings;
@@ -43,6 +44,7 @@ void register_animation();
void register_element_text();
void register_element_rect();
void register_element_image();
void register_element_script();
void register_scene();
void register_app();
@@ -55,6 +57,7 @@ inline void setup() {
register_element_text();
register_element_rect();
register_element_image();
register_element_script();
register_scene();
register_app();
}
+27 -2
View File
@@ -32,6 +32,15 @@ struct Text {
}
};
struct Scripted {
SDL_Texture *texture;
Vec2<float> size;
~Scripted() {
SDL_DestroyTexture(texture);
}
};
// Image pool
inline Pool<Image> image_pool;
@@ -41,6 +50,9 @@ inline Pool<Font> font_pool;
// Text pool
inline Pool<Text> text_pool;
// Scripted pool
inline Pool<Scripted> scripted_pool;
struct Event {
enum Type {
NONE,
@@ -107,12 +119,19 @@ public:
uint64_t create_text(uint64_t font_id, const char *text, uint32_t length, Color color);
Vec2<float> get_text_size(uint64_t text_id);
uint64_t create_scripted(Vec2<float> size, bool pixel_art);
Vec2<float> get_scripted_size(uint64_t scripted_id);
bool update_scripted(uint64_t scripted_id, Rect content_rect, const char *new_content, uint32_t length);
bool write(uint64_t text_id, Vec2<float> position, float z, float angle, Vec2<float> pivot, Vec2<float> scale, float alpha);
bool draw(uint64_t image_id, Vec2<float> position, float z, float angle, Vec2<float> pivot, Vec2<float> scale, float alpha);
bool draw_rect(Rect rect, Color color, float z, float angle, Vec2<float> pivot, Vec2<float> scale, float alpha);
bool draw_scripted(uint64_t scripted_id, Vec2<float> position, float z, float angle, Vec2<float> pivot, Vec2<float> scale, float alpha);
void render(); // the rest of the calls only build the instruction list, this actually renders everything added to the screen (following z order)
void clear(); // clears the instruction list
@@ -147,7 +166,8 @@ private:
enum Type {
DRAW_IMAGE,
DRAW_TEXT,
DRAW_RECT
DRAW_RECT,
DRAW_SCRIPTED
} type;
float z;
@@ -171,10 +191,15 @@ private:
Rect rect;
Color color;
} draw_rect;
struct {
uint64_t scripted_id;
Vec2<float> position;
} draw_scripted;
};
};
std::vector<struct RenderInstruction> render_instructions;
std::vector<RenderInstruction> render_instructions;
void compute_transform();