Push branche HUD. Propriétes et getters pour Player (HP, username). Changements minimes à Audio (enlever d'un merge). Modification et ajouts de DrawHud et DrawData

This commit is contained in:
Mathieu Duval 2023-09-15 11:24:43 -04:00
parent 8e40f17b75
commit f8ad857666
9 changed files with 216 additions and 133 deletions

View File

@ -14,7 +14,7 @@ Audio::Audio(const char * music) {
m_engine->setRolloffFactor(1); m_engine->setRolloffFactor(1);
m_engine->setDefault3DSoundMinDistance(1); m_engine->setDefault3DSoundMinDistance(1);
m_engine->setDefault3DSoundMaxDistance(1000); m_engine->setDefault3DSoundMaxDistance(1000);
m_music = m_engine->play2D(music, true, true, true, irrklang::ESM_STREAMING); m_music = m_engine->play2D(music, false, true, true, irrklang::ESM_STREAMING);
} }
Audio::~Audio() { Audio::~Audio() {
@ -40,9 +40,9 @@ void Audio::Render3DAudioObj(irrklang::ISound* sound, Vector3f& pos, Vector3f& v
sound->setVolume(volume); sound->setVolume(volume);
} }
void Audio::PlaySong(const char* music) { //void Audio::PlaySong(const char* music) {
m_music = m_engine->play2D(music, true, true, true, irrklang::ESM_STREAMING); // m_music = m_engine->play2D(music, false, false, false, irrklang::ESM_STREAMING);
} //}
void Audio::ToggleMusicState() { m_music->setIsPaused(!m_music->getIsPaused()); } void Audio::ToggleMusicState() { m_music->setIsPaused(!m_music->getIsPaused()); }

View File

@ -22,7 +22,7 @@ public:
void Render3DAudioObj(irrklang::ISound* sound, Vector3f& pos, Vector3f& vel, float volume); void Render3DAudioObj(irrklang::ISound* sound, Vector3f& pos, Vector3f& vel, float volume);
void PlaySong(const char* music); //void PlaySong(const char* music);
void ToggleMusicState(); void ToggleMusicState();

View File

@ -4,7 +4,7 @@
#include "transformation.h" #include "transformation.h"
#include "player.h" #include "player.h"
Engine::Engine() { } Engine::Engine() {}
Engine::~Engine() { Engine::~Engine() {
m_world.CleanUpWorld(m_renderCount, true); m_world.CleanUpWorld(m_renderCount, true);
@ -21,22 +21,22 @@ void Engine::Init() {
abort(); abort();
} }
glDisable(GL_FRAMEBUFFER_SRGB); //glDisable(GL_FRAMEBUFFER_SRGB);
glClearColor(0.f, 0.f, 0.f, 1.f); //glEnable(GL_DEPTH_TEST);
//glEnable(GL_STENCIL_TEST);
//glEnable(GL_POINT_SMOOTH);
glEnable(GL_CULL_FACE);
//glEnable(GL_BLEND);
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);
glEnable(GL_DEPTH_TEST); //glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_STENCIL_TEST); //glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glShadeModel(GL_SMOOTH);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR);
glBlendEquation(GL_FUNC_SUBTRACT); 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!
@ -57,7 +57,7 @@ void Engine::Init() {
HideCursor(); HideCursor();
} }
void Engine::DeInit() { } void Engine::DeInit() {}
void Engine::LoadResource() { void Engine::LoadResource() {
LoadTexture(m_skybox.GetTexture(), TEXTURE_PATH "skybox.png"); LoadTexture(m_skybox.GetTexture(), TEXTURE_PATH "skybox.png");
@ -97,47 +97,49 @@ void Engine::LoadResource() {
void Engine::UnloadResource() {} void Engine::UnloadResource() {}
void Engine::DrawHud(float elapsedTime, BlockType bloc) { void Engine::DrawHud() {
// Setter le blend function , tout ce qui sera noir sera transparent glDisable(GL_BLEND);
glDisable(GL_STENCIL_TEST); glDisable(GL_STENCIL_TEST);
glColor4f(1.f, 1.f, 1.f, 1.f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glBlendEquation(GL_FUNC_ADD);
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glPushMatrix(); glPushMatrix();
glLoadIdentity(); glLoadIdentity();
glOrtho(0, Width(), 0, Height(), -1, 1); glOrtho(0, Width(), 0, Height(), -1, 1);
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glPushMatrix(); glPushMatrix();
glLoadIdentity();
// Bind de la texture pour le font float fBarWidth = Width() / 6;
m_textureFont.Bind(); float fBarHeight = Height() / 25;
std::ostringstream ss; float fPosX = Width() / 25;
ss << " Fps : " << GetFps(elapsedTime); float fPosY = Height() - (Height() - (fPosX + fBarHeight));
PrintText(10, Height() - 25, ss.str());
ss.str(""); float playerHp = m_player.GetHP();
ss << " Rendered Chunks : " << m_renderCount;
PrintText(10, Height() - 35, ss.str()); glBlendFunc(GL_SRC_ALPHA, GL_ONE);
ss.str(""); glBlendEquation(GL_FUNC_ADD);
ss << " To-Be-Deleted Chunks : " << m_world.GettbDeleted(); glColor3f(1.0f, 1.0f, 1.0f);
PrintText(10, Height() - 45, ss.str()); glBegin(GL_QUADS);
ss.str(""); glVertex2f(fPosX, fPosY);
ss << " Velocity : " << m_player.GetVelocity(); // IMPORTANT : on utilise l operateur << pour afficher la position glVertex2f(fPosX + fBarWidth, fPosY);
PrintText(10, 10, ss.str()); glVertex2f(fPosX + fBarWidth, fPosY - fBarHeight);
ss.str(""); glVertex2f(fPosX, fPosY - fBarHeight);
ss << " Direction : " << m_player.GetDirection(); glEnd();
PrintText(10, 20, ss.str());
ss.str(""); glColor3f(0.0f, 1.0f, 0.0f);
ss << " Position : " << m_player.GetPosition(); glBegin(GL_QUADS);
PrintText(10, 30, ss.str()); glVertex2f(fPosX, fPosY);
ss.str(""); glVertex2f(fPosX + fBarWidth * playerHp, fPosY);
ss << " Block : "; glVertex2f(fPosX + fBarWidth * playerHp, fPosY - fBarHeight);
if (bloc == BTYPE_LAST) glVertex2f(fPosX, fPosY - fBarHeight);
ss << "Weapon."; glEnd();
else ss << (int)bloc;
PrintText(10, 40, ss.str());
m_textureCrosshair.Bind(); 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);
@ -150,18 +152,82 @@ void Engine::DrawHud(float elapsedTime, BlockType bloc) {
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); glBlendFunc(GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR);
glBlendEquation(GL_FUNC_SUBTRACT); glBlendEquation(GL_FUNC_SUBTRACT);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST); glEnable(GL_STENCIL_TEST);
}
void Engine::DrawData(float elapsedTime, BlockType bloc) {
// Setter le blend function , tout ce qui sera noir sera transparent
glDisable(GL_STENCIL_TEST);
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glBlendEquation(GL_FUNC_ADD);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, Width(), 0, Height(), -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
// Bind de la texture pour le font
glColor4f(1.f, 1.f, 1.f, 1.f);
m_textureFont.Bind();
std::ostringstream ss;
float scale = 1.0f;
ss << " Fps : " << GetFps(elapsedTime);
PrintText(10, Height() - 25, scale, ss.str());
ss.str("");
ss << " Rendered Chunks : " << m_renderCount;
PrintText(10, Height() - 35, scale, ss.str());
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);
glBlendEquation(GL_FUNC_SUBTRACT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glPopMatrix(); glPopMatrix();
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glPopMatrix(); glPopMatrix();
} }
void Engine::PrintText(unsigned int x, unsigned int y, const std::string& t) { void Engine::PrintText(unsigned int x, unsigned int y, float scale, const std::string& t) {
glLoadIdentity(); glLoadIdentity();
glTranslated(x, y, 0); glTranslated(x, y, 0);
for (unsigned int i = 0; i < t.length(); ++i) { for (unsigned int i = 0; i < t.length(); ++i) {
@ -178,6 +244,7 @@ void Engine::PrintText(unsigned int x, unsigned int y, const std::string& t) {
glTexCoord2f(left, 1.f - top); glTexCoord2f(left, 1.f - top);
glVertex2f(0, 12); glVertex2f(0, 12);
glEnd(); glEnd();
glTranslated(8, 0, 0); glTranslated(8, 0, 0);
} }
} }
@ -238,7 +305,7 @@ void Engine::Render(float elapsedTime) {
m_bullets[0] = new Bullet(m_player); m_bullets[0] = new Bullet(m_player);
} }
bulletTime = .1f; bulletTime = .1f;
m_audio.Create3DAudioObj(m_powpow, AUDIO_PATH "pow.wav", m_player.GetPOV(), m_player.GetDirection() * 10, .5f); m_audio.Create3DAudioObj(m_powpow, AUDIO_PATH "windowsaccount.wav", m_player.GetPOV(), m_player.GetDirection() * 10, .5f);
if (m_flash) { // Coupe le rendering et affiche un frame blanc, pour simuler un flash. if (m_flash) { // Coupe le rendering et affiche un frame blanc, pour simuler un flash.
glClearColor(.8f, .8f, .8f, 1.f); glClearColor(.8f, .8f, .8f, 1.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
@ -263,7 +330,13 @@ void Engine::Render(float elapsedTime) {
if (m_wireframe) if (m_wireframe)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
DrawHud(elapsedTime, bloc);
if (m_displayHud)
DrawHud();
if (m_displayData)
DrawData(elapsedTime, bloc);
if (m_wireframe) if (m_wireframe)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
@ -280,54 +353,51 @@ void Engine::Render(float elapsedTime) {
void Engine::KeyPressEvent(unsigned char key) { void Engine::KeyPressEvent(unsigned char key) {
switch (key) { switch (key) {
case 36: // ESC case 0: // A - Gauche
Stop();
break;
case 94: // F10
SetFullscreen(!IsFullscreen());
break;
case 22: // W
if (!m_keyW) {
// std::cout << "W " << std::endl;
m_keyW = true;
}
break;
case 0: // A
if (!m_keyA) { if (!m_keyA) {
//std::cout << "A " << std::endl;
m_keyA = true; m_keyA = true;
} }
break; break;
case 18: // S case 3: // D - Droite
if (!m_keyS) {
//std::cout << "S " << std::endl;
m_keyS = true;
}
break;
case 3: // D
if (!m_keyD) { if (!m_keyD) {
//std::cout << "D " << std::endl;
m_keyD = true; m_keyD = true;
} }
break; break;
case 38: // Left Shift case 18: // S - Reculer
if (!m_keylshift) { if (!m_keyS) {
//std::cout << "Dash!" << std::endl; m_keyS = true;
m_keylshift = true;
} }
break; break;
case 57: // Space case 22: // W - Avancer
if (!m_keyW) {
m_keyW = true;
}
break;
case 36: // ESC - Quitter
Stop();
break;
case 57: // Space - Sauter
if (!m_keySpace) { if (!m_keySpace) {
//std::cout << "Jump! " << std::endl;
m_keySpace = true; m_keySpace = true;
} }
break; break;
case 94: // F10 - Plein écran
SetFullscreen(!IsFullscreen());
break;
case 5: // F - Ignorer case 5: // F - Ignorer
case 24: // Y - Ignorer break;
case 255: // Fn - Ignorer
case 12: // M - Ignorer case 12: // M - Ignorer
break;
case 7: // H - Ignorer
break;
case 8: // I - Ignorer
break;
case 17: // R - Ignorer case 17: // R - Ignorer
break; break;
case 24: // Y - Ignorer
break;
case 255: // Fn - Ignorer
break;
default: default:
std::cout << "Unhandled key: " << (int)key << std::endl; std::cout << "Unhandled key: " << (int)key << std::endl;
} }
@ -335,44 +405,43 @@ void Engine::KeyPressEvent(unsigned char key) {
void Engine::KeyReleaseEvent(unsigned char key) { void Engine::KeyReleaseEvent(unsigned char key) {
switch (key) { switch (key) {
case 5: case 0: // A - Stop gauche
m_keyA = false;
break;
case 3: // D - Stop droite
m_keyD = false;
break;
case 5: // F - Toggle flash
m_flash = !m_flash; m_flash = !m_flash;
break; break;
case 12: case 7: // H - Toggle HUD
m_displayHud = !m_displayHud;
std::cout << "DISPLAY HUD " << (m_displayHud ? "enabled" : "disabled") << std::endl;
break;
case 8: // I - Toggle render data
m_displayData = !m_displayData;
std::cout << "DISPLAY DATA " << (m_displayData ? "enabled" : "disabled") << std::endl;
break;
case 12: // M - Toggle music
m_audio.ToggleMusicState(); m_audio.ToggleMusicState();
break; break;
case 17: case 17: // R - Toggle skybox
m_isSkybox = !m_isSkybox; m_isSkybox = !m_isSkybox;
break; break;
case 24: // Y case 18: // S - Stop reculer
m_keyS = false;
break;
case 22: // W - Stop avancer
m_keyW = false;
break;
case 24: // Y - Wireframe
m_wireframe = !m_wireframe; m_wireframe = !m_wireframe;
if (m_wireframe) if (m_wireframe)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
else else
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
break; break;
case 22: // W case 57: // Espace - Stop sauter
//std::cout << "rW " << std::endl;
m_keyW = false;
break;
case 0: // A
//std::cout << "rA " << std::endl;
m_keyA = false;
break;
case 18: // S
//std::cout << "rS " << std::endl;
m_keyS = false;
break;
case 3: // D
//std::cout << "rD " << std::endl;
m_keyD = false;
break;
case 38: // Left Shift
//std::cout << "rLS " << std::endl;
m_keylshift = false;
break;
case 57: // Espace
//std::cout << "rSpace " << std::endl;
m_keySpace = false; m_keySpace = false;
break; break;
} }
@ -439,8 +508,7 @@ void Engine::MouseReleaseEvent(const MOUSE_BUTTON& button, int x, int y) {
bool Engine::LoadTexture(Texture& texture, const std::string& filename, bool stopOnError) { bool Engine::LoadTexture(Texture& texture, const std::string& filename, bool stopOnError) {
texture.Load(filename); texture.Load(filename);
if (!texture.IsValid()) if (!texture.IsValid()) {
{
std::cerr << "Unable to load texture (" << filename << ")" << std::endl; std::cerr << "Unable to load texture (" << filename << ")" << std::endl;
if (stopOnError) if (stopOnError)
Stop(); Stop();

View File

@ -29,11 +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(float elapsedTime, BlockType bloc); void DrawHud();
void PrintText(unsigned int x, unsigned int y, const std::string& t); void DrawData(float elapsedTime, BlockType bloc);
void PrintText(unsigned int x, unsigned int y, float scale, const std::string& t);
int GetFps(float elapsedTime) const; int GetFps(float elapsedTime) const;
bool m_wireframe = false; bool m_wireframe = false;
@ -52,7 +54,7 @@ private:
Texture m_textureCrosshair; Texture m_textureCrosshair;
Skybox m_skybox; Skybox m_skybox;
Audio m_audio = Audio(AUDIO_PATH "music01.wav"); Audio m_audio = Audio(AUDIO_PATH "start.wav");
irrklang::ISound* m_powpow; irrklang::ISound* m_powpow;
irrklang::ISound* m_scream; irrklang::ISound* m_scream;
@ -65,7 +67,6 @@ private:
bool m_keyA = false; bool m_keyA = false;
bool m_keyS = false; bool m_keyS = false;
bool m_keyD = false; bool m_keyD = false;
bool m_keylshift = false;
bool m_keySpace = false; bool m_keySpace = false;
bool m_mouseL = false; bool m_mouseL = false;
bool m_mouseR = false; bool m_mouseR = false;
@ -75,6 +76,8 @@ private:
bool m_block = false; bool m_block = false;
bool m_flash = true; bool m_flash = true;
bool m_displayData = false;
bool m_displayHud = true;
}; };

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -4,6 +4,8 @@
Player::Player(const Vector3f& position, float rotX, float rotY) : m_position(position), m_rotX(rotX), m_rotY(rotY) { Player::Player(const Vector3f& position, float rotX, float rotY) : m_position(position), m_rotX(rotX), m_rotY(rotY) {
m_velocity = Vector3f(0, 0, 0); m_velocity = Vector3f(0, 0, 0);
m_airborne = true; m_airborne = true;
m_hp = 0.75f;
m_username = "John Doe";
} }
void Player::TurnLeftRight(float value) { void Player::TurnLeftRight(float value) {
@ -183,6 +185,10 @@ Vector3f Player::GetPOV() const { return Vector3f(GetPosition().x, m_POV, GetPos
Vector3f Player::GetDirection() const { return m_direction; } Vector3f Player::GetDirection() const { return m_direction; }
std::string Player::GetUsername() const { return m_username; }
float Player::GetHP() const { return m_hp; }
void Player::Teleport(int& x, int& z) { void Player::Teleport(int& x, int& z) {
m_position.x -= x * CHUNK_SIZE_X; m_position.x -= x * CHUNK_SIZE_X;
m_position.z -= z * CHUNK_SIZE_Z; m_position.z -= z * CHUNK_SIZE_Z;

View File

@ -20,6 +20,8 @@ public:
Vector3f GetDirection() const; Vector3f GetDirection() const;
Vector3f GetVelocity() const; Vector3f GetVelocity() const;
Vector3f GetPOV() const; Vector3f GetPOV() const;
std::string GetUsername() const;
float GetHP() const;
void Teleport(int& x, int& z); void Teleport(int& x, int& z);
private: private:
@ -27,10 +29,14 @@ private:
Vector3f m_velocity; Vector3f m_velocity;
Vector3f m_direction; Vector3f m_direction;
std::string m_username;
float m_rotX = 0; float m_rotX = 0;
float m_rotY = 0; float m_rotY = 0;
float m_POV; float m_POV;
float m_hp;
bool m_airborne; bool m_airborne;
}; };
#endif //_PLAYER_H__ #endif //_PLAYER_H__