From ce701cbf9a9ebbd4c10709f94c85a372486f9dc4 Mon Sep 17 00:00:00 2001 From: Sepand Date: Tue, 3 May 2022 11:38:39 +0430 Subject: [PATCH] Added Support for OpenGL3.1 --- .gitignore | 5 +++- Tetris.vcxproj | 18 +++++++++++++- src/application.cpp | 7 +++++- src/game.cpp | 58 +++++++++++++++++++++++---------------------- src/game.hpp | 2 +- src/main.cpp | 7 +++++- src/renderer.hpp | 6 ++--- src/shader.cpp | 2 +- 8 files changed, 68 insertions(+), 37 deletions(-) diff --git a/.gitignore b/.gitignore index 6a6ec3f..d1d8c61 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ -glad.c \ No newline at end of file +glad.c +settings.json +Tetris.vcxproj.filters +Tetris.vcxproj.user \ No newline at end of file diff --git a/Tetris.vcxproj b/Tetris.vcxproj index 81dc9b6..a800f71 100644 --- a/Tetris.vcxproj +++ b/Tetris.vcxproj @@ -102,12 +102,16 @@ true $(SolutionDir)Dependencies\;$(SolutionDir)Dependencies\glm-0.9.9.7\;$(SolutionDir)Dependencies\glad-4.6\include;$(SolutionDir)Tetris\src\;$(SolutionDir)Dependencies\glew-2.1.0\include\;$(SolutionDir)Dependencies\glfw-3.3.6.bin.WIN64\include\;$(SolutionDir)Dependencies\freetype\include;$(SolutionDir)Dependencies\nlohmann stdcpp20 + stdc17 Console true glew32s.lib;glfw3.lib;OpenGL32.lib;freetype.lib;%(AdditionalDependencies) $(SolutionDir)Dependencies\glew-2.1.0\lib\Release\x64\;$(SolutionDir)Dependencies\glfw-3.3.6.bin.WIN64\lib-vc2022\;$(SolutionDir)Dependencies\glad-4.6\src;$(SolutionDir)Dependencies\freetype-2.12.0\objs\x64\Release Static\ + + + false @@ -120,6 +124,7 @@ true $(SolutionDir)Dependencies\;$(SolutionDir)Dependencies\glm-0.9.9.7\;$(SolutionDir)Dependencies\glad-4.6\include;$(SolutionDir)Tetris\src\;$(SolutionDir)Dependencies\glew-2.1.0\include\;$(SolutionDir)Dependencies\glfw-3.3.6.bin.WIN64\include\;$(SolutionDir)Dependencies\freetype\include;$(SolutionDir)Dependencies\nlohmann stdcpp20 + stdc17 Console @@ -128,6 +133,9 @@ true glew32s.lib;glfw3.lib;OpenGL32.lib;freetype.lib;%(AdditionalDependencies) $(SolutionDir)Dependencies\glew-2.1.0\lib\Release\x64\;$(SolutionDir)Dependencies\glfw-3.3.6.bin.WIN64\lib-vc2022\;$(SolutionDir)Dependencies\glad-4.6\src;$(SolutionDir)Dependencies\freetype-2.12.0\objs\x64\Release Static\ + + + false @@ -138,12 +146,16 @@ true $(SolutionDir)Dependencies\;$(SolutionDir)Dependencies\glm-0.9.9.7\;$(SolutionDir)Dependencies\glad-4.6\include;$(SolutionDir)Tetris\src\;$(SolutionDir)Dependencies\glew-2.1.0\include\;$(SolutionDir)Dependencies\glfw-3.3.6.bin.WIN64\include\;$(SolutionDir)Dependencies\freetype\include;$(SolutionDir)Dependencies\nlohmann stdcpp20 + stdc17 Console true glew32s.lib;glfw3.lib;OpenGL32.lib;freetype.lib;%(AdditionalDependencies) $(SolutionDir)Dependencies\glew-2.1.0\lib\Release\x64\;$(SolutionDir)Dependencies\glfw-3.3.6.bin.WIN64\lib-vc2022\;$(SolutionDir)Dependencies\glad-4.6\src;$(SolutionDir)Dependencies\freetype-2.12.0\objs\x64\Release Static\ + + + false @@ -156,14 +168,18 @@ true $(SolutionDir)Dependencies\;$(SolutionDir)Dependencies\glm-0.9.9.7\;$(SolutionDir)Dependencies\glad-4.6\include;$(SolutionDir)Tetris\src\;$(SolutionDir)Dependencies\glew-2.1.0\include\;$(SolutionDir)Dependencies\glfw-3.3.6.bin.WIN64\include\;$(SolutionDir)Dependencies\freetype\include;$(SolutionDir)Dependencies\nlohmann stdcpp20 + stdc17 - Console + Windows true true true glew32s.lib;glfw3.lib;OpenGL32.lib;freetype.lib;%(AdditionalDependencies) $(SolutionDir)Dependencies\glew-2.1.0\lib\Release\x64\;$(SolutionDir)Dependencies\glfw-3.3.6.bin.WIN64\lib-vc2022\;$(SolutionDir)Dependencies\glad-4.6\src;$(SolutionDir)Dependencies\freetype-2.12.0\objs\x64\Release Static\ + + + false diff --git a/src/application.cpp b/src/application.cpp index 1670855..f65b5d4 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -2,9 +2,11 @@ Game Application::Tetris; GLFWwindow* Application::window; + + int Application::Run() { glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE); @@ -31,9 +33,12 @@ int Application::Run() { glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); +#ifdef _DEBUG glDebugMessageCallback(MessageCallback, 0); glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_LOW, 0, NULL, GL_FALSE); glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_NOTIFICATION, 0, NULL, GL_FALSE); +#endif // _DEBUG + Tetris.Init(); diff --git a/src/game.cpp b/src/game.cpp index eb8663c..2cc361a 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1,7 +1,8 @@ #include "game.hpp" void Game::Init() { - DefaultText_.Init("res/fonts/SourceCodePro-Regular.ttf", 256); + SmallText_.Init("res/fonts/SourceCodePro-Regular.ttf", 128); + BigText_.Init("res/fonts/SourceCodePro-Regular.ttf", 256); Renderer::Init2D(); EmptyCellTextureID_ = Renderer::LoadTexture("res/textures/EmptyCell.png"); @@ -664,7 +665,7 @@ void Game::RenderScoreBox() { GetColor(Color::White), GridTextureID_); - DefaultText_.Write( + SmallText_.Write( "SCORE:\n" + std::to_string(Score_) + "\nLEVEL:\n" + std::to_string(Level_), ScoreWindowCoords_ + glm::vec2(CellSize_ * 2), glm::vec2(DefaultFontSize_), @@ -681,7 +682,7 @@ void Game::RenderPause() { GetColor(Color::White), PauseButtonTextureID_); - DefaultText_.Write( + SmallText_.Write( std::string("PRESS ") + GetKeyName(Settings::Json["settings"]["hotkeys"]["PAUSE"]) + " TO RESUME", glm::vec2(windowWidth / 2, windowHeight / 2 + PauseButtonSize_ * 0.4f), glm::vec2(PauseButtonSize_ / 2.5f), @@ -692,13 +693,13 @@ void Game::RenderPause() { void Game::RenderGameOver() { - DefaultText_.Write( + BigText_.Write( "GAME OVER", glm::vec2(windowWidth / 2, windowHeight / 2) - glm::vec2(0.0f, PauseButtonSize_ * 0.5f), glm::vec2(PauseButtonSize_ * 1.5f), GetColor(Color::White), Renderer::Text::HorizontalAlignment::Center); - DefaultText_.Write( + SmallText_.Write( std::string("PRESS ") + GetKeyName(Settings::Json["settings"]["hotkeys"]["RESTART"]) + " TO RESTART", glm::vec2(windowWidth / 2, windowHeight / 2 + PauseButtonSize_ * 0.2f), glm::vec2(PauseButtonSize_ / 2.5f), @@ -711,7 +712,7 @@ void Game::RenderGameOver() { void Game::RenderCountDown() { static const char* numbers[] = { "3","2","1" }; static float size = (float)windowHeight / 4; - DefaultText_.Write( + BigText_.Write( numbers[CountDownInd_], glm::vec2(windowWidth / 2, windowHeight / 2), glm::vec2(size), @@ -731,6 +732,7 @@ void Game::RenderHotkeysMenu() { auto position = SettingsBoxCoords_; static const float offset = 2.5f; + static const auto fontSize = glm::vec2(DefaultFontSize_ * 0.9); auto max = std::min((int)Settings::Hotkeys.size(), TopModuleind_ + MAX_VISIBLE_SETTINGS_ + 1); @@ -744,10 +746,10 @@ void Game::RenderHotkeysMenu() { glm::vec4(0.16f, 0.16f, 0.16f, 1.0f), Renderer::Data.WhiteTextureID ); - DefaultText_.Write( + SmallText_.Write( name, position + SettingsBoxSize_ / 2.0f, - glm::vec2(48.0f), + fontSize, GetColor(Color::White), Renderer::Text::HorizontalAlignment::Center, Renderer::Text::VerticalAlignment::Center @@ -759,10 +761,10 @@ void Game::RenderHotkeysMenu() { GetColor(Color::Gray1), Renderer::Data.WhiteTextureID ); - DefaultText_.Write( + SmallText_.Write( GetKeyName(key), position + glm::vec2(SettingsBoxSize_.x, 0.0f) + SettingsBoxSize_ / 2.0f, - glm::vec2(48.0f), + fontSize, GetColor(Color::White), Renderer::Text::HorizontalAlignment::Center, Renderer::Text::VerticalAlignment::Center @@ -777,10 +779,10 @@ void Game::RenderHotkeysMenu() { GetColor(Color::Gray2), Renderer::Data.WhiteTextureID ); - DefaultText_.Write( + SmallText_.Write( "RESET TO DEFAULT", position + glm::vec2(SettingsBoxSize_.x / 2.0f, 0.0f) + SettingsBoxSize_ / 2.0f, - glm::vec2(SettingsBoxSize_.y / 2.0f), + fontSize, GetColor(Color::White), Renderer::Text::HorizontalAlignment::Center, Renderer::Text::VerticalAlignment::Center @@ -794,10 +796,10 @@ void Game::RenderHotkeysMenu() { GetColor(Color::Gray2), Renderer::Data.WhiteTextureID ); - DefaultText_.Write( + SmallText_.Write( "APPLY", position + glm::vec2(SettingsBoxSize_.x / 2.0f, 0.0f) + SettingsBoxSize_ / 2.0f, - glm::vec2(SettingsBoxSize_.y / 2.0f), + fontSize, GetColor(Color::White), Renderer::Text::HorizontalAlignment::Center, Renderer::Text::VerticalAlignment::Center @@ -809,10 +811,10 @@ void Game::RenderHotkeysMenu() { GetColor(Color::Gray1), Renderer::Data.WhiteTextureID ); - DefaultText_.Write( + SmallText_.Write( "MENU", glm::vec2(SettingsBoxSize_.x / 3, SettingsBoxSize_.y) / 2.0f + glm::vec2(offset) * 4.0f, - glm::vec2(DefaultFontSize_), + fontSize, GetColor(Color::White), Renderer::Text::HorizontalAlignment::Center, Renderer::Text::VerticalAlignment::Center @@ -836,10 +838,10 @@ void Game::RenderHotkeysMenu() { } if (InvalidCustomHotkey_) { - DefaultText_.Write( + SmallText_.Write( "INVALID!", SettingsBoxCoords_ + glm::vec2(SettingsBoxSize_.x * 2.0f, SettingsBoxSize_.y * (HighlightedSettingsInd_ - TopModuleind_ + 0.5f)), - glm::vec2(DefaultFontSize_), + fontSize, GetColor(Color::Red), Renderer::Text::HorizontalAlignment::Left, Renderer::Text::VerticalAlignment::Center @@ -906,10 +908,10 @@ void Game::DrawHighlightSelection(const glm::vec2& position, const glm::vec2& si void Game::RenderMainMenu() { - DefaultText_.Write( + BigText_.Write( "TETRIS", - glm::vec2(windowWidth / 2, windowHeight / 4), - glm::vec2(windowHeight / 4), + glm::vec2(windowWidth / 2.0f, windowHeight / 4.0f), + glm::vec2(windowHeight / 4.0f), GetColor(Color::White), Renderer::Text::HorizontalAlignment::Center, Renderer::Text::VerticalAlignment::Center @@ -934,7 +936,7 @@ void Game::RenderMainMenu() { GetColor(Color::Gray2), Renderer::Data.WhiteTextureID ); - DefaultText_.Write( + SmallText_.Write( button, pos, glm::vec2(size.y / 2), @@ -948,13 +950,13 @@ void Game::RenderMainMenu() { void Game::RenderAbout() { - DefaultText_.Write( - "Tetris clone made using OpenGL in C++ \n" - "Developed by Somso2e in the spring of 2022 as a passion project.\n" - "The full source code is available at\n" + SmallText_.Write( + "Tetris clone made using OpenGL in C++\n" + "Developed by Somso2e in the spring of 2022 as a passion project.\n" + "The full source code is available at\n" "https://github.com/somso2e/Tetris-openGL", glm::vec2(windowWidth / 2, windowHeight / 4), - glm::vec2(DefaultFontSize_ / 1.5f), + glm::vec2(DefaultFontSize_ / 1.2f), GetColor(Color::White), Renderer::Text::HorizontalAlignment::Center, Renderer::Text::VerticalAlignment::Top @@ -977,7 +979,7 @@ void Game::RenderAbout() { GetColor(Color::Gray3) ); - DefaultText_.Write( + SmallText_.Write( "BACK", glm::vec2(windowWidth / 2, windowHeight * 3 / 4), glm::vec2(DefaultFontSize_), diff --git a/src/game.hpp b/src/game.hpp index 70470dc..62ae116 100644 --- a/src/game.hpp +++ b/src/game.hpp @@ -16,7 +16,7 @@ class Game { bool KeysProcessed_[350]{}; private: - Renderer::Text DefaultText_; + Renderer::Text SmallText_, BigText_; enum class GameState { MainMenu, diff --git a/src/main.cpp b/src/main.cpp index 7f76e93..c4772ea 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,11 @@ #include "application.hpp" +#ifdef _DEBUG +#define MAIN main +#else +#define MAIN WinMain +#endif -int main() { +int MAIN() { int init = Application::Run(); if (init) { return -1; diff --git a/src/renderer.hpp b/src/renderer.hpp index aeb9401..079b095 100644 --- a/src/renderer.hpp +++ b/src/renderer.hpp @@ -6,8 +6,8 @@ #include FT_FREETYPE_H -static unsigned int windowHeight = 1080; -static unsigned int windowWidth = 1920; +static unsigned int windowHeight = 720; +static unsigned int windowWidth = 1280; namespace Renderer { @@ -71,7 +71,7 @@ namespace Renderer { }; void Write(const std::string& text, glm::vec2 position, glm::vec2 size, const glm::vec4& color, HorizontalAlignment horizontalAlignment = HorizontalAlignment::Left, VerticalAlignment verticalAlignment = VerticalAlignment::Top); private: - static const int MAXWIDTH = 4096; + static const int MAXWIDTH = GL_MAX_TEXTURE_SIZE; float TextureIndex_; unsigned int TextureWidth_ = 0; diff --git a/src/shader.cpp b/src/shader.cpp index 632900d..f092cb4 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -47,7 +47,7 @@ void Shader::SetUniform1iv(const char* name, GLsizei count, const GLint* value) auto loc = glGetUniformLocation(ID, name); glUniform1iv(loc, count, value); -} +} void Shader::Use() { glUseProgram(ID);