I have to create a simple sketch program where the user can click on a color from the color menu and draw a free-form line with that color. The use should be able to switch colors and draw multiple lines.
This is what I have right now… can anyone tell me where I am going wrong? (I know there are like three different places :()
#ifdef __APPLE__
# include <OpenGL/gl.h>
# include <OpenGL/glu.h>
# include <GLUT/glut.h>
#else
# include <GL/gl.h>
# include <GL/glu.h>
# include <GL/glut.h>
#endif
#include <iostream>
#include <cmath>
using namespace std;
#define WIDTH 400
#define HEIGHT 800
#define Menu_Width 100
#define Box_Height (HEIGHT/8)
#define WLEFT 0
#define WRIGHT WIDTH
#define WBOTTOM 0
#define WTOP HEIGHT
#define NCOLORS 8
#define RED 1,0,0
#define ORANGE 1,0.5,0
#define YELLOW 1,1,0
#define GREEN 0,1,0
#define BLUE 0,0,1
#define PURPLE 0.5,0,0.5
#define BLACK 0,0,0
#define WHITE 1,1,1
#define GREY 0.7,0.7,0.7
#define R 0
#define O 1
#define Y 2
#define G 3
#define B 4
#define P 5
#define Bl 6
#define W 7
static float ColorMenu[][8] = {{RED},{ORANGE},{YELLOW},{GREEN},{BLUE},{PURPLE},{BLACK},{WHITE}};
int InColorMenu(int x, int y)
{
return (x >= 0 && x <= Menu_Width && y >= 0 && y <= HEIGHT);
}
int ColorMenuIndex(int x, int y)
{
if(!InColorMenu(x,y))
return -1;
else
return(y/Box_Height);
}
void DrawMenu()
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
for(i = 0; i < NCOLORS; i++)
{
glColor3f(ColorMenu[i][R],ColorMenu[i][O],ColorMenu[i][Y]);//,ColorMenu[i][G])//ColorMenu[i][b],ColorMenu[i][P],ColorMenu[i][Bl],ColorMenu[i][W]);
glRecti(1,Box_Height * i + 1,
Menu_Width - 1, Box_Height * (i + 1) - 1);
}
glFlush();
}
static int tracking = 0;
int InWindow(int x, int y)
{
return (x > WLEFT && x < WRIGHT && y > WBOTTOM && y < WTOP);
}
void m_Motion(int x, int y)
{
int reassignX;
int reassignY;
y = WTOP - y;
if(tracking && InWindow(x, y))
{
glBegin(GL_LINES);
glVertex2i(reassignX,reassignY);
glVertex2i(x, y);
glEnd();
reassignX = x;
reassignY = y;
glFlush();
}
}
void HandleButton(int button, int state, int x, int y)
{
static int MenuIndex = -1;
y = HEIGHT - y;
if(button != GLUT_LEFT_BUTTON)
return;
if(state == GLUT_DOWN)
{
if(InColorMenu(x, y))
MenuIndex = ColorMenuIndex(x, y);
tracking = 1;
}
else
{
if(InColorMenu(x, y) && ColorMenuIndex(x, y) == MenuIndex)
{
glColor3f(ColorMenu[MenuIndex][R],ColorMenu[MenuIndex][G],ColorMenu[MenuIndex][b]);//,ColorMenu[MenuIndex][G],ColorMenu[MenuIndex][b],ColorMenu[MenuIndex][P],ColorMenu[MenuIndex][Bl],ColorMenu[MenuIndex][W]);
glRecti(Menu_Width, 0, WIDTH, HEIGHT);
tracking = 0;
}
glFlush();
}
}
void DrawMouse()
{
int i;
glClearColor(GREY, 1);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(BLACK);
glFlush();
}
int main(int argc, char* argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("Rainbow Sketch");
gluOrtho2D(0, WIDTH, 0, HEIGHT);
glutDisplayFunc(DrawMouse);
glutDisplayFunc(DrawMenu);
glutMouseFunc(HandleButton);
glutMotionFunc(m_Motion);
glClearColor(BLACK, 1);
glutMainLoop();
return 0;
}