454 lines
13 KiB
C
454 lines
13 KiB
C
#include <stdio.h>
|
|
#include <GL/glut.h>
|
|
#include <unistd.h>
|
|
#include <stdbool.h>
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
#include "stb_image.h"
|
|
#include "3d.h"
|
|
#define uchar unsigned char
|
|
struct motion_struct{
|
|
unsigned char xy,t;
|
|
} motion[36];
|
|
struct target_struct{
|
|
unsigned char x;
|
|
unsigned char y;
|
|
};
|
|
//#include "figurinemove.h"
|
|
#include "logic.h"
|
|
float k;
|
|
int width, height;
|
|
int t_1, t_2;
|
|
unsigned char figure[8][8];
|
|
//unsigned char motion[8][8];
|
|
//unsigned char target[] = {0,0};
|
|
struct target_struct target;
|
|
struct target_struct target_click;
|
|
/*
|
|
struct motion{
|
|
unsigned char x;
|
|
unsigned char y;
|
|
struct motion *next;
|
|
unsigned char stop;
|
|
};
|
|
*/
|
|
|
|
|
|
float vretices_2[][2] = {
|
|
{0,0},
|
|
{1,0},
|
|
{1,1},
|
|
{0,1}
|
|
};
|
|
|
|
float vretices[][2] = {
|
|
{-1,-1},
|
|
{1,-1},
|
|
{1,1},
|
|
{-1,1}
|
|
};
|
|
float colors[] = {1,0,0, 0,1,0, 0,0,1, 1,1,0};
|
|
GLuint index_L[] = {1,2,3, 3,0,1};
|
|
//GLuint sas_s[] = {1,2,3}; //sas
|
|
float texCoord_2[] = {0,1, 1,1, 1,0, 0,0};
|
|
float texCoord[] = {0,4, 4,4, 4,0, 0,0};
|
|
unsigned int texsture, texsture_target, texsture_pawn, texsture_wall, texsture_horse, texsture_elephant, texsture_queen, texsture_king;
|
|
void glTexParam(){
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);// GL_CLAMP
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);// GL_REPEAT
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);// GL_NEAREST
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);// GL_LINEAR
|
|
}
|
|
void texsture_target_add(){
|
|
int x, y, cnt;// y x //TEST_kord.png
|
|
unsigned char *data = stbi_load("target.png", &x, &y, &cnt, 0);
|
|
printf("cnt: %i\n", cnt);
|
|
printf("x: %i y: %i\n", x, y);
|
|
glGenTextures(1, &texsture_target);
|
|
glBindTexture(GL_TEXTURE_2D, texsture_target);
|
|
glTexParam();
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y,
|
|
0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
|
glBindTexture(GL_TEXTURE_2D,0);
|
|
//delete data;
|
|
}
|
|
void texsture_figure_add(){
|
|
//printf("cnt: %i\n", cnt);
|
|
//printf("x: %i y: %i\n", x, y);
|
|
int x, y, cnt;// y x //TEST_kord.png
|
|
{
|
|
unsigned char *data = stbi_load("pawn.png", &x, &y, &cnt, 0);
|
|
glGenTextures(1, &texsture_pawn);
|
|
glBindTexture(GL_TEXTURE_2D, texsture_pawn);
|
|
glTexParam();
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y,
|
|
0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
|
glBindTexture(GL_TEXTURE_2D,0);
|
|
}
|
|
{
|
|
unsigned char *data = stbi_load("horse.png", &x, &y, &cnt, 0);
|
|
glGenTextures(1, &texsture_horse);
|
|
glBindTexture(GL_TEXTURE_2D, texsture_horse);
|
|
glTexParam();
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y,
|
|
0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
|
glBindTexture(GL_TEXTURE_2D,0);
|
|
}
|
|
{
|
|
unsigned char *data = stbi_load("elephant.png", &x, &y, &cnt, 0);
|
|
glGenTextures(1, &texsture_elephant);
|
|
glBindTexture(GL_TEXTURE_2D, texsture_elephant);
|
|
glTexParam();
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y,
|
|
0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
|
glBindTexture(GL_TEXTURE_2D,0);
|
|
}
|
|
{
|
|
unsigned char *data = stbi_load("queen.png", &x, &y, &cnt, 0);
|
|
glGenTextures(1, &texsture_queen);
|
|
glBindTexture(GL_TEXTURE_2D, texsture_queen);
|
|
glTexParam();
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y,
|
|
0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
|
glBindTexture(GL_TEXTURE_2D,0);
|
|
}
|
|
{
|
|
unsigned char *data = stbi_load("wall.png", &x, &y, &cnt, 0);
|
|
glGenTextures(1, &texsture_wall);
|
|
glBindTexture(GL_TEXTURE_2D, texsture_wall);
|
|
glTexParam();
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y,
|
|
0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
|
glBindTexture(GL_TEXTURE_2D,0);
|
|
}
|
|
{
|
|
unsigned char *data = stbi_load("king.png", &x, &y, &cnt, 0);
|
|
glGenTextures(1, &texsture_king);
|
|
glBindTexture(GL_TEXTURE_2D, texsture_king);
|
|
glTexParam();
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y,
|
|
0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
|
glBindTexture(GL_TEXTURE_2D,0);
|
|
}
|
|
}
|
|
|
|
void texsture_add(){
|
|
int x = 2, y = 2;// y x
|
|
struct {unsigned char r,g,b,a;} data[y][x];
|
|
memset(data, 0,sizeof(data));
|
|
|
|
//data = (data *) malloc(sizeof(data));
|
|
data[1][0].r = 144;
|
|
data[1][0].b = 143;
|
|
data[1][0].g = 141;
|
|
|
|
data[0][1].r = 144;
|
|
data[0][1].b = 143;
|
|
data[0][1].g = 141;
|
|
|
|
data[0][0].r = 110;
|
|
data[0][0].b = 109;
|
|
data[0][0].g = 107;
|
|
|
|
data[1][1].r = 110;
|
|
data[1][1].b = 109;
|
|
data[1][1].g = 107;
|
|
|
|
|
|
glGenTextures(1, &texsture);
|
|
glBindTexture(GL_TEXTURE_2D, texsture);
|
|
glTexParam();
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, x, y,
|
|
0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
|
glBindTexture(GL_TEXTURE_2D,0);
|
|
//delete &data;
|
|
}
|
|
void render_figure(int x, int y, int texsture){
|
|
glLoadIdentity();
|
|
glOrtho(-k,k, -1,1, -1,1);
|
|
glOrtho(0,8, 0,8, -1,1);
|
|
glTranslatef(x,y,0);
|
|
glVertexPointer(2, GL_FLOAT, 0 , &vretices_2);
|
|
glBindTexture(GL_TEXTURE_2D, texsture);
|
|
glTexCoordPointer(2, GL_FLOAT, 0, texCoord_2);
|
|
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
|
glBindTexture(GL_TEXTURE_2D,0);
|
|
}
|
|
void render_figure_3(int x, int y){
|
|
glLoadIdentity();
|
|
glOrtho(-k,k, -1,1, -1,1);
|
|
glOrtho(0,8, 0,8, -1,1);
|
|
glTranslatef(x,y,1);
|
|
glVertexPointer(2, GL_FLOAT, 0 , &vretices_2);
|
|
glTexCoordPointer(2, GL_FLOAT, 0, texCoord_2);
|
|
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
|
}
|
|
|
|
float q = 0, q_2 = 0, q_3 = 0.5, q_4 = 0;
|
|
void render(void){
|
|
if (q_3 < 0) q_4+=0.00001;
|
|
else if (q_3 > 0) q_4-=0.00001;
|
|
//glClearColor(0.8f,0.8f,0.0f,0.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
|
|
glutPostRedisplay();
|
|
glPushMatrix();
|
|
|
|
glLoadIdentity();
|
|
glFrustum(-k,k, -1,1, 2, 50);
|
|
glTranslatef(-2,0.5,-3);
|
|
glRotatef(q+=0.2,1,0,1);
|
|
glRotatef(q_2+=0.1,0,1,0);
|
|
glLineWidth(2);
|
|
glColor3f(0.1f,0.8f,0.0f);
|
|
render_figure_2();
|
|
|
|
glLoadIdentity();
|
|
glFrustum(-k,k, -1,1, 2, 50);
|
|
glTranslatef(2,q_3+=q_4,-3);
|
|
glRotatef(-90,1,0,0);
|
|
glRotatef(15,0,1,0);
|
|
glRotatef(q_2,0,0,1);
|
|
glLineWidth(2);
|
|
glColor3f(0.1f,0.8f,0.0f);
|
|
render_figure_2();
|
|
|
|
glColor3f(1,1,1);
|
|
|
|
|
|
//glClearColor(1.0f,1.0f,1.0f,0.0f);
|
|
//glOrtho(-k,k, -1,1, -1,1);
|
|
glLoadIdentity();
|
|
glOrtho(-k,k, -1,1, -1,1);
|
|
//glRotatef(2,0,0,1);
|
|
|
|
|
|
glEnable(GL_ALPHA_TEST);
|
|
glAlphaFunc(GL_GREATER, 0.1);
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
glBindTexture(GL_TEXTURE_2D, texsture);
|
|
//glBindTexture(GL_TEXTURE_2D, texsture_2);
|
|
//glColor3f(1,1,1);
|
|
//glColor3f(0.3,0,1);
|
|
//glPushMatrix();
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
|
|
glVertexPointer(2, GL_FLOAT, 0 , &vretices);
|
|
glTexCoordPointer(2, GL_FLOAT, 0, texCoord);
|
|
|
|
//glColorPointer(3, GL_FLOAT, 0, &colors);
|
|
//glEnableClientState(GL_COLOR_ARRAY);
|
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, &index_L);
|
|
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
|
glBindTexture(GL_TEXTURE_2D,0);
|
|
//glOrtho(-k,k, 0,1, -1,1);
|
|
|
|
for (int i = 0; motion[i].t != 0; i++){
|
|
if (motion[i].t == 1) glColor4f(1,1,1, 0.9);
|
|
else if (motion[i].t == 2) glColor4f(1,0.2,0.2, 0.9);
|
|
render_figure_3(motion[i].xy>>4, motion[i].xy&15);
|
|
//printf("target XY: %i %i T: %i\n", motion[i].xy>>4, motion[i].xy&15, motion[i].t);
|
|
|
|
|
|
}
|
|
//printf("-------------------------\n");
|
|
for (int y = 7; y != -1; y--){
|
|
for (int x = 0; x != 8; x+=1){
|
|
glColor3f(1,1,1);
|
|
if (figure[x][y] == 1) render_figure(x,y,texsture_pawn);
|
|
if (figure[x][y] == 2) render_figure(x,y,texsture_wall);
|
|
if (figure[x][y] == 3) render_figure(x,y,texsture_horse);
|
|
if (figure[x][y] == 4) render_figure(x,y,texsture_elephant);
|
|
if (figure[x][y] == 5) render_figure(x,y,texsture_queen);
|
|
if (figure[x][y] == 6) render_figure(x,y,texsture_king);
|
|
glColor3f(0,0,0);
|
|
if (figure[x][y] == 7) render_figure(x,y,texsture_pawn);
|
|
if (figure[x][y] == 8) render_figure(x,y,texsture_wall);
|
|
if (figure[x][y] == 9) render_figure(x,y,texsture_horse);
|
|
if (figure[x][y] == 10) render_figure(x,y,texsture_elephant);
|
|
if (figure[x][y] == 11) render_figure(x,y,texsture_queen);
|
|
if (figure[x][y] == 12) render_figure(x,y,texsture_king);
|
|
|
|
|
|
}
|
|
}
|
|
glLoadIdentity();
|
|
glOrtho(-k,k, -1,1, -1,1);
|
|
|
|
render_figure(target.x,target.y,texsture_target);
|
|
|
|
|
|
glColor3f(1,1,1);
|
|
glDisableClientState(GL_VERTEX_ARRAY);
|
|
glDisableClientState(GL_COLOR_ARRAY);
|
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
glutSwapBuffers();
|
|
glFlush();
|
|
usleep(1000000/144);
|
|
|
|
|
|
|
|
}
|
|
|
|
//glutFullScreen();
|
|
void key_target(int key, int x, int y){
|
|
printf("key: %i\n", key);
|
|
switch (key){
|
|
case GLUT_KEY_F11 : glutFullScreen(); break;
|
|
case GLUT_KEY_F10 : glutReshapeWindow(width, height); break;
|
|
}
|
|
}
|
|
void ChangeWindow(int width_l, int height_l){
|
|
width = width_l;
|
|
height = height_l;
|
|
printf("width: %i width: %i\n", width, height);
|
|
glViewport(0,0, width, height);
|
|
glLoadIdentity();
|
|
k = width / (float)height;
|
|
glOrtho(-k,k, -1,1, -1,1);
|
|
t_1 = height / 8;
|
|
t_2 = width / 2 - (height / 2);
|
|
|
|
|
|
}
|
|
void TEST(int button, int state, int x, int y){
|
|
if (state){
|
|
printf("SAS %i\n", state);
|
|
//printf("SAS x: %i y: %i x: %i y: %i\n", target.x, target.y, motion[0].xy>>4, motion[0].xy&15);
|
|
|
|
for (int i = 0; motion[i].t != 0; i++){
|
|
if ((target.x == motion[i].xy>>4) && (target.y == (motion[i].xy&15))){
|
|
printf("SAS %i BB %i\n", figure[target.x][target.y], figure[target_click.x][target_click.y]);
|
|
figure[target.x][target.y] = figure[target_click.x][target_click.y];
|
|
figure[target_click.x][target_click.y] = 0;
|
|
motion[0].t = 0;
|
|
|
|
/*
|
|
if (motion[0].t == 2){
|
|
figure[target.x][target.y] = 0;
|
|
}
|
|
else{
|
|
|
|
}
|
|
*/
|
|
|
|
|
|
|
|
}
|
|
}
|
|
target_click = target;
|
|
if (figure[target_click.x][target_click.y] == 0){
|
|
motion[0].t = 0;
|
|
}
|
|
else if (figure[target_click.x][target_click.y] == 0){
|
|
motion[0].t = 0;
|
|
}
|
|
}
|
|
|
|
}
|
|
void TEST_3(){
|
|
|
|
}
|
|
void onMouseMove(int x, int y){
|
|
//printf("X: %i Y: %i\n", x, y);
|
|
//printf("t_1: %i t_2: %i\n", t_1, t_2);
|
|
//printf("TEST: %i t_1: %i\n", y / t_1, t_1);
|
|
//printf("TEST_2: %i t_2: %i\n", (x - t_2) / t_1, t_2);
|
|
//if (t_1 >= y) printf("SAS!!!!!!!!!\n");
|
|
//if (t_2 <= x and t_2 + (t_1) >= x) printf("XXXXX!!!!!!!!!\n");
|
|
//printf("x: %i y: %i\n", (x - t_2) / t_1, y / t_1);
|
|
int test = (x - t_2) / t_1;
|
|
int test_2 = -(y / t_1) + 7;
|
|
//printf("target X: %i Y: %i\n", test, test_2);
|
|
if (test >= 0 && test < 8 && test_2 != -1){// Хаглушка для ошибки test_2 когда на краю экрана можно получить test_2 = -1
|
|
target.x = test;
|
|
target.y = test_2;
|
|
}
|
|
printf("target X: %i Y: %i\n", target.x, y / t_1);
|
|
if (figure[target_click.x][target_click.y] == 0){
|
|
selection_moves(figure, motion, target);
|
|
//nnn(figure, motion, target);
|
|
}
|
|
else{
|
|
selection_moves(figure, motion, target_click);
|
|
//nnn(figure, motion, target);
|
|
}
|
|
//printf("target XY: %i %i T: %i\n", motion[0].xy>>4, motion[0].xy&15, motion[0].t);
|
|
|
|
|
|
}
|
|
/*
|
|
void TESTSSS(unsigned char figure[8][8], struct motion_struct qqq[28], unsigned char target[2]){
|
|
//motion[0]->xy = 12;
|
|
qqq[2].xy = 2;
|
|
}
|
|
*/
|
|
int main(int argc, char** argv){//
|
|
//printf("SSSS: %i\n", 1 > 2 + 2 || 2==1);
|
|
/*
|
|
int i =0;
|
|
motion[i].xy = 123;
|
|
motion[i].t = 1;
|
|
i++;
|
|
motion[i].xy = 134;
|
|
motion[i].t = 2;
|
|
TESTSSS(figure, motion, target);
|
|
printf("motion: %i\n", motion[0].xy);
|
|
*/
|
|
|
|
|
|
SAS();
|
|
glutInit(&argc, argv);
|
|
|
|
//TEST_LL(figure);
|
|
/*
|
|
for (int i = 7; i >= 0; i--) figure[i][6] = 7;
|
|
for (int i = 7; i >= 0; i--) figure[i][1] = 1;
|
|
|
|
|
|
figure[0][0] = 2; figure[0][7] = 8;
|
|
figure[7][0] = 2; figure[7][7] = 8;
|
|
|
|
figure[1][0] = 3; figure[1][7] = 9;
|
|
figure[6][0] = 3; figure[6][7] = 9;
|
|
|
|
figure[2][0] = 4; figure[2][7] = 10;
|
|
figure[5][0] = 4; figure[5][7] = 10;
|
|
|
|
figure[4][0] = 5; figure[4][7] = 11;
|
|
figure[3][0] = 6; figure[3][7] = 12;
|
|
*/
|
|
/*
|
|
figure[2][2] = 7;
|
|
figure[3][3] = 1;
|
|
|
|
figure[2][4] = 8;
|
|
figure[3][7] = 8;
|
|
figure[2][2] = 2;
|
|
figure[6][4] = 10;
|
|
figure[5][5] = 11;
|
|
|
|
figure[2][4] = 8;
|
|
figure[3][2] = 1;
|
|
figure[6][7] = 7;
|
|
figure[5][5] = 2;
|
|
*/
|
|
figure[6][7] = 7;
|
|
figure[5][5] = 3;
|
|
glutInitDisplayMode(GLUT_SINGLE);
|
|
glutInitWindowSize(800, 800);
|
|
glutInitWindowPosition(100, 100);
|
|
glutCreateWindow("Hello world!");
|
|
texsture_add();
|
|
texsture_figure_add();
|
|
texsture_target_add();
|
|
glutDisplayFunc(render);
|
|
glutSpecialFunc(key_target);
|
|
glutMouseFunc(TEST);
|
|
glutPassiveMotionFunc(onMouseMove);
|
|
glutReshapeFunc(ChangeWindow);
|
|
glutMainLoop();
|
|
|
|
|
|
return 0;
|
|
}
|