'Blinking' the surface

Hello,
at present I am using the surface blinking based on frame counter with code like this:

if ( frame % 3 == 0 )
{
apply_surface( x, y, surface, screen);
}

which ‘blinks’ every third frame.

Now I have the requirement to do some surface blinking in this way: surface has to be blitted for
1 second, then has to dissapear for 1 second , then again to stay on screen 1 second and so forth.

Does anybody knows how to do this?

dekyco

you can on/off every 1024th millisecond:----- Original Message -----
From: dekyco
To: sdl at lists.libsdl.org
Sent: Monday, January 18, 2010 2:56 PM
Subject: [SDL] ‘Blinking’ the surface

Hello,
at present I am using the surface blinking based on frame counter with code like this:

if ( frame % 3 == 0 )
{
apply_surface( x, y, surface, screen);
}

which ‘blinks’ every third frame.

Now I have the requirement to do some surface blinking in this way: surface has to be blitted for
1 second, then has to dissapear for 1 second , then again to stay on screen 1 second and so forth.

Does anybody knows how to do this?

dekyco



SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

if ( timer_milliseconds & 1024 ) on else off----- Original Message -----
From: dekyco
To: sdl at lists.libsdl.org
Sent: Monday, January 18, 2010 2:56 PM
Subject: [SDL] ‘Blinking’ the surface

Hello,
at present I am using the surface blinking based on frame counter with code like this:

if ( frame % 3 == 0 )
{
apply_surface( x, y, surface, screen);
}

which ‘blinks’ every third frame.

Now I have the requirement to do some surface blinking in this way: surface has to be blitted for
1 second, then has to dissapear for 1 second , then again to stay on screen 1 second and so forth.

Does anybody knows how to do this?

dekyco



SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

stoiko wrote:

im replying to myself but… :
if ( timer_milliseconds & 1024 ) else

Thank you for your reply stoiko. This pseudocode is however little unclear to me.
Did you mean something like modulus:

if ( timer_millisecond % 1024 )

or logic AND

if ( timer_millisecond && 1024)

?

dekyco

binary and – single &On Mon, Jan 18, 2010 at 5:33 PM, dekyco wrote:

stoiko wrote:

im replying to myself but… :
if ( timer_milliseconds & 1024 ) else

Thank you for your reply stoiko. This pseudocode is however little unclear
to me.
Did you mean something like modulus:

if ( timer_millisecond % 1024 )

or logic AND

if ( timer_millisecond && 1024)

?

dekyco


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Stoiko is using a ‘bitwise &’ trick. Every 1024 milliseconds (roughly a
second), the 10th bit will flip in binary representation. Using '& 1024’
gives you that bit. Using this in a condition gives true iff that bit is 1.
So, the bit will be on for about one second, then off for about one second.

A more intuitive way to do it (less efficient, but also more accurate) would
be to hold a boolean (or Uint8 or something in C) that you change every
second.

Uint32 last_time = SDL_GetTicks();
bool drawMe = true;
Uint32 blinkTime = 1000; // A second

bool done = false;
while(!done)
{
Uint32 this_time = SDL_GetTicks();
if(this_time - last_time >= blinkTime)
{
last_time = this_time;
drawMe = !drawMe;
}

if(drawMe)
    ; // Do drawing

}

I left out all the other stuff, like input, throttling, screen clearing and
flipping, etc.

Jonny DOn Mon, Jan 18, 2010 at 10:34 AM, Stoiko Todorov <stoiko.todorov at gmail.com>wrote:

binary and – single &

On Mon, Jan 18, 2010 at 5:33 PM, dekyco wrote:

stoiko wrote:

im replying to myself but… :
if ( timer_milliseconds & 1024 ) else

Thank you for your reply stoiko. This pseudocode is however little unclear
to me.
Did you mean something like modulus:

if ( timer_millisecond % 1024 )

or logic AND

if ( timer_millisecond && 1024)

?

dekyco


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Bit-wise & operaetor is equivalent to a modulus and a division
operation. Using simplicity I’ll use zero-padded binary numerals:

00 & 10 == 00
01 & 10 == 00
10 & 10 == 10
11 & 10 == 10

This operation and operand (&10) is the same as using integer division
to “shift off” the lower binary digits (or “bits”) and, assuming we
the only bits we have left are the ones we want, adjusting the
remaining bit back into place with multiplication:

00 / 10 * 10 == 00
01 / 10 * 10 == 00
10 / 10 * 10 == 10
11 / 10 * 10 == 10

If we have more digits, modulus is required, but could have been used
above anyway:

000 / 010 % 001 * 010 == 000
001 / 010 % 001 * 010 == 000
010 / 010 % 001 * 010 == 010
011 / 010 % 001 * 010 == 010
100 / 010 % 001 * 010 == 000
101 / 010 % 001 * 010 == 000
110 / 010 % 001 * 010 == 010
111 / 010 % 001 * 010 == 010

Learn the following identities. I shall use two asterisks (**) to
signify exponentiation, as the carot (^) in C is the XOR operator.

Let p be any positive integer:
(x/(2p)%(20)*(2**p)) == (x&p)

So, for any positive integer power of 2 “n”:
(x/n%1*n) == (x&p)

Of course, these identities only describe the & operator when one of
the operands is a power of two. You can have more complex stuff like
this:

11111111 & 11111111 == 11111111
11111111 & 00000000 == 00000000
11111111 & 00011000 == 00011000
00001111 & 01010101 == 00000101
10101010 & 01010101 == 00000000

Have funOn Mon, Jan 18, 2010 at 10:33 AM, dekyco wrote:

if ( timer_milliseconds & 1024 ) else

Did you mean something like modulus:


http://codebad.com/

Thank you very much,Donny! What Stoiko wrote did the trick:

if ((msec & 1024) && m_message != NULL)
{
apply_surface( m_x, m_y, m_message, screen );
}

and is working as expected.Thank you for additional info,I was not aware of this:)

dekyco

Be aware that this event shifts in time and cannot be accompanied with events
using real time as in a minute this method is more than a second off of the
real time.

This means that in a minute you get 57 or 58 swaps not 59 like you would if
you used a second as the interval.On Monday 18 January 2010 23:00:44 dekyco wrote:

Thank you very much,Donny! What Stoiko wrote did the trick:

if ((msec & 1024) && m_message != NULL)
{
apply_surface( m_x, m_y, m_message, screen );
}

and is working as expected.Thank you for additional info,I was not aware of
this:)

dekyco