Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Help with keyboard and graphics.

This thread is locked; no one can reply to it. rss feed Print
 1   2   3 
Help with keyboard and graphics.
Kitty Cat
Member #2,815
October 2002
avatar

Quote:

So while you hold down a key, it's going to remain within that loop until the key is released.

No, because keypressed() only returns true when readkey() has a key waiting in the buffer.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

HardTranceFan
Member #7,317
June 2006
avatar

Apologies, my interpretation of the manual was incorrect.

Back to the drawing board.

[edit]
Are you calling any key reading functions anywhere else in your code?
[/edit]

--
"Shame your mind don't shine like your possessions do" - Faithless (I want more part 1)

konedima
Member #6,241
September 2005

No key reading functions anywhere else.

Is there a quicker way to resolve this than forum tag?

War Pong HD! Every time you don't download it, a kitten of hope dies in my heart. Please, save the imaginary kittens.
http://warpong.sourceforge.net

miran
Member #2,407
June 2002

Sorry, I didn't read the whole thread, just the first few posts. Why is readkey() better than key[] again? I don't see the point. As far as I can tell readkey() will return a keypress as soon as the user presses a key, then again after a short delay if the key is being pressed down and then again in regular intervals. Those two times can be changed in the OS's keyboard settings. Which means the player will be able to change the way how the game responds to keyboard input in the Windows control panel. Which as far as I can tell is not something most people would like.

For normal everyday keyboard input the key[] array is the way to go. The code snippets Daniel and Mark posted in the first two replies are what you want. Daniel's is very simple and should be easy to adapt while Mark's is a little more involved (but complete) and just requires some initialization...

--
sig used to be here

Mark Oates
Member #1,146
March 2001
avatar

Quote:

it doesn't do anything at all

ah, you have to add
input_controller.update();
somewhere at the beginning of your main loop. Sorry I forgot to mention that.

HardTranceFan
Member #7,317
June 2006
avatar

[edit]
Damn, Mr Oates. Is that all that might have to be done?
[/edit]

Quote:

Is there a quicker way to resolve this than forum tag?

I said said:

If you attach the complete source, then some of the allegroids could have a look. It could be that you've overlooked an issue elsewhere in your program.

Quote:

I'm weirdly overprotective of my source code (espcially since I plan on releasing it as open source when it's done). I could post it if I really had to, but I'd rather look for other alternatives.

I don't fully understand why you're protective of your source code, particularly as it's going to be in the public domain later on anyway. However, we're not here to discuss that. Looking at an alternative, I'd suggest debugging.

The sort of debugging I'm thinking of doesn't mean using a debugger and stepping through the code, but putting in a small amount of code, immediately before and after a function call, that appends a line of text with a timer on it into a log file. Run your program, and then go through log and see where there are large pauses between statements. Once the function is identified, do the same within that function, but for each command line. It's slow, but debugging is not a fast sport :)

To help you get the idea, here's a sample of the type of code I'm thinking about:

1#include <fstream>
2void maingame(){
3 // this creates an empty file every run. No point in appending to previous runs
4 ofstream fout("debugging.log");
5 fout.close();
6 
7 int nspeed, k, totalspeed = 0; // Added total speed
8 LOCK_VARIABLE(speeder);
9 LOCK_FUNCTION(increment_speeder);
10 install_int_ex(increment_speeder, BPS_TO_TIMER(60));
11 while (win <= 0){
12 totalspeed += speeder; // Added
13 if (speeder == 0)
14 rest(1);
15 nspeed = speeder;
16 speeder = 0;
17 while(nspeed > 0) {
18 nspeed--;
19 while(keypressed()) {
20 k = readkey() >>8;
21 fout.open("debugging.log", ios::app); // Added
22 fout << "before inpt(k)" << totalspeed + speeder << "\n";// Added
23 fout.close() // Added
24 input(k);
25 fout.open("debugging.log", ios::app); // Added
26 fout << "after inpt(k)" << totalspeed + speeder << "\n"; // Added
27 fout.close() // Added
28 }
29 workbullets();
30 ballmove();
31 players();
32 }
33 quickdisplay();
34 }
35}

It may not be the most eloquent or efficient method, but it works, and it's fairly quick to do.

Good luck :)

--
"Shame your mind don't shine like your possessions do" - Faithless (I want more part 1)

Mark Oates
Member #1,146
March 2001
avatar

Quote:

Damn, Mr Oates. Is that all that might have to be done?

Perhaps not. But it's clean, clear and managed completely in the background. ;) you get these three wonderful functions:

pressed()
just_pressed()
just_released()

[edit] The only thing it doesn't do yet is time how long a button has been held down, or send triggers in regular intervals. But I'll add that in the future.

Kitty Cat
Member #2,815
October 2002
avatar

Quote:

Why is readkey() better than key[] again?

Because for firing and quitting, he wants to know when the key is actually pressed, not if its down or up at a given point in time. Sure you could emulate it with a shadow key[] array or similar, memcpy'ing, and checking between the two arrays, but why hack in functionality that's already provided for you?

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

konedima
Member #6,241
September 2005

To be honest, I don't like revealing my source code because I never wanted my projects to be open source - it's just that Sourceforge has free hosting, no ads, etc.

I don't know if having all the extra source code will help (very little of it is relevant to the issue at hand). But, ask and ye shall recieve.
http://warpong.sourceforge.net/warpongc.cpp

By the way: I know there's probably a lot of bad stuff in there (I should give people an error message if one of the graphics doesn't exist) but I'll fix that up for the release version.

War Pong HD! Every time you don't download it, a kitten of hope dies in my heart. Please, save the imaginary kittens.
http://warpong.sourceforge.net

Evert
Member #794
November 2000
avatar

Quote:

I don't fully understand why you're protective of your source code, particularly as it's going to be in the public domain later on anyway.

Something being open source does not imply in any way that it is in the public domain, though you're by no means the only one to think that it does.

Quote:

Sure you could emulate it with a shadow key[] array or similar, memcpy'ing, and checking between the two arrays, but why hack in functionality that's already provided for you?

This does not emulate readkey(). Also note that readkey() does not tell you if a key was pressed and held down, or pressed multiple times in rapid succession. For normal game input I use key[] and a copy of the key[] array from the last timestep to detect key down and key up events (actually, I don't, I use a wrapper around either a keyboard or a gamepad so that I can use either without changing a line of code, but that uses key[] for the keyboard). Another reason to use key[] over readkey() has to do with modifier keys that you may (or may not) want to use like any other key. It depends very much on the situation (and the game in question) what is better. For a top-down RPG or dungeon crawler, I would (do) use key[]. For an RTS, I would (do) use readkey().
In this particular case, judging from the described intended behavior, readkey() does seem the more natural implementation. However, if he wants to fire the bullet only when the key is released again, then key[] is the proper (only) way to go.

HardTranceFan
Member #7,317
June 2006
avatar

Konedima,

I've got the solution to your problem. Thanks for providing your source - I used that plus the graphics from sourceforge to compile the game.

Anyway, the problem was that you were only checking for player movement keys if keypressed() was true. This causes a stutter at the start and a jutter along the way as it appears to use the OS key repeat settings.

So, I've modified the code as below:

1// Somewhere where you declare all your functions:
2void movePlayers();
3 
4// Then...
5 
6void maingame(){
7 int nspeed, k;
8 LOCK_VARIABLE(speeder);
9 LOCK_FUNCTION(increment_speeder);
10 install_int_ex(increment_speeder, BPS_TO_TIMER(60));
11 while (win <= 0){
12 if (speeder == 0)
13 rest(1);
14 nspeed = speeder;
15 speeder = 0;
16 while(nspeed > 0) {
17 nspeed--;
18 while(keypressed()) {
19 k = readkey() >>8;
20 input(k);
21 }
22 workbullets();
23 movePlayers(); // New
24 ballmove();
25 players();
26 }
27 quickdisplay();
28 }
29}
30 
31void input(int k){
32 // Player 1
33 if (k == KEY_D) player1newbullet();
34 // Player 2
35 if (k == KEY_LEFT) player2newbullet();
36 // General
37 if (k == KEY_ESC) win = 3;
38}
39 
40// The new move function checked independently of keypressed()
41void movePlayers(){
42 // Player 1
43 if (key[KEY_W]) player1y = player1y - 8;
44 if (key[KEY_S]) player1y = player1y + 8;
45 // Player 2
46 if (key[KEY_UP]) player2y = player2y - 8;
47 if (key[KEY_DOWN]) player2y = player2y + 8;
48 // General
49}

BTW, if you want some pointers to speeding up the bats but not the ball, easier ways to load your graphics, drop me a PM.

--
"Shame your mind don't shine like your possessions do" - Faithless (I want more part 1)

konedima
Member #6,241
September 2005

Huzzah! Finally, success! The input works perfectly, thanks for all your help.

HardTranceFan, did you have to modify the graphics from Sourceforge? Because if I recall, the graphics in the resources pack were 16 bit bitmaps, like I've been having trouble with.

War Pong HD! Every time you don't download it, a kitten of hope dies in my heart. Please, save the imaginary kittens.
http://warpong.sourceforge.net

HardTranceFan
Member #7,317
June 2006
avatar

No changes to graphics were required. I used the retrowar set of graphics.

[edit]
Sorry, my memory failed me - the graphics are just black quads. The graphics didn't work.
[/edit]

--
"Shame your mind don't shine like your possessions do" - Faithless (I want more part 1)

konedima
Member #6,241
September 2005

...which still leaves the graphics problem, which is thoroughly confusing by my amatuer standards.

War Pong HD! Every time you don't download it, a kitten of hope dies in my heart. Please, save the imaginary kittens.
http://warpong.sourceforge.net

LennyLen
Member #5,313
December 2004
avatar

It's your images that are the problem, not your code. I loaded and resaved them with MSPaint, and they worked fine.

konedima
Member #6,241
September 2005

MSPaint doesn't save 16 bit bitmaps. The only option above 8 bit is 24 bit.
I don't have a problem with 24 bit bitmaps.

War Pong HD! Every time you don't download it, a kitten of hope dies in my heart. Please, save the imaginary kittens.
http://warpong.sourceforge.net

miran
Member #2,407
June 2002

I think Allegro doesn't support 16bit bitmaps. If you're concerned about filesize, use PNG or if you want to save even more space at the expense of quality, JPEG. There are add-ons available for loading both formats...

--
sig used to be here

konedima
Member #6,241
September 2005

Now someone tells me.

War Pong HD! Every time you don't download it, a kitten of hope dies in my heart. Please, save the imaginary kittens.
http://warpong.sourceforge.net

LennyLen
Member #5,313
December 2004
avatar

Someone needs to rewrite the manual then. Under load_bmp(), it says 16bit.

miran
Member #2,407
June 2002

I don't know, I might be wrong. But BMP as such is a very broad term. There can be many variants and almost certainly Allegro doesn't support all of them.

--
sig used to be here

Evert
Member #794
November 2000
avatar

Quote:

I think Allegro doesn't support 16bit bitmaps.

It should. I vaguely recall that there used to be a bug that prevented it from working sometime pre 4.2, but that should be fixed.
Haven't tested it recently though.

Kikaru
Member #7,616
August 2006
avatar

Is your avvie an example of what your game will look like? If so, it looks cool. Glad to see your problem is fixed. I just use 24-bit for everything, it's not too much trouble. :)

LennyLen
Member #5,313
December 2004
avatar

Quote:

Is your avvie an example of what your game will look like?

Not if he's using the graphics from his sourceforge page.

Kauhiz
Member #4,798
July 2004

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

konedima
Member #6,241
September 2005

Actually it supports many skins (the current tally is 5 in the latest release). Something that simple shouldn't take me too long to make, and it's cool, then why not?.

My avatar is only that simple because I'm lazy :).

War Pong HD! Every time you don't download it, a kitten of hope dies in my heart. Please, save the imaginary kittens.
http://warpong.sourceforge.net

 1   2   3 


Go to: