1
0
Fork 0
mirror of https://codeberg.org/kbruen/y3s2-gui-project.git synced 2025-04-20 23:03:56 +03:00
y3s2-gui-project/src/maze.cpp

154 lines
3.1 KiB
C++
Raw Normal View History

2022-06-05 02:17:49 +03:00
#include "maze.h"
#include <cmath>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include "utils.hpp"
constexpr double PI = 3.14159265358979;
MazeScreen::MazeScreen(const std::vector<std::vector<int>>& maze):
maze(maze),
angleX(0),
mouseCapture(false),
posX(1),
posZ(1) {}
void MazeScreen::display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//configures the camera with the position_x and position_z variables
gluLookAt(
posX,
0,
posZ,
posX + cos((angleX * PI) / 180) * 3,
0,
posZ - sin((angleX * PI) / 180) * 3,
0,
1,
0
);
for (int i = 0; i < maze.size(); i++) {
for (int j = 0; j < maze[i].size(); j++) {
switch (maze[i][j]) {
case -1:
glPushMatrix();
glColor3f(1, 0, 0);
glTranslatef(i, 0, j);
glutWireCube(1);
glPopMatrix();
break;
case 1:
glPushMatrix();
// Scale green by X
auto green = Utils::clamp<float>(Utils::nummap<float>(abs(i - posX), 0, 3, 0, 1), 0, 1);
// Scale blue by Z
auto blue = Utils::clamp<float>(Utils::nummap<float>(abs(j - posZ), 0, 3, 0, 1), 0, 1);
glColor3f(1, green, blue);
glTranslatef(i, 0, j);
if (solid) {
glutSolidCube(1);
}
else {
glutWireCube(1);
}
glPopMatrix();
break;
}
}
}
glFlush();
glutSwapBuffers();
}
void MazeScreen::keyboard(unsigned char key, int x, int y) {
if (key == '\e') {
mouseCapture = false;
glutSetCursor(GLUT_CURSOR_INHERIT);
}
else if (key == ' ') {
solid = !solid;
}
else if (key == 'w') {
forceX += 1;
}
else if (key == 's') {
forceX -= 1;
}
else if (key == 'a') {
forceZ -= 1;
}
else if (key == 'd') {
forceZ += 1;
}
glutPostRedisplay();
}
void MazeScreen::keyboardUp(unsigned char key, int x, int y) {
if (key == 'w') {
forceX -= 1;
}
else if (key == 's') {
forceX += 1;
}
else if (key == 'a') {
forceZ += 1;
}
else if (key == 'd') {
forceZ -= 1;
}
}
int gameTime;
void MazeScreen::idle() {
int newTime = glutGet(GLUT_ELAPSED_TIME);
auto deltaTime = (newTime - gameTime) / 1000.0;
gameTime = newTime;
posX += forceX * deltaTime * cos(angleX * PI / 180) + forceZ * deltaTime * sin(angleX * PI / 180);
posZ += forceZ * deltaTime * cos(angleX * PI / 180) - forceX * deltaTime * sin(angleX * PI / 180);
glutPostRedisplay();
}
void MazeScreen::passiveMotion(int x, int y) {
if (mouseCapture) {
auto centerX = glutGet(GLUT_WINDOW_WIDTH) / 2;
auto centerY = glutGet(GLUT_WINDOW_HEIGHT) / 2;
auto diffX = x - centerX;
auto diffY = x - centerY;
// only left-right movement
angleX -= diffX;
if (x != centerX || y != centerY) {
glutWarpPointer(centerX, centerY);
}
glutPostRedisplay();
}
}
void MazeScreen::mouse(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON) {
auto centerX = glutGet(GLUT_WINDOW_WIDTH) / 2;
auto centerY = glutGet(GLUT_WINDOW_HEIGHT) / 2;
glutWarpPointer(centerX, centerY);
mouseCapture = true;
glutSetCursor(GLUT_CURSOR_NONE);
}
else if (button == GLUT_RIGHT_BUTTON) {
mouseCapture = false;
glutSetCursor(GLUT_CURSOR_INHERIT);
}
}