Draw image with a solid color

BlitzMax Forums/BlitzMax Beginners Area/Draw image with a solid color

mothmanbr(Posted 2007) [#1]
Remember in those old 8-bit and 16-bit games where, for example, when you hit an enemy, his sprite changed to a solid red color then faded back to the normal colors? Is there any easy way to do this?

All I could think of is creating a new image that is just a white "shadow" of the sprite, and drawing it on top of the main one using setalpha.


Perturbatio(Posted 2007) [#2]
This can almost definitely be optimised but...




Dreamora(Posted 2007) [#3]
you could as well make an "object class" which has 2 images actually in. the regular one and an effect image. the effect image is a grayscale version of the regular one (can be computed on load through similar way as above).

What you then do on an effect is draw the regular image and set blend to alphablend afterwards with an alpha of 0.5 - 0.7.
Now you set the color to the desired one (can change over time) and draw the effect image above the regular one.

Nice blend effect.

Unless you naturally don't want the original colors to be visible at all then you don't draw the regular one and just the effect picture :)


ImaginaryHuman(Posted 2007) [#4]
SetBlend LightBlend
SetColor $FF,$00,$00

then draw an all-white version of the object over the top of the full-color one that you already drew.


mothmanbr(Posted 2007) [#5]
Perturbatio: Thanks, I'll try it out and see how it goes.

Dreamora and AngelDaniel: Thanks, but that kinda was my original plan already. I just thought maybe there was a better way to do it :)

Edit: Just a bug I think, I tried it out with an image from my game, and it draws a single column of pixels at the end instead of the original position. It is swapped from somewhere else in the image. Also a question, how can I set it to work with different mask colors?


tonyg(Posted 2007) [#6]
This includes source from sswift for both gl/dx


mothmanbr(Posted 2007) [#7]
Perturbatio: I did some research and testing, and I modified your code a bit :P It works flawlessly here for me, I'm going to post here to make sure I didn't do anything wrong, or if anyone has any idea to optimize it.



Img is the object's main image, and Shape is the all-white image. Working flawlessly so far.


Perturbatio(Posted 2007) [#8]
not so much an optimization, but if you change Img to be a function parameter and return the result of the LoadImage function rather than assigning it directly, you have yourself a reusable function

CreateShapeFromMask:TImage(img:TImage)()
End Function

myPlayer.DamageImage = CreateShapeFromMask(playerImage)
Monster1.DamageImage = CreateShapeFromMask(monsterImage1)

(actually code may vary)


mothmanbr(Posted 2007) [#9]
Good idea :) If I need this outside the building class, I'll change it.


tonyg(Posted 2007) [#10]
@mothmanbr, I don't understand why you're not using SSwift's code from the link I gave :

<edit> updated for 1.24


mothmanbr(Posted 2007) [#11]
tonyg, I don't understand why either :P I'll test it today, must be way better.

Edit: I don't know anything about DX or GL, where do I declare this "PrimaryDevice" and what should it be? Or maybe do I need to import a module?


tonyg(Posted 2007) [#12]
Ooops. Sorry Bmax core code has changed since then so I have updated the code above to work on 1.24


mothmanbr(Posted 2007) [#13]
Awesome, thanks