Fix main loop, add example and cleanup
This commit is contained in:
+40
-24
@@ -1,3 +1,6 @@
|
||||
#ifndef APP_H
|
||||
#define APP_H
|
||||
|
||||
#include "binding/ruby.h"
|
||||
#include "localization/localization.h"
|
||||
#include "utils.h"
|
||||
@@ -25,11 +28,10 @@ struct Element {
|
||||
}
|
||||
};
|
||||
|
||||
struct Image : Element {
|
||||
struct EImage : Element {
|
||||
Animation animation;
|
||||
float alpha = 1.0f;
|
||||
|
||||
Image(Animation animation) : animation(std::move(animation)) {}
|
||||
EImage(Animation animation) : animation(std::move(animation)) {}
|
||||
|
||||
Vec2<float> size(Window *window) override {
|
||||
if (animation.frames.empty())
|
||||
@@ -42,24 +44,24 @@ struct Image : Element {
|
||||
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);
|
||||
window.draw(animation.frames[frame_index], position, z, rotation, scale);
|
||||
}
|
||||
};
|
||||
|
||||
struct Text : Element {
|
||||
struct EText : 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)
|
||||
EText(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 {
|
||||
void render(Window &window, uint64_t) override {
|
||||
if (last_language != Localization::current_language || last_key != key || text_id == -1) {
|
||||
std::string localized_str = Localization::get(key);
|
||||
if (text_id != -1)
|
||||
@@ -86,12 +88,12 @@ struct ERect : Element {
|
||||
|
||||
ERect(Vec2<float> shape, Color color) : shape(shape), color(color) {}
|
||||
|
||||
void render(Window &window, uint64_t abs_time) override {
|
||||
void render(Window &window, uint64_t) override {
|
||||
window.draw_rect(Rect::from_size(position, shape), color, z, rotation);
|
||||
}
|
||||
};
|
||||
|
||||
extern Pool<std::unique_ptr<Element>> element_pool;
|
||||
inline Pool<Element *> element_pool;
|
||||
|
||||
struct Scene {
|
||||
std::vector<int> element_ids;
|
||||
@@ -120,7 +122,7 @@ struct Scene {
|
||||
|
||||
void click(Window *window, Vec2<float> position) {
|
||||
for (auto it = element_ids.rbegin(); it != element_ids.rend(); ++it) {
|
||||
Element *e = element_pool[*it].get();
|
||||
Element *e = element_pool[*it];
|
||||
Vec2<float> size = e->size(window);
|
||||
Rect rect = Rect::from_size(e->position, size * e->scale);
|
||||
if (rect.contains(position)) {
|
||||
@@ -139,31 +141,39 @@ struct Scene {
|
||||
}
|
||||
};
|
||||
|
||||
extern Pool<std::unique_ptr<Scene>> scene_pool;
|
||||
inline Pool<Scene *> scene_pool;
|
||||
|
||||
struct App {
|
||||
Window window;
|
||||
int current_scene_id = 0;
|
||||
bool running = true;
|
||||
|
||||
App(Vec2<float> size, const char *title, Animation loading_animation)
|
||||
: window(size, title) {
|
||||
std::unique_ptr<Scene> loading_scene = std::make_unique<Scene>();
|
||||
App(Vec2<float> size, const char *title, Animation loading_animation) {
|
||||
window.update(size, title);
|
||||
#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));
|
||||
Scene *loading_scene = new Scene{};
|
||||
EImage *image = new EImage(loading_animation);
|
||||
int image_id = element_pool.acquire(image);
|
||||
loading_scene->add_element(image_id);
|
||||
scene_pool.acquire(std::move(loading_scene));
|
||||
scene_pool.acquire(loading_scene);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
const uint64_t target_ms = 1000 / 60;
|
||||
|
||||
uint64_t time_a = window.get_time();
|
||||
uint64_t time_b = window.get_time();
|
||||
|
||||
while (running) {
|
||||
time_a = window.get_time();
|
||||
uint64_t frame_time = time_a - time_b;
|
||||
if (frame_time <= target_ms)
|
||||
usleep((target_ms - frame_time) * 1000);
|
||||
|
||||
time_b = window.get_time();
|
||||
|
||||
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);
|
||||
Scene *scene = scene_pool[current_scene_id];
|
||||
|
||||
Event e;
|
||||
while (window.poll(e)) {
|
||||
@@ -173,6 +183,7 @@ struct App {
|
||||
break;
|
||||
case Event::MOUSE_BUTTON_DOWN:
|
||||
scene->click(&window, e.mouse_button.position);
|
||||
printf("DT: %lu ms\n", dt);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -180,10 +191,15 @@ struct App {
|
||||
window.consume(e);
|
||||
}
|
||||
|
||||
scene->render(window, abs_time);
|
||||
scene->update_scene(dt);
|
||||
scene->render(window, window.get_time());
|
||||
|
||||
window.render();
|
||||
|
||||
window.clear();
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace app
|
||||
} // namespace app
|
||||
|
||||
#endif
|
||||
@@ -2,7 +2,7 @@
|
||||
#include "utils.h"
|
||||
|
||||
namespace Ruby {
|
||||
extern mrb_state *mrb;
|
||||
inline mrb_state *mrb = mrb_open();
|
||||
struct Block {
|
||||
mrb_value proc;
|
||||
|
||||
|
||||
+2
-7
@@ -158,11 +158,6 @@ struct Rect {
|
||||
|
||||
template <typename T>
|
||||
struct Pool {
|
||||
Pool() {
|
||||
items.reserve(20);
|
||||
free_indices.reserve(20);
|
||||
}
|
||||
|
||||
T &operator[](int index) {
|
||||
return items[index];
|
||||
}
|
||||
@@ -175,10 +170,10 @@ struct Pool {
|
||||
if (!free_indices.empty()) {
|
||||
int index = free_indices.back();
|
||||
free_indices.pop_back();
|
||||
items[index] = item;
|
||||
items[index] = std::move(item);
|
||||
return index;
|
||||
} else {
|
||||
items.push_back(item);
|
||||
items.push_back(std::move(item));
|
||||
return items.size() - 1;
|
||||
}
|
||||
}
|
||||
|
||||
+13
-6
@@ -4,6 +4,11 @@
|
||||
#include "pch.h"
|
||||
#include "utils.h"
|
||||
|
||||
struct Image {
|
||||
SDL_Texture *texture;
|
||||
Vec2<float> size;
|
||||
};
|
||||
|
||||
struct Font {
|
||||
TTF_Font *font;
|
||||
bool pixel_art;
|
||||
@@ -59,11 +64,12 @@ public:
|
||||
// title can be free'd by the caller after the window is created, as it's copied internally.
|
||||
Window(Vec2<float> size, const char *title);
|
||||
|
||||
void update(Vec2<float> new_size, const char *new_title);
|
||||
|
||||
~Window();
|
||||
|
||||
// Rendering functions
|
||||
|
||||
int load_image(const char *path, bool pixel_art = false);
|
||||
int load_image(const char *path, float alpha = 1.0f, bool pixel_art = false);
|
||||
void unload_image(int image_id);
|
||||
Vec2<float> get_image_size(int image_id);
|
||||
|
||||
@@ -77,7 +83,7 @@ public:
|
||||
|
||||
bool write(int text_id, Vec2<float> position, float z, float angle = 0.0f, Vec2<float> scale = {1.0f, 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(int image_id, Vec2<float> position, float z, float angle = 0.0f, Vec2<float> scale = {1.0f, 1.0f});
|
||||
|
||||
bool draw_rect(Rect rect, Color color, float z, float angle = 0.0f);
|
||||
|
||||
@@ -123,10 +129,10 @@ private:
|
||||
SDL_Texture *white_tex;
|
||||
|
||||
// Image pool
|
||||
Pool<SDL_Texture *> image_pool;
|
||||
Pool<Image> image_pool;
|
||||
|
||||
// Font pool
|
||||
Pool<struct Font> font_pool;
|
||||
Pool<Font> font_pool;
|
||||
|
||||
// Text pool
|
||||
Pool<SDL_Texture *> text_pool;
|
||||
@@ -146,7 +152,6 @@ private:
|
||||
struct {
|
||||
int image_id;
|
||||
Vec2<float> position;
|
||||
float alpha;
|
||||
} draw_image;
|
||||
|
||||
struct {
|
||||
@@ -172,4 +177,6 @@ private:
|
||||
void draw_letterbox();
|
||||
};
|
||||
|
||||
inline Window window({720, 480}, "Mysth Engine");
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user