diff --git a/targets/app/common/Game.cpp b/targets/app/common/Game.cpp index aeb8668bd..4db87fc20 100644 --- a/targets/app/common/Game.cpp +++ b/targets/app/common/Game.cpp @@ -73,6 +73,13 @@ #include #include +#ifdef __android__ +#include + +#define TAG "4JCRAFT" +#define AndroidPrintf(...) do { __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__); printf( __VA_ARGS__); } while( 0 ); +#endif + #include "platform/sdl2/Input.h" #include "app/common/src/Audio/SoundEngine.h" #include "app/common/src/Colours/ColourTable.h" @@ -249,8 +256,12 @@ void Game::DebugPrintf(const char* szFormat, ...) { va_start(ap, szFormat); vsnprintf(buf, sizeof(buf), szFormat, ap); va_end(ap); +#if defined(__android__) + AndroidPrintf("%s", buf); +#else OutputDebugStringA(buf); #endif +#endif } void Game::DebugPrintf(int user, const char* szFormat, ...) { @@ -261,7 +272,11 @@ void Game::DebugPrintf(int user, const char* szFormat, ...) { va_start(ap, szFormat); vsnprintf(buf, sizeof(buf), szFormat, ap); va_end(ap); +#if defined(__android__) + AndroidPrintf("%s", buf); +#else OutputDebugStringA(buf); +#endif if (user == USER_UI) { ui.logDebugString(buf); } diff --git a/targets/app/common/src/UI/Controls/UIControl_MinecraftHorse.cpp b/targets/app/common/src/UI/Controls/UIControl_MinecraftHorse.cpp index b785e2a02..013dbea41 100644 --- a/targets/app/common/src/UI/Controls/UIControl_MinecraftHorse.cpp +++ b/targets/app/common/src/UI/Controls/UIControl_MinecraftHorse.cpp @@ -1,6 +1,10 @@ #include "app/common/src/UI/Controls/UIControl_MinecraftHorse.h" +#ifdef GLES +#include +#else #include +#endif #include #include diff --git a/targets/app/common/src/UI/Controls/UIControl_MinecraftPlayer.cpp b/targets/app/common/src/UI/Controls/UIControl_MinecraftPlayer.cpp index 4a025068b..60461442e 100644 --- a/targets/app/common/src/UI/Controls/UIControl_MinecraftPlayer.cpp +++ b/targets/app/common/src/UI/Controls/UIControl_MinecraftPlayer.cpp @@ -1,6 +1,10 @@ #include "UIControl_MinecraftPlayer.h" +#ifdef GLES +#include +#else #include +#endif #include #include diff --git a/targets/app/include/stubs.h b/targets/app/include/stubs.h index 68788082c..4a6579a35 100644 --- a/targets/app/include/stubs.h +++ b/targets/app/include/stubs.h @@ -2,8 +2,12 @@ #include #ifdef __linux__ +#ifdef GLES +#include +#else #include #include +#endif #undef GL_SMOOTH #undef GL_FLAT @@ -166,7 +170,13 @@ public: static const int GL_FLAT = 0x1D00; #undef glShadeModel #define GL_SHADEMODEL_IS_FUNCTION - static void glShadeModel(int mode) { ::glShadeModel(mode); } + static void glShadeModel(int mode) { +#ifdef GLES + glShadeModel(mode); +#else + ::glShadeModel(mode); +#endif + } }; #undef GL_ARRAY_BUFFER_ARB #undef GL_STREAM_DRAW_ARB diff --git a/targets/app/linux/Iggy/gdraw/gdraw.c b/targets/app/linux/Iggy/gdraw/gdraw.c index 8b9f716ea..60d52dcb9 100644 --- a/targets/app/linux/Iggy/gdraw/gdraw.c +++ b/targets/app/linux/Iggy/gdraw/gdraw.c @@ -2,6 +2,16 @@ #include "gdraw.h" +// Do we really really need gdraw for the java ui? +#ifndef _ENABLEIGGY +void gdraw_GL_BeginCustomDraw_4J(IggyCustomDrawCallbackRegion* region, F32* matrix) {} +void gdraw_GL_CalculateCustomDraw_4J(IggyCustomDrawCallbackRegion* region, F32* matrix) {} +void gdraw_GL_EndCustomDraw(IggyCustomDrawCallbackRegion* region) {} +void gdraw_GL_SetTileOrigin(S32 vx, S32 vy, unsigned int framebuffer) {} +void gdraw_GL_WrappedTextureDestroy(GDrawTexture* handle) {} + +#else + #include #include #include @@ -838,3 +848,4 @@ void gdraw_GL_CalculateCustomDraw_4J(IggyCustomDrawCallbackRegion* region, F32* matrix) { gdraw_GetObjectSpaceMatrix(matrix, region->o2w, gdraw->projection, 0.0f, 0); } +#endif \ No newline at end of file diff --git a/targets/app/linux/LinuxGL.cpp b/targets/app/linux/LinuxGL.cpp index 19a3ea8c0..b43c8db9f 100644 --- a/targets/app/linux/LinuxGL.cpp +++ b/targets/app/linux/LinuxGL.cpp @@ -11,17 +11,14 @@ extern C4JRender RenderManager; #ifdef GLES extern "C" { -extern void glClearDepthf(float depth); -void glClearDepth(double depth) { glClearDepthf((float)depth); } -void glTexGeni(unsigned int, unsigned int, int) {} -void glTexGenfv(unsigned int, unsigned int, const float*) {} -void glTexCoordPointer(int, unsigned int, int, const void*) {} -void glNormalPointer(unsigned int, int, const void*) {} -void glColorPointer(int, unsigned int, int, const void*) {} -void glVertexPointer(int, unsigned int, int, const void*) {} -void glEndList(void) {} -void glCallLists(int, unsigned int, const void*) {} + void glTexGeni(GLenum, GLenum, GLint) {} } +void glTexGenfv(unsigned int, unsigned int, const float*) {} +void glTexCoordPointer(int, unsigned int, int) {} +void glNormalPointer(unsigned int, int) {} +void glColorPointer(int, unsigned int, int, const void*) {} +void glVertexPointer(int, unsigned int, int) {} +void glCallLists(int) {} #endif inline int* getIntPtr(IntBuffer* buf) { @@ -110,14 +107,32 @@ void glTexGen_4J(int, int, FloatBuffer*) {} #include #include +#ifdef GLES +static PFNGLGENQUERIESPROC _glGenQueriesARB = nullptr; +static PFNGLBEGINQUERYPROC _glBeginQueryARB = nullptr; +static PFNGLENDQUERYPROC _glEndQueryARB = nullptr; +static PFNGLGETQUERYOBJECTUIVPROC _glGetQueryObjectuivARB = nullptr; +static bool _queriesInitialized = false; +#else static PFNGLGENQUERIESARBPROC _glGenQueriesARB = nullptr; static PFNGLBEGINQUERYARBPROC _glBeginQueryARB = nullptr; static PFNGLENDQUERYARBPROC _glEndQueryARB = nullptr; static PFNGLGETQUERYOBJECTUIVARBPROC _glGetQueryObjectuivARB = nullptr; static bool _queriesInitialized = false; +#endif static void initQueryFuncs() { if (_queriesInitialized) return; +#ifdef GLES + _queriesInitialized = true; + _glGenQueriesARB = + (PFNGLGENQUERIESPROC)dlsym(RTLD_DEFAULT, "glGenQueries"); + _glBeginQueryARB = + (PFNGLBEGINQUERYPROC)dlsym(RTLD_DEFAULT, "glBeginQuery"); + _glEndQueryARB = (PFNGLENDQUERYPROC)dlsym(RTLD_DEFAULT, "glEndQuery"); + _glGetQueryObjectuivARB = (PFNGLGETQUERYOBJECTUIVPROC)dlsym( + RTLD_DEFAULT, "glGetQueryObjectuiv"); +#else _queriesInitialized = true; _glGenQueriesARB = (PFNGLGENQUERIESARBPROC)dlsym(RTLD_DEFAULT, "glGenQueriesARB"); @@ -126,6 +141,7 @@ static void initQueryFuncs() { _glEndQueryARB = (PFNGLENDQUERYARBPROC)dlsym(RTLD_DEFAULT, "glEndQueryARB"); _glGetQueryObjectuivARB = (PFNGLGETQUERYOBJECTUIVARBPROC)dlsym( RTLD_DEFAULT, "glGetQueryObjectuivARB"); +#endif } void glGenQueriesARB_4J(IntBuffer* buf) { diff --git a/targets/app/linux/Linux_UIController.cpp b/targets/app/linux/Linux_UIController.cpp index e6cf502b2..b8a47f938 100644 --- a/targets/app/linux/Linux_UIController.cpp +++ b/targets/app/linux/Linux_UIController.cpp @@ -1,5 +1,8 @@ +#ifdef GLES +#include +#else #include - +#endif // GDraw GL backend for Linux #include "platform/sdl2/Render.h" #include "Linux_UIController.h" diff --git a/targets/app/meson.build b/targets/app/meson.build index 84f34fc2a..146d1753e 100644 --- a/targets/app/meson.build +++ b/targets/app/meson.build @@ -69,6 +69,27 @@ endif platform_services_src = files('../platform/PlatformServices.cpp') +if host_machine.system() == 'android' + client_dependencies += declare_dependency(dependencies: cc.find_library('log', required: true)) +endif + +if host_machine.system() == 'android' +client = shared_library( + 'MinecraftClient', + client_sources + platform_sources + platform_services_src + localisation[1], + include_directories: include_directories('include', '..'), + dependencies: client_dependencies, + cpp_args: global_cpp_args + + global_cpp_defs + + [ + '-DUNICODE', + '-D_UNICODE', + ], + c_args: global_cpp_defs + ['-DUNICODE', '-D_UNICODE'], + install: true, + install_dir: '', +) +else client = executable( 'Minecraft.Client', client_sources + platform_sources + platform_services_src + localisation[1], @@ -84,6 +105,7 @@ client = executable( install: true, install_dir: '', ) +endif custom_target( 'copy_assets_to_client', diff --git a/targets/java/src/File.cpp b/targets/java/src/File.cpp index 4af84d5de..9e3ca83d2 100644 --- a/targets/java/src/File.cpp +++ b/targets/java/src/File.cpp @@ -205,7 +205,10 @@ bool File::mkdirs() const { return false; } - return fs::create_directories(path, error); + // Previous behavior could return false even if the directory did not exist + // and was successfully created, at least this happens on Android + fs::create_directories(path, error); + return !error; } /* diff --git a/targets/minecraft/client/Camera.cpp b/targets/minecraft/client/Camera.cpp index 8963c1cfd..7ac425d5e 100644 --- a/targets/minecraft/client/Camera.cpp +++ b/targets/minecraft/client/Camera.cpp @@ -1,6 +1,10 @@ #include "Camera.h" +#ifdef GLES +#include +#else #include +#endif #include #include #include @@ -9,6 +13,7 @@ #include "MemoryTracker.h" #include "app/include/stubs.h" +#include "platform/sdl2/Render.h" #include "java/FloatBuffer.h" #include "minecraft/world/entity/LivingEntity.h" #include "minecraft/world/entity/player/Player.h" diff --git a/targets/minecraft/client/Lighting.cpp b/targets/minecraft/client/Lighting.cpp index 77bfbddd6..019889655 100644 --- a/targets/minecraft/client/Lighting.cpp +++ b/targets/minecraft/client/Lighting.cpp @@ -1,6 +1,10 @@ #include "Lighting.h" +#ifdef GLES +#include +#else #include +#endif #include "platform/sdl2/Render.h" #include "app/include/stubs.h" diff --git a/targets/minecraft/client/gui/GuiComponent.cpp b/targets/minecraft/client/gui/GuiComponent.cpp index e6e40ac8c..6cd2c7203 100644 --- a/targets/minecraft/client/gui/GuiComponent.cpp +++ b/targets/minecraft/client/gui/GuiComponent.cpp @@ -1,6 +1,10 @@ #include "GuiComponent.h" +#ifdef GLES +#include +#else #include +#endif #include #include "platform/sdl2/Render.h" diff --git a/targets/minecraft/client/gui/Minimap.cpp b/targets/minecraft/client/gui/Minimap.cpp index c59353751..5635b64bc 100644 --- a/targets/minecraft/client/gui/Minimap.cpp +++ b/targets/minecraft/client/gui/Minimap.cpp @@ -1,6 +1,10 @@ #include "Minimap.h" +#ifdef GLES +#include +#else #include +#endif #include #include #include diff --git a/targets/minecraft/client/gui/achievement/AchievementPopup.cpp b/targets/minecraft/client/gui/achievement/AchievementPopup.cpp index fcab78a71..3904f9455 100644 --- a/targets/minecraft/client/gui/achievement/AchievementPopup.cpp +++ b/targets/minecraft/client/gui/achievement/AchievementPopup.cpp @@ -1,6 +1,10 @@ #include "AchievementPopup.h" +#ifdef GLES +#include +#else #include +#endif #include "platform/sdl2/Render.h" #include "java/System.h" diff --git a/targets/minecraft/client/gui/achievement/AchievementScreen.cpp b/targets/minecraft/client/gui/achievement/AchievementScreen.cpp index 45e98cb32..5d2f728d5 100644 --- a/targets/minecraft/client/gui/achievement/AchievementScreen.cpp +++ b/targets/minecraft/client/gui/achievement/AchievementScreen.cpp @@ -1,6 +1,10 @@ #include "AchievementScreen.h" +#ifdef GLES +#include +#else #include +#endif #include #include diff --git a/targets/minecraft/client/gui/inventory/BeaconScreen.cpp b/targets/minecraft/client/gui/inventory/BeaconScreen.cpp index 8ff2f0589..84a2babd3 100644 --- a/targets/minecraft/client/gui/inventory/BeaconScreen.cpp +++ b/targets/minecraft/client/gui/inventory/BeaconScreen.cpp @@ -1,6 +1,10 @@ #include "BeaconScreen.h" +#ifdef GLES +#include +#else #include +#endif #include #include diff --git a/targets/minecraft/client/gui/inventory/CreativeInventoryScreen.cpp b/targets/minecraft/client/gui/inventory/CreativeInventoryScreen.cpp index e3e88aa76..51d93311e 100644 --- a/targets/minecraft/client/gui/inventory/CreativeInventoryScreen.cpp +++ b/targets/minecraft/client/gui/inventory/CreativeInventoryScreen.cpp @@ -1,6 +1,10 @@ #include "CreativeInventoryScreen.h" +#ifdef GLES +#include +#else #include +#endif #include #include diff --git a/targets/minecraft/client/gui/inventory/RepairScreen.cpp b/targets/minecraft/client/gui/inventory/RepairScreen.cpp index 2ee2cd99a..a8086d8ce 100644 --- a/targets/minecraft/client/gui/inventory/RepairScreen.cpp +++ b/targets/minecraft/client/gui/inventory/RepairScreen.cpp @@ -1,6 +1,10 @@ #include "RepairScreen.h" +#ifdef GLES +#include +#else #include +#endif #include #include diff --git a/targets/minecraft/client/model/ChestModel.cpp b/targets/minecraft/client/model/ChestModel.cpp index 1c4200673..67d46da4b 100644 --- a/targets/minecraft/client/model/ChestModel.cpp +++ b/targets/minecraft/client/model/ChestModel.cpp @@ -1,6 +1,10 @@ #include "ChestModel.h" +#ifdef GLES +#include +#else #include +#endif #include "minecraft/client/model/geom/ModelPart.h" diff --git a/targets/minecraft/client/model/geom/ModelPart.cpp b/targets/minecraft/client/model/geom/ModelPart.cpp index c64c567b5..284afe6a2 100644 --- a/targets/minecraft/client/model/geom/ModelPart.cpp +++ b/targets/minecraft/client/model/geom/ModelPart.cpp @@ -1,6 +1,10 @@ #include "ModelPart.h" +#ifdef GLES +#include +#else #include +#endif #include diff --git a/targets/minecraft/client/particle/FootstepParticle.cpp b/targets/minecraft/client/particle/FootstepParticle.cpp index 1a270c663..e5b87cec0 100644 --- a/targets/minecraft/client/particle/FootstepParticle.cpp +++ b/targets/minecraft/client/particle/FootstepParticle.cpp @@ -1,6 +1,10 @@ #include "FootstepParticle.h" +#ifdef GLES +#include +#else #include +#endif #include diff --git a/targets/minecraft/client/particle/HugeExplosionParticle.cpp b/targets/minecraft/client/particle/HugeExplosionParticle.cpp index 428de1d37..a77089869 100644 --- a/targets/minecraft/client/particle/HugeExplosionParticle.cpp +++ b/targets/minecraft/client/particle/HugeExplosionParticle.cpp @@ -1,6 +1,10 @@ #include "HugeExplosionParticle.h" +#ifdef GLES +#include +#else #include +#endif #include "platform/sdl2/Render.h" #include "app/common/App_enums.h" diff --git a/targets/minecraft/client/particle/ParticleEngine.cpp b/targets/minecraft/client/particle/ParticleEngine.cpp index 0ec0743af..ca65e0cf1 100644 --- a/targets/minecraft/client/particle/ParticleEngine.cpp +++ b/targets/minecraft/client/particle/ParticleEngine.cpp @@ -1,6 +1,10 @@ #include "ParticleEngine.h" +#ifdef GLES +#include +#else #include +#endif #include #include diff --git a/targets/minecraft/client/particle/TakeAnimationParticle.cpp b/targets/minecraft/client/particle/TakeAnimationParticle.cpp index 9f4e44be7..60202b2aa 100644 --- a/targets/minecraft/client/particle/TakeAnimationParticle.cpp +++ b/targets/minecraft/client/particle/TakeAnimationParticle.cpp @@ -1,6 +1,10 @@ #include "TakeAnimationParticle.h" +#ifdef GLES +#include +#else #include +#endif #include diff --git a/targets/minecraft/client/renderer/Chunk.cpp b/targets/minecraft/client/renderer/Chunk.cpp index d89a7385f..fe581b473 100644 --- a/targets/minecraft/client/renderer/Chunk.cpp +++ b/targets/minecraft/client/renderer/Chunk.cpp @@ -1,6 +1,10 @@ #include "Chunk.h" +#ifdef GLES +#include +#else #include +#endif #include #include diff --git a/targets/minecraft/client/renderer/ItemInHandRenderer.cpp b/targets/minecraft/client/renderer/ItemInHandRenderer.cpp index bc85fa098..476c6a837 100644 --- a/targets/minecraft/client/renderer/ItemInHandRenderer.cpp +++ b/targets/minecraft/client/renderer/ItemInHandRenderer.cpp @@ -1,6 +1,10 @@ #include "ItemInHandRenderer.h" +#ifdef GLES +#include +#else #include +#endif #include #include diff --git a/targets/minecraft/client/renderer/LevelRenderer.cpp b/targets/minecraft/client/renderer/LevelRenderer.cpp index 2651d019f..e427202df 100644 --- a/targets/minecraft/client/renderer/LevelRenderer.cpp +++ b/targets/minecraft/client/renderer/LevelRenderer.cpp @@ -1,6 +1,10 @@ #include "LevelRenderer.h" +#ifdef GLES +#include +#else #include +#endif #include #include #include diff --git a/targets/minecraft/client/renderer/Tesselator.cpp b/targets/minecraft/client/renderer/Tesselator.cpp index b10c8fd3f..78b36f503 100644 --- a/targets/minecraft/client/renderer/Tesselator.cpp +++ b/targets/minecraft/client/renderer/Tesselator.cpp @@ -1,6 +1,10 @@ #include "Tesselator.h" +#ifdef GLES +#include +#else #include +#endif #include diff --git a/targets/minecraft/client/renderer/TileRenderer.cpp b/targets/minecraft/client/renderer/TileRenderer.cpp index cb4390a3a..0dbeb2994 100644 --- a/targets/minecraft/client/renderer/TileRenderer.cpp +++ b/targets/minecraft/client/renderer/TileRenderer.cpp @@ -1,6 +1,10 @@ #include "TileRenderer.h" +#ifdef GLES +#include +#else #include +#endif #include #include #include diff --git a/targets/minecraft/client/renderer/culling/Frustum.cpp b/targets/minecraft/client/renderer/culling/Frustum.cpp index e39c9cf6d..4ee3686f7 100644 --- a/targets/minecraft/client/renderer/culling/Frustum.cpp +++ b/targets/minecraft/client/renderer/culling/Frustum.cpp @@ -1,6 +1,10 @@ #include "Frustum.h" +#ifdef GLES +#include +#else #include +#endif #include #include diff --git a/targets/minecraft/client/renderer/entity/ItemFrameRenderer.cpp b/targets/minecraft/client/renderer/entity/ItemFrameRenderer.cpp index fd162027d..d48e9c86b 100644 --- a/targets/minecraft/client/renderer/entity/ItemFrameRenderer.cpp +++ b/targets/minecraft/client/renderer/entity/ItemFrameRenderer.cpp @@ -1,4 +1,8 @@ +#ifdef GLES +#include +#else #include +#endif #include diff --git a/targets/minecraft/client/renderer/texture/Texture.h b/targets/minecraft/client/renderer/texture/Texture.h index d1699f23f..60d04ca37 100644 --- a/targets/minecraft/client/renderer/texture/Texture.h +++ b/targets/minecraft/client/renderer/texture/Texture.h @@ -1,6 +1,11 @@ #pragma once +#ifdef GLES +#include +#include +#else #include +#endif #include #include @@ -13,12 +18,19 @@ class BufferedImage; class Texture { public: static const int WM_WRAP = GL_REPEAT; +#ifdef GLES + static const int WM_CLAMP = GL_CLAMP_TO_EDGE; +#else static const int WM_CLAMP = GL_CLAMP; +#endif static const int WM_MIRROR = 0; // GL_MIRRORED_REPEAT; static const int TFMT_RGBA = GL_RGBA; +#ifdef GLES + static const int TFMT_BGRA = GL_BGRA_EXT; +#else static const int TFMT_BGRA = GL_BGRA; - +#endif static const int TFLT_NEAREST = GL_NEAREST; static const int TFLT_LINEAR = GL_LINEAR; static const int TFLT_LINEAR_MIP_NEAREST = 0; // GL_LINEAR_MIPMAP_NEAREST; diff --git a/targets/minecraft/client/skins/AbstractTexturePack.cpp b/targets/minecraft/client/skins/AbstractTexturePack.cpp index 688e2cc7f..f87820210 100644 --- a/targets/minecraft/client/skins/AbstractTexturePack.cpp +++ b/targets/minecraft/client/skins/AbstractTexturePack.cpp @@ -1,6 +1,10 @@ #include "AbstractTexturePack.h" +#ifdef GLES +#include +#else #include +#endif #include #include diff --git a/targets/minecraft/client/title/TitleScreen.cpp b/targets/minecraft/client/title/TitleScreen.cpp index 52633dada..3e34628c4 100644 --- a/targets/minecraft/client/title/TitleScreen.cpp +++ b/targets/minecraft/client/title/TitleScreen.cpp @@ -4,6 +4,7 @@ #include #include +#include // for std::numbers #include "platform/sdl2/Render.h" #include "app/linux/LinuxGame.h" diff --git a/targets/platform/StdFileIO.h b/targets/platform/StdFileIO.h index d5bf0c5fa..811cb13a0 100644 --- a/targets/platform/StdFileIO.h +++ b/targets/platform/StdFileIO.h @@ -75,13 +75,18 @@ public: } std::filesystem::path getBasePath() override { -#if defined(__linux__) +#if defined(__linux__) && !defined (__android__) char buf[4096]; ssize_t len = readlink("/proc/self/exe", buf, sizeof(buf) - 1); if (len > 0) { buf[len] = '\0'; return std::filesystem::path(buf).parent_path(); } +#elif defined(__android__) + // Because of the static init fiasco + // I need to set the path before loading the library + // I can only do this through ENV + return std::filesystem::path( getenv("MC_PATH") ); #endif return std::filesystem::current_path(); } diff --git a/targets/platform/sdl2/Render.cpp b/targets/platform/sdl2/Render.cpp index 0e570dc1d..e77f79fb0 100644 --- a/targets/platform/sdl2/Render.cpp +++ b/targets/platform/sdl2/Render.cpp @@ -1,11 +1,11 @@ #include "Render.h" #include "../PlatformTypes.h" -#include "SDL.h" -#include "SDL_error.h" -#include "SDL_events.h" -#include "SDL_stdinc.h" -#include "SDL_video.h" +#include +#include +#include +#include +#include #include "gl3_loader.h" // undefine macros from header to avoid argument mismatch @@ -41,6 +41,11 @@ #include #include +#ifdef __android__ +#include +#include +#endif + #include #include #include @@ -51,6 +56,12 @@ #include #include + +#ifdef __android__ +#define fprintf(file, ...) ((void)__android_log_print(ANDROID_LOG_INFO, "4JRENDER", __VA_ARGS__)) +#define printf(...) ((void)__android_log_print(ANDROID_LOG_INFO, "4JRENDER", __VA_ARGS__)) +#endif + C4JRender RenderManager; // MARK: Shaders @@ -81,6 +92,11 @@ static const char* FRAG_SRC = // MARK: OpenGL state // Hello SDL and opengl 3.3 +#ifdef __android__ +static EGLDisplay s_eglDisplay = EGL_NO_DISPLAY; +static EGLContext s_mainEglContext = EGL_NO_CONTEXT; +static EGLSurface s_mainEglSurface = EGL_NO_SURFACE; +#endif static SDL_Window* s_window = nullptr; static SDL_GLContext s_glContext = nullptr; static bool s_shouldClose = false; @@ -94,8 +110,14 @@ static pthread_key_t s_glCtxKey; static pthread_once_t s_glCtxKeyOnce = PTHREAD_ONCE_INIT; static void makeGLCtxKey() { pthread_key_create(&s_glCtxKey, nullptr); } static const int MAX_SHARED_CTXS = 6; + +#ifdef __android__ +static EGLSurface s_sharedWins[MAX_SHARED_CTXS] = {}; +static EGLContext s_sharedCtxs[MAX_SHARED_CTXS] = {}; +#else static SDL_Window* s_sharedWins[MAX_SHARED_CTXS] = {}; static SDL_GLContext s_sharedCtxs[MAX_SHARED_CTXS] = {}; +#endif static int s_sharedCtxCount = 0; static int s_nextSharedCtx = 0; static pthread_mutex_t s_sharedMtx = PTHREAD_MUTEX_INITIALIZER; @@ -647,6 +669,9 @@ void C4JRender::Initialise() { SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); +#ifdef __android__ + SDL_SetHint(SDL_HINT_ORIENTATIONS, "LandscapeLeft LandscapeRight"); +#endif Uint32 wf = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; if (s_fullscreen) wf |= SDL_WINDOW_FULLSCREEN_DESKTOP; s_window = SDL_CreateWindow("Minecraft Console Edition", @@ -686,16 +711,50 @@ void C4JRender::Initialise() { s_mainThread = pthread_self(); s_mainThreadSet = true; pthread_setspecific(s_glCtxKey, (void*)s_window); + +#ifdef __android__ + s_eglDisplay = eglGetCurrentDisplay(); + + EGLint pb_atr[] = { + EGL_WIDTH, 1, + EGL_HEIGHT, 1, + EGL_NONE + }; + + EGLint cfg_id; + if (!eglQueryContext(s_eglDisplay, (EGLContext)s_glContext, EGL_CONFIG_ID, &cfg_id)) + { + printf("BRUH\n"); + } + + EGLConfig cfg; + EGLint cfgs; + EGLint atrs[] = { EGL_CONFIG_ID, cfg_id, EGL_NONE }; + + eglChooseConfig(s_eglDisplay, atrs, &cfg, 1, &cfgs); +#endif + SDL_GL_MakeCurrent(s_window, s_glContext); for (int i = 0; i < MAX_SHARED_CTXS; i++) { SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1); +#ifndef __android__ SDL_Window* w = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1, 1, SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL); if (!w) break; SDL_GLContext ctx = SDL_GL_CreateContext(w); +#else + EGLint ctx_atr[] = { EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE }; + EGLContext ctx = eglCreateContext(s_eglDisplay, cfg, (EGLContext)s_glContext, ctx_atr); + EGLSurface w = eglCreatePbufferSurface(s_eglDisplay, cfg, pb_atr); + if ( w == EGL_NO_SURFACE ) { + printf("EGL_NO_SURFACE: %d\n", eglGetError() ); + } +#endif if (!ctx) { +#ifndef __android__ SDL_DestroyWindow(w); +#endif break; } s_sharedWins[s_sharedCtxCount] = w; @@ -722,10 +781,18 @@ void C4JRender::InitialiseContext() { } void* cp = pthread_getspecific(s_glCtxKey); if (cp) { +#ifdef __android__ + EGLContext ctx = (EGLContext)cp; +#else SDL_GLContext ctx = (SDL_GLContext)cp; +#endif for (int i = 0; i < s_sharedCtxCount; i++) if (s_sharedCtxs[i] == ctx) { +#ifdef __android__ + eglMakeCurrent(s_eglDisplay, s_sharedWins[i], s_sharedWins[i], ctx ); +#else SDL_GL_MakeCurrent(s_sharedWins[i], ctx); +#endif return; } return; @@ -738,7 +805,11 @@ void C4JRender::InitialiseContext() { if (!shared) return; for (int i = 0; i < s_sharedCtxCount; i++) if (s_sharedCtxs[i] == shared) +#ifdef __android__ + eglMakeCurrent(s_eglDisplay, s_sharedWins[i], s_sharedWins[i], shared ); +#else SDL_GL_MakeCurrent(s_sharedWins[i], shared); +#endif pthread_setspecific(s_glCtxKey, (void*)shared); } @@ -801,8 +872,14 @@ void C4JRender::Shutdown() { s_window = nullptr; } for (int i = 0; i < s_sharedCtxCount; i++) { +#ifdef __android__ + eglMakeCurrent(s_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); + if (s_sharedCtxs[i]) eglDestroySurface(s_eglDisplay, s_sharedWins[i]); + if (s_sharedWins[i]) eglDestroyContext(s_eglDisplay, s_sharedCtxs[i]); +#else if (s_sharedCtxs[i]) SDL_GL_DeleteContext(s_sharedCtxs[i]); if (s_sharedWins[i]) SDL_DestroyWindow(s_sharedWins[i]); +#endif } SDL_Quit(); } diff --git a/targets/platform/sdl2/Render.h b/targets/platform/sdl2/Render.h index aeb67c263..78d5fd587 100644 --- a/targets/platform/sdl2/Render.h +++ b/targets/platform/sdl2/Render.h @@ -2,8 +2,12 @@ #include "gl3_loader.h" // NOTE: gl3_loader.h must be included before these two +#ifdef GLES +#include +#else #include #include +#endif #include #include @@ -386,6 +390,34 @@ extern C4JRender RenderManager; #define GL_TRIANGLE_STRIP 0x0005 #endif +// These constants do not exist on the GLES, +// so for now it is just stub +#ifdef GLES + +#ifndef GL_COLOR_MATERIAL +#define GL_COLOR_MATERIAL 0x0B57 +#endif +#ifndef GL_RESCALE_NORMAL +#define GL_RESCALE_NORMAL 0x803A +#endif +#ifndef GL_NORMALIZE +#define GL_NORMALIZE 0x0BA1 +#endif +#ifndef GL_POLYGON_OFFSET_LINE +#define GL_POLYGON_OFFSET_LINE 0x2A02 +#endif +#ifndef GL_AMBIENT_AND_DIFFUSE +#define GL_AMBIENT_AND_DIFFUSE 0x1602 +#endif + +extern "C" { +void glShadeModel(GLenum); +void glColorMaterial(GLenum, GLenum); +void glNormal3f(GLfloat, GLfloat, GLfloat); +void glTexGeni(GLenum, GLenum, GLint); +} +#endif + // glCallList / display list macros #undef glNewList #define glNewList(_list, _mode) RenderManager.CBuffStart(_list) @@ -726,3 +758,7 @@ void glGetQueryObjectu_4J_Helper(unsigned int id, unsigned int pname, #define glLight(a, b, c) glLight_4J(a, b, c) #define glLightModel(a, b) glLightModel_4J(a, b) #define glTexGen(a, b, c) glTexGen_4J(a, b, c) + +#ifdef GLES +#define glClearDepth(a) glClearDepthf((float)a) +#endif \ No newline at end of file diff --git a/targets/platform/sdl2/gl3_loader.h b/targets/platform/sdl2/gl3_loader.h index 6416a1152..8b6b26401 100644 --- a/targets/platform/sdl2/gl3_loader.h +++ b/targets/platform/sdl2/gl3_loader.h @@ -2,9 +2,13 @@ #ifndef GL_GLEXT_PROTOTYPES #define GL_GLEXT_PROTOTYPES 1 #endif +#ifdef GLES +#include +#include +#else #include #include -#include +#endif #include #ifndef GL_ARRAY_BUFFER