Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

o_wnd.cpp

Go to the documentation of this file.
00001 // OMICRON ENGINE HEADER FILE
00002 //
00003 // --------------------------------------------------------------------------
00004 // Copyright (C) 2001-2002 by Bjoern Paetzel <kolrabi@gmx.de>
00005 //
00006 // This file is part of the Omicron Engine.
00007 //
00008 // The Omicron Engine is free software; you can redistribute it and/or modify
00009 // it under the terms of the  GNU General Public License  as published by the
00010 // Free Software Foundation;  either version  2  of the License,  or (at your
00011 // option) any later version.
00012 //
00013 // The Omicron Engine  is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00015 // or  FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU General Public License
00016 // for more details.
00017 //
00018 // You should have  received a copy of the  GNU General Public License  along
00019 // with The Omicron Engine;  if not,  write to the  Free Software Foundation,
00020 // Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
00021 //
00022 // --------------------------------------------------------------------------
00023 // Last modified:       $Date: 2002/12/14 20:02:33 $
00024 // By           :       $Author: kolrabi $
00025 // $Id: o_wnd.cpp,v 1.5 2002/12/14 20:02:33 kolrabi Exp $ 
00026 
00027 /*
00028 
00029   $Log: o_wnd.cpp,v $
00030   Revision 1.5  2002/12/14 20:02:33  kolrabi
00031   fixed some memory leaks
00032 
00033   Revision 1.4  2002/12/13 15:59:47  kolrabi
00034   several bug fixes
00035 
00036   Revision 1.3  2002/12/08 20:08:15  kolrabi
00037   Now really runs under X11
00038 
00039   Revision 1.2  2002/12/08 11:04:10  kolrabi
00040   Setting Window title correctly
00041 
00042   Revision 1.1.1.1  2002/12/07 19:02:06  kolrabi
00043   initial release
00044 
00045 
00046 */
00047 
00052 #include            "omicron/internal.h" 
00053 #include            "omicron/render.h"
00054 #include            "omicron/gui.h" 
00055 #include            "omicron/file.h"
00056 #include            "omicron/net.h"
00057 #include            "omicron/sound.h"
00058 
00059 /**************************************************************************** 
00060  **************************************************************************** 
00061  * WINDOW VARIABKES ********************************************************* 
00062  **************************************************************************** 
00063  ****************************************************************************/ 
00064  
00065 #if defined(WIN32) 
00066 static WNDCLASS wcWindow;           // the window class 
00067 #endif 
00068  
00069 static ulong    end,        // timer values 
00070                 start, 
00071                 lastend; 
00072  
00073 /**************************************************************************** 
00074  **************************************************************************** 
00075  * WINDOW ROUTINES ********************************************************** 
00076  **************************************************************************** 
00077  ****************************************************************************/ 
00078  
00079  
00080  
00081 #if defined(WIN32) 
00082 /**************************************************************************** 
00083  * _win_wndproc                                         window message proc * 
00084  ****************************************************************************/ 
00085 LRESULT CALLBACK _win_wndproc 
00086 ( 
00087     HWND        hwnd, 
00088     UINT        uMsg, 
00089     WPARAM      wParam, 
00090     LPARAM      lParam 
00091 ) 
00092 { 
00093 //    STARTPROFILE("wndproc");
00094     switch ( uMsg ) 
00095     { 
00096         case    WM_CLOSE: 
00097             PostQuitMessage(0); 
00098             gv.bExit = 1; 
00099 //            ENDPROFILE("wndproc");
00100             return 1; 
00101  
00102         case    WM_SIZE: 
00103             { 
00104                 RECT r; 
00105  
00106                 GetClientRect((HWND)gv.init.wnd, &r); 
00107                 _gfx_resize( (ushort)r.right, (ushort)r.bottom, 0 ); 
00108                 gv.init.hres = (ushort)r.right;
00109                 gv.init.vres = (ushort)r.bottom;
00110                 //_r_render_scene(); 
00111             } 
00112             break; 
00113 /*
00114         case WM_LBUTTONDOWN:
00115             gv.mouse_b |= 1;
00116             break;
00117 
00118         case WM_RBUTTONDOWN:
00119             gv.mouse_b |= 2;
00120             break;
00121  
00122         case WM_LBUTTONUP:
00123             gv.mouse_b &= ~1;
00124             break;
00125 
00126         case WM_RBUTTONUP:
00127             gv.mouse_b &= ~2;
00128             break;*/
00129 
00130         case WM_ACTIVATE:
00131             gv.active = !!LOWORD(wParam);
00132 //            ENDPROFILE("wndproc");
00133             return 0; 
00134     } 
00135 //    ENDPROFILE("wndproc");
00136     return DefWindowProc(hwnd, uMsg, wParam, lParam); 
00137 } 
00138 #else
00139 
00140 void _win_process_event(XEvent &event)
00141 {
00142     switch (event.type)
00143     {
00144         case ConfigureNotify:
00145 
00146         if ((event.xconfigure.width != gv.init.hres) || 
00147             (event.xconfigure.height != gv.init.vres))
00148           {
00149               gv.init.hres = event.xconfigure.width;
00150               gv.init.vres = event.xconfigure.height;
00151               _gfx_resize(event.xconfigure.width,
00152                           event.xconfigure.height, gv.init.fullscreen);
00153           }
00154         break;
00155     }
00156 }
00157 
00158 #endif 
00159  
00160  
00161 /**************************************************************************** 
00162  * _win_init                                          windows initialization * 
00163  ****************************************************************************/ 
00164 #if defined(WIN32) 
00165 HWND            _win_init 
00166 ( 
00167     ulong       nWidth, 
00168     ulong       nHeight, 
00169     HINSTANCE   hInstance, 
00170     bool        bFullscreen ) 
00171 #else 
00172 ulong           _win_init 
00173 ( 
00174     ulong       nWidth, 
00175     ulong       nHeight, 
00176     bool        bFullscreen ) 
00177 #endif 
00178 { 
00179     _log_printf(MSG_DEBUG, "- _win_init() - -"); 
00180  
00181 #if defined(WIN32) 
00182     ZeroMemory(&wcWindow, sizeof(WNDCLASS)); 
00183  
00184     wcWindow.hInstance      = hInstance; 
00185     wcWindow.style          = CS_VREDRAW|CS_HREDRAW|CS_OWNDC; 
00186     wcWindow.lpfnWndProc    = _win_wndproc; 
00187     wcWindow.lpszClassName  = gv.init.wndclassname; 
00188     wcWindow.hbrBackground  = NULL; //CreateSolidBrush(RGB(0,0,0)); 
00189  
00190     if (!RegisterClass(&wcWindow)) 
00191         return 0; 
00192  
00193     if (!bFullscreen) 
00194     { 
00195         // windowed mode
00196         gv.init.wnd =  (ulong)CreateWindow( 
00197             gv.init.wndclassname, 
00198             gv.init.wndtitle, 
00199             WS_OVERLAPPEDWINDOW, 
00200             0,0, nWidth, nHeight, 
00201             NULL, NULL, 
00202             hInstance, NULL 
00203         );
00204 
00205         if (!gv.init.wnd)
00206         {
00207             UnregisterClass(gv.init.wndclassname, hInstance);
00208             return 0;
00209         }
00210 
00211         RECT r; 
00212 
00213         GetClientRect((HWND)gv.init.wnd, &r); 
00214         AdjustWindowRect(&r, WS_OVERLAPPEDWINDOW, false); 
00215 
00216         MoveWindow((HWND)gv.init.wnd, 0, 0, r.right-r.left, r.bottom-r.top, false); 
00217     } 
00218     else 
00219     { 
00220         // fullscreen mode
00221         gv.init.wnd =  (ulong)CreateWindow( 
00222             gv.init.wndclassname, 
00223             gv.init.wndtitle, 
00224             WS_POPUP, 
00225             0,0, nWidth, nHeight, 
00226             NULL, NULL, 
00227             hInstance, NULL 
00228         ); 
00229 
00230         if (!gv.init.wnd)
00231         {
00232             UnregisterClass(gv.init.wndclassname, hInstance);
00233             return 0;
00234         }
00235 
00236         // FIXME:
00237         // somehow gv.init.*res get changed here, i the heck dont know why
00238 
00239         gv.init.hres = (ushort)nWidth;
00240         gv.init.vres = (ushort)nHeight;
00241     } 
00242 
00243     ShowCursor(false); 
00244 
00245     return (HWND)gv.init.wnd; 
00246  
00247 #else 
00248  
00249     gv.init.dpy = XOpenDisplay(NULL); 
00250     gv.init.vis = DefaultVisual(gv.init.dpy,0); 
00251  
00252     // XVisualInfo *vi; 
00253  
00254     if (!gv.init.nogfx) 
00255     { 
00256         int attribsdbl[] =  
00257         { 
00258             GLX_RGBA, GLX_DOUBLEBUFFER, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1, None 
00259         }; 
00260         int attribssgl[] =  
00261         { 
00262             GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1, None 
00263         }; 
00264  
00265         gv.init.vi = glXChooseVisual(gv.init.dpy, DefaultScreen(gv.init.dpy), attribsdbl); 
00266  
00267         if (!gv.init.vi) 
00268         { 
00269             gv.init.vi = glXChooseVisual(gv.init.dpy, DefaultScreen(gv.init.dpy), attribssgl); 
00270  
00271             if (!gv.init.vi) 
00272             return 0; 
00273  
00274         } 
00275         gv.init.vis = gv.init.vi->visual; 
00276     } 
00277  
00278     if (bFullscreen) 
00279     { 
00280     } 
00281  
00282     gv.init.wnd = XCreateSimpleWindow(gv.init.dpy, DefaultRootWindow(gv.init.dpy),
00283         0,0, nWidth, nHeight,
00284         1, 0, 0);
00285 
00286  
00287     if (!gv.init.nogfx) 
00288     { 
00289         gv.glxc = glXCreateContext(gv.init.dpy, gv.init.vi, 0, GL_TRUE); 
00290         glXMakeCurrent(gv.init.dpy, gv.init.wnd, gv.glxc); 
00291     } 
00292  
00293     XStoreName(gv.init.dpy, gv.init.wnd, gv.init.wndtitle); 
00294 
00295     return gv.init.wnd; 
00296 #endif 
00297 } 
00298  
00299  
00300  
00301 /**************************************************************************** 
00302  * _win_deinit                                      windows deinitialization * 
00303  ****************************************************************************/ 
00304 void            _win_deinit() 
00305 { 
00306     _log_printf(MSG_DEBUG, "- _win_deinit() ------------------------"); 
00307  
00308 #if defined(WIN32) 
00309     _log_printf(MSG_DEBUG, "destroying window.."); 
00310     DestroyWindow((HWND)gv.init.wnd); 
00311  
00312     _log_printf(MSG_DEBUG, "unregistering window.."); 
00313     UnregisterClass(gv.init.wndclassname, gv.init.hInstance); 
00314 #else
00315 
00316     // free vi
00317     XFree(gv.init.vi);
00318 
00319 #endif 
00320  
00321     _log_printf(MSG_DEBUG, "----------------------------------------"); 
00322 } 
00323  
00324 
00325 /**************************************************************************** 
00326  * _win_mainloop                                          windows main loop * 
00327  ****************************************************************************/ 
00328 ulong           _win_mainloop() 
00329 {
00330 #if defined(WIN32)
00331 
00332     MSG msg;
00333 
00334 #else
00335 
00336     XEvent event;
00337 
00338     XSelectInput(gv.init.dpy, gv.init.wnd, StructureNotifyMask);
00339 
00340 #endif
00341 
00342     lastend = end = start = _time_get(); 
00343    
00344     while (!gv.bExit) 
00345     { 
00346 //        STARTPROFILE("frame");
00347 
00348         end = _time_get(); 
00349 #if defined(WIN32)
00350         while(PeekMessage(&msg, (HWND)gv.init.wnd, 0, 0, PM_REMOVE))
00351         {
00352             TranslateMessage(&msg); 
00353             DispatchMessage(&msg); 
00354         }
00355 #else
00356         while (XPending(gv.init.dpy)>0)
00357         {
00358             XNextEvent(gv.init.dpy, &event);
00359             _win_process_event(event);
00360         }
00361 #endif
00362  
00363         long dif = end-start; 
00364  
00365         gv.dtime    = (dif * 0.001f)*gv.timescale-gv.time; 
00366         gv.time     = dif * 0.001f * gv.timescale; 
00367 
00368         if (gv.fileman)
00369             gv.fileman->update(); 
00370 
00371         if (gv.init.noinput == false) 
00372             _input_update(); 
00373 
00374         if (gv.gui)
00375             gv.gui->update();
00376 
00377 #if !defined(NOSOUND)
00378         if (gv.init.nosound == false) 
00379             gv.soundman->update(); 
00380 #endif
00381 
00382         if (gv.netman)
00383             gv.netman->update();
00384 
00385         if (gv.iface)
00386             gv.iface->world_update();
00387 
00388         if (gv.renderer)
00389             gv.renderer->render(); 
00390 
00391         if (gv.iface)
00392             gv.iface->afterframe(); 
00393     } 
00394 
00395     return 0;
00396 } 

Generated on Wed Dec 18 15:48:47 2002 for omicron engine by doxygen1.2.18