Custom Types and Drawing Images

Blitz3D Forums/Blitz3D Beginners Area/Custom Types and Drawing Images

The3Leaf(Posted 2011) [#1]
Hey all. I'm new to blitz3d, but I'm loving it. I learned a bit (or alot I don't know) about custom types and decided to practice them by making a 2d mapper to help me build 2d games. Well as far as I know you can have multiple pointers with the same name....or multiple types with the same pointer (guess I don't know that much after all lol). I seem to be having problems with this and drawing images to the screen. I'll give you an example.

sand01 = LoadImage("sand.png")

Type terrain
field X
field Y
field image$
end type

If MouseHit(1) then
s.terrain = new terrain
s\X = MouseX()
s\Y = MouseY()
s\image = "sand01"
sandexists = 1
End If

If sandexists = 1 then DrawImage(s\image,s\X,S\Y)





This isn't exactly how my code looks, but close enough so you can see what I'm trying to do. What this does is draw one image at the mouse position (with others under is my guess). I'm not surprised this didn't work the way I planned, but my question is what is the write way to do this? I've tryed many other things, but they were all the same a principle and all had unsatisfying results. Thanks for reading and any help is welcomed, I have alot to learn.



I solved my problem with a For loop lol and now I feel silly. If somone can tell me how to remove this post it would be appreciated or if you would like to know how I solved my problem just post.

Last edited 2011


Yasha(Posted 2011) [#2]
Hum well you solved your problem before I finished answering...

Anyway, you can't delete your posts on this forum.


Unrelated to your question: You can't access variables using strings - they're not related concepts. As Blitz3D is a compiled language, variables are reduced to simple memory offsets at runtime and can't be manipulated directly in that way. What you're actually drawing there is whatever image handle gets cast out of the string "sand01" - which will be 0, as that character string doesn't represent a valid number.

Last edited 2011


The3Leaf(Posted 2011) [#3]
Yeah sorry about that. To what you said though, isnt a string a variable? And are you saying the image wont load? Cause the image loads fine. Sorry you just have me confused, thanks for posting though.


And since I cant remove this post I'll just post how I solved my problem in case anyone has the same problem I had.


Just add this to what I posted above and it works. As long as you get rid of the whole "If sandexists " part.
for s = Each terrain
DrawImage(s\image,s\X,s\Y)




Yasha(Posted 2011) [#4]
isnt a string a variable? And are you saying the image wont load?


First thing is terminology: a variable is a named slot somewhere in the program's memory (you obviously know this!). A string is a value, something that can be held by a variable. Blitz3D supports four kinds of values: integers, floats, strings, and pointers (integers also act as pointers for "engine objects" like images and 3D stuff). So if you do this:

Local myVar$ = "foo"
Print myVar


...you will get 'foo' (no quotes) printed to screen. If you do this:

Local myVar$ = "foo"
Print "myVar"


...you will get 'myVar' printed to screen, because in this case it's just a character string value, and has no connection to the variable that happens to be named myVar. Once the program is running, it no longer knows that that particular memory slot was also labelled 'myVar' at compile-time.

So in your first example, you've set the image field of s to "sand01", a character string value. Unless those quotes weren't present in your original program, in which case it would convert the image handle to a string, and store that (so the program would still work as the string was later converted back to a pointer when passed to DrawImage - slow, but not technically wrong).

If those quotes were there and the program worked as expected... erm, I would be very surprised.


The3Leaf(Posted 2011) [#5]
You are exactly right I apologize for not paying attention to what I was doing. In my original program Quotes were not present. I just checked and you are right, I get what your saying. Sorry about that, and thanks for the lesson.