Skip to content

Instantly share code, notes, and snippets.

@kevinclark
Created November 29, 2009 05:06
Show Gist options
  • Save kevinclark/244797 to your computer and use it in GitHub Desktop.
Save kevinclark/244797 to your computer and use it in GitHub Desktop.
#include <vector>
#include <iostream>
#include <OpenGl/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#include "boost/bind.hpp"
#include "Point.h"
std::vector<Point> g_points;
void Display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_STRIP);
glColor3f(1.0, 0.5, 0.5);
glVertex2f(0.1, 0.1);
glVertex2f(0.2, 0.2);
glVertex2f(0.3,0.3);
glVertex2f(0.4, 0.1);
std::for_each(g_points.begin(), g_points.end(),
boost::bind(&Point::draw, _1));
glVertex2f(0.8, 0.8);
glEnd();
GLenum err;
if ((err = glGetError()) != GL_NO_ERROR)
{
std::cout << "Got an error: " << err << std::endl;
}
glFlush();
}
void Init()
{
glClearColor(0,0,0,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
}
void ResizeScene(int width, int height)
{
glViewport(0,0, width, height);
}
void MouseControl(int button, int state, int x, int y)
{
std::cout << "Got mouse change:" << std::endl;
std::cout << "\tButton -> " << button << std::endl;
std::cout << "\tState -> " << state << std::endl;
std::cout << "\tX -> " << x << std::endl;
std::cout << "\tY -> " << y << std::endl;
std::cout << "GLUT_DOWN: " << GLUT_DOWN << std::endl;
std::cout << "Number of points: " << g_points.size() << std::endl;
g_points.push_back(Point(x / 1000.0, y / 1000.0,0));
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(0, 0);
glutCreateWindow("Level Up!");
gluOrtho2D(0, 1, 0, 1);
Init();
glutDisplayFunc(Display);
glutReshapeFunc(ResizeScene);
glutMouseFunc(MouseControl);
glutMainLoop();
return 0;
}
#ifndef POINT_H
#define POINT_H
#include <tr1/tuple>
class Point
{
public:
Point(double x, double y, double z)
: _point(x,y,z)
{}
void draw()
{
std::cout << "Printing! (" << X() << ", " << Y() << ")" << std::endl;
glVertex2d(X(), Y());
}
double X() { return std::tr1::get<0>(_point); }
double Y() { return std::tr1::get<1>(_point); }
double Z() { return std::tr1::get<2>(_point); }
private:
std::tr1::tuple<double, double, double> _point;
};
#endif POINT_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment