Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: use inherited abstract procedure with typecasting?

  1. #11

    use inherited abstract procedure with typecasting?

    @savage: please do explain and an example would also be nice.

    [EDIT]
    Looking into class factories i do see benefits for loading meshes, but not yet for saving a mesh into a different format as it is loaded in. Hmm maybe via cloning or something like that?
    [/EDIT]
    http://3das.noeska.com - create adventure games without programming

  2. #12

    use inherited abstract procedure with typecasting?

    [pascal]
    type
    TModelLoaderClass = class of TGameInterface;

    function TModelLoadingManager.LoadModel( aModelLoader : TModelLoaderClass; string : aFileName ) : TModelLoader;
    begin
    if aModelLoader <> nil do
    begin
    result := aModelLoader .Create( aFileName );
    end;
    end;
    [/pascal]

    which can then be called as

    [pascal]
    var
    ModelToLoad : TModelLoader;
    begin
    ModelToLoad := MyModelLoadingManager.LoadModel( T3DSModel, 'c:\somewhere.3ds' );
    ModelToLoad.DoStuffWithModel;
    .
    .
    .
    ModelToLoad := MyModelLoadingManager.LoadModel( TDirectXModel, 'c:\somewhere.x' );
    ModelToLoad.DoOtherStuffWithModel;
    end;
    [/pascal]

    I'm using this sort of factory technique in the SoAoS port to decide what interface to load and display next. It looks like this...

    [pascal]
    type
    TGameInterfaceClass = class of TGameInterface;
    var
    CurrentGameInterface : TGameInterfaceClass = TGameIntro;
    GameWindow : TGameInterface;

    begin
    while CurrentGameInterface <> nil do
    begin
    GameWindow := CurrentGameInterface.Create( Application );
    GameWindow.LoadSurfaces;

    Application.Show;
    CurrentGameInterface := GameWindow.NextGameInterface;

    if ( GameWindow <> nil ) then
    FreeAndNil( GameWindow );
    end;
    end.
    [/pascal]

    This way I can add X new screens/interfaces without the need to update any enumeration types. I just have to make sure that when I want to display an interface that I populate the NextGameInterface variable with a game interface class type and it just works.

    I'm not saying this doesn't have it's drawbacks, but I think is less of a maintenance headache than updating an enumerated list everytime I think of adding something new. Also no case statements required either. Though they can be very usefull at times.

    To do something that would allow you to load one model and then save it as some other format, you will need to have one common way of holding the data in your base class or similar, then each derived class would use this stored data to convert it to what format it is familiar with. Look through the VCL and look at how TGraphic/TPicture work to see how this can be implemented.

    I hope this helps.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  3. #13

    use inherited abstract procedure with typecasting?

    Do class factories also work with free pascal?

    So in the baseclass i need to implement the assign funtion and i am there.
    Then i can create a new object from the class i want to use for saving and use something like
    var ModelToSave: TMsaModel;
    ModelToSave := TMsaModel.Create(nil);
    ModelToSave.Assign(ModelToLoad);
    ModelToSave.SaveToFile('test.txt');
    ModelToSave.Free;
    Or i could place the above code in TModelLoadingManager as an safetoformat function.

    So i have to make sure i clean up the copies as otherwise i could end up with a lot copies of the same object.

    Next is Rendering. Assigning the model to an TGlModel for each render is not working nice. What could work is doing it once after loading and free the original meshes.
    OR
    Use the suggested seperate render class. The render class needs to read out details for the models. That way i could also optimize rendering by sending out all geometry at once and other things like that....

    Thankt to you all...
    http://3das.noeska.com - create adventure games without programming

  4. #14
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    use inherited abstract procedure with typecasting?

    The way I get around these problems is to not use the Abstract keyword

    [pascal]
    TModel = Class
    Procedure Render; Virtual;
    Procedure LoadFromFile(FileName : String); Virtual;
    End;

    Procedure TModel.Render;
    Begin
    // Empty Method, override to do something useful
    End;

    Procedure TModel.LoadFromFile(FileName : String);
    Begin
    // Empty Method, override to do something usefull
    End;
    [/pascal]

    Then the child classes do what they need

    [pascal]
    TmsaModel = Class(TModel)
    Procedure Render; Override;
    Procedure LoadFromFile(FileName : String); Override;
    End;

    Procedure TmsaModel.Render;
    Begin
    Inherited;
    ..
    Do Something
    ..
    End;

    Procedure TmsaModel.LoadFromFile(FileName : String);
    Begin
    Inherited;
    ..
    Do Something
    ..
    End;
    [/pascal]

    Then when creating a msa model, but making it compatible with all other models I use the base class and let polymorphism take care of what gets called.

    [pascal]
    Var
    MyModel : TModel;
    Begin
    // Create it as the child class type I need to use
    MyModel := TmsaModel.Create;
    MyModel.LoadFromFile('Model.msatype');

    //To render
    MyModel.Render;
    End;
    [/pascal]

    Because the methods are virtual in the base class but the created type is TmsaModel the TMsaModel methods will be called and not the base methods.

    Of course this means that at the base level you need to create stubs for all the methods you will be using later. but remember you can also do this

    [pascal]
    If MyModel is TmsaModel then
    TmsaModel(MyModel).msaMethod;
    [/pascal]

    To call specific methods for the various types.
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

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