SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppGLFW.cpp File Reference

App::run implementation from App.h for the GLFW platform. More...

#include <App.h>
#include <SLGLState.h>
#include <SLEnums.h>
#include <SLInterface.h>
#include <AppCommon.h>
#include <SLAssetManager.h>
#include <SLScene.h>
#include <SLSceneView.h>
#include <CVCapture.h>
#include <Profiler.h>
#include <SLAssetLoader.h>
#include <GLFW/glfw3.h>
Include dependency graph for AppGLFW.cpp:

Go to the source code of this file.

Macros

#define GLFW_INCLUDE_NONE
 

Functions

static SLbool onPaint ()
 Static paint function that gets called once every frame. More...
 
static void onGLFWError (int error, const char *description)
 
static void onResize (GLFWwindow *myWindow, int width, int height)
 
static void onMouseButton (GLFWwindow *myWindow, int button, int action, int mods)
 
static void onMouseMove (GLFWwindow *myWindow, double x, double y)
 
static void onMouseWheel (GLFWwindow *myWindow, double xscroll, double yscroll)
 
static void onKey (GLFWwindow *myWindow, int GLFWKey, int scancode, int action, int mods)
 
static void onCharInput (GLFWwindow *, SLuint c)
 Event handler for GLFW character input. More...
 
static void onClose (GLFWwindow *myWindow)
 
static SLKey mapKeyToSLKey (int key)
 Maps the GLFW key codes to the SLKey codes. More...
 

Variables

static GLFWwindow * window
 The global glfw window handle. More...
 
static SLint svIndex
 Scene view index. More...
 
static SLint scrWidth
 Window width at start up. More...
 
static SLint scrHeight
 Window height at start up. More...
 
static SLfloat contentScaleX
 Content scale in X direction. More...
 
static SLfloat contentScaleY
 Content scale in Y direction. More...
 
static SLint dpi = 142
 Dot per inch resolution of screen. More...
 
static SLbool fixAspectRatio = false
 Flag if wnd aspect ratio should be fixed. More...
 
static SLint startX
 start position x in pixels More...
 
static SLint startY
 start position y in pixels More...
 
static SLint mouseX
 Last mouse position x in pixels. More...
 
static SLint mouseY
 Last mouse position y in pixels. More...
 
static SLint lastWidth
 Last window width in pixels. More...
 
static SLint lastHeight
 Last window height in pixels. More...
 
static SLfloat lastMouseDownTime = 0.0f
 Last mouse press time. More...
 
static SLKey modifiers = K_none
 last modifier keys More...
 
static SLbool fullscreen = false
 flag if window is in fullscreen mode More...
 
static SLVec2i windowPosBeforeFullscreen
 Window position before entering fullscreen mode. More...
 

Detailed Description

App::run implementation from App.h for the GLFW platform.

The functions implement mostly the callbacks for the platform independent OpenGL window framework for desktop OS. For more info on how to create a new app with SLProject see: https://github.com/cpvrlab/SLProject4/wiki/Creating-a-New-App For more info about App framework see: https://cpvrlab.github.io/SLProject4/app-framework.html

Date
June 2024
Authors
Marino von Wattenwyl
Remarks
Please use clangformat to format the code. See more code style on https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style

Definition in file AppGLFW.cpp.

Macro Definition Documentation

◆ GLFW_INCLUDE_NONE

#define GLFW_INCLUDE_NONE

Definition at line 29 of file AppGLFW.cpp.

Function Documentation

◆ mapKeyToSLKey()

static SLKey mapKeyToSLKey ( int  key)
static

Maps the GLFW key codes to the SLKey codes.

Definition at line 504 of file AppGLFW.cpp.

505 {
506  switch (key)
507  {
508  case GLFW_KEY_SPACE: return K_space;
509  case GLFW_KEY_ESCAPE: return K_esc;
510  case GLFW_KEY_F1: return K_F1;
511  case GLFW_KEY_F2: return K_F2;
512  case GLFW_KEY_F3: return K_F3;
513  case GLFW_KEY_F4: return K_F4;
514  case GLFW_KEY_F5: return K_F5;
515  case GLFW_KEY_F6: return K_F6;
516  case GLFW_KEY_F7: return K_F7;
517  case GLFW_KEY_F8: return K_F8;
518  case GLFW_KEY_F9: return K_F9;
519  case GLFW_KEY_F10: return K_F10;
520  case GLFW_KEY_F11: return K_F11;
521  case GLFW_KEY_F12: return K_F12;
522  case GLFW_KEY_UP: return K_up;
523  case GLFW_KEY_DOWN: return K_down;
524  case GLFW_KEY_LEFT: return K_left;
525  case GLFW_KEY_RIGHT: return K_right;
526  case GLFW_KEY_LEFT_SHIFT:
527  case GLFW_KEY_RIGHT_SHIFT: return K_shift;
528  case GLFW_KEY_LEFT_CONTROL:
529  case GLFW_KEY_RIGHT_CONTROL: return K_ctrl;
530  case GLFW_KEY_LEFT_ALT:
531  case GLFW_KEY_RIGHT_ALT: return K_alt;
532  case GLFW_KEY_LEFT_SUPER:
533  case GLFW_KEY_RIGHT_SUPER: return K_super; // Apple command key
534  case GLFW_KEY_TAB: return K_tab;
535  case GLFW_KEY_ENTER: return K_enter;
536  case GLFW_KEY_BACKSPACE: return K_backspace;
537  case GLFW_KEY_INSERT: return K_insert;
538  case GLFW_KEY_DELETE: return K_delete;
539  case GLFW_KEY_PAGE_UP: return K_pageUp;
540  case GLFW_KEY_PAGE_DOWN: return K_pageDown;
541  case GLFW_KEY_HOME: return K_home;
542  case GLFW_KEY_END: return K_end;
543  case GLFW_KEY_KP_0: return K_NP0;
544  case GLFW_KEY_KP_1: return K_NP1;
545  case GLFW_KEY_KP_2: return K_NP2;
546  case GLFW_KEY_KP_3: return K_NP3;
547  case GLFW_KEY_KP_4: return K_NP4;
548  case GLFW_KEY_KP_5: return K_NP5;
549  case GLFW_KEY_KP_6: return K_NP6;
550  case GLFW_KEY_KP_7: return K_NP7;
551  case GLFW_KEY_KP_8: return K_NP8;
552  case GLFW_KEY_KP_9: return K_NP9;
553  case GLFW_KEY_KP_DIVIDE: return K_NPDivide;
554  case GLFW_KEY_KP_MULTIPLY: return K_NPMultiply;
555  case GLFW_KEY_KP_SUBTRACT: return K_NPSubtract;
556  case GLFW_KEY_KP_ADD: return K_NPAdd;
557  case GLFW_KEY_KP_DECIMAL: return K_NPDecimal;
558  case GLFW_KEY_UNKNOWN: return K_none;
559  default: break;
560  }
561  return (SLKey)key;
562 }
SLKey
Keyboard key codes enumeration.
Definition: SLEnums.h:16
@ K_down
Definition: SLEnums.h:25
@ K_NP5
Definition: SLEnums.h:38
@ K_delete
Definition: SLEnums.h:23
@ K_space
Definition: SLEnums.h:18
@ K_F2
Definition: SLEnums.h:50
@ K_F1
Definition: SLEnums.h:49
@ K_F12
Definition: SLEnums.h:60
@ K_NP9
Definition: SLEnums.h:42
@ K_F6
Definition: SLEnums.h:54
@ K_F4
Definition: SLEnums.h:52
@ K_up
Definition: SLEnums.h:24
@ K_enter
Definition: SLEnums.h:20
@ K_esc
Definition: SLEnums.h:21
@ K_none
Definition: SLEnums.h:17
@ K_tab
Definition: SLEnums.h:19
@ K_NP6
Definition: SLEnums.h:39
@ K_shift
Definition: SLEnums.h:62
@ K_end
Definition: SLEnums.h:29
@ K_insert
Definition: SLEnums.h:30
@ K_right
Definition: SLEnums.h:26
@ K_F9
Definition: SLEnums.h:57
@ K_NPDivide
Definition: SLEnums.h:43
@ K_pageDown
Definition: SLEnums.h:32
@ K_F8
Definition: SLEnums.h:56
@ K_F5
Definition: SLEnums.h:53
@ K_pageUp
Definition: SLEnums.h:31
@ K_NPMultiply
Definition: SLEnums.h:44
@ K_NPSubtract
Definition: SLEnums.h:46
@ K_NP8
Definition: SLEnums.h:41
@ K_NP1
Definition: SLEnums.h:34
@ K_NP3
Definition: SLEnums.h:36
@ K_ctrl
Definition: SLEnums.h:63
@ K_NP7
Definition: SLEnums.h:40
@ K_NP2
Definition: SLEnums.h:35
@ K_F10
Definition: SLEnums.h:58
@ K_F11
Definition: SLEnums.h:59
@ K_NP4
Definition: SLEnums.h:37
@ K_super
Definition: SLEnums.h:61
@ K_F3
Definition: SLEnums.h:51
@ K_F7
Definition: SLEnums.h:55
@ K_alt
Definition: SLEnums.h:64
@ K_left
Definition: SLEnums.h:27
@ K_NP0
Definition: SLEnums.h:33
@ K_backspace
Definition: SLEnums.h:22
@ K_home
Definition: SLEnums.h:28
@ K_NPDecimal
Definition: SLEnums.h:48
@ K_NPAdd
Definition: SLEnums.h:45

◆ onCharInput()

void onCharInput ( GLFWwindow *  ,
SLuint  c 
)
static

Event handler for GLFW character input.

Definition at line 489 of file AppGLFW.cpp.

490 {
491  slCharInput(svIndex, c);
492 }
static SLint svIndex
Scene view index.
Definition: AppGLFW.cpp:36
void slCharInput(int sceneViewIndex, unsigned int character)

◆ onClose()

void onClose ( GLFWwindow *  myWindow)
static

onClose event handler for deallocation of the scene & sceneview. onClose is called glfwPollEvents, glfwWaitEvents or glfwSwapBuffers.

Definition at line 498 of file AppGLFW.cpp.

499 {
500  slShouldClose(true);
501 }
bool slShouldClose()

◆ onGLFWError()

static void onGLFWError ( int  error,
const char *  description 
)
static

Error callback handler for GLFW.

Definition at line 256 of file AppGLFW.cpp.

257 {
258  fputs(description, stderr);
259 }

◆ onKey()

static void onKey ( GLFWwindow *  myWindow,
int  GLFWKey,
int  scancode,
int  action,
int  mods 
)
static

Key event handler sets the modifier key state & forwards the event to the slKeyPress/slKeyRelease functions.

Definition at line 420 of file AppGLFW.cpp.

425 {
426  SLKey key = mapKeyToSLKey(GLFWKey);
427 
428  // Do not handle key events if scene is being loaded.
429  if (!AppCommon::scene) return;
430 
431  if (action == GLFW_PRESS)
432  {
433  switch (key)
434  {
435  case K_ctrl: modifiers = (SLKey)(modifiers | K_ctrl); return;
436  case K_alt: modifiers = (SLKey)(modifiers | K_alt); return;
437  case K_shift: modifiers = (SLKey)(modifiers | K_shift); return;
438  default: break;
439  }
440  }
441  else if (action == GLFW_RELEASE)
442  {
443  switch (key)
444  {
445  case K_ctrl: modifiers = (SLKey)(modifiers ^ K_ctrl); return;
446  case K_alt: modifiers = (SLKey)(modifiers ^ K_alt); return;
447  case K_shift: modifiers = (SLKey)(modifiers ^ K_shift); return;
448  default: break;
449  }
450  }
451 
452  // Special treatment for ESC key
453  if (key == K_esc && action == GLFW_RELEASE && fullscreen)
454  {
455  fullscreen = false;
456  glfwSetWindowSize(myWindow, scrWidth, scrHeight);
457  glfwSetWindowPos(myWindow, windowPosBeforeFullscreen.x, windowPosBeforeFullscreen.y);
458  }
459 
460  // Toggle fullscreen mode
461  if (key == K_F9 && action == GLFW_PRESS)
462  {
464 
465  if (fullscreen)
466  {
467  GLFWmonitor* primary = glfwGetPrimaryMonitor();
468  const GLFWvidmode* mode = glfwGetVideoMode(primary);
469  glfwSetWindowSize(myWindow, mode->width, mode->height);
470  glfwGetWindowPos(myWindow, &windowPosBeforeFullscreen.x, &windowPosBeforeFullscreen.y);
471  glfwSetWindowPos(myWindow, 0, 0);
472  }
473  else
474  {
475  glfwSetWindowSize(myWindow, scrWidth, scrHeight);
476  glfwSetWindowPos(myWindow, windowPosBeforeFullscreen.x, windowPosBeforeFullscreen.y);
477  }
478 
479  return;
480  }
481 
482  if (action == GLFW_PRESS)
484  else if (action == GLFW_RELEASE)
486 }
static SLint scrHeight
Window height at start up.
Definition: AppGLFW.cpp:38
static SLKey modifiers
last modifier keys
Definition: AppGLFW.cpp:50
static SLbool fullscreen
flag if window is in fullscreen mode
Definition: AppGLFW.cpp:51
static SLVec2i windowPosBeforeFullscreen
Window position before entering fullscreen mode.
Definition: AppGLFW.cpp:52
static SLint scrWidth
Window width at start up.
Definition: AppGLFW.cpp:37
static SLKey mapKeyToSLKey(int key)
Maps the GLFW key codes to the SLKey codes.
Definition: AppGLFW.cpp:504
void slKeyRelease(int sceneViewIndex, SLKey key, SLKey modifier)
void slKeyPress(int sceneViewIndex, SLKey key, SLKey modifier)
static WAI::ModeOrbSlam2 * mode
Definition: WAIInterface.cpp:5
static SLScene * scene
Pointer to the one and only SLScene instance.
Definition: AppCommon.h:61
T y
Definition: SLVec2.h:30
T x
Definition: SLVec2.h:30

◆ onMouseButton()

static void onMouseButton ( GLFWwindow *  myWindow,
int  button,
int  action,
int  mods 
)
static

Mouse button event handler forwards the events to the slMouseDown or slMouseUp. Two finger touches of touch devices are simulated with ALT & CTRL modifiers.

Definition at line 300 of file AppGLFW.cpp.

304 {
305  SLint x = mouseX;
306  SLint y = mouseY;
307  startX = x;
308  startY = y;
309 
310  // Translate modifiers
311  modifiers = K_none;
312  if ((uint)mods & (uint)GLFW_MOD_SHIFT) modifiers = (SLKey)(modifiers | K_shift);
313  if ((uint)mods & (uint)GLFW_MOD_CONTROL) modifiers = (SLKey)(modifiers | K_ctrl);
314  if ((uint)mods & (uint)GLFW_MOD_ALT) modifiers = (SLKey)(modifiers | K_alt);
315 
316  if (action == GLFW_PRESS)
317  {
318  SLfloat mouseDeltaTime = (SLfloat)glfwGetTime() - lastMouseDownTime;
319  lastMouseDownTime = (SLfloat)glfwGetTime();
320 
321  // handle double click
322  if (mouseDeltaTime < 0.3f)
323  {
324  switch (button)
325  {
326  case GLFW_MOUSE_BUTTON_LEFT:
328  break;
329  case GLFW_MOUSE_BUTTON_RIGHT:
331  break;
332  case GLFW_MOUSE_BUTTON_MIDDLE:
334  break;
335  default: break;
336  }
337  }
338  else // normal mouse clicks
339  {
340  switch (button)
341  {
342  case GLFW_MOUSE_BUTTON_LEFT:
343  if (modifiers & K_alt && modifiers & K_ctrl)
344  slTouch2Down(svIndex, x - 20, y, x + 20, y);
345  else
347  break;
348  case GLFW_MOUSE_BUTTON_RIGHT:
350  break;
351  case GLFW_MOUSE_BUTTON_MIDDLE:
353  break;
354  default: break;
355  }
356  }
357  }
358  else
359  { // flag end of mouse click for long touches
360  startX = -1;
361  startY = -1;
362 
363  switch (button)
364  {
365  case GLFW_MOUSE_BUTTON_LEFT:
367  break;
368  case GLFW_MOUSE_BUTTON_RIGHT:
370  break;
371  case GLFW_MOUSE_BUTTON_MIDDLE:
373  break;
374  default: break;
375  }
376  }
377 }
static SLint startY
start position y in pixels
Definition: AppGLFW.cpp:44
static SLfloat lastMouseDownTime
Last mouse press time.
Definition: AppGLFW.cpp:49
static SLint mouseX
Last mouse position x in pixels.
Definition: AppGLFW.cpp:45
static SLint startX
start position x in pixels
Definition: AppGLFW.cpp:43
static SLint mouseY
Last mouse position y in pixels.
Definition: AppGLFW.cpp:46
float SLfloat
Definition: SL.h:173
int SLint
Definition: SL.h:170
@ MB_left
Definition: SLEnums.h:100
@ MB_right
Definition: SLEnums.h:102
@ MB_middle
Definition: SLEnums.h:101
void slMouseDown(int sceneViewIndex, SLMouseButton button, int xpos, int ypos, SLKey modifier)
void slTouch2Down(int sceneViewIndex, int xpos1, int ypos1, int xpos2, int ypos2)
void slMouseUp(int sceneViewIndex, SLMouseButton button, int xpos, int ypos, SLKey modifier)
void slDoubleClick(int sceneViewIndex, SLMouseButton button, int xpos, int ypos, SLKey modifier)

◆ onMouseMove()

static void onMouseMove ( GLFWwindow *  myWindow,
double  x,
double  y 
)
static

Mouse move event handler forwards the events to slMouseMove or slTouch2Move.

Definition at line 382 of file AppGLFW.cpp.

385 {
386  // x & y are in screen coords.
387  mouseX = (int)x;
388  mouseY = (int)y;
389 
390  if (modifiers & K_alt && modifiers & K_ctrl)
392  (int)(x - 20),
393  (int)y,
394  (int)(x + 20),
395  (int)y);
396  else
398  (int)x,
399  (int)y);
400 }
void slTouch2Move(int sceneViewIndex, int xpos1, int ypos1, int xpos2, int ypos2)
void slMouseMove(int sceneViewIndex, int x, int y)

◆ onMouseWheel()

static void onMouseWheel ( GLFWwindow *  myWindow,
double  xscroll,
double  yscroll 
)
static

Mouse wheel event handler forwards the events to slMouseWheel

Definition at line 405 of file AppGLFW.cpp.

408 {
409  // make sure the delta is at least one integer
410  int dY = (int)yscroll;
411  if (dY == 0) dY = (int)(Utils::sign(yscroll));
412 
414 }
void slMouseWheel(int sceneViewIndex, int pos, SLKey modifier)
T sign(T a)
Definition: Utils.h:245

◆ onPaint()

static SLbool onPaint ( )
static

Static paint function that gets called once every frame.

Definition at line 222 of file AppGLFW.cpp.

223 {
224  PROFILE_SCOPE("AppGLFW::onPaint");
225 
226  if (AppCommon::sceneViews.empty())
227  return false;
228 
230 
232  {
234  AppCommon::sceneToLoad = {}; // sets optional to empty
235  }
236 
237  if (AppCommon::assetLoader->isLoading())
239 
240  ////////////////////////////////////////////////////////////////
241  SLbool appNeedsUpdate = App::config.onUpdate && App::config.onUpdate(sv);
242  SLbool jobIsRunning = slUpdateParallelJob();
243  SLbool isLoading = AppCommon::assetLoader->isLoading();
244  SLbool viewNeedsUpdate = slPaintAllViews();
245  ////////////////////////////////////////////////////////////////
246 
247  // Fast copy the back buffer to the front buffer. This is OS dependent.
248  glfwSwapBuffers(window);
249 
250  return appNeedsUpdate || viewNeedsUpdate || jobIsRunning || isLoading;
251 }
static GLFWwindow * window
The global glfw window handle.
Definition: AppGLFW.cpp:35
#define PROFILE_SCOPE(name)
Definition: Instrumentor.h:40
bool SLbool
Definition: SL.h:175
void slSwitchScene(SLSceneView *sv, SLSceneID sceneID)
bool slPaintAllViews()
bool slUpdateParallelJob()
static optional< SLSceneID > sceneToLoad
Scene id to load at start up.
Definition: AppCommon.h:90
static SLVSceneView sceneViews
Vector of sceneview pointers.
Definition: AppCommon.h:62
static SLAssetLoader * assetLoader
Asset-loader for async asset loading.
Definition: AppCommon.h:60
void checkIfAsyncLoadingIsDone()
bool isLoading() const
Definition: SLAssetLoader.h:68
SceneView class represents a dynamic real time 3D view onto the scene.
Definition: SLSceneView.h:69
Config config
The configuration set in App::run.
Definition: AppAndroid.cpp:34
OnUpdateCallback onUpdate
Definition: App.h:71

◆ onResize()

static void onResize ( GLFWwindow *  myWindow,
int  width,
int  height 
)
static

onResize: Event handler called on the resize event of the window. This event should called once before the onPaint event.

Definition at line 265 of file AppGLFW.cpp.

266 {
267  if (AppCommon::sceneViews.empty()) return;
269 
270  if (fixAspectRatio)
271  {
272  float aspectRatio = (float)width / (float)height;
273 
274  // correct target width and height
275  if ((float)height * aspectRatio <= (float)width)
276  {
277  width = (int)((float)height * aspectRatio);
278  height = (int)((float)width / aspectRatio);
279  }
280  else
281  {
282  height = (int)((float)width / aspectRatio);
283  width = (int)((float)height * aspectRatio);
284  }
285  }
286 
287  lastWidth = width;
288  lastHeight = height;
289 
290  // width & height are in screen coords.
291  slResize(svIndex, width, height);
292 
293  onPaint();
294 }
static SLbool onPaint()
Static paint function that gets called once every frame.
Definition: AppGLFW.cpp:222
static SLint lastHeight
Last window height in pixels.
Definition: AppGLFW.cpp:48
static SLint lastWidth
Last window width in pixels.
Definition: AppGLFW.cpp:47
static SLbool fixAspectRatio
Flag if wnd aspect ratio should be fixed.
Definition: AppGLFW.cpp:42
void slResize(int sceneViewIndex, int width, int height)

Variable Documentation

◆ contentScaleX

SLfloat contentScaleX
static

Content scale in X direction.

Definition at line 39 of file AppGLFW.cpp.

◆ contentScaleY

SLfloat contentScaleY
static

Content scale in Y direction.

Definition at line 40 of file AppGLFW.cpp.

◆ dpi

SLint dpi = 142
static

Dot per inch resolution of screen.

Definition at line 41 of file AppGLFW.cpp.

◆ fixAspectRatio

SLbool fixAspectRatio = false
static

Flag if wnd aspect ratio should be fixed.

Definition at line 42 of file AppGLFW.cpp.

◆ fullscreen

SLbool fullscreen = false
static

flag if window is in fullscreen mode

Definition at line 51 of file AppGLFW.cpp.

◆ lastHeight

SLint lastHeight
static

Last window height in pixels.

Definition at line 48 of file AppGLFW.cpp.

◆ lastMouseDownTime

SLfloat lastMouseDownTime = 0.0f
static

Last mouse press time.

Definition at line 49 of file AppGLFW.cpp.

◆ lastWidth

SLint lastWidth
static

Last window width in pixels.

Definition at line 47 of file AppGLFW.cpp.

◆ modifiers

SLKey modifiers = K_none
static

last modifier keys

Definition at line 50 of file AppGLFW.cpp.

◆ mouseX

SLint mouseX
static

Last mouse position x in pixels.

Definition at line 45 of file AppGLFW.cpp.

◆ mouseY

SLint mouseY
static

Last mouse position y in pixels.

Definition at line 46 of file AppGLFW.cpp.

◆ scrHeight

SLint scrHeight
static

Window height at start up.

Definition at line 38 of file AppGLFW.cpp.

◆ scrWidth

SLint scrWidth
static

Window width at start up.

Definition at line 37 of file AppGLFW.cpp.

◆ startX

SLint startX
static

start position x in pixels

Definition at line 43 of file AppGLFW.cpp.

◆ startY

SLint startY
static

start position y in pixels

Definition at line 44 of file AppGLFW.cpp.

◆ svIndex

SLint svIndex
static

Scene view index.

Definition at line 36 of file AppGLFW.cpp.

◆ window

GLFWwindow* window
static

The global glfw window handle.

Definition at line 35 of file AppGLFW.cpp.

◆ windowPosBeforeFullscreen

SLVec2i windowPosBeforeFullscreen
static

Window position before entering fullscreen mode.

Definition at line 52 of file AppGLFW.cpp.