Cube  Diff

Differences From Artifact [fbd5eecbef]:

To Artifact [6b830d51dc]:


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
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









+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+




















+
-
+

-
+
-
-
-
-
-
+
+
+
+
+
+

+
-
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+







// console.cpp: the console buffer, its display, and command line control

#include "cube.h"

#include <ctype.h>

#import "KeyMapping.h"
#import "OFString+Cube.h"

@interface ConsoleLine: OFObject
@property (readonly, copy) OFString *text;
@property (readonly) int outtime;
struct cline {
	char *cref;
	int outtime;
};
vector<cline> conlines;

- (instancetype)initWithText:(OFString *)text outtime:(int)outtime;
@end

static OFMutableArray<ConsoleLine *> *conlines;

@implementation ConsoleLine
- (instancetype)initWithText:(OFString *)text outtime:(int)outtime
{
	self = [super init];

	_text = [text copy];
	_outtime = outtime;

	return self;
}

- (OFString *)description
{
	return _text;
}
@end

const int ndraw = 5;
const int WORDWRAP = 80;
int conskip = 0;

bool saycommandon = false;
static OFMutableString *commandbuf;

void
setconskip(int n)
{
	conskip += n;
	if (conskip < 0)
		conskip = 0;
}
COMMANDN(conskip, setconskip, ARG_1INT)

static void
conline(OFString *sf, bool highlight) // add a line to the console buffer
{
	OFMutableString *text;
	cline cl;

	// constrain the buffer size
	cl.cref = conlines.length() > 100 ? conlines.pop().cref
	if (conlines.count > 100) {
	                                  : (char *)calloc(_MAXDEFSTR, 1);
	// for how long to keep line on screen
	cl.outtime = lastmillis;
	conlines.insert(0, cl);
	if (highlight) {
		text = [conlines.lastObject.text mutableCopy];
		[conlines removeLastObject];
	} else
		text = [[OFMutableString alloc] init];

	if (highlight)
		// show line in a different colour, for chat etc.
		[text appendString:@"\f"];
		cl.cref[0] = '\f';

		cl.cref[1] = 0;
		strcat_s(cl.cref, sf.UTF8String);
	} else {
		strcpy_s(cl.cref, sf.UTF8String);
	}
	puts(cl.cref);
	[text appendString:sf];

	if (conlines == nil)
		conlines = [[OFMutableArray alloc] init];

	[conlines insertObject:[[ConsoleLine alloc] initWithText:text
	                                                 outtime:lastmillis]
	               atIndex:0];

	puts(text.UTF8String);
#ifndef OF_WINDOWS
	fflush(stdout);
#endif
}

void
conoutf(OFConstantString *format, ...)
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
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







+

-
+


-
+
-
-
-
-
-
+
+
+
+
+
+



-
-
-
-
-
-
+
+
+
+
+
+
+
+
-







			conline([string substringToIndex:WORDWRAP], n++ != 0);
			string = [string substringFromIndex:WORDWRAP];
		}
		conline(string, n != 0);
	}
}

// render buffer taking into account time & scrolling
void
renderconsole() // render buffer taking into account time & scrolling
renderconsole()
{
	int nd = 0;
	char *refs[ndraw];
	OFString *refs[ndraw];
	loopv(conlines)
	{
		if (conskip ? i >= conskip - 1 || i >= conlines.length() - ndraw
		            : lastmillis - conlines[i].outtime < 20000) {
			refs[nd++] = conlines[i].cref;

	size_t i = 0;
	for (ConsoleLine *conline in conlines) {
		if (conskip ? i >= conskip - 1 || i >= conlines.count - ndraw
		            : lastmillis - conline.outtime < 20000) {
			refs[nd++] = conline.text;
			if (nd == ndraw)
				break;
		}
	}
	@autoreleasepool {
		loopj(nd)
		{
			draw_text(@(refs[j]), FONTH / 3,
			    (FONTH / 4 * 5) * (nd - j - 1) + FONTH / 3, 2);

		i++;
	}

	loopj(nd)
	{
		draw_text(refs[j], FONTH / 3,
		    (FONTH / 4 * 5) * (nd - j - 1) + FONTH / 3, 2);
		}
	}
}

// keymap is defined externally in keymap.cfg

static OFMutableArray<KeyMapping *> *keyMappings = nil;