Push FIX pour DrawHUD. Toggle (HUD - h) (Crosshair - c) (Infos - i) avec keypress

This commit is contained in:
Mathieu Duval 2023-09-15 20:28:20 -04:00
parent f8ad857666
commit 8faa67596e
3 changed files with 105 additions and 116 deletions

View File

@ -25,20 +25,21 @@ void Engine::Init() {
//glEnable(GL_DEPTH_TEST); //glEnable(GL_DEPTH_TEST);
//glEnable(GL_STENCIL_TEST); //glEnable(GL_STENCIL_TEST);
//glEnable(GL_POINT_SMOOTH); //glEnable(GL_POINT_SMOOTH);
glEnable(GL_CULL_FACE);
//glEnable(GL_BLEND); //glEnable(GL_BLEND);
glEnable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glLoadIdentity(); glLoadIdentity();
//glClearColor(0.f, 0.f, 0.f, 1.f);
gluPerspective(45.0f, (float)Width() / (float)Height(), 0.1f, VIEW_DISTANCE); gluPerspective(45.0f, (float)Width() / (float)Height(), 0.1f, VIEW_DISTANCE);
//glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glShadeModel(GL_SMOOTH);
//glShadeModel(GL_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBlendEquation(GL_FUNC_SUBTRACT);
//glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
//glDisable(GL_BLEND);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glBlendEquation(GL_FUNC_SUBTRACT);
//
// Objet de skybox avec sa propre texture et son propre shader! // Objet de skybox avec sa propre texture et son propre shader!
m_skybox.Init(0.2f); m_skybox.Init(0.2f);
@ -97,49 +98,8 @@ void Engine::LoadResource() {
void Engine::UnloadResource() {} void Engine::UnloadResource() {}
void Engine::DrawHud() { void Engine::DisplayCrosshair() {
glDisable(GL_BLEND); m_textureCrosshair.Bind();
glDisable(GL_STENCIL_TEST);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, Width(), 0, Height(), -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
float fBarWidth = Width() / 6;
float fBarHeight = Height() / 25;
float fPosX = Width() / 25;
float fPosY = Height() - (Height() - (fPosX + fBarHeight));
float playerHp = m_player.GetHP();
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glBlendEquation(GL_FUNC_ADD);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glVertex2f(fPosX, fPosY);
glVertex2f(fPosX + fBarWidth, fPosY);
glVertex2f(fPosX + fBarWidth, fPosY - fBarHeight);
glVertex2f(fPosX, fPosY - fBarHeight);
glEnd();
glColor3f(0.0f, 1.0f, 0.0f);
glBegin(GL_QUADS);
glVertex2f(fPosX, fPosY);
glVertex2f(fPosX + fBarWidth * playerHp, fPosY);
glVertex2f(fPosX + fBarWidth * playerHp, fPosY - fBarHeight);
glVertex2f(fPosX, fPosY - fBarHeight);
glEnd();
glEnable(GL_BLEND);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
/*m_textureCrosshair.Bind();
static const int crossSize = 32; static const int crossSize = 32;
glLoadIdentity(); glLoadIdentity();
glTranslated(Width() / 2 - crossSize / 2, Height() / 2 - crossSize / 2, 0); glTranslated(Width() / 2 - crossSize / 2, Height() / 2 - crossSize / 2, 0);
@ -152,33 +112,83 @@ void Engine::DrawHud() {
glVertex2i(crossSize, crossSize); glVertex2i(crossSize, crossSize);
glTexCoord2f(0, 1); glTexCoord2f(0, 1);
glVertex2i(0, crossSize); glVertex2i(0, crossSize);
glEnd();*/ glEnd();
glEnable(GL_CULL_FACE);
m_textureFont.Bind();
std::ostringstream ss;
ss << m_player.GetUsername();
float scale = 1.5f;
PrintText(fPosX, fPosY + fBarHeight, scale, ss.str());
glBlendFunc(GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR);
glBlendEquation(GL_FUNC_SUBTRACT);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
} }
void Engine::DrawData(float elapsedTime, BlockType bloc) { void Engine::DisplayHud() {
glBindTexture(GL_TEXTURE_2D, 0);
glLoadIdentity();
glDisable(GL_BLEND);
float fBarWidth = Width() / 6;
float fBarHeight = Height() / 25;
float fPosX = Width() / 20;
float fPosY = Height() - (Height() - (fBarHeight + fBarHeight + fBarHeight + fBarHeight));
float playerHp = m_player.GetHP();
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glVertex2f(fPosX, fPosY - fBarHeight); // Bottom-left
glVertex2f(fPosX + fBarWidth, fPosY - fBarHeight); // Bottom-right
glVertex2f(fPosX + fBarWidth, fPosY); // Top-right
glVertex2f(fPosX, fPosY); // Top-left
glEnd();
glColor3f(0.0f, 1.0f, 0.0f);
glBegin(GL_QUADS);
glVertex2f(fPosX, fPosY - fBarHeight); // Bottom-left
glVertex2f(fPosX + fBarWidth * playerHp, fPosY - fBarHeight); // Bottom-right
glVertex2f(fPosX + fBarWidth * playerHp, fPosY); // Top-right
glVertex2f(fPosX, fPosY); // Top-left
glEnd();
glEnable(GL_BLEND);
m_textureFont.Bind();
glColor3f(1.0f, 1.0f, 1.0f);
std::ostringstream ss;
float scale = 1.5f;
unsigned int x = Width() / 20;
unsigned int y = Height() - (Height() - 85);
ss << m_player.GetUsername();
PrintText(x, y, scale, ss.str());
}
void Engine::DisplayInfo(float elapsedTime, BlockType bloc) {
// Bind de la texture pour le font
m_textureFont.Bind();
std::ostringstream ss;
float scale = 1.0f;
unsigned int x = Width() / 25;
ss << " Fps : " << GetFps(elapsedTime);
PrintText(x, Height() - 25, scale, ss.str());
ss.str("");
ss << " Rendered Chunks : " << m_renderCount;
PrintText(x, Height() - 35, scale, ss.str());
ss.str("");
ss << " To-Be-Deleted Chunks : " << m_world.GettbDeleted();
PrintText(x, Height() - 45, scale, ss.str());
ss.str("");
ss << " Velocity : " << m_player.GetVelocity(); // IMPORTANT : on utilise l operateur << pour afficher la position
PrintText(x, 10, scale, ss.str());
ss.str("");
ss << " Direction : " << m_player.GetDirection();
PrintText(x, 20, scale, ss.str());
ss.str("");
ss << " Position : " << m_player.GetPosition();
PrintText(x, 30, scale, ss.str());
ss.str("");
ss << " Block : ";
if (bloc == BTYPE_LAST)
ss << "Weapon.";
else ss << (int)bloc;
PrintText(x, 40, scale, ss.str());
}
void Engine::DrawHud(float elapsedTime, BlockType bloc) {
// Setter le blend function , tout ce qui sera noir sera transparent // Setter le blend function , tout ce qui sera noir sera transparent
glDisable(GL_STENCIL_TEST); glDisable(GL_STENCIL_TEST);
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE); glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glBlendEquation(GL_FUNC_ADD); glBlendEquation(GL_FUNC_ADD);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glPushMatrix(); glPushMatrix();
glLoadIdentity(); glLoadIdentity();
@ -186,41 +196,22 @@ void Engine::DrawData(float elapsedTime, BlockType bloc) {
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glPushMatrix(); glPushMatrix();
// Bind de la texture pour le font if (m_displayHud) {
glColor4f(1.f, 1.f, 1.f, 1.f); DisplayHud();
m_textureFont.Bind(); }
std::ostringstream ss;
float scale = 1.0f; if (m_displayInfo) {
ss << " Fps : " << GetFps(elapsedTime); DisplayInfo(elapsedTime, bloc);
PrintText(10, Height() - 25, scale, ss.str()); }
ss.str("");
ss << " Rendered Chunks : " << m_renderCount; if (m_displayCrosshair) {
PrintText(10, Height() - 35, scale, ss.str()); DisplayCrosshair();
ss.str(""); }
ss << " To-Be-Deleted Chunks : " << m_world.GettbDeleted();
PrintText(10, Height() - 45, scale, ss.str());
ss.str("");
ss << " Velocity : " << m_player.GetVelocity(); // IMPORTANT : on utilise l operateur << pour afficher la position
PrintText(10, 10, scale, ss.str());
ss.str("");
ss << " Direction : " << m_player.GetDirection();
PrintText(10, 20, scale, ss.str());
ss.str("");
ss << " Position : " << m_player.GetPosition();
PrintText(10, 30, scale, ss.str());
ss.str("");
ss << " Block : ";
if (bloc == BTYPE_LAST)
ss << "Weapon.";
else ss << (int)bloc;
PrintText(Width() / 25, 40, scale, ss.str());
glBlendFunc(GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR); glBlendFunc(GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR);
glBlendEquation(GL_FUNC_SUBTRACT); glBlendEquation(GL_FUNC_SUBTRACT);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST); glEnable(GL_STENCIL_TEST);
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glPopMatrix(); glPopMatrix();
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
@ -328,17 +319,7 @@ void Engine::Render(float elapsedTime) {
if (m_isSkybox) m_skybox.Render(skybox); if (m_isSkybox) m_skybox.Render(skybox);
if (m_wireframe) DrawHud(elapsedTime, bloc);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
if (m_displayHud)
DrawHud();
if (m_displayData)
DrawData(elapsedTime, bloc);
if (m_wireframe)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
static bool fell = false; static bool fell = false;
if (m_player.GetPosition().y < 1.7f && !fell) { if (m_player.GetPosition().y < 1.7f && !fell) {
@ -384,6 +365,8 @@ void Engine::KeyPressEvent(unsigned char key) {
case 94: // F10 - Plein écran case 94: // F10 - Plein écran
SetFullscreen(!IsFullscreen()); SetFullscreen(!IsFullscreen());
break; break;
case 2: // C - Ignorer
break;
case 5: // F - Ignorer case 5: // F - Ignorer
break; break;
case 12: // M - Ignorer case 12: // M - Ignorer
@ -408,6 +391,10 @@ void Engine::KeyReleaseEvent(unsigned char key) {
case 0: // A - Stop gauche case 0: // A - Stop gauche
m_keyA = false; m_keyA = false;
break; break;
case 2: // C - Toggle crosshair
m_displayCrosshair = !m_displayCrosshair;
std::cout << "DISPLAY CROSSHAIR " << (m_displayCrosshair ? "enabled" : "disabled") << std::endl;
break;
case 3: // D - Stop droite case 3: // D - Stop droite
m_keyD = false; m_keyD = false;
break; break;
@ -419,8 +406,8 @@ void Engine::KeyReleaseEvent(unsigned char key) {
std::cout << "DISPLAY HUD " << (m_displayHud ? "enabled" : "disabled") << std::endl; std::cout << "DISPLAY HUD " << (m_displayHud ? "enabled" : "disabled") << std::endl;
break; break;
case 8: // I - Toggle render data case 8: // I - Toggle render data
m_displayData = !m_displayData; m_displayInfo = !m_displayInfo;
std::cout << "DISPLAY DATA " << (m_displayData ? "enabled" : "disabled") << std::endl; std::cout << "DISPLAY INFO " << (m_displayInfo ? "enabled" : "disabled") << std::endl;
break; break;
case 12: // M - Toggle music case 12: // M - Toggle music
m_audio.ToggleMusicState(); m_audio.ToggleMusicState();

View File

@ -29,12 +29,13 @@ public:
virtual void MouseMoveEvent(int x, int y); virtual void MouseMoveEvent(int x, int y);
virtual void MousePressEvent(const MOUSE_BUTTON &button, int x, int y); virtual void MousePressEvent(const MOUSE_BUTTON &button, int x, int y);
virtual void MouseReleaseEvent(const MOUSE_BUTTON &button, int x, int y); virtual void MouseReleaseEvent(const MOUSE_BUTTON &button, int x, int y);
const char* GetGLErrorString(GLenum error);
private: private:
bool LoadTexture(Texture& texture, const std::string& filename, bool stopOnError = true); bool LoadTexture(Texture& texture, const std::string& filename, bool stopOnError = true);
void DrawHud(); void DisplayCrosshair();
void DrawData(float elapsedTime, BlockType bloc); void DisplayHud();
void DisplayInfo(float elapsedTime, BlockType bloc);
void DrawHud(float elapsedTime, BlockType bloc);
void PrintText(unsigned int x, unsigned int y, float scale, const std::string& t); void PrintText(unsigned int x, unsigned int y, float scale, const std::string& t);
int GetFps(float elapsedTime) const; int GetFps(float elapsedTime) const;
@ -76,8 +77,9 @@ private:
bool m_block = false; bool m_block = false;
bool m_flash = true; bool m_flash = true;
bool m_displayData = false; bool m_displayCrosshair = true;
bool m_displayHud = true; bool m_displayHud = true;
bool m_displayInfo = false;
}; };

View File

@ -5,7 +5,7 @@ Player::Player(const Vector3f& position, float rotX, float rotY) : m_position(po
m_velocity = Vector3f(0, 0, 0); m_velocity = Vector3f(0, 0, 0);
m_airborne = true; m_airborne = true;
m_hp = 0.75f; m_hp = 0.75f;
m_username = "John Doe"; m_username = "Zelda Bee-Bop";
} }
void Player::TurnLeftRight(float value) { void Player::TurnLeftRight(float value) {