2 #include "sdl-application.h"
10 #include <sys/select.h>
11 #include <sys/ioctl.h>
16 #include <dali/public-api/dali-core.h>
17 #include <dali/integration-api/events/touch-event-integ.h>
20 #include "integration-api/egl-interface.h"
21 #include "base/separate-update-render/frame-time.h"
22 #include <gl/egl-implementation.h>
24 #include "platform-abstractions/emscripten/emscripten-callbacks.h"
26 // emscripten are using SDL version 1.3 that isnt available for download
27 // but version 2.0 is close to version 1.3 but version 1.2 support removed.
28 // This is one way it isnt.
29 #if SDL_MAJOR_VERSION == 2
30 # define VIDEO_FLAGS SDL_WINDOW_OPENGL
32 # define VIDEO_FLAGS SDL_OPENGL
38 void LogFunction(Dali::Integration::Log::DebugPriority priority, std::string& message)
40 printf("%s", message.c_str());
41 EM_ASM( console.log( message.c_str() ) );
49 typedef ::Pixmap XPixmap;
50 typedef ::Window XWindow;
51 typedef ::Display XDisplay;
52 typedef ::Screen XScreen;
54 const unsigned int SdlApplication::DEFAULT_SURFACE_WIDTH = 600;
55 const unsigned int SdlApplication::DEFAULT_SURFACE_HEIGHT= 480;
58 SdlApplication::SdlApplication( size_t surfaceWidth,
63 mSurfaceWidth( surfaceWidth ),
64 mSurfaceHeight( surfaceHeight ),
70 EGLNativeDisplayType display = (EGLNativeDisplayType)XOpenDisplay(NULL);
71 bool isOwnSurface = true;
72 mEglImplementation.InitializeGles( display, isOwnSurface );
74 SdlCreateWindow(surfaceWidth, surfaceHeight, "Dali");
76 bool isWindowType = true;
77 Dali::ColorDepth depth = Dali::COLOR_DEPTH_32;
78 mEglImplementation.ChooseConfig( isWindowType, depth );
80 EGLNativeWindowType window = NULL;
82 mEglImplementation.CreateSurfaceWindow( window, depth );
84 mEglImplementation.CreateContext();
86 mEglImplementation.MakeContextCurrent();
89 // SDL/EGL setup, now create core
91 mCore = Dali::Integration::Core::New(
97 ResourcePolicy::DataRetention::DALI_RETAINS_ALL_DATA);
99 mCore->ContextCreated();
100 mCore->SurfaceResized( mSurfaceWidth, mSurfaceHeight );
101 mCore->SetDpi( horizontalDpi, verticalDpi );
103 Dali::Integration::Log::InstallLogFunction( LogFunction );
105 mCore->SceneCreated();
108 SdlApplication::~SdlApplication()
110 Dali::Integration::Log::UninstallLogFunction();
115 void SdlApplication::SdlCreateWindow(size_t surfaceWidth,
116 size_t surfaceHeight,
117 const std::string &title)
119 if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
121 DALI_LOG_WARNING("SDL_Init Err: %s\n", SDL_GetError() );
122 DALI_ASSERT_ALWAYS(!"Couldn't initialize SDL");
125 // load support for the JPG and PNG image formats
126 int flags = IMG_INIT_JPG | IMG_INIT_PNG;
127 int initted = IMG_Init(flags);
128 if( (initted & flags) != flags)
130 DALI_LOG_WARNING("IMG_Init Err:%s\n", IMG_GetError());
131 DALI_ASSERT_ALWAYS("!IMG_Init: Failed to init required jpg and png support!\n");
135 int bpp = 0; // 0 means current display bpp
137 Uint32 video_flags = VIDEO_FLAGS;
139 SDL_Surface *surface = SDL_SetVideoMode( static_cast<int>(surfaceWidth),
140 static_cast<int>(surfaceHeight),
144 if ( surface == NULL )
146 DALI_LOG_WARNING("Couldn't set GL mode: %s\n", SDL_GetError());
147 DALI_ASSERT_ALWAYS("Couldn't set GL mode");
152 /* Set the window manager title bar */
153 SDL_WM_SetCaption( title.c_str(), "daliweb" );
155 /* Set the gamma for the window */
158 SDL_SetGamma(gamma, gamma, gamma);
163 void SdlApplication::DoUpdate( void )
166 mCore->ProcessEvents();
168 // Update Time values
169 static Internal::Adaptor::FrameTime frameTime;
170 static bool init = false;
173 frameTime.SetMinimumFrameTimeInterval( 16667 );
177 static unsigned int frameNo = 0;
179 frameTime.SetSyncTime(frameNo);
181 float lastFrameDelta( 0.0f );
182 unsigned int lastSyncTime( 0 );
183 unsigned int nextSyncTime( 0 );
184 frameTime.PredictNextSyncTime( lastFrameDelta, lastSyncTime, nextSyncTime );
186 Integration::UpdateStatus status;
188 mCore->Update( lastFrameDelta, lastSyncTime, nextSyncTime, status );
190 Dali::Internal::Emscripten::stats.lastFrameDeltaSeconds = lastFrameDelta;
191 Dali::Internal::Emscripten::stats.lastSyncTimeMilliseconds = lastSyncTime;
192 Dali::Internal::Emscripten::stats.nextSyncTimeMilliseconds = nextSyncTime;
194 Dali::Internal::Emscripten::stats.keepUpdating = status.keepUpdating;
195 Dali::Internal::Emscripten::stats.needsNotification = status.needsNotification;
196 Dali::Internal::Emscripten::stats.secondsFromLastFrame = status.secondsFromLastFrame;
201 void SdlApplication::DoRender()
204 mCore->Render( mRenderStatus );
208 Dali::Internal::Emscripten::stats.frameCount = mFrame;
210 mEglImplementation.SwapBuffers();
214 void SdlApplication::SendTouchEvent(double x, double y, int mouseState)
216 PointState::Type state = PointState::UP;
217 if( 0 == mouseState )
219 state = PointState::DOWN;
221 else if( 1 == mouseState )
223 state = PointState::UP;
225 else if( 2 == mouseState )
227 state = PointState::MOTION;
230 Dali::Integration::TouchEvent e;
231 Dali::Integration::Point point;
232 point.SetState( state );
233 point.SetScreenPosition( Vector2( static_cast<float>(x), static_cast<float>(y) ) );
236 mCore->QueueEvent(e);
239 void SdlApplication::SetSurfaceWidth( unsigned int width, unsigned height )
241 mSurfaceWidth = width;
242 mSurfaceHeight = height;
244 mCore->SurfaceResized( mSurfaceWidth, mSurfaceHeight );