Thursday 15 August 2013

c++ - Bugs while loading from text file -


I am currently creating an RTS game and I am loading the unit from txt files like this :

1 1 700 200 10

/ unit type / / player / / x co-normal / / y co-normal / / health /

I am using a settler (only a unit currently in the unit) to keep all the units (not the indicator)

When I load the units and I want to load them I try to drag on the screen so they are not there yet are you there. I did some tests and it appears that even when I load with TXT file, vector "settlers" is empty

code of main.cpp:

  Vector & lt; Settler & gt; Settle down; ... zero load_units (string filename) {settlers.clear (); Ifstream unit_file (filename.c_str ()); String line; Of vector & lt; Vector & lt; Integer & gt; & Gt; Ww; While (unit_file.eof ()) {while (getline (unit_file, line)) {stringstream ss (line); Int i; Of vector & lt; Int & gt; W; While (SS & gt; & gt; i) {w.push_back (i); } Ww.push_back (w); }} For (int i = 0; i & lt; ww.size (); i ++) {int type = ww [i] [0]; Integer player = ww [i] [1]; Int x = ww [i] [2]; Int y = ww [i] [3]; Intel HP = ww [i] [4]; If (type == 1) // inhabitant {the settler dependent (x, y, hp, player); Settlers.push_back (settler); }} Unit_file.close ();} ... zero init () {SDL_Init (SDL_INIT_EVERYTHING); TTF_Init (); MixOn Audio (22050, MIX_DEFAULT_FORMAT, 2, 4096); SDL_WM_SetIcon (IMG_Load ("icon.png"), NULL); Screen = SDL_SetVideoMode (1600, 900, 32, SDL_FULLSCREEN); Map = IMG_Load ("tlo.png"); Bar = IMG_Load ("bar.png"); Pause_menu = IMG_Load ("menu.png"); Save_game_menu = IMG_Load ("save_game_menu.png"); Save_prompt = IMG_Load ("saved_prompt.png"); Load_game_menu = IMG_Load ("load_game_menu.png"); Load_prompt = IMG_Load ("loaded_prompt.png"); Intro_control = true; Menu = true; Current = false; Paused = false; Saving = wrong; Loading = false; MapX = 0; Map Y = 0; Xoffet = 0; Yoffset = 0; Load_map ("mapa1.txt"); Load_units ("units1.txt"); {String file_frame; for {int i = 0; i & lt; 60; i ++) Stringstream ss; SS & lt; & Lt; I + 1; If (i & lt; 9) {file_frame = ("Introduction / 000"); } And {file_frame = ("Introduction / 00"); } File_frame.append (ss.str ()); File_frame.append (".png"); Introduction [i] = IMG_Load (file_frame.c_str ()); }}   

...

  int main (int argc, char * args []) {int frames = 0; Timer fps; in this(); Player p1 (0, player_1_core, "player1"); Int frame_control = 0; While (intro_control) {  

.....

PS: I suspect that "while the unit_file (.)) May be wrong with { " Statement. But this is just an estimate

There are some problems with your code:

    < Li> Loop loops are launched as long as AOF is set (you should only loop until AOF is set)
  • Even if you fix the loop , It will still be wrong because eof is set only after you have read beyond it, you will have an invalid entry
  • Why a line first Rapt, just put him in a string stream and then parse it? You can simply use the file object's streaming operators directly.

    I think the following would be a cleaner solution to your problem:

    First of all, the Setler structure:

      struct Settler {int type; // Maybe some of them should be unsigned, // but I left them as if you were already a player; Int x; Int y; full health; };   

    Then you & gt; & Gt; To allow overloading to be streamed from any input stream, the operator for its class takes the stream to read it as the first parameter and object where the result should be known as the second parameter:

      std :: istream & amp; Operator & gt; & Gt; (Std :: istream in & amp;; setler & amp; settler) {// just in the corresponding field of Setler & gt; & Gt; Settler.type & gt; & Gt; Settler.player & gt; & Gt; Settler.x & gt; & Gt; Settler.y & gt; & Gt; Settler.health; }   

    Then you can read people who are settled using a simple file:

      int main () {std :: Ifstream file ("test .text"); Turn satellor; Std :: vector & lt; Settler & gt; Settle down; While (File> ON / OFF) {// read as long as possible // PassBack (current) that settled in a Settleer; }; }   

    An example of a work (which loads for good output & lt; & lt; though a real program may load in & lt; & lt; the same format To read & gt; & gt;)

No comments:

Post a Comment