From d6713c3a9626ed1a572baf596a7a3f44d2c89264 Mon Sep 17 00:00:00 2001 From: Thimo Date: Sun, 8 Nov 2020 14:09:47 +0100 Subject: [PATCH 1/4] Fix compiler warnings --- src/Assets/Sprite.bf | 4 ++-- src/Static/Console.bf | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Assets/Sprite.bf b/src/Assets/Sprite.bf index e2b61b0..a6bd6e9 100644 --- a/src/Assets/Sprite.bf +++ b/src/Assets/Sprite.bf @@ -560,7 +560,7 @@ namespace Strawberry delete BlendModes; } - private static readonly Blend[] BlendModes = new Blend[] + private static readonly Blend[] BlendModes = new Blend[]( { // 0 - NORMAL new (dest, index, src, opacity) => @@ -596,7 +596,7 @@ namespace Strawberry dest[index + 3] = a; } } - }; + }); [Inline] private static int MUL_UN8(int a, int b) diff --git a/src/Static/Console.bf b/src/Static/Console.bf index 70a888e..08fb0ef 100644 --- a/src/Static/Console.bf +++ b/src/Static/Console.bf @@ -229,7 +229,7 @@ namespace Strawberry { let name = scope String(); type.GetName(name); - let error = Calc.StringArgs("{0} type arguments not supported in commands.", scope Object[] { name }); + let error = Calc.StringArgs("{0} type arguments not supported in commands.", scope Object[]({ name })); Runtime.FatalError(error); } From decb8b855d3c301216ea88b90ccaf74b873afd97 Mon Sep 17 00:00:00 2001 From: Thimo Date: Sun, 8 Nov 2020 14:12:08 +0100 Subject: [PATCH 2/4] Add hitbox outline utility method --- SampleGame/src/Player.bf | 6 +++--- src/Core/Entity.bf | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/SampleGame/src/Player.bf b/SampleGame/src/Player.bf index 99342ad..63ea746 100644 --- a/SampleGame/src/Player.bf +++ b/SampleGame/src/Player.bf @@ -12,7 +12,7 @@ namespace Strawberry.Sample public this(Point pos) : base(pos) { - Hitbox = Rect(-4, -8, 8, 8); + Hitbox = Rect(-4, -8, 16, 16); Add(tJumpGrace = new Timer()); Add(tVarJump = new Timer()); @@ -102,8 +102,8 @@ namespace Strawberry.Sample { base.Draw(); - DrawHitbox(.Green); - //Game.Batcher.Tex(Assets.Textures["test"], X - 4, Y - 8); + DrawHitboxOutline(.Green); + Game.Batcher.Tex(Assets.Textures["test"], X - 4, Y - 8); } } } diff --git a/src/Core/Entity.bf b/src/Core/Entity.bf index 8182e46..7a3173a 100644 --- a/src/Core/Entity.bf +++ b/src/Core/Entity.bf @@ -238,6 +238,20 @@ namespace Strawberry } } + public Rect SceneHitboxOutline + { + [Inline] + get + { + Rect hb = Hitbox + Position; + hb.X -= 1; + hb.Y -= 1; + hb.Width += 2; + hb.Height += 2; + return hb; + } + } + public int Left { [Inline] @@ -463,6 +477,11 @@ namespace Strawberry Game.Batcher.Rect(SceneHitbox, color); } + public void DrawHitboxOutline(Color color) + { + Game.Batcher.Rect(SceneHitboxOutline, color); + } + public T SceneAs() where T : Scene { Runtime.Assert(Scene is T, "Scene type mismatch!"); From ae8ffdd2e3b567a1faef4936366348cf7ab38418 Mon Sep 17 00:00:00 2001 From: Thimo Date: Sun, 8 Nov 2020 14:20:08 +0100 Subject: [PATCH 3/4] Fix texures * Add missing Mipmap generation call * Add missing state clearing * Add optional linear Mipmap flag setting * Add optional edge clamping * Fix texture orientation in Batcher.bf * Fix example --- SampleGame/src/Program.bf | 1 + src/PlatformLayer/Batcher.bf | 8 ++++---- src/PlatformLayer/SDL2/SDL2PlatformLayer.bf | 4 +++- src/PlatformLayer/Texture.bf | 20 +++++++++++++++++++- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/SampleGame/src/Program.bf b/SampleGame/src/Program.bf index 1f7c2c3..7e68b51 100644 --- a/SampleGame/src/Program.bf +++ b/SampleGame/src/Program.bf @@ -8,6 +8,7 @@ namespace Strawberry.Sample static public int Main(String[] args) { let sdl = scope SDL2PlatformLayer(); + sdl.TexturesEnableEdgeClamping = true; let game = scope SampleGame(sdl); game.Run(); diff --git a/src/PlatformLayer/Batcher.bf b/src/PlatformLayer/Batcher.bf index f40876e..290b6c9 100644 --- a/src/PlatformLayer/Batcher.bf +++ b/src/PlatformLayer/Batcher.bf @@ -69,10 +69,10 @@ namespace Strawberry public void Tex(Texture texture, float x, float y) { PushQuad(.TextureTint, texture, - .Tex(.(x, y), .(0, 1), Color.White), - .Tex(.(x + texture.Width, y), .(1, 1), Color.White), - .Tex(.(x + texture.Width, y + texture.Height), .(1, 0), Color.White), - .Tex(.(x, y + texture.Height), .(0, 0), Color.White)); + .Tex(.(x, y), .(0, 0), Color.White), + .Tex(.(x + texture.Width, y), .(1, 0), Color.White), + .Tex(.(x + texture.Width, y + texture.Height), .(1, 1), Color.White), + .Tex(.(x, y + texture.Height), .(0, 1), Color.White)); } } } diff --git a/src/PlatformLayer/SDL2/SDL2PlatformLayer.bf b/src/PlatformLayer/SDL2/SDL2PlatformLayer.bf index e88f05d..12e1c52 100644 --- a/src/PlatformLayer/SDL2/SDL2PlatformLayer.bf +++ b/src/PlatformLayer/SDL2/SDL2PlatformLayer.bf @@ -8,6 +8,8 @@ namespace Strawberry.SDL2 { public int TransformMatrixLocation { get; private set; } public int TextureMatrixLocation { get; private set; } + public bool TexturesEnableLinearFilter = false; + public bool TexturesEnableEdgeClamping = false; private SDL.Window* window; private SDL.Surface* screen; @@ -174,7 +176,7 @@ namespace Strawberry.SDL2 Debug.Assert(surface != null, "Could not load from path."); Debug.Assert(surface.format.bytesPerPixel == 4, "Surface format incorrect."); - var tex = new Texture(surface.w, surface.h, (uint8*)surface.pixels); + var tex = new Texture(surface.w, surface.h, (uint8*)surface.pixels, TexturesEnableLinearFilter, TexturesEnableEdgeClamping); SDL.FreeSurface(surface); return tex; diff --git a/src/PlatformLayer/Texture.bf b/src/PlatformLayer/Texture.bf index 2906675..4c46d7a 100644 --- a/src/PlatformLayer/Texture.bf +++ b/src/PlatformLayer/Texture.bf @@ -6,14 +6,32 @@ namespace Strawberry public int Width { get; private set; } public int Height { get; private set; } - public this(int width, int height, uint8* pixels) + public this(int width, int height, uint8* pixels, bool indLinear, bool indClamp) { Width = width; Height = height; GL.glGenTextures(1, &Handle); GL.glBindTexture(GL.GL_TEXTURE_2D, Handle); + + // (OPTIONAL) Set linear Mipmaps + if (indLinear) { + GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR); + GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR_MIPMAP_LINEAR); + } + + // (OPTIONAL) Apply edge clamping + if (indClamp) { + GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE); + GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE); + } + GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, width, height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, pixels); + + GL.glGenerateMipmap(GL.GL_TEXTURE_2D); // Unavailable in OpenGL 2.1. + + // Clear state + GL.glBindTexture(GL.GL_TEXTURE_2D, 0); } public ~this() From 0c69b4a31e845b61dbb998578442e4428cf43768 Mon Sep 17 00:00:00 2001 From: Thimo Date: Sun, 8 Nov 2020 14:54:59 +0100 Subject: [PATCH 4/4] Properly implement texture Mipmap generation * Add missing GL_GENERATE_MIPMAP * Add one-time retrieval of glGetIntegerv(GL_MAJOR_VERSION, *) * Opt for Mipmap generation methos based on OGL version major (<3.0 = GL_GENERATE_MIPMAP, >=3.0 = glGenerateMipmap) --- src/PlatformLayer/GL.bf | 1 + src/PlatformLayer/Texture.bf | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/PlatformLayer/GL.bf b/src/PlatformLayer/GL.bf index 66b1afc..5fecf20 100644 --- a/src/PlatformLayer/GL.bf +++ b/src/PlatformLayer/GL.bf @@ -221,6 +221,7 @@ namespace Strawberry public const uint GL_TEXTURE_DEPTH = 0x8071; public const uint GL_TEXTURE_WRAP_R = 0x8072; public const uint GL_MAX_3D_TEXTURE_SIZE = 0x8073; + public const uint GL_GENERATE_MIPMAP = 0x8191; public const uint GL_UNSIGNED_BYTE_2_3_3_REV = 0x8362; public const uint GL_UNSIGNED_SHORT_5_6_5 = 0x8363; public const uint GL_UNSIGNED_SHORT_5_6_5_REV = 0x8364; diff --git a/src/PlatformLayer/Texture.bf b/src/PlatformLayer/Texture.bf index 4c46d7a..fcc4ff5 100644 --- a/src/PlatformLayer/Texture.bf +++ b/src/PlatformLayer/Texture.bf @@ -6,11 +6,16 @@ namespace Strawberry public int Width { get; private set; } public int Height { get; private set; } + private static int32 OGLMajorV = -1; + public this(int width, int height, uint8* pixels, bool indLinear, bool indClamp) { Width = width; Height = height; + if (OGLMajorV == -1) + GL.glGetIntegerv(GL.GL_MAJOR_VERSION, &OGLMajorV); + GL.glGenTextures(1, &Handle); GL.glBindTexture(GL.GL_TEXTURE_2D, Handle); @@ -26,9 +31,13 @@ namespace Strawberry GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE); } + if (OGLMajorV < 3) // OpenGL > 1.4 && < 3.0 + GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_GENERATE_MIPMAP, GL.GL_TRUE); + GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, width, height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, pixels); - GL.glGenerateMipmap(GL.GL_TEXTURE_2D); // Unavailable in OpenGL 2.1. + if (OGLMajorV >= 3) // OpenGL >= 3.0 + GL.glGenerateMipmap(GL.GL_TEXTURE_2D); // Clear state GL.glBindTexture(GL.GL_TEXTURE_2D, 0);