Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: a0cc7c50a99ad2d6f7e29ea4331eec09a2b06dd9 (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
/*
   client/console.cc
   This file is part of the Osirion project and is distributed under 
   the terms and conditions of the GNU General Public License version 2 
*/

#include "client/client.h"
#include "client/console.h"
#include "core/core.h"
#include "render/render.h"

#include <iostream>

namespace client {

Console::Console() {
	visible = false;
}

std::ostream & Console::messagestream()
{
	return (buffer << ". ");
}

std::ostream & Console::warningstream()
{
	return (buffer << "* ");
}

std::ostream & Console::errorstream()
{
	return (buffer << "! ");
}

std::ostream & Console::debugstream()
{
	return (buffer << "? ");
}

void Console::draw()
{
	using namespace render;
	
	// flush console messages in the buffer
	flush();

	float height;
	bool showloader = false;

	if (core::game()) {
		if (!core::game()->ready()) {
			height = 0.6f;
			showloader = true;
		} else if (visible)
			height = 0.6f;
		else
			height = 0.0f;
	} else {
		showloader = true;
		height = 1.0f;
	}

	if (showloader) {
		// draw the loader background
		gl::color(0.5f, 0.5f, 0.5f, 1.0f);

		gl::begin(gl::Quads);
		gl::vertex(0,0, 0);
		gl::vertex(video.width,0,0);
		gl::vertex(video.width,video.height,0);
		gl::vertex(0,video.height,0);
		gl::end();	
	}
	
	if (height > 0) {
		// draw version below the bottom of the console
		gl::color(0.0f, 1.0f, 0.0f, 1.0f);
		std:: string version = std::string("The Osirion Project ");
		version.append(PACKAGE_VERSION);
		draw_text(video.width-CHARSIZE*(version.size()+1), video.height*height-CHARSIZE-4, version);

		// draw the transparent console background
		gl::color(1, 1, 1, .5);
		
		gl::enable(GL_TEXTURE_2D);
		glBindTexture(GL_TEXTURE_2D, render::textures[0]); // bitmaps/loader.tga

		gl::begin(gl::Quads);
		glTexCoord2f(0.0f, 0.0f);
		gl::vertex(0,0, 0);
		
		glTexCoord2f(1.0f, 0.0f);
		gl::vertex(video.width,0,0);
		
		glTexCoord2f(1.0f, 1.0f);
		gl::vertex(video.width,video.height*height,0);
		
		glTexCoord2f(0.0f, 1.0f);
		gl::vertex(0,video.height*height,0);
		
		gl::end();

		glBindTexture(GL_TEXTURE_2D, render::textures[1]); // bitmaps/conchars.tga
		gl::enable(GL_BLEND);
		
		// draw the console text
		gl::color(1,1,1,1);
		std::deque<std::string>::reverse_iterator rit = console.text.rbegin();
		float y = video.height*height-2*CHARSIZE-8;
		while (y > 0 && rit < console.text.rend()) {
			draw_text(0, y, *rit);
			y -= CHARSIZE;
			++rit;
		}

		// draw the console input
		gl::color(0.0f, 1.0f, 0.0f, 1.0f);
		draw_text(0, video.height*height - CHARSIZE - 4, input);
		
		gl::disable(GL_TEXTURE_2D);
		gl::disable(GL_BLEND);
	}
	
}

void Console::flush() 
{
	char line[MAXCMDSIZE];
	while(this->buffer.getline(line, MAXCMDSIZE-1)) {
		
		while (text.size() >= MAXCONLINES) {
			text.pop_front();
		}
		text.push_back(std::string(line));
		std::cout << line << std::endl;
	}
	
	buffer.clear();
}

void Console::toggle()
{
	visible = !visible;
}

void Console::handle_keyreleased(SDL_keysym* keysym)
{
	switch( keysym->sym ) {
	case '`':
	case '~':
		toggle();
		return;
		break;

	case SDLK_RETURN:
		if (input.size()) {
			core::cmd << input << std::endl;
			input.clear();	
		}
		break;
	case SDLK_BACKSPACE:
		if (input.size()) {
			input.erase(input.size()-1, 1);
		}
		break;
	default:
		break;
	}

	if (keysym->sym >= 32 && keysym->sym <= 175) {
		input += (char) keysym->sym;
	}
}

} // namespace client