aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/quest/quest.cpp
blob: cbddedaba849ff18a1f1e1bee18ee28e00b0821a (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
/* 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/quest/quest.h"
#include "glk/quest/geas_glk.h"
#include "glk/quest/geas_glk.h"
#include "glk/quest/streams.h"
#include "common/config-manager.h"
#include "common/translation.h"

namespace Glk {
namespace Quest {

Quest *g_vm;

Quest::Quest(OSystem *syst, const GlkGameDescription &gameDesc) : GlkAPI(syst, gameDesc), _saveSlot(-1) {
	g_vm = this;
}

void Quest::runGame() {
	// Check for savegame
	_saveSlot = ConfMan.hasKey("save_slot") ? ConfMan.getInt("save_slot") : -1;

	if (!initialize()) {
		GUIErrorMessage(_("Could not start Quest game"));
		return;
	}

	playGame();

	deinitialize();
}

void Quest::playGame() {
	char cur_buf[1024];
	char buf[200];

	GeasRunner *gr = GeasRunner::get_runner(new GeasGlkInterface());
	gr->set_game(String(getFilename().c_str()));
	banner = gr->get_banner();
	draw_banner();

	while (gr->is_running()) {
		if (inputwin != mainglkwin)
			glk_window_clear(inputwin);
		else
			glk_put_cstring("\n");

		sprintf(cur_buf, "> ");
		glk_put_string_stream(inputwinstream, cur_buf);

		glk_request_line_event(inputwin, buf, (sizeof buf) - 1, 0);

		event_t ev;
		ev.type = evtype_None;

		while (ev.type != evtype_LineInput) {
			glk_select(&ev);
			if (shouldQuit())
				return;

			switch (ev.type) {
			case evtype_LineInput:
				if (ev.window == inputwin) {
					String cmd = String(buf, ev.val1);
					if (inputwin == mainglkwin)
						ignore_lines = 2;
					gr->run_command(cmd);
				}
				break;

			case evtype_Timer:
				gr->tick_timers();
				break;

			case evtype_Arrange:
			case evtype_Redraw:
				draw_banner();
				break;
			}
		}
	}
}

bool Quest::initialize() {
	Streams::initialize();
	glk_stylehint_set(wintype_TextBuffer, style_User2, stylehint_ReverseColor, 1);

	// Open the main window
	mainglkwin = glk_window_open(0, 0, 0, wintype_TextBuffer, 1);
	if (!mainglkwin)
		return false;
	glk_set_window(mainglkwin);

	glk_stylehint_set(wintype_TextGrid, style_User1, stylehint_ReverseColor, 1);
	bannerwin = glk_window_open(mainglkwin,
		winmethod_Above | winmethod_Fixed,
		1, wintype_TextGrid, 0);

	if (use_inputwindow)
		inputwin = glk_window_open(mainglkwin,
			winmethod_Below | winmethod_Fixed,
			1, wintype_TextBuffer, 0);
	else
		inputwin = NULL;

	if (!inputwin)
		inputwin = mainglkwin;

	inputwinstream = glk_window_get_stream(inputwin);

	if (!glk_gestalt(gestalt_Timer, 0)) {
		const char *err = "\nNote -- The underlying Glk library does not support"
			" timers.  If this game tries to use timers, then some"
			" functionality may not work correctly.\n\n";
		glk_put_string(err);
	}

	glk_request_timer_events(1000);
	ignore_lines = 0;

	return true;
}

void Quest::deinitialize() {
	Streams::deinitialize();
}

} // End of namespace Quest
} // End of namespace Glk