|
using allegro with RGB image array ? |
Eximius
Member #15,901
March 2015
|
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
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
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 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 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. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Eximius
Member #15,901
March 2015
|
Ah, nice ! Thank you for that.
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
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. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Audric
Member #907
January 2001
|
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
|
|