create SDL window
This commit is contained in:
@@ -17,28 +17,45 @@ FetchContent_Declare(
|
|||||||
)
|
)
|
||||||
FetchContent_MakeAvailable(googletest)
|
FetchContent_MakeAvailable(googletest)
|
||||||
|
|
||||||
|
# sdl3
|
||||||
|
FetchContent_Declare(
|
||||||
|
sdl3
|
||||||
|
GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
|
||||||
|
GIT_TAG release-3.4.x
|
||||||
|
)
|
||||||
|
FetchContent_MakeAvailable(sdl3)
|
||||||
|
|
||||||
# add_subdirectory() to nest CMakeLists
|
# add_subdirectory() to nest CMakeLists
|
||||||
|
|
||||||
add_executable(maiden
|
add_executable(maiden
|
||||||
src/App.cpp
|
src/App.cpp
|
||||||
|
src/Window.cpp
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
# include extra source files here
|
# include extra source files here
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(maiden_test
|
add_executable(maiden_test
|
||||||
src/App.cpp
|
src/App.cpp
|
||||||
|
src/Window.cpp
|
||||||
test/TestApp.cpp
|
test/TestApp.cpp
|
||||||
)
|
)
|
||||||
|
# i think the strat is to build all of the core app components into a single library
|
||||||
|
# and then you link that to the two different executables (real main and test main)
|
||||||
|
|
||||||
target_include_directories(maiden PRIVATE
|
target_include_directories(maiden PRIVATE
|
||||||
"${CMAKE_CURRENT_SOURCE_DIR}/src"
|
"${CMAKE_CURRENT_SOURCE_DIR}/src"
|
||||||
# add additional include directories here
|
# add additional include directories here
|
||||||
)
|
)
|
||||||
|
|
||||||
|
target_link_libraries(maiden PRIVATE
|
||||||
|
SDL3::SDL3
|
||||||
|
)
|
||||||
|
|
||||||
target_include_directories(maiden_test PRIVATE
|
target_include_directories(maiden_test PRIVATE
|
||||||
"${CMAKE_CURRENT_SOURCE_DIR}/src"
|
"${CMAKE_CURRENT_SOURCE_DIR}/src"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# test only stuff down here VVV
|
||||||
target_compile_options(maiden_test PRIVATE --coverage)
|
target_compile_options(maiden_test PRIVATE --coverage)
|
||||||
target_link_options(maiden_test PRIVATE --coverage)
|
target_link_options(maiden_test PRIVATE --coverage)
|
||||||
|
|
||||||
@@ -46,6 +63,7 @@ target_link_options(maiden_test PRIVATE --coverage)
|
|||||||
target_link_libraries(maiden_test PRIVATE
|
target_link_libraries(maiden_test PRIVATE
|
||||||
GTest::gmock
|
GTest::gmock
|
||||||
GTest::gmock_main
|
GTest::gmock_main
|
||||||
|
SDL3::SDL3
|
||||||
)
|
)
|
||||||
|
|
||||||
include(CTest)
|
include(CTest)
|
||||||
|
|||||||
49
src/App.cpp
49
src/App.cpp
@@ -1,18 +1,49 @@
|
|||||||
|
|
||||||
#include "App.hpp"
|
#include "App.hpp"
|
||||||
|
|
||||||
void App::run() {
|
#include <thread>
|
||||||
|
#include <chrono>
|
||||||
std::cout << "im an app and im running !!" << std::endl;
|
#include <SDL3/SDL_events.h>
|
||||||
|
|
||||||
(void)foo();
|
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
|
App::App(): window_(new Window()) {
|
||||||
|
// smart pointers might be a good idea
|
||||||
|
// since app will be the top level owner we can use unique ptrs for almost everything
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t App::foo() {
|
int32_t App::run() {
|
||||||
|
|
||||||
return 12;
|
SDL_Event event;
|
||||||
|
while (window_->open()) {
|
||||||
|
// app loop for as long as the window is open
|
||||||
|
|
||||||
|
// pass events to the window
|
||||||
|
while(SDL_PollEvent(&event) != 0) {
|
||||||
|
window_->handleEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
// pass vulkan handling to engine
|
||||||
|
// call engine.render() or something
|
||||||
|
// engine will have a pointer to window so can handle pushing to the screen
|
||||||
|
|
||||||
|
if(window_->rendering()) {
|
||||||
|
// engine.draw();
|
||||||
|
|
||||||
|
// TODO: performance profiling :3
|
||||||
|
// static int counter = 0;
|
||||||
|
// if(counter > 100) {
|
||||||
|
// std::cout << "out" << std::endl;
|
||||||
|
// counter = 0;
|
||||||
|
// }
|
||||||
|
// counter++;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// throttle while the window isn't active
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SDL teardown handled in window destructor
|
||||||
|
|
||||||
|
return 0; // maybe want to have an error codes enum for better clarity
|
||||||
|
|
||||||
}
|
}
|
||||||
14
src/App.hpp
14
src/App.hpp
@@ -4,16 +4,20 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include "Window.hpp"
|
||||||
|
|
||||||
class App {
|
class App {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
App() = default;
|
App();
|
||||||
~App() = default;
|
~App() = default;
|
||||||
|
|
||||||
void run();
|
int32_t run();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
int32_t foo();
|
Window* window_;
|
||||||
|
|
||||||
|
bool rendering_ = true;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
45
src/Window.cpp
Normal file
45
src/Window.cpp
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
|
||||||
|
#include "Window.hpp"
|
||||||
|
|
||||||
|
#include <SDL3/SDL_events.h>
|
||||||
|
|
||||||
|
Window::Window() {
|
||||||
|
|
||||||
|
(void)init();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Window::~Window() {
|
||||||
|
|
||||||
|
SDL_DestroyWindow(sdlWindow_);
|
||||||
|
SDL_Quit();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int Window::init() {
|
||||||
|
|
||||||
|
// TODO: config service for controlling window parameters
|
||||||
|
sdlWindow_ = SDL_CreateWindow("Ouros: Vulkan", 1280u, 720u, SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE);
|
||||||
|
|
||||||
|
if(sdlWindow_ != nullptr) {
|
||||||
|
rendering_ = true;
|
||||||
|
open_ = true;
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Window::handleEvent(SDL_Event& event) {
|
||||||
|
|
||||||
|
if(event.type == SDL_EVENT_QUIT ) {
|
||||||
|
open_ = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(event.type == SDL_EVENT_WINDOW_MINIMIZED) {
|
||||||
|
rendering_ = false;
|
||||||
|
} else if(event.type == SDL_EVENT_WINDOW_RESTORED) {
|
||||||
|
rendering_ = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
34
src/Window.hpp
Normal file
34
src/Window.hpp
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "SDL3/SDL.h"
|
||||||
|
#include "SDL3/SDL_vulkan.h"
|
||||||
|
|
||||||
|
// reference: https://wiki.libsdl.org/SDL3/SDL_CreateWindow
|
||||||
|
class Window {
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
Window();
|
||||||
|
~Window();
|
||||||
|
|
||||||
|
// for SDL3 event polling, runs once per app iteration in its while(running) loop
|
||||||
|
void handleEvent(SDL_Event& event);
|
||||||
|
|
||||||
|
bool rendering() { return rendering_; }
|
||||||
|
bool open() { return open_; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
// launches window, runs at app startup
|
||||||
|
int init();
|
||||||
|
|
||||||
|
// this window class will eventually hold mouse, keyboard, audio, etc. interfaces, like an SDL3 hub
|
||||||
|
// app will be able to attach callbacks for mouse and keyboard events
|
||||||
|
|
||||||
|
SDL_Window* sdlWindow_;
|
||||||
|
|
||||||
|
bool rendering_ = false;
|
||||||
|
bool open_ = false;
|
||||||
|
|
||||||
|
};
|
||||||
@@ -7,8 +7,10 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
std::cout << "hi mom !" << std::endl;
|
std::cout << "hi mom !" << std::endl;
|
||||||
|
|
||||||
|
// create app and run
|
||||||
App app;
|
App app;
|
||||||
app.run();
|
app.run();
|
||||||
|
// no freaking way
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,7 @@
|
|||||||
#include <gmock/gmock.h>
|
#include <gmock/gmock.h>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#define private public
|
#include "App.hpp"
|
||||||
#include <App.hpp>
|
|
||||||
#undef private
|
|
||||||
|
|
||||||
class TestApp : public testing::Test {
|
class TestApp : public testing::Test {
|
||||||
|
|
||||||
@@ -21,16 +19,7 @@ class TestApp : public testing::Test {
|
|||||||
TEST_F(TestApp, TestApp_run_nominal) {
|
TEST_F(TestApp, TestApp_run_nominal) {
|
||||||
|
|
||||||
createUut();
|
createUut();
|
||||||
uut_->run();
|
|
||||||
|
|
||||||
// no expect here
|
//EXPECT_EQ(uut_->run(), 0l);
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_F(TestApp, TestApp_foo_nominal) {
|
|
||||||
|
|
||||||
createUut();
|
|
||||||
|
|
||||||
EXPECT_EQ(uut_->foo(), 12);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user