Cube  Check-in [31ef5be209]

Overview
Comment:Clean up key mapping
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 31ef5be209e12c2b31695e15653e093d6bb96523a45513c06e3fbee1860935b2
User & Date: js on 2025-03-05 22:24:16
Other Links: manifest | tags
Context
2025-03-05
22:25
Merge accidental fork check-in: 7cfa53cdf1 user: js tags: trunk
22:24
Clean up key mapping check-in: 31ef5be209 user: js tags: trunk
21:55
Clean up console output functions check-in: 003b06901f user: js tags: trunk
Changes

Modified src/Cube.mm from [398e5608de] to [00adf80d1c].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// main.cpp: initialisation & main loop

#include "cube.h"

OF_APPLICATION_DELEGATE(Cube)

VARF(gamespeed, 10, 100, 1000, if (multiplayer()) gamespeed = 100);
VARP(minmillis, 0, 5, 1000);

@implementation Cube {
	int _width, _height;
	OFIRI *_userDataIRI;
}

+ (Cube *)sharedInstance
{
	return (Cube *)OFApplication.sharedApplication.delegate;
}












<







1
2
3
4
5
6
7
8
9
10
11

12
13
14
15
16
17
18
// main.cpp: initialisation & main loop

#include "cube.h"

OF_APPLICATION_DELEGATE(Cube)

VARF(gamespeed, 10, 100, 1000, if (multiplayer()) gamespeed = 100);
VARP(minmillis, 0, 5, 1000);

@implementation Cube {
	int _width, _height;

}

+ (Cube *)sharedInstance
{
	return (Cube *)OFApplication.sharedApplication.delegate;
}

Modified src/client.mm from [733d35b36f] to [b809582588].

73
74
75
76
77
78
79
80
81
82

83
84
85
86
87
88
89
	@autoreleasepool {
		strn0cpy(player1->team, name.UTF8String, 5);
	}
}
COMMANDN(team, newteam, ARG_1STR)

void
writeclientinfo(FILE *f)
{
	fprintf(f, "name \"%s\"\nteam \"%s\"\n", player1->name, player1->team);

}

void
connects(OFString *servername)
{
	disconnect(1); // reset state
	addserver(servername);







|

|
>







73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
	@autoreleasepool {
		strn0cpy(player1->team, name.UTF8String, 5);
	}
}
COMMANDN(team, newteam, ARG_1STR)

void
writeclientinfo(OFStream *stream)
{
	[stream writeFormat:@"name \"%s\"\nteam \"%s\"\n", player1->name,
	        player1->team];
}

void
connects(OFString *servername)
{
	disconnect(1); // reset state
	addserver(servername);

Modified src/command.mm from [931e4eb08f] to [c9d2804930].

581
582
583
584
585
586
587



588


589
590



591
592

593
594

595

596
597
598
599
600
601
602
603

604
605

606
607
608
609
610
611
612
613
614

615
616
617
618
619
620
621
622
		}
	}
}

void
writecfg()
{



	FILE *f = fopen("config.cfg", "w");


	if (!f)
		return;



	fprintf(f, "// automatically written on exit, do not modify\n// delete "
	           "this file to have defaults.cfg overwrite these "

	           "settings\n// modify settings in game, or put settings in "
	           "autoexec.cfg to override anything\n\n");

	writeclientinfo(f);

	fprintf(f, "\n");
	[idents enumerateKeysAndObjectsUsingBlock:^(
	    OFString *name, Ident *ident, bool *stop) {
		if (ident.type == ID_VAR && ident.persist) {
			fprintf(f, "%s %d\n", ident.name.UTF8String,
			    *ident.storage);
		}
	}];

	fprintf(f, "\n");
	writebinds(f);

	fprintf(f, "\n");
	[idents enumerateKeysAndObjectsUsingBlock:^(
	    OFString *name, Ident *ident, bool *stop) {
		if (ident.type == ID_ALIAS &&
		    !strstr(ident.name.UTF8String, "nextmap_")) {
			fprintf(f, "alias \"%s\" [%s]\n", ident.name.UTF8String,
			    ident.action.UTF8String);
		}
	}];

	fclose(f);
}

COMMAND(writecfg, ARG_NONE)

// below the commands that implement a small imperative language. thanks to the
// semantics of
// () and [] expressions, any control construct can be defined trivially.







>
>
>
|
>
>
|

>
>
>
|
|
>
|
|
>
|
>
|



|
|


>
|
|
>
|



|
|
|
<

>
|







581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625

626
627
628
629
630
631
632
633
634
635
		}
	}
}

void
writecfg()
{
	OFStream *stream;
	@try {
		OFIRI *IRI = [Cube.sharedInstance.userDataIRI
		    IRIByAppendingPathComponent:@"config.cfg"];
		stream = [[OFIRIHandler handlerForIRI:IRI] openItemAtIRI:IRI
		                                                    mode:@"w"];
	} @catch (id e) {
		return;
	}

	[stream writeString:
	            @"// automatically written on exit, do not modify\n"
	            @"// delete this file to have defaults.cfg overwrite these "
	            @"settings\n"
	            @"// modify settings in game, or put settings in "
	            @"autoexec.cfg to override anything\n"
	            @"\n"];
	writeclientinfo(stream);
	[stream writeString:@"\n"];

	[idents enumerateKeysAndObjectsUsingBlock:^(
	    OFString *name, Ident *ident, bool *stop) {
		if (ident.type == ID_VAR && ident.persist) {
			[stream
			    writeFormat:@"%@ %d\n", ident.name, *ident.storage];
		}
	}];
	[stream writeString:@"\n"];

	writebinds(stream);
	[stream writeString:@"\n"];

	[idents enumerateKeysAndObjectsUsingBlock:^(
	    OFString *name, Ident *ident, bool *stop) {
		if (ident.type == ID_ALIAS &&
		    ![ident.name hasPrefix:@"nextmap_"])
			[stream writeFormat:@"alias \"%@\" [%@]\n", ident.name,
			        ident.action];

	}];

	[stream close];
}

COMMAND(writecfg, ARG_NONE)

// below the commands that implement a small imperative language. thanks to the
// semantics of
// () and [] expressions, any control construct can be defined trivially.

Modified src/console.mm from [ea51019800] to [20418af9a4].

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
		draw_text(refs[j], FONTH / 3,
		    (FONTH / 4 * 5) * (nd - j - 1) + FONTH / 3, 2);
	};
};

// keymap is defined externally in keymap.cfg





struct keym {








	int code;
	char *name;
	char *action;


} keyms[256];
int numkm = 0;


void
keymap(OFString *code, OFString *key, OFString *action)
{
	@autoreleasepool {
		keyms[numkm].code = (int)code.longLongValue;
		keyms[numkm].name = newstring(key.UTF8String);
		keyms[numkm++].action = newstringbuf(action.UTF8String);

	}




}
COMMAND(keymap, ARG_3STR)

void
bindkey(OFString *key_, OFString *action)
{
	@autoreleasepool {
		std::unique_ptr<char> key(strdup(key_.UTF8String));
		for (char *x = key.get(); *x; x++)

			*x = toupper(*x);
		loopi(numkm) if (strcmp(keyms[i].name, key.get()) == 0)
		{
			strcpy_s(keyms[i].action, action.UTF8String);
			return;
		}
		conoutf(@"unknown key \"%s\"", key.get());
	}


}
COMMANDN(bind, bindkey, ARG_2STR)

void
saycommand(char *init) // turns input to the command line on or off
{
	saycommandon = (init != NULL);







>
>
>
>
|
>
>
>
>
>
>
>
>
|
|
|
>
>
|
|
>




<
<
|
<
>
|
>
>
>
>




|

<
<
|
>
|
<
<
|


<

>
>







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
148
149
		draw_text(refs[j], FONTH / 3,
		    (FONTH / 4 * 5) * (nd - j - 1) + FONTH / 3, 2);
	};
};

// keymap is defined externally in keymap.cfg

@interface KeyMapping : OFObject
@property (readonly) int code;
@property (readonly, nonatomic) OFString *name;
@property (copy, nonatomic) OFString *action;

- (instancetype)initWithCode:(int)code name:(OFString *)name;
@end

@implementation KeyMapping
- (instancetype)initWithCode:(int)code name:(OFString *)name
{
	self = [super init];

	_code = code;
	_name = [name copy];

	return self;
}
@end

static OFMutableArray<KeyMapping *> *keyMappings = nil;

void
keymap(OFString *code, OFString *key, OFString *action)
{


	if (keyMappings == nil)

		keyMappings = [[OFMutableArray alloc] init];

	KeyMapping *mapping =
	    [[KeyMapping alloc] initWithCode:(int)code.longLongValue name:key];
	mapping.action = action;
	[keyMappings addObject:mapping];
}
COMMAND(keymap, ARG_3STR)

void
bindkey(OFString *key, OFString *action)
{


	for (KeyMapping *mapping in keyMappings) {
		if ([mapping.name caseInsensitiveCompare:key] ==
		    OFOrderedSame) {


			mapping.action = action;
			return;
		}

	}

	conoutf(@"unknown key \"%@\"", key);
}
COMMANDN(bind, bindkey, ARG_2STR)

void
saycommand(char *init) // turns input to the command line on or off
{
	saycommandon = (init != NULL);
240
241
242
243
244
245
246
247

248

249
250
251
252
253
254
255
256
257
258
259




260
261
262
263
264
265
266

267
268
269
270
271
272
273
274
275
276

						toserver(commandbuf);
				};
				saycommand(NULL);
			} else if (code == SDLK_ESCAPE) {
				saycommand(NULL);
			};
		};
	} else if (!menukey(code, isdown)) // keystrokes go to menu

	{

		loopi(numkm) if (keyms[i].code ==
		                 code) // keystrokes go to game, lookup in
		                       // keymap and execute
		{
			string temp;
			strcpy_s(temp, keyms[i].action);
			execute(temp, isdown);
			return;
		};
	};
};





char *
getcurcommand()
{
	return saycommandon ? commandbuf : NULL;
};


void
writebinds(FILE *f)
{
	loopi(numkm)
	{
		if (*keyms[i].action)
			fprintf(f, "bind \"%s\" [%s]\n", keyms[i].name,
			    keyms[i].action);
	};
};








|
>
|
>
|
|
|
<
|
|
|
|
<
<
<
>
>
>
>





<
|
>

|

|
<
|
|
|
<
<
>
255
256
257
258
259
260
261
262
263
264
265
266
267
268

269
270
271
272



273
274
275
276
277
278
279
280
281

282
283
284
285
286
287

288
289
290


291
						toserver(commandbuf);
				};
				saycommand(NULL);
			} else if (code == SDLK_ESCAPE) {
				saycommand(NULL);
			};
		};
	} else if (!menukey(code, isdown)) {
		// keystrokes go to menu

		for (KeyMapping *mapping in keyMappings) {
			if (mapping.code == code) {
				// keystrokes go to game, lookup in keymap and
				// execute

				string temp;
				strcpy_s(temp, mapping.action.UTF8String);
				execute(temp, isdown);
				return;



			}
		}
	}
}

char *
getcurcommand()
{
	return saycommandon ? commandbuf : NULL;

}

void
writebinds(OFStream *stream)
{
	for (KeyMapping *mapping in keyMappings)

		if (mapping.action.length > 0)
			[stream writeFormat:@"bind \"%@\" [%@]\n", mapping.name,
			        mapping.action];


}

Modified src/cube.h from [f1d0278915] to [a3e2530d8c].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// one big bad include file for the whole engine... nasty!

#import <ObjFW/ObjFW.h>

#define gamma gamma__
#include <SDL2/SDL.h>
#undef gamma

#include "tools.h"

@interface Cube : OFObject <OFApplicationDelegate>
@property (class, readonly, nonatomic) Cube *sharedInstance;
@property (readonly, nonatomic) SDL_Window *window;
@property (readonly, nonatomic) OFIRI *gameDataIRI;
@property (nonatomic) bool repeatsKeys;
@property (nonatomic) int framesInMap;
@end

enum // block types, order matters!
{
	SOLID = 0, // entirely solid cube [only specifies wtex]













|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// one big bad include file for the whole engine... nasty!

#import <ObjFW/ObjFW.h>

#define gamma gamma__
#include <SDL2/SDL.h>
#undef gamma

#include "tools.h"

@interface Cube : OFObject <OFApplicationDelegate>
@property (class, readonly, nonatomic) Cube *sharedInstance;
@property (readonly, nonatomic) SDL_Window *window;
@property (readonly, nonatomic) OFIRI *gameDataIRI, *userDataIRI;
@property (nonatomic) bool repeatsKeys;
@property (nonatomic) int framesInMap;
@end

enum // block types, order matters!
{
	SOLID = 0, // entirely solid cube [only specifies wtex]

Modified src/protos.h from [c2d6bbe699] to [caa79545eb].

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
extern void writecfg();

// console
extern void keypress(int code, bool isdown, int cooked);
extern void renderconsole();
extern void conoutf(OFConstantString *format, ...);
extern char *getcurcommand();
extern void writebinds(FILE *f);

// init
extern void enqueueInit(void (^init)(void));
extern void processInitQueue(void);

// menus
extern bool rendermenu();







|







17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
extern void writecfg();

// console
extern void keypress(int code, bool isdown, int cooked);
extern void renderconsole();
extern void conoutf(OFConstantString *format, ...);
extern char *getcurcommand();
extern void writebinds(OFStream *stream);

// init
extern void enqueueInit(void (^init)(void));
extern void processInitQueue(void);

// menus
extern bool rendermenu();
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
extern void gets2c();
extern void c2sinfo(dynent *d);
extern void neterr(OFString *s);
extern void initclientnet();
extern bool netmapstart();
extern int getclientnum();
extern void changemapserv(char *name, int mode);
extern void writeclientinfo(FILE *f);

// clientgame
extern void mousemove(int dx, int dy);
extern void updateworld(int millis);
extern void startmap(char *name);
extern void changemap(OFString *name);
extern void initclient();







|







79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
extern void gets2c();
extern void c2sinfo(dynent *d);
extern void neterr(OFString *s);
extern void initclientnet();
extern bool netmapstart();
extern int getclientnum();
extern void changemapserv(char *name, int mode);
extern void writeclientinfo(OFStream *stream);

// clientgame
extern void mousemove(int dx, int dy);
extern void updateworld(int millis);
extern void startmap(char *name);
extern void changemap(OFString *name);
extern void initclient();