Mail Archives: djgpp/1998/02/26/02:45:24
C.Gugler wrote:
> I'm having trouble blitting a background to a simple Pong Game I'm
> making with DJGPP and Allegro. Before I put the code in to put a
> background bmp file in, it ran fine, at just the right speed. After It
> started going slower and the Ball moves 'choppy'. Is there anyway I can
> only have the bitmap drawn once without it have to redraw the whole
> thing every frame? Right Now i'm using double buffering, I was thinking
> of trying Dirty Rectangles if that would go quicker. Anyways, here's
> the code, If you have any suggestions, I'd be greatful.. Thanks in
> Advance.. Oh yeah, also if you have any info on how I could make it run
> as slow/fast on all machines(A pentium 300 won't go too much(or any)
> faster than a 486, or low-end pentium.)
>
> This is just the bottom portion of the C source file..
One thing you could do is have a background preset just before the main loop. Then copy the bitmap to another temp
bitmap (double buffering) on which everything is drawn.
>
>
> void pong_game(void)
> {
> /* Set the first random direction for the ball */
> direction=random_direction();
> delay(200);
BITMAP *background=create_bitmap(640,480);
clear_to_color(background, 83);
blit(backdrop,background,0,0,10,30,620,455);
// you can add the some next on this background
// might need a change in background
int redraw=0;
>
>
> /*Play the game while the ESC key is not pressed*/
> while(!key[KEY_ESC])
> {
>
> /* Used to change music... */
> if(changem>9)
> changem=0;
> if(changem>0)
> changem++;
>
> /* put background in, Too slow! */
blit(background,buffer,0,0,0,0,640,480);
if (redraw!=0){
clear_to_color(background, 83);
blit(backdrop,background,0,0,10,30,620,455);
// and other redrawing
redraw=0;
}
> move_ball();
> key_respond(); // respond to input and display paddle's
>
> /* Score's and Misc Info around the play area */
> textout(buffer,pong_datafile[pfont].dat,"Player 1 Score:",150,0,21);
>
> textout(buffer,pong_datafile[pfont].dat,itoa(score_p1,NULL,10),text_length(pong_datafile[pfont].dat,"Player
> 1 Score:")+150,0,21);
> textout(buffer,pong_datafile[pfont].dat,"Player 2
> Score:",350,0,21);
>
> textout(buffer,pong_datafile[pfont].dat,itoa(score_p2,NULL,10),text_length(pong_datafile[pfont].dat,"Player
> 2 Score:")+350,0,21);
>
> textout(buffer,pong_datafile[pfont].dat,"A/Z",0,0,14);
>
// some of this text could be put into the background image
> textout(buffer,pong_datafile[pfont].dat,"Current Music
> Selection:",230,20,14);
>
> textout(buffer,pong_datafile[pfont].dat,itoa(cpong_midi,NULL,10),text_length(pong_datafile[pfont].dat,"Current
> Music Selection:")+230,20,14);
>
> textout(buffer,pong_datafile[pfont].dat,"Up/Down",618-text_length(pong_datafile[pfont].dat,"Up/Down"),0,14);
>
> textout(buffer,pong_datafile[pfont].dat,"Press 'M' to change
> Music",210,426,14);
>
> textout(buffer,pong_datafile[pfont].dat,"Press 'Esc' to
> Exit",210,4380,14);
>
> textout(buffer,pong_datafile[pfont].dat,"Special Thanks to, DJ
> Delorie(DJGPP), Shawn Hargreaves(Allegro), ",0,455,14);
>
> textout(buffer,pong_datafile[pfont].dat,"and anyone else who helped with
> Allegro or DJGPP.",0,465,14);
>
> /* Draw a line to set the boundries */
> line(buffer,0,30,640,30,252);
> line(buffer,0,425,640,425,252);
>
> /* Put the buffer screen on the screen */
> vsync();
> blit(buffer,screen,0,0,0,0,640,480);
>
> /*Clear the buffer*/
> clear_to_color(buffer, 83);
> }
> return;
> }
>
> int main(void)
> {
> allegro_init();
> install_timer();
> install_keyboard();
> initialise_joystick();
> install_mouse();
>
> if((pong_datafile=load_datafile("Cpong.dat"))==NULL)
> {
> /*Shutdown Allegro*/
> allegro_exit();
> /*Print error message*/
> printf("Error loading \"Cpong.dat\"\n%s\n\n", allegro_error);
> exit(1);
> }
>
> printf("Setting up the Sound...\n");
>
> if(install_sound(DIGI_AUTODETECT,MIDI_AUTODETECT,NULL) != 0)
> {
> allegro_exit();
> /*Print error message*/
> printf("Error setting up Sound System\n%s\n\n", allegro_error);
>
> exit(1);
>
> }
>
> printf("Setting up graphics mode 640x480...\n");
>
> if (set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0) != 0)
> {
> allegro_exit();
> /* Print error message */
> printf("Error setting graphics mode 640 x 480\n%s\n\n",
> allegro_error);
> exit(1);
> }
>
> /* Set the pallete for the bitmap */
> set_pallete(pong_datafile[pal2].dat);
>
> backdrop=create_bitmap(620,455);
> blit(pong_datafile[titleb].dat, backdrop, 0, 0, 0, 0, 620, 455);
>
> buffer=create_bitmap(640,480);
> clear_to_color(buffer, 83);
>
> /*Set the Ball position*/
> ball_x=SCREEN_W/2;
> ball_y=35;
>
> play_midi(pong_datafile[xcircus].dat, TRUE);
>
> while(!keypressed())
> {
> draw_rle_sprite(buffer,pong_datafile[title].dat,0,0);
> textout(buffer,pong_datafile[pfont].dat,"Welcome...",3,468,5);
> blit(buffer,screen,0,0,0,0,640,480);
> }
> /* Set the Pallete for the Sprites */
> set_pallete(pong_datafile[pal].dat);
> /*Play the Game*/
> clear_to_color(buffer, 83);
> pong_game();
>
> stop_midi();
> allegro_exit();
>
> if(score_p1>score_p2)
> printf("Player 1 wins! \n");
> if(score_p2>score_p1)
> printf("Player 2 wins! \n");
> if(score_p1==score_p2)
> printf("Player 1 and Player 2 Tied! \n");
> return;
> }
- Raw text -