Fix main loop, add example and cleanup
This commit is contained in:
+11
-59
@@ -1,67 +1,19 @@
|
||||
#include "window/window.h"
|
||||
#include "app/app.h"
|
||||
|
||||
using namespace app;
|
||||
|
||||
int main() {
|
||||
printf("Starting engine...\n");
|
||||
int loading_image = window.load_image("assets/images/menu_bg.png", 1.0f, true);
|
||||
|
||||
Window w({800, 600}, "Render Test");
|
||||
Animation loading_animation = {
|
||||
.frames = {loading_image},
|
||||
.frame_rate = 1.0f, // Not animated, so frame rate doesn't matter
|
||||
.loop = false
|
||||
};
|
||||
|
||||
int img = w.load_image("./assets/images/arrow.png", true);
|
||||
int font = w.load_font(
|
||||
"./assets/fonts/charybdis.ttf",
|
||||
24,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
true
|
||||
);
|
||||
App app({720, 480}, "Mysth Engine", loading_animation);
|
||||
|
||||
int text = w.create_text(font, "Hello Engine", 12, {255, 255, 255, 255});
|
||||
|
||||
float t = 0.0f;
|
||||
|
||||
bool running = true;
|
||||
|
||||
printf("Entering main loop...\n");
|
||||
|
||||
while (running) {
|
||||
Event e;
|
||||
while (w.poll(e)) {
|
||||
switch (e.type) {
|
||||
case Event::QUIT:
|
||||
printf("Exiting\n");
|
||||
running = false;
|
||||
break;
|
||||
case Event::KEY_DOWN:
|
||||
printf("Key down: %d\n", e.key.key);
|
||||
if (e.key.key == 41) {
|
||||
printf("Exiting\n");
|
||||
running = false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
w.consume(e);
|
||||
}
|
||||
|
||||
w.clear();
|
||||
|
||||
// animated position
|
||||
t += 0.016f;
|
||||
float x = 200 + std::sin(t) * 100;
|
||||
float y = 150 + std::cos(t) * 100;
|
||||
|
||||
// background rect (low z)
|
||||
w.draw_rect({{0, 0}, {800, 600}}, {20, 20, 30, 255}, 0, 20.0f);
|
||||
|
||||
// image (mid z)
|
||||
w.draw(img, {x, y}, 1.0f, t * 50.0f, {2.0f, 2.0f}, 1.0f);
|
||||
|
||||
// text (top z)
|
||||
w.write(text, {0, 0}, 2.0f, 0.0f, {1.0f, 1.0f});
|
||||
|
||||
w.render();
|
||||
}
|
||||
app.loop();
|
||||
|
||||
return 0;
|
||||
}
|
||||
+45
-20
@@ -29,10 +29,20 @@ Window::Window(Vec2<float> size, const char *title) {
|
||||
SDL_UpdateTexture(white_tex, nullptr, &pixel, sizeof(pixel));
|
||||
};
|
||||
|
||||
void Window::update(Vec2<float> new_size, const char *new_title) {
|
||||
if (new_size.x > 0 && new_size.y > 0) {
|
||||
virtual_size = new_size;
|
||||
compute_transform();
|
||||
}
|
||||
|
||||
if (new_title)
|
||||
SDL_SetWindowTitle(window, new_title);
|
||||
}
|
||||
|
||||
Window::~Window() {
|
||||
for (auto tex : image_pool.all())
|
||||
if (tex)
|
||||
SDL_DestroyTexture(tex);
|
||||
for (auto &tex : image_pool.all())
|
||||
if (tex.texture)
|
||||
SDL_DestroyTexture(tex.texture);
|
||||
|
||||
for (auto tex : text_pool.all())
|
||||
if (tex)
|
||||
@@ -48,7 +58,7 @@ Window::~Window() {
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
int Window::load_image(const char *path, bool pixel_art) {
|
||||
int Window::load_image(const char *path, float alpha, bool pixel_art) {
|
||||
SDL_Texture *texture = IMG_LoadTexture(renderer, path);
|
||||
|
||||
if (pixel_art) {
|
||||
@@ -60,7 +70,20 @@ int Window::load_image(const char *path, bool pixel_art) {
|
||||
return UINT32_MAX;
|
||||
}
|
||||
|
||||
return image_pool.acquire(texture);
|
||||
SDL_SetTextureAlphaMod(texture, (uint8_t)(alpha * 255));
|
||||
|
||||
float w, h;
|
||||
SDL_GetTextureSize(texture, &w, &h);
|
||||
|
||||
return image_pool.acquire({texture, {w, h}});
|
||||
}
|
||||
|
||||
Vec2<float> Window::get_image_size(int image_id) {
|
||||
if (!image_pool.is_valid(image_id)) {
|
||||
fprintf(stderr, "Invalid image ID: %d\n", image_id);
|
||||
return {0, 0};
|
||||
}
|
||||
return {image_pool[image_id].size.x, image_pool[image_id].size.y};
|
||||
}
|
||||
|
||||
void Window::unload_image(int image_id) {
|
||||
@@ -69,7 +92,7 @@ void Window::unload_image(int image_id) {
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_DestroyTexture(image_pool[image_id]);
|
||||
SDL_DestroyTexture(image_pool[image_id].texture);
|
||||
image_pool.release(image_id);
|
||||
}
|
||||
|
||||
@@ -132,6 +155,16 @@ int Window::create_text(int font_id, const char *text, uint32_t length, Color co
|
||||
return text_pool.acquire(texture);
|
||||
}
|
||||
|
||||
Vec2<float> Window::get_text_size(int text_id) {
|
||||
if (!text_pool.is_valid(text_id)) {
|
||||
fprintf(stderr, "Invalid text ID: %d\n", text_id);
|
||||
return {0, 0};
|
||||
}
|
||||
float w, h;
|
||||
SDL_GetTextureSize(text_pool[text_id], &w, &h);
|
||||
return {w, h};
|
||||
}
|
||||
|
||||
void Window::unload_text(int text_id) {
|
||||
if (!text_pool.is_valid(text_id)) {
|
||||
fprintf(stderr, "Invalid text ID: %d\n", text_id);
|
||||
@@ -162,7 +195,7 @@ bool Window::write(int text_id, Vec2<float> position, float z, float angle, Vec2
|
||||
return true;
|
||||
};
|
||||
|
||||
bool Window::draw(int image_id, Vec2<float> position, float z, float angle, Vec2<float> scale, float alpha) {
|
||||
bool Window::draw(int image_id, Vec2<float> position, float z, float angle, Vec2<float> scale) {
|
||||
if (!image_pool.is_valid(image_id)) {
|
||||
fprintf(stderr, "Invalid image ID: %d\n", image_id);
|
||||
return false;
|
||||
@@ -176,7 +209,6 @@ bool Window::draw(int image_id, Vec2<float> position, float z, float angle, Vec2
|
||||
.draw_image = {
|
||||
.image_id = image_id,
|
||||
.position = position,
|
||||
.alpha = alpha,
|
||||
}}
|
||||
);
|
||||
|
||||
@@ -236,9 +268,7 @@ void Window::render() {
|
||||
std::stable_sort(
|
||||
render_instructions.begin(),
|
||||
render_instructions.end(),
|
||||
[](const RenderInstruction &a, const RenderInstruction &b) {
|
||||
return a.z < b.z;
|
||||
}
|
||||
[](const RenderInstruction &a, const RenderInstruction &b) { return a.z < b.z; }
|
||||
);
|
||||
|
||||
for (const RenderInstruction &instruction : render_instructions) {
|
||||
@@ -277,10 +307,7 @@ void Window::render() {
|
||||
} break;
|
||||
|
||||
case RenderInstruction::DRAW_IMAGE: {
|
||||
SDL_Texture *texture = image_pool[instruction.draw_image.image_id];
|
||||
|
||||
float w, h;
|
||||
SDL_GetTextureSize(texture, &w, &h);
|
||||
Image image = image_pool[instruction.draw_image.image_id];
|
||||
|
||||
SDL_FlipMode flip = SDL_FLIP_NONE;
|
||||
|
||||
@@ -295,14 +322,12 @@ void Window::render() {
|
||||
SDL_FRect dst_rect;
|
||||
dst_rect.x = instruction.draw_image.position.x * scale + offset.x;
|
||||
dst_rect.y = instruction.draw_image.position.y * scale + offset.y;
|
||||
dst_rect.w = scale_x * w;
|
||||
dst_rect.h = scale_y * h;
|
||||
|
||||
SDL_SetTextureAlphaMod(texture, static_cast<Uint8>(instruction.draw_image.alpha * 255));
|
||||
dst_rect.w = scale_x * image.size.x;
|
||||
dst_rect.h = scale_y * image.size.y;
|
||||
|
||||
SDL_RenderTextureRotated(
|
||||
renderer,
|
||||
texture,
|
||||
image.texture,
|
||||
nullptr,
|
||||
&dst_rect,
|
||||
instruction.angle,
|
||||
|
||||
Reference in New Issue
Block a user