src/Map.cpp

Go to the documentation of this file.
00001 /* Map Class Code
00002  */
00003 
00004 #include <guichan.hpp>
00005 #include "StateSailing.h"
00006 #include <cstdlib>
00007 #include "SDL.h"
00008 #include "GLee.h"
00009 #include "Log.h"
00010 #include "Config.h"
00011 #include "Texture.h"
00012 #include "font.h"
00013 #include "IGameState.h"
00014 #include "Ship.h"
00015 #include "Terrain.h"
00016 #include "CityManager.h"
00017 #include "City.h"
00018 #include "Menu/CCGui.h"
00019 #include "Menu/CCFont.h"
00020 #include "Menu/CCLabel.h"
00021 #include "Map.h"
00022 
00023 const string Map::s_mainFontName = "smallFont.png";
00024 
00025 Map::Map(StateSailing *curGame, Ship *playerShip, Terrain *terrain):IGameState(curGame) {
00026   m_winWidth = Config::s_config->GetWinWidth();
00027   m_winHeight = Config::s_config->GetWinHeight();
00028 
00029   m_terrainWidth = terrain->GetWidth();
00030   m_terrainHeight = terrain->GetHeight();
00031 
00032   // Set the pointer to the main sailing state to null
00033   m_mainGame = curGame;
00034 
00035   m_playerShip = playerShip;
00036 
00037   // Load the background
00038   glEnable(GL_TEXTURE_2D);
00039   m_background = new Texture("mapscreen.png");
00040   m_background->BindTexture();
00041 
00042   m_curMousePosX = 0;
00043   m_curMousePosY = 0;
00044 
00045   m_transport = false;
00046 
00047   InitMenu();
00048 }
00049 
00050 Map::~Map() {
00051   gcn::Widget* tempWidget;
00052   unsigned int numWidgets;
00053 
00054   delete m_background;
00055 
00056   numWidgets = m_guichanWidgets.size();
00057 
00058   for (unsigned int i = 0; i < numWidgets; i++) {
00059     tempWidget = m_guichanWidgets[i];
00060     m_guichanWidgets[i] = NULL;
00061     delete tempWidget;
00062   }
00063   m_guichanWidgets.clear();
00064 
00065   delete m_shipIcon;
00066 
00067   delete m_cityIconImage;
00068   delete m_shipIconImage;
00069 }
00070 
00071 void Map::Update(const unsigned int ticks) {
00072   float screenX, screenY;
00073 
00074   // Process incoming events
00075   CheckInput();
00076 
00077   screenX = TerrainToScreenX((float)m_playerShip->m_x);
00078   screenY = TerrainToScreenZ((float)m_playerShip->m_z);
00079   m_shipIcon->setX((int) screenX - (m_shipIconImage->getWidth() / 2));
00080   m_shipIcon->setY((int) screenY - (m_shipIconImage->getHeight() / 2));
00081 }
00082 
00083 void Map::Display() {
00084   glLoadIdentity();
00085 
00086   // Draw the background
00087   DisplayBackground(m_background->GetTexture(), .6);
00088 }
00089 
00090 
00091 void Map::CheckInput() {
00092   /* Our SDL event placeholder. */
00093   SDL_Event event;
00094 
00095   HandleMouseMove();
00096   /* Grab all the events off the queue. */
00097   while( SDL_PollEvent( &event ) ) {
00098     switch( event.type ) {
00099     case SDL_KEYDOWN:
00100       /* Handle key presses. */
00101       HandleKeyDown( &event.key.keysym );
00102       break;
00103     case SDL_KEYUP:
00104       HandleKeyUp(&event.key.keysym);
00105       break;
00106     case SDL_QUIT:
00107       /* Handle quit requests (like Ctrl-c). */
00108       DoExit();
00109       break;
00110     }
00111   }
00112 }
00113 
00114 void Map::HandleMouseMove() {
00115   Uint8 buttons;
00116 
00117   buttons = SDL_GetMouseState(&m_curMousePosX, &m_curMousePosY);
00118 
00119   if ((buttons == SDL_BUTTON(1)) && m_transport) {
00120     m_playerShip->m_x = ScreenToTerrainX((float)m_curMousePosX);
00121     m_playerShip->m_z = ScreenToTerrainZ((float)m_curMousePosY);
00122   }
00123 }
00124 
00125 void Map::HandleKeyUp(SDL_keysym* keysym) {
00126   switch(keysym->sym) {
00127   case SDLK_ESCAPE:
00128     // Go back to the game, if it is loaded
00129     if (m_previous != NULL) {
00130       PrepareStateSwitch(m_previous, this);
00131     }
00132     break;
00133   case SDLK_m:
00134     // Go back to the game, if it is loaded
00135     if (m_previous != NULL) {
00136       PrepareStateSwitch(m_previous, this);
00137     }
00138     break;
00139   case SDLK_t:
00140     // Stop the transport function
00141     m_transport = false;
00142     break;
00143 
00144   default:
00145     break;
00146   }
00147 }
00148 
00149 void Map::HandleKeyDown(SDL_keysym* keysym) {
00150   switch( keysym->sym ) {
00151   case SDLK_z:
00152     TakeScreenshot();
00153     break;
00154   case SDLK_t:
00155     m_transport = true;
00156     break;
00157   default:
00158     break;
00159   }
00160 }
00161 
00162 void Map::SwitchTo(IGameState *oldState) {
00163   m_transport = false;
00164   EnterTextMode(Config::s_config->GetWinWidth(), Config::s_config->GetWinHeight());
00165 }
00166 
00167 void Map::SwitchFrom() {
00168 }
00169 
00170 void Map::DrawGui() {
00171   glPushMatrix();
00172   glLoadIdentity();
00173   m_background->BindTexture();
00174 
00175   glColor3f(0, 0, 0);
00176 
00177   //Draw quad for map
00178   glBegin(GL_QUADS);
00179   glTexCoord2f(0,0);
00180   glVertex3f(10, m_winHeight - 250, 0);
00181   glTexCoord2f(0, 1);
00182   glVertex3f(10, m_winHeight - 10, 0);
00183   glTexCoord2f(1, 1);
00184   glVertex3f(370, m_winHeight - 10, 0);
00185   glTexCoord2f(1, 0);
00186   glVertex3f(370, m_winHeight - 250, 0);
00187   glEnd();
00188 
00189   glColor3f(1, 1, 1);
00190   glPopMatrix();
00191 }
00192 
00193 void Map::InitMenu() {
00194   gcn::Widget* tempWidget;
00195   unsigned int i, numCities;
00196   float screenX, screenY;
00197 
00198   m_cityIconImage = new gcn::Image("data/Textures/cityicon.png");
00199   m_shipIconImage = new gcn::Image("data/Textures/shipicon.png");
00200 
00201   numCities = m_mainGame->m_cities->NumberCities();
00202 
00203   m_guichanWidgets.reserve(numCities * 2);
00204 
00205   for (i = 0; i < numCities; i++) {
00206     screenX = TerrainToScreenX((float)m_mainGame->m_cities->GetCity(i)->GetLocX());
00207     screenY = TerrainToScreenZ((float)m_mainGame->m_cities->GetCity(i)->GetLocZ());
00208 
00209     tempWidget = new gcn::Icon(m_cityIconImage);
00210     m_guichanWidgets.push_back(tempWidget);
00211     m_top->add(tempWidget, (int) screenX - (m_cityIconImage->getWidth() / 2), (int) screenY - (m_cityIconImage->getHeight() / 2));
00212 
00213     tempWidget = new CCLabel(m_mainGame->m_cities->GetCity(i)->getName(), s_mainFontName);
00214     m_guichanWidgets.push_back(tempWidget);
00215     m_top->add(tempWidget, (int) screenX, (int) screenY);
00216   }
00217 
00218   screenX = TerrainToScreenX((float)m_playerShip->m_x);
00219   screenY = TerrainToScreenZ((float)m_playerShip->m_z);
00220 
00221   m_shipIcon = new gcn::Icon(m_shipIconImage);
00222   m_top->add(m_shipIcon, (int) screenX - (m_shipIconImage->getWidth() / 2), (int) screenY - (m_shipIconImage->getHeight() / 2));
00223 }
00224 
00225 float Map::TerrainToScreenX(float x) {
00226   return m_winWidth * (x / m_terrainWidth + 0.5);
00227 }
00228 
00229 float Map::TerrainToScreenZ(float z) {
00230   return m_winHeight * (z / m_terrainHeight + 0.5);
00231 }
00232 
00233 float Map::ScreenToTerrainX(float x) {
00234   return m_terrainWidth * ((float)x/m_winWidth - 0.5);
00235 }
00236 
00237 float Map::ScreenToTerrainZ(float z) {
00238   return m_terrainHeight * ((float)z/m_winHeight - 0.5);
00239 }

Generated on Mon Jan 8 22:34:12 2007 for CrownandCutlass by  doxygen 1.4.7