Skip to content

Commit

Permalink
Move code from C++ to python
Browse files Browse the repository at this point in the history
Use boost python to get a C++ struct/class from python.
Implement Graphics2Config (temp name) - has resolution and screen number. Populate struct from config.yaml via config.py.
Modify game to take resolution and screen values from config.yaml. Deprecates settings app.
Note: Need to document new install step - pip3 install pyyaml somewhere.
Implement unit tests in configuration.
  • Loading branch information
royfalk committed Oct 10, 2024
1 parent 4354d63 commit 3eced31
Show file tree
Hide file tree
Showing 16 changed files with 273 additions and 70 deletions.
39 changes: 33 additions & 6 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ IF (UNIX)
${Vega_Strike_SOURCE_DIR}/src/damage
${Vega_Strike_SOURCE_DIR}/src/resource
${Vega_Strike_SOURCE_DIR}/src/components
${Vega_Strike_SOURCE_DIR}/src/python/config
${Vega_Strike_BINARY_DIR}
${Vega_Strike_BINARY_DIR}/src
/usr/include/harfbuzz/
Expand All @@ -177,6 +178,7 @@ ELSE ()
${Vega_Strike_SOURCE_DIR}/src/damage
${Vega_Strike_SOURCE_DIR}/src/resource
${Vega_Strike_SOURCE_DIR}/src/components
${Vega_Strike_SOURCE_DIR}/src/python/config
${Vega_Strike_BINARY_DIR}
${Vega_Strike_BINARY_DIR}/src
)
Expand Down Expand Up @@ -374,6 +376,7 @@ IF (Python3_FOUND)
SET(TST_INCLUDES ${Python3_INCLUDE_DIRS})
SET(TST_LIBS ${Python3_LIBRARIES})
SET(HAVE_PYTHON 1)
add_compile_definitions(Python_SITELIB=${Python3_SITELIB})
ELSE (Python3_FOUND)
MESSAGE(FATAL_ERROR "Can't find python")
ENDIF (Python3_FOUND)
Expand Down Expand Up @@ -712,11 +715,15 @@ SET(LIBRESOURCE
src/resource/product.cpp
src/resource/cargo.cpp
src/resource/manifest.cpp
src/resource/python_utils.cpp
src/resource/random_utils.cpp
src/cmd/json.cpp
)

SET(LIBPYTHON
src/python/config/python_utils.cpp
src/python/config/graphics_config.cpp
)

SET(LIBCOMPONENT
src/components/component.cpp

Expand Down Expand Up @@ -1130,7 +1137,21 @@ SET(LIBAUDIO_SOURCES
)


ADD_LIBRARY(vegastrike_python SHARED
${LIBPYTHON}
)

TARGET_LINK_LIBRARIES(
vegastrike_python
${Boost_LIBRARIES}
${Python3_LIBRARIES}
)

# Name must be witout the lib prefix
SET_TARGET_PROPERTIES(vegastrike_python PROPERTIES PREFIX "")

ADD_LIBRARY(vegastrike-engine_com
${LIBPYTHON}
${LIBVS_LOGGING}
${LIBCONFIG}
${LIBDAMAGE}
Expand Down Expand Up @@ -1721,13 +1742,14 @@ IF (USE_GTEST)
src/resource/tests/resource_test.cpp
src/resource/tests/manifest_tests.cpp
src/resource/tests/random_tests.cpp
src/resource/tests/python_tests.cpp
src/configuration/tests/python_tests.cpp
src/exit_unit_tests.cpp
src/components/tests/energy_container_tests.cpp
src/components/tests/balancing_tests.cpp
)

ADD_LIBRARY(vegastrike-testing
${LIBPYTHON}
${LIBCONFIG}
${LIBDAMAGE}
${LIBRESOURCE}
Expand Down Expand Up @@ -1762,18 +1784,23 @@ IF (USE_GTEST)
ENDIF()

FILE(
COPY "src/configuration/tests/vegastrike.config"
COPY "src/cmd/tests/units.json"
DESTINATION ${CMAKE_BINARY_DIR}/test_assets
)

FILE(
COPY "src/cmd/tests/units.json"
COPY "src/configuration/tests/python_tests.py"
DESTINATION ${CMAKE_BINARY_DIR}/test_assets
)

FILE(
COPY "src/components/tests/python_tests.py"
DESTINATION ${CMAKE_BINARY_DIR}
COPY "src/configuration/tests/config.py"
DESTINATION ${CMAKE_BINARY_DIR}/test_assets
)

FILE(
COPY "src/configuration/tests/config.yaml"
DESTINATION ${CMAKE_BINARY_DIR}/test_assets
)

INCLUDE(GoogleTest)
Expand Down
6 changes: 6 additions & 0 deletions engine/src/configuration/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@
#endif
#include <math.h>

#include "vsfilesystem.h"

using vega_config::GetGameConfig;

Configuration::Configuration() {
//logging.verbose_debug = GetGameConfig().GetBool("data.verbose_debug", false);
graphics2_config = GetGraphics2Config(
VSFileSystem::programdir,
VSFileSystem::datadir,
"config", "get_config");
}

/* Override the default value(provided by constructor) with the value from the user specified configuration file, if any.
Expand Down
3 changes: 3 additions & 0 deletions engine/src/configuration/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <memory>
#include <string>

#include "python/config/graphics_config.h"

namespace vega_config {
// Config Structs Declaration

Expand Down Expand Up @@ -449,6 +451,7 @@ class Configuration {
Configuration();
void OverrideDefaultsWithUserConfiguration();
vega_config::GeneralConfig general_config;
Graphics2Config graphics2_config;
vega_config::DataConfig data_config;
vega_config::AIConfig ai;
vega_config::AudioConfig audio_config;
Expand Down
24 changes: 24 additions & 0 deletions engine/src/configuration/tests/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import yaml
import vegastrike_python


def get_config():
with open('config.yaml', 'r') as file:
yaml_file = yaml.safe_load(file)
print(yaml_file)

graphics = vegastrike_python.GraphicsConfig()
graphics.screen = yaml_file['graphics']['screen']
graphics.resolution_x = yaml_file['graphics']['resolution_x']
graphics.resolution_y = yaml_file['graphics']['resolution_y']


return graphics


#gfx = get_config()
#print('screen: ', gfx.screen)
#print('x: ', gfx.resolution_x)
#print('y: ', gfx.resolution_y)


8 changes: 8 additions & 0 deletions engine/src/configuration/tests/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
general:

graphics:
resolution_x: 2560
resolution_y: 1600
screen: 0

advanced:
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@
#include <boost/filesystem.hpp>
#include <iostream>

#include "python_utils.h"
#include "python/config/python_utils.h"
#include "python/config/graphics_config.h"

using namespace boost::python;

// This code shows how to call a python file from c++
// It is not portable

struct World
{
Expand Down Expand Up @@ -86,15 +85,6 @@ TEST(Python, Call_Function) {

Py_Initialize();

// Use the following code to figure out your original path, as the above Py_SetPath overrides it.
// Note that Py_GetPath must be called after Py_Initialize and Py_SetPath before.
/*wchar_t* w_path_ptr = Py_GetPath();
std::wstring w_path_w( w_path_ptr );
std::string path( w_path_w.begin(), w_path_w.end() );
std::cout << "Path: " << path << std::endl;*/

//PyObject* moduleString = PyUnicode_FromString((char*)"python_tests");
//PyObject* module = PyImport_Import(moduleString);
PyObject* module = PyImport_ImportModule("python_tests");
std::cout << "PyImport_ImportModule did not crash\n" << std::flush;
if(!module) {
Expand Down Expand Up @@ -140,4 +130,25 @@ TEST(Python, Call_Function) {

// Uncomment to see prints
//EXPECT_FALSE(true);
}
}

// Test ability to get config file from python
TEST(Python, Config) {
boost::filesystem::path test_path(boost::filesystem::current_path());
boost::filesystem::path lib_path = test_path.parent_path();
std::cout << "test_path: " << test_path << std::endl;
std::cout << "lib_path: " << lib_path << std::endl;

PyObject* object = GetClassFromPython(
lib_path.string(),
test_path.string(),
"config", "get_config");

Graphics2Config& cfg2 = extract<Graphics2Config&>(object);
EXPECT_EQ(cfg2.screen, 0);
EXPECT_EQ(cfg2.resolution_x, 2560);
EXPECT_EQ(cfg2.resolution_y, 1600);

// Uncomment to see prints*/
//EXPECT_FALSE(true);
}
File renamed without changes.
32 changes: 0 additions & 32 deletions engine/src/configuration/tests/vegastrike.config

This file was deleted.

7 changes: 3 additions & 4 deletions engine/src/gldrv/winsys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,9 @@ static bool setup_sdl_video_mode(int *argc, char **argv) {
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
}
//#endif
width = g_game.x_resolution;
height = g_game.y_resolution;

const int screen_number = configuration()->general_config.screen;
width = configuration()->graphics2_config.resolution_x;
height = configuration()->graphics2_config.resolution_y;
const int screen_number = configuration()->graphics2_config.screen;
SDL_Window *window = nullptr;
if(screen_number == 0) {
window = SDL_CreateWindow("Vegastrike", 0, 0, width, height, video_flags);
Expand Down
2 changes: 2 additions & 0 deletions engine/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ int main(int argc, char *argv[]) {
const boost::filesystem::path program_name{program_path.filename()}; //canonical_program_path.filename();
const boost::filesystem::path program_directory_path{program_path.parent_path()};

VSFileSystem::programdir = program_directory_path.string();

// This will be set later
boost::filesystem::path home_subdir_path{};

Expand Down
30 changes: 30 additions & 0 deletions engine/src/python/config/graphics_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <string>
#include <boost/python.hpp>
#include <boost/filesystem.hpp>

#include "graphics_config.h"
#include "python_utils.h"



BOOST_PYTHON_MODULE(vegastrike_python) {
boost::python::class_<Graphics2Config>("GraphicsConfig", boost::python::init<>())
.def_readwrite("screen", &Graphics2Config::screen)
.def_readwrite("resolution_x", &Graphics2Config::resolution_x)
.def_readwrite("resolution_y", &Graphics2Config::resolution_y);
}



Graphics2Config GetGraphics2Config(
const std::string build_path,
const std::string path_string,
const std::string file_name,
const std::string function_name) {
PyObject* object = GetClassFromPython(
build_path, path_string, file_name, function_name);


Graphics2Config cfg2 = boost::python::extract<Graphics2Config>(object);
return cfg2;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* python_utils.cpp
* graphics_config.h
*
* Copyright (c) 2001-2002 Daniel Horn
* Copyright (c) 2002-2019 pyramid3d and other Vega Strike Contributors
Expand All @@ -25,21 +25,23 @@

// -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-

#include "python_utils.h"
#ifndef VEGA_STRIKE_ENGINE_PYTHON_CONFIG_GRAPHICS_CONFIG_H
#define VEGA_STRIKE_ENGINE_PYTHON_CONFIG_GRAPHICS_CONFIG_H

#include <boost/python.hpp>
#include <iostream>
// TODO: remove the other GraphicsConfig and rename this
struct Graphics2Config {
int screen{0};
int resolution_x{1920};
int resolution_y{1080};

using namespace boost::python;
Graphics2Config() = default;
};

const std::string GetPythonPath() {
Py_Initialize();
wchar_t* w_path_ptr = Py_GetPath();
Py_Finalize();

std::wstring w_path_w( w_path_ptr );
std::string path( w_path_w.begin(), w_path_w.end() );
std::cout << "Python path: " << path << std::endl;
Graphics2Config GetGraphics2Config(
const std::string build_path,
const std::string path_string,
const std::string file_name,
const std::string function_name
);

return path;
}
#endif // VEGA_STRIKE_ENGINE_PYTHON_CONFIG_GRAPHICS_CONFIG_H
Loading

0 comments on commit 3eced31

Please sign in to comment.