21 #include "hx3d/window/sdl2/sdl2_application.hpp" 22 #include "hx3d/window/sdl2/sdl2_events.hpp" 24 #include "hx3d/utils/log.hpp" 25 #include "hx3d/utils/assets.hpp" 27 #include "hx3d/graphics/texture.hpp" 28 #include "hx3d/graphics/framebuffer.hpp" 29 #include "hx3d/graphics/shader.hpp" 30 #include "hx3d/graphics/font.hpp" 32 #include "hx3d/core/core.hpp" 33 #include "hx3d/window/game.hpp" 34 #include "hx3d/window/events.hpp" 36 #include "hx3d/audio/audio.hpp" 38 #include "hx3d/math/number_utils.hpp" 40 #include "hx3d/net/net.hpp" 55 SDL2Application::~SDL2Application() {
60 SDL_GL_DeleteContext(_context);
63 SDL_DestroyWindow(_window);
68 #if defined(__ANDROID__) 85 void SDL2Application::create(
int width,
int height, std::string title) {
89 if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) < 0) {
90 Log.
Error(
"SDL Init Error: %s", SDL_GetError());
101 * ***** *+/++++/+* /++++* +++++++!*\n\ 103 +/+ +/+ +/++++/+ */ + * +\n\ 104 +/+ +/+ +/+ +/+ *+++++/ *++++++/\n";
108 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
109 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
110 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
112 #ifdef DESKTOP_OPENGL 113 auto window_flags = SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | (
_fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
114 _window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, window_flags);
119 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
120 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 0);
122 _window = SDL_CreateWindow(
"", 0, 0, 0, 0, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS);
124 SDL_GetWindowSize(_window, &_width, &
_height);
126 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
127 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
128 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
131 _context = SDL_GL_CreateContext(_window);
133 Log.
Error(
"OpenGL Context Error: %s", SDL_GetError());
137 SDL_GL_MakeCurrent(_window, _context);
139 #ifdef DESKTOP_OPENGL 140 GLenum init = glewInit();
141 if (init != GLEW_OK) {
142 Log.
Error(
"GLEW error: %s", glewGetErrorString(init));
149 glViewport(0, 0, _width,
_height);
151 glEnable(GL_DEPTH_TEST);
154 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
158 glGetIntegerv(GL_STENCIL_BITS, &bitplanes);
160 Log.
Info(
"Stencil bitplanes: %d", bitplanes);
165 Framebuffer::fetchDefaultFramebuffer();
166 Texture::generateBlankTexture();
175 void SDL2Application::display() {
177 Uint32 frameTime = 0;
180 float deltaTime = 0.f;
185 frameStart = SDL_GetTicks();
190 frameEnd = SDL_GetTicks();
191 frameTime = frameEnd - frameStart;
192 deltaTime = frameTime / 1000.f;
198 void SDL2Application::update(
float delta) {
205 _game->update(delta);
208 if (!
_game->isRunning())
212 void SDL2Application::render() {
216 SDL_GL_SwapWindow(_window);
218 if (!
_game->isRunning())
SDL2 event manager implementation.
static void setGame(window::Game *game)
Set the game.
float mclamp(float value, float min, float max)
Modulo clamp.
Application configuration.
static void shutdown()
Shutdown the core system.
2D and 3D graphics components.
static AssetManager * Assets()
Get the asset manager.
bool _fullscreen
Is the application in fullscreen ?
void create(std::string name, Args...args)
Create an asset with arguments.
int _width
Application width.
bool _running
Is the application running ?
Ptr< Game > _game
Game pointer.
void Error(const std::string fmt,...)
Write an error message.
virtual void poll()=0
Poll the event queue.
static hx3d::LogImpl Log
Current log implementation.
int _height
Application height.
float _elapsedTime
Elapsed time since the beginning.
virtual void start(const Ptr< Game > &game) override
Start the window.
std::string _title
Application title.
static void initialize(window::Application *app, window::EventManager *events)
Initialize the core system.
float _currentFPS
Current FPS.
static window::EventManager * Events()
Get the event manager.
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
void Info(const std::string fmt,...)
Write an info message.