I plan on using this thread to recieve help for many different problems in allegro. I will ask 1 by 1.
Prob Num 1:
Can I get an example ( with comments) of a program that will take your Sprite.bmp and put it on screen?
Printable View
I plan on using this thread to recieve help for many different problems in allegro. I will ask 1 by 1.
Prob Num 1:
Can I get an example ( with comments) of a program that will take your Sprite.bmp and put it on screen?
Did that sprite code I sent you work?
look at your PM :p
Thanks Josh for helpin me there.
Now
Num 2:
How can I make the ship move, and not leave a trail?Code:#include <allegro.h>
int x = 300, quit = 0;
int main()
{
allegro_init();
install_keyboard();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0);
BITMAP *ship = load_bitmap( "Ship.bmp", NULL );
while ( !quit )
{
if ( keypressed() ){
if ( key[KEY_LEFT] ) x--;
else if ( key[KEY_ESC] ) quit = 1;
}
draw_sprite ( screen, ship, x, 400 );
}
destroy_bitmap(ship);
allegro_exit();
return 0;
}
END_OF_MAIN();
Also why does it move so freakin fast?
You have to use dirty rectangles (i think) or clear the screen. Clearing the screen would be slow, and cause alot of flicker. I will try to code an example of how to move a sprite with no flicker in a little bit.
i know almost nothing about allegro, but from expereince in other apis, your ship is leaving a trail because youre not erasing it. you can expecet the stupid box in front of you to know when to clear something.
I might be able ( with josh's help ) to find a function for this.
But my problem is if i barely tap LEFT it flys of the flippin' screen!
Here is what I think is happening:
It detects the LEFT key and makes x++, but when it loops back, it continues to x++.
either that, or youre not checking the key every time you loop. try printing to a file or something what you get as input on every loop. also, arent there delay functions in allegro(for slowing down the movement of the ship)?
After you get the input and change the coordinates, try something simple like for(int j = 0; j < 400; j++) ;
nope, makes no difference what so ever :(
I was looking in the Allegro docs and found this:
void set_keyboard_rate(int delay, int repeat);
Try setting it before your loop. Set is after install_keyboard();.
Mess around with the parameters till it works right.
i was 1 or 2 steps ahead of you. I tried
set_keyboard_rate(0, 0)
that is supposed to disable it but it didnt work :(
im gonna try 1, 1 now :)
edit: nope :(
Hmm, I do not know, but this may fix it, after you change the coordinates and redraw the sprite put clear_keybuf();
i just finished a game in allegro and this i what i did to conrol game speed. you update the screen and keyboard input every loop, but you only change the objects position after so much time. heres an ex.
Code:
#include <time.h>
clock_t time1,time2;
int main()
time1 = clock();
while (key[KEY_ESC==0]))
{
clear(screen);
keyboard_input
update_screen
time2 = clock();
if (time2-time1>100) //makes this # bigger to slow down
{
move object
time1 = clock();
}
}
return;
what is update_screen?
And i really didnt want to go this route...
I dont want flicker in my game :mad:
:confused:
Vicious didn't u get the code i send u about doublebuffering?
i got a problem i remember u have to include some file your
project in order to make allegro work but now i have forgotten what that file is can someone please check that for me
btw i use DEV-C++
i tried this and it didt go as fast.... but i t flickers like crap :(
that clear(screen) func slowed it down....Code:#include <allegro.h>
int x = 300, i;
int main()
{
allegro_init();
install_keyboard();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0);
BITMAP *ship = load_bitmap( "Ship.bmp", NULL );
draw_sprite ( screen, ship, x, 400 );
while ( !key[KEY_ESC] ){
clear(screen);
if ( x > 50 && key[KEY_LEFT]) x--;
if ( x < 540 && key[KEY_RIGHT]) x++;
draw_sprite ( screen, ship, x, 400 );
}
destroy_bitmap(ship);
allegro_exit();
return 0;
}
END_OF_MAIN();
its still fast but not insane. Any ideas?
Josh?
:confused:
I just decided to give Allegro a try, too, and holy crap is it easy. Not to mention that it is ummm hmmm awesome!
Well if it is soo easy... what is wrongwith my program :P
Take out the clear screen function and it won't flicker anymore. In the loop it is continously clearing and redrawing the bmp to the screen. Take out the clear screen and then all it is doing is drawing the bmp to the screen so you can't see a difference.
Well, it's easy in the sense that all the functions are "english like". Basically I can understand all of the syntax without even really learning it yet. See what I mean?Quote:
Well if it is soo easy... what is wrongwith my program
Btw, I wrote the taking out the clear screen function post before I saw your very last post.:)
i know....
im working on a timer function right now...
Right now I'm using your code and having the mouse move the bmp. This is so much cooler than console programming. The docs for allegro are very useful, and make no need for tutorials (for the most part).:cool:
YAY, it works... now to make it clear it self....
Code:#include <allegro.h>
int counter;
int x = 300, left = 0, right = 0;
void update_counter()
{
counter++;
}
END_OF_FUNCTION(update_counter);
int main()
{
allegro_init();
install_keyboard();
LOCK_VARIABLE(counter);
LOCK_FUNCTION(update_counter);
install_int_ex( update_counter, BPS_TO_TIMER(60));
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0);
BITMAP *ship = load_bitmap( "Ship.bmp", NULL );
while ( !key[KEY_ESC] )
{
while (counter > 0) {
if (left) x-=5;
if (right) x+=5;
counter--;
}
if (key[KEY_RIGHT])
right = 1;
else
right = 0;
if (key[KEY_LEFT])
left = 1;
else
left = 0;
draw_sprite ( screen, ship, x, 300 );
}
destroy_bitmap(ship);
allegro_exit();
return 0;
}
END_OF_MAIN();
the way you guys are talking about this you're really making me wanna try it out, i'm gonna download it soon ;) then i'll change my game so it runs with REAL graphics lol :p
I think ill have to use double-buffering or page flipping...
any ideas?
well....
it works.... with some minor blinking that is...Code:#include <allegro.h>
int counter;
int x = 300, left = 0, right = 0;
void update_counter()
{
counter++;
}
END_OF_FUNCTION(update_counter);
int main()
{
allegro_init();
install_keyboard();
LOCK_VARIABLE(counter);
LOCK_FUNCTION(update_counter);
install_int_ex( update_counter, BPS_TO_TIMER(60));
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0);
BITMAP *ship = load_bitmap( "Ship.bmp", NULL );
BITMAP *buffer = create_bitmap( 640, 480 );
while ( !key[KEY_ESC] )
{
while (counter > 0) {
if (left) x-=5;
if (right) x+=5;
counter--;
}
if ( x < 540 && key[KEY_RIGHT])
right = 1;
else
right = 0;
if ( x > 50 && key[KEY_LEFT])
left = 1;
else
left = 0;
draw_sprite ( screen, ship, x, 400 );
clear_bitmap( buffer); // Clear the buffer
blit( buffer, ship, 0, 0, x, 400, ship->w, ship->h); // Blit the ship to the buffer
blit( buffer, screen, 0, 0, 0, 0, 640, 480); // Blit the buffer to the screen
}
destroy_bitmap(buffer);
destroy_bitmap(ship);
allegro_exit();
return 0;
}
END_OF_MAIN();
I got an idea to get rid of that flicker. What kind of background are you going to use? If it is solid black, then create a bitmap the same size as your ship, make it solid black. Before you draw the coordinates of the new ship, draw the black bitmap at the coordinates of the old ship. That way it deletes the old one, without redrawing the whole screen (which makes it flicker).
i was gonna use a star field back ground but i might leave it black...
know what....
If someone wants to try this for me (im away from my computer right now).
put vsync(); right b4 i blit the buffer to the screen....
hey Vicious i tried this and works really well:
the only problem is that it messes up a tiny bit at the beginning and it leaves a trail once you move the bmp. however, there isn't any flickering at all right after the beginning.Code:
#include <allegro.h>
int counter;
int x = 300, left = 0, right = 0;
void update_counter()
{
counter++;
}
END_OF_FUNCTION(update_counter);
int main()
{
allegro_init();
install_keyboard();
LOCK_VARIABLE(counter);
LOCK_FUNCTION(update_counter);
install_int_ex( update_counter, BPS_TO_TIMER(60));
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0);
BITMAP *ship = load_bitmap( "ship.bmp", NULL );
draw_sprite ( screen, ship, x, 300 );
while ( !key[KEY_ESC] )
{
while (counter > 0) {
if (left) x-=5;
if (right) x+=5;
counter--;
}
if ( x < 540 && key[KEY_RIGHT])
right = 1;
else
right = 0;
if ( x > 50 && key[KEY_LEFT])
left = 1;
else
left = 0;
vsync();
clear_to_color (screen, 0);
draw_sprite ( screen, ship, x, 300 );
}
destroy_bitmap(ship);
allegro_exit();
return 0;
}
END_OF_MAIN();
yup, if you dont put in the buffer thingy it will leave a trail i think....
it will work better than the clear(screen) or the clear_to_color...
try it?
I just tried the vsync() before you blit and it flickers way too much. My above code (well mostly yours actually;) ) works the best so far.
does you clear(screen) method leave a trail?
replacing clear to color() with clear(screen) doesn't work as well in that example doesn't work as well.
how bad was the flicker when it didnt leave a trail?
bad bad?
screenshot?
sorry, ill quit asking these stupid questions when i get to my computer
It's bad enough that it would not be acceptable in a game. The trail is not nearly as bad as the flickering. I'm trying some things out right now.
What I've been trying to do is to not blit the bmp but only blit the bmp when the bmp is moved. So if the bmp is not being moved there shouldn't be any flickering. But everytime I try this I mess everything up entirely.
have you tried
Code:if ( keypressed() )
{
//do the vsync(), draw_sprite(), blit....
}
and what kind of trail we talkin bout? you mean like a blur? or an actual trail?
yeah i tried that...basically anytime blit is involved there is a lot of flickering.
the trail is just like a blur. it's not all that bad but of course you'd rather have it without a blur.
oh, that blur.... yah its just part optical part bright color moving fast against a black background
:rolleyes:
its rally unavoidable if it is what im thinking
Oh well then that's awesome, and I'm sure you're right, too.
I have a question. If somebody uses your program on their computer and they don't have allegro installed what files will they need? I even had the person get the allegro40.dll file and my program still wouldn't work. he tried both in the folder of the program itself and in the windows/system folder. For some reason it would never work. I'm above 100% positive that it is not because of the program. The program works perfectly fine for me. Are there any other files that person needs in order to execute my program?
If you staticly link it, they do not need the files. And it is not the blit creating the flicker, it is the clearing of the screen. Just use two screen buffers. Draw to the back one, then when that is done, flip it to the front one (don't blit it) and it should not flicker.
I am downloading the latest version of Allegro, if I can get it working with C++ Builder I am gonna figure out the best way to move sprites without flicker.
tech... i used vsync() before that last blit and it did fine until i went up into lower y coords... (top of screen )
Does anybody know of any methods of quickly displaying two bitmaps at one time. I know that technically it's not possible but... Such as if you were in a plane and you were firing at something so bullets appear as if are being shot of the plane. But what if you want to quickly shoot twice. I can't seem to find a way that is able to display all of the bullets flying at once. I have an algorithm that is able to do it okay at best, but it's not good enough.
Here is what I'm doing:
//display the beginning animation of the bullets coming out of the plane
//continue to display the bullets until it reaches a certain point
//if a certain variable is triggered then display animation 3
//if a certain variable is triggered then display animation 2
//if the shoot key (space bar) is pressed during the display of shooting bullets then animation 1 is triggered...animation 1 can not be triggered if variable 2 or 3 are triggered...hence this means that it can only display 2 bullet display at once (Sorry it's hard to explain)
//animation 1 = the beginning display of the bullets for the 2nd shooting
//animation 2 = the repetitive display of bullets being shot until the 2nd bullets reach the 1st bullets y coordinates
//animation 3 = the repetitive display of bullets until the bullets reach a certain point
//so the first round of bullets go off then once the second round go off the first round stop until the second round reach the height of the first round of bullets...once the first round of bullets reaches that certain point then the second round of bullets finish off reaching to that certain point
that technique does not look pretty at all. would it best if i just created bitmaps that would represent all of these situations? but that would mean i would have to create a ton of bitmaps. should i create more animations to where it doesn't appear as if much lag is happening when the bullet movements are switching back and forth. is their a better way that i'm not aware of? i don't think there are multithreading capabilities in allegro are there? any ideas anybody?
sorry, vicious, for kind of stealing your thread and your ideas...well somewhat. :D
EDIT: Thanks Josh for the info. And, Vicious, it doesn't look good at all when I do it...not sure why?
EDIT 2: Would page flipping be something that could help resolve that problem?
page flipping makes an extremely wierd effect when I try it....
anyway, i made some baddys and made them bounce side to side... now for lasers :(
i know there is a better way to do it than loading seperate bitmaps.... it was just the only way I knew how :(Code:#include <allegro.h>
int counter;
int x = 300, y = 425, left = 0, right = 0, go = 0, fire = 0;
int bx = 100, cx = 150, dx = 200, ex = 250, fx = 300, gx = 350, hx = 400, ix = 450, jx = 500;
void update_counter()
{
counter++;
}
END_OF_FUNCTION(update_counter);
int main()
{
allegro_init();
install_keyboard();
LOCK_VARIABLE(counter);
LOCK_FUNCTION(update_counter);
install_int_ex( update_counter, BPS_TO_TIMER(60));
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0);
BITMAP *ship = load_bitmap( "Ship.bmp", NULL );
BITMAP *baddy = load_bitmap( "Baddy.bmp", NULL );
BITMAP *baddy2 = load_bitmap( "Baddy.bmp", NULL );
BITMAP *baddy3 = load_bitmap( "Baddy.bmp", NULL );
BITMAP *baddy4 = load_bitmap( "Baddy.bmp", NULL );
BITMAP *baddy5 = load_bitmap( "Baddy.bmp", NULL );
BITMAP *baddy6 = load_bitmap( "Baddy.bmp", NULL );
BITMAP *baddy7 = load_bitmap( "Baddy.bmp", NULL );
BITMAP *baddy8 = load_bitmap( "Baddy.bmp", NULL );
BITMAP *baddy9 = load_bitmap( "Baddy.bmp", NULL );
BITMAP *laser = load_bitmap( "laser.bmp", NULL );
BITMAP *buffer = create_bitmap( 640, 480 );
while ( !key[KEY_ESC] )
{
while (counter > 0) {
if (left) x-=10;
if (right) x+=10;
counter--;
}
if ( x < 600 && key[KEY_RIGHT])
right = 1;
else
right = 0;
if ( x > 0 && key[KEY_LEFT])
left = 1;
else
left = 0;
clear_bitmap( buffer);
draw_sprite ( buffer, ship, x, 425 );
draw_sprite ( buffer, baddy, bx, 20 );
draw_sprite ( buffer, baddy2, cx, 20 );
draw_sprite ( buffer, baddy3, dx, 20 );
draw_sprite ( buffer, baddy4, ex, 20 );
draw_sprite ( buffer, baddy5, fx, 20 );
draw_sprite ( buffer, baddy6, gx, 20 );
draw_sprite ( buffer, baddy7, hx, 20 );
draw_sprite ( buffer, baddy8, ix, 20 );
draw_sprite ( buffer, baddy9, jx, 20 );
vsync();
blit( buffer, screen, 0, 0, 0, 0, 640, 480);
if ( bx > 10 && go == 0){
bx--;
cx--;
dx--;
ex--;
fx--;
gx--;
hx--;
ix--;
jx--;
}
else if ( jx < 600 ){
go = 1;
bx++;
cx++;
dx++;
ex++;
fx++;
gx++;
hx++;
ix++;
jx++;
}
else if ( jx = 600 ){
go = 0;
}
}
destroy_bitmap(buffer);
destroy_bitmap(ship);
destroy_bitmap(baddy);
destroy_bitmap(baddy2);
destroy_bitmap(baddy3);
destroy_bitmap(baddy4);
destroy_bitmap(baddy5);
destroy_bitmap(baddy6);
destroy_bitmap(baddy7);
destroy_bitmap(baddy8);
destroy_bitmap(baddy9);
destroy_bitmap(laser);
allegro_exit();
return 0;
}
END_OF_MAIN();
edit: this code is flicker free :P
oh... i know that code is really messy but im just getting the basic ideas down before i separate everything...
and besides i have only been programming for a few months so dont expect a miracle from me :D
What is the weird effect?
My code will compile, but it gives an illegal operation, any idea why? Here is the code:
Code:#include <allegro.h>
void setup();
void shutdown();
BITMAP *background = load_bitmap("background.bmp", NULL);
BITMAP *plane = load_bitmap("plane.bmp", NULL);
int main()
{
setup();
blit(background, screen, 0, 0, 0, 0, 640, 480);
draw_sprite(screen, plane, 150, 150);
while(!key[KEY_ESC])
;
shutdown();
return 0;
}
END_OF_MAIN();
void setup()
{
allegro_init();
install_keyboard();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0);
}
void shutdown()
{
destroy_bitmap(background);
destroy_bitmap(plane);
allegro_exit();
}
I commented out all the lines having to do with background and plane, and it worked. So it is something wrong with them...
[edit]
Sorry for three post in a row. If I comment out all the lines, except the two that load plane.bmp and background.bmp I still get an illegal operation. Background.bmp is 640x480, maybe it is too big...
[/edit]
[edit]
Any time I try to do anything with creating or loading a bitmap I get an error. Even if I comment out all the stuff with plane and background, if I just use create_bitmap() I get an illegal operation. Maybe it is because I have allegro_init() in setup() instead of main()??
[/edit]
[edit]
Ignore all that, I got it working. I was calling a function outside of main. Some people at allegro.cc helped me, that is a nice site.
[/edit]
Well, I'm probably going to make a couple posts in a row so I can give you a quick comment Josh.
do this instead:
You have to load the bitmaps inside of a function. I had the same problem earlier so don't feel bad.Code:
BITMAP *background;
BITMAP *plane;
int main()
{
background = load_bitmap("background.bmp", NULL);
plane = load_bitmap("plane.bmp", NULL);
}
Btw, here is a link to some good posts by JustinW:
http://www.cprogramming.com/cboard/s...&threadid=7735
EDIT: well, n/m. You edited your post while I was posting mine.
You could make an array of "baddies".Quote:
i know there is a better way to do it than loading seperate bitmaps.... it was just the only way I knew how
I want to know how you can load different bitmaps out of the same bitmap. Kind of like how the commercial games are done. Right now it's not all that important, though.
Could somebody explain blit to me? I've read a ton of things on it, and I still don't understand it? I don't understand how to use it, I don't understand when to use it, and I don't understand why to use it.
blit means copy. You have a bitmap called screen. You load a bitmap called background or plane. How do you display background or plane? You copy it to the screen, or blit. Here is the function prototype:
P.S. I just wrote a demo of moving a plane around in some clouds, it does not flicker, and I do not have to use a timer or any tricks to get the input working. PM me if you want me to post the source.Code:void blit(BITMAP *source, BITMAP *dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height);
Thanks for the offer, but I already have source that can do that. So far my program can move a plane around, shoot bullets, while it's shooting bullets the plane can still move around, and if the plane shoots again the old bullets' animation is destroyed and the new one is created (I got rid of the fake animation sequence I was using to allow double shooting). Right now I'm trying to create a real animation sequence of multiple shootings.:)
About blit: I know what the source_x and source_y are for, but what are the dests for?
Does anybody else notice that when you push one of the arrow keys really hard sometimes the bitmap flies off of the screen, even though, you set restrictions against it from doing so?
P.S. Actually, Josh, I will take you up on that offer. That way I can see if what I'm doing is a good method or not. If you want me to send you my source, too, I'll be happy to do so.
I will post the source in a minute, how can your game do all that, but you did not understand blit? The destinations are where on the bitmap you are copying it to it appears. That does not make too much sense, lets say you are copying bitmap1 to screen, the destinations are the coordinates that bitmap1 will appear on screen.
check your pms.
ahh.... when i try to compile this it says that SS.exe had a problem and brings up the send or not to send box.
:confused:
Code:#include <allegro.h>
int counter;
int left = 0, right = 0;
BITMAP *buffer = create_bitmap( 640, 480 );
class player
{
public:
int x, y;
BITMAP* ship;
player(): x(300), y(425) { ship = load_bitmap ( "ship.bmp", NULL ); }
~player() { destroy_bitmap(ship); }
void getmove()
{
if ( x < 600 && key[KEY_LEFT] )
{
left = 1;
}
else
{
left = 0;
}
if ( x > 0 && key[KEY_RIGHT] )
{
right = 1;
}
else
{
right = 0;
}
}
void draw(BITMAP* b)
{
draw_sprite ( b, ship, x, 425 );
}
};
void update_counter()
{
counter++;
}END_OF_FUNCTION(update_counter);
int main()
{
allegro_init();
install_keyboard();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0);
LOCK_VARIABLE(counter);
LOCK_FUNCTION(update_counter);
install_int_ex( update_counter, BPS_TO_TIMER(60));
player player1;
do{
while ( counter > 0 )
{
player1.getmove();
if ( left = 1 ) player1.x -= 10;
if ( right = 1 ) player1.x += 10;
counter--;
}
clear(buffer);
player1.draw(buffer);
}
while ( !key[KEY_ESC] );
allegro_exit();
return 0;
}
END_OF_MAIN();
Same problem I had, you put:
BITMAP *buffer = create_bitmap( 640, 480 );
You can not call the function create_bitmap from outside of main().
Change it to BITMAP *buffer = NULL;
Then somewhere in main() put buffer = create_bitmap(640, 48);
I do mine in a setup() function
it will compile but it says the same error message when i run thje exe...
are you using dbl buffer, cause when i use it there is no blur at all. also if you want to see the source for the snake game i made w/ allgero just ask. it is pretty simple, so you should be able to understand it.
I do not see player::draw()
player.draw(buffer);
i dunno if thats right or not... this is the first time ive even attempted oop ( classes).
any way here is where im at know
it runs.. i see the ship.. i move the ship... when i close the app... it gives me the same errorCode:#include <allegro.h>
int counter;
int left = 0, right = 0;
BITMAP *buffer = NULL;
class player
{
public:
int x, y;
BITMAP* ship;
player(): x(300), y(425) { ship = load_bitmap ( "ship.bmp", NULL ); }
~player() { destroy_bitmap(ship); }
void getmove()
{
if ( x < 600 && key[KEY_LEFT] )
{
left = 1;
}
else
{
left = 0;
}
if ( x > 0 && key[KEY_RIGHT] )
{
right = 1;
}
else
{
right = 0;
}
}
void draw(BITMAP* b)
{
draw_sprite ( b, ship, x, 425 );
}
};
void update_counter()
{
counter++;
}END_OF_FUNCTION(update_counter);
int main()
{
allegro_init();
install_keyboard();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0);
buffer = create_bitmap( 640, 480 );
LOCK_VARIABLE(counter);
LOCK_FUNCTION(update_counter);
install_int_ex( update_counter, BPS_TO_TIMER(60));
player player1;
do{
while ( counter > 0 )
{
player1.getmove();
if ( left == 1 ) player1.x -= 10;
if ( right == 1 ) player1.x += 10;
counter--;
}
clear(buffer);
player1.draw(buffer);
blit( buffer, screen, 0, 0, 0, 0, 640, 480);
}
while ( !key[KEY_ESC] );
allegro_exit();
return 0;
}
END_OF_MAIN();
I am not too good at OOP, but Maybe you should make a class called object, and it can draw itself, it has a bitmap loaded representing itself, then make a player class that is inherited from the object class, but adds the player specific stuff, and make an enemy class that is inherited from the object class. Then you do not have to recode as much stuff.
what the crap did you just say... :confused:
its all fixed now :)
Flicker free... no errors
Code:#include <allegro.h>
int counter;
BITMAP *buffer = NULL;
class player
{
public:
int x, y, left, right;
BITMAP* ship;
player(): x(300), y(425), left(0), right(0) { ship = load_bitmap ( "ship.bmp", NULL ); }
~player() { destroy_bitmap(ship); }
void getmove()
{
if ( x > 0 && key[KEY_LEFT] )
{
left = 1;
}
else
{
left = 0;
}
if ( x < 600 && key[KEY_RIGHT] )
{
right = 1;
}
else
{
right = 0;
}
if ( left == 1 ) x -= 10;
if ( right == 1 ) x += 10;
}
void draw(BITMAP* b)
{
draw_sprite ( b, ship, x, 425 );
}
};
void update_counter()
{
counter++;
}END_OF_FUNCTION(update_counter);
int main()
{
allegro_init();
install_keyboard();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0);
buffer = create_bitmap( 640, 480 );
LOCK_VARIABLE(counter);
LOCK_FUNCTION(update_counter);
install_int_ex( update_counter, BPS_TO_TIMER(60));
player *player1 = new player;
do{
while ( counter > 0 )
{
player1->getmove();
counter--;
}
clear(buffer);
player1->draw(buffer);
blit( buffer, screen, 0, 0, 0, 0, 640, 480);
}
while ( !key[KEY_ESC] );
delete player1;
allegro_exit();
return 0;
}
END_OF_MAIN();
are any of you having this problem? ---
once in a while when i press the arrow keys my bitmap flies off of the screen. it seems to happen mainly at the start of the program. and it especially occurs the most when i press space at the beginning and then press an arrow key...does this happen to you guys?
also, when you just press the arrow key once does your bitmap take a larger gap rather than when you are holding the arrow key? my bitmap flies far to the left when i press the left arrow key, but when i hold down the left arrow key it only moves slightly to the left. do any of you have this problem?
I use to have that problem, post the code you use to get the input. Here is mine:
Code:while(!key[KEY_ESC])
{
if(key[KEY_UP] && y > 5) // Move up
y = y - 5;
else if(key[KEY_DOWN] && y < 400) // Move down
y = y + 5;
else if(key[KEY_LEFT] && x > 5) // Move left
x = x - 5;
else if(key[KEY_RIGHT] && x < 525) // Move right
x = x + 5;
else if(key[KEY_SPACE]) // Shoot, if there are already 10 bullets, do nothing
{
// Find an empty place in the bullets struct
for(int i = 0; i < MAX_BULLETS; i++)
{
if(bullets[i].exists == 0) // We found an empty slot
{
bullets[i].exists = 1; // Make it exist and set its coords
bullets[i].x = x;
bullets[i].y = y;
}
}
}
josh have you got working bullets... if so can you post some source to show how you did it....
and tech it sounds like a timer problem to me...
btw... all 3 of us are making a shooter? wierd... and cool :)
but not fair cause you guys are better than me :(
Josh, when I use your calculations they work much better. Thanks.:b:
Vicious, I'm getting close to getting working bullets. I just can't seem to get it so old bullets can display once new bullets are being shot. Actually I've been stuck with this problem all day long, and I can't find a way to get it to work. Right now I think I'm close but whatever I'm doing isn't working exactly how I want it to.
Yeah it is pretty cool that each of us are making the same game. I mainly got the idea from you Vicious. And I don't think I'm better than you.
Ok, I've probably spent around 10 hours today trying to get multiple animations to go on at once. I was finally able to do that but it was flicking too much. I could probably go back (ctrl+z) and get to that point, and fix what I know was wrong with that. But I'd rather do it the way I'm doing it now...more time efficient in the drawing routine. But the problem is that it's not working. It compiles in all but it doesn't work the way it should (by should I mean the way I intended for it to work:D ).
Here's the problem: Even when I press space (shoot) twice the drawing routine only shows one shooting animation, and that is the old one. I can't display a new shooting animation until the old animation ends. Unless I start pressing the arrow keys and press shoot and then when I do this it's not what I want it do. It destroys the old animation and creates a new animation, but all that is beside the point right now.
here's the code...I don't see what's wrong with it???:confused:
Btw, I commented the hell out of it to make it as easy as possible for any of you to read.Code:
void user_spacebar()
{
a[0] = y; //setting 1st bullet's y to y
b[0] = x; //setting 1st bullet's x to x
num[0] = a[0]; //setting 1st bullets num to a
do
{
a[0]-=3; // moving the bullets upwards for the 1st
num[0] = a[0];
draw_sprite ( buffer, plane, x, y ); //drawing the plane because the buffer has been cleared
if(num[0] > 119)
{
draw_sprite ( buffer, bullets, b[0], a[0] ); //drawing the 1st bullet to the buffer
}
if(var[1] == 1 && num[1] > 119) //if the 2nd bullet has been initialized to be drawn
{ //initialization can't take place until 1st bullet has been drawn at least once
a[1]-=3; //move the bullets upwards for the 2nd bullet
num[1] = a[1]; //setting 2nd bullets num to a
draw_sprite ( buffer, bullets, b[1], a[1] ); //drawing the 2nd bullet to the buffer
}
if(var[1] == 1 && num[1] < 119) //if 2nd bullet drawing has ended
{
num[1] = 0; //restoring values
var[1] = 0;
}
blit( buffer, screen, 0, 0, 0, 0, 640, 480 ); //copying buffer to the screen
show_video_bitmap(screen); //displaying the screen
clear_bitmap ( buffer ); //clearing the buffer *but not the screen*
if( keypressed() && (key[KEY_SPACE]) && (var[1] != 1)) //if keypressed once while 2nd bullet isn't initialized
{
var[1] = 1; //setting values
a[1] = y;
b[1] = x;
}
if( keypressed() && !key[KEY_SPACE] ) //if any other key besides space has been pressed
{
key_detection(); //going to a key detection function
}
}while((num[0] > 119 || num[1] > 119) && !key[KEY_ESC]); //while 1st or 2nd bullet is initialized or while esc hasn't been pressed
var[1] = 0; //i don't remember lol
}
Thank you.
I did my bullets like this:
Then every time right before I draw to the screen (well, the backbuffer) I call the function updatebullets() which goes through each bullet and subtracts from y, to make it move up the screen. I also have that function change exists to 0 if the bullet goes off the screen, later I will have the code for when a bullet hits something. I loop through it like thisCode:#define MAX_BULLETS 10
struct bullet {
int x, y;
int exists;
} bullets[MAX_BULLETS];
That code loops through the whole bullets array and for everyone that exists it subtract five from y, therfore all the bullets move up the screen.Code:for(int i = 0; i < MAX_BULLETS; i++)
{
if(bullets[i].exists == 1)
{
bullets[i].y -= 5;
}
}
When I create a bullet I have to loop through the bullets array and find the first empty space. If I had an integer for how many bullets are on the screen, and add to it everytime I add a bullet, and delete from it everytime a bullet disapears, I would overwrite them. Here is an example of how that would not work (using the bullet struct from above)
I tried that method first, it would not work, obviously. So now everytime I create a bullet, I have it loop through the bullets array and find the first place where exists = 0; The code for this is not hard, I will show you.Code:int NumberOfBullets = 0;
// Okay lets say the user presses space
// We create a bullet
bullets[NumberOfBullets].exists = 1;
bullets[NumberOfBullets].x = 300;
bullets[NumberOfBullets].y = 400;
int NumberOfBullets++;
// Now lets create another bullet
// Note the first bullets location would be bullets[0]
bullets[NumberOfBullets].exists = 1;
bullets[NumberOfBullets].x = 200;
bullets[NumberOfBullets].y = 450;
NumberOfBullets++;
// Now lets say some time has passed in our game, the first
// bullet is off screen now, but the second is still on screen.
// Now our code would break
// First since our first bullet went off screen we do this
NumberOfBullets--;
// Now what happends if we create a new bullet?
bullets[NumberOfBullets].exists = 1;
bullets[NumberOfBullets].x = 245;
bullets[NumberOfBullets].y = 420;
// Uh oh, we just over wrote our second bullet, they are both
// bullet[1].
Well, I hope I explained everything, here is the code to my game so far, there are no enemies yet, but you can have up to 30 bullets on screen at once, there is no flicker in bullet movement or plane movement. I am still looking for a new plane graphic. I think I am gonna quit with this game, because it is starting to get lame. I need to design what I want the game to be more, before I code. I will work on another game shortly, probably either pong or a space attack game. Here is the full code to the airplane game:Code:for(int i = 0; i < MAX_BULLETS; i++)
{
if(bullets[i].exists == 0)
{
// We found a bullet in the array that does not exist yet
// meaning we can use that place to hold a bullet
// We want to give it coordinates, then set exists to 1 so
// it gets drawn, and nothing overwrites it
bullets[i].x = 200;
bullets[i].y = 200;
bullets[i].exists = 1;
// A problem I had earlier was that I did not put a break
// statement here, that made it create a bullet with the
// same coordinates every place there was a empty bullet
// which meant that I could only have one bullet at a time
break;
}
}
I tried to attach a zip with the graphics, but it was too big. I will upload it to my website and post the url here if anyone wants it. I can also include the source and exec.Code:#include <allegro.h>
#define MAX_BULLETS 10
// Draws ship and bullets to buffer, then
void redraw(); // blits buffer to screen
// Initializes Allegro, sets screen mode
void setup(); // and loads bitmaps
void shutdown(); // Destroys bitmaps and calls allegro_exit()
void updatebullets(); // Updates bullet coordinates
BITMAP *background = NULL; // Clouds background
BITMAP *bullet = NULL; // A bullet...
BITMAP *plane = NULL; // Da plane! Da plan!
BITMAP *buffer = NULL; // The buffer, reduces flicker
// I found the easiest way to implement bullets is to create
// an array of structures, so it is easy to keep track of
// each bullets coordinates and state (exists or not)
struct bullet
{
int x, y;
int exists;
} bullets[MAX_BULLETS];
// The ships coordinates, maybe I should make a ship structure...
int x = 300, y = 375;
int main()
{
setup();
while(!key[KEY_ESC])
{
if(key[KEY_UP] && y > 5) // Move up
y = y - 5;
else if(key[KEY_DOWN] && y < 400) // Move down
y = y + 5;
else if(key[KEY_LEFT] && x > 5) // Move left
x = x - 5;
else if(key[KEY_RIGHT] && x < 525) // Move right
x = x + 5;
else if(key[KEY_SPACE]) // Shoot, if there are already 10 bullets, do nothing
{
// Find an empty place in the bullets struct
for(int i = 0; i < MAX_BULLETS; i++)
{
if(bullets[i].exists == 0) // We found an empty slot
{
bullets[i].exists = 1; // Make it exist and set its coords
bullets[i].x = x;
bullets[i].y = y;
break;
}
}
}
updatebullets();
redraw();
}
shutdown();
return 0;
}
END_OF_MAIN();
void redraw()
{
blit(background, buffer, 0, 0, 0, 0, 640, 480);
draw_sprite(buffer, plane, x, y);
for(int i = 0; i < MAX_BULLETS; i++)
{
if(bullets[i].exists == 1)
{
draw_sprite(buffer, bullet, bullets[i].x, bullets[i].y);
}
}
blit(buffer, screen, 0, 0, 0, 0, 640, 480);
}
void setup()
{
allegro_init();
install_keyboard();
// Set colors depth and set graphics mode
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0);
// Load bitmaps
background = load_bitmap("background.bmp", NULL);
bullet = load_bitmap("bullet.bmp", NULL);
plane = load_bitmap("plane.bmp", NULL);
buffer = create_bitmap(640, 480);
// Make sure all bullets do not exist
for(int i = 0; i < MAX_BULLETS; i++)
{
bullets[i].exists = 0;
}
}
void shutdown()
{
destroy_bitmap(background);
destroy_bitmap(plane);
allegro_exit();
}
void updatebullets()
{
// Delete bullets that go past screen
for(int j = 0; j < MAX_BULLETS; j++)
{
if(bullets[j].exists == 1)
{
if(bullets[j].y <= 0)
bullets[j].exists = 0;
}
}
// Move bullets up the screen
for(int i = 0; i < MAX_BULLETS; i++)
{
if(bullets[i].exists == 1)
{
bullets[i].y -= 5;
}
}
}
Sorry if my previous post did not make sense, it is 5:00 in the morning.
http://www.ciusa.net/~jrgrant/planeall.zip
That is the exec, source, and graphics
http://www.ciusa.net/~jrgrant/planegraphics.zip
That is just the graphics.
TechWins, I was looking through your code, and one of the things that makes it flicker is that you are drawing to the screen, and you do it in different places. The best way to do it, is to have code that parses the user input, it changes different variable, then you have one, and only one function that draws to the screen. Think about it, if you are moving the ship around, and redrawing it with code in the function moveship(), and you have bullets moving and drawing in the function user_space(), then you could not have the ship moving while the bullets are moving. Read that long ass example I just posted. I am learning how to animate sprites right now.
josh... i love you
my ship has bullets now :)
i need to tweak it so it will fire from the center of the ship though...Code:#include <allegro.h>
#define max_laser 15
int counter;
BITMAP *buffer = NULL;
BITMAP *laser = NULL;
void updatelasers();
struct laser
{
int x, y;
int exists;
}lasers[max_laser];
class player
{
public:
int x, y, left, right, fire;
BITMAP* ship;
player(): x(300), y(425), left(0), right(0) { ship = load_bitmap ( "ship.bmp", NULL ); }
~player() { destroy_bitmap(ship); }
void getmove()
{
if ( x > 0 && key[KEY_LEFT] )
{
left = 1;
}
else
{
left = 0;
}
if ( x < 600 && key[KEY_RIGHT] )
{
right = 1;
}
else
{
right = 0;
}
if ( key[KEY_SPACE] )
{
for ( int i = 0; i < max_laser; i++ )
{
if ( lasers[i].exists == 0 )
{
lasers[i].exists = 1;
lasers[i].x = x;
lasers[i].y = y;
break;
}
}
}
updatelasers();
if ( left == 1 ) x -= 10;
if ( right == 1 ) x += 10;
}
void draw(BITMAP* b)
{
draw_sprite ( b, ship, x, 425 );
}
};
void update_counter()
{
counter++;
}END_OF_FUNCTION(update_counter);
int main()
{
allegro_init();
install_keyboard();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0);
buffer = create_bitmap( 640, 480 );
laser = load_bitmap ( "laser.bmp", NULL );
LOCK_VARIABLE(counter);
LOCK_FUNCTION(update_counter);
install_int_ex( update_counter, BPS_TO_TIMER(60));
player *player1 = new player;
do{
while ( counter > 0 )
{
player1->getmove();
counter--;
}
clear(buffer);
player1->draw(buffer);
for(int i = 0; i < max_laser; i++)
{
if( lasers[i].exists == 1 )
{
draw_sprite(buffer, laser, lasers[i].x, lasers[i].y);
}
}
blit( buffer, screen, 0, 0, 0, 0, 640, 480);
}
while ( !key[KEY_ESC] );
delete player1;
allegro_exit();
return 0;
}
END_OF_MAIN();
void updatelasers()
{
for(int j = 0; j < max_laser; j++)
{
if( lasers[j].exists == 1 )
{
if( lasers[j].y <= 0 )
lasers[j].exists = 0;
}
}
for(int i = 0; i < max_laser; i++)
{
if( lasers[i].exists == 1 )
{
lasers[i].y -= 10;
}
}
}
and josh... in your game your ship will not move diagonally...
i thought about making mine move up and down as well but im gonna leave it a simple first game...
>> and i dont think im better than you...
i know you dont think you KNOW :D
here is a screen shot of an intense battle between me and nothing....
That looks good, way better than my bullets. I think I recognize that plane from somewhere, say Total Annihilation? Does your ship move diagonaly? I quit working on my shooter, like I do everything, like the Sniper game, like this Windows text editor I was making. You ever read Pixelate? I think it is cool, I would like to write my own online game programming magazine, but I do not know enough, yet... I am trying to learn to do sprite animation, know any resources?
it doesnt move diagonally but it has b4...
and... I actually got that ship by taking a screen shot of a game called space rocks and then i modified it a bit and poof...
umm... i dont know of anywhere to find animation help...
u know where hasnt helped? :p
does any one know how to make one bitmap
BITMAP* baddy = NULL;
and use it for multiple ships?
i tried just using multiple draw_sprites with different X coords but then i couldnt do collision detection on em...
the only other way i know of would be
BITMAP* baddy1;
BITMAP* baddy2;
ect...