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 Professional Discussion / Get image from another image?

Author
Message
Goz3rr
16
Years of Service
User Offline
Joined: 27th Mar 2010
Location:
Posted: 30th Apr 2011 17:23 Edited at: 30th Apr 2011 17:26
Basically, i got this 512x512 image that has 1024 16x16 squares on it.
Now what i want to do, is specify a size (1x1 = 16x16 pixels, some textures will be 32x32 instead of 16x16 so these have a size of 2x2 squares). And a number, where the first line is 0-15, second line is 16-31 etc. and i already got this code:



And then a texture would be created like:
dirt = LOAD_TEXTURE(0,1,1)
^ would load the first image, top left, in the file, dirt is the id of the image holding the texture

I've attached the terrain.png with 2 tiles filled in.

...
Jimmy
22
Years of Service
User Offline
Joined: 20th Aug 2003
Location: Back in the USA
Posted: 30th Apr 2011 19:15
I understand why you would want to use memblocks, and I almost went the same route until I saw that SvenB's Image Kit plugin has the IK Copy Image command, which, if you pass it the right parameters, allows you to copy just sections of an image, rather than the entire thing. It preserves transparency and is pretty fast to boot.

Example:




May 10th / Geek Culture / Jerico2day-OBese87 / VOTE
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 30th Apr 2011 19:33
You can also do something similar with DBPro's rendering to images (although it's a little fiddly), or with my own plug-ins too, so there's lots of choice.

I used 32x32 images here simply so that I could fit the numbers entirely within a cell:


Goz3rr
16
Years of Service
User Offline
Joined: 27th Mar 2010
Location:
Posted: 30th Apr 2011 19:47
Thanks jimmy, i've been eager with non TGC plugins, but ill have a look into image kit as it definitely looks interesting!

Thanks for the snippet IanM, but it looks like that draws to the screen and then gets a part from it, and that is something i don't really want.

...
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 30th Apr 2011 20:04
If it looks like that, then you're reading the code incorrectly. The only place it's using GET IMAGE is when I build the numbered image - you'd replace that whole function with a single LOAD IMAGE command.

The important bits are the DRAW TO IMAGE, PASTE IMAGE, and then switching rendering back to the display with DRAW TO BITMAP - right in the centre of the code.

Goz3rr
16
Years of Service
User Offline
Joined: 27th Mar 2010
Location:
Posted: 30th Apr 2011 20:31
Oh, i knew (and saw) that there was such a draw to image command. My fault!

...
Goz3rr
16
Years of Service
User Offline
Joined: 27th Mar 2010
Location:
Posted: 30th Apr 2011 20:36 Edited at: 30th Apr 2011 20:48
Another problem, IK Copy image takes two points, the second will be just the first + size, the y of the first i got like:
if num < 15 then
y = 0
else if num < 31 then
y = 16
etc.

How would i get the x? :\

Edit: full code, probably could be done better with a for loop but whatever.


This (should) return the y coord for a number. (Between 0 and 1024, the max tiles atm)

Edit: I'll just do the same for x i guess.. lol

...
Jimmy
22
Years of Service
User Offline
Joined: 20th Aug 2003
Location: Back in the USA
Posted: 30th Apr 2011 21:02
What I would do for that is a little math:

y = (num/16)*16

Because you're dividing integers, the compiler is going to round down for you. So if num is 14, it's going to calculate num/16 as 0. 20/16 is 1, and so on.

You can do the same thing with x.

Also, I decided to slap together an image cutting function using memblocks, just for fun Check it oouuutttt:




May 10th / Geek Culture / Jerico2day-OBese87 / VOTE
Goz3rr
16
Years of Service
User Offline
Joined: 27th Mar 2010
Location:
Posted: 30th Apr 2011 21:14
Awesome snipped, once again!

...
Goz3rr
16
Years of Service
User Offline
Joined: 27th Mar 2010
Location:
Posted: 30th Apr 2011 21:16
But the y = (num/16)*16, that divides by 16, then multiplies, basically doing nothing... ?

...
Jimmy
22
Years of Service
User Offline
Joined: 20th Aug 2003
Location: Back in the USA
Posted: 30th Apr 2011 21:23
It would, except that it rounds down the division, so any number between 0 and 15 that gets divided by 16 is going to result in 0.

Ok, in the real world 14/16 is 0.875, which multiplied by 16 is 14 again, but when you jam 0.875 into the integer 'y' DBP is going to round it down to 0, because integers can't handle decimal values.

Does that make sense?


May 10th / Geek Culture / Jerico2day-OBese87 / VOTE
Goz3rr
16
Years of Service
User Offline
Joined: 27th Mar 2010
Location:
Posted: 30th Apr 2011 21:27
Why does 0.875 round to 0 instead of 1 (Normal logic..)

But if it does what you are saying, how would i use this for the x axis? 0 gives 0 with that formula, but 1 too which should be 16 for the x axis..

...
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 30th Apr 2011 21:37
Integer division always truncates the result, or in other words, it always throws away the remainder.

Jimmy
22
Years of Service
User Offline
Joined: 20th Aug 2003
Location: Back in the USA
Posted: 30th Apr 2011 21:42
I guess "rounding" is the wrong word to use. It's not so much rounding as it is ignoring everything after the decimal.

Is it not the same for the x-axis? x<16 = 0, x<32 = 1?


May 10th / Geek Culture / Jerico2day-OBese87 / VOTE
Goz3rr
16
Years of Service
User Offline
Joined: 27th Mar 2010
Location:
Posted: 30th Apr 2011 21:48 Edited at: 30th Apr 2011 21:50
0: x = 0
1: x = 16
2: x = 32
...
16: x = 0
etc.

Wait. Is there no make image?
Ill just create a blank image from a memblock..

...
Jimmy
22
Years of Service
User Offline
Joined: 20th Aug 2003
Location: Back in the USA
Posted: 30th Apr 2011 21:54
Make Image is one of IanM's commands from his matrix utils plugins.


May 10th / Geek Culture / Jerico2day-OBese87 / VOTE
Goz3rr
16
Years of Service
User Offline
Joined: 27th Mar 2010
Location:
Posted: 30th Apr 2011 22:02
Why does

load image "terrain.png",1
load image "blank.png",2
IK Copy image 2,1,x,y,x+xsize,y+ysize

Give a image not exist on the IK Copy image line>

...
Jimmy
22
Years of Service
User Offline
Joined: 20th Aug 2003
Location: Back in the USA
Posted: 30th Apr 2011 22:03 Edited at: 30th Apr 2011 22:06
As for your formula, it was a little tricky, but I got:

x = 16*(num Mod 16)

If you're unfamiliar with Mod, what it does is give you the remainder of the first number divided by the second, so 1 Mod 16 is 1, 24 Mod 16 is 8. No matter how high you go, num Mod 16 will always give you a number between 0 and 15.

Quote: "Why does

load image "terrain.png",1
load image "blank.png",2
IK Copy image 2,1,x,y,x+xsize,y+ysize

Give a image not exist on the IK Copy image line>
"


IK Copy Image creates a new image rather than actually copying to an existing one, so it's just complaining that image 2 already exists.


May 10th / Geek Culture / Jerico2day-OBese87 / VOTE
Goz3rr
16
Years of Service
User Offline
Joined: 27th Mar 2010
Location:
Posted: 30th Apr 2011 22:04
Yea, i know of modulus, its % in C++ but i couldnt find it for db

...
Jimmy
22
Years of Service
User Offline
Joined: 20th Aug 2003
Location: Back in the USA
Posted: 30th Apr 2011 22:06
Quote: "Yea, i know of modulus, its % in C++ but i couldnt find it for db "


Now u hav it


May 10th / Geek Culture / Jerico2day-OBese87 / VOTE
Goz3rr
16
Years of Service
User Offline
Joined: 27th Mar 2010
Location:
Posted: 30th Apr 2011 22:07
Any solution to the IK Copy thing? runtime error 502..

...
Goz3rr
16
Years of Service
User Offline
Joined: 27th Mar 2010
Location:
Posted: 30th Apr 2011 22:09
O shiz, didnt read reply that it makes a new image. Testing..

...
Goz3rr
16
Years of Service
User Offline
Joined: 27th Mar 2010
Location:
Posted: 30th Apr 2011 22:10
id = FREE_IMAGE()
IK Copy image id,1,x,y,x+xsize,y+ysize

Still gives a image doesn't exist.

...
Goz3rr
16
Years of Service
User Offline
Joined: 27th Mar 2010
Location:
Posted: 30th Apr 2011 22:13
Okay this really makes no sense, moving the load image into the function makes it work fine..



...
Goz3rr
16
Years of Service
User Offline
Joined: 27th Mar 2010
Location:
Posted: 30th Apr 2011 22:13
God i'm stupid, i did LOAD_TEXTURE before load image..

...
Jimmy
22
Years of Service
User Offline
Joined: 20th Aug 2003
Location: Back in the USA
Posted: 30th Apr 2011 22:14
That's strange... the only thing I could think is that maybe image 1 is empty? Dunno, tho


May 10th / Geek Culture / Jerico2day-OBese87 / VOTE
Goz3rr
16
Years of Service
User Offline
Joined: 27th Mar 2010
Location:
Posted: 30th Apr 2011 22:16 Edited at: 30th Apr 2011 22:16
I love you both guys <3

I'll give you credit if my game ever finishes.

@Jimmy: I called the custom LOAD_TEXTURE function before loading image 1.

...
Jimmy
22
Years of Service
User Offline
Joined: 20th Aug 2003
Location: Back in the USA
Posted: 30th Apr 2011 22:21
Yeah! time!


May 10th / Geek Culture / Jerico2day-OBese87 / VOTE

Login to post a reply

Server time is: 2026-07-11 12:20:48
Your offset time is: 2026-07-11 12:20:48