|
Help with keyboard and graphics. |
konedima
Member #6,241
September 2005
|
I have two questions here, so I'll address them individually. 1: In the game I am making, you need to press a key to fire a bullet. When I press the key though, it fires multiple bullets at once (which is bad because they need to be fired individually). 2: My game runs at 16 bit colour depth (it always has, purely because I feel like it). The graphics for use in game are stored as bitmaps. When I try using 16-bit bitmaps in-game (they are 16-bit because there is no point in them being any more detailed, it won't show any more detail at 16-bit colour, obviously) they just show up as black squares the size of the bitmap. I can fix it by saving the bitmaps at any other colour depth (or indexed colour, which can be handy for the ones that only have a few different colours). As I said, I don't want to go to all the hassle of converting all my bitmaps to a different colour depth and since my game is skinnable by the end user I don't want them to have problems. set_color_depth(16); set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0; This loads a graphic: BITMAP *background; background = load_bitmap("background.bmp", NULL);
And this displays that graphic: Any and all help with these problems would be greatly appreciated. War Pong HD! Every time you don't download it, a kitten of hope dies in my heart. Please, save the imaginary kittens. |
DanielH
Member #934
January 2001
|
|
Mark Oates
Member #1,146
March 2001
|
if you're using c++:
it's easy to use! just if (just_pressed(KEY_D)) fire_bullet(); -- |
Paul whoknows
Member #5,081
September 2004
|
konedima, your avatar rocks! ____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
Kitty Cat
Member #2,815
October 2002
|
int k = (keypressed() ? (readkey() >> 8) : 0); ... if(k == KEY_D) player1newbullet(); Why do you people keep insisting on using key[] in a way it wasn't designed for? Also, the ordering of how you call set_gfx_mode and load_bitmap is very important. When using Allegro properly, there's really no reason to restrict to a specific color depth, unless you're using palette effects (which you're obviously not). You should try to use desktop_color_depth() whenever possible, otherwise you may get a bad speed hit from color conversions. -- |
konedima
Member #6,241
September 2005
|
I'm having mixed success with your suggestions. DanielH: That does nothing, basically. The bullets fire in shorter bursts but I need it to be that only one fires. Mark Oates: When I put that in (where I thought it should be, I'm pretty new to C++ but I think I did it right), and I press the key, it doesn't do anything at all. Paul whoknows: Thanks! Kitty Cat: For the keys, that doesn't work. Like Mark's, the key presses don't do anything. And I did set it the colour depth to desktop_color_depth(), thanks for the advice. It's not any faster (it was never particularly slow, and I have to slow it down to keep it at a decent speed) and it doesn't fix my graphics problem. It still displays my 16 bit bitmaps as black. War Pong HD! Every time you don't download it, a kitten of hope dies in my heart. Please, save the imaginary kittens. |
Kitty Cat
Member #2,815
October 2002
|
You're going to have to show more code, then. -- |
konedima
Member #6,241
September 2005
|
How much do you want? War Pong HD! Every time you don't download it, a kitten of hope dies in my heart. Please, save the imaginary kittens. |
Kitty Cat
Member #2,815
October 2002
|
Initialization (where you call set_gfx_mode, create_bitmap, load_bitmap, and stuff), and your input routine. -- |
konedima
Member #6,241
September 2005
|
These are missing some parts inbetween, but all these bits are shown in the order they appear in the code. Same for the rest of the graphics. int player1size; int player1score; Same for the rest of the global variables. void loadgraphics(); void scoreforone(); Same for the rest of the functions.
That's the entire main loop.
Those are the only functions that take keyboard input. War Pong HD! Every time you don't download it, a kitten of hope dies in my heart. Please, save the imaginary kittens. |
Kitty Cat
Member #2,815
October 2002
|
Unless you're disabling color conversions, I see no reason why your graphics come out black. Though I can't see the code where you actually load the graphics. As for input, you're mixing your logic and drawing. Something like this would work:
Note that player up-down movement will be continuous while the key is held down. If you don't want that, do this instead: if (k == KEY_W) player1y = player1y - 4; if (k == KEY_S) player1y = player1y + 4; ... if (k == KEY_UP) player2y = player2y - 4; if (k == KEY_DOWN) player2y = player2y + 4;
-- |
konedima
Member #6,241
September 2005
|
While I have a look at that keyboard code here's an example of loading the graphics: ball = load_bitmap("ball.bmp", NULL); background = load_bitmap("background.bmp", NULL); They're all like that. Edit: I generally don't work well with code other than my own, and when I implemented your changes, I can't seem to get it to work (and when I do, it just freezes on a black screen). This is what I've got:
War Pong HD! Every time you don't download it, a kitten of hope dies in my heart. Please, save the imaginary kittens. |
Kitty Cat
Member #2,815
October 2002
|
You're mixing logic and drawing again. And don't use rest() to time the game. The FAQ is your friend. -- |
konedima
Member #6,241
September 2005
|
Now I'm getting somewhere (it works!) but the bats (using w,s,up,down) move very choppily and pressing esc or either of the shoot buttons don't register at all. This is what I got:
War Pong HD! Every time you don't download it, a kitten of hope dies in my heart. Please, save the imaginary kittens. |
LennyLen
Member #5,313
December 2004
|
konedima
Member #6,241
September 2005
|
Sorry, that doesn't work. Any other ideas? War Pong HD! Every time you don't download it, a kitten of hope dies in my heart. Please, save the imaginary kittens. |
LennyLen
Member #5,313
December 2004
|
Oops, my fault for misreading the manual. That was the test for ALT + key. What happens if you change it to: and then change the other checks to (for example):
|
Kitty Cat
Member #2,815
October 2002
|
I don't see why the shoot buttons and escape don't work. Have you traced them to see that they behave properly? As for the choppiness, that's probably because you're moving four pixels at a time. And you're using rest(10);, which is probably causing timing issues. Instead, use: while(speeder <= 0) rest(1); So it only rests when you have time to wait. -- |
konedima
Member #6,241
September 2005
|
For whatever reason, now it is registering those key presses. And when I mean moving the bats is choppy, I mean that before I did all this they moved smoothly. And faster. Sorry if I sound like I'm whining. War Pong HD! Every time you don't download it, a kitten of hope dies in my heart. Please, save the imaginary kittens. |
HardTranceFan
Member #7,317
June 2006
|
[edit] konedima, does the following make any difference to the choppiness?
-- |
konedima
Member #6,241
September 2005
|
I'm good at not being clear . All the input issues are fixed. The problems I have are that the bat (and sometimes the bullet) movement is very choppy (bullets appear to move back and forth occasionally) and that 16-bit bitmaps still don't work. HardTranceFan, your code doesn't help, thanks anyway. I've changed the code since I last posted it - I've moved all the drawing operations into one function, and out of their logic functions. Here's the main game loop:
War Pong HD! Every time you don't download it, a kitten of hope dies in my heart. Please, save the imaginary kittens. |
HardTranceFan
Member #7,317
June 2006
|
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. Or attach the binary and see if it occurs on other machines. -- |
konedima
Member #6,241
September 2005
|
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. The only thing I can think of is that as mentioned before, the bats are moved up four pixels at a time (actually now it's eight). The program was probably running at a faster framerate before because I only had a rest(10) to slow it down, so that could have been it. I don't know how to fix it because if they move any slower, they won't move nearly fast enough. Something else I've noticed during testing of the latest code is that if you shoot a bullet while moving the bat (by holding the key down), the movement stops until you let go and press down again. Is there any way to fix that? War Pong HD! Every time you don't download it, a kitten of hope dies in my heart. Please, save the imaginary kittens. |
HardTranceFan
Member #7,317
June 2006
|
Quote: Something else I've noticed during testing of the latest code is that if you shoot a bullet while moving the bat (by holding the key down), the movement stops until you let go and press down again. Is there any way to fix that? Ahhh, I just noticed you have a : while(keypressed()) { k = readkey() >>8; input(k); } So while you hold down a key, it's going to remain within that loop until the key is released. It will update the co-ordinates, but not draw them to the screen. Change the while to an if. I suspect this may fix your visual problem. -- |
konedima
Member #6,241
September 2005
|
Nope, it doesn't. War Pong HD! Every time you don't download it, a kitten of hope dies in my heart. Please, save the imaginary kittens. |
|
|