making 'power flags'

Blitz3D Forums/Blitz3D Beginners Area/making 'power flags'

aCiD2(Posted 2003) [#1]
i think they are called power flags are something along those lines. anyhow, in my gui i'd like it so the user can choose a flag/falgs when creating a window

1 - Blank window
2 - Has Title bar
4 - Has minimize

so they could basically do widnow(blah blah, 6) is that possible? if you still dotn get me, look up the documentation for load texture.

acid2


Rob Farley(Posted 2003) [#2]
I know there is a slick way of doing this using binary controls however I can't remember off the top of my head...

An easy way would be

flag=6

if flag-4>0 then flag=flag-4: has minimize
if flag-2>0 then flag=flag-2: has titlebar
if flag-1>0 then flag=flag-1: blank window

That will work... but as I said, you can do it with binary controls I'm sure someone will post after me with this solution.


aCiD2(Posted 2003) [#3]
cheers dr av, i think i'll use that method, but if you get the binary controls method do letme know :)


Perturbatio(Posted 2003) [#4]
You could use AND to test each flag.

a = 1 + 2 + 4

If a And 1 Then Print "1" Else Print "not 1"
If a And 2 Then Print "2" Else Print "not 2"
If a And 4 Then Print "4" Else Print "not 4"
If a And 8 Then Print "8" Else Print "not 8"

WaitKey()

End



ford escort(Posted 2003) [#5]
or simply use bit operations :)
minimize=flag And 1
titlebar=flag Shr 1 And 1
blank   =flag Shr 2 And 1


hope it help :)


soja(Posted 2003) [#6]
You don't need to use bit shifting operations.

This:
blank    = flag And 1
titlebar = flag Shr 1 And 1
minimize = flag Shr 2 And 1

...is effectively the same as this:
blank    = flag And 1
titlebar = flag And 2
minimize = flag And 4

...except with more operations, and not quite so obvious.


MSW(Posted 2003) [#7]

blank    = flag And 1
titlebar = flag And 2
minimize = flag And 4
 




That would only work if you wanted to set a single bit value(and clear out the rest) or to test if a bit has been set.

typicaly the way to use bit flags is to define some constants to test/set them with:

Const blank = 1    ;00000000000000000000000000000001
Const titlebar = 2 ;00000000000000000000000000000010
Const Minimize = 4 ;00000000000000000000000000000100


Then to set the bit value if it hasn't been set yet:

Function setbit%(flag%,bit%)
  
  If NOT flag AND bit then flag = flag OR bit
  
  Return flag
End Function


To specificly clear a bit:

Function clearbit%(flag%,bit%)
  
  If flag AND bit then flag = flag OR bit
  
  Return flag
End Function


To toggle a bit:

Function togglebit%(flag%,bit%)
  
  flag = flag OR bit
  
  Return flag
End Function


To test a bit:

Function testbit%(flag%,bit%)
  
  return flag AND bit

End Function


Then in your code you can just call on the functions:


Flag% = 0


;set the titlebar flag

flag = setbit(flag,titlebar)

;toggle the blank flag

flag = togglebit(flag,blank)

;test if minimize flag has been set

If testflag(flag,minimize) Then Print "Minimize flag has been set!"

;test if both titlebar and blank flags have been set

If testflag(flag,titlebar) And testflag(flag,blank) Then Print "titlebar And blank flags have been set!"

;toggle the blank flag (if off it turns it on, if on it turns it off)

flag = togglebit(flag,blank)

;test if the blank flag is off

If NOT testbit(flag,blank) Then Print "Blank flag is now off!"



Pretty simple...


Warren(Posted 2003) [#8]
I've heard bit testing might work. Anyone know if that's true?


Yan(Posted 2003) [#9]
It's even simpler with functions that work ;op

Graphics 400, 600, 0, 2

Const blank = 1     
Const titlebar = 2  
Const minimize = 4 

Print "blank    = " + Bin$(blank)
Print "titlebar = " + Bin$(titlebar)
Print "minimize = " + Bin$(minimize)
Print

flag = 0
Print "Begin"
Print Bin$(flag)
Print

flag = setbit(flag, titlebar)
Print "Set 'titlebar' bit"
Print Bin$(flag)
Print

flag = togglebit(flag, blank)
Print "Toggle 'blank' bit on"
Print Bin$(flag)
Print

flag = togglebit(flag, blank)
Print "Toggle 'blank' bit off"
Print Bin$(flag)
Print

flag = setbit(flag, minimize)
Print "Set 'minimize' bit"
Print Bin$(flag)
Print

flag = setbit(flag, blank)
Print "Set 'blank' bit"
Print Bin$(flag)
Print

flag = clearbit(flag, titlebar)
Print "Clear 'titlebar' bit"
Print Bin$(flag)
Print

Print "Test 'blank' bit"
Print testbit(flag, blank)
Print 

Print "Test 'titlebar' bit"
Print testbit(flag, titlebar)
Print 

Print "Test 'minimize' bit"
Print testbit(flag, minimize)
Print 

WaitKey()

End


Function setbit(flag, bit)
    Return (flag Or bit)
End Function

Function clearbit(flag, bit)
    Return (flag And ~bit)
End Function

Function togglebit(flag, bit)  
    Return (flag Xor bit)
End Function

Function testbit(flag, bit)
    If (flag And bit) Then Return 1
End Function

Yan


MSW(Posted 2003) [#10]

It's even simpler with functions that work ;op



[Homer Simpson voice]
DOH!
[/Homer Simpson voice]

Thats what I get for starting the New Year celebrations early :P


ford escort(Posted 2003) [#11]
anyway, get the one that fit your need :)