Antkeeper  0.0.1
control-profile.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "game/control-profile.hpp"
26 #include <engine/debug/log.hpp>
27 
37 template <>
38 void serializer<::control_profile>::serialize(const ::control_profile& profile, serialize_context& ctx)
39 {
40  // Write number of mappings
41  std::uint64_t size = static_cast<std::uint64_t>(profile.mappings.size());
42  ctx.write64<std::endian::big>(reinterpret_cast<const std::byte*>(&size), 1);
43 
44  // Write mappings
45  for (const auto& [key, value]: profile.mappings)
46  {
47  // Write key
48  ctx.write32<std::endian::big>(reinterpret_cast<const std::byte*>(&key), 1);
49 
50  // Write mapping type
51  const input::mapping_type mapping_type = value->get_mapping_type();
52  ctx.write8(reinterpret_cast<const std::byte*>(&mapping_type), 1);
53 
54  // Write mapping
55  switch (mapping_type)
56  {
58  serializer<input::gamepad_axis_mapping>().serialize(*static_cast<const input::gamepad_axis_mapping*>(value.get()), ctx);
59  break;
60 
63  break;
64 
66  serializer<input::key_mapping>().serialize(*static_cast<const input::key_mapping*>(value.get()), ctx);
67  break;
68 
70  serializer<input::mouse_button_mapping>().serialize(*static_cast<const input::mouse_button_mapping*>(value.get()), ctx);
71  break;
72 
74  serializer<input::mouse_motion_mapping>().serialize(*static_cast<const input::mouse_motion_mapping*>(value.get()), ctx);
75  break;
76 
78  serializer<input::mouse_scroll_mapping>().serialize(*static_cast<const input::mouse_scroll_mapping*>(value.get()), ctx);
79  break;
80 
81  default:
82  throw serialize_error("Unsupported mapping type");
83  break;
84  }
85  }
86 
87  // Write settings
88  serializer<dict<hash::fnv1a32_t>>().serialize(profile.settings, ctx);
89 }
90 
100 template <>
102 {
103  profile.mappings.clear();
104 
105  // Read number of mappings
106  std::uint64_t size = 0;
107  ctx.read64<std::endian::big>(reinterpret_cast<std::byte*>(&size), 1);
108 
109  // Read mappings
110  for (std::uint64_t i = 0; i < size; ++i)
111  {
112  // Read key
113  hash::fnv1a32_t key;
114  ctx.read32<std::endian::big>(reinterpret_cast<std::byte*>(&key), 1);
115 
116  // Read mapping type
118  ctx.read8(reinterpret_cast<std::byte*>(&mapping_type), 1);
119 
120  // Read mapping
121  switch (mapping_type)
122  {
124  {
127  profile.mappings.emplace(key, std::make_unique<input::gamepad_axis_mapping>(std::move(mapping)));
128  break;
129  }
130 
132  {
135  profile.mappings.emplace(key, std::make_unique<input::gamepad_button_mapping>(std::move(mapping)));
136  break;
137  }
138 
140  {
141  input::key_mapping mapping;
143  profile.mappings.emplace(key, std::make_unique<input::key_mapping>(std::move(mapping)));
144  break;
145  }
146 
148  {
151  profile.mappings.emplace(key, std::make_unique<input::mouse_button_mapping>(std::move(mapping)));
152  break;
153  }
154 
156  {
159  profile.mappings.emplace(key, std::make_unique<input::mouse_motion_mapping>(std::move(mapping)));
160  break;
161  }
162 
164  {
167  profile.mappings.emplace(key, std::make_unique<input::mouse_scroll_mapping>(std::move(mapping)));
168  break;
169  }
170 
171  default:
172  throw deserialize_error("Unsupported mapping type");
173  break;
174  }
175  }
176 
177  // Read settings
178  deserializer<dict<hash::fnv1a32_t>>().deserialize(profile.settings, ctx);
179 }
180 
181 template <>
183 {
184  std::unique_ptr<control_profile> profile = std::make_unique<control_profile>();
185 
187 
188  return profile;
189 }
An exception of this type is thrown when an error occurs during deserialization.
Maps a direction along a gamepad axis to a control input value.
Definition: mapping.hpp:61
Maps a gamepad button to a control input value.
Definition: mapping.hpp:95
Maps a keyboard key to a control input value.
Definition: mapping.hpp:125
Maps a mouse button to a control input value.
Definition: mapping.hpp:163
Maps a direction along a mouse motion axis to a control input value.
Definition: mapping.hpp:193
Maps a direction along a mouse scroll axis to a control input value.
Definition: mapping.hpp:227
static std::unique_ptr< T > load(::resource_manager &resource_manager, deserialize_context &ctx)
Loads a resource.
Manages the loading, caching, and saving of resources.
An exception of this type is thrown when an error occurs during serialization.
mapping_type
Input mapping types.
@ gamepad_axis
Gamepad axis mapping.
@ key
Key mapping.
@ mouse_button
Mouse button mapping.
@ mouse_scroll
Mouse scroll mapping.
@ mouse_motion
Mouse motion mapping.
@ gamepad_button
Gamepad button mapping.
dict< hash::fnv1a32_t > settings
Profile-specific settings.
std::multimap< hash::fnv1a32_t, std::unique_ptr< input::mapping > > mappings
Input mappings.
Provides access to a deserialization state.
std::size_t read32(std::byte *data, std::size_t count) noexcept(false)
Reads 32-bit (double word) data.
virtual std::size_t read8(std::byte *data, std::size_t count) noexcept(false)=0
Reads 8-bit (byte) data.
std::size_t read64(std::byte *data, std::size_t count) noexcept(false)
Reads 64-bit (quad word) data.
Specializations of deserializer define the deserialization process for a given type.
void deserialize(T &value, deserialize_context &ctx)
Deserializes a value.
32-bit FNV-1a hash value.
Definition: fnv1a.hpp:117
Provides access to a serialization state.
virtual std::size_t write8(const std::byte *data, std::size_t count) noexcept(false)=0
Writes 8-bit (byte) data.
std::size_t write32(const std::byte *data, std::size_t count) noexcept(false)
Writes 32-bit (double word) data.
std::size_t write64(const std::byte *data, std::size_t count) noexcept(false)
Writes 64-bit (quad word) data.
Specializations of serializer define the serialization process for a given type.
Definition: serializer.hpp:32
void serialize(const T &value, serialize_context &ctx)
Serializes a value.