From 53dbb607dda4742514e7bb66c53abd97de053b7a Mon Sep 17 00:00:00 2001 From: Eero Holmala Date: Sat, 8 Apr 2023 17:51:58 +0300 Subject: [PATCH] Add sdl2 template to App. Start Math in App --- App/.vscode/settings.json | 3 +- App/App/CMakeLists.txt | 4 +- App/App/include/App.hh | 2 + App/App/src/App.cpp | 105 +++++++++++++++++++++++++++++- App/App/src/main.cpp | 3 +- App/AppLib/include/AppLib/Math.hh | 17 +++++ App/AppLib/src/Math.cpp | 4 ++ 7 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 App/AppLib/include/AppLib/Math.hh create mode 100644 App/AppLib/src/Math.cpp diff --git a/App/.vscode/settings.json b/App/.vscode/settings.json index 32046fb..c085a8b 100644 --- a/App/.vscode/settings.json +++ b/App/.vscode/settings.json @@ -43,5 +43,6 @@ "xstring": "cpp", "xtr1common": "cpp", "xutility": "cpp" - } + }, + "cmake.configureOnOpen": true } \ No newline at end of file diff --git a/App/App/CMakeLists.txt b/App/App/CMakeLists.txt index 35a537d..55f67e1 100644 --- a/App/App/CMakeLists.txt +++ b/App/App/CMakeLists.txt @@ -11,7 +11,9 @@ add_executable(App ${HDRS} ) -target_link_libraries(App PUBLIC AppLib) +find_package(SDL2 CONFIG REQUIRED) +target_link_libraries(App PRIVATE AppLib SDL2::SDL2 SDL2::SDL2main) +# target_link_libraries(App PUBLIC ) target_include_directories(App PUBLIC "${CMAKE_SOURCE_DIR}/App/include") target_include_directories(App PUBLIC "${CMAKE_SOURCE_DIR}/AppLib/include/AppLib") diff --git a/App/App/include/App.hh b/App/App/include/App.hh index 818f3db..446e6a3 100644 --- a/App/App/include/App.hh +++ b/App/App/include/App.hh @@ -8,8 +8,10 @@ namespace App { private: Logger* _logger; + int _return_code; public: App(/* args */); + int getReturnCode() const; ~App(); }; } // namespace App diff --git a/App/App/src/App.cpp b/App/App/src/App.cpp index a1eb7de..e1acb68 100644 --- a/App/App/src/App.cpp +++ b/App/App/src/App.cpp @@ -1,16 +1,117 @@ #include +#include "SDL2/SDL.h" #include "App.hh" +// #ifdef _WIN32 +// #pragma comment(linker, "/subsystem:windows") +// #define VK_USE_PLATFORM_WIN32_KHR +// #define PLATFORM_SURFACE_EXTENSION_NAME VK_KHR_WIN32_SURFACE_EXTENSION_NAME +// #endif + +#define SCREEN_WIDTH 800 +#define SCREEN_HEIGHT 600 + namespace App { - App::App() + App::App() : _return_code(0) { - std::cout << "This is app" << std::endl; + std::cout << "Eternity Engine" << std::endl; + _logger = new Logger(); + if(SDL_Init(SDL_INIT_VIDEO) < 0) + { + std::cout << "SDL could not be initialized!" << std::endl + << "SDL_Error: " << SDL_GetError() << std::endl; + _return_code=-1; + return; + } + // SDL_Vulkan_LoadLibrary(nullptr); + SDL_Window *window = SDL_CreateWindow("Basic C++ SDL project", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + SCREEN_WIDTH, SCREEN_HEIGHT, + SDL_WINDOW_SHOWN); + if(!window) + { + std::cout << "Window could not be created!" << std::endl + << "SDL_Error: " << SDL_GetError() << std::endl; + _return_code=-1; + return; + } + // Create renderer + SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + if(!renderer) + { + std::cout << "Renderer could not be created!" << std::endl + << "SDL_Error: " << SDL_GetError() << std::endl; + _return_code=-1; + return; + } + else + { + // Declare rect of square + SDL_Rect squareRect; + + // Square dimensions: Half of the min(SCREEN_WIDTH, SCREEN_HEIGHT) + squareRect.w = std::min(SCREEN_WIDTH, SCREEN_HEIGHT) / 2; + squareRect.h = std::min(SCREEN_WIDTH, SCREEN_HEIGHT) / 2; + + // Square position: In the middle of the screen + squareRect.x = SCREEN_WIDTH / 2 - squareRect.w / 2; + squareRect.y = SCREEN_HEIGHT / 2 - squareRect.h / 2; + + + // Event loop exit flag + bool quit = false; + + // Event loop + while(!quit) + { + SDL_Event e; + + // Wait indefinitely for the next available event + SDL_WaitEvent(&e); + + // User requests quit + if(e.type == SDL_QUIT) + { + quit = true; + } + + // Initialize renderer color white for the background + SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); + + // Clear screen + SDL_RenderClear(renderer); + + // Set renderer color red to draw the square + SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF); + + // Draw filled square + SDL_RenderFillRect(renderer, &squareRect); + + // Update screen + SDL_RenderPresent(renderer); + } + + // Destroy renderer + SDL_DestroyRenderer(renderer); + } + + // Destroy window + SDL_DestroyWindow(window); + SDL_Quit(); } App::~App() { + _logger->~Logger(); + delete _logger; + } + + int App::getReturnCode() const + { + return _return_code; } } // namespace App diff --git a/App/App/src/main.cpp b/App/App/src/main.cpp index 511afbe..20f3d10 100644 --- a/App/App/src/main.cpp +++ b/App/App/src/main.cpp @@ -1,7 +1,8 @@ #include "App.hh" + int main(int argc, char const *argv[]) { App::App* app = new App::App(); - return 0; + return app->getReturnCode(); } diff --git a/App/AppLib/include/AppLib/Math.hh b/App/AppLib/include/AppLib/Math.hh new file mode 100644 index 0000000..46bcf9b --- /dev/null +++ b/App/AppLib/include/AppLib/Math.hh @@ -0,0 +1,17 @@ +namespace Core +{ + struct Vec2 + { + float x; + float y; + }; + + struct Vec3 + { + float x; + float y; + float z; + float w = 1; + }; + +} // namespace Core diff --git a/App/AppLib/src/Math.cpp b/App/AppLib/src/Math.cpp new file mode 100644 index 0000000..d1d9407 --- /dev/null +++ b/App/AppLib/src/Math.cpp @@ -0,0 +1,4 @@ +namespace Core +{ + +} // namespace Core \ No newline at end of file