![]() |
|
Running OpenGL by myself on top of Allegro 5 |
SpectreNectar
Member #10,969
May 2009
![]() |
My other topic in the programming section changed focus and I made this instead. Hope that's alright. I have no idea what I'm doing, but I thought what the hey - let's try and import OpenGL along with A5 so I did this: main.cpp 1#include <stdio.h>
2#include <iostream>
3#include <windows.h>
4#include <gl/gl.h>
5#include <allegro5/allegro.h>
6#include <allegro5/allegro_image.h>
7//#include <allegro5/allegro_physfs.h>
8#include <allegro5/allegro_primitives.h>
9#include <allegro5/allegro_color.h>
10#include <allegro5/allegro_font.h>
11#include <allegro5/allegro_ttf.h>
12#include <allegro5/allegro_physfs.h>
13#include <allegro5/allegro_audio.h>
14#include <allegro5/allegro_acodec.h>
15#include <physfs.h>
16
17#include "configure.h"
18#include "application.h"
19
20struct Assets {
21 ALLEGRO_DISPLAY *display;
22 ALLEGRO_EVENT_QUEUE *event_queue;
23 ALLEGRO_TIMER *timer;
24 Settings *config;
25 HDC hDC;
26 HGLRC hRC;
27};
28
29bool init( Assets& v );
30bool setup( Assets& v );
31bool run( Assets& v );
32bool deinit( Assets& v );
33
34void EnableOpenGL(HWND hwnd, HDC*, HGLRC*);
35void DisableOpenGL(HWND, HDC, HGLRC);
36
37int main(int argc, char **argv) {
38
39 Assets vars;
40 vars.display = 0;
41 vars.event_queue = 0;
42 vars.timer = 0;
43 vars.config = 0;
44
45 if(init(vars)) {
46
47 setup(vars);
48 run(vars);
49 deinit(vars);
50
51 return 0;
52 }
53
54 return 1;
55
56}
57
58
59bool init( Assets& v ) {
60
61 //ESSENTIALS
62 if(!al_init()) {
63 fprintf(stderr, "failed to initialize allegro!\n");
64 } else if(!al_install_keyboard()) {
65 fprintf(stderr, "failed to initialize the keyboard!\n");
66 } else if(!al_install_joystick()) {
67 fprintf(stderr, "failed to initialize the joystick!\n");
68 } else if(!al_install_mouse()) {
69 fprintf(stderr, "failed to initialize the mouse!\n");
70 } else if(!al_install_audio()) {
71 fprintf(stderr, "failed to initialize audio!\n");
72 } else {
73
74 //ASSETS
75 //al_set_new_display_flags(ALLEGRO_FULLSCREEN); //tryme
76 //al_set_new_display_flags(ALLEGRO_RESIZABLE); //tryme
77 al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST);
78 al_set_new_display_option(ALLEGRO_SAMPLES, 4, ALLEGRO_SUGGEST);
79
80 v.config = new Settings();
81 if(v.config->get_fullscreen()) al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW);
82
83 v.display = al_create_display(v.config -> get_screen_w(), v.config -> get_screen_h());
84 v.timer = al_create_timer(1.0 / v.config -> FPS);
85 v.event_queue = al_create_event_queue();
86 if(!v.display) {
87 fprintf(stderr, "failed to create display!\n");
88 } else if(!v.timer) {
89 fprintf(stderr, "failed to create timer!\n");
90 } else if(!v.event_queue) {
91 fprintf(stderr, "failed to create event_queue!\n");
92 } else {
93
94 al_init_font_addon();
95
96 //ADDONS
97 if(!al_init_image_addon()) {
98 fprintf(stderr, "failed to initialize image addon!\n");
99 } else if(!al_init_primitives_addon()) {
100 fprintf(stderr, "failed to initialize primitive addon!\n");
101 } else if(!al_init_ttf_addon()) {
102 fprintf(stderr, "failed to initialize ttf font addon!\n");
103 } else {
104
105 //SUCCESS
106 al_set_window_title(v.display, "SpectreNectar's WIP Game Title");
107 al_set_target_bitmap(al_get_backbuffer(v.display));
108
109 return true;
110
111 }
112 }
113
114 deinit(v);
115
116 }
117
118 return false;
119
120}
121
122bool setup( Assets& v ) {
123
124 al_set_physfs_file_interface();
125
126 /* Set up PhysicsFS. */
127 if(!PHYSFS_init(0)) std::cout << "failed initializing" << std::endl;
128 // This creates a ~/.allegro directory, which is very annoying to say the
129 // least - and no need for it in this example.
130 // if (!PHYSFS_setSaneConfig("allegro", "ex_physfs", NULL, 0, 0))
131 // return 1;
132 else if(!PHYSFS_addToSearchPath("data", 1)) std::cout << "failed adding search path" << std::endl;
133
134 al_register_event_source(v.event_queue, al_get_display_event_source(v.display));
135 al_register_event_source(v.event_queue, al_get_timer_event_source(v.timer));
136 al_register_event_source(v.event_queue, al_get_mouse_event_source());
137 al_register_event_source(v.event_queue, al_get_keyboard_event_source());
138 al_register_event_source(v.event_queue, al_get_joystick_event_source());
139
140 // Audio
141 al_reserve_samples(16);
142 al_init_acodec_addon();
143
144 //al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR); // Make full screen blurry
145
146 al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA); // Enable alpha for lines
147 al_clear_to_color(al_map_rgb(0,0,0));
148 al_flip_display();
149 al_start_timer(v.timer);
150
151 // Disable screensaver
152 //al_inhibit_screensaver(true); //Full screen only
153 //al_set_display_icon(v.display, bmp);
154 //al_toggle_display_flag(v.display, ALLEGRO_FULLSCREEN_WINDOW, true);
155
156 return true;
157}
158
159bool run( Assets& v ) {
160
161 EnableOpenGL(al_get_win_window_handle(v.display), &v.hDC, &v.hRC);
162
163 float theta = 0.0f;
164
165 Application app(v.display, v.config);
166 while(!app.doexit) {
167 ALLEGRO_EVENT ev;
168 al_wait_for_event(v.event_queue, &ev);
169
170 app.update(&ev);
171
172 if(app.redraw && al_is_event_queue_empty(v.event_queue)) {
173
174 /* OpenGL animation code goes here */
175
176 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
177 glClear(GL_COLOR_BUFFER_BIT);
178
179 glPushMatrix();
180 glRotatef(theta, 0.0f, 0.0f, 1.0f);
181
182 glBegin(GL_TRIANGLES);
183
184 glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(0.0f, 1.0f);
185 glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.87f, -0.5f);
186 glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(-0.87f, -0.5f);
187
188 glEnd();
189
190 glPopMatrix();
191
192 SwapBuffers(hDC);
193
194 theta += 1.0f;
195
196 app.draw();
197 al_flip_display();
198 }
199 }
200
201 DisableOpenGL(al_get_win_window_handle(v.display), v.hDC, v.hRC);
202
203 return true;
204}
205
206bool deinit( Assets& v ) {
207
208
209
210 PHYSFS_deinit();
211
212 delete v.config;
213 al_uninstall_audio();
214 al_destroy_timer(v.timer);
215 al_destroy_event_queue(v.event_queue);
216 al_destroy_display(v.display);
217
218 return true;
219
220}
221
222
223
224void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC)
225{
226 PIXELFORMATDESCRIPTOR pfd;
227
228 int iFormat;
229
230 /* get the device context (DC) */
231 *hDC = GetDC(hwnd);
232
233 /* set the pixel format for the DC */
234 ZeroMemory(&pfd, sizeof(pfd));
235
236 pfd.nSize = sizeof(pfd);
237 pfd.nVersion = 1;
238 pfd.dwFlags = PFD_DRAW_TO_WINDOW |
239 PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
240 pfd.iPixelType = PFD_TYPE_RGBA;
241 pfd.cColorBits = 24;
242 pfd.cDepthBits = 16;
243 pfd.iLayerType = PFD_MAIN_PLANE;
244
245 iFormat = ChoosePixelFormat(*hDC, &pfd);
246
247 SetPixelFormat(*hDC, iFormat, &pfd);
248
249 /* create and enable the render context (RC) */
250 *hRC = wglCreateContext(*hDC);
251
252 wglMakeCurrent(*hDC, *hRC);
253}
254
255void DisableOpenGL (HWND hwnd, HDC hDC, HGLRC hRC)
256{
257 wglMakeCurrent(NULL, NULL);
258 wglDeleteContext(hRC);
259 ReleaseDC(hwnd, hDC);
260}
And got this: In function 'bool run(Assets&)':| It would seem that the al_get_win_window_handle function is no longer supported. I'm attempting to use OpenGL directly for my drawing in case you wondered. |
Matthew Leverton
Supreme Loser
January 1999
![]() |
You need: #include <allegro5/allegro_windows.h> Also see: http://www.allegro.cc/manual/5/opengl.html along with the OpenGL examples. |
SpectreNectar
Member #10,969
May 2009
![]() |
This is great! The manual never ceases to amaze me ^^ |
flowbsd
Member #13,650
October 2011
|
#include <GL/glew.h>
____________________________________ |
|