GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: No Line Numbers


vector problem by thejoshinator
1
 
int CLevel::Load_Map(char* filename)
{
    // This function loads a map from a text file

    string word = "", // placeholder for strings
    bitmap_file = ""; // will hold the name of the bitmap file to load
    int num_tiles;      // the number of tiles per layer
    int index = 0;      // looping variable

    // bob info
    int width,                // from file
        height,                // from file
        max_frames_per_row, // from file
        num_frames,            // from file
        anim_speed,            // from file
        row,                // from file
        x,y,                // from file
        bob_attr = 0;        // init to 0 so we don't have junk. Will be determined

    // create the stream
    ifstream fin;

    // make sure we opened it
    if (fin.fail())
        return 0;

    // alright now we need to read in all the info
    // first the starting tile to draw
    fin >> word >> this->map.starting_tile;

    // now we need the width of the entire map
    fin >> word >> this->map.width;

    // and the height
    fin >> word >> this->map.height;

    // now the width of the screen in tiles.
    fin >> word >> this->map.scrn_width;

    // read in the height in tiles
    fin >> word >> this->map.scrn_height;

    // read in the number of tiles per layer of this map
    fin >> word >> num_tiles;

    // loop till we reach the end of the file
    while (!fin.eof())
    {
        // read in next word. It will be a keyword
        fin >> word;
            
        // check which keyword it is
        if (word ==#t2#0--)
        {
            Open_Error_File#t2#1--, 0);
            Write_Error#t2#2--);
            Close_Error_File();
            // We read#t2#0-- so we need to point the cursor past the layer index
            fin >> word;

            // Now create another layer and push it back
            LAYER new_layer;
            this->map.layers.push_back(new_layer);
        } // end if
        else if (word ==#t2#3--)
        {
            // We read#t2#3-- so we need to get the filename
            fin >> word;

             while (word !=#t2#4--)
            {
                // append it to bitmap_file
                bitmap_file += word +#t2#5--;

                // get the next word
                fin >> word;
            } // end while

            // We have to close any bitmaps we have open before we can close another one
            Unload_Bitmap_File(&bitmap16bit);

            // Now we can finally we can open the new bitmap
            Load_Bitmap_File(&bitmap16bit, (char*)bitmap_file.c_str());
        } // end else if
        else if (word ==#t2#6--)
        {
            // We read#t2#6-- so we begin the fun stuff...

            // first read in all the bob info we need
            fin >> width >> height >> max_frames_per_row >> num_frames >> anim_speed >> row >> x >> y;

            // Determine the bob's attributes
            if (num_frames == 1)
                bob_attr = BOB_ATTR_SINGLE_FRAME;
            else
                bob_attr = BOB_ATTR_MULTI_FRAME;

            // Now push back another bob
            this->map.layers.back().tiles.push_back(this->bobs[Get_Next_BOB()]);

            // Create the bob using the bob info
            Create_BOB(&this->map.layers.back().tiles.back(), x, y, width, height, num_frames, bob_attr, DDSCAPS_SYSTEMMEMORY, 0, 16);

            // Load the frames since the bitmap is already open
            for (index=0; index<num_frames; index++)
                Load_Frame_BOB16(&this->map.layers.back().tiles.back(), &bitmap16bit, index, index, row, BITMAP_EXTRACT_MODE_CELL);

            // Set the animation speed
            Set_Anim_Speed_BOB(&this->map.layers.back().tiles.back(), anim_speed);
        } // end else if
    } // end while

    // Be sure to close the last bitmap file
    Unload_Bitmap_File(&bitmap16bit);

    // don't forget to close the file
    fin.close();

    return 1; // return success
} // end CLevel::Load_Map();