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->setDefault3DSoundMinDistance(1);
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() {
@ -40,9 +40,9 @@ void Audio::Render3DAudioObj(irrklang::ISound* sound, Vector3f& pos, Vector3f& v
sound->setVolume(volume);
}
void Audio::PlaySong(const char* music) {
m_music = m_engine->play2D(music, true, true, true, irrklang::ESM_STREAMING);
}
//void Audio::PlaySong(const char* music) {
// m_music = m_engine->play2D(music, false, false, false, irrklang::ESM_STREAMING);
//}
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 PlaySong(const char* music);
//void PlaySong(const char* music);
void ToggleMusicState();

View File

@ -4,7 +4,7 @@
#include "transformation.h"
#include "player.h"
Engine::Engine() { }
Engine::Engine() {}
Engine::~Engine() {
m_world.CleanUpWorld(m_renderCount, true);
@ -21,22 +21,22 @@ void Engine::Init() {
abort();
}
glDisable(GL_FRAMEBUFFER_SRGB);
glClearColor(0.f, 0.f, 0.f, 1.f);
//glDisable(GL_FRAMEBUFFER_SRGB);
//glEnable(GL_DEPTH_TEST);
//glEnable(GL_STENCIL_TEST);
//glEnable(GL_POINT_SMOOTH);
glEnable(GL_CULL_FACE);
//glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glClearColor(0.f, 0.f, 0.f, 1.f);
gluPerspective(45.0f, (float)Width() / (float)Height(), 0.1f, VIEW_DISTANCE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR);
//glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
//glShadeModel(GL_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBlendEquation(GL_FUNC_SUBTRACT);
// Objet de skybox avec sa propre texture et son propre shader!
@ -57,7 +57,7 @@ void Engine::Init() {
HideCursor();
}
void Engine::DeInit() { }
void Engine::DeInit() {}
void Engine::LoadResource() {
LoadTexture(m_skybox.GetTexture(), TEXTURE_PATH "skybox.png");
@ -97,47 +97,49 @@ void Engine::LoadResource() {
void Engine::UnloadResource() {}
void Engine::DrawHud(float elapsedTime, BlockType bloc) {
// Setter le blend function , tout ce qui sera noir sera transparent
void Engine::DrawHud() {
glDisable(GL_BLEND);
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_CULL_FACE);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, Width(), 0, Height(), -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// Bind de la texture pour le font
m_textureFont.Bind();
std::ostringstream ss;
ss << " Fps : " << GetFps(elapsedTime);
PrintText(10, Height() - 25, ss.str());
ss.str("");
ss << " Rendered Chunks : " << m_renderCount;
PrintText(10, Height() - 35, ss.str());
ss.str("");
ss << " To-Be-Deleted Chunks : " << m_world.GettbDeleted();
PrintText(10, Height() - 45, ss.str());
ss.str("");
ss << " Velocity : " << m_player.GetVelocity(); // IMPORTANT : on utilise l operateur << pour afficher la position
PrintText(10, 10, ss.str());
ss.str("");
ss << " Direction : " << m_player.GetDirection();
PrintText(10, 20, ss.str());
ss.str("");
ss << " Position : " << m_player.GetPosition();
PrintText(10, 30, ss.str());
ss.str("");
ss << " Block : ";
if (bloc == BTYPE_LAST)
ss << "Weapon.";
else ss << (int)bloc;
PrintText(10, 40, ss.str());
m_textureCrosshair.Bind();
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;
glLoadIdentity();
glTranslated(Width() / 2 - crossSize / 2, Height() / 2 - crossSize / 2, 0);
@ -150,18 +152,82 @@ void Engine::DrawHud(float elapsedTime, BlockType bloc) {
glVertex2i(crossSize, crossSize);
glTexCoord2f(0, 1);
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) {
// 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);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
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();
glTranslated(x, y, 0);
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);
glVertex2f(0, 12);
glEnd();
glTranslated(8, 0, 0);
}
}
@ -238,7 +305,7 @@ void Engine::Render(float elapsedTime) {
m_bullets[0] = new Bullet(m_player);
}
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.
glClearColor(.8f, .8f, .8f, 1.f);
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)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
DrawHud(elapsedTime, bloc);
if (m_displayHud)
DrawHud();
if (m_displayData)
DrawData(elapsedTime, bloc);
if (m_wireframe)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
@ -280,54 +353,51 @@ void Engine::Render(float elapsedTime) {
void Engine::KeyPressEvent(unsigned char key) {
switch (key) {
case 36: // ESC
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
case 0: // A - Gauche
if (!m_keyA) {
//std::cout << "A " << std::endl;
m_keyA = true;
}
break;
case 18: // S
if (!m_keyS) {
//std::cout << "S " << std::endl;
m_keyS = true;
}
break;
case 3: // D
case 3: // D - Droite
if (!m_keyD) {
//std::cout << "D " << std::endl;
m_keyD = true;
}
break;
case 38: // Left Shift
if (!m_keylshift) {
//std::cout << "Dash!" << std::endl;
m_keylshift = true;
case 18: // S - Reculer
if (!m_keyS) {
m_keyS = true;
}
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) {
//std::cout << "Jump! " << std::endl;
m_keySpace = true;
}
break;
case 94: // F10 - Plein écran
SetFullscreen(!IsFullscreen());
break;
case 5: // F - Ignorer
case 24: // Y - Ignorer
case 255: // Fn - Ignorer
break;
case 12: // M - Ignorer
break;
case 7: // H - Ignorer
break;
case 8: // I - Ignorer
break;
case 17: // R - Ignorer
break;
case 24: // Y - Ignorer
break;
case 255: // Fn - Ignorer
break;
default:
std::cout << "Unhandled key: " << (int)key << std::endl;
}
@ -335,44 +405,43 @@ void Engine::KeyPressEvent(unsigned char key) {
void Engine::KeyReleaseEvent(unsigned char 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;
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();
break;
case 17:
case 17: // R - Toggle skybox
m_isSkybox = !m_isSkybox;
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;
if (m_wireframe)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
else
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
break;
case 22: // W
//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;
case 57: // Espace - Stop sauter
m_keySpace = false;
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) {
texture.Load(filename);
if (!texture.IsValid())
{
if (!texture.IsValid()) {
std::cerr << "Unable to load texture (" << filename << ")" << std::endl;
if (stopOnError)
Stop();

View File

@ -29,11 +29,13 @@ public:
virtual void MouseMoveEvent(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);
const char* GetGLErrorString(GLenum error);
private:
bool LoadTexture(Texture& texture, const std::string& filename, bool stopOnError = true);
void DrawHud(float elapsedTime, BlockType bloc);
void PrintText(unsigned int x, unsigned int y, const std::string& t);
void DrawHud();
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;
bool m_wireframe = false;
@ -52,7 +54,7 @@ private:
Texture m_textureCrosshair;
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_scream;
@ -65,7 +67,6 @@ private:
bool m_keyA = false;
bool m_keyS = false;
bool m_keyD = false;
bool m_keylshift = false;
bool m_keySpace = false;
bool m_mouseL = false;
bool m_mouseR = false;
@ -75,6 +76,8 @@ private:
bool m_block = false;
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) {
m_velocity = Vector3f(0, 0, 0);
m_airborne = true;
m_hp = 0.75f;
m_username = "John Doe";
}
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; }
std::string Player::GetUsername() const { return m_username; }
float Player::GetHP() const { return m_hp; }
void Player::Teleport(int& x, int& z) {
m_position.x -= x * CHUNK_SIZE_X;
m_position.z -= z * CHUNK_SIZE_Z;

View File

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