qub,
спасибо, работает. Такой вопрос: как можно уменьшить и оптимизировать данный код что бы в функции display() вместо прямоугольника выводилась текстура?
#include <GL/freeglut.h>
#include <fstream>
#include <iostream>
#include <cmath>
const int TIME = 50;
const int SIZE = 10;
const int SIZE_X = 24;
const int SIZE_Y = 20;
bool WIN;
int x, y;
int pole[SIZE_X][SIZE_Y];
void poleInit()
{
std::ifstream fin("map.txt");
for(int m=0; m<SIZE_X; m++)
{
for(int n=0; n<SIZE_Y; n++)
{
fin >> pole[m][n];
if (pole[m][n]==2)
{
x = m;
y = n;
}
}
}
pole[x][y] = 2;
}
void PrintText2D(double x, double y, std::string text)
{
glRasterPos2f(x, y);
glutBitmapString(GLUT_BITMAP_9_BY_15, (const unsigned char*)text.c_str());
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
for(int m=0; m<SIZE_X; m++)
{
for(int n=0; n<SIZE_Y; n++)
{
switch(pole[m][n])
{
case 1:
glColor3f(0.0, 0.0, 0.6);
glRectf(m*SIZE, n*SIZE, m*SIZE+SIZE, n*SIZE+SIZE);
break; //стена
case 2:
glColor3f(0.5, 0.5, 0.5);
glRectf(m*SIZE+1, n*SIZE+1, m*SIZE+SIZE-1, n*SIZE+SIZE-1);
break; //игрок
//case 3:
//glColor3f(0.0, 0.00, 0.0);
//glRectf(m*SIZE, n*SIZE, m*SIZE+SIZE, n*SIZE+SIZE);
//break; //выход
}
}
}
if (WIN==true)
{
glColor3f(1.0, 1.0, 1.0);
PrintText2D(100, 100, "YOU WIN!");
}
glFlush();
}
void win()
{
WIN = true;
}
void Keyboard(unsigned char key, int au, int bu)
{
if (key == 27) exit(0);
}
void MyKeyboard(int key, int xu, int yu)
{
pole[x][y] = 0;
switch(key)
{
case GLUT_KEY_UP : if (y != SIZE_Y-1 && pole[x][y+1] != 1) y++; break;
case GLUT_KEY_DOWN : if (y != 0 && pole[x][y-1] != 1) y--; break;
case GLUT_KEY_LEFT : if (x != 0 && pole[x-1][y] != 1) x--; break;
case GLUT_KEY_RIGHT : if (x != SIZE_X-1 && pole[x+1][y] != 1) x++; break;
}
if (pole[x][y]==3)win();
pole[x][y] = 2;
}
void timer(int = 0)
{
display();
glutTimerFunc(TIME,timer,0);
}
int main(int argc, char **argv)
{
poleInit();
for (int xx=0; xx<SIZE_X; xx++)
{
for (int yy=0; yy<SIZE_Y; yy++)
{
std::cout << pole[xx][yy] << " ";
}
std::cout << std::endl;
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(SIZE*2*SIZE_X, SIZE*2*SIZE_Y);
glutInitWindowPosition(250, 50);
glutCreateWindow("Labirint");
glClearColor(0.0, 0.0, 0.0, 0.0); //чёрный
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, SIZE_X*SIZE, 0, SIZE_Y*SIZE);
glutDisplayFunc(display);
glutTimerFunc(TIME,timer,0);
glutKeyboardFunc(Keyboard);
glutSpecialFunc(MyKeyboard);
glutMainLoop();
}