#include #include #include #include // The font we will be using FONT* Font; // Possible primitives const int NumPrimitives = 10; const int Primitives[NumPrimitives] = { GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, GL_POLYGON }; // Primitive names string PrimitiveNames[NumPrimitives] = { "GL_POINTS", "GL_LINES", "GL_LINE_STRIP", "GL_LINE_LOOP", "GL_TRIANGLES", "GL_TRIANGLE_STRIP", "GL_TRIANGLE_FAN", "GL_QUADS", "GL_QUAD_STRIP", "GL_POLYGON" }; // A struct to hold a point. struct Point { float x, y; Point(float x, float y) : x(x), y(y) {} }; void Init() { // Start up Allegro and AllegroGL systems allegro_init(); install_allegro_gl(); install_keyboard(); install_mouse(); // Set some AllegroGL options allegro_gl_set(AGL_DOUBLEBUFFER, true); allegro_gl_set(AGL_COLOR_DEPTH, 16); allegro_gl_set(AGL_RENDERMETHOD, true); allegro_gl_set(AGL_SUGGEST, AGL_DOUBLEBUFFER | AGL_COLOR_DEPTH | AGL_RENDERMETHOD); // Set up a suitable viewing window set_color_depth(16); set_gfx_mode(GFX_OPENGL_WINDOWED, 640, 480, 0, 0); // Fill front-facing polygons and line back-facing ones. glPolygonMode(GL_FRONT, GL_FILL); glPolygonMode(GL_BACK, GL_LINE); // Set the background color to black glClearColor(0.0, 0.0, 0.0, 0.0); } int main() { Init(); std::vector vertices; bool mouse_b1 = false; bool upd_title = true; char* title_buf = 0; float mx = 0.0, my = 0.0; int prim = 0; while(!key[KEY_ESC]) { // Clear the screen glClear(GL_COLOR_BUFFER_BIT); // Poll the mouse poll_mouse(); mx = (mouse_x / 320.0) - 1.0; my = (-mouse_y / 240.0) + 1.0; // Check to see if a vertex needs to be added if(!(mouse_b & 1) && mouse_b1) // Mouse button 1 up { vertices.push_back(Point(mx, my)); } mouse_b1 = mouse_b & 1; // Check to see if the primitive should be drawn if((mouse_b & 2) && (vertices.size() > 0)) { glBegin(Primitives[prim]); glColor3f(0.0, 0.0, 1.0); for(unsigned int i = 0; i < vertices.size(); i++) { glVertex2f(vertices[i].x, vertices[i].y); } glEnd(); glFlush(); allegro_gl_flip(); set_window_title("Press any key to continue."); upd_title = true; clear_keybuf(); readkey(); } // Clear the vertex list if necessary if(key[KEY_DEL] && (vertices.size() > 0)) { vertices.clear(); } // Draw the vertices and the mouse cursor glPointSize(5.0); glBegin(GL_POINTS); glColor3f(1.0, 0.0, 0.0); for(unsigned int i = 0; i < vertices.size(); i++) { glVertex2f(vertices[i].x, vertices[i].y); } glColor3f(0.8, 0.8, 0.8); glVertex2f(mx, my); glEnd(); // Cycle through the available primitives. if(key[KEY_1]) { prim = 0; upd_title = true; } if(key[KEY_2]) { prim = 1; upd_title = true; } if(key[KEY_3]) { prim = 2; upd_title = true; } if(key[KEY_4]) { prim = 3; upd_title = true; } if(key[KEY_5]) { prim = 4; upd_title = true; } if(key[KEY_6]) { prim = 5; upd_title = true; } if(key[KEY_7]) { prim = 6; upd_title = true; } if(key[KEY_8]) { prim = 7; upd_title = true; } if(key[KEY_9]) { prim = 8; upd_title = true; } if(key[KEY_0]) { prim = 9; upd_title = true; } // Set the window title if(upd_title) { std::string title = string("Type: ") + PrimitiveNames[prim] + string(" | LMB: Add vtx | RMB: Draw prim. | 0-9: Change prim. type | DEL: Clear"); delete[] title_buf; title_buf = new char[title.size()]; strcpy(title_buf, title.c_str()); set_window_title(title_buf); upd_title = false; } // Flush & flip. glFlush(); allegro_gl_flip(); } return 0; } END_OF_MAIN();