Skip to content

Commit

Permalink
curl support
Browse files Browse the repository at this point in the history
Added curl support to perform HTTP(S) requests

This feature can be enabled by setting -DBINOCLE_HTTP=1 when invoking cmake.
  • Loading branch information
tanis2000 authored May 8, 2023
1 parent e430cfb commit ec6cb67
Show file tree
Hide file tree
Showing 16 changed files with 571 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ jobs:
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: |
ls -al /usr/local/opt/openssl@3/
export PKG_CONFIG_PATH="/usr/local/opt/openssl@3/lib/pkgconfig"
cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DBINOCLE_HTTP=1
- name: Build
shell: bash
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: |
source emsdk_env.sh
emcmake cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
emcmake cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DBINOCLE_HTTP=1
- name: Build
shell: bash
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: |
cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DBINOCLE_HTTP=1
- name: Build
shell: powershell
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,27 @@ open http://localhost:8000/ExampleProject.html

To generate the documentation just add the `-DBUILD_DOC=ON` option when running cmake.

### HTTP Support

Binocle supports HTTP requests through curl. The feature can be enabled by setting BINOCLE_HTTP.
This is only supported on desktop systems and the browser. There is no support planned for iOS and Android at the moment.

#### macOS additional requirements

On macOS, when enabling HTTP support, you have to provide the path to OpenSSL as it will be linked as a dynamic library.
In order to do that please do the following before running cmake.

On Intel Mac:
```shell
export PKG_CONFIG_PATH=/usr/local/opt/openssl@3/lib/pkgconfig
```

On ARM Mac:
```shell
export PKG_CONFIG_PATH=/opt/homebrew/opt/openssl@3/lib/pkgconfig
```


## Credits

Many of the concepts come from Matt Thorson's Monocle engine (the name of this project is a kind of joke around Matt's engine name as you can guess).
Expand Down
8 changes: 4 additions & 4 deletions cmake/BinocleWeb.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ set (CMAKE_EXECUTABLE_SUFFIX ".html")
if (DEBUG)
message("Compiling for EMSCRIPTEN in Debug mode")
#it might be worth disabling WASM for debugging (-s WASM=0) but I'm not even sure it can be of any help today
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -s ASSERTIONS=2 -s USE_SDL=2 -s USE_ZLIB=1 -s USE_LIBPNG=1 -s USE_VORBIS=1 -s USE_OGG=1 -s STB_IMAGE=1 -sFETCH -s INITIAL_MEMORY=1073741824 -s DISABLE_EXCEPTION_CATCHING=0 -s NO_EXIT_RUNTIME=1 -s USE_WEBGL2=1 -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sFORCE_FILESYSTEM=1 -sSTACK_SIZE=128000 --preload-file ${BINOCLE_DATA_DIR}@/assets/ --shell-file ${BINOCLE_DATA_DIR}index.html --use-preload-plugins -Wno-invalid-offsetof -std=gnu++0x")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -s ASSERTIONS=2 -s USE_SDL=2 -s USE_ZLIB=1 -s USE_LIBPNG=1 -s USE_VORBIS=1 -s USE_OGG=1 -s STB_IMAGE=1 -sFETCH -s INITIAL_MEMORY=1073741824 -s DISABLE_EXCEPTION_CATCHING=0 -s NO_EXIT_RUNTIME=1 -s USE_WEBGL2=1 -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sFORCE_FILESYSTEM=1 -sSTACK_SIZE=128000 --preload-file ${BINOCLE_DATA_DIR}@/assets/ --shell-file ${BINOCLE_DATA_DIR}index.html --use-preload-plugins")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -s ASSERTIONS=2 -s USE_SDL=2 -s USE_ZLIB=1 -s USE_LIBPNG=1 -s USE_VORBIS=1 -s USE_OGG=1 -s STB_IMAGE=1 -sFETCH -s INITIAL_MEMORY=1073741824 -s DISABLE_EXCEPTION_CATCHING=0 -s NO_EXIT_RUNTIME=1 -s USE_WEBGL2=1 -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sFORCE_FILESYSTEM=1 -sASYNCIFY -sSTACK_SIZE=128000 --preload-file ${BINOCLE_DATA_DIR}@/assets/ --shell-file ${BINOCLE_DATA_DIR}index.html --use-preload-plugins -Wno-invalid-offsetof -std=gnu++0x")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -s ASSERTIONS=2 -s USE_SDL=2 -s USE_ZLIB=1 -s USE_LIBPNG=1 -s USE_VORBIS=1 -s USE_OGG=1 -s STB_IMAGE=1 -sFETCH -s INITIAL_MEMORY=1073741824 -s DISABLE_EXCEPTION_CATCHING=0 -s NO_EXIT_RUNTIME=1 -s USE_WEBGL2=1 -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sFORCE_FILESYSTEM=1 -sASYNCIFY -sSTACK_SIZE=128000 --preload-file ${BINOCLE_DATA_DIR}@/assets/ --shell-file ${BINOCLE_DATA_DIR}index.html --use-preload-plugins")
else()
message("Compiling for EMSCRIPTEN in Release mode")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O -s USE_SDL=2 -s USE_ZLIB=1 -s USE_LIBPNG=1 -s USE_VORBIS=1 -s USE_OGG=1 -s STB_IMAGE=1 -sFETCH -s INITIAL_MEMORY=1073741824 -s DISABLE_EXCEPTION_CATCHING=0 -s NO_EXIT_RUNTIME=1 -s USE_WEBGL2=1 -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sFORCE_FILESYSTEM=1 -sSTACK_SIZE=128000 --preload-file ${BINOCLE_DATA_DIR}@/assets/ --shell-file ${BINOCLE_DATA_DIR}index.html --use-preload-plugins -Wno-invalid-offsetof -std=gnu++0x")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O -s USE_SDL=2 -s USE_ZLIB=1 -s USE_LIBPNG=1 -s USE_VORBIS=1 -s USE_OGG=1 -s STB_IMAGE=1 -sFETCH -s INITIAL_MEMORY=1073741824 -s DISABLE_EXCEPTION_CATCHING=0 -s NO_EXIT_RUNTIME=1 -s USE_WEBGL2=1 -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sFORCE_FILESYSTEM=1 -sSTACK_SIZE=128000 --preload-file ${BINOCLE_DATA_DIR}@/assets/ --shell-file ${BINOCLE_DATA_DIR}index.html --use-preload-plugins")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O -s USE_SDL=2 -s USE_ZLIB=1 -s USE_LIBPNG=1 -s USE_VORBIS=1 -s USE_OGG=1 -s STB_IMAGE=1 -sFETCH -s INITIAL_MEMORY=1073741824 -s DISABLE_EXCEPTION_CATCHING=0 -s NO_EXIT_RUNTIME=1 -s USE_WEBGL2=1 -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sFORCE_FILESYSTEM=1 -sASYNCIFY -sSTACK_SIZE=128000 --preload-file ${BINOCLE_DATA_DIR}@/assets/ --shell-file ${BINOCLE_DATA_DIR}index.html --use-preload-plugins -Wno-invalid-offsetof -std=gnu++0x")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O -s USE_SDL=2 -s USE_ZLIB=1 -s USE_LIBPNG=1 -s USE_VORBIS=1 -s USE_OGG=1 -s STB_IMAGE=1 -sFETCH -s INITIAL_MEMORY=1073741824 -s DISABLE_EXCEPTION_CATCHING=0 -s NO_EXIT_RUNTIME=1 -s USE_WEBGL2=1 -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sFORCE_FILESYSTEM=1 -sASYNCIFY -sSTACK_SIZE=128000 --preload-file ${BINOCLE_DATA_DIR}@/assets/ --shell-file ${BINOCLE_DATA_DIR}index.html --use-preload-plugins")
message("EMCC settings: ${CMAKE_C_FLAGS}")
endif ()

Expand Down
63 changes: 62 additions & 1 deletion example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
if (${BINOCLE_HTTP})
message(STATUS "HTTP support enabled")
add_definitions(-DBINOCLE_HTTP)
endif ()

if (NOT BINOCLE_DATA_DIR)
message("Setting BINOCLE_DATA_DIR to the default value")
set(BINOCLE_DATA_DIR "${CMAKE_SOURCE_DIR}/assets/")
endif ()

message("Binocle data dir: ${BINOCLE_DATA_DIR}")
message("Project source dir: ${PROJECT_SOURCE_DIR}")

include_directories(${CMAKE_SOURCE_DIR}/src)
include_directories(
${CMAKE_SOURCE_DIR}/src/deps
Expand Down Expand Up @@ -72,7 +85,22 @@ else ()
add_executable(ExampleProject MACOSX_BUNDLE ${EXE_TYPE} ${SOURCE_FILES} ${BINOCLE_ICON} ${RESOURCES})
endif ()

target_link_libraries(ExampleProject ${BINOCLE_LINK_LIBRARIES})
if (NOT EMSCRIPTEN AND NOT ANDROID AND NOT IOS)
set(OPENSSL_USE_STATIC_LIBS TRUE)
set(OPENSSL_MSVC_STATIC_RT TRUE)
find_package(OpenSSL REQUIRED)
message("OpenSSL Libraries: ${OPENSSL_LIBRARIES}")
message("OpenSSL include dir: ${OPENSSL_INCLUDE_DIR}")
if (MSVC)
target_link_libraries(ExampleProject ${BINOCLE_LINK_LIBRARIES} ${OPENSSL_LIBRARIES} wldap32 crypt32 Ws2_32)
message(STATUS "Target link libraries: ${BINOCLE_LINK_LIBRARIES} ${OPENSSL_LIBRARIES} wldap32 crypt32 Ws2_32")
else()
target_link_libraries(ExampleProject ${BINOCLE_LINK_LIBRARIES} ${OPENSSL_LIBRARIES})
message(STATUS "Target link libraries: ${BINOCLE_LINK_LIBRARIES} ${OPENSSL_LIBRARIES}")
endif()
else ()
target_link_libraries(ExampleProject ${BINOCLE_LINK_LIBRARIES})
endif ()

set_property(TARGET ExampleProject PROPERTY C_STANDARD 99)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
Expand Down Expand Up @@ -153,6 +181,32 @@ elseif (APPLE)
set(EXE_TYPE MACOSX_BUNDLE)
endif ()

if (MSVC)
set(EXE_TYPE WIN32)
# set (SOURCE_FILES ${SOURCE_FILES} ${CMAKE_SOURCE_DIR}/build/cmake/Binocle.rc)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
file(GLOB_RECURSE RESOURCES "${BINOCLE_DATA_DIR}*")
foreach(RES_FILE ${RESOURCES})
message("RES_FILE: ${RES_FILE}")
file(RELATIVE_PATH RES_PATH "${BINOCLE_DATA_DIR}" ${RES_FILE})
message("RES_PATH: ${RES_PATH}")
get_filename_component(RES_FOLDER ${RES_PATH} DIRECTORY)
message("RES_FOLDER: ${RES_FOLDER}")
add_custom_command(TARGET ExampleProject
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${RES_FILE} ${CMAKE_CURRENT_BINARY_DIR}/src/assets/${RES_PATH}
)
endforeach(RES_FILE)
elseif (APPLE)
#ICNS
set(MACOSX_BUNDLE_ICON_FILE ExampleProject.icns)
if (NOT IOS)
set(BINOCLE_ICON ${CMAKE_SOURCE_DIR}/build/cmake/Binocle.icns)
endif ()
set_source_files_properties(${BINOCLE_ICON} PROPERTIES MACOSX_PACKAGE_LOCATION "Resources")
set(EXE_TYPE MACOSX_BUNDLE)
endif ()

if (ANDROID)
message("Post build will copy to ${CMAKE_CURRENT_BINARY_DIR}/../../../platform/android/android-project/app/libs/${ANDROID_ABI}/libmain.so")
add_custom_command(TARGET ExampleProject
Expand Down Expand Up @@ -184,3 +238,10 @@ endif ()
if(NOT MSVC)
add_subdirectory(gameplay)
endif(NOT MSVC)

if (APPLE)
add_custom_command ( TARGET ExampleProject POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${OPENSSL_LIBRARIES} $<TARGET_FILE_DIR:ExampleProject>
)
endif ()
36 changes: 36 additions & 0 deletions example/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "binocle_bitmapfont.h"
#include "binocle_audio.h"
#include "sokol_gfx.h"
#include "binocle_http.h"

//#define GAMELOOP 1
//#define DEMOLOOP
Expand Down Expand Up @@ -93,6 +94,7 @@ sg_image wabbit_image;
#endif

#ifdef WITH_PHYSICS
#include "binocle_http.h"
#include "chipmunk/chipmunk.h"
#include "physics.h"
physics_state_t ps;
Expand All @@ -105,6 +107,34 @@ void wren_update(float dt) {
WrenInterpretResult result = wrenCall(wren->vm, method);
}

#if defined(BINOCLE_HTTP)
void test_http_get() {
binocle_http_body_t body;
if (binocle_http_get("https://podium.altralogica.it/l/binocle-example/top/0", &body)) {
binocle_log_info(body.memory);
}
free(body.memory);
}

void test_http_post() {
binocle_http_body_t body;
char *req = "{score=1}";
if (binocle_http_post("https://podium.altralogica.it/l/binocle-example/members/tanis/score", req, &body)) {
binocle_log_info(body.memory);
}
free(body.memory);
}

void test_http_put() {
binocle_http_body_t body;
char *req = "{\"score\":1}";
if (binocle_http_put("https://podium.altralogica.it/l/binocle-example/members/tanis/score", req, &body)) {
binocle_log_info(body.memory);
}
free(body.memory);
}
#endif

#ifdef TWODLOOP
void main_loop() {
binocle_window_begin_frame(window);
Expand Down Expand Up @@ -621,6 +651,12 @@ int main(int argc, char *argv[])

binocle_gd_setup_default_pipeline(&gd, DESIGN_WIDTH, DESIGN_HEIGHT, default_shader, screen_shader);

#if defined(BINOCLE_HTTP)
test_http_get();
test_http_post();
test_http_put();
#endif

running_time = 0;
#ifdef GAMELOOP
binocle_game_run(window, input);
Expand Down
19 changes: 16 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
if (${BINOCLE_HTTP})
message(STATUS "HTTP support enabled")
add_definitions(-DBINOCLE_HTTP)
endif ()

add_subdirectory(deps)
add_subdirectory(binocle)
add_subdirectory(binocle/core/backend)
Expand All @@ -14,7 +19,7 @@ set(BINOCLE_STATIC_LIBS
$<TARGET_OBJECTS:wren>
)

if (NOT EMSCRIPTEN AND NOT ANDROID AND NOT IOS)
if (NOT EMSCRIPTEN AND NOT ANDROID AND NOT IOS AND BINOCLE_HTTP)
find_package(OpenSSL REQUIRED)
endif ()

Expand All @@ -38,8 +43,12 @@ if (APPLE AND NOT IOS)
#$<TARGET_OBJECTS:luajit-5.1>
$<TARGET_OBJECTS:lua>
# $<TARGET_OBJECTS:newton>
$<TARGET_OBJECTS:curl>
)
if (BINOCLE_HTTP)
set(BINOCLE_STATIC_LIBS ${BINOCLE_STATIC_LIBS}
$<TARGET_OBJECTS:curl>
)
endif ()
endif()

if (IOS)
Expand All @@ -62,8 +71,12 @@ if (MSVC)
# we're using lua instead of luajit on macos to be able to test with valgrind
#$<TARGET_OBJECTS:luajit-5.1>
$<TARGET_OBJECTS:lua>
$<TARGET_OBJECTS:curl>
)
if (BINOCLE_HTTP)
set(BINOCLE_STATIC_LIBS ${BINOCLE_STATIC_LIBS}
$<TARGET_OBJECTS:curl>
)
endif ()
endif()

message("List of static libraries for Binocle: ${BINOCLE_STATIC_LIBS}")
Expand Down
Loading

0 comments on commit ec6cb67

Please sign in to comment.