Posts: 153
Threads: 30
Joined: Aug 2001
I have a function that detects if a key is pressed and it returns a value. How do I make the function return that UP and LEFT were pressed? I've seen in other people's code the use of AND OR or something, so could someone help me. DOWN = 1 RIGHT = 2 UP = 3 LEFT = 4.
am: "Where should I put this thing so that it doesn't hurt anyone we know or care about?"
Max:"Out the window, Sam. There's nobody but strangers out there."
Posts: 2,020
Threads: 24
Joined: Jun 2002
you need to use separate digits of any system.
for instance, if you wanted to use arabic numerals:
1010 - one thousand and ten.
but for total efficiency, use binary:
1010 - ten.
to read a binary digit:
is_it_on = x and (2^digit - 1) - qbasic
is_it_on = x & (exp(2, digit) - 1); - c/c++ [i think]
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Posts: 153
Threads: 30
Joined: Aug 2001
I made the function return a string and then I parse the string for commands that it gave. But it will still only take one command at a time. Here's code:
Code:
std::string s = getInput();
if (!s.find("RIGHT")) {
xv+= ACCEL;
}
if (!s.find("UP")) {
y--;
//yv-= ((ACCEL * 255) * YFRICTION);
}
Code:
std::string cBubble::getInput()
{
std::string s;
if (key[KEY_ESC]) return "ESC";
if (key[KEY_RIGHT]) s+="RIGHT";
if (key[KEY_LEFT]) s+="LEFT";
if (key[KEY_DOWN]) s+="DOWN";
if (key[KEY_UP]) s+="UP";
return s;
}
am: "Where should I put this thing so that it doesn't hurt anyone we know or care about?"
Max:"Out the window, Sam. There's nobody but strangers out there."
Posts: 6,419
Threads: 74
Joined: Mar 2002
I would do something like this:
Code:
UP = bit 1
DOWN = bit 2
LEFT = bit 3
RIGHT = bit 4
Then I just...
Code:
pressed = 0;
if (key[KEY_UP])
pressed = pressed | 1;
else
if (key[KEY_DOWN])
pressed = pressed | 2;
if (key[KEY_LEFT])
pressed = pressed | 4;
else
if (key[KEY_RIGHT])
pressed = pressed | 8;
return pressed;
Then, to check if left, for example:
Code:
if ( (pressed & 4) == 4 )
// left was pressed.
if ( (pressed & 5) == 5)
// left and up were pressed ( 4 + 1 = 5, bits 1 _AND_ 3).
Bit #n's power of two is calculated this way (n ranges from 1 to...):
Code:
power_of_two = 1 << (n-1)
Posts: 153
Threads: 30
Joined: Aug 2001
Awesome. That's exactly what I wanted. I got it running, but didn't use that last part. What is that and what does it do?
Quote:Bit #n's power of two is calculated this way (n ranges from 1 to...):
Code:
power_of_two = 1 << (n-1)
and if I wanted to add in other options to it, I just add in another number 2x the size of the last one?
am: "Where should I put this thing so that it doesn't hurt anyone we know or care about?"
Max:"Out the window, Sam. There's nobody but strangers out there."
Posts: 6,419
Threads: 74
Joined: Mar 2002
It calculates a power of two without multiplying.
When you are dealing with bits 1, 2, 3 and stuff you will remember the values (1,2,4,8...) but which number will you use to AND/OR to modify bit #27?
This is used if the bit # is a variable. To print a number (unsigned short 16 bits integer) in binary:
Code:
for (i=16;i>=1;i++)
{
mask = 1<<(i-1);
printf("%i", (number & mask) / mask);
}
Posts: 3,288
Threads: 167
Joined: Nov 2001
<< =Shift Left
>> = shift Right
Posts: 1,166
Threads: 62
Joined: Apr 2003
Quote:<< =Shift Left
>> = shift Right
isn't >>> the right shift that doesn't wrap?
am an asshole. Get used to it.
Posts: 530
Threads: 17
Joined: May 2002
erm, afaik >> doesnt wrap. It just kicks off the extra bits.
Posts: 3,288
Threads: 167
Joined: Nov 2001
Quote:relsoft Wrote:<< =Shift Left
>> = shift Right
isn't >>> the right shift that doesn't wrap?
Shr and Shl does not wrap
there is a Rotate Bits function in ASM. Dunno the C flavor though.