Refactor app and add glcontext

This commit is contained in:
Eero Holmala 2023-04-28 16:13:18 +03:00
parent 4b9411d73b
commit f8ac97ff7e
5 changed files with 93 additions and 69 deletions

View File

@ -45,7 +45,32 @@
"xutility": "cpp", "xutility": "cpp",
"list": "cpp", "list": "cpp",
"vector": "cpp", "vector": "cpp",
"xtree": "cpp" "xtree": "cpp",
"any": "cpp",
"charconv": "cpp",
"chrono": "cpp",
"condition_variable": "cpp",
"format": "cpp",
"forward_list": "cpp",
"functional": "cpp",
"iomanip": "cpp",
"locale": "cpp",
"map": "cpp",
"mutex": "cpp",
"optional": "cpp",
"ratio": "cpp",
"set": "cpp",
"sstream": "cpp",
"stop_token": "cpp",
"string": "cpp",
"thread": "cpp",
"unordered_map": "cpp",
"variant": "cpp",
"xhash": "cpp",
"xlocbuf": "cpp",
"xlocmes": "cpp",
"xlocmon": "cpp",
"xloctime": "cpp"
}, },
"cmake.configureOnOpen": true "cmake.configureOnOpen": true
} }

View File

@ -12,7 +12,13 @@ add_executable(App
) )
find_package(SDL2 CONFIG REQUIRED) find_package(SDL2 CONFIG REQUIRED)
target_link_libraries(App PRIVATE AppLib SDL2::SDL2 SDL2::SDL2main) find_package(OpenGL REQUIRED)
target_link_libraries(App PRIVATE
AppLib
SDL2::SDL2
SDL2::SDL2main
OpenGL::GL
)
# target_link_libraries(App PUBLIC ) # target_link_libraries(App PUBLIC )
target_include_directories(App PUBLIC "${CMAKE_SOURCE_DIR}/App/include") target_include_directories(App PUBLIC "${CMAKE_SOURCE_DIR}/App/include")

View File

@ -1,5 +1,13 @@
#include <memory>
#include "SDL2/SDL.h"
// #include "SDL2/SDL_vulkan.h"
#include <SDL2/SDL_opengl.h>
#include <GL/gl.h>
#include "Logger.hh" #include "Logger.hh"
using namespace Core; using namespace Core;
namespace App namespace App
@ -7,8 +15,13 @@ namespace App
class App class App
{ {
private: private:
Logger* _logger; std::shared_ptr<Logger> m_Logger;
int _return_code; int m_ReturnCode;
int m_WindowWidth;
int m_WindowHeight;
SDL_Window* m_Window;
SDL_GLContext m_glContext;
bool m_Quit;
public: public:
App(/* args */); App(/* args */);
int getReturnCode() const; int getReturnCode() const;

View File

@ -1,72 +1,63 @@
#include <iostream> #include <iostream>
#include "SDL2/SDL.h"
#include "SDL2/SDL_vulkan.h"
#include "App.hh" #include "App.hh"
// #ifdef _WIN32 #ifdef _WIN32
// #pragma comment(linker, "/subsystem:windows") #pragma comment(linker, "/subsystem:windows")
// #define VK_USE_PLATFORM_WIN32_KHR #define VK_USE_PLATFORM_WIN32_KHR
// #define PLATFORM_SURFACE_EXTENSION_NAME VK_KHR_WIN32_SURFACE_EXTENSION_NAME #define PLATFORM_SURFACE_EXTENSION_NAME VK_KHR_WIN32_SURFACE_EXTENSION_NAME
// #endif #endif
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
namespace App namespace App
{ {
App::App() : _return_code(0) App::App() : m_ReturnCode(0), m_WindowWidth(800), m_WindowHeight(600), m_Quit(false)
{ {
std::cout << "Eternity Engine" << std::endl; std::cout << "Eternity Engine" << std::endl;
_logger = new Logger(); m_Logger = std::make_shared<Logger>();
//Use OpenGL 3.1 core
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
if(SDL_Init(SDL_INIT_VIDEO) < 0) if(SDL_Init(SDL_INIT_VIDEO) < 0)
{ {
std::cout << "SDL could not be initialized!" << std::endl std::cout << "SDL could not be initialized!" << std::endl
<< "SDL_Error: " << SDL_GetError() << std::endl; << "SDL_Error: " << SDL_GetError() << std::endl;
_return_code=-1; m_ReturnCode=-1;
return; return;
} }
// SDL_Vulkan_LoadLibrary(nullptr); // SDL_Vulkan_LoadLibrary(nullptr);
SDL_Window *window = SDL_CreateWindow("Basic C++ SDL project", m_Window = SDL_CreateWindow("App",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT, m_WindowWidth, m_WindowHeight,
SDL_WINDOW_SHOWN); SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
if(!window) if(!m_Window)
{ {
std::cout << "Window could not be created!" << std::endl std::cout << "Window could not be created!" << std::endl
<< "SDL_Error: " << SDL_GetError() << std::endl; << "SDL_Error: " << SDL_GetError() << std::endl;
_return_code=-1; m_ReturnCode=-1;
return; return;
} }
// Create renderer
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); m_glContext = SDL_GL_CreateContext(m_Window);
if(!renderer) if( !m_glContext )
{ {
std::cout << "Renderer could not be created!" << std::endl // Display error message
<< "SDL_Error: " << SDL_GetError() << std::endl; std::cout << "OpenGL context could not be created!" << std::endl
_return_code=-1; << "SDL_Error: " << SDL_GetError() << std::endl;
m_ReturnCode=-1;
return; return;
} }
else 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 // Event loop exit flag
bool quit = false; m_Quit = false;
// Event loop // Event loop
while(!quit) while(!m_Quit)
{ {
SDL_Event e; SDL_Event e;
@ -76,43 +67,32 @@ namespace App
// User requests quit // User requests quit
if(e.type == SDL_QUIT) if(e.type == SDL_QUIT)
{ {
quit = true; m_Quit = true;
} }
// Initialize renderer color white for the background // Set background color as cornflower blue
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); glClearColor(0.39f, 0.58f, 0.93f, 1.f);
// Clear color buffer
// Clear screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SDL_RenderClear(renderer); // Update window with OpenGL rendering
SDL_GL_SwapWindow(m_Window);
// 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() App::~App()
{ {
_logger->~Logger(); SDL_DestroyWindow(m_Window);
delete _logger; SDL_Quit();
m_Logger->~Logger();
} }
int App::getReturnCode() const int App::getReturnCode() const
{ {
return _return_code; return m_ReturnCode;
} }
} // namespace App } // namespace App

View File

@ -5,7 +5,7 @@
using namespace std; using namespace std;
int main(int argc, char const *argv[]) int main(int argc, char * argv[])
{ {
App::App* app = new App::App(); App::App* app = new App::App();
return app->getReturnCode(); return app->getReturnCode();