43 #include <SDL_syswm.h> 47 using namespace vlSDL;
53 bool mUpdateFlag =
true;
55 std::map<int, vl::EKey> key_translation_map;
57 int key_translation_vec[] =
179 initSDLWindow(title, info, width, height);
184 if (mScreen || mSDLWindow)
191 framebuffer()->setWidth(width);
192 framebuffer()->setHeight(height);
196 for(
int i=0; key_translation_vec[i]; i+=2)
197 key_translation_map[ key_translation_vec[i] ] = (
vl::EKey)key_translation_vec[i+1];
201 char win_pos[32] = {0};
202 sprintf ( win_pos,
"SDL_VIDEO_WINDOW_POS=%d,%d", x, y );
208 if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
210 printf(
"Unable to init SDL: %s\n", SDL_GetError());
214 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, info.
rgbaBits().
r());
215 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, info.
rgbaBits().
g());
216 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, info.
rgbaBits().
b());
217 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, info.
rgbaBits().
a());
219 SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, info.
accumRGBABits().
r());
220 SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, info.
accumRGBABits().
g());
221 SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, info.
accumRGBABits().
b());
222 SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, info.
accumRGBABits().
a());
227 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, info.
doubleBuffer()?1:0);
228 SDL_GL_SetAttribute(SDL_GL_STEREO, info.
stereo());
229 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, info.
multisample()?1:0);
232 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, info.
vSync());
234 int bpp = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
235 Uint32 flags = SDL_OPENGL | (info.
fullscreen() ? SDL_FULLSCREEN : 0);
236 width = width !=0 ? width : SDL_GetVideoInfo()->current_w;
237 height = height !=0 ? height : SDL_GetVideoInfo()->current_h;
238 mScreen = SDL_SetVideoMode( width, height, bpp, flags );
241 vl::Log::print(
vl::Say(
"\n error: SDL_SetVideoMode(%n, %n, %n, %hn) failed: %s\n") << width << height << bpp << flags << SDL_GetError() );
248 glGetIntegerv(GL_VIEWPORT, viewport);
251 if (viewport[2] != mScreen->w || viewport[3] != mScreen->h)
253 vl::Log::print(
vl::Say(
"\n warning: OpenGL reported %nx%n as video size but SDL says %nx%n\n") << viewport[2] << viewport[3] << mScreen->w << mScreen->h );
261 dispatchResizeEvent(width, height);
267 SDL_EnableUNICODE(1);
271 SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
272 static SDL_SysWMinfo pInfo;
273 SDL_VERSION(&pInfo.version);
274 SDL_GetWMInfo(&pInfo);
275 HWND hWnd = pInfo.window;
276 DWORD ExStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
277 ExStyle |= WS_EX_ACCEPTFILES;
278 SetWindowLong(hWnd, GWL_EXSTYLE, ExStyle);
287 SDL_ShowCursor(
true);
288 SDL_WM_GrabInput(SDL_GRAB_OFF);
289 setWindowTitle(title);
295 while ( SDL_PollEvent(&event) ) {}
303 unsigned short unicode = 0;
304 if ( ev && (ev->type == SDL_KEYDOWN || ev->type == SDL_KEYUP) )
306 if( key_translation_map.find(ev->key.keysym.sym) != key_translation_map.end() )
307 key = key_translation_map[ ev->key.keysym.sym ];
311 unicode = ev->key.keysym.unicode;
322 if (ev->type == SDL_KEYDOWN)
343 dispatchKeyPressEvent(unicode, key);
346 if (ev->type == SDL_KEYUP)
367 dispatchKeyReleaseEvent(unicode, key);
370 if (ev->type == SDL_MOUSEBUTTONDOWN)
372 if (ev->button.button == SDL_BUTTON_WHEELUP)
373 dispatchMouseWheelEvent(1);
375 if (ev->button.button == SDL_BUTTON_WHEELDOWN)
376 dispatchMouseWheelEvent(-1);
378 if (ev->button.button == SDL_BUTTON_LEFT)
379 dispatchMouseDownEvent(
vl::LeftButton, ev->button.x, ev->button.y);
381 if (ev->button.button == SDL_BUTTON_RIGHT)
384 if (ev->button.button == SDL_BUTTON_MIDDLE)
388 if (ev->type == SDL_MOUSEBUTTONUP)
397 if (ev->button.button == SDL_BUTTON_LEFT)
400 if (ev->button.button == SDL_BUTTON_RIGHT)
403 if (ev->button.button == SDL_BUTTON_MIDDLE)
407 if (ev->type == SDL_MOUSEMOTION)
409 dispatchMouseMoveEvent(ev->motion.x, ev->motion.y);
412 if (ev->type == SDL_VIDEORESIZE)
418 if( ev->type == SDL_SYSWMEVENT )
421 if (ev->syswm.msg->msg == WM_DROPFILES)
423 HDROP hDrop = (HDROP) ev->syswm.msg->wParam;
424 int count = DragQueryFile(hDrop, 0xFFFFFFFF, 0, 0);
425 const int char_count = 1024;
426 std::vector<String> files;
427 for(
int i=0; i<count; ++i)
429 wchar_t file_path[char_count];
430 memset(file_path, 0, char_count);
431 DragQueryFile(hDrop,i,file_path,char_count);
432 files.push_back(file_path);
434 dispatchFileDroppedEvent(files);
439 if (ev->type == SDL_QUIT)
450 if ( SDL_PollEvent(&ev) )
451 mSDLWindow->translateEvent(&ev);
454 if ( mUpdateFlag || mSDLWindow->continuousUpdate() )
456 mSDLWindow->dispatchUpdateEvent();
472 dispatchDestroyEvent();
488 SDL_GL_SwapBuffers();
494 static SDL_SysWMinfo pInfo;
495 SDL_VERSION(&pInfo.version);
496 SDL_GetWMInfo(&pInfo);
497 HWND hWnd = pInfo.window;
499 SetWindowPos(hWnd, 0, x, y, 0, 0, SWP_NOSIZE );
void update()
If the OpenGLContext is a widget this function requests a redraw and generates an updateEvent()...
static SDL_Surface * mScreen
int stencilBufferBits() const
void setPosition(int x, int y)
If the OpenGL context is a widget this function sets its position.
A simple String formatting class.
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
int multisampleSamples() const
const T_Scalar & r() const
static void error(const String &message)
Use this function to provide information about run-time errors: file not found, out of memory...
The SDLWindow class implements an OpenGLContext using the SDL API.
Visualization Library main namespace.
virtual void swapBuffers()
Swaps the back and front buffers to present the last rendering.
The SDL bindings namespace.
const T_Scalar & g() const
The OpenGLContextFormat class encapsulates the settings of an OpenGL rendering context.
bool initSDLWindow(const vl::String &title, const vl::OpenGLContextFormat &info, int x=0, int y=0, int width=640, int height=480)
const ivec4 & rgbaBits() const
static void print(const String &message)
Application message for the user.
const ivec4 & accumRGBABits() const
const T_Scalar & b() const
String format(unsigned long long n, int base, int field, int decimals, int align, int fill, int plus, int finalizer, int eur) const
VLSDL_EXPORT void messageLoop()
void translateEvent(SDL_Event *ev)
int depthBufferBits() const
std::string toStdString() const
Returns a UTF8 encoded std::string.
void setWindowTitle(const vl::String &)
If the OpenGL context is a top window this function sets its title.
void quitApplication()
Quits the event loop.
bool doubleBuffer() const
const T_Scalar & a() const