File size: 2,775 Bytes
4559903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
 *  Copyright (C) 2016-2018 Simon Boyé
 *
 *  This file is part of lair.
 *
 *  lair 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  lair 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 lair.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


#include <lair/geometry/shape_2d.h>

#include <lair/render_gl3/texture_set.h>

#include "main_state.h"

#include "game.h"


// This function is declared in an auto-generated file
void registerTemplateResource(lair::MemoryFileSystem& fs);


GameConfig::GameConfig()
	: GameConfigBase()
{
}

void GameConfig::setFromArgs(int& argc, char** argv) {
	GameConfigBase::setFromArgs(argc, argv);
}

const PropertyList& GameConfig::properties() const {
	return GameConfig::staticProperties();
}

const PropertyList& GameConfig::staticProperties() {
	return GameConfigBase::staticProperties();
}



Game::Game(int argc, char** argv)
    : GameBase(argc, argv),
      _mainState() {
	serializer().registerType<Shape2D>();
	serializer().registerType<Shape2DVector>();
	serializer().registerType<TextureSetCSP>(
	    [this](TextureSetCSP& value, const Variant& var, Logger& logger) {
		    return varRead(value, var, _renderer, _loader.get(), logger);
	    },
	    static_cast<bool(*)(Variant&, const TextureSetCSP&, Logger&)>(varWrite)
	);

	_sceneName = (argc > 1)? argv[1]: "simple";
}


Game::~Game() {
}


void Game::initialize() {
	GameBase::initialize(_config);

	registerTemplateResource(*_memoryFs);

	typedef std::pair<const lair::Path*, const MemFile*> File;
	std::vector<File> files;
	for(const auto& pathFile: *_memoryFs) {
		files.emplace_back(&pathFile.first, &pathFile.second);
	}
	std::sort(files.begin(), files.end(), [](const File& pf0, const File& pf1) {
		return *pf0.first < *pf1.first;
	});
	for(const File& pathFile: files) {
		log().info("Builtin file: ", *pathFile.first, " (", pathFile.second->size, "B)");
	}

	window()->setUtf8Title("Lair - template");

	_mainState.reset(new MainState(this));
	_mainState->initialize();
	if(_sceneName.size())
		_mainState->setScene(_sceneName);
}


void Game::shutdown() {
	_mainState->shutdown();
	_mainState.reset();  // Required to ensure that everything is freed

	GameBase::shutdown();
}


GameConfig& Game::config() {
	return _config;
}


MainState* Game::mainState() {
	return _mainState.get();
}