Not logged inGosu Forums
Forum back to Help Search Register Login
Up Topic Gosu / Gosu Exchange / Set image transparency
- - By il mietitore Date 2011-09-26 18:01
Hi to everyone ;)

I was wondering: is there a way to set the transparency of an image? I'm trying to have a sort of glow effect, but with no transparency that's pretty hard :S
Parent - - By lol_o2 Date 2011-09-26 19:01
Draw this image using color with lower alpha value, like 0x80ffffff
Parent - - By il mietitore Date 2011-09-26 19:45
cool, this seems to work :P

is there a way i can insert a variable inside the alpha value? i mean: this isn't working, but if i had the possibility to write something like this:

@immagini_logo.draw(0, 0, 0, factor_x=1, factor_y=1, color=0x(ff-$n)ffffff)

with $n floating between 00 and ff, i could have my glowing image. But this code just doesn't seem to work :\
Parent - - By Dahrkael Date 2011-09-26 20:18
use the Color class instead of a fixnum
Parent - - By il mietitore Date 2011-09-26 22:23
uhm. sorry but, as you can see, I am definitely a noob. How can I use a class to modify the color of the image? you mean using Gosu::Color in someway?
Parent - By lol_o2 Date 2011-09-27 05:16
@immagini_logo.draw(0, 0, 0, 1, 1, , red , green , blue))

And you don't need to write 'factor_x='  or   'color='   each time.
Parent - - By RunnerPack Date 2011-09-26 23:13

@immagini_logo.draw(0, 0, 0, 1, 1, ((0xFF - $n) << 24) + 0xFFFFFF)

(Or use the the Color class like Dahrkael said).
Parent - - By erisdiscord Date 2011-09-27 15:41
-1 for being the sort of hack I'd resort to. :D
Parent - - By RunnerPack Date 2011-09-29 04:30

I only did it to show how close his code was to working. He's obviously a newbie (no offense, Italian reaper dude) and I knew there would be other people spoiling him by coding it for him "the right way". This way, I thought he might at least learn something on his own, even if it's not directly "properly" applicable here...
Parent - By erisdiscord Date 2011-09-29 04:32
Hopefully the jocular nature of my comment came through! I have done it that way and probably would again. :D
Parent - By il mietitore Date 2011-09-29 23:39
no offense, I surely am a newbie ;)

(i do these things for hobby, i never had some serious programming course... i do what i can XD)
Parent - - By il mietitore Date 2011-09-27 16:15
ok, this works, but i can't understand why.

what's that << 24 ? :S
Parent - By RunnerPack Date 2011-09-29 04:34
It means "shift a number 24 bits to the left". It's shorthand for "multiply by 16777216". It seems to be a bit beyond your skill level at this point, but if you keep learning about programming you'll eventually get to binary math and it'll make sense. Just do it the "right" way using Gosu::Color.
Parent - - By erisdiscord Date 2011-09-27 15:46
It's also worth a note that you probably shouldn't be using the $ sigil (as in $n), because that indicates a global variable. Just use a bare name for local variables.

You will encounter strange bugs later using global variables like that if you use the same name in more than one place. C:
Parent - - By il mietitore Date 2011-09-27 16:25
uhm. but if i use a local variable, then i can use it in an other method?

for example, in this case, now we have:

def initialize
    $glow_logo = 0
    $glow_logo_boolean = false

def update
    if $menu == true then
      if $glow_logo_boolean == false then
        $glow_logo = $glow_logo + 2
        if $glow_logo == 200 then
          $glow_logo_boolean = true
        $glow_logo = $glow_logo - 2
        if $glow_logo == 0 then
          $glow_logo_boolean = false

def draw
    if ($menu == true) then
      @immagini_logo.draw(0, 0, 0, factor_x=1, factor_y=1, ((0xFF - $glow_logo) << 24) + 0xFFFFFF)

I just tried to change $glow_logo_boolean in a local variable, but it doesn't work.
Parent - By erisdiscord Date 2011-09-27 20:20
Well, for variables that need to be shared across methods, use an instance variable. They shouldn't need to be accessible outside of your object.

As for fading in and out, let me see if I can get the math right this time. I wrote an equation that was wrong and kept getting the corrections wrong a while ago because I was on a tablet and couldn't test it!

There we go. 0.5 + Math.sin(Math::PI * Gosu.milliseconds / 1000.0) / 2 should give you a float in the range 0 – 1 inclusive, with a period of one second. If you want toit to go faster or slower, change 1000 to something else—it's basically the period in milliseconds.
Parent - - By erisdiscord Date 2011-09-27 20:36
More usefully:

  def draw
    if @menu
      alpha = (128 + 128 * Math.sin(Math::PI * Gosu.milliseconds / 1000.0)).floor
      @immagini_logo.draw 0, 0, 0, 1, 1, Gosu::Color.argb(alpha, 255, 255, 255)
Parent - - By il mietitore Date 2011-09-27 22:47 Edited 2011-09-27 22:54
that's crazy.

it works but i can't understand what the heck is that >.<

what is PI? and that .floor? O___O

(thanks a lot =D )
Parent - - By lol_o2 Date 2011-09-28 05:58
PI is a math constant. You should know it from school.
.floor removes decimal, like (1.25).floor = 1
Parent - - By il mietitore Date 2011-09-28 12:38
ah, the π!

what i'm trying to understand is: why this equation gives a different result at every refresh? i mean, there are no variables that changes each time... i suppose it's related to that Gosu.milliseconds, because all the other things are fixed >.< what is that Gosu.milliseconds, anyway?
Parent - - By jlnr (dev) Date 2011-09-28 13:16
The current time in milliseconds. The rdoc is ugly right now, but you will still be able to find all that here:
Parent - By il mietitore Date 2011-09-29 19:20
So here it is, now it's all clear.

Well thanks a lot to everyone in here ;) i suppose i'll be back soon, i'll surely find some other curious problem to fight with.

heya ;)
Parent - - By Dahrkael Date 2011-09-28 16:41
i tested the snippet and it crashes after some seconds.

>ruby test.rb

test.rb:13:in argb': Wrong arguments for overloaded method 'Color.argb'. (ArgumentError)
Possible C/C++ prototypes are:
    Gosu::Color Color.argb(Gosu::Color const *self)
    Gosu::Color Color.argb(Gosu::Color::Channel a, Gosu::Color::Channel r, Gosu::Color::Channel g, Gosu::Color::Channel b)
    Gosu::Color Color.argb(std::tr1::uint32_t argb)
  from test.rb:13:in
  from test.rb:18:in `<main>'

>Exit code: 1

Parent - By jlnr (dev) Date 2011-09-28 20:08
The trick is to use 127.5 + 127.5 * ...:)
Parent - - By erisdiscord Date 2011-09-30 03:31
Oops! Wow, that didn't happen for me even after many, many iterations. I had numerous previous versions that did. :D

@jlnr Can we get a constructor that takes normalized floats? I'm much more comfortable with those. C:
Parent - By jlnr (dev) Date 2011-09-30 05:46
I thought about overloading it, but it would be confusing if passing 1.0 and 1 would make a difference. I could add a _f variant for everything on Color, but I am not sure about that.

It may be a bit clearer if you work with normalized floats and then do *255 in the end, though. Now if Color.argb only called .to_i on its arguments...sigh.
Parent - - By jlnr (dev) Date 2011-10-08 11:06
Color now (=in the next release) truncates and wraps all values into 0..255 automatically. That was a painless fix at least. :)
Parent - By erisdiscord Date 2011-10-10 23:08
This is a good change. :D
Up Topic Gosu / Gosu Exchange / Set image transparency

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill