#define GLUT_DISABLE_ATEXIT_HACK
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>
#define PI 3.1415926535
#define DR 0.0174533 //or degrees to radians
float CameraX, CameraY, CameraDeltaX, CameraDeltaY, CameraAngle = 0;
int Mapx = 8; Mapy = 8;
int map[8][8] =
{
{1,1,1,1,1,1,1,1},
{1,0,1,0,0,0,0,1},
{1,0,1,0,0,0,0,1},
{1,0,1,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,1,0,1},
{1,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1}
};
typedef struct
{
int w, a, d, s; //button state on off
}ButtonKeys; ButtonKeys Keys;
void drawMinimap()
{
int x, y, xo, yo;
for (y = 0;y < Mapy;y++)
{
for (x = 0;x < Mapx;x++)
{
if (map[x][y] == 1) { glColor3f(1, 1, 1); }
else { glColor3f(0, 0, 0); }
xo = x * 10; yo = y * 10;
glBegin(GL_QUADS);
glVertex2i(0 + xo + 1, 0 + yo + 1);
glVertex2i(0 + xo + 1, 10 + yo - 1);
glVertex2i(10 + xo - 1, 10 + yo - 1);
glVertex2i(10 + xo - 1, 0 + yo + 1);
glEnd();
}
}
glColor3f(1, 1, 0);
glPointSize(3);
glBegin(GL_POINTS);
glVertex2i(CameraX * 10, CameraY * 10);
glEnd();
glLineWidth(1);
glBegin(GL_LINES);
glVertex2i(CameraX * 10, CameraY * 10);
glVertex2i(CameraX * 10 + CameraDeltaX * 2, CameraY * 10 + CameraDeltaY * 2);
glEnd();
}
void Draw3dWorldUsingRC()
{
int mx = 0, my = 0, dof = 0, side; float rx, ry, ra, disX, disY;
ra = CameraAngle - DR * 48;
for (int i = 0; i < 96; i++)
{
int hit = 0;
side = 0;
rx = CameraX;
ry = CameraY;
disX = 0;
disY = 0;
dof = 0;
while (hit == 0)
{
if (side == 1)
{
rx += cos(ra) * 0.1;
disX += 0.1;
side = 0;
dof += 0.1;
}
else
{
ry += sin(ra) * 0.1;
disY += 0.1;
side = 1;
dof += 0.1;
}
if (map[(int)floor(rx)][(int)floor(ry)] != 0)
{
hit = 1;
}
}
float ca = CameraAngle - ra; if (ca < 0) { ca += 2 * PI; } if (ca > 2*PI) { ca -= 2 * PI; }
float lineheight = 90;
if (side == 1)
{
disX = disX * cos(ca);
glColor3f(0, 1, 0);glLineWidth(2); glBegin(GL_LINES); glVertex2i(100 + i * 2, lineheight - lineheight / disX); glVertex2i(100 + i * 2, lineheight + lineheight/disX); glEnd();
}
else
{
disY = disY * cos(ca);
glColor3f(0, 0.8, 0);glLineWidth(2); glBegin(GL_LINES); glVertex2i(100 + i * 2, lineheight - lineheight / disY); glVertex2i(100 + i * 2, lineheight + lineheight/ disY); glEnd();
}
glColor3f(0.5, 0.5, 0.5);glLineWidth(2); glBegin(GL_LINES); glVertex2i(CameraX*10, CameraY * 10); glVertex2i(rx * 10, ry * 10); glEnd();
ra += DR;
}
}
float frame1, frame2, fps, smoothing;
void controls(unsigned char key, int x, int y)
{
if (key == 'a') { Keys.a = 1; }
if (key == 'd') { Keys.d = 1; }
if (key == 'w') { Keys.w = 1; }
if (key == 's') { Keys.s = 1; }
}
void controlsup(unsigned char key, int x, int y)
{
if (key == 'a') { Keys.a = 0; }
if (key == 'd') { Keys.d = 0; }
if (key == 'w') { Keys.w = 0; }
if (key == 's') { Keys.s = 0; }
glutPostRedisplay();
}
void display()
{
frame2 = glutGet(GLUT_ELAPSED_TIME); fps = (frame2 - frame1); frame1 = glutGet(GLUT_ELAPSED_TIME);
if (Keys.w == 1) { CameraX += cos(CameraAngle) * fps; CameraY += sin(CameraAngle) * fps;}
if (Keys.s == 1) { CameraX -= cos(CameraAngle) * fps; CameraY -= sin(CameraAngle) * fps;}
if (Keys.d == 1) { CameraX += CameraAngle * DR * 5 * fps;}
if (Keys.a == 1) { CameraX -= CameraAngle * DR * 5 * fps;}
glClear(GL_COLOR_BUFFER_BIT);
drawMinimap();
Draw3dWorldUsingRC();
glutSwapBuffers();
}
void init()
{
glClearColor(0.3, 0.3, 0.3, 0);
gluOrtho2D(0, 280, 180, 0);
CameraX = 4; CameraY = 4;
CameraDeltaX = cos(CameraAngle) * 5; CameraDeltaY = sin(CameraAngle) * 5;
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(280, 180);
glutCreateWindow("Neptune Engine Alpha 1");
glutDisplayFunc(display);
init();
glutKeyboardFunc(controls);
glutKeyboardUpFunc(controlsup);
glutMainLoop();
return 0;
}
Today I was working on my project and it suddenly started acting like a slide show could anybody help me solve what is happening? I've switched to visual studio 2019 and it is only affecting the actual project window. I was trying to make the movement smoother. I don't seem to be getting any errors and I can move and scale the window freely without any lag. Could somebody help me please?
UPDATE: I solved this recently I just don't know how to close this question.
As seen in the comments, you have to render more. You can do this by calling glutPostRedisplay
immediately after it's done displaying, but this will probably run at 100% CPU speed and will tie up your rendering with your engine speed. A slightly more nuanced way is to even out the rendering by registering a glutTimerFunc
for periodic updates. In the timer function, call glutPostRedisplay
and glutTimerFunc
again.
static void update(int zero) {
(void *)zero;
glutTimerFunc(20 /* 50 fps */, &update, 0);
/* TODO: Advance your engine;
_ie_ move some stuff that doesn't really
`display` in here. */
glutPostRedisplay();
}
You've done a glutGet(GLUT_ELAPSED_TIME)
, which should be there as well, because glutTimerFunc, "GLUT attempts to deliver the timer callback as soon as possible after the expiration of the callback's time interval," which, I take it, is not really very accurate at keeping absolute time.