|
|
@ -1,243 +1,158 @@ |
|
|
|
#include <iostream> |
|
|
|
#include <fstream> |
|
|
|
#include <functional> |
|
|
|
#include <vector> |
|
|
|
|
|
|
|
#include <memory> |
|
|
|
#include <string> |
|
|
|
#include <string> |
|
|
|
#include <cmath> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <GL/gl.h> |
|
|
|
#include <GL/gl.h> |
|
|
|
#include <GL/glu.h> |
|
|
|
#include <GL/glu.h> |
|
|
|
#include <GL/glut.h> |
|
|
|
#include <GL/glut.h> |
|
|
|
|
|
|
|
|
|
|
|
#include "pixel_wise.h" |
|
|
|
#include "glut_events.h" |
|
|
|
#include "drawable.h" |
|
|
|
|
|
|
|
#include "utils.hpp" |
|
|
|
#include "utils.hpp" |
|
|
|
|
|
|
|
#include "no_maze.h" |
|
|
|
|
|
|
|
#include "maze.h" |
|
|
|
|
|
|
|
|
|
|
|
GLfloat deltaTime; |
|
|
|
std::vector<std::vector<int>> maze; |
|
|
|
|
|
|
|
std::unique_ptr<GlutEvents> events; |
|
|
|
bool gameRunning = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool mouseDown = false; |
|
|
|
|
|
|
|
int leftScore = 0; |
|
|
|
|
|
|
|
int rightScore = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int leftMove = 0; |
|
|
|
|
|
|
|
int rightMove = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
void mouse(int button, int state, int x, int y) { |
|
|
|
void mouse(int button, int state, int x, int y) { |
|
|
|
if (state == GLUT_DOWN) { |
|
|
|
if (events) { |
|
|
|
gameRunning = true; |
|
|
|
events->mouse(button, state, x, y); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void motion(int x, int y) { |
|
|
|
class BoardMiddle : public Drawable { |
|
|
|
if (events) { |
|
|
|
public: |
|
|
|
events->motion(x, y); |
|
|
|
virtual void draw() { |
|
|
|
|
|
|
|
glBegin(GL_POLYGON); |
|
|
|
|
|
|
|
glColor3ub(0xBD, 0xBD, 0xBD); |
|
|
|
|
|
|
|
glVertex2fv(PW::nm(-2, -1).data()); |
|
|
|
|
|
|
|
glVertex2fv(PW::nm(2, -1).data()); |
|
|
|
|
|
|
|
glVertex2fv(PW::nm(2, 1).data()); |
|
|
|
|
|
|
|
glVertex2fv(PW::nm(-2, 1).data()); |
|
|
|
|
|
|
|
glEnd(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} bm; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Ball : public Drawable { |
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
static constexpr GLfloat defaultVeloX = 0.5; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GLfloat radius; |
|
|
|
|
|
|
|
GLfloat x, y; |
|
|
|
|
|
|
|
GLfloat veloX, veloY; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ball(GLfloat x, GLfloat y) : x(x), y(y), radius(0.025), veloX(defaultVeloX) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
virtual void draw() { |
|
|
|
void passiveMotion(int x, int y) { |
|
|
|
glBegin(GL_POLYGON); |
|
|
|
if (events) { |
|
|
|
glColor3ub(0xFF, 0xEE, 0x58); |
|
|
|
events->passiveMotion(x, y); |
|
|
|
for (float i = 0; i < 360; i += 0.5) { |
|
|
|
|
|
|
|
const auto rad = Utils::toRad(i); |
|
|
|
|
|
|
|
glVertex2f(x + radius * cos(rad), y + radius * sin(rad)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
glEnd(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
void update() { |
|
|
|
void visibility(int state) { |
|
|
|
x += veloX * deltaTime; |
|
|
|
if (events) { |
|
|
|
y += veloY * deltaTime; |
|
|
|
events->visibility(state); |
|
|
|
} |
|
|
|
} |
|
|
|
} ball {0, 0}; |
|
|
|
} |
|
|
|
|
|
|
|
void entry(int state) { |
|
|
|
class Text : public Drawable { |
|
|
|
if (events) { |
|
|
|
std::function<std::string()> strObtainer; |
|
|
|
events->entry(state); |
|
|
|
public: |
|
|
|
} |
|
|
|
std::function<GLfloat()> x, y; |
|
|
|
} |
|
|
|
void *font; |
|
|
|
void special(int key, int x, int y) { |
|
|
|
Text(std::function<std::string()> strObtainer, GLfloat x, GLfloat y) : strObtainer(strObtainer), x([=](){return x;}), y([=](){return y;}), font(GLUT_BITMAP_HELVETICA_18) {} |
|
|
|
if (events) { |
|
|
|
Text(std::function<std::string()> strObtainer, std::function<GLfloat()> x, std::function<GLfloat()> y) : strObtainer(strObtainer), x(x), y(y), font(GLUT_BITMAP_HELVETICA_18) {} |
|
|
|
events->special(key, x, y); |
|
|
|
Text(std::function<std::string()> strObtainer, std::array<GLfloat, 2> pos) : strObtainer(strObtainer), x([=](){return pos[0];}), y([=](){return pos[1];}), font(GLUT_BITMAP_HELVETICA_18) {} |
|
|
|
|
|
|
|
Text(std::function<std::string()> strObtainer, std::function<std::array<GLfloat, 2>()> pos) : strObtainer(strObtainer), x([=](){return pos()[0];}), y([=](){return pos()[1];}), font(GLUT_BITMAP_HELVETICA_18) {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
virtual void draw() { |
|
|
|
|
|
|
|
glRasterPos2f(x(), y()); |
|
|
|
|
|
|
|
for (const auto& c: strObtainer()) { |
|
|
|
|
|
|
|
glutBitmapCharacter(font, c); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} leftScoreText{[](){ return std::to_string(leftScore); }, [](){return PW::tm(-100, 20);}}, |
|
|
|
|
|
|
|
rightScoreText{[](){ return std::to_string(rightScore); }, [](){return PW::tm(100, 20);}}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Paddle : public Drawable { |
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
GLfloat height, width; |
|
|
|
|
|
|
|
GLfloat x, y; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Paddle(GLfloat x, GLfloat y) : x(x), y(y), height(0.4), width(0.02) {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
virtual void draw() { |
|
|
|
|
|
|
|
glBegin(GL_POLYGON); |
|
|
|
|
|
|
|
glVertex2f(x, y); |
|
|
|
|
|
|
|
glVertex2f(x + width, y); |
|
|
|
|
|
|
|
glVertex2f(x + width, y + height); |
|
|
|
|
|
|
|
glVertex2f(x, y + height); |
|
|
|
|
|
|
|
glEnd(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} leftPaddle{-0.9, -0.2}, rightPaddle{0.9 - 0.02, -0.2}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void display(void) { |
|
|
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bm.draw(); |
|
|
|
|
|
|
|
ball.draw(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glColor3ub(0x66, 0xBB, 0x6A); |
|
|
|
|
|
|
|
leftScoreText.draw(); |
|
|
|
|
|
|
|
leftPaddle.draw(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glColor3ub(0x42, 0xA5, 0xF5); |
|
|
|
|
|
|
|
rightScoreText.draw(); |
|
|
|
|
|
|
|
rightPaddle.draw(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glFlush(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int gameTime; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const GLfloat paddleMoveSpeed = 0.8; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void idle() { |
|
|
|
void idle() { |
|
|
|
int newTime = glutGet(GLUT_ELAPSED_TIME); |
|
|
|
if (events) { |
|
|
|
deltaTime = (newTime - gameTime) / 1000.0; |
|
|
|
events->idle(); |
|
|
|
gameTime = newTime; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (gameRunning) { |
|
|
|
void load_maze(const std::string& path) { |
|
|
|
ball.update(); |
|
|
|
std::ifstream f{path}; |
|
|
|
|
|
|
|
std::string line; |
|
|
|
|
|
|
|
|
|
|
|
// Detect ball collission
|
|
|
|
while(true) { |
|
|
|
if (leftPaddle.x <= (ball.x + ball.radius) && (ball.x - ball.radius) <= (leftPaddle.x + leftPaddle.width)) { |
|
|
|
std::getline(f, line); |
|
|
|
// Ball inside left-right area of left paddle
|
|
|
|
if (!f) { |
|
|
|
if (leftPaddle.y <= ball.y && ball.y <= leftPaddle.y + leftPaddle.height) { |
|
|
|
break; |
|
|
|
// Ball inside up-down area of left paddle
|
|
|
|
|
|
|
|
ball.veloX = ball.defaultVeloX; |
|
|
|
|
|
|
|
ball.veloY = Utils::nummap(ball.y, leftPaddle.y, leftPaddle.y + leftPaddle.height, (GLfloat)-1, (GLfloat)1); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
else if (rightPaddle.x <= (ball.x + ball.radius) && (ball.x - ball.radius) <= (rightPaddle.x + rightPaddle.width)) { |
|
|
|
|
|
|
|
// Ball inside left-right area of right paddle
|
|
|
|
std::vector<int> row; |
|
|
|
if (rightPaddle.y <= ball.y && ball.y <= rightPaddle.y + rightPaddle.height) { |
|
|
|
for (const auto& c: line) { |
|
|
|
// Ball inside up-down area of right paddle
|
|
|
|
switch(c) { |
|
|
|
ball.veloX = -ball.defaultVeloX; |
|
|
|
case ' ': |
|
|
|
ball.veloY = Utils::nummap(ball.y, rightPaddle.y, rightPaddle.y + rightPaddle.height, (GLfloat)-1, (GLfloat)1); |
|
|
|
row.push_back(0); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case 'X': |
|
|
|
|
|
|
|
row.push_back(1); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
row.push_back(-1); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else if (ball.x + ball.radius < -1) { |
|
|
|
if (row.size() > 0) { |
|
|
|
// Ball exited board on the left side
|
|
|
|
maze.push_back(row); |
|
|
|
rightScore += 1; |
|
|
|
|
|
|
|
ball.x = ball.y = 0; |
|
|
|
|
|
|
|
ball.veloY = 0; |
|
|
|
|
|
|
|
gameRunning = false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (ball.x - ball.radius > 1) { |
|
|
|
|
|
|
|
// Ball exited board on the right side
|
|
|
|
|
|
|
|
leftScore += 1; |
|
|
|
|
|
|
|
ball.x = ball.y = 0; |
|
|
|
|
|
|
|
ball.veloY = 0; |
|
|
|
|
|
|
|
gameRunning = false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (ball.y - ball.radius < -1) { |
|
|
|
|
|
|
|
// Ball touched bottom side
|
|
|
|
|
|
|
|
ball.veloY *= -1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (ball.y + ball.radius > 1) { |
|
|
|
|
|
|
|
// Ball touched top side
|
|
|
|
|
|
|
|
ball.veloY *= -1; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Move paddle
|
|
|
|
glutInit(&argc, argv); |
|
|
|
leftPaddle.y += leftMove * paddleMoveSpeed * deltaTime; |
|
|
|
|
|
|
|
rightPaddle.y += rightMove * paddleMoveSpeed * deltaTime; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Clamp paddle to stay on screen
|
|
|
|
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); |
|
|
|
if (leftPaddle.y < -1) leftPaddle.y = -1; |
|
|
|
glClearDepth(1.0f); |
|
|
|
if (rightPaddle.y < -1) rightPaddle.y = -1; |
|
|
|
glDepthFunc(GL_LEQUAL); |
|
|
|
if (leftPaddle.y > 1 - leftPaddle.height) leftPaddle.y = 1 - leftPaddle.height; |
|
|
|
|
|
|
|
if (rightPaddle.y > 1 - rightPaddle.height) rightPaddle.y = 1 - rightPaddle.height; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glutPostRedisplay(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void keyDown(unsigned char key, int x, int y) { |
|
|
|
glutInitWindowSize(640, 480); |
|
|
|
switch(key) { |
|
|
|
glutInitWindowPosition((glutGet(GLUT_SCREEN_WIDTH) - 640) * 0.5, (glutGet(GLUT_SCREEN_HEIGHT) - 480) * 0.5); |
|
|
|
case 'w': |
|
|
|
if (maze_file.size() > 0) { |
|
|
|
case 'W': |
|
|
|
glutCreateWindow((std::string{"Maze - "} + maze_file).c_str()); |
|
|
|
leftMove = 1; |
|
|
|
} |
|
|
|
break; |
|
|
|
else { |
|
|
|
case 's': |
|
|
|
glutCreateWindow("Maze"); |
|
|
|
case 'S': |
|
|
|
|
|
|
|
leftMove = -1; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case 'p': |
|
|
|
|
|
|
|
case 'P': |
|
|
|
|
|
|
|
rightMove = 1; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ';': |
|
|
|
|
|
|
|
case ':': |
|
|
|
|
|
|
|
rightMove = -1; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void keyUp(unsigned char key, int x, int y) { |
|
|
|
if (maze_file.size() > 0) { |
|
|
|
switch(key) { |
|
|
|
glEnable(GL_DEPTH_TEST); |
|
|
|
case 'w': |
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
|
|
|
case 'W': |
|
|
|
glMatrixMode(GL_PROJECTION); |
|
|
|
case 's': |
|
|
|
glLoadIdentity(); |
|
|
|
case 'S': |
|
|
|
gluPerspective(60, 1, 0.1, 100); |
|
|
|
leftMove = 0; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case 'p': |
|
|
|
|
|
|
|
case 'P': |
|
|
|
|
|
|
|
case ';': |
|
|
|
|
|
|
|
case ':': |
|
|
|
|
|
|
|
rightMove = 0; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) |
|
|
|
glutDisplayFunc(display); |
|
|
|
{ |
|
|
|
glutOverlayDisplayFunc(overlayDisplay); |
|
|
|
glutInit(&argc, argv); |
|
|
|
glutReshapeFunc(reshape); |
|
|
|
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); |
|
|
|
|
|
|
|
glutInitWindowSize(600, 600); |
|
|
|
|
|
|
|
glutInitWindowPosition(glutGet(GLUT_SCREEN_WIDTH) * 0.6, (glutGet(GLUT_SCREEN_HEIGHT) - 600) * 0.5); |
|
|
|
|
|
|
|
glutCreateWindow("Pong"); |
|
|
|
|
|
|
|
glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF); |
|
|
|
glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF); |
|
|
|
glutKeyboardFunc(keyDown); |
|
|
|
glutKeyboardFunc(keyDown); |
|
|
|
glutKeyboardUpFunc(keyUp); |
|
|
|
glutKeyboardUpFunc(keyUp); |
|
|
|
glutDisplayFunc(display); |
|
|
|
|
|
|
|
glutMouseFunc(mouse); |
|
|
|
glutMouseFunc(mouse); |
|
|
|
|
|
|
|
glutMotionFunc(motion); |
|
|
|
|
|
|
|
glutPassiveMotionFunc(passiveMotion); |
|
|
|
|
|
|
|
glutVisibilityFunc(visibility); |
|
|
|
|
|
|
|
glutEntryFunc(entry); |
|
|
|
|
|
|
|
glutSpecialFunc(special); |
|
|
|
glutIdleFunc(idle); |
|
|
|
glutIdleFunc(idle); |
|
|
|
glutMainLoop(); |
|
|
|
glutMainLoop(); |
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|