diff --git a/src/computervision/object_detection.cpp b/src/computervision/object_detection.cpp index ea3c13f..31b94e2 100644 --- a/src/computervision/object_detection.cpp +++ b/src/computervision/object_detection.cpp @@ -23,6 +23,7 @@ namespace computervision bool background_calibrated = false; bool skin_calibrated = false; + bool hand_open = false; ObjectDetection::ObjectDetection() { @@ -94,6 +95,7 @@ namespace computervision skin_calibrated = true; hand_calibrator.SetSkinCalibration(skin_calibrated); time = 0; + calibration_callback(); } } @@ -126,8 +128,8 @@ namespace computervision hand_present = hand_calibrator.CheckIfHandPresent(handMask, handcalibration::HandDetectionType::MENU); hand_calibrator.SetHandPresent(hand_present); - - return fingers_amount > 0; + hand_open = fingers_amount > 0; + return hand_open; } void ObjectDetection::CalculateDifference() @@ -187,5 +189,15 @@ namespace computervision time += delt_time; } + bool ObjectDetection::IsHandOpen() + { + return hand_open; + } + + bool ObjectDetection::IsCalibrated() + { + return background_calibrated && skin_calibrated; + } + } \ No newline at end of file diff --git a/src/computervision/object_detection.h b/src/computervision/object_detection.h index 4cf76d6..a9bb3b1 100644 --- a/src/computervision/object_detection.h +++ b/src/computervision/object_detection.h @@ -11,6 +11,7 @@ #include #include +#include #include "background_remover.h" #include "skin_detector.h" #include "finger_count.h" @@ -81,6 +82,8 @@ namespace computervision */ bool IsHandOpen(); + bool IsCalibrated(); + /** * @brief checks whether the hand is held within the detection square. @@ -91,10 +94,13 @@ namespace computervision cv::VideoCapture GetCap(); + void SetCalibrationCallback(std::function fun) { calibration_callback = fun; }; + private: bool is_hand_open; bool is_hand_present; void UpdateTime(); + std::function calibration_callback; }; diff --git a/src/scenes/game_Over_Scene.cpp b/src/scenes/game_Over_Scene.cpp index 8da7490..111934b 100644 --- a/src/scenes/game_Over_Scene.cpp +++ b/src/scenes/game_Over_Scene.cpp @@ -23,11 +23,10 @@ namespace scene { shaders::GuiShader* gui_shader_gameOver; std::vector guis_gameOver; - computervision::ObjectDetection objDetect_gameOver; std::vector> score_textures_gameOver; - - float item_number_gameOver = 0; - bool hand_mode_gameOver = false; + double delta_time = 0; + double time = 0; + bool game_over = false; Game_Over_Scene::Game_Over_Scene(int score) { @@ -76,21 +75,6 @@ namespace scene } } - gui::GuiTexture* GetMenuItemGameOver(bool hand_state) { - if (hand_state) - item_number_gameOver += 0.20f; - - int temp_item_number = item_number_gameOver; - - //If temp_item_number is equal to the size of the array, set item_number bac to zero to loop through the array again - if (temp_item_number == guis_gameOver.size()) { - item_number_gameOver = 0; - temp_item_number = 0; - } - std::cout << guis_gameOver[temp_item_number]->texture << std::endl; - return guis_gameOver[temp_item_number]; - } - scene::Scenes scene::Game_Over_Scene::start(GLFWwindow* window) { gui::Button button_start_scene(render_engine::loader::LoadTexture("res/Birb1.jpg"), glm::vec2(0.0f, -0.5f), glm::vec2(0.25f, 0.25f)); button_start_scene.SetHoverTexture(render_engine::loader::LoadTexture("res/Birb2.jpg")); @@ -112,46 +96,8 @@ namespace scene { render(); update(window); - - if (hand_mode_gameOver) - { - cameraFrame = objDetect_gameOver.ReadCamera(); - - bool detect = false; - bool hand_detection = objDetect_gameOver.DetectHand(cameraFrame, detect); - - if (hand_detection) - { - hand_closed = false; - std::cout << "hand is opened" << std::endl; - - //Loop through menu items - chosen_item_gameOver = GetMenuItemGameOver(true); - - gui::Button* new_button = ConvertGuiTextureToButtonGameOver(chosen_item_gameOver); - if (new_button != NULL) { - const float x_pos = (chosen_item_gameOver->position.x + 1.0) * WINDOW_WIDTH / 2; - const float y_pos = (1.0 - chosen_item_gameOver->position.y) * WINDOW_HEIGHT / 2; - - //Set cursor to location of selected menu_item - glfwSetCursorPos(window, x_pos, y_pos); - } - } - else if (!hand_detection) - { - std::cout << "hand is closed" << std::endl; - - //Gets selected menu_item - chosen_item_gameOver = GetMenuItemGameOver(false); - gui::Button* new_button = ConvertGuiTextureToButtonGameOver(chosen_item_gameOver); - - if (new_button != NULL && !hand_closed) { - //Run function click - new_button->ForceClick(GLFW_MOUSE_BUTTON_LEFT); - hand_closed = true; - } - } - } + + if (game_over) button_start_scene.ForceClick(GLFW_MOUSE_BUTTON_LEFT); glfwSwapBuffers(window); glfwPollEvents(); } @@ -169,7 +115,10 @@ namespace scene render_engine::renderer::Prepare(); // Render GUI items - render_engine::renderer::Render(guis_gameOver, *gui_shader_gameOver); + //render_engine::renderer::Render(guis_gameOver, *gui_shader_gameOver); + + render_engine::renderer::Render(game_over_texture, *gui_shader_gameOver); + DrawScore(end_score); } /** @@ -182,11 +131,16 @@ namespace scene if (new_button != NULL) new_button->Update(window); } - bool hand_present; - objDetect_gameOver.DetectHand(objDetect_gameOver.ReadCamera(), hand_present); + + UpdateDeltaTime(); + time += delta_time; + + if (time >= 5.0) + { + game_over = true; + } - render_engine::renderer::Render(game_over_texture, *gui_shader_gameOver); - DrawScore(end_score); + } /** @@ -199,9 +153,6 @@ namespace scene //return_value = scene::Scenes::STARTUP; cv::destroyWindow("camera"); } - else if (glfwGetKey(window, GLFW_KEY_BACKSPACE) == GLFW_PRESS) { - hand_mode_gameOver = !hand_mode_gameOver; - } } void Game_Over_Scene::DrawScore(int score) @@ -217,4 +168,13 @@ namespace scene render_engine::renderer::Render(score_textures_gameOver[digits[i]], *gui_shader_gameOver); } } + + void Game_Over_Scene::UpdateDeltaTime() + { + double current_time = glfwGetTime(); + static double last_frame_time = current_time; + delta_time = current_time - last_frame_time; + last_frame_time = current_time; + + } } \ No newline at end of file diff --git a/src/scenes/game_Over_Scene.h b/src/scenes/game_Over_Scene.h index 5fd7203..4b28658 100644 --- a/src/scenes/game_Over_Scene.h +++ b/src/scenes/game_Over_Scene.h @@ -14,6 +14,7 @@ namespace scene scene::Scenes return_value = scene::Scenes::GAMEOVER; std::vector> score_guis_gameOver; std::shared_ptr game_over_texture; + void UpdateDeltaTime(); public: diff --git a/src/scenes/in_Game_Scene.cpp b/src/scenes/in_Game_Scene.cpp index 0265f18..15daff1 100644 --- a/src/scenes/in_Game_Scene.cpp +++ b/src/scenes/in_Game_Scene.cpp @@ -275,6 +275,7 @@ namespace scene *ptr = score; std::cout << "Score: " << score << std::endl; return_value = scene::Scenes::GAMEOVER; + cv::destroyWindow("camera"); } camera->Follow(main_character->GetPosition()); diff --git a/src/scenes/startup_Scene.cpp b/src/scenes/startup_Scene.cpp index 9d1181e..0e881d9 100644 --- a/src/scenes/startup_Scene.cpp +++ b/src/scenes/startup_Scene.cpp @@ -40,6 +40,11 @@ namespace scene gui_shader1->Init(); } + void Startup_Scene::EnableHandMode() + { + hand_mode = true; + } + gui::Button* ConvertGuiTextureToButton(gui::GuiTexture* texture) { gui::Button* button; if (texture != NULL) @@ -92,7 +97,7 @@ namespace scene scene::Scenes scene::Startup_Scene::start(GLFWwindow *window) { // GUI stuff - gui::Button button_start(render_engine::loader::LoadTexture("res/menu_item_start1.png"), glm::vec2(0.0f, 0.6f), glm::vec2(0.25f, 0.25f)); + gui::Button button_start(render_engine::loader::LoadTexture("res/menu_item_start1.png"), glm::vec2(0.0f, 0.3f), glm::vec2(0.25f, 0.25f)); button_start.SetHoverTexture(render_engine::loader::LoadTexture("res/menu_item_start1_hover.png")); button_start.SetClickedTexture(render_engine::loader::LoadTexture("res/menu_item_start1_click.png")); std::function start_fun = [this]() @@ -106,7 +111,7 @@ namespace scene guis1.push_back(&button_start); - gui::Button button_quit(render_engine::loader::LoadTexture("res/menu_item_quit1.png"), glm::vec2(0.0f, -0.6f), glm::vec2(0.25f, 0.25f)); + gui::Button button_quit(render_engine::loader::LoadTexture("res/menu_item_quit1.png"), glm::vec2(0.0f, -0.3f), glm::vec2(0.25f, 0.25f)); button_quit.SetHoverTexture(render_engine::loader::LoadTexture("res/menu_item_quit1_hover.png")); button_quit.SetClickedTexture(render_engine::loader::LoadTexture("res/menu_item_quit1_click.png")); std::function quit_fun = [this]() @@ -118,11 +123,11 @@ namespace scene button_quit.SetOnClickAction(quit_fun); guis1.push_back(&button_quit); - computervision::ObjectDetection objDetect; cv::Mat cameraFrame; gui::GuiTexture* chosen_item = NULL; //This is the selected menu_item bool hand_closed = false; //Flag to prevent multiple button presses - + std::function calibration_func = [this]() {EnableHandMode(); }; + objDetect.SetCalibrationCallback(calibration_func); while (return_value == scene::Scenes::STARTUP) { render(); diff --git a/src/scenes/startup_Scene.h b/src/scenes/startup_Scene.h index b38fb39..ebfcde2 100644 --- a/src/scenes/startup_Scene.h +++ b/src/scenes/startup_Scene.h @@ -11,6 +11,7 @@ namespace scene private: //return_value is an enum that is necessary for the scene switching. Whenever this changes, the scene will change to a different scene. scene::Scenes return_value = scene::Scenes::STARTUP; + void EnableHandMode(); public: /**