Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » allegro5, openGL. how to?

This thread is locked; no one can reply to it. rss feed Print
allegro5, openGL. how to?
fend25
Member #14,597
October 2012

I'd like to make a game, using OpenGL and Allegro.
so, how to use OpenGL with Allegro5?
I read a lot of topics, wiki articles and so on (on allegro.wiki good topic is only about allegro4). I searched in google and this forum. And found nothing what can completely describe it.
Does somebody know how to use A5 and Ogl together?

my code:

#SelectExpand
1#include <allegro5\allegro.h> 2#include <allegro5\allegro_opengl.h> 3 4int main(int argc, char **argv) { 5 if(!al_init()) { 6 fprintf(stderr, "failed to initialize allegro!\n"); 7 return -1; 8 } 9 10 al_set_new_display_flags (ALLEGRO_OPENGL); 11 al_set_new_display_option (ALLEGRO_DEPTH_SIZE, 16, ALLEGRO_REQUIRE); 12 13 ALLEGRO_DISPLAY *display = al_create_display (640, 480); 14 if(!display) { 15 fprintf(stderr, "failed to create display!\n"); 16 return -1; 17 } 18 19 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue (); 20 if(!event_queue) { 21 fprintf(stderr, "failed to create event queue\n"); 22 return -1; 23 } 24 25 al_register_event_source (event_queue, al_get_display_event_source (display)); 26 27 28 glOrtho(-1, 1, -1, 1, -1, 1); 29 30 glDisable(GL_DEPTH_TEST); 31 glClearColor (0.5, 0.5, 0.5, 1.0); 32 33 while (1) { 34 ALLEGRO_EVENT event; 35 while (al_get_next_event (event_queue, &event)) { 36 if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 37 al_destroy_event_queue (event_queue); 38 return EXIT_SUCCESS; 39 } 40 41 glBegin(GL_QUADS); 42 //If we set the color to white here, then the textured quad won't be 43 //tinted red or half-see-through or something when we draw it based on 44 //the last call to glColor*(). 45 glColor4ub(255, 255, 255, 255); 46 47 //Draw our four points, clockwise. 48 glTexCoord2f(0, 0); glVertex2f(-0.5, 0.5); 49 glTexCoord2f(1, 0); glVertex2f(0.5, 0.5); 50 glTexCoord2f(1, 1); glVertex2f(0.5, -0.5); 51 glTexCoord2f(0, 1); glVertex2f(-0.5, -0.5); 52 glEnd(); 53 54 al_flip_display (); 55 } 56 } 57}

Kris Asick
Member #1,424
July 2001

OpenGL is just a state machine, so mixing and matching Allegro and OpenGL is easy enough. If you can code in both, then all you need to do is load in Allegro and make OpenGL calls whenever you need to do something Allegro can't do itself. Also, there's a set of OpenGL specific Allegro functions designed to make it easier to use the two together, which you can find information about in the Allegro manual.

However, remember that you need to set ALLEGRO_WINDOWED or ALLEGRO_FULLSCREEN along with the ALLEGRO_OPENGL flag like this:

al_set_new_display_flags(ALLEGRO_FULLSCREEN|ALLEGRO_OPENGL);

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Thomas Fjellstrom
Member #476
June 2000
avatar

ALLEGRO_FULLSCREEN_WINDOW is probably a better option than ALLEGRO_FULLSCREEN.

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

Schyfis
Member #9,752
May 2008
avatar

ALLEGRO_FULLSCREEN_WINDOW is probably a better option than ALLEGRO_FULLSCREEN.

Is there any reason why?

________________________________________________________________________________________________________
[freedwill.us]
[unTied Games]

Elias
Member #358
May 2000

Schyfis said:

Is there any reason why?

Non-native resolutions usually look like crap.

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

Kris Asick
Member #1,424
July 2001

I just use this little routine I whipped up to get the user's desktop res:

void GetDesktopResolution (int &ret_width, int &ret_height)
{
  RECT rc;
  GetWindowRect(GetDesktopWindow(),&rc);
  ret_width = rc.right - rc.left;
  ret_height = rc.bottom - rc.top;
}

If it fails to set resolution, which is only going to happen on multi-monitor systems, I scale back to safe values in an attempt to get the game to start, then let the user try to set their own resolution settings in the game options.

Granted, this code only works in Windows, so if you're going cross-platform you'll need other solutions too.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Arthur Kalliokoski
Second in Command
February 2005
avatar

They all watch too much MSNBC... they get ideas.

Go to: