#pragma once
// GLEW
#include<iostream>
#define GLEW_STATIC
#include <GL/glew.h>
using namespace std;
// GLFW
#include <GLFW/glfw3.h>
class Button
{
public:
float x, y, w, h;
Button(float bx, float by, float bw, float bh)
{
x = bx, y = by, w = bw, h = bh;
}
void drawButton()
{
glBegin(GL_QUADS);
glVertex2f(x, y);
glVertex2f(x, y+h);
glVertex2f(x+w, y+h);
glVertex2f(x+w, y);
glEnd();
}
bool isBtnClick(float posx, float posy)
{
if (posx > x && posx < x + w)
{
if (posy > y && posy < y + h)
{
//cout << posx << posy;
return true;
}
}
return false;
}
};
main.cpp
#include "Button.h"
Button startbtn(0.0f, 0.0f, 0.4f, 0.2f);
Button settingBtn(0.2f, 0.2f, 0.4, 0.2f);
static int levelNo = 0;
while (!glfwWindowShouldClose(window))
{
Initialize();
float currentFrame = glfwGetTime();
float deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
glfwGetCursorPos(window, &xpos, &ypos);
mouseClick(window);
processInput(window, deltaTime);
sceneManagement();
glfwSwapBuffers(window);
glfwPollEvents();
}
void mouseClick(GLFWwindow* window)
{
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
{
float newXpos = xpos / WIDTH * 2 - 1;
float newYpos = (ypos / HEIGHT * 2 - 1) * -1;
glColor3f(1, 0.3, 1);
if (startbtn.isBtnClick(newXpos, newYpos))
{
cout<<"startButton clicked";
}
}
}
void sceneManagement()
{
switch (levelNo)
{
case 0:
startPage();
break;
case 1:
gameplay();
break;
default:
startPage();
}
}
void startPage()
{
startbtn.drawButton();
settingBtn.drawButton();
}
void mouseClick2(GLFWwindow* window)
{
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
{
float newXpos = xpos / WIDTH * 2 - 1;
float newYpos = (ypos / HEIGHT * 2 - 1) * -1;
/*cout << "x= " << newXpos << " /y= " << newYpos<<endl;
cout << "x= " << startbtn.x << " /y= " << startbtn.x + startbtn.w << endl;*/
if (newXpos > startbtn.x && newXpos < startbtn.x+ startbtn.w)
if (newYpos > startbtn.y && newYpos < startbtn.y + startbtn.h)
{
cout << "start button Clicked" << endl;
}
if (newXpos > settingBtn.x && newXpos < settingBtn.x + settingBtn.w)
if (newYpos > settingBtn.y && newYpos < settingBtn.y + settingBtn.h)
{
cout << "settings button Clicked" << endl;
}
}
}
Button.h
#pragma once
// GLEW
#include<iostream>
#define GLEW_STATIC
#include <GL/glew.h>
using namespace std;
// GLFW
#include <GLFW/glfw3.h>
class Button
{
public:
float x, y, w, h;
Button(float bx, float by, float bw, float bh)
{
x = bx, y = by, w = bw, h = bh;
}
void drawButton()
{
glBegin(GL_QUADS);
glVertex2f(x, y);
glVertex2f(x, y+h);
glVertex2f(x+w, y+h);
glVertex2f(x+w, y);
glEnd();
}
};