4#include "hellfire/utilities/ServiceLocator.h"
5#include "../platform/windows_linux/GLFWWindow.h"
6#include "hellfire/scene/Scene.h"
21 Shader *shader = shader_registry_.load_and_get_shader(
22 "assets/shaders/standard.vert",
23 "assets/shaders/phong.frag"
29 }
catch (
const std::exception &e) {
30 std::cerr <<
"Warning: Could not load default shaders: " << e.what() << std::endl;
33 std::cerr <<
"Using minimal fallback shader" << std::endl;
37 if (fallback_id != 0) {
38 return shader_registry_.get_shader_from_id(fallback_id);
45 const char *vertex_source = R"(
46 #version 330 core
47 layout (location = 0) in vec3 aPos;
48 uniform mat4 MVP;
49 void main() {
50 gl_Position = MVP * vec4(aPos, 1.0);
51 }
52 )";
54 const char *fragment_source = R"(
55 #version 330 core
56 out vec4 FragColor;
57 void main() {
58 FragColor = vec4(1.0, 0.0, 1.0, 1.0); // Magenta to indicate fallback
59 }
60 )";
63 return shader_manager_.compile_shader_program(vertex_source, fragment_source);
64 }
catch (
const std::exception &e) {
65 std::cerr <<
"Failed to create minimal fallback shader: " << e.what() << std::endl;
72 input_manager_ = std::make_unique<InputManager>();
74 window_ = std::make_unique<GLFWWindow>();
75 if (!window_->create(window_info_.width, window_info_.height, window_info_.title)) {
76 throw std::runtime_error(
"Failed to create window");
79 window_->set_event_handler(
this);
81 window_->make_current();
82 if (glewInit() != GLEW_OK) {
83 throw std::runtime_error(
"Failed to initialize GLEW");
87 ServiceLocator::register_service<InputManager>(input_manager_.get());
88 ServiceLocator::register_service<ShaderManager>(&shader_manager_);
89 ServiceLocator::register_service<IWindow>(window_.get());
106 while (!window_->should_close()) {
108 window_->wait_for_events();
113 window_->poll_events();
117 input_manager_->update();
120 if (
auto sm = ServiceLocator::get_service<SceneManager>()) {
135 if (
auto renderer = ServiceLocator::get_service<Renderer>()) {
136 renderer->begin_frame();
138 if (
auto sm = ServiceLocator::get_service<SceneManager>()) {
139 if (
auto *active_scene = sm->get_active_scene()) {
140 Entity *camera_override =
nullptr;
143 if (!camera_override) {
148 renderer->render(*active_scene, camera_override);
159 if (
auto renderer = ServiceLocator::get_service<Renderer>()) {
160 renderer->end_frame();
166 window_->swap_buffers();
175 input_manager_->on_key_down(key);
180 input_manager_->on_key_up(key);
184 bool consumed = call_plugins_until_consumed([button, pressed](
IApplicationPlugin &plugin) {
205 if (x < 100 || x >
static_cast<
float>(
window_info_.width - 100) || y < 100 || y >
static_cast<
float>(
207 window_->warp_cursor(
static_cast<
double>(window_info_.width) / 2,
208 static_cast<
double>(window_info_.height) / 2);
214 input_manager_->on_mouse_move(x, y);
227 bool consumed = call_plugins_until_consumed([x, y, x_offset, y_offset](
IApplicationPlugin &plugin) {
231 if (consumed)
return;
237 void Application::on_window_resize(
const int width,
const int height) {
238 if (width == 0 || height == 0) {
247 glViewport(0, 0, width, height);
250 if (
auto sm = ServiceLocator::get_service<SceneManager>()) {
251 if (
Scene *active_scene = sm->get_active_scene()) {
252 for (
const EntityID camera_id: sm->get_camera_entities()) {
253 if (
const Entity *camera_entity = active_scene->get_entity(camera_id)) {
254 if (
auto *camera_comp = camera_entity->get_component<CameraComponent>()) {
255 camera_comp->set_aspect_ratio(window_info_.aspect_ratio);
272 exit_condition_ = std::move(condition);
281 if (exit_condition_ && exit_condition_())
return true;
void on_mouse_move(float x, float y) override
void set_exit_condition(std::function< bool()> condition)
void on_render() override
Application(int width=800, int height=600, std::string title="hellfire Application")
void handle_cursor_warping(float x, float y) const
bool handle_first_mouse_movement(float x, float y)
void on_key_up(int key) override
void on_key_down(int key) override
void on_window_minimize(bool minimized) override
Shader * ensure_fallback_shader()
void on_mouse_button(int button, bool pressed) override
uint32_t create_minimal_fallback_shader()
virtual bool on_mouse_move(float x, float y, float x_offset, float y_offset)
virtual bool on_mouse_button(int button, bool pressed)
virtual void on_end_frame()
virtual bool on_key_down(int key)
virtual void on_begin_frame()
virtual Entity * get_render_camera_override()
virtual void on_initialize(Application &app)
virtual void on_window_resize(int width, int height)
virtual void on_mouse_button(int button, bool pressed)
Manages a collection of entities and their hierarchical relationships.