From b1a5bf2fe4abfd9cf7f45bde625fdf5362563c61 Mon Sep 17 00:00:00 2001 From: Evert Date: Fri, 13 Apr 2018 22:53:56 +0300 Subject: [PATCH] Humble beginnings - Simple window! --- .gitignore | 6 +++ CMakeLists.txt | 50 ++++++++++++++++++++++ src/Application.cpp | 102 ++++++++++++++++++++++++++++++++++++++++++++ src/Application.h | 21 +++++++++ src/Common.h | 18 ++++++++ src/Main.cpp | 8 ++++ 6 files changed, 205 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 src/Application.cpp create mode 100644 src/Application.h create mode 100644 src/Common.h create mode 100644 src/Main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3ae74b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +CMakeFiles +CMakeCache.txt +cmake_install.cmake +Makefile +/bin/universium +/build diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..2167257 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,50 @@ +cmake_minimum_required(VERSION 3.10) +project(universium-engine) + +# Modules +#SET(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/CMake/Modules ${CMAKE_MODULE_PATH}) + +# Enable compiler warning. +string(APPEND CMAKE_CXX_FLAGS " -Wall") +string(APPEND CMAKE_CXX_FLAGS " -Werror") +string(APPEND CMAKE_CXX_FLAGS " -Wextra") +string(APPEND CMAKE_CXX_FLAGS " -Wno-reorder") +string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-parameter") + +# Sources and headers +include_directories(${PROJECT_SOURCE_DIR}/src) +file(GLOB_RECURSE SOURCES ${PROJECT_SOURCE_DIR}/src/*) + +# Executable output +set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) + +# System-specific options +# TODO: this +if (${CMAKE_SYSTEM_NAME} MATCHES "Linux") + set(UNIVERSIUM_EXECUTABLE "universium") +elseif (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + set(UNIVERSIUM_EXECUTABLE "universium") +elseif (${CMAKE_SYSTEM_NAME} MATCHES "Windows") + set(UNIVERSIUM_EXECUTABLE "universium.exe") +endif() + +add_executable(${UNIVERSIUM_EXECUTABLE} ${SOURCES}) + +# Include GL +find_package(OpenGL REQUIRED) +include_directories(${OPENGL_INCLUDE_DIR}) +target_link_libraries(${UNIVERSIUM_EXECUTABLE} ${OPENGL_LIBRARIES}) + +# Include GLEW +find_package(GLEW REQUIRED) +include_directories(${GLEW_INCLUDE_PATH}) +target_link_libraries(${UNIVERSIUM_EXECUTABLE} ${GLEW_LIBRARY}) + +# Include SDL +INCLUDE(FindPkgConfig) + +PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2) +PKG_SEARCH_MODULE(SDL2IMAGE REQUIRED SDL2_image>=2.0.0) + +include_directories(${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIRS}) +target_link_libraries(${UNIVERSIUM_EXECUTABLE} ${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARIES}) diff --git a/src/Application.cpp b/src/Application.cpp new file mode 100644 index 0000000..1eb7448 --- /dev/null +++ b/src/Application.cpp @@ -0,0 +1,102 @@ +#include "Application.h" + +Application::Application() +{ + +} + +Application::~Application() +{ + +} + +void Application::Initialize() +{ + if (SDL_Init(SDL_INIT_VIDEO) < 0) + { + std::cout << "Failed to init SDL\n"; + printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); + return; + } + + // Create our window centered at 1080x720 resolution + m_window = SDL_CreateWindow( + "Universium", + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, + 1080, + 720, + SDL_WINDOW_OPENGL + ); + + if (!m_window) + { + printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); + return; + } + + // Set GL Attributes + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + + SDL_GLContext glContext = SDL_GL_CreateContext(m_window); + + if(glContext == NULL) + { + printf("OpenGL context could not be created! SDL Error: %s\n", SDL_GetError()); + return; + } + + // Initialize GLEW + glewInit(); + + // Run the engine + Run(); +} + +void Application::Run() +{ + m_run = true; + + while (m_run) + { + while(SDL_PollEvent(&m_event) != 0) + { + // Close button is pressed + if(m_event.type == SDL_QUIT) + { + m_run = false; + } + } + + Render(); + + // Set background color as cornflower blue + glClearColor(0.39f, 0.58f, 0.93f, 1.f); + + // Clear color buffer + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + // Update window with OpenGL rendering + SDL_GL_SwapWindow(m_window); + } + + //Destroy window + SDL_DestroyWindow(m_window); + m_window = NULL; + + //Quit SDL subsystems + SDL_Quit(); +} + +void Application::Update(GLfloat dtime) +{ + +} + +void Application::Render() +{ + +} diff --git a/src/Application.h b/src/Application.h new file mode 100644 index 0000000..179541a --- /dev/null +++ b/src/Application.h @@ -0,0 +1,21 @@ +#ifndef __APPLICATION_H__ +#define __APPLICATION_H__ +#include "Common.h" + +class Application +{ + public: + Application(); + ~Application(); + + void Initialize(); + private: + SDL_Window* m_window; + SDL_Event m_event; + bool m_run; + + void Run(); + void Render(); + void Update(GLfloat dtime); +}; +#endif // __APPLICATION_H__ diff --git a/src/Common.h b/src/Common.h new file mode 100644 index 0000000..a335733 --- /dev/null +++ b/src/Common.h @@ -0,0 +1,18 @@ +#ifndef __COMMON_H__ +#define __COMMON_H__ + +#include + +#include +#include + +#include +#include + +#include +#include +#include + +using namespace std; + +#endif // __COMMON_H__ diff --git a/src/Main.cpp b/src/Main.cpp new file mode 100644 index 0000000..5df56f8 --- /dev/null +++ b/src/Main.cpp @@ -0,0 +1,8 @@ +#include "Application.h" + +int main(int argc, char const *argv[]) +{ + Application app; + app.Initialize(); + return 0; +}