Skip to content

Commit

Permalink
feat(gamepp): create C wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
craftablescience committed Oct 23, 2024
1 parent 98a047f commit 578a7d1
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ endif()
# Add libraries
add_sourcepp_library(bsppp NO_TEST ) # sourcepp::bsppp
add_sourcepp_library(dmxpp ) # sourcepp::dmxpp
add_sourcepp_library(gamepp PYTHON ) # sourcepp::gamepp
add_sourcepp_library(gamepp C PYTHON ) # sourcepp::gamepp
add_sourcepp_library(kvpp BENCH) # sourcepp::kvpp
add_sourcepp_library(mdlpp ) # sourcepp::mdlpp
add_sourcepp_library(steampp C PYTHON ) # sourcepp::steampp
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
<td>Get Source engine instance window title/position/size</td>
<td align="center">✅</td>
<td align="center">❌</td>
<td rowspan="3" align="center">Python</td>
<td rowspan="3" align="center">C<br>Python</td>
</tr>
<tr><!-- empty row to disable github striped bg color --></tr>
<tr>
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
<td>Get Source engine instance window title/position/size</td>
<td align="center">✅</td>
<td align="center">❌</td>
<td rowspan="2" align="center">Python</td>
<td rowspan="2" align="center">C<br>Python</td>
</tr>
<tr>
<td>Run commands in a Source engine instance remotely</td>
Expand Down
23 changes: 23 additions & 0 deletions lang/c/include/gameppc/Convert.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

/*
* This is a header designed to be included in C++ source code.
* It should not be included in applications using any C wrapper libraries!
*/
#ifndef __cplusplus
#error "This header can only be used in C++!"
#endif

#include "gamepp.h"

namespace gamepp {

class GameInstance;

} // namespace gamepp

namespace Convert {

gamepp::GameInstance* gameInstance(gamepp_game_instance_handle_t handle);

} // namespace Convert
45 changes: 45 additions & 0 deletions lang/c/include/gameppc/gamepp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

#include <sourceppc/Buffer.h>
#include <sourceppc/String.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef void* gamepp_game_instance_handle_t;

#ifdef __cplusplus
} // extern "C"
#endif

// REQUIRES MANUAL FREE: gamepp_game_instance_free
SOURCEPP_API gamepp_game_instance_handle_t gamepp_find_game_instance();

// REQUIRES MANUAL FREE: gamepp_game_instance_free
SOURCEPP_API gamepp_game_instance_handle_t gamepp_find_game_instance_with_name(const char* windowNameOverride);

SOURCEPP_API void gamepp_game_instance_free(gamepp_game_instance_handle_t* handle);

// REQUIRES MANUAL FREE: sourcepp_string_free
SOURCEPP_API sourcepp_string_t gamepp_get_window_title(gamepp_game_instance_handle_t handle);

SOURCEPP_API int gamepp_get_window_pos_x(gamepp_game_instance_handle_t handle);

SOURCEPP_API int gamepp_get_window_pos_y(gamepp_game_instance_handle_t handle);

SOURCEPP_API int gamepp_get_window_width(gamepp_game_instance_handle_t handle);

SOURCEPP_API int gamepp_get_window_height(gamepp_game_instance_handle_t handle);

SOURCEPP_API void gamepp_command(gamepp_game_instance_handle_t handle, const char* command);

SOURCEPP_API void gamepp_input_begin(gamepp_game_instance_handle_t handle, const char* input);

SOURCEPP_API void gamepp_input_end(gamepp_game_instance_handle_t handle, const char* input);

SOURCEPP_API void gamepp_input_once(gamepp_game_instance_handle_t handle, const char* input);

SOURCEPP_API void gamepp_input_hold(gamepp_game_instance_handle_t handle, const char* input, double sec);

SOURCEPP_API void gamepp_wait(gamepp_game_instance_handle_t handle, double sec);
9 changes: 9 additions & 0 deletions lang/c/src/gameppc/Convert.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <gameppc/Convert.hpp>

#include <gamepp/gamepp.h>

using namespace gamepp;

GameInstance* Convert::gameInstance(gamepp_game_instance_handle_t handle) {
return static_cast<GameInstance*>(handle);
}
6 changes: 6 additions & 0 deletions lang/c/src/gameppc/_gameppc.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add_pretty_parser(gamepp C
SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/lang/c/include/gameppc/Convert.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/lang/c/include/gameppc/gamepp.h"
"${CMAKE_CURRENT_LIST_DIR}/Convert.cpp"
"${CMAKE_CURRENT_LIST_DIR}/gamepp.cpp")
98 changes: 98 additions & 0 deletions lang/c/src/gameppc/gamepp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <gameppc/gamepp.h>

#include <gamepp/gamepp.h>

#include <gameppc/Convert.hpp>
#include <sourceppc/Convert.hpp>
#include <sourceppc/Helpers.h>

using namespace gamepp;

SOURCEPP_API gamepp_game_instance_handle_t gamepp_find_game_instance() {
auto instance = GameInstance::find();
if (!instance) {
return nullptr;
}
return new GameInstance{*instance};
}

SOURCEPP_API gamepp_game_instance_handle_t gamepp_find_game_instance_with_name(const char* windowNameOverride) {
auto instance = GameInstance::find(windowNameOverride);
if (!instance) {
return nullptr;
}
return new GameInstance{*instance};
}

SOURCEPP_API void gamepp_game_instance_free(gamepp_game_instance_handle_t* handle) {
SOURCEPP_EARLY_RETURN(handle);

delete Convert::gameInstance(*handle);
*handle = nullptr;
}

SOURCEPP_API sourcepp_string_t gamepp_get_window_title(gamepp_game_instance_handle_t handle) {
SOURCEPP_EARLY_RETURN_VAL(handle, SOURCEPP_STRING_INVALID);

return Convert::toString(Convert::gameInstance(handle)->getWindowTitle());
}

SOURCEPP_API int gamepp_get_window_pos_x(gamepp_game_instance_handle_t handle) {
SOURCEPP_EARLY_RETURN_VAL(handle, 0);

return Convert::gameInstance(handle)->getWindowPos()[0];
}

SOURCEPP_API int gamepp_get_window_pos_y(gamepp_game_instance_handle_t handle) {
SOURCEPP_EARLY_RETURN_VAL(handle, 0);

return Convert::gameInstance(handle)->getWindowPos()[1];
}

SOURCEPP_API int gamepp_get_window_width(gamepp_game_instance_handle_t handle) {
SOURCEPP_EARLY_RETURN_VAL(handle, 0);

return Convert::gameInstance(handle)->getWindowSize()[0];
}

SOURCEPP_API int gamepp_get_window_height(gamepp_game_instance_handle_t handle) {
SOURCEPP_EARLY_RETURN_VAL(handle, 0);

return Convert::gameInstance(handle)->getWindowSize()[1];
}

SOURCEPP_API void gamepp_command(gamepp_game_instance_handle_t handle, const char* command) {
SOURCEPP_EARLY_RETURN(handle);

Convert::gameInstance(handle)->command(command);
}

SOURCEPP_API void gamepp_input_begin(gamepp_game_instance_handle_t handle, const char* input) {
SOURCEPP_EARLY_RETURN(handle);

Convert::gameInstance(handle)->inputBegin(input);
}

SOURCEPP_API void gamepp_input_end(gamepp_game_instance_handle_t handle, const char* input) {
SOURCEPP_EARLY_RETURN(handle);

Convert::gameInstance(handle)->inputEnd(input);
}

SOURCEPP_API void gamepp_input_once(gamepp_game_instance_handle_t handle, const char* input) {
SOURCEPP_EARLY_RETURN(handle);

Convert::gameInstance(handle)->inputOnce(input);
}

SOURCEPP_API void gamepp_input_hold(gamepp_game_instance_handle_t handle, const char* input, double sec) {
SOURCEPP_EARLY_RETURN(handle);

Convert::gameInstance(handle)->inputHold(input, sec);
}

SOURCEPP_API void gamepp_wait(gamepp_game_instance_handle_t handle, double sec) {
SOURCEPP_EARLY_RETURN(handle);

Convert::gameInstance(handle)->wait(sec);
}

0 comments on commit 578a7d1

Please sign in to comment.