I have a compilation or runtime error when I try to run this code in VC 9.0. Can anybody see where I've slipped up?
#include<glut.h>
#include<math.h>
#include<iostream.h>
#include <stdlib.h>
using namespace std;
class Point2 {
public:
Point2() {x = y = 0.0f;} Point2(float xx, float yy) {x=xx; y=yy;} void set(float xx, float yy) {x=xx; y=yy;}
float getX() {return x;}
float getY() {return y;}
void draw(void)
{
glBegin(GL_POINTS); glVertex2f((GLfloat)x, (GLfloat)y);
glEnd();
}
private:
float x, y;
};
class IntRect {
public:
IntRect() {l = 0; r=100; b=0; t=100;} IntRect(int left, int right, int bottom, int top)
{l = left; r=right; b=bottom; t=top;}
void set(int left, int right, int bottom, int top)
{
l=left; r=right; b=bottom; t=top;
}
void draw(void); int getL(void)
{
return l;
}
int getR(void)
{
return r;
}
int getT(void)
{
return t;
}
int getB(void)
{
return b;
}
private:
int l, r, b, t;
};
class RealRect {
public:
RealRect() {l = 0; r=100; b=0; t=100;} RealRect(float left, float right, float bottom, float top)
{l = left; r=right; b=bottom; t=top;}
void set(float left, float right, float bottom, float top)
{
l=left; r=right; b=bottom; t=top;
}
float getL(void)
{
return l;
}
float getR(void)
{
return r;
}
float getT(void)
{
return t;
}
float getB(void)
{
return b;
}
void draw(void); private:
float l, r, b, t;
};
class Canvas
{
public:
Canvas(int width, int height, char* windowTitle); void setWindow(float l, float r, float b, float t);
void setViewport(int l, int r, int b, int t);
IntRect getViewport(void); RealRect getWindow(void); float getWindowAspectRatio(void);
void clearScreen();
void setBackgroundColor(float r, float g, float b);
void setColor(float r, float g, float b);
void lineTo(float x, float y);
void lineTo(Point2 p);
void moveTo(float x, float y);
void moveTo(Point2 p);
void moveRel(float dx, float dy);
void turnTo(float angle);
void turn(float angle);
void forward(float dist, int isVisible);
Point2 Tween(Point2 A, Point2 B, float t);
void drawTween(Point2 A[], Point2 B[], int N, float t);
private:
Point2 CP; IntRect viewport; RealRect window; float CD; } ;
Canvas:: Canvas(int width, int height, char* windowTitle)
{
char* argv[1]; char dummyString[8];
argv[0] = dummyString; int argc = 1;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(width, height);
glutInitWindowPosition (20,20);
glutCreateWindow (windowTitle);
setWindow(0, (float)width, 0, (float)height); setViewport (0, width, 0, height); CP.set(0.0f, 0.0f); }
void Canvas:: moveTo(Point2 p) {
float x1, y1;
x1 = p.getX();
y1 = p.getY();
CP.set(x1, y1);
}
void Canvas:: forward(float dist, int isVisible) {
const float RadPerDeg=0.017453293; float x = CP.getX() + dist * cos(RadPerDeg * CD);
float y = CP.getY() + dist * sin(RadPerDeg * CD);
if(isVisible)
lineTo(x, y);
else
moveTo(x, y);
}
void Canvas:: turnTo(float angle)
{
CD = angle;
}
void Canvas:: turn(float angle) {CD += angle;}
float Canvas:: getWindowAspectRatio(void) {
float width, height, aspectRatio;
width = window.getR() - window.getL();
height = window.getT() - window.getB();
aspectRatio = width/height;
return aspectRatio;
}
void Canvas:: moveRel(float dx, float dy)
{
CP.set(CP.getX() + dx, CP.getY() + dy);
}
void Canvas:: setWindow (float l, float r, float b, float t)
{
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluOrtho2D ((GLdouble)l, (GLdouble)r, (GLdouble)b, (GLdouble)t);
window.set (l, r, b, t);
}
void Canvas:: setViewport (int l, int r, int b, int t)
{
glViewport(l, b, r-l, t-b);
viewport.set (l, r, b, t);
}
RealRect Canvas:: getWindow(void)
{
return window;
}
IntRect Canvas:: getViewport(void)
{
return viewport;
}
void Canvas:: clearScreen(void)
{
glClear(GL_COLOR_BUFFER_BIT);
}
void Canvas:: setBackgroundColor(float r, float g, float b)
{
glClearColor(r, g, b, 0.0); }
void Canvas:: setColor(float r, float g, float b)
{
glColor3f(r, g, b);
}
void Canvas:: lineTo(Point2 p)
{
glBegin (GL_LINES);
glVertex2f((GLfloat) CP.getX(), (GLfloat) CP.getY());
glVertex2f((GLfloat) p.getX(), (GLfloat) p.getY());
glEnd();
CP.set(p.getX(), p.getY());
glFlush();
}
void Canvas:: moveTo(float x, float y)
{
CP.set(x, y);
}
void Canvas:: lineTo(float x, float y)
{
glBegin(GL_LINES);
glVertex2f((GLfloat) CP.getX(), (GLfloat) CP.getY());
glVertex2f((GLfloat) x, (GLfloat) y); glEnd();
CP.set (x, y); glFlush();
}
Point2 Canvas:: Tween(Point2 A, Point2 B, float t)
{
Point2 P;
P.set(A.getX() + (B.getX() - A.getX())*t, A.getY() + (B.getY() - A.getY())*t);
return P;
}
void Canvas:: drawTween(Point2 A[], Point2 B[], int N, float t)
{
for(int i = 0; i < N; i++)
{
Point2 P;
P = Tween(A[i], B[i], t);
if (i == 0) moveTo(P.getX(), P.getY());
else lineTo(P.getX(), P.getY());
}
}
Canvas cvs(640, 640, "Example of tweening");
void myDisplay(void)
{
cvs.setViewport(0, 640, 0, 640);
cvs.clearScreen();
cvs.setWindow(0.0, 1.0, 0.0, 1.0);
Point2 A[10];
Point2 B[10];
A[0].set(0.2, 0.5);
A[1].set(0.5, 0.35);
A[2].set(0.8, 0.5);
A[3].set(0.5, 0.2);
A[4].set(0.2, 0.5);
B[0].set(0.2, 0.2);
B[1].set(0.2, 0.8);
B[2].set(0.8, 0.8);
B[3].set(0.8, 0.2);
B[4].set(0.2, 0.2);
float t = 0.0;
int N = 10; float tinc = 1.0/N;
int vpsize = 640/(N+1);
for (int i = 0; i <= N; i++)
{
cvs.setViewport(i*vpsize,(i+1)*vpsize, (N/2)*vpsize, (N/2+1)*vpsize);
cvs.drawTween(A, B, 5, t);
t = t + tinc;
}
}
int main(int argc, char **argv)
{
cvs.setWindow(-1.0, 1.0, -1.0, 1.0);
cvs.setViewport(0, 640, 0, 640);
cvs.setBackgroundColor(1.0, 1.0, 1.0);
cvs.setColor(0.0, 0.0, 0.0);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}
i recieve exit error from standard library header file.
error report:
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\stdlib.h(371) : warning C4985: 'exit': attributes not present on previous declaration.
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\gl\glut.h(146) : see declaration of 'exit'