Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

glutmain.cpp File Reference

#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <time.h>
#include <unistd.h>
#include <celengine/gl.h>
#include <GL/glut.h>
#include <celengine/celestia.h>
#include <celmath/vecmath.h>
#include <celmath/quaternion.h>
#include <celutil/util.h>
#include <celutil/debug.h>
#include <celmath/mathlib.h>
#include <celengine/astro.h>
#include "celestiacore.h"

Include dependency graph for glutmain.cpp:

Go to the source code of this file.

Defines

#define MOUSE_WHEEL_DOWN   4
#define MOUSE_WHEEL_UP   3

Functions

static void Display (void)
static void HandleSpecialKey (int key, bool down)
static void Idle (void)
static void KeyPress (unsigned char c, int x, int y)
static void KeyUp (unsigned char c, int x, int y)
int main (int argc, char *argv[])
static void MouseButton (int button, int state, int x, int y)
static void MouseDrag (int x, int y)
static void Resize (int w, int h)
static void SpecialKeyPress (int key, int x, int y)
static void SpecialKeyUp (int key, int x, int y)

Variables

static CelestiaCoreappCore = NULL
char AppName [] = "Celestia"
static int lastX = 0
static int lastY = 0
static bool leftButton = false
static int mainWindow = 1
static bool middleButton = false
static bool ready = false
static bool rightButton = false


Define Documentation

#define MOUSE_WHEEL_DOWN   4
 

Definition at line 61 of file glutmain.cpp.

Referenced by MouseButton().

#define MOUSE_WHEEL_UP   3
 

Definition at line 60 of file glutmain.cpp.

Referenced by MouseButton().


Function Documentation

static void Display void   )  [static]
 

Definition at line 75 of file glutmain.cpp.

References appCore, CelestiaCore::draw(), and ready.

Referenced by Idle(), and main().

00076 {
00077     if (ready)
00078     {
00079         appCore->draw();
00080         glutSwapBuffers();
00081     }
00082 }

static void HandleSpecialKey int  key,
bool  down
[static]
 

Definition at line 176 of file glutmain.cpp.

References appCore, CelestiaCore::keyDown(), and CelestiaCore::keyUp().

Referenced by SpecialKeyPress(), and SpecialKeyUp().

00177 {
00178     int k = -1;
00179     switch (key)
00180     {
00181     case GLUT_KEY_UP:
00182         k = CelestiaCore::Key_Up;
00183         break;
00184     case GLUT_KEY_DOWN:
00185         k = CelestiaCore::Key_Down;
00186         break;
00187     case GLUT_KEY_LEFT:
00188         k = CelestiaCore::Key_Left;
00189         break;
00190     case GLUT_KEY_RIGHT:
00191         k = CelestiaCore::Key_Right;
00192         break;
00193     case GLUT_KEY_HOME:
00194         k = CelestiaCore::Key_Home;
00195         break;
00196     case GLUT_KEY_END:
00197         k = CelestiaCore::Key_End;
00198         break;
00199     case GLUT_KEY_F1:
00200         k = CelestiaCore::Key_F1;
00201         break;
00202     case GLUT_KEY_F2:
00203         k = CelestiaCore::Key_F2;
00204         break;
00205     case GLUT_KEY_F3:
00206         k = CelestiaCore::Key_F3;
00207         break;
00208     case GLUT_KEY_F4:
00209         k = CelestiaCore::Key_F4;
00210         break;
00211     case GLUT_KEY_F5:
00212         k = CelestiaCore::Key_F5;
00213         break;
00214     case GLUT_KEY_F6:
00215         k = CelestiaCore::Key_F6;
00216         break;
00217     case GLUT_KEY_F7:
00218         k = CelestiaCore::Key_F7;
00219         break;
00220     case GLUT_KEY_F11:
00221         k = CelestiaCore::Key_F11;
00222         break;
00223     case GLUT_KEY_F12:
00224         k = CelestiaCore::Key_F12;
00225         break;
00226     }
00227     /* Glut doesn't seem to handle Keypad Keys, so we can't pass them on.
00228        They will be passed as the appropriate Special keys instead */
00229 
00230     if (k >= 0)
00231     {
00232         if (down)
00233             appCore->keyDown(k);
00234         else
00235             appCore->keyUp(k);
00236     }
00237 }

static void Idle void   )  [static]
 

Definition at line 84 of file glutmain.cpp.

References appCore, Display(), mainWindow, and CelestiaCore::tick().

Referenced by main().

00085 {
00086     if (glutGetWindow() != mainWindow)
00087         glutSetWindow(mainWindow);
00088 
00089     appCore->tick();
00090 
00091     Display();
00092 }

static void KeyPress unsigned char  c,
int  x,
int  y
[static]
 

Definition at line 159 of file glutmain.cpp.

References appCore, and CelestiaCore::charEntered().

Referenced by main().

00160 {
00161     // Ctrl-Q exits
00162     if (c == '\021')
00163         exit(0);
00164 
00165     appCore->charEntered((char) c);
00166     //appCore->keyDown((int) c);
00167 }

static void KeyUp unsigned char  c,
int  x,
int  y
[static]
 

Definition at line 170 of file glutmain.cpp.

References appCore, and CelestiaCore::keyUp().

Referenced by main().

00171 {
00172     appCore->keyUp((int) c);
00173 }

int main int  argc,
char *  argv[]
 

Definition at line 433 of file glutmain.cpp.

References appCore, Display(), Idle(), CelestiaCore::initRenderer(), CelestiaCore::initSimulation(), KeyPress(), KeyUp(), mainWindow, MouseButton(), MouseDrag(), ready, Resize(), CelestiaCore::runScript(), SetDebugVerbosity(), CelestiaCore::setTimeZoneBias(), CelestiaCore::setTimeZoneName(), SpecialKeyPress(), SpecialKeyUp(), and CelestiaCore::start().

00434 {
00435         setlocale(LC_ALL, "");
00436         setlocale(LC_NUMERIC, "C");
00437         bindtextdomain(PACKAGE, LOCALEDIR);
00438         bind_textdomain_codeset(PACKAGE, "UTF-8");
00439         textdomain(PACKAGE);
00440                         
00441    #ifdef MACOSX
00442    #define BUNDLEONLY 1
00443    #ifndef BUNDLEONLY
00444    // for bundles only!
00445    if (argc==2 && argv[1][0]=='-' && argv[1][1]=='p' && argv[1][2]=='s' && argv[1][3]=='n')
00446    {
00447    #endif /* !BUNDLEONLY */
00448         argc--;
00449         dirFixup(argv[0]);
00450     #ifndef BUNDLEONLY
00451     } else  
00452     /* BE REAL DAMN CAREFUL WITH THIS LINGERING IF! */
00453     #endif /* !BUNDLEONLY */
00454     #else /* MACOSX */
00455     if (chdir(CONFIG_DATA_DIR) == -1)
00456     {
00457         cerr << "Cannot chdir to '" << CONFIG_DATA_DIR <<
00458             "', probably due to improper installation\n";
00459     }
00460     #endif /* !MACOSX */
00461     // Not ready to render yet
00462     ready = false;
00463 
00464     char c;
00465         int startfile = 0;
00466     while ((c = getopt(argc, argv, "v::f")) > -1)
00467     {
00468         if (c == '?')
00469         {
00470             cout << "Usage: celestia [-v] [-f <filename>]\n";
00471             exit(1);
00472         }
00473         else if (c == 'v')
00474         {
00475             if(optarg)
00476                 SetDebugVerbosity(atoi(optarg));
00477             else
00478                 SetDebugVerbosity(0);
00479         }
00480                 else if (c == 'f') {
00481                         startfile = 1;
00482                 }
00483     }
00484 
00485     appCore = new CelestiaCore();
00486     if (appCore == NULL)
00487     {
00488         cerr << "Out of memory.\n";
00489         return 1;
00490     }
00491 
00492     if (!appCore->initSimulation())
00493     {
00494         return 1;
00495     }
00496 
00497     glutInit(&argc, argv);
00498     glutInitWindowSize(480, 360);
00499     glutInitWindowPosition(0, 0);
00500     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
00501     mainWindow = glutCreateWindow("Celestia");
00502 
00503     Resize(480, 360);
00504     glutReshapeFunc(Resize);
00505     glutDisplayFunc(Display);
00506     glutIdleFunc(Idle);
00507     glutMouseFunc(MouseButton);
00508     glutMotionFunc(MouseDrag);
00509     glutKeyboardFunc(KeyPress);
00510     glutKeyboardUpFunc(KeyUp);
00511     glutSpecialFunc(SpecialKeyPress);
00512     glutSpecialUpFunc(SpecialKeyUp);
00513 
00514     #ifdef MACOSX
00515     initMenus();
00516     #endif
00517 
00518     // GL should be all set up, now initialize the renderer.
00519     appCore->initRenderer();
00520 
00521     // Set the simulation starting time to the current system time
00522     time_t curtime=time(NULL);
00523     appCore->start((double) curtime / 86400.0 + (double) astro::Date(1970, 1, 1));
00524     #ifdef MACOSX
00525     /* localtime in Darwin is is reentrant only
00526        equiv to Linux localtime_r()
00527        should probably port !MACOSX code to use this too, available since
00528        libc 5.2.5 according to manpage */
00529     struct tm *temptime=localtime(&curtime);
00530     appCore->setTimeZoneBias(temptime->tm_gmtoff);
00531     appCore->setTimeZoneName(temptime->tm_zone);
00532     #else
00533     localtime(&curtime); // Only doing this to set timezone as a side effect
00534     appCore->setTimeZoneBias(-timezone);
00535     appCore->setTimeZoneName(tzname[daylight?0:1]);
00536     #endif
00537 
00538         if (startfile == 1) {
00539                 if (argv[argc - 1][0] == '-') {
00540                         cout << "Missing Filename.\n";
00541                         return 1;
00542                 }
00543         
00544                 cout << "*** Using CEL File: " << argv[argc - 1] << endl;
00545                 appCore->runScript(argv[argc - 1]);
00546         }
00547 
00548     ready = true;
00549     glutMainLoop();
00550 
00551     return 0;
00552 }

static void MouseButton int  button,
int  state,
int  x,
int  y
[static]
 

Definition at line 110 of file glutmain.cpp.

References appCore, lastX, lastY, leftButton, middleButton, MOUSE_WHEEL_DOWN, MOUSE_WHEEL_UP, CelestiaCore::mouseButtonDown(), CelestiaCore::mouseButtonUp(), CelestiaCore::mouseWheel(), and rightButton.

Referenced by main().

00111 {
00112 #ifdef MACOSX
00113     if (button == GLUT_LEFT_BUTTON) {
00114         UInt32 mods=GetCurrentKeyModifiers ();
00115         if      (mods & optionKey)  button=GLUT_MIDDLE_BUTTON;
00116         else if (mods & cmdKey)     button=GLUT_RIGHT_BUTTON;
00117     }
00118 #endif /* MACOSX */
00119     // On Linux, mouse wheel up and down are usually translated into
00120     // mouse button 4 and 5 down events.
00121     if (button == MOUSE_WHEEL_UP)
00122     {
00123         appCore->mouseWheel(-1.0f, 0);
00124     }
00125     else if (button == MOUSE_WHEEL_DOWN)
00126     {
00127         appCore->mouseWheel(1.0f, 0);
00128     }
00129     else if (button == GLUT_LEFT_BUTTON)
00130     {
00131         leftButton = (state == GLUT_DOWN);
00132         if (state == GLUT_DOWN)
00133             appCore->mouseButtonDown(x, y, CelestiaCore::LeftButton);
00134         else
00135             appCore->mouseButtonUp(x, y, CelestiaCore::LeftButton);
00136     }
00137     else if (button == GLUT_RIGHT_BUTTON)
00138     {
00139         rightButton = (state == GLUT_DOWN);
00140         if (state == GLUT_DOWN)
00141             appCore->mouseButtonDown(x, y, CelestiaCore::RightButton);
00142         else
00143             appCore->mouseButtonUp(x, y, CelestiaCore::RightButton);
00144     }
00145     else if (button == GLUT_MIDDLE_BUTTON)
00146     {
00147         middleButton = (state == GLUT_DOWN);
00148         if (state == GLUT_DOWN)
00149             appCore->mouseButtonDown(x, y, CelestiaCore::MiddleButton);
00150         else
00151             appCore->mouseButtonUp(x, y, CelestiaCore::MiddleButton);
00152     }
00153 
00154     lastX = x;
00155     lastY = y;
00156 }

static void MouseDrag int  x,
int  y
[static]
 

Definition at line 94 of file glutmain.cpp.

References appCore, lastX, lastY, leftButton, middleButton, CelestiaCore::mouseMove(), and rightButton.

Referenced by main().

00095 {
00096     int buttons = 0;
00097     if (leftButton)
00098         buttons |= CelestiaCore::LeftButton;
00099     if (rightButton)
00100         buttons |= CelestiaCore::RightButton;
00101     if (middleButton)
00102         buttons |= CelestiaCore::MiddleButton;
00103 
00104     appCore->mouseMove(x - lastX, y - lastY, buttons);
00105 
00106     lastX = x;
00107     lastY = y;
00108 }

static void Resize int  w,
int  h
[static]
 

Definition at line 65 of file glutmain.cpp.

References appCore, and CelestiaCore::resize().

Referenced by main().

00066 {
00067     appCore->resize(w, h);
00068 }

static void SpecialKeyPress int  key,
int  x,
int  y
[static]
 

Definition at line 240 of file glutmain.cpp.

References HandleSpecialKey().

Referenced by main().

00241 {
00242     HandleSpecialKey(key, true);
00243 }

static void SpecialKeyUp int  key,
int  x,
int  y
[static]
 

Definition at line 246 of file glutmain.cpp.

References HandleSpecialKey().

Referenced by main().

00247 {
00248     HandleSpecialKey(key, false);
00249 }


Variable Documentation

CelestiaCore* appCore = NULL [static]
 

Definition at line 44 of file glutmain.cpp.

Referenced by Display(), HandleSpecialKey(), Idle(), KeyPress(), KeyUp(), main(), MouseButton(), MouseDrag(), and Resize().

char AppName[] = "Celestia"
 

Definition at line 42 of file glutmain.cpp.

Referenced by CreateOpenGLWindow(), DestroyOpenGLWindow(), and WinMain().

int lastX = 0 [static]
 

Definition at line 51 of file glutmain.cpp.

Referenced by MouseButton(), and MouseDrag().

int lastY = 0 [static]
 

Definition at line 52 of file glutmain.cpp.

Referenced by MouseButton(), and MouseDrag().

bool leftButton = false [static]
 

Definition at line 53 of file glutmain.cpp.

Referenced by MouseButton(), and MouseDrag().

int mainWindow = 1 [static]
 

Definition at line 57 of file glutmain.cpp.

Referenced by Idle(), and main().

bool middleButton = false [static]
 

Definition at line 54 of file glutmain.cpp.

Referenced by MouseButton(), and MouseDrag().

bool ready = false [static]
 

Definition at line 47 of file glutmain.cpp.

Referenced by Display(), and main().

bool rightButton = false [static]
 

Definition at line 55 of file glutmain.cpp.

Referenced by MouseButton(), and MouseDrag().


Generated on Sat Jan 14 22:31:45 2006 for Celestia by  doxygen 1.4.1