Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How to draw to bitmap, then blit to screen later, OGL

Credits go to Trent Gamblin for helping out!
This thread is locked; no one can reply to it. rss feed Print
How to draw to bitmap, then blit to screen later, OGL
Arthur Kalliokoski
Second in Command
February 2005
avatar

This simplified program illustrates the problem, as it is, I get

r: /home/prog/allegro-5.0.8/src/bitmap.c:210: _bitmap_drawer: Assertion `bitmap != dest && bitmap != dest->parent' failed.
Aborted

I also tried getting the texture ID and drawing it with glBegin(GL_QUADS), but it only showed a black rectangle instead of the light gray and dark gray associated with blank alpha backgrounds.

Cookies await!

#SelectExpand
1 2#include <stdio.h> 3#include <allegro5/allegro.h> 4#include <allegro5/allegro_opengl.h> 5#include <allegro5/allegro_ttf.h> 6#include <allegro5/allegro_primitives.h> 7#include <allegro5/allegro_image.h> 8 9#define SCRW 640 10#define SCRH 480 11 12 13int main(int argc, char **argv) 14{ 15 ALLEGRO_EVENT_QUEUE *queue; 16 ALLEGRO_TIMER *timer; 17 ALLEGRO_DISPLAY *display; 18 ALLEGRO_BITMAP *alphaback; 19 ALLEGRO_EVENT event; 20 int display_width = SCRW; 21 int display_height = SCRH; 22 int frames; 23 int redraw = 0; 24 int i,j; 25 26 if (!al_init()) 27 { 28 fprintf(stderr,"Could not init Allegro.\n"); 29 return 1; 30 } 31 32 al_install_keyboard(); 33 al_install_mouse(); 34 al_init_image_addon(); 35 al_init_primitives_addon(); 36 37 al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); 38 al_set_new_display_option(ALLEGRO_SAMPLES, 4, ALLEGRO_SUGGEST); 39 al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_SUGGEST); 40 al_set_new_display_flags(ALLEGRO_OPENGL); 41 display_width = SCRW; 42 display_height = SCRH; 43 display = al_create_display(display_width, display_height); 44 if (!display) 45 { 46 fprintf(stderr,"Could not create display.\n"); 47 return 1; 48 } 49 50 alphaback = al_create_bitmap(512,512); 51 if(!alphaback) 52 { 53 fprintf(stderr,"can't create bitmap\n"); 54 return 1; 55 } 56 57 al_set_target_bitmap(alphaback); 58 al_clear_to_color(al_map_rgba(128,128,128,255)); 59 60 for(j=0;j<512;j+=32) 61 { 62 for(i=0;i<512;i+=32) 63 { 64 al_draw_filled_rectangle(j,i,j+16,i+16,al_map_rgba(64,64,64,255)); 65 al_draw_filled_rectangle(j+16,i+16,j+32,i+32,al_map_rgba(64,64,64,255)); 66 } 67 } 68 69 al_set_target_bitmap(al_get_backbuffer(display)); 70 71 timer = al_create_timer(1.0 / 60.0); 72 73 //al_set_window_position(display,320,240); 74 queue = al_create_event_queue(); 75 al_register_event_source(queue, al_get_keyboard_event_source()); 76 al_register_event_source(queue, al_get_display_event_source(display)); 77 al_register_event_source(queue, al_get_mouse_event_source()); 78 al_register_event_source(queue, al_get_timer_event_source(timer)); 79 80 al_start_timer(timer); 81 82 while (true) 83 { 84 al_wait_for_event(queue, &event); 85 { 86 switch (event.type) 87 { 88 case ALLEGRO_EVENT_DISPLAY_CLOSE: 89 goto done; 90 91 case ALLEGRO_EVENT_TIMER: 92 { 93 redraw++; 94 } 95 } 96 97 while( (redraw > 0) && al_is_event_queue_empty(queue)) 98 { 99 al_clear_to_color(al_map_rgba(255,0,0,255)); 100 101 al_draw_bitmap(al_get_backbuffer(display),20.0,20.0,0); 102 103 al_flip_display(); 104 //printf("root %d child %d grand %d\n",root.active,child.active,grandchild.active); 105 frames++; 106 redraw--; 107 } 108 } 109 } 110 111done: 112 113 al_destroy_event_queue(queue); 114 115 return 0; 116}

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Trent Gamblin
Member #261
April 2000
avatar

On line 101 you're trying to draw the backbuffer to the backbuffer. I think you meant al_draw_bitmap(alphaback....

Arthur Kalliokoski
Second in Command
February 2005
avatar

I didn't see that, pushing too hard, I guess. OTOH, it still doesn't draw anything (in the main program). That square in the middle is a bevel drawn separately, the image should be inside it.

{"name":"607079","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/8\/a8fa7c20a81b822305211b24a444479f.png","w":809,"h":633,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/8\/a8fa7c20a81b822305211b24a444479f"}607079

[EDIT]

Whoopsy, I tried it in the example program just for completeness, and it showed the squares! I should be able to figure it out now. Thanks, Trent!

[EDIT2]

Well, no. I'll just malloc a block, set some "pixels" and glBindTexture() it like the FSM intended.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Thomas Fjellstrom
Member #476
June 2000
avatar

Well, no. I'll just malloc a block, set some "pixels" and glBindTexture() it like the FSM intended.

Could also al_lock_bitmap and access the raw bitmap data. But I think you're just doing something odd.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Trent Gamblin
Member #261
April 2000
avatar

Ah, that looks like a WinXP window border? The real thing? Because on Windows with DirectX you can't lock the backbuffer IIRC. Though I'm not sure you're doing that anyway. Just sayin'. You could try al_set_new_display_flags(ALLEGRO_OPENGL) before creating the display if you ARE on Windows, and see if that helps.

Arthur Kalliokoski
Second in Command
February 2005
avatar

It's not windows, it's xfce with RedmondXP style and something called "Raleigh". But for what it's worth, the code specifies OpenGL and even inits the primitive addon.

[EDIT]

Well, I just tried it again, (since the font shows OK), and I could load and display an image file just fine. Then I tried using al_map_rgba() like in the example instead of using my pre-defined array of 4 float colors and it worked! This array of colors is used for drawing everything else without problems, including the font, and all the alphas in it are 1.0.

[EDIT2]

Looking some more, there's a function "zz_init_gui()" that initializes these colors, and somehow I forgot about it and tried to draw to the alphaback bitmap before zz_init_gui() was called. Where's the blushing embarrassed smiley?

#SelectExpand
1 2void zz_init_gui(ALLEGRO_DISPLAY *display) 3{ 4 int i; 5 for(i=0;i<10;i++) 6 { 7 zz_gui_colors[SELECTED][i] = al_map_rgba_f(colordefs[SELECTED][i].FLOAT_RED,colordefs[SELECTED][i].FLOAT_GREEN,colordefs[SELECTED][i].FLOAT_BLUE,colordefs[SELECTED][i].FLOAT_ALPHA); 8 zz_gui_colors[UNSELECTED][i] = al_map_rgba_f(colordefs[UNSELECTED][i].FLOAT_RED,colordefs[UNSELECTED][i].FLOAT_GREEN,colordefs[UNSELECTED][i].FLOAT_BLUE,colordefs[UNSELECTED][i].FLOAT_ALPHA); 9 } 10 11 active_gl = al_get_display_flags(display) & ALLEGRO_OPENGL; 12 if(active_gl & ALLEGRO_OPENGL) 13 { 14 zz_draw_focus = &zz_draw_ogl_focus; 15 } 16 else //fall through to direct3d 17 { 18 zz_draw_focus = &zz_draw_d3d_focus; 19 } 20}

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Trent Gamblin
Member #261
April 2000
avatar

There is no shame in fixing bugs in your programs. Only in giving up. </stuff you already know>

Arthur Kalliokoski
Second in Command
February 2005
avatar

I was getting in too much of a hurry. Drawing to a bitmap with A5 seemed weird, and I knew it needed to be done after creating a display, so I put it as the very next thing. I was snagged by this zz_init_gui() another time a couple of months ago, so maybe I should make a wrapper func that creates a display and initializes these colors transparently.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Trent Gamblin
Member #261
April 2000
avatar

Hell, there isn't even shame in giving up. Just do what makes you happy. D:

EDIT: I'm trying not to sound philosophical here, but I think I'm failing.

Arthur Kalliokoski
Second in Command
February 2005
avatar

If I was going to give up on anything, it'd be A5 (which I've threatened to do before), but since the core library is being tested on hundreds of computers, I couldn't hope to match that with my own stuff based on NeHe tutorials.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Thomas Fjellstrom
Member #476
June 2000
avatar

The nice thing about A5, is the more time goes on, and the more people like you we have using it, the better it'll get.

I find it sad when we get a new user who can't seem to get it work for them, and then has to switch back to whatever they were using due to time constraints or frustration overload.

That reminds me, someone was having issues with shaders and multi texturing. He just couldn't get allegro to work when setting up multiple textures which were then passed into his shader.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Elias
Member #358
May 2000

Maybe we should make ex_shader use two bitmaps. I'd expect that all you need is:

al_set_shader_sampler(shader, "my_bitmap_1", my_bitmap_1, 0);
al_set_shader_sampler(shader, "my_bitmap_2", my_bitmap_2, 1);

So quite trivial. But if someone has problems with it, can never hurt to have it in an example.

--
"Either help out or stop whining" - Evert

Go to: