Author Topic: ModulateAlpha for sprites on a specific layer  (Read 11383 times)

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
ModulateAlpha for sprites on a specific layer
« on: 2008-03-09, 08:00:48 AM »
with some custom code i could change the alpha of tiles an a specific layer.
how would i start if i wanted to do the same for sprites?

i don't find the thread right now, here is the code from back then:

Code: [Select]
          int overrideModulation = -1;
         
       if (this is Spiel_Map.inventory_bg_Lyr)
          {
             overrideModulation = new ColorValue(255,255,255,Counter.AlphaMod2.CurrentValue).ToArgb();
          }


           for (int x = nStartCol; x <= EndCol; x++)
          {
             int[] SubFrames = GetTileFrame(x,y);
             for (int nFrame = 0; nFrame < SubFrames.Length; nFrame++)
             {
               Frame f = m_Frameset[SubFrames[nFrame]];
                spr.Transform = Matrix.Multiply(f.Transform, Matrix.Translation(
                   x * nTileWidth + CurrentPosition.X + ViewRect.X,
                   y * nTileHeight + CurrentPosition.Y + ViewRect.Y, 0));
              spr.Draw(f.GraphicSheetTexture.Texture, f.SourceRect, Vector3.Empty, Vector3.Empty, overrideModulation==-1?f.Color:overrideModulation);
             }
         }
      }

just in case: i know i could do it without creating custom code.

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: ModulateAlpha for sprites on a specific layer
« Reply #1 on: 2008-03-09, 09:21:04 AM »
You'll want to find the other points where spr.Draw is called and insert the overrideModulation in there as well.  There's one inside the if (Injected != null)  block above the posted code, and one below it in the while (Injected != null) block.
Edward Dassmesser

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: ModulateAlpha for sprites on a specific layer
« Reply #2 on: 2008-03-09, 09:38:01 AM »
thanks, i'll try that.
but here is another question about alpha.

i create some dynamic sprites, which alpha shall decrease. i made this rules:
do =
value: ModulateAlpha-10
output: ModulateAlpha


but the sprite doesn't become invisible, instead it flickers. i do not understand why.

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: ModulateAlpha for sprites on a specific layer
« Reply #3 on: 2008-03-09, 06:29:49 PM »
After it gets to 0, it probably loops back to 255.  You have to make it stop subtracting if the result would be less than 0.

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: ModulateAlpha for sprites on a specific layer
« Reply #4 on: 2008-03-10, 06:30:31 AM »
i think you are right, the number is looping. i know how i can prevent it, but there is another problem. it loops very fast. it seems really that it decreases faster than my frame rate, is that possible?
anyway, i can't use counters (because they are many dynamic objects) to slow it down, and i can't use -0.1 because it accepts only whole numbers.
i had just the idea to use an expression for natural numbers, decreasing the alpha by 0.1 and round it up, but i realized right now it won't work.

i need to slow down the decreasing of modulateAlpha, help!

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: ModulateAlpha for sprites on a specific layer
« Reply #5 on: 2008-03-10, 07:14:47 AM »
Use a counter that takes numbers up to 2550, then divide by 10 when applying the value in the code.  Something like:

overrideModulation = new ColorValue(255,255,255,Counter.AlphaMod2.CurrentValue/10f).ToArgb();

(notice the f after 10 to ensure floating point division for better accuracy).  You could also make a floating point value in the code, so that you don't have to worry about a counter in between, but that's a bit more difficult to explain.
Edward Dassmesser

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: ModulateAlpha for sprites on a specific layer
« Reply #6 on: 2008-03-10, 07:19:37 AM »
do you mind to explain the other suggestion? i don't think i can use the counter, because it can happen that dozens of these sprites are on the screen at the same time. one counter would make them have all the same alpha, right?

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: ModulateAlpha for sprites on a specific layer
« Reply #7 on: 2008-03-10, 07:49:21 AM »
Oh, I though this was still the same situation where the entire layer was being drawn this way.

Create parameters called AlphaFloat and init.  Then create rules like this

if init == 0
  do init = 1
  do AlphaFloat = 2550
do AlphaFloat = AlphaFloat - 1
do ModulateAlpha = AlphaFloat / 10


The rules would be if ==, do =, do = (end if), do -, do /
Edward Dassmesser

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: ModulateAlpha for sprites on a specific layer
« Reply #8 on: 2008-03-10, 08:33:09 AM »
thank you.
your script works. but now that everything is running slower, i can clearly see that ModulateAlpha is behaving really strange. instead of being 245 when AlphaFloat is 2450, it's -13 (or another silly number down from 0). i don't understand this. As long as the dynamic sprite lives, it's ModulateAlpha decreases in negative number room.
before that, as everything was faster, it was sometimes at -120 or so.

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: ModulateAlpha for sprites on a specific layer
« Reply #9 on: 2008-03-10, 11:26:27 AM »
hm, just now i thought i had caught the error. i always write "do expression-10 = expression" instead of
do - 
expression (value 1)
1              (value 2)
expression (output)


but i don't think that matters. btw, there is no way to multiply or divide at the do-rules (only minus, plus and the rest). why?

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: ModulateAlpha for sprites on a specific layer
« Reply #10 on: 2008-03-10, 12:29:10 PM »
Well, I tested out what I said, and it works just fine.  Are you sure there aren't other rules that also modify ModulateAlpha that conflict with this?

On a side note, you probably don't need all that.  Just decreasing ModulateAlpha was slow enough to work just fine.  The problem is probably that some other part of the system is changing the value incorrectly, making it flicker pseudorandomly.
Edward Dassmesser

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: ModulateAlpha for sprites on a specific layer
« Reply #11 on: 2008-03-10, 02:14:08 PM »
i think i figured it out now. but the reason for that i don't understand.
if i execute the rule
copy 2450/10 to ModulateAlpha
the value ModulateAlpha gets is NOT 245. it is -10!
don't ask my why, but i debugged every little thing in this mini-script and that must be what happens.
(i copy the SAME value into another parameter and debug that: there is 245, so that works.)

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: ModulateAlpha for sprites on a specific layer
« Reply #12 on: 2008-03-10, 03:23:24 PM »
I'm really not sure what's going on here.  Just throwing out ideas:

There must be SOMETHING changing the value of ModulateAlpha at some point if you really do have do ModulateAlpha = 2450/10 and it changes between then and when you show the debug value.

Possibly you have simply typed in the /10 part wrong?

Look at the .cs file directly.  Look for that bit in the ProcessRules function (or whatever it's called).  Does it really look correct?
Edward Dassmesser

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: ModulateAlpha for sprites on a specific layer
« Reply #13 on: 2008-03-10, 04:10:19 PM »
Code: [Select]
// if parameter init == 0
         if (((init) == (0)))
         {
            // set parameter AlphaFloat to 2550
            AlphaFloat = 2550;
            // set init to 1
            init = 1;
         }
         // decrement AlphaFloat
         AlphaFloat = ((AlphaFloat) - (1));
         // output AlphaFloat/10
         FloatDividedByTen = AlphaFloat/10;
         // copy AlphaFloat/10 to Alpha
         ModulateAlpha = AlphaFloat/10;

well, there are a few rules additionally, but they are either suspended or something like: reactToSolid, and move or so.

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: ModulateAlpha for sprites on a specific layer
« Reply #14 on: 2008-03-10, 05:23:04 PM »
Are there more rules after the last line you have there?  Try doing the same thing in the sample project, and see if it works for you.  If it does, then there's definitely some problem with the project itself.
Edward Dassmesser