![]() |
|
allegro5, openGL. how to? |
fend25
Member #14,597
October 2012
|
I'd like to make a game, using OpenGL and Allegro. my code: 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) |
Thomas Fjellstrom
Member #476
June 2000
![]() |
ALLEGRO_FULLSCREEN_WINDOW is probably a better option than ALLEGRO_FULLSCREEN. -- |
Schyfis
Member #9,752
May 2008
![]() |
Thomas Fjellstrom said: ALLEGRO_FULLSCREEN_WINDOW is probably a better option than ALLEGRO_FULLSCREEN. Is there any reason why? ________________________________________________________________________________________________________ |
Elias
Member #358
May 2000
|
Schyfis said: Is there any reason why? Non-native resolutions usually look like crap. -- |
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) |
Arthur Kalliokoski
Second in Command
February 2005
![]() |
They all watch too much MSNBC... they get ideas. |
|