2011-04-14 09:11:17 +00:00
|
|
|
//
|
|
|
|
// Written and (c) Torsten Dreyer - Torsten(at)t3r_dot_de
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
2016-12-04 18:36:53 +00:00
|
|
|
//
|
2011-04-14 09:11:17 +00:00
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
//
|
2011-05-27 18:53:16 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <exception>
|
|
|
|
#include <stdio.h>
|
2011-06-04 19:27:04 +00:00
|
|
|
|
2011-05-27 18:53:16 +00:00
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2011-06-04 19:27:04 +00:00
|
|
|
#if defined (SG_MAC)
|
|
|
|
#include <OpenGL/gl.h>
|
|
|
|
#include <GLUT/glut.h>
|
2016-12-04 18:36:53 +00:00
|
|
|
#elif defined (_GLES2)
|
|
|
|
#include <GLES2/gl2.h>
|
|
|
|
#include "GLES_utils.hxx"
|
2011-06-04 19:27:04 +00:00
|
|
|
#else
|
2016-12-04 18:36:53 +00:00
|
|
|
#include <GL/glew.h> // Must be included before <GL/gl.h>
|
2011-05-27 18:53:16 +00:00
|
|
|
#include <GL/gl.h>
|
2016-12-04 18:36:53 +00:00
|
|
|
#include <GL/freeglut.h>
|
2011-06-04 19:27:04 +00:00
|
|
|
#endif
|
2011-04-14 09:11:17 +00:00
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
#include <simgear/compiler.h>
|
2011-04-14 09:11:17 +00:00
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
#include "FGGLApplication.hxx"
|
2011-04-14 09:11:17 +00:00
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
FGGLApplication *FGGLApplication::application = NULL;
|
|
|
|
|
|
|
|
FGGLApplication::FGGLApplication (const char * a_name, int argc, char **argv) :
|
|
|
|
gameMode (false),
|
|
|
|
name (a_name) {
|
|
|
|
if (application != NULL ) {
|
|
|
|
cerr << "Only one instance of FGGLApplication allowed!" << endl;
|
|
|
|
throw exception ();
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
2016-12-04 18:36:53 +00:00
|
|
|
application = this;
|
2011-04-14 09:11:17 +00:00
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
#ifdef _GLES2
|
|
|
|
GLES_utils::instance ().init ("FG Panel");
|
|
|
|
#else
|
|
|
|
glutInit (&argc, argv);
|
|
|
|
glutInitContextVersion (2, 1);
|
|
|
|
glutInitContextFlags (GLUT_CORE_PROFILE);
|
|
|
|
#endif
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
FGGLApplication::~FGGLApplication () {
|
|
|
|
#ifndef _GLES2
|
|
|
|
if (gameMode) {
|
|
|
|
glutLeaveGameMode ();
|
|
|
|
}
|
|
|
|
#endif
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
void
|
|
|
|
FGGLApplication::DisplayCallback () {
|
|
|
|
if (application) {
|
|
|
|
application->Display ();
|
|
|
|
}
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
void
|
|
|
|
FGGLApplication::IdleCallback () {
|
|
|
|
if (application) {
|
|
|
|
application->Idle ();
|
|
|
|
}
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
void
|
|
|
|
FGGLApplication::KeyCallback (const unsigned char key, const int x, const int y) {
|
|
|
|
if (application) {
|
|
|
|
application->Key (key, x, y);
|
|
|
|
}
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
void
|
|
|
|
FGGLApplication::ReshapeCallback (const int width, const int height) {
|
|
|
|
if (application) {
|
|
|
|
application->Reshape (width, height);
|
|
|
|
}
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
void
|
|
|
|
FGGLApplication::Run (const int glutMode,
|
|
|
|
const bool gameMode,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
const int bpp) {
|
|
|
|
#ifndef _GLES2
|
|
|
|
glutInitDisplayMode (glutMode);
|
|
|
|
if (gameMode) {
|
|
|
|
width = glutGet (GLUT_SCREEN_WIDTH);
|
|
|
|
height = glutGet (GLUT_SCREEN_HEIGHT);
|
2011-04-14 09:11:17 +00:00
|
|
|
char game_mode_str[20];
|
2016-12-04 18:36:53 +00:00
|
|
|
snprintf (game_mode_str, 20, "%dx%d:%d", width, height, bpp);
|
|
|
|
glutGameModeString (game_mode_str);
|
|
|
|
glutEnterGameMode ();
|
2011-04-14 09:11:17 +00:00
|
|
|
this->gameMode = gameMode;
|
|
|
|
} else {
|
2016-12-04 18:36:53 +00:00
|
|
|
if (width == -1) {
|
|
|
|
width = glutGet (GLUT_SCREEN_WIDTH);
|
|
|
|
}
|
|
|
|
if (height == -1) {
|
|
|
|
height = glutGet (GLUT_SCREEN_HEIGHT);
|
|
|
|
}
|
|
|
|
glutInitDisplayMode (glutMode);
|
|
|
|
glutInitWindowSize(width, height);
|
|
|
|
windowId = glutCreateWindow (name);
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
2016-12-04 18:36:53 +00:00
|
|
|
const GLenum GLEW_err (glewInit ());
|
|
|
|
if (GLEW_OK != GLEW_err) {
|
|
|
|
cerr << "Unable to initialize GLEW " << glewGetErrorString (GLEW_err) << endl;
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
if (!GLEW_VERSION_2_1) { // check that the machine supports the 2.1 API.
|
|
|
|
cerr << "GLEW version 2.1 not supported : " << glewGetString (GLEW_VERSION) << endl;
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
cout << "OpenGL version = " << glGetString (GL_VERSION) << endl;
|
|
|
|
#endif
|
|
|
|
Init ();
|
|
|
|
#ifdef _GLES2
|
|
|
|
GLES_utils::instance ().register_keyboard_func (FGGLApplication::KeyCallback);
|
|
|
|
GLES_utils::instance ().register_idle_func (FGGLApplication::IdleCallback);
|
|
|
|
GLES_utils::instance ().register_display_func (FGGLApplication::DisplayCallback);
|
|
|
|
GLES_utils::instance ().register_reshape_func (FGGLApplication::ReshapeCallback);
|
|
|
|
GLES_utils::instance ().main_loop ();
|
|
|
|
#else
|
|
|
|
glutKeyboardFunc (FGGLApplication::KeyCallback);
|
|
|
|
glutIdleFunc (FGGLApplication::IdleCallback);
|
|
|
|
glutDisplayFunc (FGGLApplication::DisplayCallback);
|
|
|
|
glutReshapeFunc (FGGLApplication::ReshapeCallback);
|
|
|
|
glutMainLoop ();
|
|
|
|
#endif
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|