Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How to make allegro5 do use the display of an OpenGL application?

This thread is locked; no one can reply to it. rss feed Print
How to make allegro5 do use the display of an OpenGL application?
aniquilator
Member #9,841
May 2008
avatar

The point is, I don't want to create a display with allegro:

mydisplay = al_create_display(320, 240);

I want to allegro to use an OpenGL context besides using its own display, like I have a GTK application with an OpenGL widget, all I want to do is:

HEY allegro! Dont need to create another display, use this one!

mydisplay = al_get_display("???WINDOW ID???");

Is there a way to do so?

thanks in advance.

Trezker
Member #1,739
December 2001
avatar

Just do your opengl stuff in the Allegro display. Just make sure it has an opengl context.

http://alleg.sourceforge.net/a5docs/refman/display.html#al_set_new_display_flags

aniquilator
Member #9,841
May 2008
avatar

I think you dont understand my question..
Its like, I have an opengl context already initialized, and I want to allegro5 to use this context, don't create its own display, I will post my test code here:

#SelectExpand
1#include <gtk/gtk.h> 2#include <gtk/gtkgl.h> 3#include <allegro5/allegro.h> 4#include <allegro5/allegro_opengl.h> 5#include <allegro5/allegro_image.h> 6#include <allegro5/allegro_primitives.h> 7 8ALLEGRO_BITMAP* bmp; 9ALLEGRO_DISPLAY* mydisplay; 10 11static gboolean expose (GtkWidget *da, GdkEventExpose *event, gpointer user_data) 12{ 13 GdkGLContext *glcontext = gtk_widget_get_gl_context (da); 14 GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(da); 15 16 if (!gdk_gl_drawable_gl_begin (gldrawable, glcontext)) 17 { 18 g_assert_not_reached (); 19 } 20 21 /* ALLEGRO */ 22 int x=10,y=10; 23 int W=50,H=50; 24 25 al_set_target_backbuffer(mydisplay); 26 al_clear_to_color(al_map_rgba_f(0, 0, 1, 255)); 27 al_draw_filled_rectangle(x, y, x + W, y + H, al_map_rgba_f(1, 0, 0, 1)); 28 al_draw_filled_rectangle(0, 0, W, H, al_map_rgba_f(0, 1, 0, 0.5)); 29 al_draw_bitmap(bmp, 100, 100, 0); 30 /* ALLEGRO */ 31 32 if (gdk_gl_drawable_is_double_buffered (gldrawable)) 33 { 34 gdk_gl_drawable_swap_buffers (gldrawable); 35 } 36 else 37 { 38 glFlush (); 39 } 40 gdk_gl_drawable_gl_end (gldrawable); 41 return TRUE; 42} 43 44int main (int argc, char **argv) 45{ 46 GtkWidget *window; 47 GtkWidget *da; 48 GdkGLConfig *glconfig; 49 50 gtk_init (&argc, &argv); 51 gtk_gl_init (&argc, &argv); 52 53 window = gtk_window_new (GTK_WINDOW_TOPLEVEL); 54 gtk_window_set_default_size (GTK_WINDOW (window), 800, 600); 55 56 da = gtk_drawing_area_new (); 57 58 gtk_container_add (GTK_CONTAINER (window), da); 59 g_signal_connect_swapped (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); 60 gtk_widget_set_events (da, GDK_EXPOSURE_MASK); 61 62 gtk_widget_show (window); 63 64 /* prepare GL */ 65 glconfig = gdk_gl_config_new_by_mode (GDK_GL_MODE_RGB | GDK_GL_MODE_DEPTH | GDK_GL_MODE_DOUBLE); 66 if (!glconfig) 67 { 68 g_assert_not_reached (); 69 } 70 71 if (!gtk_widget_set_gl_capability (da, glconfig, NULL, TRUE, GDK_GL_RGBA_TYPE)) 72 { 73 g_assert_not_reached (); 74 } 75 76//-------------------------------ALLEGRO INIT--------------------------------------------- 77 al_init(); 78 al_init_image_addon(); 79 bmp = al_load_bitmap("hello.bmp"); 80 81 al_set_new_display_flags(ALLEGRO_OPENGL); 82 83 mydisplay = al_create_display(800, 600); 84 printf("ok\n"); 85//-------------------------------ALLEGRO INIT--------------------------------------------- 86 87 g_signal_connect (da, "expose-event" ,G_CALLBACK (expose) , NULL); 88 gtk_widget_show_all (window); 89 gtk_main (); 90} 91/*You can compile it in linux this way: 92gcc -o gtkAl_example `pkg-config --cflags --libs gtk+-2.0 gtkglext-1.0 gtkglext-x11-1.0` `pkg-config --libs allegro-4.9 allegro_image-4.9 allegro_primitives-4.9` gtkAl_example.c 93*/

As you can see, GTK creates my openGL context, so I want to use it, instead of create a new one... something like that.???
thanks in advance.

jmasterx
Member #11,410
October 2009

What's wrong with the GL context that Allegro makes? It can do everything the gdk window can.

aniquilator
Member #9,841
May 2008
avatar

I know that, but the problem is: I want allegro to draw into a GTK's GUI.
Like I will build my gui in gtk, and draw with allegro in a widget. Its like embed allegro in a gtk widget... For that I need allegro to receive the display that gtks create for an opengl context. The way I described in my last post I can draw into the gtk display, but I have an allegro display all black messing around, so I just want to eliminate it. I don't know if I have explained better :(...:-/

SiegeLord
Member #7,827
October 2006
avatar

I don't think its possible right now. If it were possible, how do you imagine input and other things would work? Or are you looking only for graphical output?

Anyway... what happens when you destroy the allegro display?

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

aniquilator
Member #9,841
May 2008
avatar

I just want it for graphics display. :)
Its for an editor I'm doing.
When I destroy the allegro display it works just fine. I've putted this after gtk_main():

#SelectExpand
1al_destroy_bitmap(bmp); 2al_destroy_display(mydisplay); 3al_uninstall_system();

So basically I don't want allegro to deal nothing more than drawing purposes.

Evert
Member #794
November 2000
avatar

I suppose it would be possible to have a function to tell Allegro to use an existing OpenGL context for its rendering, something like bool al_create_display_from_opengl_context(context). Not sure if the input type would be the same on all platforms, but that's something we could work around (it could take whatever the correct datatype is on each respective platform).

Not sure we should be persuing that for 5.0, but I do think it should be possible (and probably quite useful as well).

As for how you do it, right now, you don't...

Thomas Fjellstrom
Member #476
June 2000
avatar

or we can provide some way to tell allegro to embed its window in another?

--
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

Evert
Member #794
November 2000
avatar

or we can provide some way to tell allegro to embed its window in another?

Maybe. I suspect that's more difficult to do in a "platform neutral" way.
Obviously, there's not going to be a "true" platform neutral way to set up the window without using Allegro (you need OS-specific code to do that, or toolkit-specific code, in this case GTK) even if you have a "platform neutral" (in the sense of same function name and same argument list) way of setting the OpenGL context.
I guess the phrase I'm looking for is "platform agnostic".

Put it this way: if we have a way to tell Allegro to use a particular OpenGL context (same prototype and argument list on all platforms), that's fairly easy to maintain. The alternative is that we need platform-specific functions for each platform to let Allegro embed its own OpenGL context in another window. I think that's more hassle and work on our part and ultimately not much easier for the end user.

Elias
Member #358
May 2000

I agree, a way to use an existing OpenGL context sounds like a good idea and shouldn't be hard to do. I think it could just be a new display driver, then create the display like normal with al_create_display. (Seems we forgot to ever add a way to select a specific display driver though.)

The driver would just fill in a dummy ALLEGRO_DISPLAY. Window-specific functions like setting the title would be no-ops, drawing operations would simply use the current OpenGL context.

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

Thomas Fjellstrom
Member #476
June 2000
avatar

It is probably the best solution.

--
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

Evert
Member #794
November 2000
avatar

Oh, one caveat with this idea: you will probbaly not be able to use Allegro input at all (on OS X anyway). I don't suppose that's a problem for the most part (if you're using a different toolkit to manage the window, you're probably using it to record input as well) but it's something to warn the user of. It could be slightly annoying if all you wanted to do is add a menubar to your Allegro application.

aniquilator
Member #9,841
May 2008
avatar

I agree with all you have said. =)
The best solution maybe just create a display based in the opengl context. That would be definitely perfect!

And the user will have to be aware of the limitation of this approach as well.

I will take a look at allegro's source, in the display creation, if I see where it creates the opengl context, or something like that. =)

Go to: