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
40 static const int STDIN = 0;
41 static bool initialized = false;
45 // Use termios to turn off line buffering
47 tcgetattr(STDIN, &term);
48 term.c_lflag &= ~ICANON;
49 tcsetattr(STDIN, TCSANOW, &term);
55 ioctl(STDIN, FIONREAD, &bytesWaiting);
60 void LogFunction(Dali::Integration::Log::DebugPriority priority, std::string& message)
62 printf("%s", message.c_str());
63 EM_ASM( console.log( message.c_str() ) );
71 typedef ::Pixmap XPixmap;
72 typedef ::Window XWindow;
73 typedef ::Display XDisplay;
74 typedef ::Screen XScreen;
76 const unsigned int SdlApplication::DEFAULT_SURFACE_WIDTH = 600;
77 const unsigned int SdlApplication::DEFAULT_SURFACE_HEIGHT= 480;
80 SdlApplication::SdlApplication( size_t surfaceWidth,
85 mSurfaceWidth( surfaceWidth ),
86 mSurfaceHeight( surfaceHeight ),
92 EGLNativeDisplayType display = (EGLNativeDisplayType)XOpenDisplay(NULL);
93 bool isOwnSurface = true;
94 mEglImplementation.InitializeGles( display, isOwnSurface );
96 SdlCreateWindow(surfaceWidth, surfaceHeight, "Dali");
98 bool isWindowType = true;
99 Dali::ColorDepth depth = Dali::COLOR_DEPTH_32;
100 mEglImplementation.ChooseConfig( isWindowType, depth );
102 EGLNativeWindowType window = NULL;
104 mEglImplementation.CreateSurfaceWindow( window, depth );
106 mEglImplementation.CreateContext();
108 mEglImplementation.MakeContextCurrent();
111 // SDL/EGL setup, now create core
113 mCore = Dali::Integration::Core::New(
115 mPlatformAbstraction,
119 ResourcePolicy::DataRetention::DALI_RETAINS_ALL_DATA);
121 mCore->ContextCreated();
122 mCore->SurfaceResized( mSurfaceWidth, mSurfaceHeight );
123 mCore->SetDpi( horizontalDpi, verticalDpi );
125 Dali::Integration::Log::InstallLogFunction( LogFunction );
127 mCore->SceneCreated();
130 SdlApplication::~SdlApplication()
132 Dali::Integration::Log::UninstallLogFunction();
137 void SdlApplication::SdlCreateWindow(size_t surfaceWidth,
138 size_t surfaceHeight,
139 const std::string &title)
141 if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
143 DALI_LOG_WARNING("SDL_Init Err: %s\n", SDL_GetError() );
144 DALI_ASSERT_ALWAYS(!"Couldn't initialize SDL");
147 // load support for the JPG and PNG image formats
148 int flags = IMG_INIT_JPG | IMG_INIT_PNG;
149 int initted = IMG_Init(flags);
150 if( (initted & flags) != flags)
152 DALI_LOG_WARNING("IMG_Init Err:%s\n", IMG_GetError());
153 DALI_ASSERT_ALWAYS("!IMG_Init: Failed to init required jpg and png support!\n");
157 int bpp = 0; // 0 means current display bpp
159 Uint32 video_flags = VIDEO_FLAGS;
161 SDL_Surface *surface = SDL_SetVideoMode( static_cast<int>(surfaceWidth),
162 static_cast<int>(surfaceHeight),
166 if ( surface == NULL )
168 DALI_LOG_WARNING("Couldn't set GL mode: %s\n", SDL_GetError());
169 DALI_ASSERT_ALWAYS("Couldn't set GL mode");
174 /* Set the window manager title bar */
175 SDL_WM_SetCaption( title.c_str(), "daliweb" );
177 /* Set the gamma for the window */
180 SDL_SetGamma(gamma, gamma, gamma);
185 void SdlApplication::DoUpdate( void )
188 mCore->ProcessEvents();
190 // Update Time values
191 static Internal::Adaptor::FrameTime frameTime;
192 static bool init = false;
195 frameTime.SetMinimumFrameTimeInterval( 16667 );
199 static unsigned int frameNo = 0;
201 frameTime.SetSyncTime(frameNo);
203 float lastFrameDelta( 0.0f );
204 unsigned int lastSyncTime( 0 );
205 unsigned int nextSyncTime( 0 );
206 frameTime.PredictNextSyncTime( lastFrameDelta, lastSyncTime, nextSyncTime );
208 Integration::UpdateStatus status;
210 mCore->Update( lastFrameDelta, lastSyncTime, nextSyncTime, status );
212 Dali::Internal::Emscripten::stats.lastFrameDeltaSeconds = lastFrameDelta;
213 Dali::Internal::Emscripten::stats.lastSyncTimeMilliseconds = lastSyncTime;
214 Dali::Internal::Emscripten::stats.nextSyncTimeMilliseconds = nextSyncTime;
216 Dali::Internal::Emscripten::stats.keepUpdating = status.keepUpdating;
217 Dali::Internal::Emscripten::stats.needsNotification = status.needsNotification;
218 Dali::Internal::Emscripten::stats.secondsFromLastFrame = status.secondsFromLastFrame;
223 void SdlApplication::DoRender()
226 mCore->Render( mRenderStatus );
230 Dali::Internal::Emscripten::stats.frameCount = mFrame;
232 mEglImplementation.SwapBuffers();
236 void SdlApplication::SendTouchEvent(double x, double y, int mouseState)
238 TouchPoint::State state = TouchPoint::Up;
239 if( 0 == mouseState )
241 state = TouchPoint::Down;
243 else if( 1 == mouseState )
245 state = TouchPoint::Up;
247 else if( 2 == mouseState )
249 state = TouchPoint::Motion;
252 Dali::Integration::TouchEvent e;
253 e.AddPoint( TouchPoint( 0,
255 static_cast<float>(x),
256 static_cast<float>(y) ) );
258 mCore->QueueEvent(e);
261 void SdlApplication::SetSurfaceWidth( unsigned int width, unsigned height )
263 mSurfaceWidth = width;
264 mSurfaceHeight = height;
266 mCore->SurfaceResized( mSurfaceWidth, mSurfaceHeight );