This repository has been archived on 2019-04-23. You can view files and clone it, but cannot push or open issues or pull requests.
2048/src/glut.cpp

246 lines
3.9 KiB
C++

#include "../inc/header.h"
extern Case matrice[GRID][GRID];
extern Case oldMatrice[GRID][GRID];
std::string currentText = std::string();
void initializationGlut(int *argc, char **argv)
{
glutInit(argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(SIZE_X, SIZE_Y);
glutInitWindowPosition(POSITION_X, POSITION_Y);
glutCreateWindow("2048");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
#if DARK_MODE
glColor3ub(255, 255, 255);
glClearColor(0, 0, 0, 0);
glColor3d(1, 1, 1);
#else
glColor3ub(0, 0, 0);
glClearColor(255, 255, 255, 255);
glColor3d(0, 0, 0);
#endif
}
void resetDisplay(bool post = true)
{
if(post)
{
glutPostRedisplay();
}
glFlush();
glutSwapBuffers();
}
void display(void)
{
int w = glutGet(GLUT_WINDOW_WIDTH);
int h = glutGet(GLUT_WINDOW_HEIGHT);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, 0, h);
glLineWidth(3.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
for(short i(0); i <= GRID; i++)
{
glBegin(GL_LINES);
glVertex2f(i * (w / GRID), 0);
glVertex2f(i * (w / GRID), h);
glEnd();
glBegin(GL_LINES);
glVertex2f(0, i * (h / GRID));
glVertex2f(w, i * (h / GRID));
glEnd();
}
for(short i(0); i < GRID; i++)
{
for(short j(0); j < GRID; j++)
{
if(matrice[i][j].getState() == FILLED)
{
displayNumber(i, j, matrice[i][j].getValue(), w, h);
}
}
}
// We may need to re-display the text
displayText(currentText);
resetDisplay(false);
}
void reshape(int x, int y)
{
glViewport(0, 0, x, y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, x, 0, y);
}
void keyboard(unsigned char key, int x, int y)
{
special((int)key, x, y);
}
void special(int key, int x, int y)
{
/* In order to deal with '-Wextra' "Unused variable" warning */
(void)x;
(void)y;
bool keepPlaying(false), isPaused(false);
if(!isPaused)
{
switch(key)
{
case GLUT_KEY_UP:
motionUp();
break;
case GLUT_KEY_DOWN:
motionDown();
break;
case GLUT_KEY_LEFT:
motionLeft();
break;
case GLUT_KEY_RIGHT:
motionRight();
break;
default:
return;
}
}
else
{
switch(key)
{
case 'y':
case 'Y':
case 'o':
case 'O':
case 13: // Enter (carriage return)
isPaused = false;
keepPlaying = true;
currentText = std::string();
resetDisplay();
return;
case 'n':
case 'N':
case 32: // Escape
sleep(1);
exit(0);
default:
return;
}
}
if(matriceDidChanged())
{
getRandomNewCase();
resetDisplay();
}
if(!keepPlaying && matriceGot((unsigned int)WIN_NUMBER))
{
// We just set this boolean to true and lets...
// ... the user choose during the next `special` call !
isPaused = true;
currentText = std::string(WON);
return;
}
if(gridFinished())
{
if(!matriceHavePowers())
{
currentText = std::string(LOSE);
}
else
{
currentText = std::string(END);
}
displayText(currentText);
resetDisplay();
sleep(2);
exit(0);
}
}
void displayNumber(short y, short x, unsigned int value, short w, short h)
{
int textWidth(0);
std::string buffer = std::to_string(value);
for(size_t i(0); i < buffer.length(); i++)
{
textWidth += glutBitmapWidth(FONT, buffer.at(i));
}
y = h - (((2 * y) + 1) * (h / (GRID * 2)));
x = (((2 * x) + 1) * (w / (GRID * 2))) - (textWidth / 2.0);
glRasterPos2f(x, y);
for(size_t i(0); i < buffer.length(); i++)
{
glutBitmapCharacter(FONT, buffer.at(i));
}
}
void displayText(std::string text)
{
int textWidth(0);
for(size_t i(0); i < text.length(); i++)
{
textWidth += glutBitmapWidth(FONT, text.at(i));
}
// Center the text on the screen in function of its length and the font height
glRasterPos2f(
(glutGet(GLUT_WINDOW_WIDTH) / 2.0) - (textWidth / 2.0),
glutGet(GLUT_WINDOW_HEIGHT) / (3 / 2.0)
);
for(size_t i(0); i < text.length(); i++)
{
glutBitmapCharacter(FONT, text.at(i));
}
}