2 #include <ivorium_config.hpp>
17 _lastFrameTime_s( 0 ),
18 _current_character( 0 ),
22 glfmSetDisplayConfig( display,
23 GLFMRenderingAPIOpenGLES3,
24 GLFMColorFormatRGBA8888,
26 GLFMStencilFormatNone,
27 GLFMMultisampleNone );
29 glfmSetUserData( display,
this );
30 glfmSetMultitouchEnabled( display,
true );
33 glfmSetSurfaceCreatedFunc( display, GlfmWindow::s_onSurfaceCreated );
34 glfmSetSurfaceDestroyedFunc( display, GlfmWindow::s_onSurfaceDestroyed );
35 glfmSetSurfaceResizedFunc( display, GlfmWindow::s_onSurfaceResized );
36 glfmSetSurfaceErrorFunc( display, GlfmWindow::s_SurfaceErrorFunc );
37 glfmSetMemoryWarningFunc( display, GlfmWindow::s_MemoryWarningFunc );
38 glfmSetAppFocusFunc( display, GlfmWindow::s_FocusFunc );
39 glfmSetMainLoopFunc( display, GlfmWindow::s_onFrame );
42 glfmSetTouchFunc( display, GlfmWindow::s_TouchFunc );
43 glfmSetKeyFunc( display, GlfmWindow::s_KeyFunc );
44 glfmSetEmscriptenKeyFunc( display, GlfmWindow::s_EmscriptenKeyFunc );
45 glfmSetCharFunc( display, GlfmWindow::s_CharFunc );
47 PopulateEmscriptenKeyMap();
50 this->_render_target.set_geometry( RenderTarget::Geometry(
float2( 0, 0 ), 1.0 ) );
64 this->listener = listener;
69 return RenderTarget::Geometry(
float2( 100, 100 ), 1.0f );
74 return &this->_render_target;
78 void GlfmWindow::render()
82 glClear( GL_COLOR_BUFFER_BIT );
85 this->listener->
draw();
89 #if defined( GLFM_PLATFORM_ANDROID )
90 static double _glfmTimeSeconds(
struct timespec t) {
91 return t.tv_sec + (double)t.tv_nsec / 1e9;
94 static struct timespec _glfmTimeSubstract(struct timespec a, struct timespec b) {
95 struct timespec result;
96 if (b.tv_nsec > a.tv_nsec) {
97 result.tv_sec = a.tv_sec - b.tv_sec - 1;
98 result.tv_nsec = 1000000000 - b.tv_nsec + a.tv_nsec;
100 result.tv_sec = a.tv_sec - b.tv_sec;
101 result.tv_nsec = a.tv_nsec - b.tv_nsec;
106 static struct timespec _glfmTimeNow() {
108 clock_gettime(CLOCK_MONOTONIC_RAW, &t);
113 void GlfmWindow::onFrame(
double frameTime_s )
115 #if defined( GLFM_PLATFORM_ANDROID )
116 auto start = _glfmTimeNow();
120 uint64_t delta_ms = uint64_t( ( frameTime_s - this->_lastFrameTime_s ) * 1000.0 );
121 this->_lastFrameTime_s = frameTime_s;
124 this->listener->
update( delta_ms );
130 #if defined( GLFM_PLATFORM_ANDROID )
132 bool more_extras_needed = this->listener->
extra_update();
133 while( more_extras_needed )
136 auto elapsed_s = _glfmTimeSeconds( _glfmTimeSubstract( _glfmTimeNow(), start ) );
137 if( elapsed_s >= 1.0/60.0 )
145 #warning "Unimplemented proper extra updates for this platform."
150 void GlfmWindow::onSurfaceCreated(
int width,
int height )
152 TextOutput <<
"GlfmWindow: onSurfaceCreated( " << width <<
", " << height <<
" )" << std::endl;
153 TextOutput <<
" GL_VENDOR : " << glGetString( GL_VENDOR ) << std::endl;
154 TextOutput <<
" GL_RENDERER : " << glGetString( GL_RENDERER ) << std::endl;
155 TextOutput <<
" GL_VERSION : " << glGetString( GL_VERSION ) << std::endl;
156 TextOutput <<
" GL_SHADING_LANGUAGE_VERSION : " << glGetString( GL_SHADING_LANGUAGE_VERSION ) << std::endl;
160 this->onSurfaceResized( width, height );
161 this->listener->
gpu(
true,
true );
164 void GlfmWindow::onSurfaceDestroyed()
166 TextOutput <<
"GlfmWindow: onSurfaceDestroyed" << std::endl;
168 this->listener->
gpu(
false,
true );
171 void GlfmWindow::onSurfaceResized(
int width,
int height )
173 glViewport( 0, 0, width, height );
175 double scale = glfmGetDisplayScale( this->display );
178 this->_render_target.
set_geometry( RenderTarget::Geometry(
int2( width, height ), scale ) );
183 void GlfmWindow::SurfaceErrorFunc(
const char * message )
189 void GlfmWindow::MemoryWarningFunc()
194 void GlfmWindow::FocusFunc(
bool focused )
206 auto it = this->_touches.find( device_id );
207 if( it == this->_touches.end() )
220 return this->_current_character;
223 bool GlfmWindow::TouchFunc(
int touch, GLFMTouchPhase phase,
double x,
double y )
225 this->_touches[ touch ] =
int2( x, y );
227 if( phase == GLFMTouchPhaseBegan )
230 this->listener->
input( &i );
232 else if( phase == GLFMTouchPhaseEnded )
235 this->listener->
input( &i );
237 else if( phase == GLFMTouchPhaseCancelled )
240 this->listener->
input( &i );
246 void GlfmWindow::PopulateEmscriptenKeyMap()
360 bool GlfmWindow::EmscriptenKeyFunc(
const char * code, GLFMKeyAction action,
int modifiers )
362 auto it = this->emscriptenKeyMap.find( code );
363 if( it == this->emscriptenKeyMap.end() )
365 auto key = it->second;
372 default:
return false;
375 Input i( type, key, 0,
true );
376 return this->listener->
input( &i );
379 bool GlfmWindow::KeyFunc( GLFMKey keyCode, GLFMKeyAction action,
int modifiers )
384 void GlfmWindow::CharFunc(
const char * value,
int modifiers )
389 this->_current_character = ::utf8::next( value, value + strlen( value ) );
391 this->listener->
input( &i );
392 this->_current_character = 0;
396 void GlfmWindow::s_onSurfaceCreated( GLFMDisplay * display,
const int width,
const int height )
399 window->onSurfaceCreated( width, height );
402 void GlfmWindow::s_onSurfaceDestroyed( GLFMDisplay * display )
405 window->onSurfaceDestroyed();
408 void GlfmWindow::s_onSurfaceResized( GLFMDisplay * display,
int width,
int height )
411 window->onSurfaceResized( width, height );
414 void GlfmWindow::s_onFrame( GLFMDisplay * display,
double frameTime_s )
417 window->onFrame( frameTime_s );
420 bool GlfmWindow::s_TouchFunc( GLFMDisplay * display,
int touch, GLFMTouchPhase phase,
double x,
double y )
423 return window->TouchFunc( touch, phase, x, y );
426 void GlfmWindow::s_SurfaceErrorFunc( GLFMDisplay * display,
const char * message )
429 window->SurfaceErrorFunc( message );
432 void GlfmWindow::s_MemoryWarningFunc( GLFMDisplay * display )
435 window->MemoryWarningFunc();
438 void GlfmWindow::s_FocusFunc( GLFMDisplay * display,
bool focused )
441 window->FocusFunc( focused );
444 bool GlfmWindow::s_KeyFunc( GLFMDisplay * display, GLFMKey keyCode, GLFMKeyAction action,
int modifiers )
447 return window->KeyFunc( keyCode, action, modifiers );
450 void GlfmWindow::s_CharFunc( GLFMDisplay * display,
const char * utf8,
int modifiers )
453 window->CharFunc( utf8, modifiers );
456 bool GlfmWindow::s_EmscriptenKeyFunc( GLFMDisplay * display,
const char * code, GLFMKeyAction action,
int modifiers )
459 return window->EmscriptenKeyFunc( code, action, modifiers );