PDA

View Full Version : Drawing a map



kraljb
04-10-2016, 03:51 AM
Hello I have written a code for drawing squares at the cursor position and my problem is that every other click rewrites the old square. Since i want to draw a map i shouldn't be redrawing the previous quads. So if anybody can help me, this bellow is my code:

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <ctime>
#include <cstdlib>
#include <time.h>
#include <iostream>
#include <string.h>
#include <string>
#include <fstream>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

using namespace std;

int width=1000;
int height=1000;
int global=0;
int global2=0;
int xx=0;
int yy=0;

void risi(int x, int y)
{
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_QUADS);
glVertex2f(x, y);
glVertex2f(x+1, y);
glVertex2f(x+1, y+1);
glVertex2f(x, y+1);
glEnd();
}

void init(void)
{
glClearColor (0.0, 1.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, 0.0, 30.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if(global==1)
{risi(xx, yy);glFlush();
}

glutPostRedisplay();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
width=glutGet(GLUT_WINDOW_WIDTH);
height=glutGet(GLUT_WINDOW_HEIGHT);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='a')
global=1;
if(key=='s')
global=2;
if(key==27)
exit(1);
}
void mouse(int button, int state, int x, int y)
{
if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
{
xx=-(0.5-(float)x/width)*100;
yy=(0.5-(float)y/height)*100;
global=1;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (1000, 1000);
glutInitWindowPosition (0, 0);

int draw = glutCreateWindow (argv[0]);
glutSetWindowTitle("draw");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);

glutFullScreen();
glutMainLoop();
return 0;
}