DrawPixMap help for a newbie

BlitzMax Forums/BlitzMax Beginners Area/DrawPixMap help for a newbie

Arabia(Posted 2009) [#1]
Hi I'm new to the forum and new to BM.

I started writing a little program last year, which was working quiet well and then never got around to finishing it. I've decided this afternoon to try and finish it (will post it here when done) but have come across a weird problem straight away.

What I have done, works fine on my laptop (XP) - I've just taken the files across and put them on my desktop (Vista) and none of what I've done works :S I mean, the program compiles without error and runs - but none of the PNG files will display for some reason.

I've just done a very very basic program - it runs without error (i.e. no error is given that the PNG can't be loaded) - but the program just doesn't show the image

Any ideas on what I'm doing wrong?


Local tileFrame : TPixmap
tileFrame = LoadPixmap("c:\blitz\BigPicFrame.PNG")

Graphics 800, 600
Cls

DrawPixmap (tileFrame, 10, 100)
Flip

WaitKey


Thanks for any help :)


xlsior(Posted 2009) [#2]
You're hardcoding the path to the image: it will ALWAYS look in the c:\blitz folder this way. If you leave out that part, it will open the images from the same folder as the program itself -- in that case you can move the files anywhere you want, as long as you keep them together.
(you can still organize your files e.g. by making a subdirectory 'images' for your images, and loading it as LoadPixmap("images\BigPicFrame.png")

Also: do you really need pixmaps? Unless you need to modify your images in real time, it's MUCH faster to use LoadImage and DrawImage instead. That way the images reside in the video memory of your video card, and don't need to be transferred over your PCI bus to the video card every time you wish to draw them.


Arabia(Posted 2009) [#3]
Thanks.

I actually hardcoded the path when I couldn't get anything else to work - originally it was LoadPixmap("BigPicFrame.png") and the way it was worked fine under XP - it's only been since I've transferred it to Vista that the image isn't showing.

If I change it to LoadPixmap("NoExistant.PNG") then I will get a runtime error at the DrawPixmap line "Unhandled Exception: Attempt to access field or method of Null Object" - but as it stands, I get no error - the image just isn't shown on my Vista PC.

As for Pixmaps V LoadImage - well Pixmaps were what I cam across first when I started the program. Load/DrawImage may be better, but I haven't looked at them, the differences or the benefits of each - I will do that now and let you know if that works.

Thanks


Arabia(Posted 2009) [#4]
Okay, just tried LoadImage/DrawImage and it worked.

Can anyone tell me why this doesn't work:

Local tileFrame : TPixmap

tileFrame = LoadPixmap("BigPicFrame.PNG")

Graphics 800, 600
Cls
DrawPixmap (tileFrame, 10, 100)
Flip

WaitKey

And this DOES work:


Local tileFrame : timage

tileFrame = LoadImage("BigPicFrame.PNG")

Graphics 800, 600
Cls
DrawImage (tileFrame, 10, 100)
Flip

WaitKey


:S :S DrawImage may be better for what I am doing, just confused why PixMap works on my other machine - is there some issue with Vista?


Brucey(Posted 2009) [#5]
DrawPixmap can have issues depending on what graphics card/driver you have. It is notoriously poor on Intel chips - these are driver issues more than anything.
Also, DrawImage is more efficient, since the image data is loaded into the graphics card memory.


LoadImage actually uses LoadPixmap under-the-hood to load the image data. :-)


GfK(Posted 2009) [#6]
You should always use DrawImage anyway since its loads faster. If you need to modify the image at all then you can use LockImage to convert the image to a pixmap, modify the pixmap, then use UnlockImage again on the original image handle.

That way you get the best of both worlds.


Arabia(Posted 2009) [#7]
Thanks Brucey.

Maybe that explains it :S

Desktop has a NVIDIA GeForce 8600 GT - Laptop is a cheapo with onboard Video and shared memory, if anything, I would expect the problem to be the other way round.

But since you both suggest DrawImage is better, then I'll try and change to that and see how I go :)

I did change all of my code to LoadImage, DrawImage and I'm still having problems. But I'll look a bit more a post again if I still can't figure it out.

Thanks


GfK(Posted 2009) [#8]
The only thing I can see with your code that *may* be a problem, is that you're loading your image before setting a graphics mode. I'm pretty much certain that this won't make any difference whatsoever but might be worth a try setting the graphics mode first.

Other than that, are you running the IDE as administrator, or do you have UAC turned off?


Arabia(Posted 2009) [#9]
GfK #6

You should always use DrawImage anyway since its loads faster. If you need to modify the image at all then you can use LockImage to convert the image to a pixmap, modify the pixmap, then use UnlockImage again on the original image handle.

That way you get the best of both worlds.




Okay, now I'm a little lost. I was, when using Pixmap, resizing them.

How exactly do I do this now that I am using Images?

Sorry if I am asking basic questions, tell me to go look more at the documentation and I will :D

i.e. part of mycode to resize whatever picture was:

pic = ResizePixmap(pic, 640, 480)
DrawPixmap (pic, bigPicOffsetX, bigPicOffsetY)

These is no ResizeImage - so I need to Lock this (convert it to pixmap?), resize, then unlock it?


Arabia(Posted 2009) [#10]
Other than that, are you running the IDE as administrator, or do you have UAC turned off?


Running as administrator


GfK(Posted 2009) [#11]
These is no ResizeImage - so I need to Lock this (convert it to pixmap?), resize, then unlock it?
That should do it. Or a better option may be to just use SetScale before you DrawImage. SetScale affects all drawing operations though, and although you can't set the scale 'per image', there's no overhead in using SetScale a load of times every time you draw the screen.

I think you should be OK running as administrator - someone correct me if I'm wrong, as I always have UAC off because its a pain and causes more trouble than it solves.


Brucey(Posted 2009) [#12]
Also, if you want to draw an image to fit a particular size, you can use DrawImageRect :
   DrawImageRect(pic, 0, 0, 640, 480)

It's a bit like scaling...


Arabia(Posted 2009) [#13]
Also, if you want to draw an image to fit a particular size, you can use DrawImageRect :

DrawImageRect(pic, 0, 0, 640, 480)

It's a bit like scaling...


That sounds exactly like what I want :)

Haven't got any further, will maybe do some more this weekend - too tired to code at the end of the working day unfortunately - but I do hope to post my first ever (completed) program in BM soon.

It seems like a lot of people are very helpful in here, and there is an awful lot of devoted users - I don't supposed anyone has as a single user or as a community thought about producing a proper manual for BM (as well as the other variations). I'd be more than willing to shell out another $50 on the cost price of BM to have a proper manual (even if I have to print a PDF) to refer to.

Anyway, thanks for the help. I'll continue on with my effort this weekend.


GfK(Posted 2009) [#14]
You really don't need a manual. The forum and the search feature will help you to find out pretty much anything you want to know. The 'Advanced Search' generally yields much better search results. If you can't find the solution yourself then there's generally plenty of people floating around to ask.

I do recommend that you get an IDE which has intellisense as it'll help you immensely - http://www.blide.org is a great place to start, as is http://www.projectstudioide.com


xlsior(Posted 2009) [#15]
Regarding searching the forum: It seems to be a lot easier using google with a 'site:blitzmax.com' as part of the query rather than using the forum's own built-in search functionality...

(The forum's own search won't search the code archives, for one)


GfK(Posted 2009) [#16]
Regarding searching the forum: It seems to be a lot easier using google with a 'site:blitzmax.com' as part of the query rather than using the forum's own built-in search functionality...
Trouble with that is it won't be right up to date.


Arabia(Posted 2009) [#17]
You really don't need a manual


Trouble with forums is that you normally need to search through so many threads and page after page of responses, before you find what you are looking for.

The first BASIC language I ever encounted on PC's was GW-BASIC, and at least (at the time) you got a manual with it. You were given each keyword, function etc. and an example. Made learning something new a whole lot easiler. i.e. My original question was about Pixmaps - having a proper manual, I would have never asked such a question if that makes sense :S

When I got my latest PC I decided to get dual monitors, thinking it would be easier to get back into things if I could code on one monitor and have IE going on the 2nd monitor with whatever it was I was looking for on the other - but nothing can really compare to a proper manual IMO :)

The online (website) manual for BM leaves a whole lot to be desired :(, but at least it's a start.

How I found BM from memory was searching for a language that someone I used to know used on his Amiga - AMOS Basic - It was an awesome language and he did some amazing things (comapred to what PC users were used to at the time) - and even it had a decent manual.


GfK(Posted 2009) [#18]
I used AMOS and AMOSPro, too. AMOSPro had a great manual but then, we didn't have to worry about it becoming obsolete because the software wasn't ever updated like Blitzmax is.


Arabia(Posted 2009) [#19]
GfK (Posted 22 hours ago) #18

I used AMOS and AMOSPro, too. AMOSPro had a great manual but then, we didn't have to worry about it becoming obsolete because the software wasn't ever updated like Blitzmax is.




Point taken - but I still feel, a proper manual should be a priority!

I've thought about doing it... Maybe as I teach myself BM, I should document what I learn - but having little time to program these days, BM will probably be extinct before I teach myself everything :D


Arabia(Posted 2009) [#20]
Okay, another problem:

In my previous program (using Pixmaps) I had

Type TTile
	Field pm:TPixmap
	Field tileno:Byte
EndType

Local tiles:ttile [4, 3]
Local sTiles:ttile [4, 3]
Local x:Byte
Local y:Byte
For x = 0 To 3
	For y = 0 To 2
		tiles[x,y]=New ttile
		sTiles[x,y]=New ttile 
	Next
Next


Now, if I change from TPixmap to Image I get an Unhandled Memory Exception on compile.

I am guessing the PixMaps are dynamic and images aren't :S I've tried defining the area, and then using CreateImage on each element but that doesn't work:

Type TTile 
	Field tImg : tImage
	Field tNo	: Byte
EndType

Local tiles: TTile[xTiles, yTiles]

' Init tiles array

Local x,y : Byte

For x = 1 To xTiles
	For y = 1 To yTiles
		tiles[x, y].TImg=CreateImage(128,96,1,DYNAMICIMAGE|MASKEDIMAGE)
	Next
Next


Should I go back to Pixmaps for this portion of the program and convert between TPixMap (to store the images) and TImage when I want to redraw them?

Hope that makes sense :)


Arabia(Posted 2009) [#21]
Ignore my last brain fart.

Unhandled Memory Exception caused by trying to access tiles[5,5] when it should have been tiles[4,4]

I think from my VB days, there was a way to specify the bounds of an array - i.e. an array created as a[5] would actually have six elements a[0..5]

I just find it easier to think as the first element being 1 and not 0 :S


GfK(Posted 2009) [#22]
In Blitzmax everything is zero-based. Which is better because iirc, in other versions of Blitz, some things were zero-based and some things were one-based.

At least its consistent now. You do get used to it, although sometimes I still make the mistake of doing something like...
 For n:Int = 1 to myList.Count()
instead of
For n:Int = 0 to myList.Count() - 1



Brucey(Posted 2009) [#23]
Better still, you can do this :
 For n:Int = 0 Until myList.Count()

Which means you don't need to remember to stick -1 on the end.


joncom2000(Posted 2009) [#24]
I respect to your first post about drawpixmap not showing anything under vista. I have vista32bit and a geforce 8500GT and if I did as you have done above I also dont get an image displayed in some cases.

I think its because by default bmax uses dx7 if you dont set a graphics driver for all drawing and the pixmap would generally to be in BGR or BGRA format to display but loadpixmap doesnt appear to make loaded files this format by default since you may not actually want this format, you can ensure there the format you want by doing a ConvertPixmap to the required format just after its loaded which should solve the problem.

The PNG loader actually loads in RGB format this should be fine but for some reason it appears that drawpixmap under DX isnt drawing them even tho it should be doing a conversion before the drawing operation, it maybe a vista only issue with drivers since it doesnt seem to be a common error posted about :S


Arabia(Posted 2009) [#25]
In Blitzmax everything is zero-based. Which is better because iirc, in other versions of Blitz, some things were zero-based and some things were one-based.

At least its consistent now.


You've got a point. I know in early versions of basic, and array a(5) actually had six possible elements - but a(0)was rarely used. Human nature says that the first item is 1, and not 0 - which can cause some confusion until you get used to it :S

The other way around it, is to start at 1 and just make the array one bigger in size - but this does cause problems later with internal arrays (i.e. mylist as in the example you gave) where you have no control over 0 or 1 being the first item in the array.

Anyhoo, making and fixing this problem without the help of others has made me more aware of this and hopefully I won't do it too much again now :) Must admit, the Unhandled Memory Exception error was not very helpful in telling me where I went wrong - program crashes without any real indication as to what line of code was causing it.

My program is progressing nicely though thanks to the help of many of you. It's not all that an exciting program, but it's more of a learning process and I look forward to posting the source and executable to get some feedback - hopefully tomorrow on Monday.


Arabia(Posted 2009) [#26]
joncom2000 (Posted 1 hour ago) #24

I respect to your first post about drawpixmap not showing anything under vista. I have vista32bit and a geforce 8500GT and if I did as you have done above I also dont get an image displayed in some cases.

I think its because by default bmax uses dx7 if you dont set a graphics driver for all drawing and the pixmap would generally to be in BGR or BGRA format to display but loadpixmap doesnt appear to make loaded files this format by default since you may not actually want this format, you can ensure there the format you want by doing a ConvertPixmap to the required format just after its loaded which should solve the problem.

The PNG loader actually loads in RGB format this should be fine but for some reason it appears that drawpixmap under DX isnt drawing them even tho it should be doing a conversion before the drawing operation, it maybe a vista only issue with drivers since it doesnt seem to be a common error posted about :S


Thanks for that. As others have pointed out to me, I should really be using DrawImage etc. instead anyway, so I am happy with that. Took a bit more modifying than I first thought, but it did give me a chance to look at the image functions and their advantages over pixmaps :)


Arabia(Posted 2009) [#27]
Okay, project is nearing completion. It's a very basic game, so I'll post a pic in here instead of the showcase (which puts my graphic skills to shame) LOL

I'll try and finish the game today/tonight with any luck - give it a test run tomorrow on my Vista PC, and all going well, I'll find a free site to host the Win Exe and source code for anyone that is interested.

I may even try to tidy up the code and documentment it properly for anyone who may be interested in it :)