Add basic app interface
- Add pool struct for data pools
This commit is contained in:
+174
-10
@@ -1,25 +1,189 @@
|
||||
#include "binding/ruby.h"
|
||||
#include "localization/localization.h"
|
||||
#include "utils.h"
|
||||
#include "window/window.h"
|
||||
|
||||
namespace app {
|
||||
struct Element {
|
||||
Vec2<float> position;
|
||||
float rotation;
|
||||
Vec2<float> scale;
|
||||
float z;
|
||||
Vec2<float> position = {0, 0};
|
||||
float rotation = 0;
|
||||
Vec2<float> scale = {1, 1};
|
||||
float z = 0;
|
||||
bool click_through = false;
|
||||
Ruby::Block on_click;
|
||||
Ruby::Block on_update;
|
||||
Ruby::Block on_hover;
|
||||
|
||||
virtual void render(Window &window) {}
|
||||
virtual ~Element() = default;
|
||||
virtual void render(Window &window, uint64_t abs_time) = 0;
|
||||
virtual Vec2<float> size(Window *window) = 0;
|
||||
|
||||
void click() {
|
||||
if (!mrb_nil_p(on_click.proc)) {
|
||||
int x = mrb_gc_arena_save(Ruby::mrb);
|
||||
on_click.call();
|
||||
mrb_gc_arena_restore(Ruby::mrb, x);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct Image : Element {
|
||||
Animation animation;
|
||||
float alpha = 1.0f;
|
||||
|
||||
Image(Animation animation) : animation(std::move(animation)) {}
|
||||
|
||||
Vec2<float> size(Window *window) override {
|
||||
if (animation.frames.empty())
|
||||
return {0, 0};
|
||||
return window->get_image_size(animation.frames[0]);
|
||||
}
|
||||
|
||||
void render(Window &window, uint64_t abs_time) override {
|
||||
if (animation.frames.empty())
|
||||
return;
|
||||
|
||||
int frame_index = (int)((abs_time * animation.frame_rate) / 1000) % animation.frames.size();
|
||||
window.draw(animation.frames[frame_index], position, z, rotation, scale, alpha);
|
||||
}
|
||||
};
|
||||
|
||||
struct Text : Element {
|
||||
Localization::Key key;
|
||||
int font_id;
|
||||
int font_size = 16;
|
||||
Color color = {255, 255, 255, 255};
|
||||
|
||||
Text(Localization::Key key, int font_id, int font_size, Color color)
|
||||
: key(key), font_id(font_id), font_size(font_size), color(color) {}
|
||||
|
||||
Vec2<float> size(Window *window) override {
|
||||
return window->get_text_size(text_id);
|
||||
};
|
||||
|
||||
void render(Window &window, uint64_t abs_time) override {
|
||||
if (last_language != Localization::current_language || last_key != key || text_id == -1) {
|
||||
std::string localized_str = Localization::get(key);
|
||||
if (text_id != -1)
|
||||
window.unload_text(text_id);
|
||||
text_id = window.create_text(font_id, localized_str.c_str(), localized_str.size(), color);
|
||||
last_language = Localization::current_language;
|
||||
last_key = key;
|
||||
}
|
||||
window.write(text_id, position, z, rotation, scale);
|
||||
}
|
||||
|
||||
private:
|
||||
int text_id = -1;
|
||||
Localization::LanguageCode last_language = -1;
|
||||
Localization::Key last_key = -1;
|
||||
};
|
||||
|
||||
struct ERect : Element {
|
||||
Vec2<float> shape = {0, 0};
|
||||
Color color = {255, 255, 255, 255};
|
||||
Vec2<float> size(Window *) override {
|
||||
return shape;
|
||||
};
|
||||
|
||||
ERect(Vec2<float> shape, Color color) : shape(shape), color(color) {}
|
||||
|
||||
void render(Window &window, uint64_t abs_time) override {
|
||||
window.draw_rect(Rect::from_size(position, shape), color, z, rotation);
|
||||
}
|
||||
};
|
||||
|
||||
extern Pool<std::unique_ptr<Element>> element_pool;
|
||||
|
||||
struct Scene {
|
||||
std::vector<Element> elements;
|
||||
std::vector<int> element_ids;
|
||||
Ruby::Block update;
|
||||
|
||||
void add_element(int element_id) {
|
||||
auto it = std::lower_bound(
|
||||
element_ids.begin(),
|
||||
element_ids.end(),
|
||||
element_id,
|
||||
[](auto &a, auto &b) {
|
||||
return element_pool[a]->z < element_pool[b]->z || (element_pool[a]->z == element_pool[b]->z && a < b);
|
||||
}
|
||||
);
|
||||
element_ids.insert(it, element_id);
|
||||
}
|
||||
|
||||
void update_scene(uint64_t delta_time) {
|
||||
if (!mrb_nil_p(update.proc)) {
|
||||
int x = mrb_gc_arena_save(Ruby::mrb);
|
||||
mrb_value dt_val = mrb_int_value(Ruby::mrb, delta_time);
|
||||
update.call(1, &dt_val);
|
||||
mrb_gc_arena_restore(Ruby::mrb, x);
|
||||
}
|
||||
}
|
||||
|
||||
void click(Window *window, Vec2<float> position) {
|
||||
for (auto it = element_ids.rbegin(); it != element_ids.rend(); ++it) {
|
||||
Element *e = element_pool[*it].get();
|
||||
Vec2<float> size = e->size(window);
|
||||
Rect rect = Rect::from_size(e->position, size * e->scale);
|
||||
if (rect.contains(position)) {
|
||||
e->click();
|
||||
if (e->click_through)
|
||||
continue;
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void render(Window &window, uint64_t abs_time) {
|
||||
for (const int &e : element_ids)
|
||||
element_pool[e]->render(window, abs_time);
|
||||
}
|
||||
};
|
||||
|
||||
extern Pool<std::unique_ptr<Scene>> scene_pool;
|
||||
|
||||
struct App {
|
||||
Window window;
|
||||
int current_scene_id = 0;
|
||||
bool running = true;
|
||||
|
||||
App(Vec2<float> size, const char *title) : window(size, title) {}
|
||||
};
|
||||
App(Vec2<float> size, const char *title, Animation loading_animation)
|
||||
: window(size, title) {
|
||||
std::unique_ptr<Scene> loading_scene = std::make_unique<Scene>();
|
||||
#warning "Should try to center the loading image or scale it to fit the screen"
|
||||
std::unique_ptr<Image> image = std::make_unique<Image>(loading_animation);
|
||||
int image_id = element_pool.acquire(std::move(image));
|
||||
loading_scene->add_element(image_id);
|
||||
scene_pool.acquire(std::move(loading_scene));
|
||||
}
|
||||
|
||||
void loop() {
|
||||
while (running) {
|
||||
uint64_t dt = window.delta_time();
|
||||
uint64_t abs_time = window.get_time();
|
||||
|
||||
Scene *scene = scene_pool[current_scene_id].get();
|
||||
|
||||
scene->update_scene(dt);
|
||||
|
||||
Event e;
|
||||
while (window.poll(e)) {
|
||||
switch (e.type) {
|
||||
case Event::QUIT:
|
||||
running = false;
|
||||
break;
|
||||
case Event::MOUSE_BUTTON_DOWN:
|
||||
scene->click(&window, e.mouse_button.position);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
window.consume(e);
|
||||
}
|
||||
|
||||
scene->render(window, abs_time);
|
||||
|
||||
window.render();
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace app
|
||||
+14
-1
@@ -7,7 +7,20 @@ struct Block {
|
||||
mrb_value proc;
|
||||
|
||||
Block(mrb_value proc) : proc(proc) {
|
||||
mrb_gc_protect(mrb, proc);
|
||||
mrb_gc_register(mrb, proc);
|
||||
}
|
||||
|
||||
Block() : proc(mrb_nil_value()) {}
|
||||
|
||||
Block(const Block &) = delete;
|
||||
Block &operator=(const Block &) = delete;
|
||||
|
||||
mrb_value call(int argc = 0, mrb_value *argv = nullptr) const {
|
||||
return mrb_funcall(mrb, proc, "call", argc, argv);
|
||||
}
|
||||
|
||||
~Block() {
|
||||
mrb_gc_unregister(mrb, proc);
|
||||
}
|
||||
};
|
||||
}; // namespace Ruby
|
||||
@@ -0,0 +1,28 @@
|
||||
#include "pch.h"
|
||||
#include "utils.h"
|
||||
|
||||
namespace Localization {
|
||||
using Key = uint16_t;
|
||||
|
||||
// Maps localization keys to their string identifiers. This is used for text elements to look up the correct localized string.
|
||||
extern std::unordered_map<std::string, Key> localization_key_map;
|
||||
|
||||
using LanguageCode = uint32_t; // 32-bit integer to store the language code (e.g., "en", "fr", "es")
|
||||
|
||||
extern LanguageCode current_language; // The currently active language code
|
||||
|
||||
using JoinedKey = uint64_t; // 64-bit integer to store the joined language code and key
|
||||
|
||||
inline JoinedKey join_keys(LanguageCode lang_code, Key loc_key) {
|
||||
return (static_cast<JoinedKey>(lang_code) << 16) | loc_key;
|
||||
}
|
||||
|
||||
extern std::unordered_map<JoinedKey, std::string> localized_strings;
|
||||
|
||||
inline std::string get(Key key) {
|
||||
auto it = localized_strings.find(join_keys(current_language, key));
|
||||
if (it != localized_strings.end())
|
||||
return it->second;
|
||||
return "";
|
||||
}
|
||||
} // namespace Localization
|
||||
@@ -14,6 +14,16 @@ template <typename T>
|
||||
struct Vec2 {
|
||||
T x, y;
|
||||
|
||||
template <typename U>
|
||||
Vec2<float> operator*(Vec2<U> s) {
|
||||
return {x * s.x, y * s.y};
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
Vec2<float> operator/(Vec2<U> s) {
|
||||
return {x / s.x, y / s.y};
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
Vec2 operator*(U s) const {
|
||||
return {x * s, y * s};
|
||||
@@ -98,6 +108,10 @@ struct Rect {
|
||||
};
|
||||
}
|
||||
|
||||
static Rect from_size(Vec2<float> position, Vec2<float> size) {
|
||||
return Rect(position, position + size);
|
||||
}
|
||||
|
||||
bool contains(const Vec2<float> &point) const {
|
||||
return point.x >= diagonal.start.x && point.x <= diagonal.end.x &&
|
||||
point.y >= diagonal.start.y && point.y <= diagonal.end.y;
|
||||
@@ -142,4 +156,44 @@ struct Rect {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct Pool {
|
||||
Pool() {
|
||||
items.reserve(20);
|
||||
free_indices.reserve(20);
|
||||
}
|
||||
|
||||
T &operator[](int index) {
|
||||
return items[index];
|
||||
}
|
||||
|
||||
const std::vector<T> &all() {
|
||||
return items;
|
||||
}
|
||||
|
||||
int acquire(const T &item) {
|
||||
if (!free_indices.empty()) {
|
||||
int index = free_indices.back();
|
||||
free_indices.pop_back();
|
||||
items[index] = item;
|
||||
return index;
|
||||
} else {
|
||||
items.push_back(item);
|
||||
return items.size() - 1;
|
||||
}
|
||||
}
|
||||
|
||||
void release(int index) {
|
||||
free_indices.push_back(index);
|
||||
}
|
||||
|
||||
bool is_valid(size_t index) const {
|
||||
return index >= 0 && index < items.size() && std::find(free_indices.begin(), free_indices.end(), index) == free_indices.end();
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<T> items;
|
||||
std::vector<int> free_indices;
|
||||
};
|
||||
|
||||
#endif
|
||||
+21
-20
@@ -4,10 +4,6 @@
|
||||
#include "pch.h"
|
||||
#include "utils.h"
|
||||
|
||||
using ImageID = uint32_t;
|
||||
using FontID = uint32_t;
|
||||
using TextID = uint32_t;
|
||||
|
||||
struct Font {
|
||||
TTF_Font *font;
|
||||
bool pixel_art;
|
||||
@@ -52,6 +48,12 @@ struct Event {
|
||||
};
|
||||
};
|
||||
|
||||
struct Animation {
|
||||
std::vector<int> frames;
|
||||
float frame_rate;
|
||||
bool loop;
|
||||
};
|
||||
|
||||
struct Window {
|
||||
public:
|
||||
// title can be free'd by the caller after the window is created, as it's copied internally.
|
||||
@@ -61,19 +63,21 @@ public:
|
||||
|
||||
// Rendering functions
|
||||
|
||||
ImageID load_image(const char *path, bool pixel_art = false);
|
||||
void unload_image(ImageID image_id);
|
||||
int load_image(const char *path, bool pixel_art = false);
|
||||
void unload_image(int image_id);
|
||||
Vec2<float> get_image_size(int image_id);
|
||||
|
||||
FontID load_font(const char *path, int font_size, bool bold = false, bool italic = false, bool underline = false, bool pixel_art = false);
|
||||
void unload_font(FontID font_id);
|
||||
int load_font(const char *path, int font_size, bool bold = false, bool italic = false, bool underline = false, bool pixel_art = false);
|
||||
void unload_font(int font_id);
|
||||
|
||||
// text is copied internally, so it can be free'd by the caller after the text is created.
|
||||
TextID create_text(FontID font_id, const char *text, uint32_t length, Color color);
|
||||
void unload_text(TextID text_id);
|
||||
int create_text(int font_id, const char *text, uint32_t length, Color color);
|
||||
void unload_text(int text_id);
|
||||
Vec2<float> get_text_size(int text_id);
|
||||
|
||||
bool write(TextID text_id, Vec2<float> position, float z, float angle = 0.0f, Vec2<float> scale = {1.0f, 1.0f});
|
||||
bool write(int text_id, Vec2<float> position, float z, float angle = 0.0f, Vec2<float> scale = {1.0f, 1.0f});
|
||||
|
||||
bool draw(ImageID image_id, Vec2<float> position, float z, float angle = 0.0f, Vec2<float> scale = {1.0f, 1.0f}, float alpha = 1.0f);
|
||||
bool draw(int image_id, Vec2<float> position, float z, float angle = 0.0f, Vec2<float> scale = {1.0f, 1.0f}, float alpha = 1.0f);
|
||||
|
||||
bool draw_rect(Rect rect, Color color, float z, float angle = 0.0f);
|
||||
|
||||
@@ -119,16 +123,13 @@ private:
|
||||
SDL_Texture *white_tex;
|
||||
|
||||
// Image pool
|
||||
std::vector<SDL_Texture *> images;
|
||||
std::vector<int> free_image_indices;
|
||||
Pool<SDL_Texture *> image_pool;
|
||||
|
||||
// Font pool
|
||||
std::vector<struct Font> fonts;
|
||||
std::vector<int> free_font_indices;
|
||||
Pool<struct Font> font_pool;
|
||||
|
||||
// Text pool
|
||||
std::vector<SDL_Texture *> texts;
|
||||
std::vector<int> free_text_indices;
|
||||
Pool<SDL_Texture *> text_pool;
|
||||
|
||||
struct RenderInstruction {
|
||||
enum Type {
|
||||
@@ -143,13 +144,13 @@ private:
|
||||
|
||||
union {
|
||||
struct {
|
||||
ImageID image_id;
|
||||
int image_id;
|
||||
Vec2<float> position;
|
||||
float alpha;
|
||||
} draw_image;
|
||||
|
||||
struct {
|
||||
TextID text_id;
|
||||
int text_id;
|
||||
Vec2<float> position;
|
||||
} draw_text;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user