|
3 Feb 2003, 21:22
|
#1
|
NEWSBOT
Join Date: Dec 2000
Location: The enby cave!
Posts: 4,872
|
i should understand this but i dont
scenario :
lets imagine we are writing an ASCII version of tetris in C/C++ (i would suggest java, but i think i know how to do this in that)
now i can handle the grid etc fine, but theres one thing thats getting on my nerves,
every so often, the block falls downwards
you could just move the block down after a time period etc, BUT you also need to handle keyboard input to rotate/move the block during that time. The only way i can think of doing this in waiting say 10 loops before moving the block down, but this then means that only 9 or so keyboard inputs can be handled each 'move' turn.
now in java, it'd be a case of two threads, one to handle input, one to move the blocks down, but how would you do this in C/C++ ?
and also, whats the best way to read and handle keyboard input from individual keys ?
__________________
[20:27:47] <nodrog-aawy> **** i think my housemate just caught me masturbating
[11:25:32] <idimmu> you are a little piggy arent you
[13:17:00] <KaneED> i'm so closet i'm like narnia
__________________
Pretty parks and funky scrap metal things here
|
|
|
3 Feb 2003, 22:12
|
#2
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
Are you using win32?
If so, this can handle multiple keys at once:
Code:
bool keys[256]; //global
LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window
UINT uMsg, // Message For This Window
WPARAM wParam, // Additional Message Information
LPARAM lParam) // Additional Message Information
{
RECT rect,rect2;
POINT mousePos;
switch (uMsg) // Check For Windows Messages
{
case WM_KEYDOWN: // Is A Key Being Held Down?
{
keys[wParam] = TRUE; // If So, Mark It As TRUE
//g.keyboard(wParam);
return 0; // Jump Back
}
case WM_KEYUP: // Has A Key Been Released?
{
keys[wParam] = FALSE; // If So, Mark It As FALSE
return 0; // Jump Back
}
}
}
And you can thead too in C/C++, I just don't know how, but I heard it's relatively easy.
|
|
|
3 Feb 2003, 22:20
|
#3
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
Depends what kind of interface you're using. You say ASCII, but anything can do ASCII.
Message passing:
- Timer callbacks (eg. Win32, GTK)
POSIX-a-likes:
- alarm() and signal(SIGALRM, ...)
- fcntl(..., F_SETSIG, ...)
- poll, select, /dev/poll
Any:
- Threads
|
|
|
3 Feb 2003, 22:21
|
#4
|
NEWSBOT
Join Date: Dec 2000
Location: The enby cave!
Posts: 4,872
|
hmm, would that code work in a win32 console window though ? having a handle on a console window seems odd.
found a nice thing on C++ threads though at
http://www.linuxselfhelp.com/HOWTO/C...-HOWTO-18.html
probably will have to re-write and OO the whole thing though, which im not sure i can be arsed doing.
__________________
[20:27:47] <nodrog-aawy> **** i think my housemate just caught me masturbating
[11:25:32] <idimmu> you are a little piggy arent you
[13:17:00] <KaneED> i'm so closet i'm like narnia
__________________
Pretty parks and funky scrap metal things here
|
|
|
3 Feb 2003, 23:29
|
#5
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
If you actually mean a Win32 console, using AllocConsole etc? If so you could use WaitForMultipleObjectsEx. If you're using <iostream>, you're probably looking for something more portable so I'd recommend a C++ Boost library. Or Win32 threads if it's just for Win32.
|
|
|
3 Feb 2003, 23:38
|
#6
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
Also
Quote:
Originally posted by MT
I wish you wouldnt say C/C++ when you actually mean C++ :/
|
|
|
|
3 Feb 2003, 23:57
|
#7
|
NEWSBOT
Join Date: Dec 2000
Location: The enby cave!
Posts: 4,872
|
well portability isnt really an issue, as i only want it to work on my pc for now. I just wanted to make it :/
and i mean C/C++ damnit!
i'd rather do it in C, but will use ++ if necessary, heh
__________________
[20:27:47] <nodrog-aawy> **** i think my housemate just caught me masturbating
[11:25:32] <idimmu> you are a little piggy arent you
[13:17:00] <KaneED> i'm so closet i'm like narnia
__________________
Pretty parks and funky scrap metal things here
|
|
|
4 Feb 2003, 00:04
|
#8
|
Born Sinful
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
|
If you don't want to do it with multithreading, then you have to simulate it.
The simplest way is something like this:
Code:
while (int x=0; x<10; x++) {
function_to_get_key_input();
if (x == 9) {
function_to_move_blocks();
x=0;
}
}
That gives the move_blocks 'thread' 1/10th of the priority of the key_input 'thread'.
That way means you get all 10 key inputs possible AND the movement function. Missing keypresses isn't a problem considering that will be executed in a couple of miliseconds at most. You'd need to sort out the delay on moving the blocks yourself (as like that they'd be shooting down the screen at 100's of miles per hour).
I hope I'm pointing out the obvious when I note that the code above is an infinate loop and you'll need an escape condition.
__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.
|
|
|
4 Feb 2003, 00:24
|
#9
|
Friendly geek of GD :-/
Join Date: Nov 2000
Location: On my metal roid
Posts: 923
|
Just do a simple normal game-loop.
Like:
Code:
:LoopStart
- Check for any Keyboard Inputs and process them (moving, dropping, etc)
- Get system time or something in milliseconds
- Check if it is time already to drop down the block by one, if it is, do so & reset time counter
:LoopEnd
[Edit:] Really, this one is so much more simple: Check for keyboard inputs as often as you can (i.e. as fast as the machine runs), and every now and then, after a certain amount of time (note: NO loop counter, but some constant interval), you just drop your block.
Decrease this time period, and the blocks will drop faster...
__________________
[ »] Entropy increases! :-/
|
|
|
4 Feb 2003, 00:26
|
#10
|
Born Sinful
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
|
ie. what I just said.
__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.
|
|
|
4 Feb 2003, 00:48
|
#11
|
∞+♪˛
Join Date: Nov 2000
Location: :uo!te]oŻ|
Posts: 428
|
Quote:
Originally posted by meglamaniac
...
Code:
while (int x=0; x<10; x++) {
...
...
I hope I'm pointing out the obvious when I note that the code above is an infinate loop and you'll need an escape condition.
|
while(...;...;...) doesn't really make an infinite loop... Doesn't really make anything at all, actually.
__________________
Structural Integrity for Creator - since he'll probably make PA turn 3D.
Wikipedia forum
Note to self - Don't write Chinese letters with bold and italics... 猫
<!--Last incarnation: Nov 2000-->
|
|
|
4 Feb 2003, 00:53
|
#12
|
Registered User
Join Date: Jun 2000
Posts: 8,476
|
I dont understand, why cant you just use 2 threads, and have one constantly checking for user input while the other manages the game?
|
|
|
4 Feb 2003, 01:47
|
#13
|
Born Sinful
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
|
Quote:
Originally posted by Cyp
while(...;...;...) doesn't really make an infinite loop... Doesn't really make anything at all, actually.
|
damnit, you KNOW I meant for.
Bloody late night coding...
__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.
|
|
|
4 Feb 2003, 16:53
|
#14
|
NEWSBOT
Join Date: Dec 2000
Location: The enby cave!
Posts: 4,872
|
Quote:
Originally posted by Nodrog
I dont understand, why cant you just use 2 threads, and have one constantly checking for user input while the other manages the game?
|
i can, but as far as i can tell in C++ that requires objects, and im not sure i want to OO this yet.
i might re-write it OO after i've finished it though.
edit: oops, forgot to thank Megla and Jetlinus though, thanks ppl.
__________________
[20:27:47] <nodrog-aawy> **** i think my housemate just caught me masturbating
[11:25:32] <idimmu> you are a little piggy arent you
[13:17:00] <KaneED> i'm so closet i'm like narnia
__________________
Pretty parks and funky scrap metal things here
|
|
|
|
All times are GMT +1. The time now is 09:01.
| |