OpenGL超级宝典(第7版)笔记15 前三章实例 下个五子棋全代码 (附)
#pragma once
#ifndef BASIC_RA
#define BASIC_RA
#include<iostream>
#include<Windows.h>
#define GLEW_STATIC
#include<glew.h>
#include<glfw3.h>
#include<math.h>
#endif
void startup();
void render(double currentTime);
void shutdown();
void compile_shader(void);
void delete_shader(void);
GLuint linkprogram1(void);
GLuint linkprogram2(void);
GLuint vs[3]={
0};
GLuint tcs[3]={
0};
GLuint tes[3]={
0};
GLuint gs[3]={
0};
GLuint fs[3]={
0};
GLuint rendering_program1;
GLuint rendering_program2;
GLuint vertex_array_object;
int windowhigh = 1000;
int windowwide = 1000;
GLdouble lastx = 0.0f;
GLdouble lasty = 0.0f;
GLdouble kickx = 0.0f;
GLdouble kicky = 0.0f;
GLint arrayx = 0;
GLint arrayy = 0;
GLint nowkind = 1;
int iswin = 0;
GLint judgewin(GLint x, GLint y);
struct chess{
GLchar chesskind=0;
};
chess chessboard[17][17];
GLint chess_number=0;
GLchar chess_draw_list[17*17][3]={
0};
void mouse_callback(GLFWwindow* window, double xpos, double ypos) {
lastx=xpos;
lasty=ypos;
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
int whichkey=0;
if (action == GLFW_PRESS) switch (button)
{
case GLFW_MOUSE_BUTTON_LEFT:
kickx = lastx;
kicky = lasty;
whichkey=1;
std::cout << "left down" << std::endl;
std::cout << lastx << " " << lasty << std::endl;
break;
case GLFW_MOUSE_BUTTON_MIDDLE:
kickx = lastx;
kicky = lasty;
whichkey=3;
std::cout << "mid down" << std::endl;
std::cout << lastx << " " << lasty << std::endl;
break;
case GLFW_MOUSE_BUTTON_RIGHT:
kickx = lastx;
kicky = lasty;
whichkey=2;
std::cout << "right down" << std::endl;
std::cout << lastx << " " << lasty << std::endl;
if (nowkind == 1)
nowkind = 2;
else
nowkind = 1;
break;
default:
return;
}
switch(whichkey){
case 1:
if (kickx > 150 && kicky > 150 && kickx < 850 && kicky < 850) {
arrayx = (kickx - 170) / 38.75f + 1.0f;
arrayy = (kicky - 170) / 38.75f + 1.0f;
std::cout << "arrayx=" << arrayx << " " << "arrayy=" << arrayy << std::endl;
if (chessboard[arrayx][arrayy].chesskind == 0) {
chessboard[arrayx][arrayy].chesskind = nowkind;
chess_draw_list[chess_number][0]=nowkind;
chess_draw_list[chess_number][1]=arrayx;
chess_draw_list[chess_number][2]=arrayy;
chess_number++;
if (judgewin(arrayx, arrayy) == 1) {
std::cout << "nowkind " << nowkind << " is win!!" << std::endl;
iswin = nowkind;
}
if (nowkind == 1)
nowkind = 2;
else
nowkind = 1;
}
}
break;
case 3:
if (chess_number > 0) {
chess_number--;
chessboard[chess_draw_list[chess_number][1]][chess_draw_list[chess_number][2