Twometer / NextCraft

Full 3D Minecraft Multiplayer Client for 1.8
Apache License 2.0
16 stars 5 forks source link

How to build NextCraft for MacOS #1

Closed githubcraft closed 3 years ago

githubcraft commented 3 years ago

Hi! Can NextCraft build on MacOS and play? Thank you

Twometer commented 3 years ago

Hi, currently it only runs on Windows. This is because some parts of the client are using the Win32 APIs (e.g. the Tcp Client uses Winsock). Generally, it should run because OpenGL is platform independent, but it'd be some work to port the non-GL code

githubcraft commented 3 years ago

If you have some time please update it to support multi platform, so that we can easy to test! Thank you

Twometer commented 3 years ago

If I find some time, I can try to do that. However the only macOS device I have access to is from 2009 which is probably not fast enough to develop and test a Minecraft-like game.

Twometer commented 3 years ago

The main things that probably need to be changed is the threading stuff and the TCP stuff, so if you have a faster Mac and would like to try and add support for mac, feel free to submit a PR ;-)

githubcraft commented 3 years ago

@Twometer OK I will try

Twometer commented 3 years ago
githubcraft commented 3 years ago

Hi @Twometer With your help I can now setup the env and compile, as you guess, there is error with TcpClient.h I now will try to correct the TCP stuff if i can ... If you have detail document on BSD Socket API that can apply to our code, please post, I can try to understand. Thank you!!! image

Twometer commented 3 years ago

The BSD Socket API is just a fancy name for the standard C library for sockets - Any resource on the internet can probably explain that better than I can :-) You can just google C++ sockets macOS or maybe try this tutorial.

Twometer commented 3 years ago

BTW, to not break compatibility with windows, you should use #ifdefs around the relevant changes, so that the preprocessor selects the right implementation depending on the OS, for example:

#ifdef _WIN32
#include <winsock2.h>
#else
#include <sys/socket.h>
#endif

If you're interested, a list of all OS constants you can use can be found here

githubcraft commented 3 years ago

Sure! I will try not to break your code on Window platform!

githubcraft commented 3 years ago

Hi @Twometer I am on the way custom the TCP code. What is your current IDE when working with NextCraft and C++ What is your project cppStandard. I am have some problem with intelliSense of Visual Studio Code on MacOS

image

Twometer commented 3 years ago

Hi, nice to hear that it's making progress :-) I am working with JetBrains CLion. As you can also see in CMakeLists.txt, NextCraft is currently using C++ 14.

githubcraft commented 3 years ago

@Twometer Do you have Discord or Telegram, or any other message client, can we make a channel to direct message Thank you so much!

Twometer commented 3 years ago

if you like you can add me on Discord, my tag is Twometer#6969

githubcraft commented 3 years ago

@Twometer I am trying to test TCP code, Now I have another error here NextCraft/lib/mingw_stdthreads/mingw.mutex.h:46:10: fatal error: 'sdkddkver.h' file not found

Twometer commented 3 years ago

Oh... My version of mingw did not have support for std::thread and the like, and therefore I used a compatibility library which adds this. However, on your machine you don't have mingw, which is why it probably fails to build. You'd probably need to update the CMakeLists.txt file so that it does not try to build/link that support library on non-windows systems. You can do that in CMake with

if(WIN32)
  # windows-only stuff here
endif()

( from: https://stackoverflow.com/a/40217291/7702748 )

githubcraft commented 3 years ago

Hi @Twometer I slowly go to this error :)

ld: library not found for -lWs2_32.lib clang: error: linker command failed with exit code 1 (use -v to see invocation)

C++ and Cmake stuffs quite new to me so I can not do it myself, sorry for asking you step by step...

cmake_minimum_required(VERSION 3.14)
project(NextCraft)

set(CMAKE_CXX_STANDARD 14)

add_subdirectory(lib/glfw)
add_subdirectory(lib/json)
add_subdirectory(lib/CrystalUI)

option(MINGW_STDTHREADS_GENERATE_STDHEADERS "" ON)
if(WIN32)
add_subdirectory(lib/mingw_stdthreads)
endif()
add_definitions(-D_WIN32_WINNT=0x0600)

add_executable(NextCraft
        src/net/TcpClient.cpp
        src/net/McClient.cpp
        src/net/McBuffer.cpp
        src/net/NetUtils.cpp
        src/net/chat/ChatParser.cpp
        src/util/ZLib.cpp
        src/util/Logger.cpp
        src/util/FloatBuffer.cpp
        src/model/world/Chunk.cpp
        src/model/world/World.cpp
        src/model/world/Section.cpp
        src/model/world/BlockData.cpp
        src/model/world/AABB.cpp
        src/model/block/BlockRegistry.cpp
        src/model/player/Player.cpp
        src/render/Camera.cpp
        src/render/GameRenderer.cpp
        src/render/shaders/IShader.cpp
        src/render/block/IBlockRenderer.cpp
        src/render/block/DefaultBlockRenderer.cpp
        src/render/block/PlantRenderer.cpp
        src/render/block/LeavesRenderer.cpp
        src/render/block/FluidRenderer.cpp
        src/render/HighlightRenderer.cpp
        src/input/InputHandler.cpp
        src/input/Raycast.cpp
        src/gui/GuiHandler.cpp
        src/gui/MainMenuScreen.cpp
        src/mesh/AsyncMeshBuilder.cpp
        src/mesh/SectionMesh.cpp
        src/mesh/Mesh.cpp
        src/gl/Loader.cpp
        src/gl/Vao.cpp
        src/NextCraft.cpp
        src/main.cpp
        lib/glad/glad.c
        lib/spng/spng.c)

target_include_directories(NextCraft PRIVATE lib/glm lib/glfw/include inc lib/CrystalUI/src lib/CrystalUI/inc lib/concurrentqueue lib/glad/include lib/spng)
target_link_libraries(NextCraft CrystalUI glfw ${GLFW_LIBRARIES} nlohmann_json::nlohmann_json Ws2_32.lib z.lib mingw_stdthreads)
Twometer commented 3 years ago

No Problem :) I think the issue here is that you're still linking against Ws2_32.lib, which is the WinSock library that we are not using on Mac. You should also use a windows-specific if statement around the target_link_libraries. In the non-windows version, I think we should also leave out mingw_stdthreads since we no longer build that library

githubcraft commented 3 years ago

Hi @Twometer After remove lWs2_32.lib and lz.lib due to error library not found, I come to error Undefined symbols for architecture x86_64

ld: library not found for -lWs2_32.lib
clang: error: linker command failed with exit code 1 (use -v to see invocation)

ld: library not found for -lz.lib
clang: error: linker command failed with exit code 1 (use -v to see invocation)

## To fix aboove message, on MacOS we will have
## target_link_libraries(NextCraft CrystalUI glfw ${GLFW_LIBRARIES} nlohmann_json::nlohmann_json)

## ----> Now we are here

Undefined symbols for architecture x86_64:
  "TcpClient::Send(unsigned char*, int)", referenced from:
      McClient::SendPacket(int, McBuffer&) in McClient.cpp.o
  "TcpClient::Close()", referenced from:
      McClient::Connect(char const*, char const*, unsigned short) in McClient.cpp.o
  "TcpClient::Connect(char const*, unsigned short)", referenced from:
      McClient::Connect(char const*, char const*, unsigned short) in McClient.cpp.o
  "TcpClient::Receive(unsigned char*, int)", referenced from:
      McClient::Connect(char const*, char const*, unsigned short) in McClient.cpp.o
  "TcpClient::ReadByte()", referenced from:
      McClient::ReadVarInt() in McClient.cpp.o
  "_crc32", referenced from:
      _read_header in spng.c.o
      _read_chunk_bytes in spng.c.o
      _read_chunks_before_idat in spng.c.o
  "_inflate", referenced from:
      ZLib::Decompress(unsigned char*, int, unsigned char*, int) in ZLib.cpp.o
      _read_scanline_bytes in spng.c.o
  "_inflateEnd", referenced from:
      ZLib::Decompress(unsigned char*, int, unsigned char*, int) in ZLib.cpp.o
      _spng_decode_image in spng.c.o
  "_inflateInit_", referenced from:
      ZLib::Decompress(unsigned char*, int, unsigned char*, int) in ZLib.cpp.o
      _spng_decode_image in spng.c.o
  "_inflateValidate", referenced from:
      _spng_decode_image in spng.c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [NextCraft] Error 1
make[2]: *** [CMakeFiles/NextCraft.dir/all] Error 2
make[1]: *** [CMakeFiles/NextCraft.dir/rule] Error 2
make: *** [NextCraft] Error 2
Twometer commented 3 years ago

z.lib is required for NextCraft. You have to install that library on your computer

githubcraft commented 3 years ago

I just install zlib and I think the code can understand zlib image

I think I must change z.lib to something but I can not find yet My current target_link_libraries target_link_libraries(NextCraft CrystalUI glfw ${GLFW_LIBRARIES} nlohmann_json::nlohmann_json z.lib)

ld: library not found for -lz.lib
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [NextCraft] Error 1
make[2]: *** [CMakeFiles/NextCraft.dir/all] Error 2
make[1]: *** [CMakeFiles/NextCraft.dir/rule] Error 2
make: *** [NextCraft] Error 2
Twometer commented 3 years ago

I think on unix-like systems, it is just -lz Try just z as the name in cmake

githubcraft commented 3 years ago

Ah ok so we only put "z" to target_link_libraries Now the error is about x86_64, Maybe i must reimplement the detail code of TcpClient?

Undefined symbols for architecture x86_64:
  "TcpClient::Send(unsigned char*, int)", referenced from:
      McClient::SendPacket(int, McBuffer&) in McClient.cpp.o
  "TcpClient::Close()", referenced from:
      McClient::Connect(char const*, char const*, unsigned short) in McClient.cpp.o
  "TcpClient::Connect(char const*, unsigned short)", referenced from:
      McClient::Connect(char const*, char const*, unsigned short) in McClient.cpp.o
  "TcpClient::Receive(unsigned char*, int)", referenced from:
      McClient::Connect(char const*, char const*, unsigned short) in McClient.cpp.o
  "TcpClient::ReadByte()", referenced from:
      McClient::ReadVarInt() in McClient.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [NextCraft] Error 1
make[2]: *** [CMakeFiles/NextCraft.dir/all] Error 2
make[1]: *** [CMakeFiles/NextCraft.dir/rule] Error 2
make: *** [NextCraft] Error 2
Twometer commented 3 years ago

Did you accidently remove TcpClient.cpp from the build? because it should actually work now

githubcraft commented 3 years ago

Hi @Twometer Thank you very much, with your help step by step Now i can create tcp socket, connect and login to the server But when loading assets, I have error like bellow

[Info] Initializing mesh builder queue
Status: 0
Connected to 127.0.0.1
[Info] Login completed
[Debug] Loading image assets/fonts/nirmala.png
NextCraft(5385,0x116359e00) malloc: can't allocate region
:*** mach_vm_map(size=140735348928512, flags: 100) failed (error code=3)
NextCraft(5385,0x116359e00) malloc: *** set a breakpoint in malloc_error_break to debug
libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
Signal: SIGABRT (signal SIGABRT)
Twometer commented 3 years ago

You probably don't have the assets folder in path. Try setting the working directory of the app to the main directory, or copying the assets folder to t he output folder.

githubcraft commented 3 years ago

@Twometer Awesome! I now can see the GUI but can not click into any buttons

[Info] Initializing mesh builder queue
Status: 0
Connected to 127.0.0.1
[Debug] Loading image assets/fonts/nirmala.png
INFO: Loading shader
INFO: Loading shader
INFO: Loading shader
[Debug] Loading shader assets/shaders/terrain.v.glsl // assets/shaders/terrain.f.glsl
[Debug] Loading shader assets/shaders/highlight.v.glsl // assets/shaders/highlight.f.glsl
[Debug] Loading image assets/textures/atlas_blocks.png
[Info] Login completed

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

image

image

githubcraft commented 3 years ago

Achievement unlock First blood by Zombie :)))

[21:08:13 INFO]: DevClient[/127.0.0.1:53314] logged in with entity id 343 at (24.5, 69.0, 245.5)
[21:08:13 INFO]: DevClient joined the game
[21:08:43 INFO]: DevClient was slain by Zombie
Twometer commented 3 years ago

Hey, that's really cool that you got it running! 🥳

As for the buttons. I don't know why they don't work but what you can try is setting a breakpoint here and see if GLFW receives the button click. That can tell us if it is a bug with the Gui library.

githubcraft commented 3 years ago

I found that this function calculate wrong when the window on the iMac screen

void Component::on_mouse_down(glm::vec2 vec) {
        if (is_inside(vec))
            isMouseDown = true;
    }

I switch the screen to the extend desktop then the function works and button can click and i can now join the game!

The screen also "eating" my mouse so that I can not esc to get the mouse back.

But hey, thank you so much for help me getting start with this project. I can now start learning more about C++ and this awesome project!

image

githubcraft commented 3 years ago

I am also creating PR for the change, please check if you can use the code Thank you! image

Twometer commented 3 years ago

Yeah I know, it currently eats the mouse. On Windows I used to Alt+Tab out of the window. The misdetection of clicks may be related to a high-DPI screen on your Mac, I'll look into it when I have the time

Twometer commented 3 years ago

As for the PRs, the fixes look nice. However, you've somehow made it on three branches and split it on three PRs...? Normally one only makes a single PR with all your commits in it.

Maybe you can merge your branches together, and then make one Pull Request. That would make it easier for me to test and review the code before merging it into master :)

githubcraft commented 3 years ago

Hi @Twometer I only use the Github web to create PRs so some how it is very complicated. I just try to merge and make new PR, please check! Thank you very much!

Twometer commented 3 years ago

Ah yes that's much better, thank you!