File: display.cpp

package info (click to toggle)
megaglest 3.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 12,904 kB
  • ctags: 18,215
  • sloc: cpp: 144,232; ansic: 11,860; sh: 2,949; perl: 1,899; python: 1,751; objc: 142; asm: 42; makefile: 24
file content (184 lines) | stat: -rw-r--r-- 5,662 bytes parent folder | download | duplicates (3)
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
// ==============================================================
//	This file is part of Glest (www.glest.org)
//
//	Copyright (C) 2001-2008 MartiƱo Figueroa
//
//	You can redistribute this code 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
// ==============================================================

#include "display.h"

#include "metrics.h"
#include "command_type.h"
#include "util.h"
#include "leak_dumper.h"

using namespace Shared::Graphics;
using namespace Shared::Util;

namespace Glest{ namespace Game{

// =====================================================
// 	class Display
// =====================================================

Display::Display(){
	if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);

	colors[0]= Vec4f(1.f, 1.f, 1.f, 0.0f);
	colors[1]= Vec4f(1.f, 0.5f, 0.5f, 0.0f);
	colors[2]= Vec4f(0.5f, 0.5f, 1.0f, 0.0f);
	colors[3]= Vec4f(0.5f, 1.0f, 0.5f, 0.0f);
	colors[4]= Vec4f(0.0f, 0.0f, 0.0f, 1.0f);
	colors[5]= Vec4f(0.0f, 0.0f, 1.0f, 1.0f);
	colors[6]= Vec4f(1.0f, 0.0f, 0.0f, 1.0f);
	colors[7]= Vec4f(0.0f, 1.0f, 0.0f, 1.0f);
	colors[8]= Vec4f(1.0f, 1.0f, 1.0f, 1.0f);

	currentColor= 0;
	clear();

	if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}

void Display::calculateUpDimensions(int index) {
	if(index>maxUpIndex){
		maxUpIndex=index;
		if(maxUpIndex+1>upCellSideCount*upCellSideCount){
			upCellSideCount=upCellSideCount+1;
			upImageSize = static_cast<float>(imageSize) * static_cast<float>(cellSideCount) / static_cast<float>(upCellSideCount) + 0.9f;
		}
	}
}

Vec4f Display::getColor() const {
	if(currentColor < 0 || currentColor >= colorCount) {
		throw megaglest_runtime_error("currentColor >= colorCount");
	}
	return colors[currentColor];
}

void Display::setUpImage(int i, const Texture2D *image)
{
	if(i>=upCellCount) throw megaglest_runtime_error("i>=upCellCount in Display::setUpImage");
	upImages[i]= image;
	calculateUpDimensions(i);
}

//misc
void Display::clear(){
	downSelectedPos= invalidPos;
	for(int i=0; i<upCellCount; ++i){
		upImages[i]= NULL;
	}

	for(int i=0; i<downCellCount; ++i){
		downImages[i]= NULL;
		downLighted[i]= true;
		commandTypes[i]= NULL;
		commandClasses[i]= ccNull;
	}

	title.clear();
	text.clear();
	progressBar= -1;

	upCellSideCount=cellSideCount;
	upImageSize=imageSize;
	maxUpIndex= 0;
}
void Display::switchColor(){
	currentColor= (currentColor+1) % colorCount;
}

int Display::computeDownIndex(int x, int y) const {
	y= y-(downY-cellSideCount*imageSize);
	
	if(y>imageSize*cellSideCount || y < 0){
		return invalidPos;
	}

	int cellX= x/imageSize;
	int cellY= (y/imageSize) % cellSideCount;
	int index= (cellSideCount-cellY-1)*cellSideCount+cellX;;

	if(index<0 || index>=downCellCount || downImages[index]==NULL){
		index= invalidPos;
	}

	return index;
}

int Display::computeDownX(int index) const{
	return (index % cellSideCount) * imageSize;
}

int Display::computeDownY(int index) const{
	return Display::downY - (index/cellSideCount)*imageSize - imageSize;
}

int Display::computeUpX(int index) const{
	return (index % upCellSideCount) * upImageSize;
}

int Display::computeUpY(int index) const{
	return Metrics::getInstance().getDisplayH() - (index/upCellSideCount)*upImageSize - upImageSize;
}

void Display::saveGame(XmlNode *rootNode) const {
	std::map<string,string> mapTagReplacements;
	XmlNode *displayNode = rootNode->addChild("Display");

//	string title;
	displayNode->addAttribute("title",title, mapTagReplacements);
//	string text;
	displayNode->addAttribute("text",text, mapTagReplacements);
//	string infoText;
	displayNode->addAttribute("infoText",infoText, mapTagReplacements);
//	const Texture2D *upImages[upCellCount];
//	const Texture2D *downImages[downCellCount];
//	bool downLighted[downCellCount];
//	const CommandType *commandTypes[downCellCount];
//	CommandClass commandClasses[downCellCount];
//	int progressBar;
	displayNode->addAttribute("progressBar",intToStr(progressBar), mapTagReplacements);
//	int downSelectedPos;
	displayNode->addAttribute("downSelectedPos",intToStr(downSelectedPos), mapTagReplacements);
//	Vec4f colors[colorCount];
//	int currentColor;
	displayNode->addAttribute("currentColor",intToStr(currentColor), mapTagReplacements);
//	int upCellSideCount;
//	int upImageSize;
//	int maxUpIndex;
}

void Display::loadGame(const XmlNode *rootNode) {
	const XmlNode *displayNode = rootNode->getChild("Display");

	//	string title;
	title = displayNode->getAttribute("title")->getValue();
	//	string text;
	text = displayNode->getAttribute("text")->getValue();
	//	string infoText;
	infoText = displayNode->getAttribute("infoText")->getValue();
	//	const Texture2D *upImages[upCellCount];
	//	const Texture2D *downImages[downCellCount];
	//	bool downLighted[downCellCount];
	//	const CommandType *commandTypes[downCellCount];
	//	CommandClass commandClasses[downCellCount];
	//	int progressBar;
	progressBar = displayNode->getAttribute("progressBar")->getIntValue();
	//	int downSelectedPos;
	//displayNode->addAttribute("downSelectedPos",intToStr(downSelectedPos), mapTagReplacements);
	//	Vec4f colors[colorCount];
	//	int currentColor;
	//currentColor = displayNode->getAttribute("progressBar")->getIntValue();
	//	int upCellSideCount;
	//	int upImageSize;
	//	int maxUpIndex;
}

}}//end namespace