Khaotic Engine Reborn
Loading...
Searching...
No Matches
application_class.h
1#ifndef _APPLICATIONCLASS_H_
2#define _APPLICATIONCLASS_H_
3
4
6// MY CLASS INCLUDES //
8#include "d_3d_class.h"
9#include "camera_class.h"
10#include "light_class.h"
11
12#include "bitmap_class.h"
13#include "sprite_class.h"
14#include "timer_class.h"
15#include "font_shader_class.h"
16#include "font_class.h"
17#include "text_class.h"
18#include "fps_class.h"
19#include "input_class.h"
20#include "shader_manager_class.h"
21#include "modellistclass.h"
22#include "position_class.h"
23#include "frustumclass.h"
24#include "render_texture_class.h"
25#include "display_plane_class.h"
26#include "translate_shader_class.h"
27#include "reflection_shader_class.h"
28#include "physics.h"
29#include "frustum.h"
30#include "skybox.h"
31#include "shadow_map.h"
32#include "stats.h"
33
34#include "ecs/components/identity_component.h"
35#include "ecs/components/render_component.h"
36#include "ecs/components/transform_component.h"
37#include "ecs/components/physics_component.h"
38#include "ecs/components/shader_component.h"
39#include "ecs/systems/render_system.h"
40#include "ecs/components/model_path_component.h"
41
42#include <fstream>
43#include <WICTextureLoader.h>
44#include <comdef.h> // Pour _com_error
45#include <chrono>
46#include <thread>
47#include <map>
48#include <algorithm>
49#include <DirectXMath.h>
50#include <mutex>
51#include <memory>
52#include <vector>
53#include <filesystem>
54
55#include "ecs/entity_manager.h"
56
57
59// GLOBALS //
61constexpr bool full_screen = false;
62constexpr float screen_depth = 1000.0f;
63constexpr float screen_near = 0.3f;
64
65static std::map<std::string, std::shared_ptr<model_class>> g_model_cache;
66
67struct input
68{
69 bool key_left = false;
70 bool key_right = false;
71 bool key_up = false;
72 bool key_down = false;
73};
74
76// Class name: application_class
79{
80public:
87 virtual d_3d_class* get_direct_3d();
88 void set_direct_3d(d_3d_class* direct_3d) { direct_3d_ = direct_3d; };
89
94 render_texture_class* get_scene_texture() const { return scene_texture_; };
99 render_texture_class* get_render_texture() const { return render_texture_; };
104 render_texture_class* get_refraction_texture() const { return refraction_texture_; };
109 render_texture_class* get_reflection_texture() const { return reflection_texture_; };
110
116 bool create_big_cube(int side_count);
126 virtual bool initialize(int screen_width, int screen_height, HWND hwdn, bool is_vulkan);
130 void shutdown();
136 virtual bool frame(input_class*);
146 int get_physics_tick_rate() const { return physics_tick_rate_; };
151 void set_physics_tick_rate(int physics_tick_rate) { physics_tick_rate_ = physics_tick_rate; };
152
157 int get_screen_width() const;
162 void set_screen_width(int screen_width);
167 int get_screen_height() const;
172 void set_screen_height(int screen_height);
173
180 float get_speed() const { return speed_; };
187 void set_speed(const float speed) { this->speed_ = speed; };
188
192 void add_cube();
197 void delete_entity_by_id(int entity_id);
202 void add_kobject(std::wstring& filepath);
207 void set_path(WCHAR* path) { path_ = path; };
211 void set_w_folder(const std::filesystem::path& w_folder) { w_folder_ = w_folder; };
216 std::filesystem::path get_w_folder() const { return w_folder_; };
222
227 int get_object_id() const { return object_id_; };
232 void set_object_id(int object_id) { object_id_ = object_id; };
233
237 void generate_terrain();
241 void delete_terrain();
242
248 XMVECTOR get_light_position(int index);
254 XMVECTOR get_light_color(int index);
260 void set_light_position(int index, XMVECTOR position);
266 void set_light_color(int index, XMVECTOR color);
271 std::vector<light_class*> get_lights() const { return lights_; };
276 light_class* get_sun_light() const { return sun_light_; };
277
283 bool get_should_quit() const { return should_quit_; };
289 void set_should_quit(const bool should_quit) { should_quit_ = should_quit; };
290
297 void set_cel_shading(const bool enable) { enable_cel_shading_ = enable; };
298
303 void set_vsync(bool vsync) {
304 vsync_enabled_ = vsync;
305 if (direct_3d_) {
306 direct_3d_->set_vsync(vsync);
307 Logger::Get().Log("Setting Vsync to " + std::to_string(vsync), __FILE__, __LINE__);
308 }
309 };
310
315 bool get_vsync() const { return vsync_enabled_; };
316
321 HWND get_hwnd() const { return hwnd_; };
326 void set_hwnd(HWND hwnd) { hwnd_ = hwnd; };
327
332 bool is_windowed() const { return windowed_; };
337 void set_windowed(bool windowed) { windowed_ = windowed; };
338
344 void set_window_size(const ImVec2 size) { window_size_ = size; };
350 ImVec2 get_window_size() const { return window_size_; };
355 float get_aspect_ratio() const { return static_cast<float>(screen_width_) / static_cast<float>(screen_height_); };
356
361 physics* get_physics() const { return physics_; };
362
363 // ----------------------------------- //
364 // ------------- Culling ------------- //
365 // ----------------------------------- //
366
371 frustum get_frustum() const { return frustum_culling_; };
376 void set_frustum(const frustum& frustum) { frustum_culling_ = frustum; };
377
382 void construct_frustum();
383
388 int get_render_count() const { return render_count_; };
393 void set_render_count(const int render_count) { render_count_ = render_count; };
399 float get_frustum_tolerance() const { return frustum_culling_tolerance_; };
405 void set_frustum_tolerance(const float frustum_tolerance) { frustum_culling_tolerance_ = frustum_tolerance; };
406
411 bool get_can_fixed_update() const { return can_fixed_update_; };
416 void set_can_fixed_update(bool can_fixed_update) { can_fixed_update_ = can_fixed_update; };
417
422 ID3D11ShaderResourceView* get_back_buffer_srv() const {return back_buffer_srv_;};
423
428 stats* get_stats() const { return stats_; };
433 fps_class* get_fps() const { return fps_; };
434
439 ecs::EntityManager* get_entity_manager() const { return entity_manager_.get(); };
450 std::map<std::string, std::shared_ptr<model_class>>& get_model_cache() { return g_model_cache; }
451
452private:
462 bool render(float, float, float, float, float);
468 bool render_physics(float delta_time);
476 bool update_mouse_strings(int, int, bool);
481 bool update_fps();
487 bool update_render_count_string(int);
494 bool render_scene_to_texture(float);
500 bool render_refraction_to_texture();
506 bool render_reflection_to_texture();
519 bool render_pass(XMFLOAT4* diffuse, XMFLOAT4* position, XMFLOAT4* ambient, XMMATRIX view, XMMATRIX projection);
520
526 void update_skybox_position();
530 void culling_thread_function();
531
532public :
533 std::vector<ID3D11ShaderResourceView*> textures;
534
535private :
536
537 // Thread de culling
538 std::thread culling_thread_;
539 std::atomic<bool> culling_active_;
540 std::mutex objects_mutex_;
541
542 std::mutex terrain_mutex_;
543 std::vector<std::tuple<float, float, float, std::string, int>> terrain_generation_data_;
544 bool terrain_generation_ready_;
545 int next_terrain_object_id_;
546
547 // ------------------------------------- //
548 // ------------- DIRECT3D -------------- //
549 // ------------------------------------- //
550
551 d_3d_class* direct_3d_;
552 IDXGISwapChain* swap_chain_;
553 model_class* model_,* ground_model_, * wall_model_, * bath_model_, * water_model_;
554 ModelListClass* model_list_;
555 bool vsync_enabled_ = true;
556
557 HWND hwnd_;
558 bool windowed_;
559
560 // ------------------------------------- //
561 // ------------- RENDERING ------------- //
562 // ------------------------------------- //
563
564 XMMATRIX base_view_matrix_;
565 render_texture_class* render_texture_, * refraction_texture_, * reflection_texture_;
566 render_texture_class* scene_texture_;
567 display_plane_class* display_plane_;
568 int screen_width_, screen_height_;
569 camera_class* camera_;
570 camera_class* sun_camera_;
571 camera_class* active_camera_;
572 position_class* position_;
573
574 shadow_map* shadow_map_;
575 ID3D11ShaderResourceView* shadow_map_srv_;
576
577 // ------------------------------------ //
578 // ------------- OBJECTS -------------- //
579 // ------------------------------------ //
580
581 std::unique_ptr<ecs::EntityManager> entity_manager_;
582
583 object* selected_object_;
584
585 float speed_ = 0.1f; // speed for the demo spinning object
586 std::vector<object*> imported_object_;
587 int object_id_ = 0;
588 std::vector<object*> skybox_;
589
590 // ----------------------------------- //
591 // ------------- LIGHTS -------------- //
592 // ----------------------------------- //
593
594 light_class* m_light_;
595 std::vector<light_class*> lights_;
596 int num_lights_;
597 light_class* sun_light_;
598 XMFLOAT3 true_light_position_;
599 model_class* light_model_;
600
601 // ----------------------------------- //
602 // ------------- SHADERS ------------- //
603 // ----------------------------------- //
604
605 shader_manager_class* shader_manager_;
606 font_shader_class* font_shader_;
607 bitmap_class* bitmap_;
608 sprite_class* sprite_;
609 bool enable_cel_shading_;
610
611 // ----------------------------------- //
612 // ------------ VARIABLES ------------ //
613 // ----------------------------------- //
614
615 float water_height_, water_translation_;
616 wchar_t* path_;
617 std::filesystem::path w_folder_;
618
619 // ------------------------------------------------- //
620 // ------------- FPS AND INFO ON SCREEN ------------ //
621 // ------------------------------------------------- //
622
623 timer_class* timer_;
624 text_class* mouse_strings_;
625 text_class* render_count_string_;
626 font_class* font_;
627 fps_class* fps_;
628 text_class* fps_string_;
629 int previous_fps_;
630
631 // ------------------------------------------------- //
632 // ------------------- OTHER ----------------------- //
633 // ------------------------------------------------- //
634
635 bool should_quit_;
636 physics* physics_;
637 float gravity_;
638 XMVECTOR previous_position_;
639 ImVec2 window_size_;
640 int physics_tick_rate_ = 50;
641 bool can_fixed_update_ = false;
642 std::thread physics_thread_;
643
644 ID3D11Texture2D* back_buffer_texture_;
645 ID3D11ShaderResourceView* back_buffer_srv_;
646
647 stats* stats_;
648
649 // ------------------------------------------------- //
650 // ------------------- Culling --------------------- //
651 // ------------------------------------------------- //
652
653 frustum frustum_culling_;
654 int render_count_;
655 float frustum_culling_tolerance_ = 5.f;
656
657 // ------------------------------------------------- //
658 // -------------------- Input ---------------------- //
659 // ------------------------------------------------- //
660
661 input inputs_;
662 bool tab_was_pressed_;
663};
664
665#endif
static Logger & Get()
Definition Logger.h:20
void Log(const std::string &message, const std::string &fileName, int lineNumber, LogLevel level=LogLevel::Info)
Definition Logger.h:158
virtual bool frame(input_class *)
Run the main loop of the application. This function will handle the main loop, including rendering an...
ID3D11ShaderResourceView * get_back_buffer_srv() const
int get_physics_tick_rate() const
void set_light_position(int index, XMVECTOR position)
float get_aspect_ratio() const
void set_cel_shading(const bool enable)
void set_render_count(const int render_count)
render_texture_class * get_reflection_texture() const
frustum get_frustum() const
void set_frustum_tolerance(const float frustum_tolerance)
std::filesystem::path get_w_folder() const
virtual bool initialize(int screen_width, int screen_height, HWND hwdn, bool is_vulkan)
ImVec2 get_window_size() const
render_texture_class * get_render_texture() const
void set_hwnd(HWND hwnd)
void set_path(WCHAR *path)
XMVECTOR get_light_position(int index)
void delete_entity_by_id(int entity_id)
void set_w_folder(const std::filesystem::path &w_folder)
void set_vsync(bool vsync)
render_texture_class * get_scene_texture() const
std::map< std::string, std::shared_ptr< model_class > > & get_model_cache()
void set_light_color(int index, XMVECTOR color)
bool create_big_cube(int side_count)
Create a big cube with a specified number of little cube per sides.
void set_speed(const float speed)
void set_windowed(bool windowed)
XMVECTOR get_light_color(int index)
fps_class * get_fps() const
void set_physics_tick_rate(int physics_tick_rate)
render_texture_class * get_refraction_texture() const
void set_should_quit(const bool should_quit)
void add_kobject(std::wstring &filepath)
std::vector< light_class * > get_lights() const
void set_can_fixed_update(bool can_fixed_update)
float get_frustum_tolerance() const
void physics_thread_function()
Thread function for handling physics updates. this function will run in a separate thread to handle p...
void set_window_size(const ImVec2 size)
physics * get_physics() const
application_class()
Constructor for the application class. Initializes member variables and sets up the application.
ecs::EntityManager * get_entity_manager() const
void set_frustum(const frustum &frustum)
bool get_can_fixed_update() const
void set_screen_width(int screen_width)
int get_render_count() const
float get_speed() const
light_class * get_sun_light() const
stats * get_stats() const
void set_object_id(int object_id)
void set_screen_height(int screen_height)
bool get_should_quit() const
void set_vsync(bool vsync)
Sets the vertical sync state.
Definition stats.h:13