Portions of an Image

BlitzMax Forums/BlitzMax Beginners Area/Portions of an Image

Teacup(Posted 2008) [#1]
Hi there,

A quick introduction; I'm a late-20's IT worker, I've been coding since I was a young'en and remember Blitz from my days back in Amiga land - it was one of my first languages along with Amiga E.

I spend most of my coding time delving into the underbelly of C#, PHP and other assorted things for business applications. I recently found out that Blitz had been made over and decided to jump back in. It feels pretty comfortable so far and I'm hoping that it will help me get the game ideas that have been stuck in my head for so many years out into the open.

So that's me and why I'm here; now my query. :-)

I have a .png image that has a collection of sprites for a 2D game. I know I could split the file into many files and use a seperate TImage for each one, but I would rather keep them as one file / one TImage and pull out the bits I need when I need.

On the Code Archives section of this site I found Cower.Drawing which seems like it's meant to do exactly what I want.

I've added it to my mods directory as appropriate, used bmk to makemod it and used Framework to include it into my game (or at least what will be a game one day.)

I've set up the DrawImageBlock code as I think it should be, as opposed to the Draw code.

But then there's a problem. I get an error message complaining about my TImage being a null value (or similar.) To put it short, it doesn't work. I've traced the error down to the initial loading (LoadImage) of my TImage but I'm not sure why.

If I take out the Cower.Drawing stuff and use Draw instead, I get my whole image and no errors - so I know that code is fine. But the question is, why does Cower.Drawing seem to cause this problem?

Ultimately, all I want to do is display parts of the image as and when necessary, so if anyone doesn't know the answer to my question but does have an alternative solution, please do show me!


Thanks!


Amon(Posted 2008) [#2]
If you're trying to load an animated character where all frames are the same size and it is all in one image then look at LoadAnimIMage?

If this is not what you want to do and you just want to grab part of an image to display look at Grabimage and pixmaps.


Teacup(Posted 2008) [#3]
The second option is what I'm after. I'll take a look. Many thanks. :-)


Update: Solved.

This works like a charm:
http://blitzbasic.com/codearcs/codearcs.php?code=1440


tonyg(Posted 2008) [#4]
... but might be slow.
Try this might help


Teacup(Posted 2008) [#5]
Thanks Tony. If you don't mind me asking, why would it be slow in comparison to the DrawImageRect code?


tonyg(Posted 2008) [#6]
it has to reload the pixmap from system memory to video memory (i.e. new pixmap to an image/surface) while drawimagerect uses the same surface each time but displays different portions of it.


Teacup(Posted 2008) [#7]
Thanks tony, I'll go with the way you suggested.

(Sorry for the delay, ultra busy week!)


SpaceCowboy(Posted 2008) [#8]
If all your sprites are the same size you could use LoadAnimImage and draw the individual sprites by drawing the different frames. You don't have to use it for animated sprites...

e.g. if you had 100 32x32 pixel sprites..
sprite = loadanimimage ("sprites.png",32,32,0,100,MASKEDIMAGE)
drawimage sprite,x,y,0    ' draws 1st sprite
drawimage sprite,x,y,1    ' draws 2nd sprite
drawimage sprite,x,y,2    ' draws 3rd sprite, etc


..dunno if this is better/worse than the DrawImageRect method, or just different..