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

155 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"
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(Utils::toRad<float>(angleX)) * 3,
2022-06-05 02:17:49 +03:00
0,
posZ - sin(Utils::toRad<float>(angleX)) * 3,
2022-06-05 02:17:49 +03:00
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();
2022-06-05 10:48:40 +03:00
glTranslatef(i, 0, j);
2022-06-05 02:17:49 +03:00
// 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);
2022-06-05 10:48:40 +03:00
2022-06-05 02:17:49 +03:00
glColor3f(1, green, blue);
2022-06-05 10:48:40 +03:00
glutWireCube(1);
2022-06-05 02:17:49 +03:00
if (solid) {
2022-06-05 10:48:40 +03:00
glColor3f(1, green, blue);
2022-06-05 02:17:49 +03:00
glutSolidCube(1);
}
2022-06-05 10:48:40 +03:00
2022-06-05 02:17:49 +03:00
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(Utils::toRad<float>(angleX)) + forceZ * deltaTime * sin(Utils::toRad<float>(angleX));
posZ += forceZ * deltaTime * cos(Utils::toRad<float>(angleX)) - forceX * deltaTime * sin(Utils::toRad<float>(angleX));
2022-06-05 02:17:49 +03:00
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);
}
}