File: Graphics.cpp

package info (click to toggle)
vcmi 1.7.2%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: sid
  • size: 43,732 kB
  • sloc: cpp: 275,851; ansic: 734; sh: 235; xml: 163; objc: 61; makefile: 50; python: 41
file content (156 lines) | stat: -rw-r--r-- 3,997 bytes parent folder | download
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
/*
 * Graphics.cpp, part of VCMI engine
 *
 * Authors: listed in file AUTHORS in main folder
 *
 * License: GNU General Public License v2.0 or later
 * Full text of license available in license.txt file, in main folder
 *
 */
#include "StdInc.h"
#include "Graphics.h"

#include <vcmi/Entity.h>
#include <vcmi/ArtifactService.h>
#include <vcmi/CreatureService.h>
#include <vcmi/FactionService.h>
#include <vcmi/HeroTypeService.h>
#include <vcmi/SkillService.h>
#include <vcmi/spells/Service.h>

#include "../renderSDL/SDL_Extensions.h"
#include "../render/CAnimation.h"
#include "../render/IImage.h"

#include "../lib/filesystem/Filesystem.h"
#include "../lib/filesystem/CBinaryReader.h"
#include "../../lib/json/JsonNode.h"
#include "../lib/modding/CModHandler.h"
#include "../lib/modding/ModScope.h"
#include "../lib/GameLibrary.h"

#include <SDL_surface.h>

Graphics * graphics = nullptr;

void Graphics::loadPaletteAndColors()
{
	auto textFile = CResourceHandler::get()->load(ResourcePath("DATA/PLAYERS.PAL"))->readAll();
	std::string pals((char*)textFile.first.get(), textFile.second);

	int startPoint = 24; //beginning byte; used to read
	for(int i=0; i<8; ++i)
	{
		for(int j=0; j<32; ++j)
		{
			ColorRGBA col;
			col.r = pals[startPoint++];
			col.g = pals[startPoint++];
			col.b = pals[startPoint++];
			col.a = SDL_ALPHA_OPAQUE;
			startPoint++;
			playerColorPalette[i][j] = col;
		}
	}

	auto stream = CResourceHandler::get()->load(ResourcePath("config/NEUTRAL.PAL"));
	CBinaryReader reader(stream.get());

	for(int i=0; i<32; ++i)
	{
		neutralColorPalette[i].r = reader.readUInt8();
		neutralColorPalette[i].g = reader.readUInt8();
		neutralColorPalette[i].b = reader.readUInt8();
		reader.readUInt8(); // this is "flags" entry, not alpha
		neutralColorPalette[i].a = SDL_ALPHA_OPAQUE;
	}
	//colors initialization
	ColorRGBA colors[]  = {
		{0xff,0,  0,    SDL_ALPHA_OPAQUE},
		{0x31,0x52,0xff,SDL_ALPHA_OPAQUE},
		{0x9c,0x73,0x52,SDL_ALPHA_OPAQUE},
		{0x42,0x94,0x29,SDL_ALPHA_OPAQUE},

		{0xff,0x84,0,   SDL_ALPHA_OPAQUE},
		{0x8c,0x29,0xa5,SDL_ALPHA_OPAQUE},
		{0x09,0x9c,0xa5,SDL_ALPHA_OPAQUE},
		{0xc6,0x7b,0x8c,SDL_ALPHA_OPAQUE}};

	for(int i=0;i<8;i++)
	{
		playerColors[i] = colors[i];
	}
	//gray
	neutralColor.r = 0x84;
	neutralColor.g = 0x84;
	neutralColor.b = 0x84;
	neutralColor.a = SDL_ALPHA_OPAQUE;
}

void Graphics::initializeBattleGraphics()
{
	auto allConfigs = LIBRARY->modh->getActiveMods();
	allConfigs.insert(allConfigs.begin(), ModScope::scopeBuiltin());
	for(auto & mod : allConfigs)
	{
		if(!CResourceHandler::get(mod)->existsResource(ResourcePath("config/battles_graphics.json")))
			continue;
			
		const JsonNode config(JsonPath::builtin("config/battles_graphics.json"), mod);

		//initialization of AC->def name mapping
		if(!config["ac_mapping"].isNull())
		for(const JsonNode &ac : config["ac_mapping"].Vector())
		{
			int ACid = static_cast<int>(ac["id"].Float());
			std::vector< std::string > toAdd;

			for(const JsonNode &defname : ac["defnames"].Vector())
			{
				toAdd.push_back(defname.String());
			}

			battleACToDef[ACid] = toAdd;
		}
	}
}
Graphics::Graphics()
{
	loadPaletteAndColors();
	initializeBattleGraphics();

	//(!) do not load any CAnimation here
}

void Graphics::setPlayerPalette(SDL_Palette * targetPalette, PlayerColor player)
{
	assert(targetPalette);

	SDL_Color palette[32];
	if(player.isValidPlayer())
	{
		for(int i=0; i<32; ++i)
			palette[i] = CSDL_Ext::toSDL(playerColorPalette[player.getNum()][i]);
	}
	else
	{
		for(int i=0; i<32; ++i)
			palette[i] = CSDL_Ext::toSDL(neutralColorPalette[i]);
	}

	SDL_SetPaletteColors(targetPalette, palette, 224, 32);
}

void Graphics::setPlayerFlagColor(SDL_Palette * targetPalette, PlayerColor player)
{
	if(player.isValidPlayer())
	{
		SDL_Color color = CSDL_Ext::toSDL(playerColors[player.getNum()]);
		SDL_SetPaletteColors(targetPalette, &color, 5, 1);
	}
	else
	{
		SDL_Color color = CSDL_Ext::toSDL(neutralColor);
		SDL_SetPaletteColors(targetPalette, &color, 5, 1);
	}
}