diff --git a/CMakeLists.txt b/CMakeLists.txt index 9339efd..f17fbe0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,6 +64,7 @@ include_directories( ${OPENAL_INCLUDE_DIR} ) +set(SRCS ";") add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src") add_executable(diggler ${SRCS}) @@ -80,7 +81,7 @@ target_link_libraries(diggler execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink "${PATH_TO_TOPDIR}/assets" "${PROJECT_BINARY_DIR}/assets") -install(TARGETS diggler RUNTIME DESTINATION bin) +#install(TARGETS digglerz RUNTIME DESTINATION bin) find_package(Doxygen) if (DOXYGEN_FOUND) diff --git a/assets/2d.f.glsl b/assets/2d.f.glsl index 9acc3be..14879ab 100644 --- a/assets/2d.f.glsl +++ b/assets/2d.f.glsl @@ -1,4 +1,6 @@ -uniform vec4 unicolor = vec4(1.0, 1.0, 1.0, 1.0); +precision lowp float; + +uniform vec4 unicolor; #ifdef COLORED varying vec4 v_color; #endif diff --git a/assets/3d.f.glsl b/assets/3d.f.glsl index 47c9658..ec38fa7 100755 --- a/assets/3d.f.glsl +++ b/assets/3d.f.glsl @@ -1,4 +1,6 @@ -uniform vec4 unicolor = vec4(1.0, 1.0, 1.0, 1.0); +precision lowp float; + +uniform vec4 unicolor; #ifdef COLORED varying vec4 v_color; #endif @@ -10,8 +12,8 @@ uniform vec2 texshift; #endif #endif #ifdef FOG -uniform float fogEnd = 32.0; -uniform float fogStart = 16.0; +uniform float fogEnd; +uniform float fogStart; #endif diff --git a/assets/3d.v.glsl b/assets/3d.v.glsl index 9e5edfb..7baac60 100644 --- a/assets/3d.v.glsl +++ b/assets/3d.v.glsl @@ -38,6 +38,6 @@ void main(void) { gl_Position = mvp * vec4(coord, 1); #ifdef POINTSIZE float zDist = 1.0-(gl_Position.z / gl_Position.w); // 1=close 0=far - gl_PointSize = pointSize*2048*zDist; + gl_PointSize = pointSize*2048.0*zDist; #endif } \ No newline at end of file diff --git a/assets/bloom.f.glsl b/assets/bloom.f.glsl index 85fd580..fac8f41 100644 --- a/assets/bloom.f.glsl +++ b/assets/bloom.f.glsl @@ -1,15 +1,16 @@ -#define t(a,b) texture2D(mytexture, clamp(v_texcoord+vec2(a*pixshift.x,b*pixshift.y), vec2(0.0,0.0), vec2(1.0,1.0))) +#define t(a,b) texture2D(mytexture, clamp(v_texcoord+vec2(a*pixshift.x, b*pixshift.y), 0.0, 1.0)) +precision lowp float; varying vec2 v_texcoord; uniform sampler2D mytexture; -uniform float bloomThreshold = 0.4; uniform vec2 pixshift; void main(void) { - vec4 c = (t(-2,2) + t(-1,2) + t(0,2) + t(1,2) + t(2,2) + - t(-2,1) + t(-1,1) + t(0,1) + t(1,1) + t(2,1) + - t(-2,0) + t(-1,0) + t(0,0) + t(1,0) + t(2,0) + - t(-2,-1) + t(-1,-1) + t(0,-1) + t(1,-1) + t(2,-1) + - t(-2,-2) + t(-1,-2) + t(0,-2) + t(1,-2) + t(2,-2))/10.0; - gl_FragColor = c; + gl_FragColor = ( + t(-2.0, 2.0) + t(-1.0, 2.0) + t(0.0, 2.0) + t(1.0, 2.0) + t(2.0, 2.0) + + t(-2.0, 1.0) + t(-1.0, 1.0) + t(0.0, 1.0) + t(1.0, 1.0) + t(2.0, 1.0) + + t(-2.0, 0.0) + t(-1.0, 0.0) + t(0.0, 0.0) + t(1.0, 0.0) + t(2.0, 0.0) + + t(-2.0,-1.0) + t(-1.0,-1.0) + t(0.0,-1.0) + t(1.0,-1.0) + t(2.0,-1.0) + + t(-2.0,-2.0) + t(-1.0,-2.0) + t(0.0,-2.0) + t(1.0,-2.0) + t(2.0,-2.0) + )/10.0; } \ No newline at end of file diff --git a/assets/bloomExtractor.f.glsl b/assets/bloomExtractor.f.glsl index 1140e06..221877f 100644 --- a/assets/bloomExtractor.f.glsl +++ b/assets/bloomExtractor.f.glsl @@ -1,6 +1,8 @@ +precision lowp float; + varying vec2 v_texcoord; uniform sampler2D mytexture; -uniform float bloomThreshold = 0.4; +uniform float bloomThreshold; // = 0.4; vec3 cap(vec3 val) { float highest = val.x; diff --git a/src/Audio.hpp b/src/Audio.hpp index f1ebb8e..1bb6b8b 100644 --- a/src/Audio.hpp +++ b/src/Audio.hpp @@ -31,7 +31,7 @@ public: void addSound(const std::string &name, const std::string &path); void updatePos(); void updateAngle(); - // Runs UpdatePos and UpdateAngle + // Runs updatePos and updateAngle void update(); void playSound(const std::string &name); void playSound(const SoundBuffer &buf); diff --git a/src/FBO.cpp b/src/FBO.cpp index 3df7b4c..b8987e0 100644 --- a/src/FBO.cpp +++ b/src/FBO.cpp @@ -17,14 +17,16 @@ namespace Diggler { FBO::FBO(int w, int h, Texture::PixelFormat format, bool stencil) : m_hasStencil(stencil) { GLint currentBoundFBO; glGetIntegerv(GL_FRAMEBUFFER_BINDING, ¤tBoundFBO); GLint currentBoundRBO; glGetIntegerv(GL_RENDERBUFFER_BINDING, ¤tBoundRBO); - + + glGetError(); // Flush previous errors + tex = new Texture(w, h, format); glGenFramebuffers(1, &id); glBindFramebuffer(GL_FRAMEBUFFER, id); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, *tex, 0); GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0}; glDrawBuffers(1, DrawBuffers); - + glGenRenderbuffers(1, &rboId); glBindRenderbuffer(GL_RENDERBUFFER, rboId); if (stencil) @@ -32,9 +34,9 @@ FBO::FBO(int w, int h, Texture::PixelFormat format, bool stencil) : m_hasStencil else glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, w, h); glFramebufferRenderbuffer(GL_FRAMEBUFFER, stencil ? GL_DEPTH_STENCIL_ATTACHMENT : GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rboId); - + glCheck(); - + glBindFramebuffer(GL_FRAMEBUFFER, currentBoundFBO); glBindRenderbuffer(GL_RENDERBUFFER, currentBoundRBO); } @@ -47,7 +49,7 @@ void FBO::resize(int w, int h) { else glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, w, h); glBindRenderbuffer(GL_RENDERBUFFER, currentBoundRBO); - + tex->resize(w, h); } @@ -67,7 +69,6 @@ FBO::~FBO() { glDeleteFramebuffers(1, &id); delete tex; glDeleteRenderbuffers(1, &rboId); - } } diff --git a/src/Game.cpp b/src/Game.cpp index 17f0469..35c8f3e 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -34,11 +34,16 @@ void Game::init() { } } +void Game::uninitGL() { + delete PM; + delete RP; +} + Game::~Game() { if (GlobalProperties::IsClient) { + delete A; delete CR; - delete PM; delete LP; - delete RP; delete A; + delete LP; delete KB; } if (GlobalProperties::IsServer) { diff --git a/src/Game.hpp b/src/Game.hpp index a82b391..ef93893 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -51,6 +51,7 @@ public: Game(); void init(); + void uninitGL(); void updateTime(double time); ~Game(); }; diff --git a/src/GameState.cpp b/src/GameState.cpp index afa5ebb..493a202 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -261,9 +261,9 @@ void GameState::onKey(int key, int scancode, int action, int mods) { } } else { if (key == G->KB->gameMenu && action == GLFW_PRESS) { - isEscapeToggled = !isEscapeToggled; - UI.EM->setVisible(isEscapeToggled); - if (isEscapeToggled) { + isMenuToggled = !isMenuToggled; + UI.EM->setVisible(isMenuToggled); + if (isMenuToggled) { unlockMouse(); G->LP->goForward(false); G->LP->goBackward(false); @@ -273,7 +273,7 @@ void GameState::onKey(int key, int scancode, int action, int mods) { lockMouse(); } } - if (!isEscapeToggled) { + if (!isMenuToggled) { if (key == G->KB->forward) { G->LP->goForward(action == GLFW_PRESS); } else if (key == G->KB->backward) { @@ -314,7 +314,7 @@ void GameState::unlockMouse() { void GameState::onMouseButton(int key, int action, int mods) { - if (!m_mouseLocked && action == GLFW_PRESS && !isEscapeToggled) { + if (!m_mouseLocked && action == GLFW_PRESS && !isMenuToggled) { lockMouse(); } @@ -476,7 +476,7 @@ bool GameState::connectLoop() { }; double T; glm::mat4 mat; - while (!finished && !glfwWindowShouldClose(*GW)) { // Infinite loop \o/ + while (!finished && !GW->shouldClose()) { // Infinite loop \o/ T = glfwGetTime(); G->updateTime(T); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -495,12 +495,12 @@ bool GameState::connectLoop() { glfwSwapBuffers(*GW); glfwPollEvents(); } - if (glfwWindowShouldClose(*GW)) - glfwHideWindow(*GW); + if (GW->shouldClose()) + GW->setVisible(false); m_networkThread.join(); delete cUI.Connecting; delete cUI.Dot; - if (glfwWindowShouldClose(*GW)) + if (GW->shouldClose()) return true; if (!success) { std::ostringstream oss; @@ -723,7 +723,7 @@ void GameState::gameLoop() { renderDeathScreen(); } - if (isEscapeToggled) + if (isMenuToggled) UI.EM->render(); glfwSwapBuffers(*GW); diff --git a/src/GameState.hpp b/src/GameState.hpp index 51195a1..26bb465 100644 --- a/src/GameState.hpp +++ b/src/GameState.hpp @@ -107,7 +107,7 @@ private: UI::Text *Dot; }; - bool isEscapeToggled = false; + bool isMenuToggled = false; struct { bool show; diff --git a/src/GameWindow.cpp b/src/GameWindow.cpp index ad89ab7..e94f798 100644 --- a/src/GameWindow.cpp +++ b/src/GameWindow.cpp @@ -31,7 +31,7 @@ GameWindow::GameWindow(Game *G) : G(G) { glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); - //glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API); + glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API); glfwWindowHint(GLFW_SAMPLES, 0); // Gimme aliasing everywhere //glfwWindowHint(GLFW_STENCIL_BITS, 8); m_window = glfwCreateWindow(m_w, m_h, "Diggler", nullptr, nullptr); @@ -77,9 +77,7 @@ GameWindow::GameWindow(Game *G) : G(G) { const uint8 *GL_renderer = glGetString(GL_RENDERER); getOutputStreamRaw() << "GL " << GL_version << " / " << GL_renderer << std::endl; } - - - + UIM.setProjMat(glm::ortho(0.0f, (float)m_w, 0.0f, (float)m_h)); G->init(); @@ -109,6 +107,14 @@ bool GameWindow::shouldClose() const { return glfwWindowShouldClose(m_window); } +void GameWindow::setVisible(bool visible) { + return visible ? glfwShowWindow(m_window) : glfwHideWindow(m_window); +} + +bool GameWindow::isVisible() const { + return glfwGetWindowAttrib(m_window, GLFW_VISIBLE); +} + void GameWindow::cbChar(char32 unichar) { m_currentState->onChar(unichar); } diff --git a/src/GameWindow.hpp b/src/GameWindow.hpp index 0a68ab2..0f64fca 100644 --- a/src/GameWindow.hpp +++ b/src/GameWindow.hpp @@ -18,39 +18,43 @@ class State; class GameWindow { private: static int InstanceCount; - + GLFWwindow *m_window; int m_w, m_h; - + shared_ptr m_currentState, m_nextState; public: UI::Manager UIM; - + Game *G; - + GameWindow(Game*); ~GameWindow(); - + operator GLFWwindow&() const { return *m_window; } operator GLFWwindow*() const { return m_window; } - + inline int getW() const { return m_w; } inline int getH() const { return m_h; } + bool shouldClose() const; - + + void setVisible(bool); + bool isVisible() const; + void cbMouseButton(int key, int action, int mods); void cbCursorPos(double x, double y); void cbMouseScroll(double x, double y); void cbKey(int key, int scancode, int action, int mods); void cbChar(char32 unichar); void cbResize(int w, int h); - + void updateViewport(); - + void setNextState(const shared_ptr next); void run(); - + void showMessage(const std::string &msg, const std::string &submsg = ""); }; diff --git a/src/Program.cpp b/src/Program.cpp index bba7fa5..4ec6d18 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -35,6 +35,34 @@ bool Program::link() { getErrorStream() << id << ':' << getError() << std::endl; return false; } + + // Default values + GLint loc = glGetUniformLocation(id, "unicolor"); + if (loc != -1) { + GLint prevId; glGetIntegerv(GL_CURRENT_PROGRAM, &prevId); + glUseProgram(id); + glUniform4f(loc, 1.f, 1.f, 1.f, 1.f); + glUseProgram(prevId); + } + + loc = glGetUniformLocation(id, "bloomThreshold"); + if (loc != -1) { + GLint prevId; glGetIntegerv(GL_CURRENT_PROGRAM, &prevId); + glUseProgram(id); + glUniform1f(loc, .4f); + glUseProgram(prevId); + } + + // FIXME: remove this, fog needs to be dynamic + loc = glGetUniformLocation(id, "fogStart"); + if (loc != -1) { + GLint prevId; glGetIntegerv(GL_CURRENT_PROGRAM, &prevId); + glUseProgram(id); + glUniform1f(loc, 16.f); + glUniform1f(glGetUniformLocation(id, "fogEnd"), 32.f); + glUseProgram(prevId); + } + return true; } diff --git a/src/Server.cpp b/src/Server.cpp index b945db2..2828ea9 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -191,15 +191,21 @@ void Server::schedSendChunk(ChunkRef C, Player &P) { P.pendingChunks.emplace_back(C); } -void Server::sendChunk(Chunk &C, Player &P) { +void Server::sendChunks(const std::list &cs, Player &P) { OutMessage msg(MessageType::ChunkTransfer); - msg.writeU8(1); // Nbr of sent chunks - msg.writeI16(C.getWorld()->id); - glm::ivec3 pos = C.getWorldChunkPos(); - msg.writeI16(pos.x); - msg.writeI16(pos.y); - msg.writeI16(pos.z); - C.send(msg); + msg.writeU8(cs.size()); // Nbr of sent chunks + for (const ChunkRef &cr : cs) { + Chunk &c = *cr; + msg.writeI16(c.getWorld()->id); + glm::ivec3 pos = c.getWorldChunkPos(); + msg.writeI16(pos.x); + msg.writeI16(pos.y); + msg.writeI16(pos.z); + c.send(msg); + + getDebugStream() << "C[" << pos.x << ',' << pos.y << ',' << pos.z << + "] sent to " << P.name << std::endl; + } H.send(P.P, msg, Tfer::Rel, Channels::MapUpdate); } @@ -327,16 +333,17 @@ void Server::chunkUpdater(WorldRef WR, bool &continueUpdate) { NetHelper::Broadcast(G, msg, Tfer::Rel, Channels::MapUpdate); } } + std::list chunksToSend; for (Player &p : G.players) { - for (auto it = p.pendingChunks.begin(); it != p.pendingChunks.end(); ++it) { + chunksToSend.clear(); + for (auto it = p.pendingChunks.begin(); + it != p.pendingChunks.end() && chunksToSend.size() < 32; ++it) { if ((*it)->state == Chunk::State::Ready) { - glm::ivec3 pos = (*it)->getWorldChunkPos(); - getDebugStream() << "Send pent Chunk[" << pos.x << ',' << pos.y << ',' << pos.z << - "] send to " << p.name << std::endl; - sendChunk(**it, p); - it = p.pendingChunks.erase(it)--; + chunksToSend.push_back(std::move(*it)); + it = --p.pendingChunks.erase(it); } } + sendChunks(chunksToSend, p); } std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } diff --git a/src/Server.hpp b/src/Server.hpp index af69616..69486f2 100644 --- a/src/Server.hpp +++ b/src/Server.hpp @@ -31,7 +31,7 @@ private: void handlePlayerMapUpdate(Net::InMessage&, Player&); void schedSendChunk(ChunkRef, Player&); - void sendChunk(Chunk&, Player&); + void sendChunks(const std::list&, Player&); void chunkUpdater(WorldRef WR, bool &continueUpdate); diff --git a/src/Shader.cpp b/src/Shader.cpp index 8d056f2..e537936 100644 --- a/src/Shader.cpp +++ b/src/Shader.cpp @@ -40,9 +40,10 @@ bool Shader::compileFromString(const std::string &source, const std::string &pat if (source.size() == 0) return false; std::ostringstream oss; - oss << "#version 120\n"; + oss << "#version 100\n"; if (srcDefines) oss << srcDefines; + oss << "#line 1\n"; oss << source; // Beware of the lifetime, we use c_str() afterwards! std::string srcStr = oss.str(); diff --git a/src/SoundBuffer.cpp b/src/SoundBuffer.cpp index 01b9ace..32e4743 100644 --- a/src/SoundBuffer.cpp +++ b/src/SoundBuffer.cpp @@ -58,7 +58,7 @@ SoundBuffer::~SoundBuffer() { void SoundBuffer::loadOgg(const std::string &path) { int error = 0; - stb_vorbis* stream = stb_vorbis_open_filename(const_cast(path.c_str()), &error, nullptr); + stb_vorbis *stream = stb_vorbis_open_filename(const_cast(path.c_str()), &error, nullptr); if (stream == nullptr) { getDebugStream() << "Could not load " << path << " : " << error << std::endl; return; diff --git a/src/main.cpp b/src/main.cpp index 52cf601..9d1d0d1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -125,9 +125,9 @@ int main(int argc, char **argv) { if (networkSuccess) GW.setNextState(std::make_shared(&GW, host, port)); else - GW.setNextState(std::make_shared(&GW, "Network init failed!")); - GW.setNextState(std::make_shared(&GW)); + GW.showMessage("Network init failed!"); GW.run(); + G.uninitGL(); } if (GlobalProperties::IsServer) { if (!networkSuccess) {