Added JSON communication, basic simulation with sound.
This commit is contained in:
parent
b7d82fd4df
commit
ffa44c81f0
7 changed files with 49 additions and 9 deletions
3
.gitmodules
vendored
3
.gitmodules
vendored
|
@ -1,3 +1,6 @@
|
||||||
[submodule "contrib/libwebsockets"]
|
[submodule "contrib/libwebsockets"]
|
||||||
path = contrib/libwebsockets
|
path = contrib/libwebsockets
|
||||||
url = git://github.com/warmcat/libwebsockets.git
|
url = git://github.com/warmcat/libwebsockets.git
|
||||||
|
[submodule "contrib/json-spirit"]
|
||||||
|
path = contrib/json-spirit
|
||||||
|
url = git://github.com/sirikata/json-spirit.git
|
||||||
|
|
|
@ -49,4 +49,11 @@ set(WITHOUT_TESTAPPS ON CACHE INTERNAL "" FORCE)
|
||||||
add_subdirectory(${PROJECT_SOURCE_DIR}/contrib/libwebsockets/)
|
add_subdirectory(${PROJECT_SOURCE_DIR}/contrib/libwebsockets/)
|
||||||
include_directories(SYSTEM "${PROJECT_SOURCE_DIR}/contrib/libwebsockets/")
|
include_directories(SYSTEM "${PROJECT_SOURCE_DIR}/contrib/libwebsockets/")
|
||||||
|
|
||||||
|
# JSON lib
|
||||||
|
set(JSON_SPIRIT_BUILD_DEMOS OFF CACHE INTERNAL "" FORCE)
|
||||||
|
set(JSON_SPIRIT_BUILD_TESTS OFF CACHE INTERNAL "" FORCE)
|
||||||
|
set(JSON_SPIRIT_LIBRARY_TYPE "STATIC" CACHE INTERNAL "" FORCE)
|
||||||
|
add_subdirectory(${PROJECT_SOURCE_DIR}/contrib/json-spirit/build ${CMAKE_CURRENT_BINARY_DIR}/json-spirit)
|
||||||
|
include_directories(SYSTEM "${PROJECT_SOURCE_DIR}/contrib/json-spirit/include/")
|
||||||
|
|
||||||
add_subdirectory("src")
|
add_subdirectory("src")
|
||||||
|
|
1
contrib/json-spirit
Submodule
1
contrib/json-spirit
Submodule
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit b585cf771ca10de706f96ddbd6d853dde72c6bbd
|
|
@ -39,6 +39,7 @@
|
||||||
"-I${folder:${project_path:sdlgame.sublime-project}}",
|
"-I${folder:${project_path:sdlgame.sublime-project}}",
|
||||||
"-I${folder:${project_path:sdlgame.sublime-project}}/motor",
|
"-I${folder:${project_path:sdlgame.sublime-project}}/motor",
|
||||||
"-isystem${folder:${project_path:sdlgame.sublime-project}}/contrib/libwebsockets/",
|
"-isystem${folder:${project_path:sdlgame.sublime-project}}/contrib/libwebsockets/",
|
||||||
|
"-isystem${folder:${project_path:sdlgame.sublime-project}}/contrib/json-spirit/include",
|
||||||
"-isystem${folder:${project_path:sdlgame.sublime-project}}/motor/contrib/assimp-3.0.1270/include",
|
"-isystem${folder:${project_path:sdlgame.sublime-project}}/motor/contrib/assimp-3.0.1270/include",
|
||||||
"-isystem${folder:${project_path:sdlgame.sublime-project}}/motor/contrib/soil",
|
"-isystem${folder:${project_path:sdlgame.sublime-project}}/motor/contrib/soil",
|
||||||
"-isystem${folder:${project_path:sdlgame.sublime-project}}/motor/contrib/bullet/src",
|
"-isystem${folder:${project_path:sdlgame.sublime-project}}/motor/contrib/bullet/src",
|
||||||
|
|
|
@ -60,4 +60,4 @@ set(all_friggin_libraries onegame_common ${boost} ${motor} ${moggle} ${assimp} $
|
||||||
|
|
||||||
file(GLOB client_sources "client/*.cpp")
|
file(GLOB client_sources "client/*.cpp")
|
||||||
add_executable(client client_main.cpp ${client_sources})
|
add_executable(client client_main.cpp ${client_sources})
|
||||||
target_link_libraries(client ${all_friggin_libraries} websockets)
|
target_link_libraries(client ${all_friggin_libraries} websockets json_spirit)
|
||||||
|
|
|
@ -3,7 +3,12 @@
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
#include "../../../beats-server/src/json.h"
|
||||||
|
#include "../../../beats-server/src/json_ext.h"
|
||||||
|
|
||||||
namespace games {
|
namespace games {
|
||||||
|
static Client * current_client{nullptr};
|
||||||
|
|
||||||
int callback_func(libwebsocket_context * context, libwebsocket * wsi, libwebsocket_callback_reasons reason, void * user, void * in, size_t len){
|
int callback_func(libwebsocket_context * context, libwebsocket * wsi, libwebsocket_callback_reasons reason, void * user, void * in, size_t len){
|
||||||
unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 + LWS_SEND_BUFFER_POST_PADDING];
|
unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 + LWS_SEND_BUFFER_POST_PADDING];
|
||||||
buf[LWS_SEND_BUFFER_PRE_PADDING] = 'a';
|
buf[LWS_SEND_BUFFER_PRE_PADDING] = 'a';
|
||||||
|
@ -13,11 +18,25 @@ namespace games {
|
||||||
libwebsocket_callback_on_writable(context, wsi);
|
libwebsocket_callback_on_writable(context, wsi);
|
||||||
break;
|
break;
|
||||||
case LWS_CALLBACK_CLIENT_RECEIVE:
|
case LWS_CALLBACK_CLIENT_RECEIVE:
|
||||||
std::cout << std::string(static_cast<char*>(in), len) << std::endl;
|
// std::cout << std::string(static_cast<char*>(in), len) << std::endl;
|
||||||
break;
|
break;
|
||||||
case LWS_CALLBACK_CLIENT_WRITEABLE:
|
case LWS_CALLBACK_CLIENT_WRITEABLE:{
|
||||||
libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], 1, LWS_WRITE_TEXT);
|
js::Object ret;
|
||||||
|
if(current_client->lines_changed){
|
||||||
|
ret["lines"] = vector_to_json(current_client->lines, [](std::shared_ptr<Instrument> const & i){return cheap_line_type(*i);});
|
||||||
|
current_client->lines_changed = false;
|
||||||
|
} else {
|
||||||
|
ret["balls"] = vector_to_json(current_client->sim.balls, [](ball_type const & b){return cheap_ball_type(b);});
|
||||||
|
}
|
||||||
|
std::string string_to_send = write_json(ret);
|
||||||
|
|
||||||
|
unsigned char * buf = new unsigned char [LWS_SEND_BUFFER_PRE_PADDING + string_to_send.size() + LWS_SEND_BUFFER_POST_PADDING];
|
||||||
|
memcpy(&buf[LWS_SEND_BUFFER_PRE_PADDING], string_to_send.c_str(), string_to_send.size());
|
||||||
|
int n = libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], string_to_send.size(), LWS_WRITE_TEXT);
|
||||||
|
if (n < 0) return 1;
|
||||||
|
if (n < string_to_send.size()) return -1;
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -36,6 +55,8 @@ namespace games {
|
||||||
for(auto & l : line->calculate_lines()){
|
for(auto & l : line->calculate_lines()){
|
||||||
sim.lines.push_back(l);
|
sim.lines.push_back(l);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lines_changed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Client::has_ended(){
|
bool Client::has_ended(){
|
||||||
|
@ -52,6 +73,11 @@ namespace games {
|
||||||
Client::Client(int window_width_, int window_height_, std::shared_ptr<Base>& active_base_)
|
Client::Client(int window_width_, int window_height_, std::shared_ptr<Base>& active_base_)
|
||||||
: Base(window_width_, window_height_, active_base_)
|
: Base(window_width_, window_height_, active_base_)
|
||||||
{
|
{
|
||||||
|
if(current_client){
|
||||||
|
CERR << "Warning: global client already defined\n";
|
||||||
|
}
|
||||||
|
current_client = this;
|
||||||
|
|
||||||
// simulation
|
// simulation
|
||||||
beat.notes.emplace_back(note_type::kQuarterNote, note_info{100.0f, 100.0f});
|
beat.notes.emplace_back(note_type::kQuarterNote, note_info{100.0f, 100.0f});
|
||||||
cheap_line_type line{{{50.0f, 100.0f}, {150.0f, 200.0f}, simulation::kOneWay}};
|
cheap_line_type line{{{50.0f, 100.0f}, {150.0f, 200.0f}, simulation::kOneWay}};
|
||||||
|
@ -131,20 +157,21 @@ namespace games {
|
||||||
}
|
}
|
||||||
|
|
||||||
// websockets
|
// websockets
|
||||||
const float poll_interval = 5.0;
|
const float push_interval = 0.05;
|
||||||
poll_time += dt;
|
push_time += dt;
|
||||||
|
|
||||||
int ret = libwebsocket_service(context, 10);
|
int ret = libwebsocket_service(context, 10);
|
||||||
if(ret < 0) {
|
if(ret < 0) {
|
||||||
CERR << "ERROR: libwebsocket returns < 0\n";
|
CERR << "ERROR: libwebsocket returns < 0\n";
|
||||||
}
|
}
|
||||||
if(poll_time >= poll_interval){
|
if(push_time >= push_interval){
|
||||||
libwebsocket_callback_on_writable(context, wsi);
|
libwebsocket_callback_on_writable(context, wsi);
|
||||||
poll_time -= poll_interval;
|
push_time -= push_interval;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Client::~Client(){
|
Client::~Client(){
|
||||||
libwebsocket_context_destroy(context);
|
libwebsocket_context_destroy(context);
|
||||||
|
current_client = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,8 @@ namespace games {
|
||||||
// websocket part
|
// websocket part
|
||||||
libwebsocket_context * context{nullptr};
|
libwebsocket_context * context{nullptr};
|
||||||
libwebsocket * wsi{nullptr};
|
libwebsocket * wsi{nullptr};
|
||||||
float poll_time{0.0};
|
float push_time{0.0};
|
||||||
|
bool lines_changed{true};
|
||||||
|
|
||||||
// simulation part
|
// simulation part
|
||||||
int balluid{0};
|
int balluid{0};
|
||||||
|
|
Reference in a new issue