mirror of
https://github.com/Relintai/sfw.git
synced 2025-02-19 23:14:19 +01:00
Simplify main().
This commit is contained in:
parent
85bad3cfda
commit
9ec2133dae
@ -6,15 +6,17 @@
|
||||
#include "game_scene.h"
|
||||
|
||||
class GameApplication : public Application {
|
||||
public:
|
||||
GameApplication() : Application() {
|
||||
scene = new GameScene();
|
||||
}
|
||||
SFW_OBJECT(GameApplication, Application);
|
||||
|
||||
~GameApplication() {
|
||||
delete scene;
|
||||
}
|
||||
public:
|
||||
GameApplication() :
|
||||
Application() {
|
||||
scene = new GameScene();
|
||||
}
|
||||
|
||||
~GameApplication() {
|
||||
delete scene;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif // GAME_APPLICATION_H
|
||||
|
39
main.cpp
39
main.cpp
@ -2,59 +2,28 @@
|
||||
#include <emscripten.h>
|
||||
#endif // __EMSCRIPTEN__
|
||||
|
||||
#include "render_core/application.h"
|
||||
#include "render_core/window.h"
|
||||
|
||||
#include "game_application.h"
|
||||
#include "render_core/window.h"
|
||||
|
||||
#include "core/pool_vector.h"
|
||||
#include "core/string_name.h"
|
||||
#include "object/core_string_names.h"
|
||||
|
||||
Application *application = NULL;
|
||||
|
||||
void handle_frame() {
|
||||
application->main_loop();
|
||||
application->core_loop();
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
//TODO centralize these
|
||||
StringName::setup();
|
||||
MemoryPool::setup();
|
||||
CoreStringNames::create();
|
||||
|
||||
AppWindow *w = memnew(AppWindow());
|
||||
w->create(100, 0);
|
||||
|
||||
application = new GameApplication();
|
||||
application = memnew(GameApplication());
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
emscripten_set_main_loop(handle_frame, 0, 1);
|
||||
#else
|
||||
|
||||
//while (application->running) {
|
||||
// application->main_loop();
|
||||
//}
|
||||
|
||||
while (application->running) {
|
||||
if (w->frame_begin()) {
|
||||
//w->resize();
|
||||
//w->render_callback(loopArg);
|
||||
w->frame_end();
|
||||
w->frame_swap();
|
||||
} else {
|
||||
w->shutdown();
|
||||
}
|
||||
application->core_loop();
|
||||
}
|
||||
|
||||
delete application;
|
||||
memdelete(application);
|
||||
|
||||
#endif // __EMSCRIPTEN__
|
||||
|
||||
StringName::cleanup();
|
||||
MemoryPool::cleanup();
|
||||
CoreStringNames::free();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -6,6 +6,10 @@
|
||||
#include "render_core/input/input.h"
|
||||
#include "render_core/window.h"
|
||||
|
||||
#include "core/pool_vector.h"
|
||||
#include "core/string_name.h"
|
||||
#include "object/core_string_names.h"
|
||||
|
||||
void Application::input_event(const Ref<InputEvent> &event) {
|
||||
scene->input_event(event);
|
||||
}
|
||||
@ -20,6 +24,19 @@ void Application::render() {
|
||||
//SDL_GL_SwapWindow(window);
|
||||
}
|
||||
|
||||
void Application::core_loop() {
|
||||
AppWindow *w = AppWindow::get_singleton();
|
||||
|
||||
if (w->frame_begin()) { // calls Application::main_loop()
|
||||
//w->resize();
|
||||
//w->render_callback(loopArg);
|
||||
w->frame_end();
|
||||
w->frame_swap();
|
||||
} else {
|
||||
w->shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
void Application::main_loop() {
|
||||
uint64_t start = STime::time_us();
|
||||
|
||||
@ -46,12 +63,16 @@ void Application::main_loop() {
|
||||
if (remaining > 0) {
|
||||
frame_delta = tfps;
|
||||
|
||||
STime::sleep_us((double)SEC_TO_USEC(remaining));
|
||||
STime::sleep_us((double)SEC_TO_USEC(remaining));
|
||||
} else {
|
||||
frame_delta = elapsed_seconds;
|
||||
}
|
||||
}
|
||||
|
||||
void Application::_init_window() {
|
||||
AppWindow::get_singleton()->create(100, 0);
|
||||
}
|
||||
|
||||
Application::Application() {
|
||||
_instance = this;
|
||||
|
||||
@ -62,9 +83,17 @@ Application::Application() {
|
||||
|
||||
frame_delta = 0;
|
||||
|
||||
if (!Input::get_singleton()) {
|
||||
memnew(Input());
|
||||
}
|
||||
StringName::setup();
|
||||
MemoryPool::setup();
|
||||
CoreStringNames::create();
|
||||
|
||||
memnew(AppWindow());
|
||||
|
||||
if (!Input::get_singleton()) {
|
||||
memnew(Input());
|
||||
}
|
||||
|
||||
_init_window();
|
||||
|
||||
/*
|
||||
SDL_SetMainReady();
|
||||
@ -108,6 +137,12 @@ Application::Application() {
|
||||
*/
|
||||
}
|
||||
Application::~Application() {
|
||||
_instance = NULL;
|
||||
|
||||
StringName::cleanup();
|
||||
MemoryPool::cleanup();
|
||||
CoreStringNames::free();
|
||||
|
||||
//SDL_DestroyWindow(window);
|
||||
|
||||
// window = NULL;
|
||||
|
@ -12,7 +12,7 @@
|
||||
class AppWindow;
|
||||
class InputEvent;
|
||||
|
||||
class Application : Object {
|
||||
class Application : public Object {
|
||||
SFW_OBJECT(Application, Object);
|
||||
|
||||
public:
|
||||
@ -27,6 +27,7 @@ public:
|
||||
return _idle_frames;
|
||||
}
|
||||
|
||||
void core_loop();
|
||||
void main_loop();
|
||||
|
||||
Application();
|
||||
@ -41,6 +42,8 @@ public:
|
||||
real_t frame_delta;
|
||||
|
||||
protected:
|
||||
virtual void _init_window();
|
||||
|
||||
static Application *_instance;
|
||||
|
||||
uint64_t _idle_frames;
|
||||
|
Loading…
Reference in New Issue
Block a user