Generalized vector_to_json, and made ball->json more efficient (with ints instead of floats)
This commit is contained in:
parent
a4ef07ef3f
commit
97a5e453ad
4 changed files with 98 additions and 75 deletions
64
src/app.h
64
src/app.h
|
@ -7,69 +7,7 @@
|
|||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
#include "basics.h"
|
||||
#include "adaptions.h"
|
||||
|
||||
#include "simulation/Simulation.h"
|
||||
#include "simulation/Beat.h"
|
||||
|
||||
using Vec2 = math::Vec2;
|
||||
using LineKind = simulation::LineKind;
|
||||
|
||||
using ball_info = int;
|
||||
using line_info = void;
|
||||
using ball_type = simulation::Ball<ball_info>;
|
||||
using line_type = simulation::Line<line_info>;
|
||||
using simu_type = simulation::Simulation<ball_info, line_info>;
|
||||
|
||||
using note_info = Vec2;
|
||||
using note_type = Note<note_info>;
|
||||
using beat_type = Beat<note_info>;
|
||||
|
||||
struct AbstractLine {
|
||||
math::Vec2 starting_point;
|
||||
math::Vec2 end_point;
|
||||
int line_kind;
|
||||
|
||||
const float hsize = 5.0f;
|
||||
|
||||
AbstractLine() = default;
|
||||
|
||||
AbstractLine(math::Vec2 starting_point, math::Vec2 end_point, int line_kind)
|
||||
: starting_point(starting_point)
|
||||
, end_point(end_point)
|
||||
, line_kind(line_kind)
|
||||
{}
|
||||
|
||||
// create 6 lines, to emulate width, and rounded edges
|
||||
std::vector<line_type> calculate_lines() const {
|
||||
auto dir = normalize(end_point - starting_point);
|
||||
auto normal = rotate_ccw(dir);
|
||||
LineKind lk = static_cast<LineKind>(line_kind);
|
||||
|
||||
if(line_kind == simulation::kOneWay){
|
||||
std::vector<line_type> ret;
|
||||
ret.emplace_back(starting_point + hsize*normal, end_point + hsize*normal, lk);
|
||||
ret.emplace_back(end_point + hsize*normal, end_point + hsize*dir, lk);
|
||||
ret.emplace_back(end_point + hsize*dir, end_point - hsize*normal, lk);
|
||||
ret.emplace_back(end_point - hsize*normal, starting_point - hsize*normal, lk);
|
||||
ret.emplace_back(starting_point - hsize*normal, starting_point - hsize*dir, lk);
|
||||
ret.emplace_back(starting_point - hsize*dir, starting_point + hsize*normal, lk);
|
||||
return ret;
|
||||
} else {
|
||||
std::vector<line_type> ret;
|
||||
ret.emplace_back(starting_point, end_point, lk);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
BOOST_FUSION_ADAPT_STRUCT(
|
||||
AbstractLine,
|
||||
(::math::Vec2, starting_point)
|
||||
(::math::Vec2, end_point)
|
||||
(int, line_kind)
|
||||
)
|
||||
#include "app_common.h"
|
||||
|
||||
struct libwebsocket;
|
||||
struct App{
|
||||
|
|
87
src/app_common.h
Normal file
87
src/app_common.h
Normal file
|
@ -0,0 +1,87 @@
|
|||
#pragma once
|
||||
|
||||
#include "basics.h"
|
||||
#include "adaptions.h"
|
||||
|
||||
#include "simulation/Simulation.h"
|
||||
#include "simulation/Beat.h"
|
||||
|
||||
using Vec2 = math::Vec2;
|
||||
using LineKind = simulation::LineKind;
|
||||
|
||||
using ball_info = int;
|
||||
using line_info = void;
|
||||
using ball_type = simulation::Ball<ball_info>;
|
||||
using line_type = simulation::Line<line_info>;
|
||||
using simu_type = simulation::Simulation<ball_info, line_info>;
|
||||
|
||||
using note_info = Vec2;
|
||||
using note_type = Note<note_info>;
|
||||
using beat_type = Beat<note_info>;
|
||||
|
||||
struct AbstractLine {
|
||||
math::Vec2 starting_point;
|
||||
math::Vec2 end_point;
|
||||
int line_kind;
|
||||
|
||||
const float hsize = 5.0f;
|
||||
|
||||
AbstractLine() = default;
|
||||
|
||||
AbstractLine(math::Vec2 starting_point, math::Vec2 end_point, int line_kind)
|
||||
: starting_point(starting_point)
|
||||
, end_point(end_point)
|
||||
, line_kind(line_kind)
|
||||
{}
|
||||
|
||||
// create 6 lines, to emulate width, and rounded edges
|
||||
std::vector<line_type> calculate_lines() const {
|
||||
auto dir = normalize(end_point - starting_point);
|
||||
auto normal = rotate_ccw(dir);
|
||||
LineKind lk = static_cast<LineKind>(line_kind);
|
||||
|
||||
if(line_kind == simulation::kOneWay){
|
||||
std::vector<line_type> ret;
|
||||
ret.emplace_back(starting_point + hsize*normal, end_point + hsize*normal, lk);
|
||||
ret.emplace_back(end_point + hsize*normal, end_point + hsize*dir, lk);
|
||||
ret.emplace_back(end_point + hsize*dir, end_point - hsize*normal, lk);
|
||||
ret.emplace_back(end_point - hsize*normal, starting_point - hsize*normal, lk);
|
||||
ret.emplace_back(starting_point - hsize*normal, starting_point - hsize*dir, lk);
|
||||
ret.emplace_back(starting_point - hsize*dir, starting_point + hsize*normal, lk);
|
||||
return ret;
|
||||
} else {
|
||||
std::vector<line_type> ret;
|
||||
ret.emplace_back(starting_point, end_point, lk);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
BOOST_FUSION_ADAPT_STRUCT(
|
||||
AbstractLine,
|
||||
(::math::Vec2, starting_point)
|
||||
(::math::Vec2, end_point)
|
||||
(int, line_kind)
|
||||
)
|
||||
|
||||
BOOST_FUSION_DEFINE_STRUCT(
|
||||
, IntVec2,
|
||||
(int, x)
|
||||
(int, y)
|
||||
)
|
||||
|
||||
struct cheap_ball_type {
|
||||
IntVec2 position;
|
||||
ball_info information;
|
||||
|
||||
cheap_ball_type(ball_type const & b)
|
||||
: position(b.position)
|
||||
, information(b.information)
|
||||
{}
|
||||
};
|
||||
|
||||
BOOST_FUSION_ADAPT_STRUCT(
|
||||
cheap_ball_type,
|
||||
(IntVec2, position)
|
||||
(ball_info, information)
|
||||
)
|
|
@ -2,20 +2,18 @@
|
|||
|
||||
#include "json.h"
|
||||
|
||||
template <typename C>
|
||||
inline js::Value ptrvector_to_json(C const & container){
|
||||
js::Array array;
|
||||
for(auto x : container){
|
||||
array.push_back(to_json(*x));
|
||||
}
|
||||
return array;
|
||||
struct Identity{
|
||||
template <typename T>
|
||||
T operator()(T t){
|
||||
return t;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename C>
|
||||
inline js::Value vector_to_json(C const & container){
|
||||
template <typename C, typename F = Identity>
|
||||
inline js::Value vector_to_json(C const & container, F f = Identity()){
|
||||
js::Array array;
|
||||
for(auto x : container){
|
||||
array.push_back(to_json(x));
|
||||
array.push_back(to_json(f(x)));
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ websockets::TestProtocol<User> default_protocol{
|
|||
// write (will always come after receive)
|
||||
[](User& user, basic_websocket_info) -> std::string{
|
||||
js::Object ret;
|
||||
ret["balls"] = vector_to_json(app->sim.balls);
|
||||
ret["balls"] = vector_to_json(app->sim.balls, [](ball_type const & b){return cheap_ball_type(b);});
|
||||
if(user.update_lines){
|
||||
ret["lines"] = vector_to_json(app->sim.lines);
|
||||
user.update_lines = false;
|
||||
|
@ -63,7 +63,7 @@ websockets::TestProtocol<Empty> observer_protocol{
|
|||
// write (will always come after receive)
|
||||
[](Empty& user, basic_websocket_info) -> std::string{
|
||||
js::Object ret;
|
||||
ret["people"] = ptrvector_to_json(app->people_online);
|
||||
ret["people"] = vector_to_json(app->people_online, [](User* u){return *u;});
|
||||
ret["balls"] = vector_to_json(app->sim.balls);
|
||||
ret["lines"] = vector_to_json(app->sim.lines);
|
||||
return write_json(ret);
|
||||
|
|
Reference in a new issue