Merge branch 'main' into ShipSpawn

This commit is contained in:
Vladislav Khorev committed 2026-03-05 10:24:17 +03:00
commit 70ef730e86
13 files changed
+292 -152

No files matched your search

+30 -19
View File
@@ -58,9 +58,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)
@@ -68,12 +68,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.
@@ -82,14 +83,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.
@@ -102,19 +104,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
}
@@ -168,13 +174,19 @@ namespace ZL
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);
std::cerr << "Sent JOIN: " << joinMsg << std::endl;
}
space.boxCoordsArr.clear();
space.boxRenderArr.clear();
//space.boxLabels.clear();
space.boxAlive.clear();
space.serverBoxesApplied = false;
lastTickCount = 0;
spaceGameStarted = 1;
};
@@ -250,12 +262,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();
@@ -276,8 +288,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();
}
@@ -318,10 +329,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();
@@ -439,14 +450,14 @@ namespace ZL
#endif
}
render();
if (networkClient) {
#ifndef NETWORK
//#ifndef NETWORK
auto localClient = dynamic_cast<ZL::LocalClient*>(networkClient.get());
if (localClient) {
localClient->setLocalPlayerState(Environment::shipState);
}
#endif
//#endif
networkClient->Poll();
#ifdef NETWORK
auto* wsBase = dynamic_cast<ZL::WebSocketClientBase*>(networkClient.get());