Advertisement

problem moving images (with ALLEGRO)

Started by January 27, 2005 06:26 PM
11 comments, last by BlackWind 19 years, 7 months ago
hi, i have this code: #include <allegro.h> #include <alleggl.h> #pragma comment(lib, "opengl32.lib") #pragma comment(lib, "glu32.lib") const char* g_AppName = "Allegro Prueba"; int bpp = 0; int SCREEN_WIDTH = 0; int SCREEN_HEIGHT = 0; const char * imageName = "ball.bmp"; int main(int argc , char *argv[]) { // init if(allegro_init() != 0) return 0; // keyboard if( install_keyboard() != 0 ) return 0; BITMAP * the_image; PALETTE pal; the_image = load_bmp(imageName,pal); set_keyboard_rate(200,200); set_window_title( g_AppName ); bpp = desktop_color_depth(); if( bpp != 0) set_color_depth(bpp); // seteamos la que tiene el escritorio else { bpp = 8; set_color_depth(bpp); // los bpp seran de 8 } if( get_desktop_resolution( &SCREEN_WIDTH , &SCREEN_HEIGHT ) == 0 ); else { SCREEN_WIDTH = 800; SCREEN_HEIGHT = 600; } if( set_gfx_mode( GFX_AUTODETECT_WINDOWED, SCREEN_WIDTH/2 , SCREEN_HEIGHT/2 , 0 , 0 ) != 0 ) return 0; bool end = false; while ( !end) { blit(the_image, screen, 0, 0, (SCREEN_W-the_image->w)/2, (SCREEN_H-the_image->h)/2, the_image->w, the_image->h); // int val = readkey(); if ( key[KEY_ESC] ) end = true; else if( key[KEY_RIGHT] ) the_image->w += 1; else if( key[KEY_RIGHT] ) the_image->w -= 1; } return 0; } END_OF_MAIN(); this is the image (la imagen es esta:) ball.bmp
and when i tried to move it it does this: des.bmp
it changes the color and deform it.....how can i solve it?
I don't use palettes so this is just a guess, but while I see you loading it, I don't see you calling set_palette() anywhere ....

Jesus saves ... the rest of you take 2d4 fire damage.

Advertisement
If it's a high/true color bitmap, then you can pass NULL as the palette. If it's an 8-bit palette, then you will need to make a call to set_palette().

However, since you are doing "bpp = desktop_color_depth();", you are most likely not using 8-bit palettes. Your problem is most likely because you are loading the image before you are setting the graphics mode. You need to set the graphics mode first so that Allegro knows whether to use RGB or BGR.
I think the problem is that your blit function is wrong:
blit(the_image, screen, 0, 0, (SCREEN_W-the_image->w)/2, (SCREEN_H-the_image->h)/2, the_image->w, the_image->h);


I have never used allegro before but one example I found of blit is:
 blit(my_pic, screen, 50,50,100,100,150,150);//Draw from 50,50 to 150,150 on the screen at (100,100)


I say that because it looks like you are blitting incorrect places from your bitmap image. Try something like:
 blit(the_image, screen, 0,0,32,32,0,0);


Just to see if the image is displayed correctly. If not than it has to do with somethng else.

- Drew
well, i changed a little my code, and now it loads the ball with correct color.
but when i add the code to clear the bitmap (clear_bitmap(the_image))
i only draws a very small point.

can someone put a very small code to move a image to the rigth in the screen? or tell me how to solve my problem
(the real problem is not blitting it, but trying to move it)
read the allegro documentation, read more and dont be slack
Advertisement
well, i changed my code, and it almost works, (i had to clear the screen not the bitmap), but now the problem is that now it blinks.....
Quote: Original post by BlackWind
well, i changed my code, and it almost works, (i had to clear the screen not the bitmap), but now the problem is that now it blinks.....


You will probabally need to do double buffering. Take a look at this tutorial that shows an example of how to accompish this.

- Drew
thanks a lot, that was just what i needed, now it works perfectly

i implented this in my while:

draw_sprite(dbuffer, the_image,x,0);
blit(dbuffer, screen, 0, 0, 0, 0, SCREEN_WIDTH/2, SCREEN_HEIGHT/2);

but now, i have one more question........why dont i need to clean the screen?? isnt it supossed that it will draw one image over another??
Awesome!

Quote:
but now, i have one more question........why dont i need to clean the screen?? isnt it supossed that it will draw one image over another??


You probabally do still need to clear the screen, especially when you have a lot of images moving at once, but I'm not too sure. I have never used Allegro before so someone else will have to answer that. Maybe the method you are using to clear the screen is wrong? I'm not too sure.

- Drew

This topic is closed to new replies.

Advertisement