Skip to content

Instantly share code, notes, and snippets.

@AmrMKayid
Created October 14, 2018 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AmrMKayid/c2b805b1542bc379ec2031b8d4e00937 to your computer and use it in GitHub Desktop.
Save AmrMKayid/c2b805b1542bc379ec2031b8d4e00937 to your computer and use it in GitHub Desktop.
/* Libraries */
#ifdef __APPLE__
#include <GLUT/glut.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
#endif
#include <iostream>
#include <math.h>
#define WINDOW_WIDTH 1400
#define WINDOW_HEIGHT 800
double t = 0;
int x, y, beizer_timer=0;
int p0[2], p1[2], p2[2], p3[2];
bool movement_reverse = false;
int* bezier(float t, int* p0,int* p1,int* p2,int* p3) {
int res[2];
res[0]=pow((1-t),3)*p0[0]+3*t*pow((1-t),2)*p1[0]+3*pow(t,2)*(1-t)*p2[0]+pow(t,3)*p3[0];
res[1]=pow((1-t),3)*p0[1]+3*t*pow((1-t),2)*p1[1]+3*pow(t,2)*(1-t)*p2[1]+pow(t,3)*p3[1];
return res;
}
void display() {
start:
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex2f(x, y);
glVertex2f(x + 100, y);
glVertex2f(x + 100, y + 100);
glVertex2f(x, y + 100);
glEnd();
glFlush();
glutSwapBuffers();
glutPostRedisplay();
}
void timer(int value) {
beizer_timer += 30;
if(beizer_timer > 100) {
beizer_timer = 0;
t += (0.01);
}
if(t > 1) {
p1[0] += 50;
p1[1] += 50;
p2[0] += 50;
p2[1] += 50;
t = 0;
}
int *res = bezier(t, p0, p1, p2, p3);
x = res[0];
y = res[1];
// std::cout<<"X: "<<x<<"Y: "<<y<<"T: "<<t<<"\n";
glutTimerFunc(0, timer, 0);
glutPostRedisplay();
}
// -----------------------------------
// Main
// -----------------------------------
int main(int argc, char** argv) {
/* Initializers */
srand((unsigned)time(0));
glutInit(&argc, argv);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(50, 50);
glutCreateWindow("Kayid - Space Invaders");
t = 0;
p0[0]=WINDOW_WIDTH / 4;
p0[1]=WINDOW_WIDTH / 4 + 100;
p1[0]=WINDOW_WIDTH / 4;
p1[1]=WINDOW_WIDTH / 4 + 400;
p2[0]=WINDOW_WIDTH / 4 + 800;
p2[1]=WINDOW_WIDTH / 4 + 400;
p3[0]=WINDOW_WIDTH / 4 + 800;
p3[1]=WINDOW_WIDTH / 4 + 100;
x = WINDOW_WIDTH / 2;
y = WINDOW_HEIGHT / 2;
/* Callbacks */
glutDisplayFunc(display);
glutTimerFunc(0, timer, 0);
glClearColor(1, 1, 1, 0);
gluOrtho2D(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT);
/* Main loop */
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment