Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: Reading Sprites....

  1. #1

    Reading Sprites....

    Hi everyone....I am a newcomer in the world of Games programming hence the question that follows... :?

    How can you read off sprites of a sprite library such as http://www.arifeldman.com/free/downloads.html ?

    Thanks for your time and sorry for the daft question... ops:

  2. #2

    Reading Sprites....

    It's not a daft question -- we all have to start somewhere!

    The process of using those sprites can be divided into these steps:

    1) Grab the images from a file and load it into memory
    2) Find a way to specify each sprite's position as a rectangle/square (up to you!) of the larger bitmap
    3) Draw the things at some point by copying the wanted rectangle from the big bitmap onto the screen (or somewhere else)

    There are a great many ways to achieve this since there are lots of different methods for drawing stuff (e.g. Direct3d, DirectDraw, OpenGL, GDI, GDI+, and others I forgot...). It's not difficult though.

    The simplest method is to use the TBitmap class. You create a bitmap (and free it once finished) and use the LoadFromFile method to get the _entire_ bitmap into memory. You can then decide what sprites you need -- this is a case of specifying a rectangle (TRect) using the Bounds or Rect functions. You'd pass the coordinates of your sprite when you want to draw it using the CopyRect method available in TCanvas.

    If it makes things simpler, you could just cut-and-paste each sprite into many small, individual bitmaps manually (with Paint or whatever) rather than having them all crammed into one tileset. It's your call, as always .

    Here's some quick example code:

    [pascal]// the following is used for the characters
    // in the game -- a creature with a position (x, y) on
    // the screen and a picture (specified by the coords)
    type
    TMonster = record
    x, y: Integer; // current screen position
    PicCoords: TRect; // rect of the picture in overall bitmap
    end;

    procedure TYourForm.Blah(const Monster: TMonster);
    var
    Bmp: TBitmap; // stores the large image w/ lots of sprites
    begin
    Bmp := TBitmap.Create;
    try
    Bmp.LoadFromFile('somebitmap.bmp'); // load your sprites into memory

    // copy this monster onto the screen, grabbing the
    // picture as a small rectangle of the big bitmap. Make sense?
    // assuming that the sprites are 32 by 32 pixels, change appropriately
    Canvas.CopyRect(Bounds(Monster.x, Monster.y, 32, 32), Bmp.Canvas, Monster.PicCoords);
    finally
    Bmp.Free;
    end;
    end;[/pascal]
    Of course, you'd want to initialise some of your monsters with positions and so on, but that demonstrates the basic idea.

    There are variations on this of course. You can store the sprite-coords-within-larger-pic as an array of TRects, then have the monster record store an index instead of the coordinates directly. This would save space if you have lots of monsters with the same picture.

    The change would look like this:

    [pascal]type
    TMonster = record
    x, y: Integer; // current screen position
    Picture: Integer; // index of the monster's picture
    end;

    Coords: array[0..MonsterCount - 1] of TRect; // each sprite you want

    // init the coords somehow, maybe
    // ... LoadCoordsFromFile(Coords); // write this function
    // or Coords[0] := Rect(0, 0, 32, 32); Coords[1] := Rect(32, 0, 64, 32);

    // then later, when drawing...
    Canvas.CopyRect(Bounds(Monster.x, Monster.y, 32, 32), Bmp.Canvas, Coords[Monster.Picture]);[/pascal]

    If the above isn't clear then let me know and I'll write a small example app to demonstrate what I mean.

    You'd usually only create and load the bitmap at the start, keeping it in memory for as long as you can. At the end, you'd free it. This may well mean creating + loading in the form's OnCreate method and freeing in the OnDestroy. You should hopefully understand the reason why -- avoid unnecessary work each update if you can.

    Also, the different graphics libraries will function much like the above. In most cases, there's something like the TBitmap class (like an IDirectDrawSurface7 for DirectDraw, a texture in OpenGL, and so on). There will be a "stores pictures and things" class, and some way to copy smaller parts of it (like Blt in DDraw).

    You can also copy the smaller parts of the bitmap into an array and use those pictures directly, instead of with CopyRect. In other words:

    [pascal]type
    TMonster = record
    x, y: Integer;
    Pic: Integer; // index to TBitmap for the sprite
    end;
    Pics: array[0..TotalSprites - 1] of TBitmap;

    procedure CreateThePics;
    var
    i: Integer;
    MainBmp: TBitmap;
    begin
    MainBmp := TBitmap.Create;
    MainBmp.LoadFromFile('c:\wherever.bmp');

    for i := Low(Pics) to High(Pics) do
    begin
    Pics[i] := TBitmap.Create;
    Pics[i].Width := SpriteSize; // 32 or whatever
    Pics[i].Height := SpriteSize; // 32 or whatever
    // Use CopyRect to copy from the big bitmap into this small one
    // remembering that you need some way to specify the coords.
    end;
    MainBmp.Free;
    end;[/pascal]

    Keep in mind that TBitmap/TCanvas won't produce the best results, but they'll do for starters. You can have a look at http://terraqueous.f2o.org/dgdev/viewtopic.php?t=199 for a tutorial on TBitmap, etc., before moving onto cooler APIs (see the huge amount listed on dgdev's main page and pick one ).

    The main challenge is specifying the coordinates for your sprites. You can do this either by putting the coordinates into a hard-coded array or by loading them from file (better, since you can change stuff without recompiling the program). It's still pretty tedious work whatever you choose, though!
    "All paid jobs absorb and degrade the mind."
    <br />-- Aristotle

  3. #3

    Reading Sprites....

    hei....thnx a lot Alimonster... ....i ll put it to work right away and see how it goes.....
    otherwise i ll be posting more questions soon.... :shock:

    thks again..

  4. #4

    Reading Sprites....

    Quote Originally Posted by Alimonster
    ...If the above isn't clear then let me know and I'll write a small example app to demonstrate what I mean.

    ...
    Right! I have been working on the thing for the past week and i am not too sure about the whole thing....
    If you could create that application u were talking about that would be really great. Thanks Alimonster

    Traveler...:

    I have tried your first tutorial on creating a Game Framework and although i did follow everything and downloaded all the required files i only seem to get a Black screen when i compile the code.

    I am using DirectX 9.0 with Delphi 7 and WinXP....thnx ops:

  5. #5

    Reading Sprites....

    I have tried your first tutorial on creating a Game Framework and although i did follow everything and downloaded all the required files i only seem to get a Black screen when i compile the code.
    Thanks for using my tutorials
    I'm not sure what is causing your problems. It might be the DelphiX version (since you're using Delphi7) or perhaps it's something you forgot to include in the code.

    If you want to I can have a look at your code, see what's wrong with it.

  6. #6

    Reading Sprites....

    Traveler, that would be great but i do not have webspace to upload....I ll see if i can get some free temporary hosting to upload it and I ll let you know....thnx

  7. #7
    iLLUNis
    Guest

    Reading Sprites....

    If you want i could upload it for you in my website.....just PM me or email me at postmaster@illunis.net :lol:

  8. #8
    iLLUNis
    Guest

    Reading Sprites....

    Got the code....it can be found here
    http://www.illunis.co.uk/Temp/framework.rar

  9. #9

    Reading Sprites....

    Traveler, that would be great but i do not have webspace to upload....
    I probably should have been more clear, but it was alright for you to e-mail me the code, instead of uploading it to a website. I any case, I received the files. Thanks for mediating iLLUNis!

    I have looked through your code and noticed a minor error. You have misplaced some of the code. Instead of placing the code in the onTimer event, you have placed it in the onActivate event. If you change this, then it should work correctly.

  10. #10
    iLLUNis
    Guest

    Reading Sprites....

    no prob...

Page 1 of 2 12 LastLast

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
  •