static void Main(string[] args) { bool bRunning = true; bool bActive = true; bool bCapMouse = true; bool bFullScreen = false; IntPtr sdlSurface = IntPtr.Zero; int SDLVideoFlags = 0; int SDLVideoFlagsFull = 0; bool[] Keys = new bool[500]; bool[] MouseKeys = new bool[8]; int X = 0, Y = 0; int width = 800, height = 600; Console.WriteLine("Setting up SDL and Devices Surface!"); if (Sdl.SDL_Init(Sdl.SDL_INIT_VIDEO) < 0) { Console.WriteLine("Unable to start SDL, SDL gave the following error!"); Console.WriteLine("{0}", Sdl.SDL_GetError()); Sdl.SDL_Quit(); } try { Sdl.SDL_VideoInfo info = (Sdl.SDL_VideoInfo)Marshal.PtrToStructure(Sdl.SDL_GetVideoInfo(), typeof(Sdl.SDL_VideoInfo)); SDLVideoFlags = Sdl.SDL_OPENGL | Sdl.SDL_GL_DOUBLEBUFFER | Sdl.SDL_HWPALETTE | Sdl.SDL_RESIZABLE; SDLVideoFlags |= (info.hw_available > 0 ? Sdl.SDL_HWSURFACE : Sdl.SDL_SWSURFACE); if (info.blit_hw > 0) SDLVideoFlags |= Sdl.SDL_HWACCEL; SDLVideoFlagsFull = SDLVideoFlags | Sdl.SDL_FULLSCREEN; Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_DOUBLEBUFFER, 1); sdlSurface = Sdl.SDL_SetVideoMode(width, height, 0, SDLVideoFlags); Sdl.SDL_WM_SetCaption("Untitled",null); if (sdlSurface == IntPtr.Zero) { Console.WriteLine("Unable to save surface pointer"); } } catch (NullReferenceException) { Console.WriteLine("Unable to get video info"); Console.WriteLine("{0}", Sdl.SDL_GetError()); Sdl.SDL_Quit(); } catch (Exception exc) { Console.WriteLine("An unknown error has occured while initializing SDL"); Console.WriteLine("{0}", Sdl.SDL_GetError()); Console.WriteLine(exc); Sdl.SDL_Quit(); } Sdl.SDL_Event sdlEvent; InitGL(); CGameEngine engine = new CGameEngine(); engine.InitEngine(); GameConsole console = new GameConsole("console.tga"); engine.Camera.AspectRatio = width / height; engine.Camera.ResizeCameraViewPort(width,height); int halfWidth = width >> 1; int halfHeight = height >> 1; GameConsole.width = width; GameConsole.height = height; Sdl.SDL_WM_GrabInput(Sdl.SDL_GRAB_ON); Sdl.SDL_ShowCursor(0); while (bRunning) { while(Sdl.SDL_PollEvent(out sdlEvent) == 1) { switch (sdlEvent.type) { case Sdl.SDL_ACTIVEEVENT: if (sdlEvent.active.gain == 0) { Sdl.SDL_ShowCursor(1); Sdl.SDL_WM_GrabInput(Sdl.SDL_GRAB_OFF); bCapMouse = false; } else { Sdl.SDL_ShowCursor(0); Sdl.SDL_WM_GrabInput(Sdl.SDL_GRAB_ON); bCapMouse = true; } break; case Sdl.SDL_VIDEORESIZE: sdlSurface = Sdl.SDL_SetVideoMode(sdlEvent.resize.w, sdlEvent.resize.h, 0, SDLVideoFlags); engine.Camera.AspectRatio = (float)sdlEvent.resize.w / (float)sdlEvent.resize.h; engine.Camera.ResizeCameraViewPort(sdlEvent.resize.w, sdlEvent.resize.h); halfWidth = width >> 1; halfHeight = height >> 1; break; case Sdl.SDL_KEYDOWN: if (!GameConsole.IsVisible) { Keys[sdlEvent.key.keysym.sym] = true; } else { int key = sdlEvent.key.keysym.sym; if (key == (int)Engine.Keys.K_BACKSPACE) { GameConsole.Backspace(); } else if (key == (int)Engine.Keys.K_RETURN) { GameConsole.AddLine(); } else if (key == (int)Engine.Keys.K_TAB) GameConsole.ToggleState(); else if (key < 256) { GameConsole.AddCharacterToEntryLine((char)sdlEvent.key.keysym.sym); } } if (sdlEvent.key.keysym.sym == Sdl.SDLK_ESCAPE) { bActive = bRunning = false; Sdl.SDL_Quit(); } if (sdlEvent.key.keysym.sym == Sdl.SDLK_F1) { bFullScreen = !bFullScreen; Sdl.SDL_FreeSurface(sdlSurface); sdlSurface = Sdl.SDL_SetVideoMode(width, height, 0, (bFullScreen ? SDLVideoFlagsFull : SDLVideoFlags)); engine.Camera.AspectRatio = width / height; engine.Camera.ResizeCameraViewPort(width, height); InitGL(); } if (Keys[(int)Engine.Keys.K_TAB]) GameConsole.ToggleState(); break; case Sdl.SDL_KEYUP: Keys[sdlEvent.key.keysym.sym] = false; break; case Sdl.SDL_MOUSEBUTTONDOWN: MouseKeys[sdlEvent.button.button] = true; break; case Sdl.SDL_MOUSEBUTTONUP: MouseKeys[sdlEvent.button.button] = false; break; case Sdl.SDL_QUIT: bActive = bRunning = false; Sdl.SDL_Quit(); break; } } if (bCapMouse) { Sdl.SDL_GetMouseState(out X, out Y); X = X - halfWidth; Y = Y - halfHeight; Sdl.SDL_WarpMouse((short)halfWidth, (short)halfHeight); } Sdl.SDL_Delay(1); // sleep and allow other threads to execute engine.Handle_Input(Keys, MouseKeys, -X, -Y); // handle Key and mouse events in Engine engine.GameMain(); // game main if (bActive) { engine.GameRender(); console.Render(); DefaultFont.Print(string.Format("{0} Frames in {1} Seconds = {2} AVG FPS : {3} Actual FPS", EngineTime.g_TotalFrames, (int)EngineTime.CurrentTime, (int)(EngineTime.g_TotalFrames/EngineTime.CurrentTime),(int)EngineTime.g_FramePerSecond),1,1.0f,10,height -15) ; Sdl.SDL_GL_SwapBuffers(); } } Sdl.SDL_Quit(); }
[Edited by - Rob Loach on March 3, 2006 10:54:44 AM]