Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / I need help with a 2d tile-based map

Author
Message
Juggleboy
14
Years of Service
User Offline
Joined: 17th Oct 2009
Location:
Posted: 27th Mar 2010 02:06
I am trying to learn how to make a 2d tile-based map and am having trouble drawing it to the screen. I want to store my data in an array name "map". This is what I have so far. Thanks!









Ashingda 27
16
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 27th Mar 2010 07:22 Edited at: 27th Mar 2010 07:22
Dimension your map array:


Then load it up with the data:


Lastly draw to the screen:
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 27th Mar 2010 15:36
Hi Juggleboy,
I've added ashingda's code to your program and changed a few things myself which I will explain. I've replaced a few of the commands with "?" because I don't want to make it too easy for you
Search for "?" and replace them before trying to run the program.
If you aren't already using DarkEDIT I'd highly recommend it. It will be in your DarkBasic folder.


First of all I added a program header. It's nice to look back at old code and see when you wrote it, also if your code gets passed around, your name is on it so you will get credit for your work.

I grouped the image subroutines into one because they do very similar things and are both called at the same time anyway.

Your images are the right size (32x32) but this seems to be by accident.
When you draw a box 0,0,32,32 it is actually 33x33 because 0 is included; if you moved your box across and down it would become clear 1,1,33,33.
GET IMAGE works differently, it lassos the pixels. Imagine a grid running in-between the pixels like when you zoom in MSPaint, now instead of numbering the pixels you number the lines between them. Something like this:

So you see to grab from pixel 0 to 3 we have to do GET IMAGE 0,0,4,1.
Why do I have the Y coordinates 0 to 1? For the same reason: this is a one pixel high line but to include the pixel (0) I have surround it with it's lowest point 0 and it's highest point 1. (I mean highest numerically, this is of course coming down the screen).

I separated out the sections of your program with section header thingies. You don't have to do this yourself but I find it easier when I can see where my subroutines begin.

I added an END after the drawing commands. It's very important to have this command if you are using subroutines (which I'm glad to see you are); if you don't tell the program to end it will run into your subroutines and execute them as if part of the main program, and we don't want that!
Functions are even more sensitive: if the program cursor runs into those your program will crash all together!

I removed a few comments. Writing lots of comments is a good habit, especially when you are still developing your program, but some commands or subroutine calls are self-explanatory and don't need commenting.
For example: "sync on" needs no explanation.

One last thing that might interest you is how colours work in DB.
The RGB command is just a function that converts the numbers you give into a single integer. Did you know DB stores colours as a single integer?
This works by stacking the numbers on top of each other.
I see you already know that the brightest a colour can be is 255. Well to stack these colours we start with 0-255 as all the shades of blue, then we put green on top of that by multiplying it by 256. This works because even 1 green (256) is is higher than blue can ever be (255) so we wont get the two mixed up. To put red on top of that we find the multiply it by 65536 (256*256) so 1 red is higher than 255 blue and 255 green put together, and we will never get any of them mixed up.
For example rgb(255,255,255) is the same as 255*65536 + 255*256 + 255
Try it yourself.
This is why I knew I could turn rgb(0,0,255) into 255 because blue is the only colour there so there's no need for RGB. Also black is simply 0 because we'd get 0 from multiplying anyway.

"With games, we create these elaborate worlds in our minds, and the computer is there to do the bookkeeping." - Will Wright
Ashingda 27
16
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 27th Mar 2010 18:42
Quote: ""?" "

LOL


Quote: "if your code gets passed around, your name is on it so you will get credit for your work"

Ahh I never though about that!
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 1st Apr 2010 03:12
@OBese87

You actually forgot to add "edited by : OBese87" in your title! As a professional code mechanic this is a fatal mistake for which you shall die for!

(j/k)

Sorry, I'm quite useless today...

TheComet

Login to post a reply

Server time is: 2024-04-25 14:11:42
Your offset time is: 2024-04-25 14:11:42