Results 1 to 9 of 9

Thread: How do you update a TDXImageList at runtime?

  1. #1

    How do you update a TDXImageList at runtime?

    I'm using UndelphiX for Delphi 7 on Delphi 2006.

    Everything I've seen in any tuturial only talks about loading images into a TDXImageList at design time, and then using them. But what I really want to do is add images directly into the list at runtime, like I can do with the TImageList.add method.

    Since TDXImageList doesn't *have* a handy "add" method, how can I do it? I don't care how kludgy or work-around-ish it is; I just want to be able to insert items into the list at runtime, at will.

    Can anyone help me with this?

    Mason

  2. #2
    PGD Community Manager AthenaOfDelphi's Avatar
    Join Date
    Dec 2004
    Location
    South Wales, UK
    Posts
    1,245
    Blog Entries
    2

    How do you update a TDXImageList at runtime?

    It is possible and relatively straight forward.

    The key thing is not rely on the auto initialisation of the other components such that you can initialise and deinitialise them at will.

    Basically, what you do, is load the images you want to use into your TDXImageList, then initialise the TDXDraw component. Once you've done that, you link the image list to the TDXDraw and build the colour tables.

    Code:
    fDXDraw.initialize;
    
    fTileSet:=fEngineState.game.tiles;
    fTileSet.dxdraw:=fDXDraw;
    
    fTileSet.items.MakeColorTable;
    
    fDXDraw.DefColorTable:=fTileSet.items.colorTable;
    fDXDraw.ColorTable:=fTileSet.items.colorTable;
    The only problem with this is that making the colour tables can take time.

    When you're done and you want to load other tiles, you just call the finalize method of the DXDraw, and unlink the TDXImageList... or the other way round, I can't remember (Sorry :-) ).

    The obvious downside to this is the fact that DirectX will completely deinitialise and then have to reinitialise, so its not a quick load and go option.

    Hope this helps :-)
    :: AthenaOfDelphi :: My Blog :: My Software ::

  3. #3

    How do you update a TDXImageList at runtime?

    Thanks. I assume, from your example, that fTileSet is a TDXImageList object. And you've hit the mark spot-on; creating a tile set is exactly what I'm trying to do. But what is fEngineState.game.tiles? (Or to simplify, what is fEngineState?)

    Also,
    Basically, what you do, is load the images you want to use into your TDXImageList,
    Yeah, that's what I'm trying to do. But how do I do it at run-time? The only way I know to do it is to set up the list in the object inspector at design-time.

    Mason

  4. #4
    PGD Community Manager AthenaOfDelphi's Avatar
    Join Date
    Dec 2004
    Location
    South Wales, UK
    Posts
    1,245
    Blog Entries
    2

    How do you update a TDXImageList at runtime?

    Sorry I don't have more time to explain, but heres a quick breakdown.

    This code comes from my competition entry from last year. It basically had an editor and a play engine. The code I pasted earlier was from the play engine and yes, fEngineState.game.tiles is a TDXImageList that is loaded from a file stream linked to a data file containing the games base data (fEngineState is a big object that holds the entire state of the play engine... one of the sub-objects is 'game' which hold all the games base data... one component of that is the tile set 'tiles').

    To populate a TDXImageList at runtime, heres some code... this is taken straight out of my game data editor (fTileImportBuffer is a TDXImageList)

    [pascal]
    function TfrmGameEditor.loadTilesFromFile(filename:string): boolean;
    var
    dib : TDib;
    tile: TDib;
    x : integer;
    y : integer;
    tilesX : integer;
    tilesY : integer;
    srcRect : TRect;
    dstRect : TRect;
    item : TPictureCollectionItem;


    dim : integer;
    begin
    result:=false;

    fTileImportBuffer:=TDXImageList.create(application );

    dib:=TDib.create;

    dim:=8+(cboSourceSize.itemIndex*;

    try
    dib.LoadFromFile(filename);

    if (dib.Width mod dim<>0) or (dib.height mod dim<>0) then
    begin
    showMessage('Cannot load file - Not divisible by '+intToStr(dim));
    exit;
    end;

    dstRect.top:=0;
    dstRect.left:=0;
    dstRect.bottom:=dib.height;
    dstRect.right:=dib.width;

    tilesX:=(dib.width div dim);
    tilesY:=(dib.height div dim);

    dstRect.top:=0;
    dstRect.left:=0;
    dstRect.right:=dim;
    dstRect.bottom:=dim;

    tile:=TDib.create;
    tile.SetSize(dim,dim,24);

    for y:=1 to tilesY do
    for x:=1 to tilesX do
    begin
    srcRect.left:=(x-1)*dim;
    srcRect.right:=srcRect.left+dim;
    srcRect.top:=(y-1)*dim;
    srcRect.bottom:=srcRect.top+dim;

    tile.canvas.CopyRect(dstRect,dib.canvas,srcRect);

    item:=TPictureCollectionItem(fTileImportBuffer.Ite ms.add);
    item.Picture.Graphic:=tile;
    item.transparent:=true;
    item.transparentColor:=clBlack;
    end;

    result:=true;
    finally
    try
    dib.free;
    except
    end;

    try
    tile.free;
    except
    end;
    end;

    reinitializeTilePreviewDX;
    end;

    {$warnings on}

    [/pascal]

    What this does is loads a bitmap into a device independent bitmap and then chops it up into tiles of 'dim x dim' size. dim is calculated based on a size specified in a combo box on the editor form. These tiles are then just dumped into a TDXImageList.
    :: AthenaOfDelphi :: My Blog :: My Software ::

  5. #5

    How do you update a TDXImageList at runtime?

    Whee! That's precisely what I needed! (With a few minor modifications to fit my specific project, of course.) You mind if I use your code?

    Mason

  6. #6

    Ack!

    One other question.

    Trying to load up the file I'm using gave me this:
    First chance exception at $7C812A5B. Exception class EInvalidGraphic with message 'DIB is invalid'.
    I assume this is because the file I'm trying to open isn't in DIB format. (It's a PNG.) I've already got a routine written to open the file and convert it, from before I got your example.

    Code:
       dlgOpen.Filter&#58;='All images&#58; '+BitmapLoaders.GetLoadFilter;
       if dlgOpen.Execute then
          filename &#58;= dlgOpen.FileName;
          with TLinearBitmap.Create do
          try
             imgLoaded.Picture&#58;=nil;
             LoadFromFile&#40;dlgOpen.FileName&#41;;
             AssignTo&#40;imgLoaded.Picture.Bitmap&#41;;
          finally
             Free;
          end;
       //end if
    (imgLoaded is a TImage control.) What would be the best way to merge this with your code, so the dib object gets its picture from the (now decoded) imgLoaded instead of trying to bring it in straight from the file? Or is there a better way?

    Thanks for your patience,

    Mason

  7. #7
    PGD Community Manager AthenaOfDelphi's Avatar
    Join Date
    Dec 2004
    Location
    South Wales, UK
    Posts
    1,245
    Blog Entries
    2

    How do you update a TDXImageList at runtime?

    Hi mason,

    I'm afraid (as far as I know), (un)DelphiX doesn't support PNG's directly. As you've said you've got a conversion routine, I guess whether or not its compatible depends on the output from the routine.

    Maybe someone who has used DelphiX more extensively could chip in.

    For what its worth, I've stopped using (un)DelphiX because I've never been overly impressed with the performance (even with hardware acceleration). Asphyre is pretty neat, so too is DanJetX and then of course OpenGL.

    Anyhow, sorry I can't help more on the image format stuff.
    :: AthenaOfDelphi :: My Blog :: My Software ::

  8. #8

    How do you update a TDXImageList at runtime?

    Yes PNG is unsupported, I really wish one day it is added to delphix but for now, yo will have to make do with the rather crap transparent settings.

  9. #9

    ...unDelphiX 1.07f

    Tool for PNG-conversion exists (with alphachannel) and it is under testing now. I release it next week with installation tool of (un)DelphiX 1.07f (I announcing support for Delphi 2007 and good work under Windows Vista; it is tested now too). As bonus I have prepared new tool for conversion animated GIF.

    Regards for all
    Ijcro.

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
  •