ObjFW  Check-in [a0f4283e81]

Overview
Comment:Add OFGameController

Only supports Nintendo 3DS so far.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | gamecontroller
Files: files | file ages | folders
SHA3-256: a0f4283e8115bb6f91837f05fd1e1ef48dbf22288e05e193cd5a09585c6fa9de
User & Date: js on 2024-05-05 18:59:50
Other Links: branch diff | manifest | tags
Context
2024-05-05
19:22
OFGameController: Add support for Nintendo DS check-in: a7a6705d90 user: js tags: gamecontroller
18:59
Add OFGameController check-in: a0f4283e81 user: js tags: gamecontroller
12:33
Make OF_APPLICATION_DELEGATE() work with -mwindows check-in: dcea3bad47 user: js tags: trunk
Changes

Modified src/Makefile from [6486149d8c] to [188c5944db].

21
22
23
24
25
26
27

28
29
30
31
32
33
34
       OFData.m				\
       OFData+CryptographicHashing.m	\
       OFData+MessagePackParsing.m	\
       OFDate.m				\
       OFDictionary.m			\
       OFEnumerator.m			\
       OFFileManager.m			\

       OFGZIPStream.m			\
       OFHMAC.m				\
       OFINICategory.m			\
       OFINIFile.m			\
       OFIRI.m				\
       OFIRIHandler.m			\
       OFInflate64Stream.m		\







>







21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
       OFData.m				\
       OFData+CryptographicHashing.m	\
       OFData+MessagePackParsing.m	\
       OFDate.m				\
       OFDictionary.m			\
       OFEnumerator.m			\
       OFFileManager.m			\
       OFGameController.m		\
       OFGZIPStream.m			\
       OFHMAC.m				\
       OFINICategory.m			\
       OFINIFile.m			\
       OFIRI.m				\
       OFIRIHandler.m			\
       OFInflate64Stream.m		\

Added src/OFGameController.h version [d675a51f7b].



































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#import "OFObject.h"
#import "OFString.h"

OF_ASSUME_NONNULL_BEGIN

/** @file */

@class OFSet OF_GENERIC(ObjectType);

/**
 * @brief A class for reading state from a game controller.
 */
OF_SUBCLASSING_RESTRICTED
@interface OFGameController: OFObject
#ifdef OF_HAVE_CLASS_PROPERTIES
@property (class, readonly, nonatomic) size_t numControllers;
#endif

/**
 * @brief The buttons the controller has.
 */
@property (readonly, nonatomic) OFSet OF_GENERIC(OFString *) *buttons;

/**
 * @brief The currently pressed buttons on the controller.
 */
@property (readonly, nonatomic) OFSet OF_GENERIC(OFString *) *pressedButtons;

/**
 * @brief The number of axes the controller has.
 */
@property (readonly, nonatomic) size_t numAxes;

/**
 * @brief Returns the number of available controllers.
 *
 * @return The number of available controllers
 */
+ (size_t)numControllers;

/**
 * @brief Returns the specified controller.
 *
 * @param index The index of the controller to return
 * @return The specified controller
 */
+ (OFGameController *)controllerWithIndex: (size_t)index;

- (instancetype)init OF_UNAVAILABLE;

/**
 * @brief Returns the current position of the specified axis.
 *
 * The range is from (-1, -1) to (1, 1).
 *
 * @param index The index of the axis whose position to return
 * @return The current position of the specified axis
 */
- (OFPoint)positionOfAxisWithIndex: (size_t)index;
@end

OF_ASSUME_NONNULL_END

Added src/OFGameController.m version [07a9f043f8].









































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#import "OFGameController.h"

#import "OFOutOfRangeException.h"

#ifdef OF_NINTENDO_3DS
# include "platform/3DS/OFGameController.m"
#else
@implementation OFGameController
@dynamic buttons, pressedButtons, numAxes;

+ (size_t)numControllers
{
	return 0;
}

+ (OFGameController *)controllerWithIndex: (size_t)index
{
	@throw [OFOutOfRangeException exception];
}

- (instancetype)init
{
	OF_INVALID_INIT_METHOD
}

- (OFPoint)positionOfAxisWithIndex: (size_t)index
{
	OF_UNRECOGNIZED_SELECTOR
}
@end
#endif

Modified src/ObjFW.h from [11ffe4203c] to [04ce1fb016].

144
145
146
147
148
149
150

151
152
153
154
155
156
157
#import "OFSystemInfo.h"
#import "OFLocale.h"
#import "OFOptionsParser.h"
#import "OFTimer.h"
#import "OFRunLoop.h"

#import "OFMatrix4x4.h"


#ifdef OF_WINDOWS
# import "OFWindowsRegistryKey.h"
#endif

#import "OFAllocFailedException.h"
#import "OFAlreadyOpenException.h"







>







144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#import "OFSystemInfo.h"
#import "OFLocale.h"
#import "OFOptionsParser.h"
#import "OFTimer.h"
#import "OFRunLoop.h"

#import "OFMatrix4x4.h"
#import "OFGameController.h"

#ifdef OF_WINDOWS
# import "OFWindowsRegistryKey.h"
#endif

#import "OFAllocFailedException.h"
#import "OFAlreadyOpenException.h"

Added src/platform/3DS/OFGameController.m version [9240091586].







































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#import "OFGameController.h"
#import "OFSet.h"

#import "OFOutOfRangeException.h"

#define id id_3ds
#include <3ds.h>
#undef id

@interface OFGameController ()
- (instancetype)of_init OF_METHOD_FAMILY(init);
@end

static OFGameController *controller;

static void
initController(void)
{
	controller = [[OFGameController alloc] of_init];
}

@implementation OFGameController
+ (size_t)numControllers
{
	return 1;
}

+ (OFGameController *)controllerWithIndex: (size_t)index
{
	static OFOnceControl onceControl = OFOnceControlInitValue;

	if (index > 0)
		@throw [OFOutOfRangeException exception];

	OFOnce(&onceControl, initController);

	return [[controller retain] autorelease];
}

- (instancetype)init
{
	OF_INVALID_INIT_METHOD
}

- (instancetype)of_init
{
	return [super init];
}

- (OFSet *)buttons
{
	return [OFSet setWithObjects: @"A", @"B", @"Select", @"Start",
	    @"D-Pad Right", @"D-Pad Left", @"D-Pad Up", @"D-Pad Down", @"R",
	    @"L", @"X", @"Y", @"ZL", @"ZR", @"C-Stick Right", @"C-Stick Left",
	    @"C-Stick Up", @"C-Stick Down", nil];
}

- (OFSet *)pressedButtons
{
	OFMutableSet *pressedButtons = [OFMutableSet setWithCapacity: 18];
	u32 keys;

	hidScanInput();
	keys = hidKeysHeld();

	if (keys & KEY_A)
		[pressedButtons addObject: @"A"];
	if (keys & KEY_B)
		[pressedButtons addObject: @"A"];
	if (keys & KEY_SELECT)
		[pressedButtons addObject: @"Select"];
	if (keys & KEY_START)
		[pressedButtons addObject: @"Start"];
	if (keys & KEY_DRIGHT)
		[pressedButtons addObject: @"D-Pad Right"];
	if (keys & KEY_DLEFT)
		[pressedButtons addObject: @"D-Pad Left"];
	if (keys & KEY_DUP)
		[pressedButtons addObject: @"D-Pad Up"];
	if (keys & KEY_DDOWN)
		[pressedButtons addObject: @"D-Pad Down"];
	if (keys & KEY_R)
		[pressedButtons addObject: @"R"];
	if (keys & KEY_L)
		[pressedButtons addObject: @"L"];
	if (keys & KEY_X)
		[pressedButtons addObject: @"X"];
	if (keys & KEY_Y)
		[pressedButtons addObject: @"Y"];
	if (keys & KEY_ZL)
		[pressedButtons addObject: @"ZL"];
	if (keys & KEY_ZR)
		[pressedButtons addObject: @"ZR"];
	if (keys & KEY_CSTICK_RIGHT)
		[pressedButtons addObject: @"C-Stick Right"];
	if (keys & KEY_CSTICK_LEFT)
		[pressedButtons addObject: @"C-Stick Left"];
	if (keys & KEY_CSTICK_UP)
		[pressedButtons addObject: @"C-Stick Up"];
	if (keys & KEY_CSTICK_DOWN)
		[pressedButtons addObject: @"C-Stick Down"];

	[pressedButtons makeImmutable];

	return pressedButtons;
}

- (size_t)numAxes
{
	return 1;
}

- (OFPoint)positionOfAxisWithIndex: (size_t)index
{
	circlePosition pos;

	if (index > 0)
		@throw [OFOutOfRangeException exception];

	hidCircleRead(&pos);

	return OFMakePoint(
	    (float)pos.dx / (pos.dx < 0 ? INT16_MIN : INT16_MAX),
	    (float)pos.dy / (pos.dy < 0 ? INT16_MIN : INT16_MAX));
}
@end

Modified src/test/OTAppDelegate.m from [c99ef75558] to [da9bf57bcb].

335
336
337
338
339
340
341
342


343
344
345
346
347

348
349
350
351
352
353
354
				break;
		}
#elif defined(OF_NINTENDO_3DS)
		[OFStdOut setForegroundColor: [OFColor silver]];
		[OFStdOut writeLine: @"Press A to continue"];

		for (;;) {
			hidScanInput();



			if (hidKeysDown() & KEY_A)
				break;

			gspWaitForVBlank();

		}
#elif defined(OF_NINTENDO_SWITCH)
		[OFStdOut setForegroundColor: [OFColor silver]];
		[OFStdOut writeLine: @"Press A to continue"];

		while (appletMainLoop()) {
			PadState pad;







|
>
>

|



>







335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
				break;
		}
#elif defined(OF_NINTENDO_3DS)
		[OFStdOut setForegroundColor: [OFColor silver]];
		[OFStdOut writeLine: @"Press A to continue"];

		for (;;) {
			void *pool = objc_autoreleasePoolPush();
			OFGameController *controller =
			    [OFGameController controllerWithIndex: 0];

			if ([controller.pressedButtons containsObject: @"A"])
				break;

			gspWaitForVBlank();
			objc_autoreleasePoolPop(pool);
		}
#elif defined(OF_NINTENDO_SWITCH)
		[OFStdOut setForegroundColor: [OFColor silver]];
		[OFStdOut writeLine: @"Press A to continue"];

		while (appletMainLoop()) {
			PadState pad;