MCPcopy Create free account
hub / github.com/bwapi/bwapi / BroodwarOutputDevice

Class BroodwarOutputDevice

bwapi/include/BWAPI/BroodwarOutputDevice.h:18–86  ·  view source on GitHub ↗

Output device for writing text to the screen as a notification. Normally when you write stuff in Starcraft, the text wraps at word boundaries when it would otherwise exceed the screen width. When this happens any Text::Enum color in the message is lost and the next line will be some default color. To fix this color problem this device keeps track of the width of the text in the

Source from the content-addressed store, hash-verified

16 /// then colors the next line with the current color again before continuing to write.
17 /// @since 4.2.0
18 class BroodwarOutputDevice : public boost::iostreams::sink
19 {
20 static const int MAX_WIDTH = 620; //620 is the max width before starcraft wraps
21 static const int DEFAULT_FONT_CHAR_PRINTING_WIDTHS[];
22 static const int DEFAULT_FONT_MAX_WIDTH = 10;
23
24 int bufferWidth = 0;
25 std::stringbuf buffer;
26 const Text::Enum defaultColor;
27 boost::circular_buffer<Text::Enum> previousColors;
28
29 public:
30 explicit BroodwarOutputDevice(Text::Enum defaultColor = Text::Yellow)
31 : defaultColor(Text::isColor(defaultColor) ? defaultColor : Text::Yellow)
32 , previousColors(128) //arbitrary stack size
33 {
34 buffer.sputc(char(defaultColor));
35 previousColors.push_back(defaultColor);
36 }
37
38 BroodwarOutputDevice(const BroodwarOutputDevice& o)
39 : BroodwarOutputDevice(o.defaultColor)
40 {}
41
42 std::streamsize write(const char* s, std::streamsize n)
43 {
44 for (int j = 0; j < n; j++)
45 {
46 char c = s[j];
47
48 //push/pop colors
49 if (Text::isColor(Text::Enum(c)))
50 {
51 previousColors.push_back(Text::Enum(c));
52 }
53 else if (c == Text::Previous)
54 {
55 previousColors.pop_back();
56 if (previousColors.empty()) //always keep a color on the stack
57 previousColors.push_back(defaultColor);
58 c = char(previousColors.back());
59 }
60
61 if (c == '\n' || bufferWidth + charPrintingWidth(c) > MAX_WIDTH)
62 {
63 BroodwarPtr->printf("%s", buffer.str().c_str());
64 buffer.str("");
65 buffer.sputc(char(previousColors.back()));
66 bufferWidth = 0;
67 }
68
69 if (c != '\n')
70 {
71 buffer.sputc(c);
72 bufferWidth += charPrintingWidth(c);
73 }
74 }
75 return n;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected