aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/alan3/glkio.cpp
blob: 3f0da033335e28b5d48af2faea9cd8b5298a9486 (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "glk/alan3/glkio.h"
#include "glk/alan3/acode.h"
#include "glk/alan3/current.h"
#include "glk/alan3/instance.h"
#include "glk/alan3/options.h"
#include "glk/alan3/output.h"
#include "common/config-manager.h"

namespace Glk {
namespace Alan3 {

GlkIO *g_io;

GlkIO::GlkIO(OSystem *syst, const GlkGameDescription &gameDesc) : GlkAPI(syst, gameDesc),
		glkMainWin(nullptr), glkStatusWin(nullptr), onStatusLine(false), _saveSlot(-1),
		_soundChannel(nullptr) {
	g_io = this;
}

bool GlkIO::initialize() {
	// first, open a window for error output
	glkMainWin = glk_window_open(0, 0, 0, wintype_TextBuffer, 0);
	if (glkMainWin == nullptr)
		return false;

	glk_stylehint_set(wintype_TextGrid, style_User1, stylehint_ReverseColor, 1);
	glkStatusWin = glk_window_open(glkMainWin, winmethod_Above |
		winmethod_Fixed, 1, wintype_TextGrid, 0);
	glk_set_window(glkMainWin);

	// Check for a save being loaded directly from the launcher
	_saveSlot = ConfMan.hasKey("save_slot") ? ConfMan.getInt("save_slot") : -1;

	return true;
}

void GlkIO::print(const char *fmt, ...) {
	// If there's a savegame being loaded from the launcher, ignore any text out
	if (_saveSlot != -1)
		return;

	va_list argp;
	va_start(argp, fmt);
	Common::String str = Common::String::vformat(fmt, argp);
	va_end(argp);

	if (glkMainWin) {
		glk_put_string(str.c_str());
	} else {
		// assume stdio is available in this case only
		warning("%s", str.c_str());
	}
}

void GlkIO::showImage(int image, int align) {
	uint ecode;
	if (_saveSlot != -1)
		return;

	if ((glk_gestalt(gestalt_Graphics, 0) == 1) &&
		(glk_gestalt(gestalt_DrawImage, wintype_TextBuffer) == 1)) {
		glk_window_flow_break(glkMainWin);
		printf("\n");
		ecode = glk_image_draw(glkMainWin, image, imagealign_MarginLeft, 0);
		(void)ecode;
	}
}

void GlkIO::playSound(int sound) {
	if (_saveSlot != -1)
		return;

#ifdef GLK_MODULE_SOUND
	if (glk_gestalt(gestalt_Sound, 0) == 1) {
		if (_soundChannel == nullptr)
			_soundChannel = glk_schannel_create(0);
		if (_soundChannel) {
			glk_schannel_stop(_soundChannel);
			(void)glk_schannel_play(_soundChannel, sound);
		}
	}
#endif
}

void GlkIO::setStyle(int style) {
	switch (style) {
	case NORMAL_STYLE:
		glk_set_style(style_Normal);
		break;
	case EMPHASIZED_STYLE:
		glk_set_style(style_Emphasized);
		break;
	case PREFORMATTED_STYLE:
		glk_set_style(style_Preformatted);
		break;
	case ALERT_STYLE:
		glk_set_style(style_Alert);
		break;
	case QUOTE_STYLE:
		glk_set_style(style_BlockQuote);
		break;
	}
}

void GlkIO::statusLine(CONTEXT) {
	uint glkWidth;
	char line[100];
	int pcol = col;

	if (!statusLineOption || _saveSlot != -1 || glkStatusWin == nullptr)
		return;

	glk_set_window(glkStatusWin);
	glk_window_clear(glkStatusWin);
	glk_window_get_size(glkStatusWin, &glkWidth, NULL);

	onStatusLine = TRUE;
	col = 1;
	glk_window_move_cursor(glkStatusWin, 1, 0);
	CALL1(sayInstance, where(HERO, /*TRUE*/ TRANSITIVE))

		// TODO Add status message1  & 2 as author customizable messages
		if (header->maximumScore > 0)
			sprintf(line, "Score %d(%d)/%d moves", current.score, (int)header->maximumScore, current.tick);
		else
			sprintf(line, "%d moves", current.tick);
	glk_window_move_cursor(glkStatusWin, glkWidth - strlen(line) - 1, 0);
	glk_put_string(line);
	needSpace = FALSE;

	col = pcol;
	onStatusLine = FALSE;

	glk_set_window(glkMainWin);
}


/*======================================================================

  readline()

  Read a line from the user, with history and editing

*/

/* TODO - length of user buffer should be used */
bool GlkIO::readLine(CONTEXT, char *buffer, size_t maxLen) {
	event_t event;
	static bool readingCommands = FALSE;
	static frefid_t commandFileRef;
	static strid_t commandFile;

	if (_saveSlot != -1) {
		// Return a "restore" command
		forcePrint("> ");
		forcePrint("restore\n");
		strcpy(buffer, "restore");

	} else if (readingCommands) {
		if (glk_get_line_stream(commandFile, buffer, maxLen) == 0) {
			glk_stream_close(commandFile, NULL);
			readingCommands = FALSE;
		} else {
			glk_set_style(style_Input);
			printf(buffer);
			glk_set_style(style_Normal);
		}

	} else {
		glk_request_line_event(glkMainWin, buffer, maxLen, 0);

		do {
			glk_select(&event);
			if (shouldQuit())
				LONG_JUMP0

			switch (event.type) {
			case evtype_Arrange:
				R0CALL0(g_io->statusLine)
				break;

			default:
				break;
			}
		} while (event.type != evtype_LineInput);
		if (buffer[0] == '@') {
			buffer[event.val1] = 0;
			commandFileRef = glk_fileref_create_by_name(fileusage_InputRecord + fileusage_TextMode, &buffer[1], 0);
			commandFile = glk_stream_open_file(commandFileRef, filemode_Read, 0);
			if (commandFile != NULL)
				if (glk_get_line_stream(commandFile, buffer, maxLen) != 0) {
					readingCommands = TRUE;
					glk_set_style(style_Input);
					printf(buffer);
					glk_set_style(style_Normal);
				}
		} else
			buffer[event.val1] = 0;
	}
	return TRUE;
}

Common::Error GlkIO::loadGame() {
	if (_saveSlot != -1) {
		int saveSlot = _saveSlot;
		_saveSlot = -1;
		return loadGameState(saveSlot);
	} else {
		return GlkAPI::loadGame();
	}
}

} // End of namespace Alan3
} // End of namespace Glk