| 1 | // SPDX-FileCopyrightText: 2011 Torsten Dreyer |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #ifdef HAVE_CONFIG_H |
| 5 | # include <config.h> |
| 6 | #endif |
| 7 | |
| 8 | #include <iostream> |
| 9 | #include <exception> |
| 10 | #include <stdio.h> |
| 11 | |
| 12 | #ifdef HAVE_WINDOWS_H |
| 13 | #include <windows.h> |
| 14 | #endif |
| 15 | #if defined (SG_MAC) |
| 16 | #include <OpenGL/gl.h> |
| 17 | #include <GLUT/glut.h> |
| 18 | #elif defined (_GLES2) |
| 19 | #include <GLES2/gl2.h> |
| 20 | #include "GLES_utils.hxx" |
| 21 | #else |
| 22 | #include <GL/glew.h> // Must be included before <GL/gl.h> |
| 23 | #include <GL/gl.h> |
| 24 | #include <GL/freeglut.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <simgear/compiler.h> |
| 28 | |
| 29 | #include "FGGLApplication.hxx" |
| 30 | |
| 31 | using namespace std; |
| 32 | |
| 33 | FGGLApplication *FGGLApplication::application = NULL; |
| 34 | |
| 35 | FGGLApplication::FGGLApplication (const char * a_name, int argc, char **argv) : |
| 36 | gameMode (false), |
| 37 | name (a_name) { |
| 38 | if (application != NULL ) { |
| 39 | cerr << "Only one instance of FGGLApplication allowed!" << endl; |
| 40 | throw exception (); |
| 41 | } |
| 42 | application = this; |
| 43 | |
| 44 | #ifdef _GLES2 |
| 45 | GLES_utils::instance ().init ("FG Panel"); |
| 46 | #else |
| 47 | glutInit (&argc, argv); |
| 48 | glutInitContextVersion (2, 1); |
| 49 | glutInitContextFlags (GLUT_CORE_PROFILE); |
| 50 | #endif |
| 51 | } |
| 52 | |
| 53 | FGGLApplication::~FGGLApplication () { |
| 54 | #ifndef _GLES2 |
| 55 | if (gameMode) { |
| 56 | glutLeaveGameMode (); |
| 57 | } |
| 58 | #endif |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | FGGLApplication::DisplayCallback () { |
| 63 | if (application) { |
| 64 | application->Display (); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | FGGLApplication::IdleCallback () { |
| 70 | if (application) { |
| 71 | application->Idle (); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | FGGLApplication::KeyCallback (const unsigned char key, const int x, const int y) { |
| 77 | if (application) { |
| 78 | application->Key (key, x, y); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | FGGLApplication::ReshapeCallback (const int width, const int height) { |
| 84 | if (application) { |
| 85 | application->Reshape (width, height); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | FGGLApplication::Run (const int glutMode, |
| 91 | const bool gameMode, |
| 92 | int width, |
| 93 | int height, |
| 94 | const int bpp) { |
| 95 | #ifndef _GLES2 |
| 96 | glutInitDisplayMode (glutMode); |
| 97 | if (gameMode) { |
| 98 | width = glutGet (GLUT_SCREEN_WIDTH); |
| 99 | height = glutGet (GLUT_SCREEN_HEIGHT); |
| 100 | char game_mode_str[20]; |
| 101 | snprintf (game_mode_str, 20, "%dx%d:%d", width, height, bpp); |
| 102 | glutGameModeString (game_mode_str); |
| 103 | glutEnterGameMode (); |
| 104 | this->gameMode = gameMode; |
| 105 | } else { |
| 106 | if (width == -1) { |
| 107 | width = glutGet (GLUT_SCREEN_WIDTH); |
| 108 | } |
| 109 | if (height == -1) { |
| 110 | height = glutGet (GLUT_SCREEN_HEIGHT); |
| 111 | } |
| 112 | glutInitDisplayMode (glutMode); |
| 113 | glutInitWindowSize(width, height); |
| 114 | windowId = glutCreateWindow (name); |
| 115 | } |
| 116 | const GLenum GLEW_err (glewInit ()); |
| 117 | if (GLEW_OK != GLEW_err) { |
| 118 | cerr << "Unable to initialize GLEW " << glewGetErrorString (GLEW_err) << endl; |
| 119 | exit (1); |
| 120 | } |
| 121 | if (!GLEW_VERSION_2_1) { // check that the machine supports the 2.1 API. |
| 122 | cerr << "GLEW version 2.1 not supported : " << glewGetString (GLEW_VERSION) << endl; |
| 123 | exit (1); |
| 124 | } |
| 125 | cout << "OpenGL version = " << glGetString (GL_VERSION) << endl; |
| 126 | #endif |
| 127 | Init (); |
| 128 | #ifdef _GLES2 |
| 129 | GLES_utils::instance ().register_keyboard_func (FGGLApplication::KeyCallback); |
| 130 | GLES_utils::instance ().register_idle_func (FGGLApplication::IdleCallback); |
| 131 | GLES_utils::instance ().register_display_func (FGGLApplication::DisplayCallback); |
| 132 | GLES_utils::instance ().register_reshape_func (FGGLApplication::ReshapeCallback); |
| 133 | GLES_utils::instance ().main_loop (); |
| 134 | #else |
| 135 | glutKeyboardFunc (FGGLApplication::KeyCallback); |
| 136 | glutIdleFunc (FGGLApplication::IdleCallback); |
| 137 | glutDisplayFunc (FGGLApplication::DisplayCallback); |
| 138 | glutReshapeFunc (FGGLApplication::ReshapeCallback); |
| 139 | glutMainLoop (); |
| 140 | #endif |
| 141 | } |