1
0
Fork 0
mirror of https://codeberg.org/kbruen/y3s2-gui-project.git synced 2025-05-02 04:29:55 +03:00
y3s2-gui-project/src/main.cpp

160 lines
2.9 KiB
C++
Raw Normal View History

2022-06-05 02:17:49 +03:00
#include <fstream>
#include <vector>
#include <memory>
2022-03-21 01:39:06 +02:00
#include <string>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
2022-06-05 02:17:49 +03:00
#include "glut_events.h"
2022-03-21 01:39:06 +02:00
#include "utils.hpp"
2022-06-05 02:17:49 +03:00
#include "no_maze.h"
#include "maze.h"
2022-03-21 01:39:06 +02:00
2022-06-05 02:17:49 +03:00
std::vector<std::vector<int>> maze;
std::unique_ptr<GlutEvents> events;
2022-03-21 01:39:06 +02:00
2022-06-05 02:17:49 +03:00
void display(void) {
if (events) {
events->display();
}
}
void overlayDisplay(void) {
if (events) {
events->overlayDisplay();
}
}
void reshape(int width, int height) {
if (events) {
events->reshape(width, height);
}
}
void keyDown(unsigned char key, int x, int y) {
if (events) {
events->keyboard(key, x, y);
}
}
void keyUp(unsigned char key, int x, int y) {
if (events) {
events->keyboardUp(key, x, y);
}
}
2022-03-21 01:39:06 +02:00
void mouse(int button, int state, int x, int y) {
2022-06-05 02:17:49 +03:00
if (events) {
events->mouse(button, state, x, y);
2022-03-21 01:39:06 +02:00
}
}
2022-06-05 02:17:49 +03:00
void motion(int x, int y) {
if (events) {
events->motion(x, y);
2022-03-21 01:39:06 +02:00
}
2022-06-05 02:17:49 +03:00
}
void passiveMotion(int x, int y) {
if (events) {
events->passiveMotion(x, y);
2022-03-21 01:39:06 +02:00
}
2022-06-05 02:17:49 +03:00
}
void visibility(int state) {
if (events) {
events->visibility(state);
2022-03-21 01:39:06 +02:00
}
2022-06-05 02:17:49 +03:00
}
void entry(int state) {
if (events) {
events->entry(state);
}
}
void special(int key, int x, int y) {
if (events) {
events->special(key, x, y);
2022-03-21 01:39:06 +02:00
}
}
void idle() {
2022-06-05 02:17:49 +03:00
if (events) {
events->idle();
}
}
2022-03-21 01:39:06 +02:00
2022-06-05 02:17:49 +03:00
void load_maze(const std::string& path) {
std::ifstream f{path};
std::string line;
2022-03-21 01:39:06 +02:00
2022-06-05 02:17:49 +03:00
while(true) {
std::getline(f, line);
if (!f) {
break;
2022-03-21 01:59:09 +02:00
}
2022-06-05 02:17:49 +03:00
std::vector<int> row;
for (const auto& c: line) {
switch(c) {
case ' ':
row.push_back(0);
break;
case 'X':
row.push_back(1);
break;
default:
row.push_back(-1);
2022-03-21 01:59:09 +02:00
}
}
2022-06-05 02:17:49 +03:00
if (row.size() > 0) {
maze.push_back(row);
2022-03-21 01:59:09 +02:00
}
2022-03-21 01:39:06 +02:00
}
2022-06-05 02:17:49 +03:00
}
int main(int argc, char** argv)
{
std::string maze_file;
if (argc < 2) {
events = std::make_unique<NoMazeScreen>();
}
else {
maze_file = std::string{argv[1]};
load_maze(maze_file);
events = std::make_unique<MazeScreen>(maze);
}
2022-03-21 01:39:06 +02:00
2022-06-05 02:17:49 +03:00
glutInit(&argc, argv);
2022-03-21 01:59:09 +02:00
2022-06-05 02:17:49 +03:00
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);
2022-03-21 01:59:09 +02:00
2022-03-21 01:39:06 +02:00
2022-06-05 02:17:49 +03:00
glutInitWindowSize(640, 480);
glutInitWindowPosition((glutGet(GLUT_SCREEN_WIDTH) - 640) * 0.5, (glutGet(GLUT_SCREEN_HEIGHT) - 480) * 0.5);
if (maze_file.size() > 0) {
glutCreateWindow((std::string{"Maze - "} + maze_file).c_str());
}
else {
glutCreateWindow("Maze");
2022-03-21 01:39:06 +02:00
}
2022-06-05 02:17:49 +03:00
if (maze_file.size() > 0) {
glEnable(GL_DEPTH_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1, 0.1, 100);
2022-03-21 01:39:06 +02:00
}
2022-06-05 02:17:49 +03:00
glutDisplayFunc(display);
glutOverlayDisplayFunc(overlayDisplay);
glutReshapeFunc(reshape);
2022-03-21 01:39:06 +02:00
glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF);
glutKeyboardFunc(keyDown);
glutKeyboardUpFunc(keyUp);
glutMouseFunc(mouse);
2022-06-05 02:17:49 +03:00
glutMotionFunc(motion);
glutPassiveMotionFunc(passiveMotion);
glutVisibilityFunc(visibility);
glutEntryFunc(entry);
glutSpecialFunc(special);
2022-03-21 01:39:06 +02:00
glutIdleFunc(idle);
glutMainLoop();
return 0;
}