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

Support pre-compiled headers and interprocedural/link-time optimisation #2978

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
# make
#

cmake_minimum_required(VERSION 3.1)
cmake_minimum_required(VERSION 3.9)

## Project name to use as command prefix.

Expand All @@ -44,6 +44,7 @@ if(COMMAND cmake_policy)
cmake_policy(SET CMP0003 NEW)
cmake_policy(SET CMP0008 NEW)
cmake_policy(SET CMP0023 NEW)
cmake_policy(SET CMP0069 NEW)
endif()

set(CMAKE_CXX_STANDARD 17)
Expand All @@ -63,6 +64,23 @@ include(ExternalProject)
include(CheckCXXCompilerFlag)
include(CheckSymbolExists)

if(EMSCRIPTEN OR NOT (CMAKE_BUILD_TYPE MATCHES "Release|RelWithDebInfo"))
# FIXME: As of writing, Emscripten fails with "wasm-ld: error: /path/to/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/thinlto/libc.a(fileno.o): attempt to add bitcode file after LTO (fileno)"
# Seems to be a known issue: https://github.com/emscripten-core/emscripten/issues/20275
option(SUPERTUX_IPO "Use interprocedural optimisation (Takes more RAM at compilation, gives smaller and faster executables)" OFF)
else()
option(SUPERTUX_IPO "Use interprocedural optimisation (Takes more RAM at compilation, gives smaller and faster executables)" ON)
endif()
if(SUPERTUX_IPO)
include(CheckIPOSupported)
check_ipo_supported(RESULT result)
if(result)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON CACHE BOOL "" FORCE)
endif()
else()
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION OFF CACHE BOOL "" FORCE)
endif()

## For autopackage
set(APPDATADIR "${CMAKE_INSTALL_PREFIX}/share/games/supertux2")

Expand Down Expand Up @@ -232,6 +250,20 @@ add_library(supertux2_c OBJECT ${SUPERTUX_SOURCES_C})
add_library(supertux2_lib STATIC ${CMAKE_BINARY_DIR}/version.h ${SUPERTUX_SOURCES_CXX} ${SUPERTUX_RESOURCES} $<TARGET_OBJECTS:supertux2_c>)
target_include_directories(supertux2_lib PUBLIC ${CMAKE_BINARY_DIR} src/)

# Pre-compiled headers
if (CMAKE_BUILD_TYPE MATCHES "Release|RelWithDebInfo")
option(SUPERTUX_PCH "Pre-compile headers (faster compilation)" ON)
else()
option(SUPERTUX_PCH "Pre-compile headers (faster compilation)" OFF)
endif()
if(SUPERTUX_PCH)
file(GLOB_RECURSE SUPERTUX_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} src/**.hpp)
if(NOT HAVE_OPENGL)
list(FILTER SUPERTUX_HEADERS EXCLUDE REGEX src/video/gl/*)
endif()
target_precompile_headers(supertux2_lib PRIVATE ${SUPERTUX_HEADERS})
endif()

if(WIN32)
add_executable(supertux2 WIN32 src/main.cpp ${CMAKE_CURRENT_SOURCE_DIR}/data/images/engine/icons/supertux.rc)
target_link_libraries(supertux2 LibSDL2main)
Expand Down
4 changes: 4 additions & 0 deletions src/badguy/walking_badguy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

class Timer;

#ifdef _MSC_VER
#undef STRICT
#endif

/** Base class for Badguys that walk on the floor. */
class WalkingBadguy : public BadGuy
{
Expand Down
24 changes: 12 additions & 12 deletions src/editor/scroller_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "video/video_system.hpp"
#include "video/viewport.hpp"

namespace {
namespace CONSTS {

const float TOPLEFT = 16;
const float MIDDLE = 48;
HybridDog marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -46,34 +46,34 @@ EditorScrollerWidget::EditorScrollerWidget(Editor& editor) :
bool
EditorScrollerWidget::can_scroll() const
{
return m_scrolling && m_mouse_pos.x < SIZE && m_mouse_pos.y < SIZE;
return m_scrolling && m_mouse_pos.x < CONSTS::SIZE && m_mouse_pos.y < CONSTS::SIZE;
}

void
EditorScrollerWidget::draw(DrawingContext& context)
{
if (!rendered) return;

context.color().draw_filled_rect(Rectf(Vector(0, 0), Vector(SIZE, SIZE)),
context.color().draw_filled_rect(Rectf(Vector(0, 0), Vector(CONSTS::SIZE, CONSTS::SIZE)),
Color(0.9f, 0.9f, 1.0f, 0.6f),
MIDDLE, LAYER_GUI-10);
CONSTS::MIDDLE, LAYER_GUI-10);
context.color().draw_filled_rect(Rectf(Vector(40, 40), Vector(56, 56)),
Color(0.9f, 0.9f, 1.0f, 0.6f),
8, LAYER_GUI-20);
if (can_scroll()) {
draw_arrow(context, m_mouse_pos);
}

draw_arrow(context, Vector(TOPLEFT, MIDDLE));
draw_arrow(context, Vector(BOTTOMRIGHT, MIDDLE));
draw_arrow(context, Vector(MIDDLE, TOPLEFT));
draw_arrow(context, Vector(MIDDLE, BOTTOMRIGHT));
draw_arrow(context, Vector(CONSTS::TOPLEFT, CONSTS::MIDDLE));
draw_arrow(context, Vector(CONSTS::BOTTOMRIGHT, CONSTS::MIDDLE));
draw_arrow(context, Vector(CONSTS::MIDDLE, CONSTS::TOPLEFT));
draw_arrow(context, Vector(CONSTS::MIDDLE, CONSTS::BOTTOMRIGHT));
}

void
EditorScrollerWidget::draw_arrow(DrawingContext& context, const Vector& pos)
{
Vector dir = pos - Vector(MIDDLE, MIDDLE);
Vector dir = pos - Vector(CONSTS::MIDDLE, CONSTS::MIDDLE);
if (dir.x != 0 || dir.y != 0) {
// draw a triangle
dir = glm::normalize(dir) * 8.0f;
Expand Down Expand Up @@ -105,7 +105,7 @@ EditorScrollerWidget::on_mouse_button_down(const SDL_MouseButtonEvent& button)
if (button.button == SDL_BUTTON_LEFT) {
if (!rendered) return false;

if (m_mouse_pos.x < SIZE && m_mouse_pos.y < SIZE) {
if (m_mouse_pos.x < CONSTS::SIZE && m_mouse_pos.y < CONSTS::SIZE) {
m_scrolling = true;
return true;
} else {
Expand All @@ -122,8 +122,8 @@ EditorScrollerWidget::on_mouse_motion(const SDL_MouseMotionEvent& motion)
if (!rendered) return false;

m_mouse_pos = VideoSystem::current()->get_viewport().to_logical(motion.x, motion.y);
if (m_mouse_pos.x < SIZE && m_mouse_pos.y < SIZE) {
m_scrolling_vec = m_mouse_pos - Vector(MIDDLE, MIDDLE);
if (m_mouse_pos.x < CONSTS::SIZE && m_mouse_pos.y < CONSTS::SIZE) {
m_scrolling_vec = m_mouse_pos - Vector(CONSTS::MIDDLE, CONSTS::MIDDLE);
if (m_scrolling_vec.x != 0 || m_scrolling_vec.y != 0) {
float norm = glm::length(m_scrolling_vec);
m_scrolling_vec *= powf(static_cast<float>(M_E), norm / 16.0f - 1.0f);
Expand Down
7 changes: 7 additions & 0 deletions src/object/text_array_item.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef HEADER_SUPERTUX_OBJECT_TEXT_ARRAY_ITEM_HPP
#define HEADER_SUPERTUX_OBJECT_TEXT_ARRAY_ITEM_HPP

#include "object/text_object.hpp"

struct TextArrayItem
{
TextObject text_object;
Expand All @@ -22,4 +27,6 @@ struct TextArrayItem
TextArrayItem() : text_object(), duration(0.0f) {}
};

#endif

/* EOF */
85 changes: 85 additions & 0 deletions src/port/emscripten.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// SuperTux
// Copyright (C) 2021 A. Semphris <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "port/emscripten.hpp"

#ifdef __EMSCRIPTEN__

#include "addon/addon_manager.hpp"
#include "gui/menu_manager.hpp"
#include "supertux/gameconfig.hpp"
#include "supertux/globals.hpp"
#include "video/video_system.hpp"

extern "C" {

EMSCRIPTEN_KEEPALIVE // This is probably not useful, I just want ppl to know it exists
void
set_resolution(int w, int h)
{
VideoSystem::current()->on_resize(w, h);
MenuManager::instance().on_window_resize();
}

EMSCRIPTEN_KEEPALIVE // Same as above
void
save_config()
{
g_config->save();
}

void
onDownloadProgress(int id, int loaded, int total)
{
AddonManager::current()->onDownloadProgress(id, loaded, total);
}

void
onDownloadFinished(int id)
{
AddonManager::current()->onDownloadFinished(id);
}

void
onDownloadError(int id)
{
AddonManager::current()->onDownloadError(id);
}

void
onDownloadAborted(int id)
{
AddonManager::current()->onDownloadAborted(id);
}

const char*
getExceptionMessage(intptr_t address)
{
return reinterpret_cast<std::exception*>(address)->what();
}

} // extern "C"

void
init_emscripten()
{
EM_ASM({
if (window.supertux_onready)
window.supertux_onready();
}, 0); // EM_ASM is a variadic macro and Clang requires at least 1 value for the variadic argument
}

#endif
63 changes: 4 additions & 59 deletions src/port/emscripten.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,16 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef HEADER_SUPERTUX_PORT_EMSCRIPTEN_HPP
#define HEADER_SUPERTUX_PORT_EMSCRIPTEN_HPP

// Export functions for emscripten
// If you add functions here, make sure to make CMakeLists.txt export them!
#ifdef __EMSCRIPTEN__

#include <emscripten.h>
#include <emscripten/html5.h>

#include "addon/addon_manager.hpp"
#include "gui/menu_manager.hpp"
#include "supertux/gameconfig.hpp"
#include "supertux/globals.hpp"
#include "video/video_system.hpp"

extern "C" {

void set_resolution(int w, int h);
Expand All @@ -38,60 +35,8 @@ void onDownloadError(int id);
void onDownloadAborted(int id);
const char* getExceptionMessage(intptr_t address);

EMSCRIPTEN_KEEPALIVE // This is probably not useful, I just want ppl to know it exists
void
set_resolution(int w, int h)
{
VideoSystem::current()->on_resize(w, h);
MenuManager::instance().on_window_resize();
}

EMSCRIPTEN_KEEPALIVE // Same as above
void
save_config()
{
g_config->save();
}

void
onDownloadProgress(int id, int loaded, int total)
{
AddonManager::current()->onDownloadProgress(id, loaded, total);
}

void
onDownloadFinished(int id)
{
AddonManager::current()->onDownloadFinished(id);
}

void
onDownloadError(int id)
{
AddonManager::current()->onDownloadError(id);
}

void
onDownloadAborted(int id)
{
AddonManager::current()->onDownloadAborted(id);
}

const char*
getExceptionMessage(intptr_t address)
{
return reinterpret_cast<std::exception*>(address)->what();
}

} // extern "C"

void
init_emscripten()
{
EM_ASM({
if (window.supertux_onready)
window.supertux_onready();
}, 0); // EM_ASM is a variadic macro and Clang requires at least 1 value for the variadic argument
}
HybridDog marked this conversation as resolved.
Show resolved Hide resolved
#endif

#endif
5 changes: 5 additions & 0 deletions src/supertux/menu/editor_delete_level_menu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef HEADER_SUPERTUX_SUPERTUX_MENU_EDITOR_DELETE_LEVEL_MENU_HPP
#define HEADER_SUPERTUX_SUPERTUX_MENU_EDITOR_DELETE_LEVEL_MENU_HPP

#include "gui/menu.hpp"

class EditorLevelSelectMenu;
Expand Down Expand Up @@ -43,4 +46,6 @@ class EditorDeleteLevelMenu final : public Menu
EditorDeleteLevelMenu& operator=(const EditorDeleteLevelMenu&) = delete;
};

#endif

/* EOF */
4 changes: 4 additions & 0 deletions src/util/colorspace_oklab.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#ifndef HEADER_SUPERTUX_UTIL_COLORSPACE_OKLAB_HPP
#define HEADER_SUPERTUX_UTIL_COLORSPACE_OKLAB_HPP

class Color;

Expand Down Expand Up @@ -53,4 +55,6 @@ struct ColorOKLCh final {
float L, C, h;
};

#endif

/* EOF */
Loading