OpenGL超级宝典(第7版)笔记15 前三章实例 下个五子棋全代码 (附)

本文是OpenGL超级宝典第七版的读书笔记,重点介绍了前三章的实例应用,详细讲解了如何使用OpenGL实现五子棋的全代码,涵盖了必要的C++编程技巧。
摘要由CSDN通过智能技术生成

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 //!BASIC_RA

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;//黑为1白为2
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 drawString(const char* str) //屏幕显示字体
//{
   
//	static int isFirstCall = 1;
//	static GLuint lists;
// 
//	if (isFirstCall) {
   
//		isFirstCall = 0;
//		// 申请MAX_CHAR个连续的显示列表编号
//		lists = glGenLists(200);
//		// 把每个字符的绘制命令都装到对应的显示列表中
//		wglUseFontBitmaps(wglGetCurrentDC(), 0, 20, lists);
//	}
//	// 调用每个字符对应的显示列表,绘制每个字符
//	for (; *str != '\0'; ++str) {
   
//		glCallList(lists + *str);
//	}
//}
//void selectFont(int size, int charset, const char* face) {
   
//	HFONT hFont = CreateFontA(size, 0, 0, 0, FW_MEDIUM, 0, 0, 0,
//		charset, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
//		DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, face);
//	HFONT hOldFont = (HFONT)SelectObject(wglGetCurrentDC(), hFont);
//	DeleteObject(hOldFont);
//}
我想试着在屏幕上输出字,但是没有成功~~~


void mouse_callback(GLFWwindow* window, double xpos, double ypos) {
   
    //    if (lastx == 0.0f) {
   
    //        lastx = xpos;
    //    }
    //    if (lasty == 0.0f) {
   
    //        lasty = ypos;
    //    }
    //	if((angy+ypos-lasty)*rotasen>3.141f/2.0f){
   
    //		angy=3.141f/(rotasen*2.0f);
    //	}else if((angy+ypos-lasty)*rotasen<-3.141f/2.0f){
   
    //		angy=-3.141f/(rotasen*2.0f);
    //	}else{
   
    //		angy+=ypos-lasty;
    //	}
    //	angx+=xpos-lastx;
    	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;//左键为1右键为2中键为3 
	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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值