Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: starting (simple) online multiplier support #24

Draft
wants to merge 47 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
19f7b3d
WIP: starting online multiplier support
Totto16 Apr 5, 2023
19631be
added SDL2 Wrapper classes
Totto16 Apr 5, 2023
579204d
implemented PlayerManager
Totto16 Apr 5, 2023
18595a8
implemented serialization of Transportable
Totto16 Apr 5, 2023
eb391fe
more features added, client and server can connect (but still not com…
Totto16 Apr 5, 2023
26f93e5
deleted unused code
Totto16 Apr 5, 2023
b2271e9
fixed issues in offsets and in event_dispatching, now it works visual…
Totto16 Apr 6, 2023
60783f9
added ability to deserialize (WIP)
Totto16 Apr 6, 2023
851c79f
added source files to cmake
Totto16 Apr 6, 2023
6e2699f
added correct header so that msvc doesn't complain
Totto16 Apr 6, 2023
8711790
fixed meson CI (linux) to use gcc-12
Totto16 Apr 6, 2023
95ed3a3
fixed msvc warning (error)
Totto16 Apr 6, 2023
4ea8f0b
fix more msvc errors
Totto16 Apr 6, 2023
cf8f219
- fixed consistency with std::uint32_t and uint32_t
Totto16 Apr 6, 2023
d878672
- fixed many free floating templates
Totto16 Apr 6, 2023
e28ff19
fixed draw position of the preview field
Totto16 Apr 6, 2023
1e3ef32
fixed sending data, now each client can send its inputs (but the star…
Totto16 Apr 6, 2023
ce7151b
replaced c-like casts with static_cats where possible
Totto16 Apr 6, 2023
1b25223
fixed so that also the client can receive data
Totto16 Apr 7, 2023
d0fb288
added latest_sdl_net_error util
Totto16 Apr 7, 2023
5fbda19
fixed crash, where iI used the wrong variable
Totto16 Apr 7, 2023
d89ef53
added better initialization (WIP), untested
Totto16 Apr 7, 2023
32eacbd
removed unnecessary debug printing
Totto16 Apr 7, 2023
1197a8e
fixed remaining bugs:
Totto16 Apr 7, 2023
e5cd7e0
fixed todo: removed assert and replace it with throw
Totto16 Apr 7, 2023
0565a31
refined todo
Totto16 Apr 7, 2023
db2194c
fixed todo:
Totto16 Apr 7, 2023
ba482bf
removed todo:
Totto16 Apr 7, 2023
b2f49ff
refined todo
Totto16 Apr 7, 2023
30ed3f3
added GCC warning -Wold-style-cast
Totto16 Apr 7, 2023
fc0c50c
Merge branch 'main' into network-impl
Totto16 Apr 8, 2023
5ab737f
added better help for cli
Totto16 Apr 8, 2023
fe3048b
WIP: seed addition
Totto16 Apr 8, 2023
3883d06
added seed support
Totto16 Apr 8, 2023
875671f
refactored player text display, and fixed the crash
Totto16 Apr 8, 2023
f8612ae
added overload for util::to_hex_str
Totto16 Apr 8, 2023
bb6a2ac
removed duplicate use of variable name in scope
Totto16 Apr 8, 2023
dc756d1
some small render imrpovements
Totto16 Apr 8, 2023
3d88c72
update sdl2 wrap
Totto16 Apr 8, 2023
c390f11
fixed merge error
Totto16 Apr 8, 2023
028a279
fixed meson file
Totto16 Apr 8, 2023
4dec9e5
use concepts for templates
Totto16 Apr 9, 2023
b08ce2b
Merge remote-tracking branch 'upstream/main' into network-impl
Totto16 Apr 16, 2023
8667c2f
removed technical debt:
Totto16 Apr 18, 2023
b31b66b
refactor and review Connection and Server class:
Totto16 Apr 18, 2023
3b2a8e9
deleted copy constructor cause copying sockets would cause false beha…
Totto16 Apr 18, 2023
66c0a01
refined Connection class docs
Totto16 Apr 18, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/meson.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ jobs:
sudo apt-get update
sudo apt-get install ninja-build libsdl2-2.0-0 libsdl2-dev libsdl2-net* libsdl2-ttf* -y

- name: Configure
- name: Configure (Windows)
if: matrix.config.os == 'windows-latest'
run: meson setup build -Dbuildtype=${{ matrix.config.buildtype }}

- name: Build
Expand Down
83 changes: 45 additions & 38 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(CMAKE_CXX_STANDARD 23)
find_package(argparse CONFIG REQUIRED)
find_package(SDL2 CONFIG REQUIRED)
find_package(SDL2_ttf CONFIG REQUIRED)
find_package(SDL2_net CONFIG REQUIRED)
find_package(spdlog CONFIG REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED)
find_package(magic_enum CONFIG REQUIRED)
Expand All @@ -15,56 +16,61 @@ find_package(tl-expected CONFIG REQUIRED)
set(TARGET_LIST oopetris SDL2::SDL2-static)

add_executable(oopetris
src/main.cpp
src/application.hpp
src/application.cpp
src/renderer.cpp
src/renderer.hpp
src/sdl_context.cpp
src/sdl_context.hpp
src/window.cpp
src/window.hpp
src/color.hpp
src/rect.hpp
src/point.hpp
src/tetromino.hpp
src/mino.hpp
src/grid.hpp
src/tetromino_type.hpp
src/tetromino_type.cpp
src/mino.cpp
src/tetrion.cpp
src/tetrion.hpp
src/tetris_application.hpp
src/tetris_application.cpp
src/application.hpp
src/bag.cpp
src/bag.hpp
src/font.cpp
src/font.hpp
src/text.cpp
src/text.hpp
src/grid.cpp
src/input.hpp
src/color.hpp
src/command_line_arguments.hpp
src/controls.hpp
src/event_dispatcher.cpp
src/event_dispatcher.hpp
src/event_listener.hpp
src/font.cpp
src/font.hpp
src/grid.cpp
src/grid.hpp
src/input_event.hpp
src/input.cpp
src/settings.hpp
src/input.hpp
src/key_codes.hpp
src/local_multiplayer.cpp
src/magic_enum_wrapper.hpp
src/types.hpp
src/main.cpp
src/mino_stack.cpp
src/mino_stack.hpp
src/mino.cpp
src/mino.hpp
src/network/connection_manager.cpp
src/network/network_data.cpp
src/network/network_manager.cpp
src/network/network_transportable.cpp
src/network/online_handler.cpp
src/play_mode.cpp
src/point.hpp
src/random.cpp
src/random.hpp
src/recording.hpp
src/input_event.hpp
src/utils.hpp
src/utils.cpp
src/command_line_arguments.hpp
src/controls.hpp
src/mino_stack.cpp
src/mino_stack.hpp
src/tetrion_snapshot.hpp
src/rect.hpp
src/renderer.cpp
src/renderer.hpp
src/sdl_context.cpp
src/sdl_context.hpp
src/tetrion_snapshot.cpp
src/tetrion_snapshot.hpp
src/tetrion.cpp
src/tetrion.hpp
src/tetris_application.cpp
src/tetris_application.hpp
src/tetromino_type.cpp
src/tetromino_type.hpp
src/tetromino.hpp
src/text.cpp
src/text.hpp
src/types.hpp
src/utils.cpp
src/window.cpp
src/window.hpp
)

foreach (target ${TARGET_LIST})
Expand Down Expand Up @@ -103,9 +109,10 @@ target_link_libraries(oopetris PRIVATE argparse::argparse)
target_link_libraries(oopetris PRIVATE spdlog::spdlog spdlog::spdlog_header_only)
target_link_libraries(oopetris PRIVATE nlohmann_json::nlohmann_json)
target_link_libraries(oopetris PRIVATE magic_enum::magic_enum)
target_link_libraries(oopetris PRIVATE $<IF:$<TARGET_EXISTS:SDL2_ttf::SDL2_ttf>,SDL2_ttf::SDL2_ttf,SDL2_ttf::SDL2_ttf-static>)
target_link_libraries(oopetris PRIVATE tl::optional)
target_link_libraries(oopetris PRIVATE tl::expected)
target_link_libraries(oopetris PRIVATE $<IF:$<TARGET_EXISTS:SDL2_ttf::SDL2_ttf>,SDL2_ttf::SDL2_ttf,SDL2_ttf::SDL2_ttf-static>)
target_link_libraries(oopetris PRIVATE $<IF:$<TARGET_EXISTS:SDL2_net::SDL2_net>,SDL2_net::SDL2_net,SDL2_net::SDL2_net-static>)

# static runtime library
set_property(TARGET ${target} PROPERTY
Expand Down
3 changes: 2 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ cpp = meson.get_compiler('cpp')
if cpp.get_id() == 'msvc'
add_project_arguments('/std:c++latest', language: 'cpp')
elif cpp.get_id() == 'gcc'
add_project_arguments('-std=c++23', language: 'cpp')
add_project_arguments('-std=c++23', '-Wold-style-cast', language: 'cpp')
elif cpp.get_id() == 'clang'
add_project_arguments('-std=c++2b', language: 'cpp')
else
Expand Down Expand Up @@ -65,6 +65,7 @@ else
endif

deps += dependency('sdl2_ttf', 'SDL2_ttf', required: true, native: native)
deps += dependency('sdl2_net', 'SDL2_net', required: true, native: native)
deps += dependency('spdlog', required: true, native: native)
deps += dependency('nlohmann_json', required: true, native: native)
deps += dependency(
Expand Down
2 changes: 1 addition & 1 deletion src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void Application::handle_event(const SDL_Event& event) {
#if defined(__ANDROID__)
if (event.type == SDL_KEYDOWN and event.key.keysym.sym == SDLK_AC_BACK) {
//TODO: also catch the resume event from the app (see SDLActivity.java)
// pause()
// pause()
}
#endif
}
Expand Down
28 changes: 27 additions & 1 deletion src/command_line_arguments.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
#include "types.hpp"
#include <argparse/argparse.hpp>
#include <filesystem>
#include <memory>
#include <spdlog/spdlog.h>
#include <stdexcept>
#include <string>
#include <tl/optional.hpp>

struct PlayModeInformation {
bool is_multiplayer;
bool is_server;
u8 player_nums;
};


struct CommandLineArguments {
private:
static inline constexpr int default_target_fps = 60;
Expand All @@ -17,6 +25,7 @@ struct CommandLineArguments {
tl::optional<std::filesystem::path> recording_path{};
u64 target_fps{ default_target_fps };
i32 starting_level{ default_starting_level };
PlayModeInformation play_mode_information;

CommandLineArguments(int argc, char** argv) {
argparse::ArgumentParser parser{ "oopetris", "0.0.1", argparse::default_arguments::all };
Expand All @@ -29,10 +38,16 @@ struct CommandLineArguments {
.help("the starting level of the game")
.scan<'i', int>()
.default_value(default_starting_level);
parser.add_argument("-m", "--multiplayer").help("play in multiplayer mode").scan<'i', int>();
parser.add_argument("-s", "--server")
.help("is this instance the server of the multiplayer")
.default_value(false)
.implicit_value(true);

try {
parser.parse_args(argc, argv);

if (auto path = parser.present("--recording")) {
if (auto path = parser.present<std::string>("--recording")) {
spdlog::info("recording is present");
recording_path = std::filesystem::path{ *path };
}
Expand All @@ -52,6 +67,17 @@ struct CommandLineArguments {
"invalid value for starting level ({}), using default value instead ({})", level, starting_level
);
}

const auto multiplayer = parser.present<std::vector<int>>("--multiplayer");
if (multiplayer.has_value()) {
const auto multiplayer_num = parser.get<int>("--multiplayer");
const auto is_server = parser.get<bool>("--server");
play_mode_information = PlayModeInformation{ true, is_server, static_cast<u8>(multiplayer_num) };
} else {
play_mode_information = PlayModeInformation{ false, false, 0 };
}


} catch (const std::exception& err) {
spdlog::error("error parsing command line arguments: {}", err.what());
#if defined(__ANDROID__)
Expand Down
15 changes: 7 additions & 8 deletions src/grid.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "grid.hpp"
#include "rect.hpp"

Grid::Grid(Point offset, int tile_size) : m_offset{ offset - Point{ 0, invisible_rows * tile_size }}, m_tile_size{ tile_size } {
Grid::Grid(Point offset, int tile_size) : m_start_point{ offset }, m_offset{ offset - Point{ 0, invisible_rows * tile_size }}, m_tile_size{ tile_size } {
}

[[nodiscard]] Point Grid::tile_size() const {
Expand Down Expand Up @@ -42,11 +43,9 @@ void Grid::draw_playing_field_background(const Application& app) const {
width * m_tile_size - 1,
(height - invisible_rows) * m_tile_size,
};
app.renderer().draw_rect_filled(Rect{ Point::zero(), bottom_right }, background_color);
const Rect outline_rect = Rect{
Point{m_offset.x, m_offset.y}
+ Point{ 0, invisible_rows * m_tile_size},
bottom_right
};
app.renderer().draw_rect_outline(outline_rect, border_color);
app.renderer().draw_rect_filled(m_start_point + Rect{ Point::zero(), bottom_right }, background_color);
app.renderer().draw_line(
m_start_point + Point{ bottom_right.x + 1, 0 },
m_start_point + Point{ bottom_right.x + 1, app.window().size().y - 1 }, border_color
);
}
1 change: 1 addition & 0 deletions src/grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct Grid final {
static constexpr Point hold_tetromino_position = hold_background_position + Point{ 0, 1 };

private:
Point m_start_point;
Point m_offset;
int m_tile_size;

Expand Down
36 changes: 36 additions & 0 deletions src/input.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include "input.hpp"
#include "application.hpp"
#include "event_dispatcher.hpp"
#include "key_codes.hpp"
#include "recording.hpp"
#include "tetrion.hpp"
#include <algorithm>
#include <array>

#if defined(__ANDROID__)
#include <cmath>
Expand Down Expand Up @@ -346,3 +349,36 @@ tl::optional<InputEvent> TouchInput::sdl_event_to_input_event(const SDL_Event& e
return tl::nullopt;
}
#endif


namespace utils {


[[nodiscard]] std::unique_ptr<Input> create_input(
Controls controls,
Tetrion* associated_tetrion,
Input::OnEventCallback on_event_callback,
EventDispatcher* event_dispatcher
) {

#if defined(__ANDROID__)
auto input = std::make_unique<TouchInput>(associated_tetrion, std::move(on_event_callback));
event_dispatcher->register_listener(input.get());
return input;
#else
return std::visit(
overloaded{
[&](KeyboardControls& keyboard_controls) -> std::unique_ptr<Input> {
auto input = std::make_unique<KeyboardInput>(
associated_tetrion, std::move(on_event_callback), keyboard_controls
);

event_dispatcher->register_listener(input.get());
return input;
},
},
controls
);
#endif
}
} // namespace utils
17 changes: 16 additions & 1 deletion src/input.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
#pragma once

#include "controls.hpp"
#include "event_dispatcher.hpp"
#include "event_listener.hpp"
#include "input_event.hpp"
#include "network/connection_manager.hpp"
#include "network/network_data.hpp"
#include "random.hpp"
#include "settings.hpp"
#include "types.hpp"
#include <filesystem>
#include <functional>
#include <memory>
#include <tl/optional.hpp>
#include <unordered_map>
#include <vector>

struct Tetrion;

Expand Down Expand Up @@ -52,7 +57,7 @@ struct Input {

public:
virtual void update();
virtual void late_update() {};
virtual void late_update(){};
virtual ~Input() = default;
};

Expand Down Expand Up @@ -122,3 +127,13 @@ struct TouchInput final : public Input, public EventListener {
[[nodiscard]] tl::optional<InputEvent> sdl_event_to_input_event(const SDL_Event& event);
};
#endif


namespace utils {
[[nodiscard]] std::unique_ptr<Input> create_input(
Controls controls,
Tetrion* associated_tetrion,
Input::OnEventCallback on_event_callback,
EventDispatcher* event_dispatcher
);
}
Loading