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

bsp_loader.cpp

Go to the documentation of this file.
00001 #pragma warning(disable: 4786) 00002 00003 /*------------------------------------ Includes ---------------------------------------------*/ 00004 #include <fstream> 00005 #include <string> 00006 #include <iostream> 00007 00008 #include "bsp_loader.h" 00009 00010 00011 using namespace std; 00012 00013 00014 typedef enum { READING_AREA , READING_WALL , READING_UNDEF } READING_STATE; 00015 00016 00017 /*------------------------------------ Prototypes ---------------------------------------------*/ 00018 bool Get_Pair(int &x, int &y , ifstream& in); 00019 00020 00021 00022 00023 /*-----------------------------------------------------------------------------------------*/ 00024 // lit le texte "[x,y]" dans le stream 'in' et met a jour 'x' et 'y' avec les valeurs lues 00025 // return true s'il n'y a pas eu d'erreur, false sinon 00026 bool Get_Pair(int &x, int &y , ifstream& in) 00027 { 00028 char c; 00029 00030 in >> c; 00031 if( c != '[' ) 00032 return false; 00033 in >> x; 00034 in >> c; 00035 if( c != ',' ) 00036 return false; 00037 in >> y; 00038 in >> c; 00039 if( c != ']' ) 00040 return false; 00041 00042 return true; 00043 } 00044 00045 00046 00047 /****************************************************************************************************/ 00048 // class cBSP_Loader 00049 /****************************************************************************************************/ 00050 /*-----------------------------------------------------------------------------------------*/ 00051 bool cBSP_Loader::Read_Area(ifstream& in, cGame_World * world) 00052 { 00053 int x,y; 00054 if( !Get_Pair( x, y , in)) 00055 return false; 00056 00057 cGame_World::Ecran = cPositive_Rectangle( cPoint2D(0,0) , cPoint2D(x,y)); 00058 return true; 00059 } 00060 00061 00062 /*-----------------------------------------------------------------------------------------*/ 00063 bool cBSP_Loader::Read_Wall(ifstream& in , cGame_World * world) 00064 { 00065 char c; 00066 int x0,y0,x1,y1; 00067 00068 if( !Get_Pair( x0, y0 , in)) 00069 return false; 00070 00071 // on lit la fleche entre les deux points 00072 in >> c; 00073 if( c!='-' ) 00074 return false; 00075 in >> c; 00076 if( c!='>' ) 00077 return false; 00078 00079 if( !Get_Pair( x1, y1 , in)) 00080 return false; 00081 00082 world->My_BSP.Add_Divider( cHV_Wall(cPoint2D(x0,y0),cPoint2D(x1,y1) ) ); 00083 00084 00085 return true; 00086 } 00087 00088 00089 00090 /*-----------------------------------------------------------------------------------------*/ 00091 bool cBSP_Loader::Load(char *filename , cGame_World * world) 00092 { 00093 ifstream in; 00094 int n_line=0; 00095 char c; 00096 char line[100]; 00097 string str_line; 00098 READING_STATE state = READING_UNDEF; 00099 std::string err_message; 00100 00101 in.open( filename ); 00102 if( ! in.is_open() ) 00103 { 00104 cout << "error while opening file : " << filename << "\n"; 00105 err_message += "fichier "; 00106 err_message +=filename; 00107 err_message += " introuvable "; 00108 //Display_Fatal_Error( err_message.c_str() ); 00109 cout << "error loading \n"; 00110 00111 00112 return false; 00113 } 00114 00115 while( ! in.eof() ) 00116 { 00117 while( in.peek() == '\n' ) 00118 { 00119 in.ignore(1); 00120 n_line++; 00121 } 00122 if( in.eof() ) 00123 return true; 00124 00125 c= in.peek(); 00126 if( c == '<' ) // le caractere '<' en debut de ligne indique une balise, on change d'etat 00127 { 00128 in.getline(line,100); 00129 n_line++; 00130 str_line = string(line); 00131 if( str_line == "<AREA>") 00132 state = READING_AREA; 00133 else if( str_line == "<WALLS>") 00134 state = READING_WALL; 00135 else 00136 { 00137 state = READING_UNDEF; 00138 return false; 00139 } 00140 } 00141 00142 switch( state) // on appelle la fonction adequate selon l'etat de lecture 00143 { 00144 case READING_AREA: 00145 if( !Read_Area(in,world)) 00146 { 00147 Display_Error( n_line , filename); 00148 return false; 00149 } 00150 break; 00151 case READING_WALL: 00152 if( ! Read_Wall(in,world) ) 00153 { 00154 Display_Error( n_line , filename); 00155 return false; 00156 } 00157 break; 00158 default: 00159 in.getline(line,100); 00160 return false; 00161 } 00162 } 00163 00164 return true; 00165 } 00166 00167 00168 /*-----------------------------------------------------------------------------------------*/ 00169 void cBSP_Loader::Display_Error(int n_line , char * file) 00170 { 00171 // modifier selon le type d'affichage d'erreur que l'on desire 00172 // sortie text/box message... 00173 cout << "ERROR while reading file : " << file << " at line " << n_line << "\n"; 00174 }

Generated on Fri May 21 19:22:36 2004 for LIBELL by doxygen 1.3.7