Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring of PCMockup/CMake #41

Merged
merged 11 commits into from
Nov 23, 2018
154 changes: 28 additions & 126 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
cmake_minimum_required (VERSION 3.9.2)
project(pebbleofdoom)

###################################################################
# Dependencies
###################################################################

set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/external/" ${CMAKE_MODULE_PATH})

find_package(SDL2 REQUIRED)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 11)

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
link_libraries(m)
endif()

include(CTest)
include(GoogleTest)

if (MSVC AND CMAKE_C_COMPILER_ID STREQUAL "MSVC")
message(FATAL_ERROR " Microsoft C Compiler is not supported, please use GCC or Clang")
endif()

###################################################################
# Utilities
###################################################################
Expand All @@ -40,123 +19,46 @@ function(assign_source_group)
endforeach()
endfunction(assign_source_group)

# To add googletests in the best way regarding cmake version
function(add_googletest TARGET PREFIX)
if (CMAKE_VERSION VERSION_LESS "3.10.0")
gtest_add_tests(TARGET ${TARGET} TEST_PREFIX ${PREFIX})
# To enable many many warnings and treat them as errors (portable across msvc/gcc)
function(enable_warnings TARGET)
if (MSVC)
target_compile_options(${TARGET} PRIVATE "/W4" "/WX")
else()
gtest_discover_tests(${TARGET} TEST_PREFIX ${PREFIX})
target_compile_options(${TARGET} PRIVATE
"-Werror" "-Wall" "-Wextra"
"-Wpointer-arith" "-Wlogical-op"
"-Wdisabled-optimization" "-Wunsafe-loop-optimizations"
)
endif()
endfunction(add_googletest)
endfunction(enable_warnings)

###################################################################
# googletest
# Dependencies
###################################################################

configure_file(external/GTest.CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()

# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/external/" ${CMAKE_MODULE_PATH})

# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
${CMAKE_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
find_package(SDL2 REQUIRED)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 11)

# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories("${gtest_SOURCE_DIR}/include")
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
link_libraries(m)
endif()

###################################################################
# PoD renderer
###################################################################

set(sources_podrenderer
renderer/renderer.h
renderer/renderer.c
renderer/renderer_debug.c
renderer/algebra_float.c
renderer/algebra.h
renderer/algebra.c

pcmockup/pebble.c
)
assign_source_group(${sources_podrenderer})

add_library(podrenderer ${sources_podrenderer})
target_include_directories(podrenderer PUBLIC
"pcmockup/" # to access pebble.h
${SDL2_INCLUDE_DIR} # for debug output
)
target_compile_definitions(podrenderer
PUBLIC REAL_USE_FLOAT
PUBLIC DEBUG_WINDOWS
)

###################################################################
# tests
###################################################################

set(sources_test_podrenderer
test/fixtures.h
test/test_real.cpp
test/test_vector.cpp
test/test_intersection.cpp
test/test_integer.cpp
)
assign_source_group(${sources_test_podrenderer})
include(CTest)
include(GoogleTest)
include("${CMAKE_SOURCE_DIR}/cmake/GTest.cmake")
include("${CMAKE_SOURCE_DIR}/cmake/stb.cmake")

add_executable(test_podrenderer ${sources_test_podrenderer})
target_link_libraries(test_podrenderer
podrenderer
gtest_main
)
add_googletest(test_podrenderer "def.")
if (MSVC AND CMAKE_C_COMPILER_ID STREQUAL "MSVC")
message(FATAL_ERROR " Microsoft C Compiler is not supported, please use GCC or Clang")
endif()

###################################################################
# PC Mockup
# sub-directories
###################################################################

set(sources_pcmockup
pcmockup/pebble.h
pcmockup/sdl.include.h
pcmockup/stb_impl.c

pcmockup/pcmockup.h
pcmockup/pcmockup.c
pcmockup/pebblewindow.c
pcmockup/debugwindow.c
pcmockup/debugwindowset.c
pcmockup/windowgrid.c
)
assign_source_group(${sources_pcmockup})

add_executable(pcmockup ${sources_pcmockup})
add_dependencies(pcmockup
podrenderer
)
target_link_libraries(pcmockup
${SDL2_LIBRARY}
podrenderer
)
target_include_directories(pcmockup PUBLIC
"external/stb/"
"renderer/"
)
add_subdirectory("renderer")
add_subdirectory("test")
add_subdirectory("pcmockup")
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ As such a typical CMake call on Windows to develop this project looks like this:
cmake -DSDL2_PATH="C:/libs/SDL2-2.0.5/" -T llvm ..
```

You might have to copy your `SDL.dll` in the `build/Debug` or `build/Release` directory to start pcmockup.
You might have to copy your `SDL.dll` in the `build/pcmockup/Debug` or `build/pcmockup/Release` directory to start pcmockup.

### With make/gcc

Expand Down
41 changes: 41 additions & 0 deletions cmake/GTest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Configures googletest for use in PebbleOfDoom

configure_file(external/GTest.CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()

# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
${CMAKE_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)

# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories("${gtest_SOURCE_DIR}/include")
endif()

# To add googletests in the best way regarding cmake version
function(add_googletest TARGET PREFIX)
if (CMAKE_VERSION VERSION_LESS "3.10.0")
gtest_add_tests(TARGET ${TARGET} TEST_PREFIX ${PREFIX})
else()
gtest_discover_tests(${TARGET} TEST_PREFIX ${PREFIX})
endif()
endfunction(add_googletest)
22 changes: 22 additions & 0 deletions cmake/stb.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Configures stb for use in PebbleOfDoom

set(STB_DIR "${CMAKE_SOURCE_DIR}/external/stb")
if (NOT EXISTS "${STB_DIR}/stb_image.h")
message(FATAL_ERROR "Could not find stb_image, did you clone the submodules?")
endif()

set(sources_stb
pcmockup/stb_impl.c
${STB_DIR}/stb_image.h
${STB_DIR}/stb_image_write.h
)
assign_source_group(${sources_stb})

add_library(stb ${sources_stb})
target_include_directories(stb PUBLIC
${STB_DIR}
"pcmockup"
)

set(STB_LIBRARY "stb")
set(STB_INCLUDE_DIR ${STB_DIR})
17 changes: 9 additions & 8 deletions external/GTest.CMakeLists.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ project(googletest-download NONE)

include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
UPDATE_DISCONNECTED 1
)
27 changes: 27 additions & 0 deletions pcmockup/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
###################################################################
# PC Mockup
###################################################################

set(sources_pcmockup
pebble.h
sdl.include.h
stb.include.h
pcmockup.h
pcmockup.c
pebblewindow.c
debugwindow.c
debugwindowset.c
windowgrid.c
)
assign_source_group(${sources_pcmockup})

add_executable(pcmockup ${sources_pcmockup})
target_link_libraries(pcmockup
${SDL2_LIBRARY}
${STB_LIBRARY}
podrenderer
)
target_include_directories(pcmockup
PRIVATE ${SDL2_INCLUDE_DIR}
)
enable_warnings(pcmockup)
31 changes: 17 additions & 14 deletions pcmockup/debugwindow.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "pcmockup.h"
#include "../renderer/renderer.h"
#include "renderer.h"

#include <string.h>

Expand All @@ -9,19 +9,20 @@ struct DebugWindow
SDL_Renderer* renderer;
SDL_Texture* texture;
SDL_Rect texturePos;
DebugInfo info;
xz_t position;
Renderer* podRenderer;
const DebugView* view;
xz_t position, offset;
real_t zoom;
};

DebugWindow* debugWindow_init(SDL_Rect bounds, int index, const char* title)
DebugWindow* debugWindow_init(SDL_Rect bounds, const DebugView* view, Renderer* podRenderer)
{
DebugWindow* me = (DebugWindow*)malloc(sizeof(DebugWindow));
if (me == NULL)
return NULL;
memset(me, 0, sizeof(DebugWindow));

me->window = SDL_CreateWindow(title,
me->window = SDL_CreateWindow(view->name,
bounds.x, bounds.y,
bounds.w, bounds.h,
SDL_WINDOW_RESIZABLE);
Expand All @@ -42,9 +43,9 @@ DebugWindow* debugWindow_init(SDL_Rect bounds, int index, const char* title)

me->position = xz(real_zero, real_zero);
me->zoom = real_one;
me->info.index = index;
me->info.offset = xz(real_from_int(bounds.w / 2), real_from_int(bounds.h / 2));
me->info.ren = me->renderer;
me->offset = xz(real_from_int(bounds.w / 2), real_from_int(bounds.h / 2));
me->podRenderer = podRenderer;
me->view = view;
return me;
}

Expand Down Expand Up @@ -104,6 +105,13 @@ void debugWindow_endUpdate(DebugWindow* me)
SDL_RenderPresent(me->renderer);
}

void debugWindow_update(DebugWindow* me)
{
debugWindow_startUpdate(me);
me->view->callback.sdl(me->podRenderer, me->renderer, me->offset, me->view->userdata);
debugWindow_endUpdate(me);
}

void debugWindow_handleEvent(DebugWindow* me, const SDL_Event* ev)
{
Uint32 windowID = SDL_GetWindowID(me->window);
Expand All @@ -128,13 +136,8 @@ void debugWindow_handleEvent(DebugWindow* me, const SDL_Event* ev)
int textureW, textureH;
SDL_QueryTexture(me->texture, NULL, NULL, &textureW, &textureH);
xz_t halfSize = xz(real_from_int(textureW / 2), real_from_int(textureH / 2));
me->info.offset = xz_sub(
me->offset = xz_sub(
xz_invScale(halfSize, me->zoom),
me->position
);
}

const DebugInfo* debugWindow_getDebugInfo(DebugWindow* me)
{
return &me->info;
}
Loading