VirtualC64 v5.0 beta
Commodore 64 Emulator
Loading...
Searching...
No Matches
Joystick.h
1// -----------------------------------------------------------------------------
2// This file is part of VirtualC64
3//
4// Copyright (C) Dirk W. Hoffmann. www.dirkwhoffmann.de
5// This FILE is dual-licensed. You are free to choose between:
6//
7// - The GNU General Public License v3 (or any later version)
8// - The Mozilla Public License v2
9//
10// SPDX-License-Identifier: GPL-3.0-or-later OR MPL-2.0
11// -----------------------------------------------------------------------------
12
13#pragma once
14
15#include "JoystickTypes.h"
16#include "C64Types.h"
17#include "SubComponent.h"
18
19namespace vc64 {
20
21class Joystick final : public SubComponent, public Inspectable<JoystickInfo> {
22
23 Descriptions descriptions = {
24 {
25 .name = "Joystick1",
26 .description = "Joystick in Port 1"
27 },
28 {
29 .name = "Joystick2",
30 .description = "Joystick in Port 2"
31 }
32 };
33
34 ConfigOptions options = {
35
40 };
41
42 // Reference to the control port this device belongs to
43 ControlPort &port;
44
45 // Current configuration
46 JoystickConfig config = { };
47
48 // Button state
49 bool button = false;
50
51 // Horizontal joystick position (-1 = left, 1 = right, 0 = released)
52 int axisX = 0;
53
54 // Vertical joystick position (-1 = up, 1 = down, 0 = released)
55 int axisY = 0;
56
57
58 //
59 // Methods
60 //
61
62public:
63
64 Joystick(C64 &ref, ControlPort& pref);
65
66 Joystick& operator= (const Joystick& other) {
67
68 CLONE(button)
69 CLONE(axisX)
70 CLONE(axisY)
71
72 CLONE(config)
73
74 return *this;
75 }
76
77
78 //
79 // Methods from Serializable
80 //
81
82public:
83
84 template <class T> void serialize(T& worker) {
85
86 if (isResetter(worker)) return;
87
88 worker
89
90 << config.autofire
91 << config.autofireBursts
92 << config.autofireBullets
93 << config.autofireDelay;
94 }
95 void operator << (SerChecker &worker) override { serialize(worker); }
96 void operator << (SerCounter &worker) override { serialize(worker); }
97 void operator << (SerResetter &worker) override;
98 void operator << (SerReader &worker) override;
99 void operator << (SerWriter &worker) override { serialize(worker); }
100
101
102 //
103 // Methods from CoreComponent
104 //
105
106public:
107
108 const Descriptions &getDescriptions() const override { return descriptions; }
109
110private:
111
112 void _dump(Category category, std::ostream& os) const override;
113
114
115 //
116 // Methods from Inspectable
117 //
118
119private:
120
121 void cacheInfo(JoystickInfo &result) const override;
122
123
124 //
125 // Methods from Configurable
126 //
127
128public:
129
130 const JoystickConfig &getConfig() const { return config; }
131 const ConfigOptions &getOptions() const override { return options; }
132 i64 getOption(Option opt) const override;
133 void checkOption(Option opt, i64 value) override;
134 void setOption(Option opt, i64 value) override;
135
136
137 //
138 // Using the device
139 //
140
141public:
142
143 // Reads the port bits that show up in the CIA's data port registers
144 u8 getControlPort() const;
145
146 // Triggers a gamepad event
147 void trigger(GamePadAction event);
148
149
150 //
151 // Auto fire
152 //
153
154public:
155
156 // Processes an auto-fire event
157 void processEvent();
158
159private:
160
161 // Indicates if the device is currently auto-firing
162 bool isAutofiring();
163
164 // Reloads the auto-fire gun
165 void reload();
166 void reload(isize bullets);
167 template <EventSlot Slot> void reload(isize bullets);
168};
169
170}
VirtualC64 project namespace.
Definition CmdQueue.cpp:16
@ OPT_AUTOFIRE_BURSTS
Burst mode (on/off)
Definition OptionTypes.h:162
@ OPT_AUTOFIRE
Autofire status [on/off].
Definition OptionTypes.h:161
@ OPT_AUTOFIRE_BULLETS
Number of bullets per burst.
Definition OptionTypes.h:163
@ OPT_AUTOFIRE_DELAY
Delay between two shots [frames].
Definition OptionTypes.h:164