Merge pull request #2 from thibmo/contrib_Fix_Textures

Fix textures
This commit is contained in:
Matt Thorson 2020-11-08 17:04:44 -08:00 committed by GitHub
commit 9ded760c78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 62 additions and 12 deletions

View File

@ -12,7 +12,7 @@ namespace Strawberry.Sample
public this(Point pos) public this(Point pos)
: base(pos) : base(pos)
{ {
Hitbox = Rect(-4, -8, 8, 8); Hitbox = Rect(-4, -8, 16, 16);
Add(tJumpGrace = new Timer()); Add(tJumpGrace = new Timer());
Add(tVarJump = new Timer()); Add(tVarJump = new Timer());
@ -102,8 +102,8 @@ namespace Strawberry.Sample
{ {
base.Draw(); base.Draw();
DrawHitbox(.Green); DrawHitboxOutline(.Green);
//Game.Batcher.Tex(Assets.Textures["test"], X - 4, Y - 8); Game.Batcher.Tex(Assets.Textures["test"], X - 4, Y - 8);
} }
} }
} }

View File

@ -8,6 +8,7 @@ namespace Strawberry.Sample
static public int Main(String[] args) static public int Main(String[] args)
{ {
let sdl = scope SDL2PlatformLayer(); let sdl = scope SDL2PlatformLayer();
sdl.TexturesEnableEdgeClamping = true;
let game = scope SampleGame(sdl); let game = scope SampleGame(sdl);
game.Run(); game.Run();

View File

@ -560,7 +560,7 @@ namespace Strawberry
delete BlendModes; delete BlendModes;
} }
private static readonly Blend[] BlendModes = new Blend[] private static readonly Blend[] BlendModes = new Blend[](
{ {
// 0 - NORMAL // 0 - NORMAL
new (dest, index, src, opacity) => new (dest, index, src, opacity) =>
@ -596,7 +596,7 @@ namespace Strawberry
dest[index + 3] = a; dest[index + 3] = a;
} }
} }
}; });
[Inline] [Inline]
private static int MUL_UN8(int a, int b) private static int MUL_UN8(int a, int b)

View File

@ -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 public int Left
{ {
[Inline] [Inline]
@ -463,6 +477,11 @@ namespace Strawberry
Game.Batcher.Rect(SceneHitbox, color); Game.Batcher.Rect(SceneHitbox, color);
} }
public void DrawHitboxOutline(Color color)
{
Game.Batcher.Rect(SceneHitboxOutline, color);
}
public T SceneAs<T>() where T : Scene public T SceneAs<T>() where T : Scene
{ {
Runtime.Assert(Scene is T, "Scene type mismatch!"); Runtime.Assert(Scene is T, "Scene type mismatch!");

View File

@ -69,10 +69,10 @@ namespace Strawberry
public void Tex(Texture texture, float x, float y) public void Tex(Texture texture, float x, float y)
{ {
PushQuad(.TextureTint, texture, PushQuad(.TextureTint, texture,
.Tex(.(x, y), .(0, 1), Color.White), .Tex(.(x, y), .(0, 0), Color.White),
.Tex(.(x + texture.Width, y), .(1, 1), Color.White), .Tex(.(x + texture.Width, y), .(1, 0), Color.White),
.Tex(.(x + texture.Width, y + texture.Height), .(1, 0), Color.White), .Tex(.(x + texture.Width, y + texture.Height), .(1, 1), Color.White),
.Tex(.(x, y + texture.Height), .(0, 0), Color.White)); .Tex(.(x, y + texture.Height), .(0, 1), Color.White));
} }
} }
} }

View File

@ -221,6 +221,7 @@ namespace Strawberry
public const uint GL_TEXTURE_DEPTH = 0x8071; public const uint GL_TEXTURE_DEPTH = 0x8071;
public const uint GL_TEXTURE_WRAP_R = 0x8072; public const uint GL_TEXTURE_WRAP_R = 0x8072;
public const uint GL_MAX_3D_TEXTURE_SIZE = 0x8073; 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_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 = 0x8363;
public const uint GL_UNSIGNED_SHORT_5_6_5_REV = 0x8364; public const uint GL_UNSIGNED_SHORT_5_6_5_REV = 0x8364;

View File

@ -8,6 +8,8 @@ namespace Strawberry.SDL2
{ {
public int TransformMatrixLocation { get; private set; } public int TransformMatrixLocation { get; private set; }
public int TextureMatrixLocation { get; private set; } public int TextureMatrixLocation { get; private set; }
public bool TexturesEnableLinearFilter = false;
public bool TexturesEnableEdgeClamping = false;
private SDL.Window* window; private SDL.Window* window;
private SDL.Surface* screen; private SDL.Surface* screen;
@ -174,7 +176,7 @@ namespace Strawberry.SDL2
Debug.Assert(surface != null, "Could not load from path."); Debug.Assert(surface != null, "Could not load from path.");
Debug.Assert(surface.format.bytesPerPixel == 4, "Surface format incorrect."); 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); SDL.FreeSurface(surface);
return tex; return tex;

View File

@ -6,14 +6,41 @@ namespace Strawberry
public int Width { get; private set; } public int Width { get; private set; }
public int Height { get; private set; } public int Height { get; private set; }
public this(int width, int height, uint8* pixels) private static int32 OGLMajorV = -1;
public this(int width, int height, uint8* pixels, bool indLinear, bool indClamp)
{ {
Width = width; Width = width;
Height = height; Height = height;
if (OGLMajorV == -1)
GL.glGetIntegerv(GL.GL_MAJOR_VERSION, &OGLMajorV);
GL.glGenTextures(1, &Handle); GL.glGenTextures(1, &Handle);
GL.glBindTexture(GL.GL_TEXTURE_2D, 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);
}
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.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, width, height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, pixels);
if (OGLMajorV >= 3) // OpenGL >= 3.0
GL.glGenerateMipmap(GL.GL_TEXTURE_2D);
// Clear state
GL.glBindTexture(GL.GL_TEXTURE_2D, 0);
} }
public ~this() public ~this()

View File

@ -229,7 +229,7 @@ namespace Strawberry
{ {
let name = scope String(); let name = scope String();
type.GetName(name); 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); Runtime.FatalError(error);
} }