From 9562dc00b86232013d3281d579ec9d1829f0621c Mon Sep 17 00:00:00 2001 From: mduval76 <9343789@etu.cegepsth.qc.ca> Date: Fri, 8 Dec 2023 21:45:19 -0500 Subject: [PATCH] =?UTF-8?q?Push=20SimulateKeyboard=20diff=C3=A9rencie=20MA?= =?UTF-8?q?J=20et=20MIN?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SQCSim2021/engine.cpp | 215 ++++++++++++++++++++++++------------------ SQCSim2021/engine.h | 6 +- 2 files changed, 126 insertions(+), 95 deletions(-) diff --git a/SQCSim2021/engine.cpp b/SQCSim2021/engine.cpp index a7e4a29..7873f36 100644 --- a/SQCSim2021/engine.cpp +++ b/SQCSim2021/engine.cpp @@ -714,7 +714,12 @@ void Engine::DisplaySplashScreen() { glPopMatrix(); } -void Engine::DisplayLobbyMenu() { +void Engine::DisplayLobbyMenu(float elapsedTime) { + static float lobbyTime = 0.0f; + lobbyTime += elapsedTime; + float onInterval = 0.5f; + float offInterval = 1.0f; + GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); @@ -748,22 +753,29 @@ void Engine::DisplayLobbyMenu() { glTexCoord2f(0, 1); glVertex2i(Width() * 0.6, Height() * 0.95); glEnd(); - //glBindTexture(GL_TEXTURE_2D, 0); - //glBegin(GL_QUADS); - //glTexCoord2f(0, 0); glVertex2i(Width() * 0.6, Height() * 0.4); - //glTexCoord2f(1, 0); glVertex2i(Width() * 0.975, Height() * 0.4); - //glTexCoord2f(1, 1); glVertex2i(Width() * 0.975, Height() * 0.475); - //glTexCoord2f(0, 1); glVertex2i(Width() * 0.6, Height() * 0.475); - //glEnd(); + glBlendFunc(GL_SRC_ALPHA, GL_ONE); m_textureFont.Bind(); std::ostringstream ss; glColor4f(1.0f, 1.0f, 1.0f, 1.0f); - ss.str(""); - ss << "_____________________"; - PrintText(Width() * 0.6f, Height() * 0.4f, ss.str(), 2.5f); - ss.str(""); + if (m_charChanged) { + ss << m_inputChar; + } + + if (lobbyTime < onInterval) { + ss << "_"; + } + else if (lobbyTime > onInterval && lobbyTime < offInterval) { + ss << " "; + } + else { + lobbyTime -= offInterval; + } + + PrintText(Width() * 0.6f, Height() * 0.4f, ss.str(), 2.0f); + + glDisable(GL_BLEND); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); glViewport(viewport[0], viewport[1], viewport[2], viewport[3]); @@ -1214,7 +1226,7 @@ void Engine::Render(float elapsedTime) { } if (m_gamestate == GameState::LOBBY) { - DisplayLobbyMenu(); + DisplayLobbyMenu(elapsedTime); return; } @@ -1507,9 +1519,11 @@ void Engine::DrawSlider(float centerX, float centerY, float value, float minVal, } } - - void Engine::KeyPressEvent(unsigned char key) { + if (m_gamestate == LOBBY) { + m_inputChar = SimulateKeyboard(key); + return; + } switch (key) { case 0: // A - Gauche if (!m_keyA) { @@ -1540,9 +1554,6 @@ void Engine::KeyPressEvent(unsigned char key) { } //Stop(); break; - case 38: // Shift - m_keyShift = true; - break; case 57: // Space - Sauter if (!m_keySpace) { m_keySpace = true; @@ -1593,6 +1604,12 @@ void Engine::KeyPressEvent(unsigned char key) { } void Engine::KeyReleaseEvent(unsigned char key) { + if (m_gamestate == LOBBY) { + if (key == 38) { + m_keyShift = false; + } + return; + } switch (key) { case 0: // A - Stop gauche m_keyA = false; @@ -1713,162 +1730,172 @@ void Engine::MouseMoveEvent(int x, int y) { char Engine::SimulateKeyboard(unsigned char key) { switch (key) { case 0: // A - if (!m_keyShift) { return 'a'; } - else { return 'A'; } + if (!m_keyShift) { m_inputChar = 'a'; } + else { m_inputChar = 'A'; } break; case 1: // B - if (!m_keyShift) { return 'b'; } - else { return 'B'; } + if (!m_keyShift) { m_inputChar = 'b'; } + else { m_inputChar = 'B'; } break; case 2: // C - if (!m_keyShift) { return 'c'; } - else { return 'C'; } + if (!m_keyShift) { m_inputChar = 'c'; } + else { m_inputChar = 'C'; } break; case 3: // D - if (!m_keyShift) { return 'd'; } - else { return 'D'; } + if (!m_keyShift) { m_inputChar = 'd'; } + else { m_inputChar = 'D'; } break; case 4: // E - if (!m_keyShift) { return 'e'; } - else { return 'E'; } + if (!m_keyShift) { m_inputChar = 'e'; } + else { m_inputChar = 'E'; } break; case 5: // F - if (!m_keyShift) { return 'f'; } - else { return 'F'; } + if (!m_keyShift) { m_inputChar = 'f'; } + else { m_inputChar = 'F'; } break; case 6: // G - if (!m_keyShift) { return 'g'; } - else { return 'G'; } + if (!m_keyShift) { m_inputChar = 'g'; } + else { m_inputChar = 'G'; } break; case 7: // H - if (!m_keyShift) { return 'h'; } - else { return 'H'; } + if (!m_keyShift) { m_inputChar = 'h'; } + else { m_inputChar = 'H'; } break; case 8: // I - if (!m_keyShift) { return 'i'; } - else { return 'I'; } + if (!m_keyShift) { m_inputChar = 'i'; } + else { m_inputChar = 'I'; } break; case 9: // J - if (!m_keyShift) { return 'j'; } - else { return 'J'; } + if (!m_keyShift) { m_inputChar = 'j'; } + else { m_inputChar = 'J'; } break; case 10: // K - if (!m_keyShift) { return 'k'; } - else { return 'K'; } + if (!m_keyShift) { m_inputChar = 'k'; } + else { m_inputChar = 'K'; } break; case 11: // L - if (!m_keyShift) { return 'l'; } - else { return 'L'; } + if (!m_keyShift) { m_inputChar = 'l'; } + else { m_inputChar = 'L'; } break; case 12: // M - if (!m_keyShift) { return 'm'; } - else { return 'M'; } + if (!m_keyShift) { m_inputChar = 'm'; } + else { m_inputChar = 'M'; } break; case 13: // N - if (!m_keyShift) { return 'n'; } - else { return 'N'; } + if (!m_keyShift) { m_inputChar = 'n'; } + else { m_inputChar = 'N'; } break; case 14: // O - if (!m_keyShift) { return 'o'; } - else { return 'O'; } + if (!m_keyShift) { m_inputChar = 'o'; } + else { m_inputChar = 'O'; } break; case 15: // P - if (!m_keyShift) { return 'p'; } - else { return 'P'; } + if (!m_keyShift) { m_inputChar = 'p'; } + else { m_inputChar = 'P'; } break; case 16: // Q - if (!m_keyShift) { return 'q'; } - else { return 'Q'; } + if (!m_keyShift) { m_inputChar = 'q'; } + else { m_inputChar = 'Q'; } break; case 17: // R - if (!m_keyShift) { return 'r'; } - else { return 'R'; } + if (!m_keyShift) { m_inputChar = 'r'; } + else { m_inputChar = 'R'; } break; case 18: // S - if (!m_keyShift) { return 's'; } - else { return 'S'; } + if (!m_keyShift) { m_inputChar = 's'; } + else { m_inputChar = 'S'; } break; case 19: // T - if (!m_keyShift) { return 't'; } - else { return 'T'; } + if (!m_keyShift) { m_inputChar = 't'; } + else { m_inputChar = 'T'; } break; case 20: // U - if (!m_keyShift) { return 'u'; } - else { return 'U'; } + if (!m_keyShift) { m_inputChar = 'u'; } + else { m_inputChar = 'U'; } break; case 21: // V - if (!m_keyShift) { return 'v'; } - else { return 'V'; } + if (!m_keyShift) { m_inputChar = 'v'; } + else { m_inputChar = 'V'; } break; case 22: // W - if (!m_keyShift) { return 'w'; } - else { return 'W'; } + if (!m_keyShift) { m_inputChar = 'w'; } + else { m_inputChar = 'W'; } break; case 23: // X - if (!m_keyShift) { return 'x'; } - else { return 'X'; } + if (!m_keyShift) { m_inputChar = 'x'; } + else { m_inputChar = 'X'; } break; case 24: // Y - if (!m_keyShift) { return 'y'; } - else { return 'Y'; } + if (!m_keyShift) { m_inputChar = 'y'; } + else { m_inputChar = 'Y'; } break; case 25: // Z - if (!m_keyShift) { return 'z'; } - else { return 'Z'; } + if (!m_keyShift) { m_inputChar = 'z'; } + else { m_inputChar = 'Z'; } break; case 26: // 0 - if (!m_keyShift) { return '0'; } - else { return ')'; } + if (!m_keyShift) { m_inputChar = '0'; } + else { m_inputChar = ')'; } break; case 27: // 1 - if (!m_keyShift) { return '1'; } - else { return '!'; } + if (!m_keyShift) { m_inputChar = '1'; } + else { m_inputChar = '!'; } break; case 28: // 2 - if (!m_keyShift) { return '2'; } - else { return '\"'; } + if (!m_keyShift) { m_inputChar = '2'; } + else { m_inputChar = '\"'; } break; case 29: // 3 - if (!m_keyShift) { return '3'; } - else { return '/'; } + if (!m_keyShift) { m_inputChar = '3'; } + else { m_inputChar = '/'; } break; case 30: // 4 - if (!m_keyShift) { return '4'; } - else { return '$'; } + if (!m_keyShift) { m_inputChar = '4'; } + else { m_inputChar = '$'; } break; case 31: // 5 - if (!m_keyShift) { return '5'; } - else { return '%'; } + if (!m_keyShift) { m_inputChar = '5'; } + else { m_inputChar = '%'; } break; case 32: // 6 - if (!m_keyShift) { return '6'; } - else { return '?'; } + if (!m_keyShift) { m_inputChar = '6'; } + else { m_inputChar = '?'; } break; case 33: // 7 - if (!m_keyShift) { return '7'; } - else { return '&'; } + if (!m_keyShift) { m_inputChar = '7'; } + else { m_inputChar = '&'; } break; case 34: // 8 - if (!m_keyShift) { return '8'; } - else { return '*'; } + if (!m_keyShift) { m_inputChar = '8'; } + else { m_inputChar = '*'; } break; case 35: // 9 - if (!m_keyShift) { return '9'; } - else { return '(A)'; } + if (!m_keyShift) { m_inputChar = '9'; } + else { m_inputChar = ')'; } break; case 36: // SPACE - return ' '; + m_inputChar = ' '; + break; + case 38: // SHIFT + m_keyShift = true; break; case 50: // . (Period) - return '.'; + m_inputChar = '.'; break; case 56: // _ (Underscore) - if (!m_keyShift) { return '-'; } - else { return '_'; } + if (!m_keyShift) { m_inputChar = '-'; } + else { m_inputChar = '_'; } break; default: break; } + + if ((int)key != 38) { + std::cout << "Key pressed: " << (int)key << " (" << m_inputChar << ")" << std::endl; + } + + m_charChanged = true; + return m_inputChar; } void Engine::MousePressEvent(const MOUSE_BUTTON& button, int x, int y) { diff --git a/SQCSim2021/engine.h b/SQCSim2021/engine.h index ca82411..b47bacd 100644 --- a/SQCSim2021/engine.h +++ b/SQCSim2021/engine.h @@ -62,7 +62,7 @@ private: void DisplayInfo(float elapsedTime, BlockType bloc); void DisplaySplashScreen(); - void DisplayLobbyMenu(); + void DisplayLobbyMenu(float elapsedTime); void DisplayMainMenu(); void DrawButtonBackgrounds(float centerX, float centerY, int iterations); @@ -202,6 +202,9 @@ private: bool m_selectedPlayOptions = false; bool m_selectedOptions = false; bool m_selectedQuit = false; + + char m_inputChar = 0; + bool m_charChanged = false; bool m_key1 = false; bool m_key2 = false; @@ -211,6 +214,7 @@ private: bool m_keyA = false; bool m_keyS = false; bool m_keyD = false; + bool m_keyEnter = false; bool m_keySpace = false; bool m_keyShift = false;