Jump to content
  • Advertisement
Sign in to follow this  
budokai

[newbie allegro]a sprite on a background

This topic is 4885 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello boys, I am trying to draw in a window a background with over one sprite free of moving.I have understood some, is the background that whichever other thing that I want to design I must consider like one bitmap (comprised a buffer generic).
BITMAP *sfondo, *my_pic1; //Dichiara una BITMAP chiamata sfondo
sfondo = load_pcx("images/background.pcx", NULL); 
my_pic1= load_bitmap("images/goku.bmp", NULL);

[/source] Idem per il buffer
[SOURCE]
BITMAP *buffer; //Dichiara una BITMAP chiamata buffer.
buffer = create_bitmap(160,120); //Crea una bitmap vuota (vedi dimensione di my_pic).


I draw the background:
blit(sfondo, screen, 0,0,0,0,SCREEN_HT,SCREEN_WT);//Disegna lo sfondo


Then in a while loop (in which it is exited alone on pressure of key ESC) I put the instructions (beyond to those in order managing the movement of the sprite ones my_pic1):
 acquire_screen();// acquisisci lo schermo
 draw_sprite(buffer, my_pic1, my_pic_x, my_pic_y);//Disegna la figura nel buffer
 blit(buffer, screen, 0,0,0,0,160,120);//Disegna il buffer nello schermo
 clear_bitmap(buffer); // Ripulisci il contenuto del buffer bitmap
  release_screen();// Rilascia lo schermo


Using this code design the background and my_pic, the fact is that not as soon as I try to move my_pic, the background, there was my_pic becomes black (of fact comes rewrite) and moreover my_pic in motion to scompare under the background and more is not looked at.... How I can resolve the problem and move my_pic over the background without some problem? According to you, I would have to put also the background in a buffer? thanks p.s Excuse my very bad english

Share this post


Link to post
Share on other sites
Advertisement
English comments would have been nice. [smile] But why is your buffer so tiny? It's usually supposed to be screen-sized (SCREEN_W x SCREEN_H) ... and you should be blitting both the background and the sprite to it before you blit the buffer to the screen ....

Share this post


Link to post
Share on other sites
ok, thank for reply, now I post the entire code, perhaps it will render the problem more clearly.

#include <allegro.h>// Devi includere il file header di allegro

#define SCREEN_HT 1024
#define SCREEN_WT 768

volatile long speed_counter = 0;//Un intero (long) che memorizzerà il valore del contatore della velocità (ndt lo chiamo speed counter per brevità)

void increment_speed_counter() // Una funzione che incrementa lo speed counter
{
speed_counter++; // Incrementa di uno lo speed counter :)
}
END_OF_FUNCTION(increment_speed_counter);//Assicurarti di chiamare questo per chiudere la funzione



int main(int argc, char *argv[])
{
allegro_init(); // Inizializza Allegro
install_keyboard(); // Inizializza tutte le procedure adibite all'uso della tastiera
install_timer(); // Inizializza le timer routines

LOCK_VARIABLE(speed_counter); //Usato per settare il timer - esso regola la velocità del gioco
LOCK_FUNCTION(increment_speed_counter);
install_int_ex(increment_speed_counter, BPS_TO_TIMER(60));//Setta il numero di BPS

set_color_depth(32); // Imposta la profondità di colore a 32

set_gfx_mode(GFX_AUTODETECT, SCREEN_HT,SCREEN_WT,0,0); // Cambia la modalità grafica a 1024x768


BITMAP *sfondo, *my_pic1; //Dichiara una BITMAP chiamata sfondo
sfondo = load_pcx("images/background.pcx", NULL);
my_pic1= load_bitmap("images/goku.bmp", NULL);


BITMAP *buffer; //Dichiara una BITMAP chiamata buffer.

buffer = create_bitmap(160,120); //Crea una bitmap vuota (vedi dimensione di my_pic).

blit(sfondo, screen, 0,0,0,0,SCREEN_HT,SCREEN_WT);//Disegna lo sfondo

/************************************
Dichiara alcuni interi per un uso successivo:
***********************************/

int my_pic_x = 0;// Mantiene le coordinate X della figura
int my_pic_y = 0;// Mantiene le coordinate Y della figura

while(!key[KEY_ESC])//Se l'utente preme il tasto ESC, il programma esce
{
while(speed_counter > 0)
{
/*****************************
Ecco una serie di tests - possiamo
muovere la bitmap per tutto lo schermo
****************************/


if(key[KEY_RIGHT])// Se l'utente preme la freccia destra, cambia le coordinate X della figura
{
my_pic_x ++;// movimento a destra, aumenta di 1 la coordinata X della figura
}
else if(key[KEY_LEFT])// altrimenti - se si preme la freccia sinistra
{
my_pic_x --;// movimento a sinistra, diminuisce di 1 la coordinata X della figura
}
else if(key[KEY_UP])// Se l'utente preme la freccia SU, cambia le coordinate Y della figura
{
my_pic_y --;// movimento verso sopra, diminuisce di 1 la coordinata Y della figura
}
else if(key[KEY_DOWN])// altrimenti - se si preme la freccia basso
{
my_pic_y ++;// movimento in basso, aumenta di 1 la coordinata Y della figura
}
speed_counter --;
}//Chiude il test (speed_counter > 0)

acquire_screen();// acquisisci lo schermo
draw_sprite(buffer, my_pic1, my_pic_x, my_pic_y);//Disegna la figura nel buffer
blit(buffer, screen, 0,0,300,500,160,120);//Disegna il buffer nello schermo
clear_bitmap(buffer); // Ripulisci il contenuto del buffer bitmap


release_screen();// Rilascia lo schermo
}//Si chiude il test (key[KEY_ESC])

destroy_bitmap(buffer);//Rilascia l memoria occupata dal buffer
destroy_bitmap(my_pic1);//Rilascia la memoria occupata dalla bitmap
destroy_bitmap(sfondo);

return(0);// Esci senza errori
}
END_OF_MAIN();


Share this post


Link to post
Share on other sites
Wow; no one helped with this yet. [wow]

It's pretty much what I said; you need a screen sized bitmap which sfondo and my_picl get blitted to (in that order) every frame. That's the easy way, anyway; if that seems wasteful, you can look into some kind of "dirty rectangle" system to only update what parts of the screen have changed. But the basic problem remains that when the sprite is blit to the screen, the background is overwritten and lost, and needs to be reblit.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!