Basically, I am trying to make a brick breaker game using opengl.
What I am stuck with is the fact, that my functions for all different objects to be drawn gets called inside a while(1) loop, but at the same time I need inputs from my keyboard to be registered. But my glutkeyboardfunc never gets called because of the preceeding infinte while loop. What logic/method can I use to solve this? Thank you.
Here’s the code if required:
using namespace std;
#include <iostream>
#include <GL/glut.h>
#include <cstdio>
#include <cmath>
#include <vector>
#define rad 10
#define noblock 20
float DEG2RAD = 3.14159/180;
float xmin=-20, ymin=-200, xmax=70, ymax=-180,cx=35,cy=-170;
int t=1,r=1;
float cxmin,cxmax,cymin,cymax;
void delay(int n)
{
clock_t t=n+clock();
while(t>clock());
}
void dcirc(float x, float y)
{
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
for (int i=0; i < 360; i++)
{
float degInRad = i*DEG2RAD;
glVertex2f(x+cos(degInRad)*rad,y+sin(degInRad)*rad);
}
glEnd();
glFlush();
}
int hit[20]={0};
vector<float> x1,x2,ay1,y2;
void box()
{
glColor3f(0.0,1.0,0.0);
float lx1=-160,ly1=150,lx2=-130,ly2=170;
for(int i=0;i<noblock;i++)
{
if(hit[i]==0)
{
glBegin(GL_LINE_LOOP);
glVertex2i(lx1,ly1);
glVertex2i(lx2,ly1);
glVertex2i(lx2,ly2);
glVertex2i(lx1,ly2);
glEnd();
x1.push_back(lx1);
x2.push_back(lx2);
ay1.push_back(ly1);
y2.push_back(ly2);
lx1+=30;
lx2+=30;
if(i==9)
{
lx1=-160;lx2=-130;ly1=130;ly2=150;
}
}
else
{
lx1+=30;
lx2+=30;
if(i==9)
{
lx1=-160;lx2=-130;ly1=130;ly2=150;
}
}
}
glFlush();
}
void dbox()
{
glColor3f(0.0,0.0,1.0);
glBegin(GL_LINE_LOOP);
glVertex2i(xmin,ymin);
glVertex2i(xmax,ymin);
glVertex2i(xmax,ymax);
glVertex2i(xmin,ymax);
glEnd();
glFlush();
}
void mc()
{
if(cx+rad>200){r=0;}
if(cx-rad<-200){r=1;}
if(cy+rad>200){t=0;}
if(cx<=xmax&&cx>=xmin&&cy==rad-180) {t=1;}
if(cy-rad==-200) t=1;//exit(0);
for(int i=0;i<noblock;i++)
{
if(((cy+rad)==ay1[i])&&(hit[i]==0)&&(cx<=x2[i])&&(cx>=x1[i]))
{
hit[i]=1;
t=0;
}
else if(((cy-rad)==y2[i])&&(hit[i]==0)&&(cx<=x2[i])&&(cx>=x1[i]))
{
hit[i]=1;
t=1;
}
else if(((cx-rad)==x2[i])&&(hit[i]==0)&&(cy<=y2[i])&&(cy>=ay1[i]))
{
hit[i]=1;
r=1;
}
else if(((cx+rad)==x1[i])&&(hit[i]==0)&&(cy<=y2[i])&&(cy>=ay1[i]))
{
hit[i]=1;
r=0;
}
}
if(r==0) {cx--;}
if(r==1) {cx++;}
if(t==0) {cy--;}
if(t==1) {cy++;}
dcirc(cx,cy);
delay(1);
}
void show()
{
glClear(GL_COLOR_BUFFER_BIT);
dbox();
box();
mc();
}
void display()
{
glClearColor(1.0,1.0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT);
dbox();
while(1) show();
glFlush();
}
void myKey(unsigned char key, int x, int y)
{
glClear(GL_COLOR_BUFFER_BIT);
if(key=='l')
{
xmax+=20;
xmin+=20;
}
else if(key=='k')
{
xmax-=20;
xmin-=20;
}
if(xmax>=200)
{
xmax=200;
xmin=130;
}
else if(xmin<=-200)
{
xmin=-200;
xmax=-130;
}
show();
}
void myInit(void)
{
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-200,200,-200,200);
}
int main(int argc,char** argv)
{
char p;
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(600,600);
glutInitWindowPosition(0,0);
glutCreateWindow("simple");
myInit();
glutDisplayFunc(&display);
glutKeyboardFunc(myKey);
glutMainLoop();
}