Skip to content

Commit

Permalink
Added possibility to choose the driver.
Browse files Browse the repository at this point in the history
  • Loading branch information
Iunusov committed Oct 22, 2024
1 parent 120b2a2 commit 235f6b4
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 47 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,13 @@ jobs:

- name: make
run: ./make.cmd

build_macos:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2

- name: configure
run: brew install mesa
- name: make
run: ./make.cmd
28 changes: 16 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if(MSVC)
add_compile_options(/MP)
add_compile_options(/nologo)

#add_compile_options("$<$<CONFIG:Release>:/fp:strict>")
# add_compile_options("$<$<CONFIG:Release>:/fp:strict>")
add_compile_options("$<$<CONFIG:Release>:/O2>")
else()
add_compile_options("$<$<CONFIG:Release>:-O2>")
Expand Down Expand Up @@ -82,18 +82,16 @@ set(SDL_DISABLE_UNINSTALL
set(FLTK_BUILD_SHARED_LIBS
OFF
CACHE BOOL "" FORCE)




#FIXME:
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/deps/SDL/SDL_Image/include/SDL3_image)
# FIXME:
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/deps/SDL/SDL_Image/include/SDL3_image)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/deps/SDL/SDL/include/SDL3)

add_subdirectory(deps/SDL/SDL)
add_subdirectory(deps/SDL/SDL_Image)
add_subdirectory(deps/fltk)

# add executable
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/stuff)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/interfaces)
Expand All @@ -113,10 +111,10 @@ add_executable(${PROJECT_NAME} ${SUB_SOURCES})

# link and private compile options
find_package(Threads REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE SDL3::SDL3-static SDL3_image-static
Threads::Threads fltk)
target_link_libraries(
${PROJECT_NAME} PRIVATE SDL3::SDL3-static SDL3_image-static Threads::Threads
fltk)

add_definitions(-DFLTK_USE_CAIRO=0)

if(MSVC)
Expand All @@ -128,7 +126,7 @@ else()
-Wextra
-Wpedantic
-pedantic
# -Werror
# -Werror
-Wfatal-errors
-Wshadow
-Wunused
Expand All @@ -149,3 +147,9 @@ if(MSVC)
else()
file(COPY assets DESTINATION ${CMAKE_BINARY_DIR})
endif()

if(MSVC)
file(COPY renderer.txt DESTINATION ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE})
else()
file(COPY renderer.txt DESTINATION ${CMAKE_BINARY_DIR})
endif()
7 changes: 4 additions & 3 deletions Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ int main(int, char **) {
timestamp);

const auto end{std::chrono::steady_clock::now()};
const auto elapsedMS{(size_t)(
std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
.count())};
const auto elapsedMS{
(size_t)(std::chrono::duration_cast<std::chrono::milliseconds>(end -
start)
.count())};

if (expectedMS > elapsedMS) {
renderer->Delay((size_t)(expectedMS - elapsedMS));
Expand Down
114 changes: 84 additions & 30 deletions platform/VideoContextSDL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "MovableObject.hpp"
#include "StaticObject.hpp"
#include <cmath>
#include <fstream>

namespace {

Expand Down Expand Up @@ -80,68 +81,121 @@ void VideoContextSDL::delay(size_t ms) const noexcept {
void VideoContextSDL::present() noexcept { SDL_RenderPresent(rend); }

void VideoContextSDL::setup() noexcept {
SDL_Log("--------------------------------");
SDL_Log(0);
SDL_Log("-------------------------------------------------");
SDL_Log("VideoContextSDL::setup()");
SDL_Log("--------------------------------");
SDL_Log("\n");
SDL_Log("-------------------------------------------------");
SDL_Log("%s", SDL_GetRevision());
SDL_Log(0);
SDL_Log("Available Drivers:");
const auto numdrivers{SDL_GetNumRenderDrivers()};
std::string drivers;

for (auto i(0); i < numdrivers; ++i) {
drivers += SDL_GetRenderDriver(i);
drivers += " ";
}

SDL_Log("%s", drivers.c_str());

auto renderer_name_file = std::ifstream("renderer.txt");
std::string renderer_name;
renderer_name_file >> renderer_name;
if (renderer_name.size() && renderer_name[0] == '#') {
renderer_name = "";
}

if (renderer_name.size()) {
SDL_SetHint(SDL_HINT_RENDER_DRIVER, renderer_name.c_str());
}
SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");

if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) {
SDL_Log("SDL_Init failed");
return;
}

SDL_Log(0);
SDL_Log("Current Driver:");
std::string driver{SDL_GetCurrentVideoDriver()};
SDL_Log("%s", driver.c_str());
SDL_Log("\n");

SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
SDL_GL_SetSwapInterval(1);
SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");
// SDL_SetHint(SDL_HINT_GRAB_KEYBOARD, "1");
// SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2");

SDL_PropertiesID props = SDL_CreateProperties();

#ifdef _WIN32
SDL_SetPointerProperty(props, SDL_PROP_WINDOW_CREATE_WIN32_HWND_POINTER,
mainWindow);
#else
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X11_WINDOW_NUMBER,
mainWindow);
if (std::string(SDL_GetCurrentVideoDriver()) == "windows") {
SDL_SetPointerProperty(props, SDL_PROP_WINDOW_CREATE_WIN32_HWND_POINTER,
mainWindow);
}
#endif

#ifdef __linux__
if (std::string(SDL_GetCurrentVideoDriver()) == "x11") {
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X11_WINDOW_NUMBER,
mainWindow);
}
#endif
#ifdef __APPLE__
if (std::string(SDL_GetCurrentVideoDriver()) == "cocoa") {
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_COCOA_WINDOW_POINTER,
mainWindow);
}
#endif
SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_FULLSCREEN_BOOLEAN,
true);

if (renderer_name.find("opengl") != std::string::npos) {
SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_OPENGL_BOOLEAN, true);
}

if (renderer_name == "metal") {
SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_METAL_BOOLEAN, true);
}

SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIDDEN_BOOLEAN, true);
SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_MOUSE_GRABBED_BOOLEAN,
true);

if (renderer_name.find("opengl") != std::string::npos) {
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
}

win = SDL_CreateWindowWithProperties(props);
if (win == nullptr) {
SDL_Log("SDL_CreateWindow failed");
SDL_Quit();
return;
}
SDL_ShowWindow(win);

rend = SDL_CreateRenderer(win, 0);
rend = SDL_CreateRenderer(win, nullptr);

if (rend == nullptr) {
SDL_Log("SDL_CreateRenderer failed");
SDL_Quit();
return;
}

SDL_Log(0);
SDL_Log("Renderer:");
SDL_Log("%s", SDL_GetRendererName(rend));
SDL_Log("\n");
SDL_Log("Done.");
SDL_Log("--------------------------------");
SDL_Log("\n");
SDL_Log(0);
SDL_GetCurrentRenderOutputSize(rend, &w, &h);
SDL_Log("%dx%d", w, h);
SDL_Log(0);
SDL_Log("VSync: ");

int vsync{0};
SDL_GetRenderVSync(rend, &vsync);
if (vsync) {
SDL_Log("on");
} else {
SDL_Log("off");
}

SDL_ShowWindow(win);
SDL_Log("-------------------------------------------------");
SDL_Log("VideoContextSDL::setup() finished successfully");
SDL_Log("-------------------------------------------------");
SDL_Log(0);
SDL_Log(0);
}

void VideoContextSDL::draw(const StaticObject *obj, double timeDiff) noexcept {
Expand Down
1 change: 1 addition & 0 deletions renderer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#opengl

0 comments on commit 235f6b4

Please # to comment.