324 lines
9.4 KiB
C++
324 lines
9.4 KiB
C++
/*
|
|
PolyGun
|
|
|
|
Copyright (c) 2023 mrkubax10 <mrkubax10@onet.pl>
|
|
2023 kacperks
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
*/
|
|
|
|
#include "game/screens/game_session_screen.hpp"
|
|
|
|
#include "common/network/network_manager.hpp"
|
|
#include "common/logger.hpp"
|
|
#include "game/engine/engine.hpp"
|
|
#include "game/renderer/shader.hpp"
|
|
#include "game/renderer/mesh.hpp"
|
|
#include "game/renderer/texture.hpp"
|
|
|
|
using namespace polygun::screens;
|
|
|
|
int x_d, y_d, z_d, id_d;
|
|
|
|
|
|
|
|
static const std::vector<float> vertices = {
|
|
-0.1f , -0.1f, -0.1f,
|
|
0.1f , -0.1f, -0.1f,
|
|
0.1f , 0.1f, -0.1f,
|
|
0.1f , 0.1f, -0.1f,
|
|
-0.1f , 0.1f, -0.1f,
|
|
-0.1f , -0.1f, -0.1f,
|
|
|
|
-0.1f , -0.1f, 0.1f,
|
|
0.1f , -0.1f, 0.1f,
|
|
0.1f , 0.1f, 0.1f,
|
|
0.1f , 0.1f, 0.1f,
|
|
-0.1f , 0.1f, 0.1f,
|
|
-0.1f , -0.1f, 0.1f,
|
|
|
|
-0.1f , 0.1f, 0.1f,
|
|
-0.1f , 0.1f, -0.1f,
|
|
-0.1f , -0.1f, -0.1f,
|
|
-0.1f , -0.1f, -0.1f,
|
|
-0.1f , -0.1f, 0.1f,
|
|
-0.1f , 0.1f, 0.1f,
|
|
|
|
0.1f , 0.1f, 0.1f,
|
|
0.1f , 0.1f, -0.1f,
|
|
0.1f , -0.1f, -0.1f,
|
|
0.1f , -0.1f, -0.1f,
|
|
0.1f , -0.1f, 0.1f,
|
|
0.1f , 0.1f, 0.1f,
|
|
|
|
-0.1f , -0.1f, -0.1f,
|
|
0.1f , -0.1f, -0.1f,
|
|
0.1f , -0.1f, 0.1f,
|
|
0.1f , -0.1f, 0.1f,
|
|
-0.1f , -0.1f, 0.1f,
|
|
-0.1f , -0.1f, -0.1f,
|
|
|
|
-0.1f , 0.1f, -0.1f,
|
|
0.1f , 0.1f, -0.1f,
|
|
0.1f , 0.1f, 0.1f,
|
|
0.1f , 0.1f, 0.1f,
|
|
-0.1f , 0.1f, 0.1f,
|
|
-0.1f , 0.1f, -0.1f,
|
|
};
|
|
static std::vector<unsigned> indices;
|
|
static const std::vector<float> uvs={
|
|
0.0f, 0.0f,
|
|
1.0f, 0.0f,
|
|
1.0f, 1.0f,
|
|
1.0f, 1.0f,
|
|
0.0f, 1.0f,
|
|
0.0f, 0.0f,
|
|
|
|
0.0f, 0.0f,
|
|
1.0f, 0.0f,
|
|
1.0f, 1.0f,
|
|
1.0f, 1.0f,
|
|
0.0f, 1.0f,
|
|
0.0f, 0.0f,
|
|
|
|
1.0f, 0.0f,
|
|
1.0f, 1.0f,
|
|
0.0f, 1.0f,
|
|
0.0f, 1.0f,
|
|
0.0f, 0.0f,
|
|
1.0f, 0.0f,
|
|
|
|
1.0f, 0.0f,
|
|
1.0f, 1.0f,
|
|
0.0f, 1.0f,
|
|
0.0f, 1.0f,
|
|
0.0f, 0.0f,
|
|
1.0f, 0.0f,
|
|
|
|
0.0f, 1.0f,
|
|
1.0f, 1.0f,
|
|
1.0f, 0.0f,
|
|
1.0f, 0.0f,
|
|
0.0f, 0.0f,
|
|
0.0f, 1.0f,
|
|
|
|
0.0f, 1.0f,
|
|
1.0f, 1.0f,
|
|
1.0f, 0.0f,
|
|
1.0f, 0.0f,
|
|
0.0f, 0.0f,
|
|
0.0f, 1.0f
|
|
};
|
|
|
|
|
|
GameSessionScreen::GameSessionScreen() :
|
|
m_chunk_shader(nullptr),
|
|
m_node_mesh(nullptr),
|
|
m_chnk(),
|
|
m_camera(),
|
|
m_is_esc_pressed(false),
|
|
m_was_camera_locked(false),
|
|
m_connection_thread(),
|
|
m_running(true),
|
|
m_texture_atlas()
|
|
{}
|
|
|
|
void GameSessionScreen::begin() {
|
|
m_chunk_shader = m_engine->get_master_renderer().create_shader();
|
|
m_chunk_shader->load_from_file("chunk");
|
|
|
|
m_node_mesh = m_engine->get_master_renderer().create_mesh();
|
|
for(size_t i = 0; i<vertices.size(); i+=3)
|
|
indices.push_back((int)i/3);
|
|
m_node_mesh->load_from_memory(vertices, indices, uvs);
|
|
|
|
for(int i = 0; i<sizeof(world::nodes); i++) {
|
|
m_texture_atlas[i] = m_engine->get_master_renderer().create_texture();
|
|
|
|
int width, height, nrChannels;
|
|
stbi_set_flip_vertically_on_load(true);
|
|
unsigned char *data = stbi_load(world::nodes[i-1].texture.c_str(), &width, &height, &nrChannels, 0);
|
|
if (data) {
|
|
m_texture_atlas[i]->load_from_pixel_data(data, width, height, nrChannels);
|
|
}
|
|
else {
|
|
data = stbi_load("res/textures/empty.png", &width, &height, &nrChannels, 0);
|
|
m_texture_atlas[i]->load_from_pixel_data(data, width, height, nrChannels);
|
|
}
|
|
stbi_image_free(data);
|
|
}
|
|
|
|
m_engine->get_mesh_renderer()->set_3d_rendering_mode(true);
|
|
glfwSetCursorPosCallback(m_engine->get_window(), m_camera.mouse_callback);
|
|
glfwSetInputMode(m_engine->get_window(), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
|
|
|
m_engine->get_mesh_renderer()->set_camera(&m_camera);
|
|
|
|
// Note: uncomment to test connection with server
|
|
// m_connection_thread.reset(new std::thread(&GameSessionScreen::connection_thread_func, this));
|
|
}
|
|
|
|
void GameSessionScreen::render() {
|
|
m_engine->get_master_renderer().clear_screen(0, 0, 0);
|
|
// rendering
|
|
|
|
renderer::MeshRenderer* mesh_renderer = m_engine->get_mesh_renderer();
|
|
mesh_renderer->set_shader(m_chunk_shader);
|
|
for (unsigned int x = 0; x < 32; x++) {
|
|
for (unsigned int y = 0; y < 32; y++) {
|
|
for (unsigned int z = 0; z < 32; z++) {
|
|
if (m_chnk.get_node(glm::vec3(x,y,z))!= 0) {
|
|
mesh_renderer->translate(glm::vec3(x/5.0f, y/5.0f, z/5.0f));
|
|
mesh_renderer->render_textured(m_node_mesh, m_texture_atlas[m_chnk.get_node(glm::vec3(x,y,z))]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// imgui
|
|
|
|
ImGui_ImplOpenGL3_NewFrame();
|
|
ImGui_ImplGlfw_NewFrame();
|
|
ImGui::NewFrame();
|
|
|
|
ImGui::SetNextWindowSize(ImVec2(340, 200));
|
|
|
|
ImGui::Begin("Debug");
|
|
|
|
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
|
|
// Camera pos
|
|
ImGui::Text("Player Pos: %.3f, %.3f, %.3f", m_camera.m_position.x, m_camera.m_position.y, m_camera.m_position.z);
|
|
// Camera rot
|
|
ImGui::Text("Player Rot: %.3f, %.3f", m_camera.m_yaw, m_camera.m_pitch);
|
|
|
|
ImGui::InputInt("ID", &id_d);
|
|
ImGui::InputInt("X", &x_d);
|
|
ImGui::InputInt("Y", &y_d);
|
|
ImGui::InputInt("Z", &z_d);
|
|
|
|
// camera fov
|
|
ImGui::InputFloat("FOV", &m_camera.m_fov);
|
|
|
|
if (ImGui::Button("Place Block")) {
|
|
m_chnk.add_node(id_d, glm::vec3(x_d, y_d, z_d));
|
|
}
|
|
|
|
ImGui::End();
|
|
|
|
ImGui::Render();
|
|
|
|
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
|
|
|
ImGui::EndFrame();
|
|
}
|
|
|
|
void GameSessionScreen::update(double delta) {
|
|
// input
|
|
// -----
|
|
|
|
if (glfwGetKey(m_engine->get_window(), GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
|
if (!m_is_esc_pressed) {
|
|
if (glfwGetInputMode(m_engine->get_window(), GLFW_CURSOR) == GLFW_CURSOR_DISABLED)
|
|
glfwSetInputMode(m_engine->get_window(), GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
|
else
|
|
glfwSetInputMode(m_engine->get_window(), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
|
m_was_camera_locked = true;
|
|
}
|
|
m_is_esc_pressed = true;
|
|
}
|
|
else
|
|
m_is_esc_pressed = false;
|
|
|
|
|
|
if (glfwGetKey(m_engine->get_window(), GLFW_KEY_W) == GLFW_PRESS) {
|
|
m_camera.process_movement(polygun::engine::FORWARD, delta);
|
|
m_engine->get_mesh_renderer()->set_camera(&m_camera);
|
|
}
|
|
if (glfwGetKey(m_engine->get_window(), GLFW_KEY_S) == GLFW_PRESS) {
|
|
m_camera.process_movement(polygun::engine::BACKWARD, delta);
|
|
m_engine->get_mesh_renderer()->set_camera(&m_camera);
|
|
}
|
|
if (glfwGetKey(m_engine->get_window(), GLFW_KEY_A) == GLFW_PRESS) {
|
|
m_camera.process_movement(polygun::engine::LEFT, delta);
|
|
m_engine->get_mesh_renderer()->set_camera(&m_camera);
|
|
}
|
|
if (glfwGetKey(m_engine->get_window(), GLFW_KEY_D) == GLFW_PRESS) {
|
|
m_camera.process_movement(polygun::engine::RIGHT, delta);
|
|
m_engine->get_mesh_renderer()->set_camera(&m_camera);
|
|
}
|
|
if (glfwGetKey(m_engine->get_window(), GLFW_KEY_SPACE) == GLFW_PRESS) {
|
|
m_camera.process_movement(polygun::engine::UP, delta);
|
|
m_engine->get_mesh_renderer()->set_camera(&m_camera);
|
|
}
|
|
if (glfwGetKey(m_engine->get_window(), GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS) {
|
|
m_camera.process_movement(polygun::engine::DOWN, delta);
|
|
m_engine->get_mesh_renderer()->set_camera(&m_camera);
|
|
}
|
|
|
|
if(glfwGetInputMode(m_engine->get_window(), GLFW_CURSOR) == GLFW_CURSOR_DISABLED) {
|
|
if(m_was_camera_locked) {
|
|
m_camera.update(true);
|
|
m_was_camera_locked = false;
|
|
}
|
|
else
|
|
m_camera.update(false);
|
|
m_engine->get_mesh_renderer()->set_camera(&m_camera);
|
|
}
|
|
}
|
|
|
|
void GameSessionScreen::finish() {
|
|
delete m_chunk_shader;
|
|
delete m_node_mesh;
|
|
|
|
m_running = false;
|
|
if(m_connection_thread.get() && m_connection_thread->joinable()) {
|
|
LOG_VERBOSE("Waiting for connection thread to finish");
|
|
m_connection_thread->join();
|
|
}
|
|
}
|
|
|
|
void GameSessionScreen::connection_thread_func() {
|
|
polygun::network::NetworkManager network_manager("127.0.0.1", 1117);
|
|
polygun::network::NetworkPacket handshake = network_manager.create_packet(polygun::network::NetworkPacketFlag::FLAG_RELIABLE, polygun::network::NetworkPacketID::PACKET_HANDSHAKE);
|
|
polygun::network::NetworkPacket packet;
|
|
handshake.write("mrkubax10");
|
|
network_manager.send_packet(handshake);
|
|
network_manager.update();
|
|
while(m_running) {
|
|
if(network_manager.get_socket().recv(packet)) {
|
|
if((packet.get_flags() & polygun::network::NetworkPacketFlag::FLAG_RELIABLE) || packet.get_id()==polygun::network::NetworkPacketID::PACKET_ACK)
|
|
network_manager.push_packet(packet);
|
|
network_manager.update();
|
|
if(network_manager.recv_packet(packet)) {
|
|
// handle
|
|
}
|
|
}
|
|
packet = network_manager.create_packet(polygun::network::NetworkPacketFlag::FLAG_NONE, polygun::network::NetworkPacketID::PACKET_ACTIVITY);
|
|
network_manager.send_packet(packet);
|
|
}
|
|
packet = network_manager.create_packet(polygun::network::NetworkPacketFlag::FLAG_RELIABLE, polygun::network::NetworkPacketID::PACKET_EXIT);
|
|
network_manager.send_packet(packet);
|
|
while(network_manager.has_outgoing_packets()) {
|
|
if(network_manager.get_socket().recv(packet) && packet.get_id()==polygun::network::NetworkPacketID::PACKET_ACK)
|
|
network_manager.push_packet(packet);
|
|
network_manager.update();
|
|
}
|
|
}
|