Сделал 2D шахматы на C в OpenGL.

This commit is contained in:
Arseniy Romenskiy 2025-07-03 20:18:36 +03:00
parent ee8cd78b43
commit def1c798e5
18 changed files with 8858 additions and 5 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
build/
*~

41
.kdev4/C_chess.kdev4 Normal file
View file

@ -0,0 +1,41 @@
[Buildset]
BuildItems=@Variant(\x00\x00\x00\t\x00\x00\x00\x00\x01\x00\x00\x00\x0b\x00\x00\x00\x00\x01\x00\x00\x00\x0e\x00C\x00_\x00c\x00h\x00e\x00s\x00s)
[CMake]
Build Directory Count=1
Current Build Directory Index-Основная система=0
[CMake][CMake Build Directory 0]
Build Directory Path=/home/romenskiy2012/projects/C_chess/build
Build Type=Debug
CMake Binary=/usr/bin/cmake
CMake Executable=/usr/bin/cmake
Environment Profile=
Extra Arguments=
Install Directory=/usr/local
Runtime=Основная система
[Launch]
Launch Configurations=Launch Configuration 0
[Launch][Launch Configuration 0]
Configured Launch Modes=execute
Configured Launchers=nativeAppLauncher
Name=Новая конфигурация «Компилируемая программа»
Type=Native Application
[Launch][Launch Configuration 0][Data]
Arguments=
Dependencies=@Variant(\x00\x00\x00\t\x00\x00\x00\x00\x00)
Dependency Action=Nothing
EnvironmentGroup=
Executable=file:///home/romenskiy2012/projects/C_chess
External Terminal=konsole --workdir %workdir -e %exe
Kill Before Executing Again=4194304
Project Target=C_chess,c_chess
Use External Terminal=true
Working Directory=
isExecutable=false
[Project]
VersionControlSupport=kdevgit

166
3d.h Normal file
View file

@ -0,0 +1,166 @@
# define M_PI 3.14159265358979323846 /* pi */
#define CNT 10
float k;
int width, height;
float vretices_4[CNT*3][3];
//float texCoord_2[] = {0,1, 1,1, 1,0, 0,0};
GLuint index_L_3[CNT*3*3+1][2];
void SAS(){
float x,y;
int v = CNT;
float l = 0.30;
float a = M_PI * 2 / CNT;
for (int i = 0; i < CNT; i++){
x = sin(a*i) * l;
y = cos(a*i) * l;
vretices_4[i][0] = x;
vretices_4[i][1] = y;
vretices_4[i][2] = 0;
}
l = 0.15;
for (int i = 0; i < CNT; i++){
x = sin(a*i) * l;
y = cos(a*i) * l;
vretices_4[v+i][0] = x;
vretices_4[v+i][1] = y;
vretices_4[v+i][2] = 0.6;
}
v = v + CNT;
l = 0.2;
for (int q = 0; q < CNT; q++){
x = sin(a*q) * l;
y = cos(a*q) * l;
vretices_4[v+q][0] = x;
vretices_4[v+q][1] = y;
vretices_4[v+q][2] = 0.8;
}
v = v + CNT;
vretices_4[v][0] = 0;
vretices_4[v][1] = 0;
vretices_4[v][2] = 0.9;
v++;
vretices_4[v][0] = 0;
vretices_4[v][1] = 0;
vretices_4[v][2] = 0;
int i = 0, nnn;
for (int m = 0; m != 3; m++){
nnn = i;
for (int b = 0; b <= CNT-2; i++, b++){
index_L_3[i][0] = i;
index_L_3[i][1] = i+1;
}
index_L_3[i][0] = i;
index_L_3[i][1] = nnn;
i++;
}
nnn = i;
int M = i;
i = 0;
for (int b = 0; b <= CNT-1; i++, b++, nnn++){
index_L_3[nnn][0] = i;
index_L_3[nnn][1] = i+CNT;
nnn++;
index_L_3[nnn][0] = i;
index_L_3[nnn][1] = M+1;
nnn++;
if (b != 0){
index_L_3[nnn][0] = i;
index_L_3[nnn][1] = i+CNT-1;
}
else{
index_L_3[nnn][0] = i;
index_L_3[nnn][1] = i+CNT+CNT-1;
}
}
for (int b = 0; b <= CNT-1; i++, b++, nnn++){
index_L_3[nnn][0] = i;
index_L_3[nnn][1] = i+CNT;
nnn++;
index_L_3[nnn][1] = i+CNT;
index_L_3[nnn][0] = M;
nnn++;
if (b != 0){
index_L_3[nnn][0] = i;
index_L_3[nnn][1] = i+CNT-1;
}
else{
index_L_3[nnn][0] = i;
index_L_3[nnn][1] = i+CNT+CNT-1;
}
}
index_L_3[nnn][1] = M;
index_L_3[nnn][0] = M+1;
}
unsigned int texsture_target;
/*
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 render_figure_2(){
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0 , &vretices_4);
glDrawElements(GL_LINES, sizeof(index_L_3) / sizeof(index_L_3[0][0]), GL_UNSIGNED_INT, &index_L_3);
// GL_LINES GL_TRIANGLE_FAN
glDisableClientState(GL_VERTEX_ARRAY);
}
//float i = 0, q = 0;
float i = 0;
/*
void render(void){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutPostRedisplay();
glPushMatrix();
glColor3f(1,1,1);
glLoadIdentity();
glFrustum(-k,k, -1,1, 2, 50);
glTranslatef(0,0,-3);
glRotatef(q+=0.2,1,0,1);
glLineWidth(2);
glColor3f(0.1f,0.8f,0.0f);
render_figure_2();
glutSwapBuffers();
glFlush();
usleep(1000000/144);
}
*/
/*
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);
}
int main(int argc, char** argv){
SAS();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(800, 800);
glutInitWindowPosition(100, 100);
glutCreateWindow("Hello world!");
glutDisplayFunc(render);
glutReshapeFunc(ChangeWindow);
glutMainLoop();
return 0;
}
*/

View file

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.0)
project(c_chess LANGUAGES C)
set (CMAKE_C_FLAGS "-L /lib/libglut.so -lglut -lGLU -lGL -lm")
add_executable(c_chess main.c)
install(TARGETS c_chess RUNTIME DESTINATION bin)

20
README.md Normal file
View file

@ -0,0 +1,20 @@
# WebDisk_C
C_chess это 2D шахматы с рабочей логикой. Проект в разработке.
C_chess is a 2D chess game with working logic. The project is under development.
------
# Зависимости
У вас должны быть devel пакеты OpenGL.
You must have devel OpenGL packages.
-------
# Сборка Проекта/Building Project
```bash
mkdir build
cd build
cmake ..
make
```
Сосле положите содержимое папки assets в директорию с исполняемым файлом.
Next, put the contents of the assets folder in the directory with the executable file.

BIN
assets/elephant.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 157 B

BIN
assets/horse.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 B

BIN
assets/king.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 160 B

BIN
assets/pawn.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 125 B

BIN
assets/queen.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 159 B

BIN
assets/target.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 170 B

BIN
assets/wall.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 140 B

110
figurinemove.h Normal file
View file

@ -0,0 +1,110 @@
#pragma once
#include <stdio.h>
#include <stdlib.h>
unsigned char firstMove = 1;
enum FigureName{
PawnWhite = 1,
PawnBlack = 7,
KingWhite = 5,
KingBlack = 11,
RookWhite = 2,
RookBlack = 8,
BishopWhite = 4,
BishopBlack = 10,
QueenWhite = 6,
QueenBlack = 12,
KnightWhite = 3,
KnightBlack = 9
};
unsigned char convertXYcoordinatesToBits(unsigned char x, unsigned char y){
return (x << 4) + y;
}
void FigureMove(unsigned char figure[8][8], struct motion_struct motion[28], unsigned char target[2]){
int i=0;
switch(figure[target[0]][target[1]]){
case PawnWhite:
if (figure[target[0]][7] != PawnWhite){ // Генерация "стандартного" хода вверх
motion[i].xy = convertXYcoordinatesToBits(target[0], target[1]+1);
motion[i].t = 1;
i++;
if (target[1] == 1){ // Обработка первого хода
motion[i].xy = convertXYcoordinatesToBits(target[0], target[1]+2);
motion[i].t = 1;
i++;
}
if (figure[target[0]+1][target[1]+1]){ // Съедаем фигуру справа сверху
motion[i].xy = convertXYcoordinatesToBits(target[0]+1, target[1]+1);
motion[i].t = 2;
i++;
}
if (figure[target[0]-1][target[1]+1]){ // Съедаем фигуру слева сверху
motion[i].xy = convertXYcoordinatesToBits(target[0]-1, target[1]+1);
motion[i].t = 2;
i++;
}
}
break;
case PawnBlack:
if (figure[target[0]][7] != PawnBlack){// Генерация "стандартного" хода вниз
motion[i].xy = convertXYcoordinatesToBits(target[0], target[1]-1);
motion[i].t = 1;
i++;
if (target[1] == 6){ // Обработка первого хода
motion[i].xy = convertXYcoordinatesToBits(target[0], target[1]-2);
motion[i].t = 1;
i++;
}
if (figure[target[0]+1][target[1]-1]){ // Съедаем фигуру справа снизу
motion[i].xy = convertXYcoordinatesToBits(target[0]+1, target[1]-1);
motion[i].t = 2;
i++;
}
if (figure[target[0]-1][target[1]-1]){ // Съедаем фигуру слева снизу
motion[i].xy = convertXYcoordinatesToBits(target[0]-1, target[1]-1);
motion[i].t = 2;
i++;
}
break;
}
//switch(figure[8][8])
//{
// case:
//}
// figure[figureXPosition + x][figureYPosition + y] = figure[figureXPosition][figureYPosition];
// figure[figureXPosition][figureYPosition] = 0;
}
motion[i].t = 0;
}
/*
void PawnMove(unsigned char figure[8][8], enum PawnMoveOptions options, unsigned char figureXPosition, unsigned char figureYPosition)
{
switch (options)
{
case TwoUp:
if (firstMove == 1)
{
figureMove(figure, 0, 2, figureXPosition, figureYPosition);
}
firstMove = 0;
break;
case Up:
figureMove(figure, 0, 1, figureXPosition, figureYPosition);
break;
case DiagonalRight:
figureMove(figure, 1, 1, figureXPosition, figureYPosition);
break;
case DiagonalLeft:
figureMove(figure, -1, 1, figureXPosition, figureYPosition);
break;
}
}
*/

155
logic.h Normal file
View file

@ -0,0 +1,155 @@
#pragma once
#define uchar unsigned char
static void rewriting_an_array(uchar move_options[8], int n, ...){
va_list factor;
va_start(factor, n);
for(int i=0;i<n; i++){
move_options[i] = va_arg(factor, int);
}
va_arg(factor, int);
va_end(factor);
}
static uchar bit_4_conversion(uchar x, uchar y){
return (x << 4) + y;
}
static uchar enemy_detection(int x, int y,uchar figure[8][8], struct target_struct target, uchar* T){
return (figure[target.x + x][target.y + y] >= 7 && *T) || (figure[target.x + x][target.y + y] < 7 && !*T);
}
static uchar procedural_positions(int* x, int* y, uchar* n, uchar figure[8][8], struct motion_struct motion[36], struct target_struct target, uchar* T){
if (figure[target.x + *x][target.y + *y] != 0){
if (enemy_detection(*x, *y, figure, target, T)){
motion[*n].xy = bit_4_conversion(target.x + *x, target.y + *y);
motion[*n].t = 2;
*n+=1;
}
return 1;
}
else{
motion[*n].xy = bit_4_conversion(target.x + *x, target.y + *y);
motion[*n].t = 1;
*n+=1;
}
return 0;
}
static void movement_request(uchar move_options, int* x, int* y){
switch(move_options){
case 0: *x+=1; break;
case 1: *x-=1; break;
case 2: *y+=1; break;
case 3: *y-=1; break;
case 4: *x+=1; *y+=1; break;
case 5: *x+=1; *y-=1; break;
case 6: *x-=1; *y-=1; break;
case 7: *y+=1; *x-=1; break;
}
}
static void pawn(uchar figure[8][8], struct motion_struct motion[36], struct target_struct target, uchar* i, uchar* T){
if (figure[target.x][target.y + (-1 + *T * 2)] == 0){// Генерация "стандартного" хода вверх
motion[*i].xy = bit_4_conversion(target.x, target.y+(-1 + *T * 2));
motion[*i].t = 1;
*i+=1;
if ((target.y == (6 - *T * 5)) &&(figure[target.x][target.y + (-1 + *T * 2) * 2] == 0)){ // Обработка первого хода
motion[*i].xy = bit_4_conversion(target.x, target.y+(-1 + *T * 2) * 2);
motion[*i].t = 1;
*i+=1;
}
}
int x = 1,y = (-1 + *T * 2);
if (figure[target.x + x][target.y + y] != 0 && (enemy_detection(x, y, figure, target, T))){ // Съедаем фигуру справа сверху
motion[*i].xy = bit_4_conversion(target.x+1, target.y + (-1 + *T * 2));
motion[*i].t = 2;
*i+=1;
}
x = -1;
if (figure[target.x + x][target.y + y] != 0 && enemy_detection(x, y, figure, target, T)){ // Съедаем фигуру слева сверху
motion[*i].xy = bit_4_conversion(target.x-1, target.y+ (-1 + *T * 2));
motion[*i].t = 2;
*i+=1;
}
}
static void reshuffle(int* x, int* y, int* n){
*n = *x;
*x = *y;
*y = *n;
}
static void horse(uchar figure[8][8], struct motion_struct motion[36], struct target_struct target, uchar* i, uchar* T){
int n, x_shift=0, y_shift=0;
for(int G = 0, x = 0, y = -2;G!= 2;reshuffle(&x, &y, &n), G++){
for(int bf=0; bf != 2;y=-y, x=-x, bf++){
if(x == 2 || x == -2){
x_shift = 0; y_shift = 1;
}
else{
x_shift = 1; y_shift = 0;
}
for (int v=0; v!=2; v++, x_shift = -x_shift, y_shift = -y_shift){
if (target.x+x+x_shift >= 0 && target.x+x+x_shift <= 7 && target.y+y+y_shift >= 0 && target.y+y+y_shift <= 7){// Поле
if (figure[target.x+x+x_shift][target.y+y+y_shift] != 0){// Препятствие
if (enemy_detection(x+x_shift, y+y_shift, figure, target, T)){ // Враг
motion[*i].xy = bit_4_conversion(target.x+x+x_shift, target.y+y+y_shift);
motion[*i].t = 2;
*i+=1;
}
}
else{ // Перемещение
motion[*i].xy = bit_4_conversion(target.x+x+x_shift, target.y+y+y_shift);
motion[*i].t = 1;
*i+=1;
}
}
}
}
}
}
void selection_moves(uchar figure[8][8], struct motion_struct motion[36], struct target_struct target){
uchar n = 0;
if (figure[target.x][target.y] == 0){
motion[n].t = 0;
return;
}
uchar T;
if (figure[target.x][target.y] < 7) T = 1; // Wite
else T = 0;
uchar move_options[8], N;
if (figure[target.x][target.y] == 1 || figure[target.x][target.y] == 7){ // Пешка
pawn(figure, motion, target, &n, &T);
motion[n].t = 0;
return;
}
else if (figure[target.x][target.y] == 3 || figure[target.x][target.y] == 9){// Ферзь
horse(figure, motion, target, &n, &T);
motion[n].t = 0;
return;
}
else if (figure[target.x][target.y] == 2 || figure[target.x][target.y] == 8){// Ладья
N = 4;
rewriting_an_array(move_options, N, 0,1,2,3);
}
else if (figure[target.x][target.y] == 4 || figure[target.x][target.y] == 10){// Слон
N = 4;
rewriting_an_array(move_options, N, 4,5,6,7);
}
else if (figure[target.x][target.y] == 5 || figure[target.x][target.y] == 11){// Ферзь
N = 8;
rewriting_an_array(move_options, N, 0,1,2,3,4,5,6,7);
}
else{
motion[n].t = 0;
return;
}
int x, y;
for (int i = 0; i != N; i++){
for (x = 0, y = 0, movement_request(move_options[i], &x, &y);
(target.x + x != -1) && (target.y + y != -1) && (target.x + x != 8) && (target.y + y != 8);
movement_request(move_options[i], &x, &y) ){
if(procedural_positions(&x, &y, &n, figure, motion, target, &T)) break;
}
}
motion[n].t = 0;
}

454
main.c
View file

@ -1,8 +1,454 @@
#include <stdio.h>
#include <stdlib.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();
int main(int argc, char *argv[])
{
puts("Hello, World!");
return 0;
}

140
move.c Normal file
View file

@ -0,0 +1,140 @@
#include "move.h"
#include <stdio.h>
#include <stdlib.h>
#define PAWN 1
#define KNIGHT 2
#define BISHOP 3
#define ROOK 4
#define QUEEN 5
#define KING 6
struct Location PAWN_MOVES[] = {
{0,1},
{-1,1},
{1,1},
{0,2} };
struct Location KNIGHT_MOVES[] = {
{1,-2},
{1,2},
{2,-1},
{2,1},
{-1,-2},
{-1,2},
{-2,-1},
{-2,1} };
struct Location BISHOP_MOVES_MIN[] = {
{-1,-1},
{-1,1},
{1,-1},
{1,1} };
struct Location ROOK_MOVES_MIN[] = {
{0,-1},
{0,1},
{-1,0},
{1,0} };
struct Location QUEEN_MOVES_MIN[] = {
{-1,-1},
{-1,0},
{-1,1},
{0,-1},
{0,1},
{1,-1},
{1,0},
{1,1} };
struct Location KING_MOVES[] = {
{-1,-1},
{-1,0},
{-1,1},
{0,-1},
{0,1},
{1,-1},
{1,0},
{1,1} };
struct LocationNode {
struct Location location;
struct LocationNode* next;
};
struct LocationNode* newNode(struct Location location) {
struct LocationNode* locationNode = (struct LocationNode*) malloc(sizeof(struct LocationNode));
locationNode->location = location;
locationNode->next = NULL;
return locationNode;
};
void append(struct LocationNode** head_ref, struct Location location) {
struct LocationNode* new_node = newNode(location);
struct LocationNode* last = *head_ref;
if (*head_ref == NULL) {
*head_ref = new_node;
return;
}
while (last->next != NULL) {
last = last->next;
}
last->next = new_node;
};
// helper functions
unsigned long sizeofarr(struct Location* arr) {
ulong out = sizeof(BISHOP_MOVES_MIN)/sizeof(BISHOP_MOVES_MIN[0]);
return out;
};
unsigned long sizeofsll(struct LocationNode* head) {
/* Counts the number of nodes in a linked list */
ulong out = 0;
while(head != NULL){
++out;
head = head->next;
};
return out;
};
// converters
struct Location* ll_to_arr(struct LocationNode* head) {
ulong head_size = sizeofsll(head);
struct Location* out[head_size];
for(int i = 0; i < head_size; i++){
out[i] = &head->location;
head = head->next;
};
return *out;
};
struct LocationNode* expand_directions(struct Location* directions) {
struct LocationNode* head = NULL;
for( // expand to all borders
int direction_number = 0;
direction_number < sizeofarr(BISHOP_MOVES_MIN);
direction_number++){
struct Location direction = BISHOP_MOVES_MIN[direction_number];
for(int mult = 1; mult <= 8; mult++){
struct Location extension = {direction.x*mult, direction.y*mult};
append(&head, extension);
};
};
return head;
};
struct Location* getPossibleMoves(struct Location pieceLocation, unsigned int chessPiece) {
struct Location* possibleMoves;
struct LocationNode* head = NULL;
switch(chessPiece) {
{case PAWN:
possibleMoves = PAWN_MOVES;
break;
}{case KNIGHT:
possibleMoves = KNIGHT_MOVES;
break;
}{case BISHOP:
return ll_to_arr(expand_directions(BISHOP_MOVES_MIN));
}{case ROOK:
return ll_to_arr(expand_directions(ROOK_MOVES_MIN));
}{case QUEEN:
return ll_to_arr(expand_directions(QUEEN_MOVES_MIN));
}{case KING:
possibleMoves = KING_MOVES;
break;
}};
return possibleMoves;
};

11
move.h Normal file
View file

@ -0,0 +1,11 @@
#ifndef MOVE_H
#define MOVE_H
struct Location {
int x;
int y;
};
struct Location* getPossibleMoves(struct Location pieceLocation, unsigned int chessPiece);
#endif // MOVE_H

7762
stb_image.h Normal file

File diff suppressed because it is too large Load diff