using allegro with RGB image array ?
Eximius

Hello,

This might be an idiotic question, but is it possible to use an RGB array consisting of 3 or 4 bytes per pixel and get it on screen using allegro 4 or 5 in a simple manner (be it windows or a linux based os) ?

I have browsed the wiki and googled around but i could not find an example or anything refering to this way of using Allegro. And if this is unclear, i would use
the following to manipulate the pixels in the array ; (y_pos * (width * bpp) + (x_pos * bpp))

Edgar Reynaldo
Eximius said:

This might be an idiotic question, but is it possible to use an RGB array consisting of 3 or 4 bytes per pixel and get it on screen using allegro 4 or 5 in a simple manner (be it windows or a linux based os) ?

Yes, it is not very hard in either version. Also, it depends on how often you need to do it.

Allegro 4

#SelectExpand
1 BITMAP* img = create_bitmap_ex(32,width,height); 2 if (!img) {bail();} 3 for (int y = 0 ; y < height ; ++y) { 4 for (int x = 0 ; x < width ; ++x) { 5 _putpixel32(img , x , y , makeacol32(data.Red , data.Green , data.Blue , data.Alpha)); 6 7 8 } 9 } 10 11 /// To draw to screen 12 BITMAP* buffer = create_bitmap(screen->w , screen->h); 13 if (!buffer) {bail();} 14 clear_to_color(buffer , makecol(0,0,0)); 15 set_alpha_blender(); 16 draw_trans_sprite(buffer , img , (img->w + buffer->w)/2 , (img->h + buffer->h)/2);// draw centered 17 blit(buffer , screen , 0 , 0 , 0 , 0 , buffer->w , buffer->h);

Allegro 5

#SelectExpand
1 ALLEGRO_BITMAP* bmp = al_create_bitmap(width , height); 2 if (!bmp) {bail();} 3 al_set_target_bitmap(bmp); 4 ALLEGRO_LOCKED_REGION* lock = al_lock_bitmap(bmp , ALLEGRO_PIXEL_FORMAT_RGBA_8888 , ALLEGRO_LOCK_WRITEONLY; 5 for (int y = 0 ; y < height ; ++y) { 6 for int x = 0 ; x < width ; ++x) { 7 // OR, you can premulitply the alpha here, as allegro does 8 al_put_pixel(x,y,al_map_rgba(data.r , data.g , data.b , data.a)); 9 } 10 } 11 al_unlock_bitmap(bmp); 12 13 /// To draw to screen 14 al_set_target_backbuffer(display); 15 16 al_draw_bitmap(bmp , 0 , 0); 17 al_flip_display();

I left out setting up a display with either one, but I thought the tutorials should at least be able to show you how to do that. Check out the Allegro 4 Wiki Tutorial, and the Allegro 5 Tutorials.

Eximius

Ah, nice ! Thank you for that.
I was looking for a library which allows one to do that in a simple and clean way so i wanted to make sure before i dive into Allegro, as a certain simple media library made that ordeal look like a hell to me. ;)

Edgar Reynaldo

Things to keep in mind - draw_trans_sprite is using memory blending and al_draw_bitmap is using hardware acceleration and also unlocking a locked region can be slow because it has to upload to the gpu in case of a video bitmap.

Audric

If you have no control over how the RGB pixel data gets created (memory location, RGB ordering, "pitch" between different lines), SDL has a very handy function which avoids duplicating the image in memory : SDL_CreateRGBSurfaceFrom()

It exists both in SDL 1.2 and SDL 2.0
The typical usage is :

  1. obtain your pixel data

  2. call mySurface = SDL_CreateRGBSurfaceFrom(), it creates a fake Surface which actually points to your pixel data

  3. draw mySurface on screen. ex: SDL_BlitSurface() with SDL 1.2. SDL can easily display an image of any RGB ordering into a screen mode with any RGB ordering.

  4. Free_surface(mySurface) to free the fake Surface

  5. dispose of the original pixel data.

Thread #615170. Printed from Allegro.cc