PUSH - Ajout section ÉQUIPEMENT/ARME pour le HUD. +1 texture (gun01.png).

This commit is contained in:
Mathieu Duval 2023-09-16 00:12:14 -04:00
parent 8faa67596e
commit 54ae0b087a
6 changed files with 98 additions and 42 deletions

View File

@ -61,9 +61,11 @@ void Engine::Init() {
void Engine::DeInit() {}
void Engine::LoadResource() {
LoadTexture(m_skybox.GetTexture(), TEXTURE_PATH "skybox.png");
LoadTexture(m_textureCrosshair, TEXTURE_PATH "cross.bmp");
LoadTexture(m_textureFont, TEXTURE_PATH "font.bmp");
LoadTexture(m_skybox.GetTexture(), TEXTURE_PATH "skybox.png", true);
LoadTexture(m_textureCrosshair, TEXTURE_PATH "cross.bmp", true);
LoadTexture(m_textureFont, TEXTURE_PATH "font.bmp", true);
LoadTexture(m_textureGun, TEXTURE_PATH "gun01.png", false);
TextureAtlas::TextureIndex texDirtIndex = m_textureAtlas.AddTexture(TEXTURE_PATH "metal3.png");
TextureAtlas::TextureIndex texIceIndex = m_textureAtlas.AddTexture(TEXTURE_PATH "metal2.png");
TextureAtlas::TextureIndex texGrassIndex = m_textureAtlas.AddTexture(TEXTURE_PATH "grass.png");
@ -115,39 +117,75 @@ void Engine::DisplayCrosshair() {
glEnd();
}
void Engine::DisplayCurrentItem() {
}
void Engine::DisplayHud() {
glBindTexture(GL_TEXTURE_2D, 0);
glLoadIdentity();
glDisable(GL_BLEND);
float fBarWidth = Width() / 6;
// Barre HP
float fBarWidth = Width() / 4;
float fBarHeight = Height() / 25;
float fPosX = Width() / 20;
float fPosY = Height() - (Height() - (fBarHeight + fBarHeight + fBarHeight + fBarHeight));
float fPosY = Height() - (Height() - (fBarHeight * 4));
float playerHp = m_player.GetHP();
float facteurOmbrage = m_displayInfo ? 0.5f : 1.0f;
glColor3f(1.0f, 1.0f, 1.0f);
// Arrière-plan (Barre HP)
glColor3f(1.0f * facteurOmbrage, 1.0f * facteurOmbrage, 1.0f * facteurOmbrage);
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
glVertex2f(fPosX, fPosY - fBarHeight); // Bas-Gauche
glVertex2f(fPosX + fBarWidth, fPosY - fBarHeight); // Bas-Droite
glVertex2f(fPosX + fBarWidth, fPosY); // Haut-Droite
glVertex2f(fPosX, fPosY); // Haut-Gauche
glEnd();
glColor3f(0.0f, 1.0f, 0.0f);
//TODO: Associer avec méchanique de vie du joueur
// Barre HP
glColor3f(0.0f * facteurOmbrage, 1.0f * facteurOmbrage, 0.0f * facteurOmbrage);
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
glVertex2f(fPosX, fPosY - fBarHeight); // Bas-Gauche
glVertex2f(fPosX + fBarWidth * playerHp, fPosY - fBarHeight); // Bas-Droite
glVertex2f(fPosX + fBarWidth * playerHp, fPosY); // Haut-Droite
glVertex2f(fPosX, fPosY); // Haut-Gauche
glEnd();
// Barre équip
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor3f(1.0f * facteurOmbrage, 1.0f * facteurOmbrage, 1.0f * facteurOmbrage);
m_textureGun.Bind();
float margin = Width() * 0.05;
float itemWidth = Width() * 0.33;
float itemHeight = itemWidth / 2.208;
float startX = Width() - itemWidth - margin;
float startY = margin;
glTranslated(startX, startY, 0);
glBegin(GL_QUADS);
glTexCoord2f(1, 0); glVertex2i(0, 0);
glTexCoord2f(0, 0); glVertex2i(itemWidth, 0);
glTexCoord2f(0, 1); glVertex2i(itemWidth, itemHeight);
glTexCoord2f(1, 1); glVertex2i(0, itemHeight);
glEnd();
glDisable(GL_BLEND);
// Username
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
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);
float scale = 1.5f;
m_textureFont.Bind();
std::ostringstream ss;
ss << m_player.GetUsername();
PrintText(x, y, scale, ss.str());
}
@ -156,8 +194,10 @@ 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("");
@ -177,43 +217,54 @@ void Engine::DisplayInfo(float elapsedTime, BlockType bloc) {
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_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glBlendEquation(GL_FUNC_ADD);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, Width(), 0, Height(), -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
if (m_displayHud) {
DisplayHud();
}
if (m_displayInfo) {
DisplayInfo(elapsedTime, bloc);
}
if (m_displayHud) {
DisplayHud();
}
if (m_displayCrosshair) {
DisplayCrosshair();
}
// Reset du blend function
glBlendFunc(GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR);
glBlendEquation(GL_FUNC_SUBTRACT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
@ -221,19 +272,17 @@ void Engine::DrawHud(float elapsedTime, BlockType bloc) {
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) {
float left = (float)((t[i] - 32) % 16) / 16.f;
float top = (float)((t[i] - 32) / 16) / 16.f;
top += .5f;
glBegin(GL_QUADS);
glTexCoord2f(left, 1.f - top - .0625f);
glVertex2f(0, 0);
glTexCoord2f(left + .0625f, 1.f - top - .0625f);
glVertex2f(12, 0);
glTexCoord2f(left + .0625f, 1.f - top);
glVertex2f(12, 12);
glTexCoord2f(left, 1.f - top);
glVertex2f(0, 12);
glTexCoord2f(left, 1.f - top - .0625f); glVertex2f(0, 0);
glTexCoord2f(left + .0625f, 1.f - top - .0625f); glVertex2f(12, 0);
glTexCoord2f(left + .0625f, 1.f - top); glVertex2f(12, 12);
glTexCoord2f(left, 1.f - top); glVertex2f(0, 12);
glEnd();
glTranslated(8, 0, 0);
@ -493,8 +542,8 @@ 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);
bool Engine::LoadTexture(Texture& texture, const std::string& filename, bool useMipmaps, bool stopOnError) {
texture.Load(filename, useMipmaps);
if (!texture.IsValid()) {
std::cerr << "Unable to load texture (" << filename << ")" << std::endl;
if (stopOnError)

View File

@ -31,8 +31,9 @@ public:
virtual void MouseReleaseEvent(const MOUSE_BUTTON &button, int x, int y);
private:
bool LoadTexture(Texture& texture, const std::string& filename, bool stopOnError = true);
bool LoadTexture(Texture& texture, const std::string& filename, bool useMipmaps = true, bool stopOnError = true);
void DisplayCrosshair();
void DisplayCurrentItem();
void DisplayHud();
void DisplayInfo(float elapsedTime, BlockType bloc);
void DrawHud(float elapsedTime, BlockType bloc);
@ -53,6 +54,7 @@ private:
Texture m_textureSkybox;
Texture m_textureFont;
Texture m_textureCrosshair;
Texture m_textureGun;
Skybox m_skybox;
Audio m_audio = Audio(AUDIO_PATH "start.wav");

Binary file not shown.

After

Width:  |  Height:  |  Size: 79 KiB

View File

@ -4,7 +4,7 @@
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_hp = 0.75f; //TODO: Remettre à 1.0f
m_username = "Zelda Bee-Bop";
}

View File

@ -13,8 +13,7 @@ Texture::~Texture()
glDeleteTextures(1, &m_textureId);
}
bool Texture::Load(const std::string& filename)
bool Texture::Load(const std::string& filename, bool useMipMaps)
{
// Initialize Devil only once:
static bool alreadyInitialized = false;
@ -42,8 +41,14 @@ bool Texture::Load(const std::string& filename)
glGenTextures(1, &m_textureId);
glBindTexture(GL_TEXTURE_2D, m_textureId);
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
if (useMipMaps) {
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
}
else {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
ilDeleteImages(1, &texid);

View File

@ -11,7 +11,7 @@ public:
Texture(const std::string& filename = "");
~Texture();
bool Load(const std::string& filename);
bool Load(const std::string& filename, bool useMipMaps = true);
bool IsValid() const;
void Bind() const;