GML Using RGB values in sprites to apply attributes?

S

Smitherino

Guest
Hello everyone! I don't have any code right now, but i'm curious if it's possible to do this with gamemaker. If any of you played the original total war games you'd maybe know about how regions are made? If not it's fine. I'm wondering if i can give RGB values attributes. (Examples below)

R G B
0 0 255 - Water
255 0 0 - Volcano
0 255 0 - Grass

sorta like that where [regions are defined based on the seperate rgb values within an image file]. I sat and thought about it and i'm just curious about where i would start?
 

Attachments

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
The easiest way would be to create a sprite-sized surface (surface_create), draw the sprite to it (surface_set_target -> draw_sprite -> surface_reset_target) and then sample pixels via surface_getpixel (I assume in two nested loops if you want to make objects at positions).
 

sp202

Member
Isn't a shader ideal for this? Draw the image onto three different surfaces with each surface isolating the colours by using a shader to erase other colours and then you can turn the surface into a sprite and do all sorts of things.
 

zbox

Member
GMC Elder
Shaders won't allow you to return the values of each colour.

It is definitely possible, this is how image based heightmaps are done. To build on YA's answer if you are finding surface_getpixel to be too slow (trust me it is) there is a really great asset on the old GMC called Samplers and it is lightning fast and perfect for your purposes. Check it out http://gmc.yoyogames.com/index.php?showtopic=640679&hl=sampler
 

sp202

Member
The shader wouldn't be used to return colour values but rather to convert the map into a single colour channel version which would then be captured onto a surface.
 

zbox

Member
GMC Elder
Yeah but - what would you do with that =P you can't base any game logic off it or have it being anything else other than an inefficient visual. You may aswell just prerender the map to a PNG at that point.
 

sp202

Member
You'd create a sprite from each surface and assign it to an object, it's only being used as a terrain map which I assume would only need to be initialized once.
 

zbox

Member
GMC Elder
Just create the sprite in the first place? If you can't access it in a meaningful way then not much point
 

sp202

Member
This is the way I see it:
- You need a sprite for the sake of collisions, aka game logic.
- To create the sprites, sure you could just draw up the map by hand in layers of terrain types with each layer being a separate sprite but that becomes increasingly troublesome when you've got more than a few terrain types and OP wanted to read them off a single image anyway. My proposal: surfaces for each terrain layer, because you can texture a surface using blend modes and they're generally pretty useful for storing and manipulating images.
- To get the relevant data onto the surfaces from a single texture, a shader seems like one of the fastest methods.
 

TheouAegis

Member
Render the playfield to a surface, save the surface to a file, open the file and copy its contents to a buffer. Then you can just read the buffer.

(if there's a faster way to copy a surface TO a buffer, please let me know)
 
A

Ampersand

Guest
Collisions can be done without sprites or objects. In fact this seems like the perfect application for a tilemap -- make a tilemap, draw your map to a surface, and then do all your pathfinding and collisions with it. In fact there's no need for any more than one object for what he's trying to achieve. And really that would only be to call one script to define your global tilemap. Then you could use objects or different data structures as you please for game elements.
 

zbox

Member
GMC Elder
Render the playfield to a surface, save the surface to a file, open the file and copy its contents to a buffer. Then you can just read the buffer.

(if there's a faster way to copy a surface TO a buffer, please let me know)
buffer_get_surface(), it is also what the samplers I linked use
 

TheouAegis

Member
WT WTF I thought that functions as the two, but I couldn't find it in the help file. Maybe I was just tired. I saw buffer_set_surface and thought it was odd that I couldn't find a get version.
 

zbox

Member
GMC Elder
I looked in the manual too actually just then and it isn't there - undocumented? Works though!
 
Top