-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
195 lines (177 loc) · 5.6 KB
/
main.cpp
File metadata and controls
195 lines (177 loc) · 5.6 KB
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
185
186
187
188
189
190
191
192
193
194
195
/** @file main.cpp o inicio de tudo. Responsavel por instanciar a interface grafica principal. */
#if ANDROID
#include <android_native_app_glue.h>
#endif
#include <iostream>
#include <memory>
#include <stdexcept>
#include <QtCore/QDir>
#include <QtCore/QLocale>
#include <QtCore/Qt>
#include <QtGui/QSurfaceFormat>
#include <QtWidgets/QApplication>
#include <boost/asio.hpp>
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "arq/arquivo.h"
#include "ent/tabelas.h"
#include "ent/tabuleiro.h"
#include "ent/tabuleiro_interface.h"
#include "ent/util.h"
#include "ifg/qt/principal.h"
#include "ifg/tecladomouse.h"
#include "ifg/qt/qt_interface.h"
#include "m3d/m3d.h"
#include "net/cliente.h"
#include "net/servidor.h"
#include "ntf/notificacao.h"
#include "log/log.h"
#include "som/som.h"
#include "tex/texturas.h"
using namespace std;
#if 0
// Para capturar excecoes do QT.
class MyApp : public QApplication {
public:
MyApp(int& argc, char** argv) : QApplication(argc, argv) {}
bool notify(QObject* receiver, QEvent* event) {
try {
return QApplication::notify(receiver, event);
} catch (const std::exception& e) {
receiver->dumpObjectInfo();
LOG(INFO) << "exception: " << e.what();
}
return false;
}
};
#endif
namespace {
void CarregaConfiguracoes(ent::OpcoesProto* proto) {
try {
arq::LeArquivoAsciiProto(arq::TIPO_CONFIGURACOES, "configuracoes.asciiproto", proto);
LOG(INFO) << "Carregando opcoes de arquivo: ";
} catch (...) {
proto->CopyFrom(ent::OpcoesProto::default_instance());
LOG(INFO) << "Carregando opcoes padroes.";
}
if (!proto->has_iluminacao_por_pixel()) {
proto->set_iluminacao_por_pixel(true);
}
LOG(INFO) << "Opcoes inciais: " << proto->ShortDebugString();
}
QSurfaceFormat Formato() {
QSurfaceFormat formato;
formato.setVersion(2, 1);
// Caso contrario, o skip falha e o resize da biziu.
formato.setSwapBehavior(QSurfaceFormat::SingleBuffer);
formato.setSwapInterval(0); // vsync off
formato.setRedBufferSize(8);
formato.setGreenBufferSize(8);
formato.setBlueBufferSize(8);
// Nao faca isso! Isso aqui deixara a janela transparente, quebrando a transparencia.
//formato.setAlphaBufferSize(8);
//formato.setDepthBufferSize(24);
//formato.setStencilBufferSize(1);
formato.setRenderableType(QSurfaceFormat::OpenGL);
formato.setSamples(2);
return formato;
}
class SomEscopo {
public:
SomEscopo(const ent::OpcoesProto& opcoes) { som::Inicia(opcoes); }
~SomEscopo() { som::Finaliza(); }
};
} // namespace
#if ANDROID
extern "C" {
void android_main(struct android_app* state) {
int argc = 0;
char* argv[] = {};
#else
int main(int argc, char** argv) {
#endif
#if USAR_GLOG
meulog::Inicializa(argc, argv);
#endif
//MyApp q_app(argc, argv);
QSurfaceFormat::setDefaultFormat(Formato());
QApplication q_app(argc, argv);
#if WIN32
q_app.setStyle("Fusion");
#endif
QDir dir(QCoreApplication::applicationDirPath());
LOG(INFO) << "Iniciando programa: LOG LIGADO";
// Arq::Inicializa tem que vir antes, porque os outros leem varias coisas de arquivos.
#if ANDROID
arq::Inicializa(state->activity->env, state->activity->assetManager, state->activity->internalDataPath);
#else
arq::Inicializa(dir.absolutePath().toStdString());
#endif
ent::OpcoesProto opcoes;
CarregaConfiguracoes(&opcoes);
if (opcoes.desabilitar_retina()) {
QCoreApplication::setAttribute(Qt::AA_DisableHighDpiScaling);
}
boost::asio::io_service servico_io;
net::Sincronizador sincronizador(&servico_io);
ntf::CentralNotificacoes central;
ent::Tabelas tabelas(¢ral);
net::Servidor servidor(&sincronizador, ¢ral);
net::Cliente cliente(&sincronizador, ¢ral);
tex::Texturas texturas(¢ral);
m3d::Modelos3d modelos3d(¢ral);
ent::Tabuleiro tabuleiro(opcoes, tabelas, &texturas, &modelos3d, ¢ral);
ifg::TratadorTecladoMouse teclado_mouse(¢ral, &tabuleiro);
//ent::InterfaceGraficaOpengl guiopengl(tabelas, &teclado_mouse, &tabuleiro, ¢ral);
//tabuleiro.AtivaInterfaceOpengl(&guiopengl);
SomEscopo som(tabuleiro.Opcoes());
std::unique_ptr<ifg::qt::Principal> p(
ifg::qt::Principal::Cria(&q_app, tabelas, &tabuleiro, &modelos3d, &texturas, &teclado_mouse, ¢ral));
ifg::qt::InterfaceGraficaQt igqt(tabelas, p.get(), &teclado_mouse, &tabuleiro, ¢ral);
bool tentou_carregar = false;
for (int i = 1; i < argc; ++i) {
if (argv[i][0] == '-') continue;
// Carrega o tabuleiro.
auto n = ntf::NovaNotificacao(ntf::TN_DESERIALIZAR_TABULEIRO);
std::string nome(argv[i]);
if (!absl::StrContains(nome, "://")) {
// Tabuleiros estaticos devem começar com estatico://. Por padrão, carregamos dinamicos.
nome = absl::StrCat("dinamico://", nome);
}
n->set_endereco(nome);
central.AdicionaNotificacao(n.release());
LOG(INFO) << "Carregando: " << nome;
tentou_carregar = true;
break;
}
if (!tentou_carregar) {
LOG(INFO) << "Iniciando sem tabuleiro carregado, argc: " << argc;
}
// As vezes o carregamento falha por diretorios errados. Conferir se tabela carregou (pois nao da erro apos construcao).
if (tabelas.todas().tabela_classes().info_classes().empty()) {
central.AdicionaNotificacao(ntf::NovaNotificacaoErro(
absl::StrFormat(
"%s: %s", "Erro carregando tabelas, caminho: ", dir.absolutePath().toStdString().c_str())));
}
try {
p->Executa();
}
catch (exception& e) {
std::cerr << e.what() << std::endl;
#if ANDROID
return;
#else
return 1;
#endif
}
#if ANDROID
return;
#else
ent::ImprimeDadosRolados();
return 0;
#endif
}
#if ANDROID
} // extern C
#endif