好久没写了,本来想着斗地主写完了在来博客,

但是写着写着不想写了,因为设计的不好,写到后面代码沉长逻辑复杂。不想写了,等以后在写。

#define _CRT_SECURE_NO_WARNINGS
#include "poker.h"
#include <glad/glad.h>
#include <glfw3.h>
#include <shader.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <string>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <time.h>
#include "user.h"
using namespace std;

const char* str[54] = {
	"Poker/1.png","Poker/2.png","Poker/3.png",
	"Poker/4.png","Poker/5.png","Poker/6.png",
	"Poker/7.png","Poker/8.png","Poker/9.png",
	"Poker/10.png","Poker/11.png","Poker/12.png","Poker/13.png",
	"Poker/14.png","Poker/15.png","Poker/16.png","Poker/17.png",
	"Poker/18.png","Poker/19.png","Poker/20.png","Poker/21.png",
	"Poker/22.png","Poker/23.png","Poker/24.png","Poker/25.png",
	"Poker/26.png","Poker/27.png","Poker/28.png","Poker/29.png",
	"Poker/30.png","Poker/31.png","Poker/32.png","Poker/33.png",
	"Poker/34.png","Poker/35.png","Poker/36.png","Poker/37.png",
	"Poker/38.png","Poker/39.png","Poker/40.png","Poker/41.png",
	"Poker/42.png","Poker/43.png","Poker/44.png","Poker/45.png",
	"Poker/46.png","Poker/47.png","Poker/48.png","Poker/49.png",
	"Poker/50.png","Poker/51.png","Poker/52.png","Poker/53.png",
	"Poker/54.png"
};
const char* contralPath[2] = { "interface/output.png","interface/don'tOut.png" };
float contralV[] = {// positions          // colors           // texture coords
	 0.3f,  -0.3f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f, // top right
	 0.3f, -0.4f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f, // bottom right
	0.2f, -0.4f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f, // bottom left
	0.2f,  -0.3f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f,  // top left 
};
float vertices[] = {
	// positions          // colors           // texture coords
	 -0.6f,  -0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f, // top right
	 -0.6f, -1.0f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f, // bottom right
	-0.8f, -1.0f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f, // bottom left
	-0.8f,  -0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f,  // top left 
};
float outputTry[] = {
	// positions          // colors           // texture coords
	 -0.6f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1-1.0f, // top right
	 -0.6f, 1.0f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 1-0.0f, // bottom right
	-0.8f, 1.0f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 1-0.0f, // bottom left
	-0.8f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1-1.0f,  // top left 
};    
#define MAX_T 54
#define CONTRAL 2
glm::mat4 model = glm::mat4(1.0f);
Shader ourShader;
int SCR_WIDTH = 1366, SCR_HEIGHT = 768;
float pokerV[MAX_T][32];
Poker* poker[MAX_T];
Poker* contral[CONTRAL];
vector <Poker*> vP;
int cot = 0;
int signal = 0;
vector <Poker *> outPutPoker;
User user1;
User user2;
void initArrayVertic() {
	float step = 0.0f;
	for (size_t i = 0; i < MAX_T; i++)
	{
		for (size_t j = 0; j < 32; j++)
		{
			pokerV[i][j] = vertices[j];

			if (j % 8 == 0) {
				pokerV[i][j] += step;
			}
			printf("%.3f  ", pokerV[i][j]);
			if (((j + 1) % 8 == 0))
			{
				printf("\n");
			}
		}
		step += 0.05f;
	}
}
bool comp(const Poker* a, const Poker* b) {
	return a->faceValue < b->faceValue;
}

vector <int> v1;
void getPoker(int max,vector<Poker *>& vp) {
	srand((unsigned)time(NULL));
	int r;
	vector <int> s;
	for (size_t i = 0; i < 54; i++)
	{
		v1.push_back(i);
	}
	while (s.size() < max)
	{
		r = rand() % 54;
		if (v1[r] != -1)
		{
			s.push_back(r);
			v1[r] = -1;
		}
	}
	for (size_t i = 0; i < s.size(); i++)
	{
		vp.push_back(poker[s[i]]);
	}
}
void myRand() {
	srand((unsigned)time(NULL));
	vector <int> s;
	int index = 0;
	getPoker(17, vP);
	getPoker(17, user1.poker);
	getPoker(20, user2.poker);
	for (size_t i = 0; i < 17;i++)
	{
		cout << vP[i]->faceValue << "	";
		cout << user1.poker[i]->faceValue << "	";
		cout << user2.poker[i]->faceValue << endl;
	}
}
void randPerm(int num) {
	int fV = 0;
	for (int i = 0; i < num; i++) {
		poker[i] = new Poker(str[i], pokerV[i]);
		poker[i]->faceValue = fV % 13 + 1;
		fV++;
		if (i==52)
		{
			poker[i]->faceValue = 53;
		}
		else if(i == 53)
		{
			poker[i]->faceValue = 54;
		}
	}
	myRand();
	sort(vP.begin(), vP.end(), comp);
	int my = 137, maxy = 170;
	for (size_t i = 0; i < 17; i++)
	{
		vP[i]->setVertices(pokerV[i]);
		vP[i]->minY = my; vP[i]->maxY = maxy;
		my += 33, maxy += 33;
		//cout << vP[i]->faceValue << " ";
	}
	cout << endl;
}

void cbfunc(GLFWwindow* window, int x, int y, int z);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void compactPoker();
void threadFunc();
GLFWwindow* window;

int main(int argc, char *argv[]) {
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
	if (window == NULL)
	{
		std::cout << "Failed to create GLFW window" << std::endl;
		glfwTerminate();
		return -1;
	}

	glfwMakeContextCurrent(window);
	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
	glfwSetCursorPosCallback(window, mouse_callback);
	glfwSetMouseButtonCallback(window, cbfunc);
	
	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
	{
		std::cout << "Failed to initialize GLAD" << std::endl;
		return -1;
	}

	ourShader = Shader("4.1.texture.vs", "4.1.texture.fs");
	initArrayVertic();

	stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded texture's on the y-axis.
		//洗牌
	randPerm(54);

	for (size_t i = 0; i < CONTRAL; i++)
	{
		contral[i] = new Poker(contralPath[i], &contralV[i]);
	}
	while (!glfwWindowShouldClose(window))
	{
		glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);
		ourShader.use();
		for (int i = 0; i < vP.size(); i++) {
			glBindVertexArray(vP[i]->VAO);
			glBindTexture(GL_TEXTURE_2D, vP[i]->texture);
			glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
		}
		for (int i = 0; i < outPutPoker.size(); i++) {
			glBindVertexArray(outPutPoker[i]->VAO);
			glBindBuffer(GL_ARRAY_BUFFER, outPutPoker[i]->VBO);
			glBufferData(GL_ARRAY_BUFFER, sizeof(outPutPoker[i]->vertices), outPutPoker[i]->vertices, GL_STATIC_DRAW);
			glBindTexture(GL_TEXTURE_2D, outPutPoker[i]->texture);
			glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
		}
		for (int i = 0; i < CONTRAL; i++) {
			glBindVertexArray((*contral[i]).VAO);
			glBindTexture(GL_TEXTURE_2D, (*contral[i]).texture);
			glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
		}
	
		glfwSwapBuffers(window);
		glfwPollEvents();
	}
	return 0;
}

void threadFunc() {
		if (signal == 1)
		{
			
		}
		else if(signal == 0)
		{
			outPutPoker.clear();
		}
}

void start() {
	
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
	glViewport(0, 0, width, height);
}

double mouse_x, mouse_y;

void mouse_callback(GLFWwindow* window, double xpos, double ypos) {
	printf("x = %fd , y = %fd\n", xpos, ypos);
	mouse_x = xpos;
	mouse_y = ypos;
}
void statePoker(Poker* p) {
	if (mouse_x > p->minY && mouse_x < p->maxY) {
		if (mouse_y < 570) {
			return;
		}
		if (0 == p->state) {
			glBindVertexArray(p->VAO);
			glBindBuffer(GL_ARRAY_BUFFER, p->VBO);
			p->movePokerYAdd();
			glBufferData(GL_ARRAY_BUFFER, sizeof(p->vertices), p->vertices, GL_STATIC_DRAW);
			p->state = 1;
		}
		else if(1 == p->state)
		{
			glBindVertexArray(p->VAO);
			glBindBuffer(GL_ARRAY_BUFFER, p->VBO);
			p->movePokerYRemoue();
			glBufferData(GL_ARRAY_BUFFER, sizeof(p->vertices), p->vertices, GL_STATIC_DRAW);
			p->state = 0;
		}
	}
}

void compactPoker() {
	for (size_t i = 0; i < vP.size(); i++)
	{
		int j = 0;
		if (vP[i]->state == -1)
		{
			for (size_t k = 0; k < i; k++)
			{
				vP[k]->movePokerXAdd(0.05f);
				glBindBuffer(GL_ARRAY_BUFFER, vP[k]->VBO);
				glBufferData(GL_ARRAY_BUFFER, sizeof(vP[k]->vertices), vP[k]->vertices, GL_STATIC_DRAW);
				vP[k]->minY += 33; vP[k]->maxY += 33;
			}
			Poker* pk = new Poker(*vP[i]);
			outPutPoker.push_back(pk);
			vP[i]->setVertices(0);
			vP[i]->state = -2;
		}
	}
}
void checkPokerState() {
	float s = 0.1f;
	for (size_t j = 0; j < vP.size(); j++)
	{
		if (vP[j]->state == 1) {
			vP[j]->setVertices(outputTry);
			vP[j]->movePokerXAdd(s);
			glBindVertexArray(vP[j]->VAO);
			glBindBuffer(GL_ARRAY_BUFFER, vP[j]->VBO);
			glBufferData(GL_ARRAY_BUFFER, sizeof(vP[j]->vertices), vP[j]->vertices, GL_STATIC_DRAW);
			vP[j]->state = -1;
			vP[j]->minY = 0; vP[j]->minY = 0;
			s += 0.1f;
		}
	}
}
void outputFunc() {
	if (mouse_x > 820 && mouse_x < 885)
	{
		if (mouse_y > 500 && mouse_y < 535)
		{
			//出牌在下面添加代码
			checkPokerState();
			//removePoker();
			outPutPoker.clear();
			compactPoker();
		}
	}
}
void cbfunc(GLFWwindow* window, int rl, int tf, int z) {
	//在这里添加鼠标响应消息
	if (GLFW_PRESS == glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT)) {
		outputFunc();
		for (size_t i = 0; i < vP.size(); i++)
		{
			statePoker(vP[i]);
		}
	}
}
unsigned long type;
void outPutPokerType() {
	if (outPutPoker.size()==1)
	{

	}
	else if(outPutPoker.size()==2)
	{
		if (outPutPoker[0]->faceValue == outPutPoker[1]->faceValue)
		{

		}
	}
	else if (outPutPoker.size()==3)
	{
		if (outPutPoker[0]->faceValue == outPutPoker[1]->faceValue == outPutPoker[1]->faceValue)
		{

		}
	}
	else if (outPutPoker.size()==4)
	{

	}
	else if (outPutPoker.size()==5)
	{

	}
}

运行结果是这样的。

基于opengl的“斗地主”

 基于opengl的“斗地主”

 还有两个头文件我放在网盘上了,方便一点,想一起开发的来把。。

链接:https://pan.baidu.com/s/1zGOwlr5SJUHRa1H_E4j4PA 
提取码:qnh7

相关文章:

  • 2022-12-23
  • 2021-12-25
  • 2021-12-10
  • 2021-11-30
  • 2021-05-10
  • 2021-08-18
  • 2021-09-02
猜你喜欢
  • 2021-10-17
  • 2022-12-23
  • 2021-11-19
  • 2021-05-26
  • 2021-12-25
  • 2021-08-04
相关资源
相似解决方案