CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  1. #1
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    [RESOLVED] Scalability issue

    Hey. I've got a class called Screen that is an abstraction of graphical elements (that can be drawn to the screen). The class calls this function in it's constructor:
    Code:
    void Screen::Load_Elements(const std::string& directory)
    {  
      std::ifstream image_file;
      IO::open_read_file(image_file, directory + IMAGES_FN);
      IO::read_data_clone<XImage, std::vector<Base_XImage*> >(elements, image_file);
      image_file.close();
    
      std::ifstream button_file;
      IO::open_read_file(button_file, directory + BUTTONS_FN);
      IO::read_data_clone<XButton, std::vector<Base_XImage*> >(elements, button_file);
      button_file.close();
    
      std::ifstream audible_button_file;
      IO::open_read_file(audible_button_file, directory + AUDIBLE_BUTTONS_FN);
      IO::read_data_clone<Audible_XButton, std::vector<Base_XImage*> >(elements, audible_button_file);
      audible_button_file.close();
    
      std::ifstream draggable_button_file;
      IO::open_read_file(draggable_button_file, directory + DRAGGABLE_BUTTONS_FN);
      IO::read_data_clone<Draggable_XButton, std::vector<Base_XImage*> >(elements, draggable_button_file);
      draggable_button_file.close();
    
      std::ifstream dialog_box_file;
      IO::open_read_file(dialog_box_file, directory + DIALOG_BOXES_FN);
      IO::read_data_clone<XDialog_Box, std::vector<Base_XImage*> >(elements, dialog_box_file);
      dialog_box_file.close();
    
      std::ifstream input_box_file;
      IO::open_read_file(input_box_file, directory + INPUT_BOXES_FN);
      IO::read_data_clone<XInput_Box, std::vector<Base_XImage*> >(elements, input_box_file);
      input_box_file.close();
    }
    This is the member that stores all the elements.
    Code:
    std::vector<Base_XImage*> elements;
    The obvious problem here is that the Load_Elements function is huge... and will only get huger each time I create a new class like Slider or something. Also, I have a folder structure that looks like this:
    Code:
                         <screen_name>
               images.txt buttons.txt    etc....
               <name x y> <name x y>   <...>
    Where screen_name is a folder and images.txt and buttons.txt are text files and <name x y> is each individual respective element to load. Doing it this way means that each time I add a new class (e.g Slider), I have to add a corresponding text file in every "Screen" folder to every project that uses Screen, even if it's empty, otherwise open_read_file throws an exception to tell me the file wasn't found! I could fix this particular issue by putting try/catch around each paragraph of code in Load_Elements, but that is just dodgy...

    I thought about something like this:
    Code:
    template<typename T>
    void Load_Elements_Of_Type(std::vector<Base_XImage*>& elements,
    const std::string& directory, const std::string& file_name)
    {
      try
      {
      std::ifstream element_file;
      open_read_file(element_file, directory + file_name);
      read_data_clone<T, std::vector<Base_XImage*> >(elements, element_file);
      element_file.close();
      }
      catch(...)
      { // Don't want to load this type.
      }
    }
    
    // Inside Load_Elements:
    Load_Elements_Of_Type<XButton>(elements, "directory/", "file.txt");
    Load_Elements_Of_Type<XSlider>(elements, "directory/", "file.txt");
    // Load every possible type.
    But I don't know what happens when T doesn't derive from Base_XImage... and it's still gonna be non-scalable.

    I really need to sort these problems out now before they get worse... Any suggestions?

    Cheers.
    Last edited by Mybowlcut; March 6th, 2008 at 10:06 AM.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured