Working on running the game even in Telegram
This commit is contained in:
1 parent
5fcb1d1234
commit
e78429b600
9 files changed
+133
-60
No files matched your search
+24
-19
@@ -57,9 +57,9 @@ namespace ZL
|
||||
#endif
|
||||
|
||||
Game::Game()
|
||||
: window(nullptr)
|
||||
: /*window(nullptr)
|
||||
, glContext(nullptr)
|
||||
, newTickCount(0)
|
||||
, */newTickCount(0)
|
||||
, lastTickCount(0)
|
||||
, menuManager(renderer)
|
||||
, space(renderer, taskManager, mainThreadHandler, networkClient, menuManager)
|
||||
@@ -67,12 +67,13 @@ namespace ZL
|
||||
}
|
||||
|
||||
Game::~Game() {
|
||||
/*
|
||||
if (glContext) {
|
||||
SDL_GL_DeleteContext(glContext);
|
||||
}
|
||||
if (window) {
|
||||
SDL_DestroyWindow(window);
|
||||
}
|
||||
}*/
|
||||
#ifndef EMSCRIPTEN
|
||||
// In Emscripten, SDL must stay alive across context loss/restore cycles
|
||||
// so the window remains valid when the game object is re-created.
|
||||
@@ -81,14 +82,15 @@ namespace ZL
|
||||
}
|
||||
|
||||
void Game::setup() {
|
||||
glContext = SDL_GL_CreateContext(ZL::Environment::window);
|
||||
//glContext = SDL_GL_CreateContext(ZL::Environment::window);
|
||||
//glContext = in_glContext;
|
||||
|
||||
Environment::computeProjectionDimensions();
|
||||
|
||||
ZL::BindOpenGlFunctions();
|
||||
ZL::CheckGlError();
|
||||
renderer.InitOpenGL();
|
||||
|
||||
|
||||
#ifdef EMSCRIPTEN
|
||||
// These shaders and loading.png are preloaded separately (not from zip),
|
||||
// so they are available immediately without waiting for resources.zip.
|
||||
@@ -101,19 +103,23 @@ namespace ZL
|
||||
loadingTexture = std::make_unique<Texture>(CreateTextureDataFromPng("resources/loading.png", CONST_ZIP_FILE));
|
||||
#endif
|
||||
|
||||
loadingMesh.data = CreateRect2D({ Environment::projectionWidth * 0.5f, Environment::projectionHeight * 0.5f }, { Environment::projectionWidth * 0.5f, Environment::projectionHeight * 0.5f }, 3);
|
||||
loadingMesh.data = CreateRect2D({ 0.5f, 0.5f }, { 0.5f, 0.5f }, 3);
|
||||
loadingMesh.RefreshVBO();
|
||||
|
||||
|
||||
#ifdef EMSCRIPTEN
|
||||
// Asynchronously download resources.zip; setupPart2() is called on completion.
|
||||
// The loading screen stays visible until the download finishes.
|
||||
s_instance = this;
|
||||
std::cout << "Load resurces step 1" << std::endl;
|
||||
emscripten_async_wget("resources.zip", "resources.zip", onResourcesZipLoaded, onResourcesZipError);
|
||||
#else
|
||||
mainThreadHandler.EnqueueMainThreadTask([this]() {
|
||||
std::cout << "Load resurces step 2" << std::endl;
|
||||
this->setupPart2();
|
||||
std::cout << "Load resurces step 3" << std::endl;
|
||||
});
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -164,10 +170,10 @@ namespace ZL
|
||||
networkClient = std::make_unique<WebSocketClientEmscripten>();
|
||||
networkClient->Connect("localhost", 8081);
|
||||
#else
|
||||
networkClient = std::make_unique<WebSocketClient>(taskManager.getIOContext());
|
||||
networkClient->Connect("localhost", 8081);
|
||||
//networkClient = std::make_unique<WebSocketClient>(taskManager.getIOContext());
|
||||
//networkClient->Connect("localhost", 8081);
|
||||
#endif
|
||||
|
||||
|
||||
if (networkClient) {
|
||||
std::string joinMsg = std::string("JOIN:") + nickname + ":" + std::to_string(shipType);
|
||||
networkClient->Send(joinMsg);
|
||||
@@ -249,12 +255,12 @@ namespace ZL
|
||||
renderer.EnableVertexAttribArray(vPositionName);
|
||||
renderer.EnableVertexAttribArray(vTexCoordName);
|
||||
|
||||
float width = Environment::projectionWidth;
|
||||
float height = Environment::projectionHeight;
|
||||
//float width = Environment::projectionWidth;
|
||||
//float height = Environment::projectionHeight;
|
||||
|
||||
renderer.PushProjectionMatrix(
|
||||
0, width,
|
||||
0, height,
|
||||
0, 1,
|
||||
0, 1,
|
||||
-10, 10);
|
||||
|
||||
renderer.PushMatrix();
|
||||
@@ -275,8 +281,7 @@ namespace ZL
|
||||
int64_t Game::getSyncTimeMs() {
|
||||
int64_t localNow = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch()).count();
|
||||
// Добавляем смещение, полученное от сервера
|
||||
if (networkClient)
|
||||
if(networkClient)
|
||||
{
|
||||
return localNow + networkClient->getTimeOffset();
|
||||
}
|
||||
@@ -317,10 +322,10 @@ namespace ZL
|
||||
}
|
||||
|
||||
void Game::render() {
|
||||
SDL_GL_MakeCurrent(ZL::Environment::window, glContext);
|
||||
//SDL_GL_MakeCurrent(ZL::Environment::window, glContext);
|
||||
ZL::CheckGlError();
|
||||
|
||||
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
|
||||
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
drawScene();
|
||||
@@ -438,7 +443,7 @@ namespace ZL
|
||||
#endif
|
||||
}
|
||||
render();
|
||||
|
||||
|
||||
if (networkClient) {
|
||||
#ifndef NETWORK
|
||||
auto localClient = dynamic_cast<ZL::LocalClient*>(networkClient.get());
|
||||
|
||||
Reference in new issue
Block a user