startup.cs

   1:  using System;
   2:  using System.Drawing;
   3:  using System.Collections.Generic;
   4:  using System.ComponentModel;
   5:  using System.Windows.Forms;
   6:  using System.Data;
   7:  using Tao.Sdl;
   8:  using Tao.OpenGl;
   9:  using System.Reflection;
  10:  using System.Xml;
  11:  using System.IO;
  12:  using System.Threading;
  13:  using System.Runtime.InteropServices;
  14:   
  15:  [assembly: AssemblyVersion("1.0.0.1")]
  16:  #if (DEBUG)
  17:  [assembly: AssemblyDescription("Debug Version")]
  18:  #else
  19:  [assembly: AssemblyDescription("Release Version")]
  20:  #endif
  21:   
  22:  public class Game
  23:  {
  24:      public static TerraAegrus ta;
  25:      [STAThread]
  26:      public static void Main(string[] args)
  27:      {
  28:          // Modify the path so that the game can find the lib directory (for sdl.dll. 
  29:          // The other dll's can be found via config file):
  30:          System.Environment.SetEnvironmentVariable("PATH", System.Environment.GetEnvironmentVariable("PATH") 
  31:              + ";" + Path.Combine(Application.StartupPath, "lib"));
  32:          DateTime dt = DateTime.Now;
  33:          Log.WriteToLog("++++++++++++++++++++++++ Game Started At: " + dt.ToString() + 
  34:              " ++++++++++++++++++++++++");
  35:   
  36:          bool quitFlag = false;
  37:          try
  38:          {
  39:              TerraAegrus.m_SdlPointer = OnPaintHelper.InitSDL(false);
  40:   
  41:              OnPaintHelper.InitOpenGL();
  42:   
  43:              ta = new TerraAegrus();
  44:              // The very first time you load the game, there may be some monster's chasing you:
  45:              ta.PopulateObjectsToDrawOnWorldMap();
  46:   
  47:              // Create and start the threads to load the textures:
  48:              ta.m_ThreadLoading = new Thread(MyTexture.ForceLoadAllImagesOnly);
  49:              ta.m_ThreadLoading.IsBackground = false;
  50:              ta.m_ThreadLoading.Name = "Loading Thread";
  51:              ta.m_ThreadLoading.Start();
  52:   
  53:              Sdl.SDL_EnableUNICODE(Sdl.SDL_ENABLE);
  54:              if (Sdl.SDL_EnableUNICODE(Sdl.SDL_QUERY) != Sdl.SDL_ENABLE)
  55:              {
  56:                  Log.WriteToLog("***Error: Unicode failed to initialize! Keyboard input may not work.");
  57:              }
  58:              Sdl.SDL_Event evt;
  59:              while (quitFlag == false)
  60:              {
  61:                  while (Sdl.SDL_PollEvent(out evt) == 1)
  62:                  {
  63:                      switch (evt.type)
  64:                      {
  65:                          case Sdl.SDL_USEREVENT:
  66:                              TerraAegrus.m_Combat.InitCombat(Globals.g_GlobalMonsterPartyList[evt.user.code], 
  67:                                  false);
  68:                              break;
  69:                          case Sdl.SDL_KEYDOWN:
  70:                              switch (evt.key.keysym.sym)
  71:                              {
  72:                                  case Sdl.SDLK_UP:
  73:                                      ta.HandleKeyPress((int)Combat.Direction.UP);
  74:                                      break;
  75:                                  case Sdl.SDLK_DOWN:
  76:                                      ta.HandleKeyPress((int)Combat.Direction.DOWN);
  77:                                      break;
  78:                                  case Sdl.SDLK_LEFT:
  79:                                      ta.HandleKeyPress((int)Combat.Direction.LEFT);
  80:                                      break;
  81:                                  case Sdl.SDLK_RIGHT:
  82:                                      ta.HandleKeyPress((int)Combat.Direction.RIGHT);
  83:                                      break;
  84:                                  default:
  85:                                      ta.HandleKeyPress(evt.key.keysym.unicode);
  86:                                      break;
  87:                              }
  88:                              break;
  89:                          case Sdl.SDL_KEYUP:
  90:                              // We don't care what the value is, we just care of the event
  91:                              ta.HandleKeyUpEvent();
  92:                              break;
  93:                          case Sdl.SDL_QUIT:
  94:                              quitFlag = true;
  95:                              break;
  96:   
  97:                          case Sdl.SDL_MOUSEMOTION:
  98:                              ta.MouseMove(evt.motion.x, evt.motion.y);
  99:                              break;
 100:                          case Sdl.SDL_MOUSEBUTTONDOWN:
 101:                              if (evt.button.button == Sdl.SDL_BUTTON_LEFT)
 102:                              {
 103:                                  ta.HandleLeftMouseDown(evt.button.x, evt.button.y);
 104:                              }
 105:                              break;
 106:                          case Sdl.SDL_MOUSEBUTTONUP:
 107:                              if (evt.button.button == Sdl.SDL_BUTTON_LEFT)
 108:                              {
 109:                                  ta.HandleLeftMouseUp(evt.button.x, evt.button.y);
 110:                              }
 111:                              if (evt.button.button == Sdl.SDL_BUTTON_RIGHT)
 112:                              {
 113:                                  ta.HandleRightMouseUp(evt.button.x, evt.button.y);
 114:                              }
 115:                              break;
 116:                      }
 117:                  }
 118:                  try
 119:                  {
 120:                      ta.OnRender();
 121:                  }
 122:                  catch (Exception ex)
 123:                  {
 124:                      Log.WriteToLog("***Error: " + ex.ToString());
 125:                  }
 126:              }
 127:          }
 128:          catch (Exception ex)
 129:          {
 130:              Log.WriteToLog("***Error: " + ex.ToString());
 131:              Sdl.SDL_Quit();
 132:              throw;
 133:          }
 134:          finally
 135:          {
 136:              Log.WriteToLog("================= PATH ENVIRONMENT VARIABLE =====================");
 137:              foreach (string aPath in System.Environment.GetEnvironmentVariable("PATH").Split(new Char[] { ';' }))
 138:              {
 139:                  Log.WriteToLog(aPath);
 140:              }
 141:              Log.WriteToLog("===================== GAME IS EXITING ===========================");
 142:              Environment.Exit(-1);
 143:              Sdl.SDL_Quit();
 144:          }
 145:      }
 146:  }