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

Compare commits

...

9 commits

10 changed files with 99 additions and 19 deletions

11
examples/maze4.txt Normal file
View file

@ -0,0 +1,11 @@
XXXXXXXXX
X X
XXX XX X
X X
X X XX X
X X X
X XXX XXX
X X
XXXXXX X
XG X
XXXXXXXXX

5
examples/maze5.txt Normal file
View file

@ -0,0 +1,5 @@
XX
XX XX
XX

5
examples/maze6.txt Normal file
View file

@ -0,0 +1,5 @@
XXXXXXXXXX
X X X X
X XXX X
X X X
XXXXXXXXXX

View file

@ -95,6 +95,9 @@ void load_maze(const std::string& path) {
case 'X': case 'X':
row.push_back(1); row.push_back(1);
break; break;
case 'G':
row.push_back(2);
break;
default: default:
row.push_back(-1); row.push_back(-1);
} }

View file

@ -8,28 +8,36 @@
#include "utils.hpp" #include "utils.hpp"
constexpr double PI = 3.14159265358979;
MazeScreen::MazeScreen(const std::vector<std::vector<int>>& maze): MazeScreen::MazeScreen(const std::vector<std::vector<int>>& maze):
maze(maze), maze(maze),
angleX(0), angleX(0),
mouseCapture(false), mouseCapture(false),
posX(1), posX(9999),
posZ(1) {} posZ(9999),
solid(true) {
for (int i = 0; i < maze.size(); i++) {
for (int j = 0; j < maze[i].size(); j++) {
if (maze[i][j] == 0 && j < posZ) {
posX = i;
posZ = j;
}
}
}
}
void MazeScreen::display() { void MazeScreen::display() {
glClearColor(0.8, 0.8, 0.8, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); glLoadIdentity();
//configures the camera with the position_x and position_z variables
gluLookAt( gluLookAt(
posX, posX,
0, 0,
posZ, posZ,
posX + cos((angleX * PI) / 180) * 3, posX + cos(Utils::toRad<float>(angleX)) * 3,
0, 0,
posZ - sin((angleX * PI) / 180) * 3, posZ - sin(Utils::toRad<float>(angleX)) * 3,
0, 0,
1, 1,
0 0
@ -37,6 +45,14 @@ void MazeScreen::display() {
for (int i = 0; i < maze.size(); i++) { for (int i = 0; i < maze.size(); i++) {
for (int j = 0; j < maze[i].size(); j++) { for (int j = 0; j < maze[i].size(); j++) {
// Draw floor
glPushMatrix();
glColor3ub(0x33, 0x33, 0x33);
glTranslatef(i, -1, j);
glutSolidCube(1);
glPopMatrix();
float green, blue;
switch (maze[i][j]) { switch (maze[i][j]) {
case -1: case -1:
glPushMatrix(); glPushMatrix();
@ -47,18 +63,28 @@ void MazeScreen::display() {
break; break;
case 1: case 1:
glPushMatrix(); 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); glTranslatef(i, 0, j);
// Scale green by X
green = Utils::clamp<float>(Utils::nummap<float>(abs(i - posX), 0, 3, 0, 1), 0, 1);
// Scale blue by Z
blue = Utils::clamp<float>(Utils::nummap<float>(abs(j - posZ), 0, 3, 0, 1), 0, 1);
glColor3f(1, green, blue);
glutWireCube(1);
if (solid) { if (solid) {
glColor3f(1, green, blue);
glutSolidCube(1); glutSolidCube(1);
} }
else {
glutWireCube(1); glPopMatrix();
} break;
case 2:
glPushMatrix();
glColor3ub(0xee, 0xee, 0x00);
glTranslatef(i, -1, j);
glutSolidCube(1);
glPopMatrix(); glPopMatrix();
break; break;
} }
@ -75,7 +101,7 @@ void MazeScreen::keyboard(unsigned char key, int x, int y) {
glutSetCursor(GLUT_CURSOR_INHERIT); glutSetCursor(GLUT_CURSOR_INHERIT);
} }
else if (key == ' ') { else if (key == ' ') {
solid = !solid; solid = false;
} }
else if (key == 'w') { else if (key == 'w') {
forceX += 1; forceX += 1;
@ -105,6 +131,10 @@ void MazeScreen::keyboardUp(unsigned char key, int x, int y) {
else if (key == 'd') { else if (key == 'd') {
forceZ -= 1; forceZ -= 1;
} }
else if (key == ' ') {
solid = true;
}
} }
int gameTime; int gameTime;
@ -114,8 +144,8 @@ void MazeScreen::idle() {
auto deltaTime = (newTime - gameTime) / 1000.0; auto deltaTime = (newTime - gameTime) / 1000.0;
gameTime = newTime; gameTime = newTime;
posX += forceX * deltaTime * cos(angleX * PI / 180) + forceZ * deltaTime * sin(angleX * PI / 180); posX += forceX * deltaTime * cos(Utils::toRad<float>(angleX)) + forceZ * deltaTime * sin(Utils::toRad<float>(angleX));
posZ += forceZ * deltaTime * cos(angleX * PI / 180) - forceX * deltaTime * sin(angleX * PI / 180); posZ += forceZ * deltaTime * cos(Utils::toRad<float>(angleX)) - forceX * deltaTime * sin(Utils::toRad<float>(angleX));
glutPostRedisplay(); glutPostRedisplay();
} }

View file

@ -20,3 +20,9 @@ void NoMazeScreen::display() {
glFlush(); glFlush();
glutSwapBuffers(); glutSwapBuffers();
} }
void NoMazeScreen::keyboard(unsigned char key, int x, int y) {
if (key == '\e') {
exit(EXIT_SUCCESS);
}
}

View file

@ -4,4 +4,5 @@
struct NoMazeScreen : public GlutEvents { struct NoMazeScreen : public GlutEvents {
virtual void display(); virtual void display();
virtual void keyboard(unsigned char key, int x, int y);
}; };

View file

@ -26,4 +26,14 @@ namespace Utils {
constexpr T toRad(T deg) { constexpr T toRad(T deg) {
return deg * PI / 180; return deg * PI / 180;
} }
template <typename T>
constexpr T min(T a, T b) {
if (a < b) {
return a;
}
else {
return b;
}
}
} }

6
y3s2-gui-project.desktop Normal file
View file

@ -0,0 +1,6 @@
[Desktop Entry]
Name=Maze Y3S2 GUI Project
Exec=y3s2-gui-project
Terminal=false
Type=Application
Categories=Game;

View file

@ -1,5 +1,5 @@
Name: y3s2-gui-project Name: y3s2-gui-project
Version: 1.0.1 Version: 1.0.7
Release: 1%{?dist} Release: 1%{?dist}
Summary: Maze project in OpenGL for Graphics and User Interfaces lecture in year 3, semester 2 Summary: Maze project in OpenGL for Graphics and User Interfaces lecture in year 3, semester 2
@ -9,6 +9,7 @@ Source0: https://codeberg.org/kbruen/%{name}/archive/v%{version}.tar.gz
BuildRequires: mesa-libGLU-devel BuildRequires: mesa-libGLU-devel
BuildRequires: freeglut-devel BuildRequires: freeglut-devel
BuildRequires: desktop-file-utils
%description %description
Maze project in OpenGL for Graphics and User Interfaces lecture in year 3, semester 2 Maze project in OpenGL for Graphics and User Interfaces lecture in year 3, semester 2
@ -26,8 +27,10 @@ mkdir obj
%install %install
%make_install %make_install
desktop-file-install --dir=%{buildroot}%{_datadir}/applications %{name}.desktop
%files %files
%{_bindir}/%{name} %{_bindir}/%{name}
%{_datadir}/applications/%{name}.desktop