Compare commits

...

3 Commits

Author SHA1 Message Date
Hajime Hoshi
89933bf0ab internal/graphicsdriver/playstation5: bug fix: compile error 2024-08-10 21:52:38 +09:00
Hajime Hoshi
332da38565 internal/graphicsdriver/playstation5: update DrawTriangles
A Go pointer in a C struct could cause some troubles.
2024-08-10 21:21:09 +09:00
Hajime Hoshi
fbf40a4455 vector: bug fix: isPointCloseToSegment didn't work when two p0 and p1 are the same
Closes #3061
2024-08-10 17:13:09 +09:00
6 changed files with 154 additions and 49 deletions

View File

@ -32,8 +32,11 @@ ebitengine_NewScreenFramebufferImage(int *image, int width, int height) {
extern "C" void ebitengine_DisposeImage(int id) {}
extern "C" ebitengine_Error
ebitengine_DrawTriangles(ebitengine_DrawTrianglesArgs *args) {
ebitengine_Error
ebitengine_DrawTriangles(int dst, int *srcs, int srcCount, int shader,
ebitengine_DstRegion *dstRegions, int dstRegionCount,
int indexOffset, ebitengine_Blend blend,
uint32_t *uniforms, int uniformCount, int fillRule) {
return {};
}

View File

@ -118,26 +118,24 @@ func (g *Graphics) NewShader(program *shaderir.Program) (graphicsdriver.Shader,
}
func (g *Graphics) DrawTriangles(dst graphicsdriver.ImageID, srcs [graphics.ShaderSrcImageCount]graphicsdriver.ImageID, shader graphicsdriver.ShaderID, dstRegions []graphicsdriver.DstRegion, indexOffset int, blend graphicsdriver.Blend, uniforms []uint32, fillRule graphicsdriver.FillRule) error {
csrcs := make([]C.int, len(srcs))
cSrcs := make([]C.int, len(srcs))
for i, src := range srcs {
csrcs[i] = C.int(src)
cSrcs[i] = C.int(src)
}
defer runtime.KeepAlive(cSrcs)
cDstRegions := make([]C.ebitengine_DstRegion, len(dstRegions))
defer runtime.KeepAlive(cDstRegions)
for i, r := range dstRegions {
cDstRegions[i] = C.ebitengine_DstRegion{
Region: C.ebitengine_Rectangle{
MinX: C.int(r.Region.Min.X),
MinY: C.int(r.Region.Min.Y),
MaxX: C.int(r.Region.Max.X),
MaxY: C.int(r.Region.Max.Y),
},
MinX: C.int(r.Region.Min.X),
MinY: C.int(r.Region.Min.Y),
MaxX: C.int(r.Region.Max.X),
MaxY: C.int(r.Region.Max.Y),
IndexCount: C.int(r.IndexCount),
}
}
cUniforms := make([]C.uint32_t, len(uniforms))
for i, u := range uniforms {
cUniforms[i] = C.uint32_t(u)
}
cBlend := C.ebitengine_Blend{
BlendFactorSourceRGB: C.uint8_t(blend.BlendFactorSourceRGB),
BlendFactorSourceAlpha: C.uint8_t(blend.BlendFactorSourceAlpha),
@ -147,23 +145,15 @@ func (g *Graphics) DrawTriangles(dst graphicsdriver.ImageID, srcs [graphics.Shad
BlendOperationAlpha: C.uint8_t(blend.BlendOperationAlpha),
}
args := C.ebitengine_DrawTrianglesArgs{
Dst: C.int(dst),
Srcs: &csrcs[0],
SrcCount: C.int(len(csrcs)),
Shader: C.int(shader),
DstRegions: &cDstRegions[0],
DstRegionCount: C.int(len(cDstRegions)),
IndexOffset: C.int(indexOffset),
Blend: cBlend,
Uniforms: &cUniforms[0],
UniformCount: C.int(len(cUniforms)),
FillRule: C.int(fillRule),
cUniforms := make([]C.uint32_t, len(uniforms))
defer runtime.KeepAlive(cUniforms)
for i, u := range uniforms {
cUniforms[i] = C.uint32_t(u)
}
if err := C.ebitengine_DrawTriangles(&args); !C.ebitengine_IsErrorNil(&err) {
if err := C.ebitengine_DrawTriangles(C.int(dst), &cSrcs[0], C.int(len(cSrcs)), C.int(shader), &cDstRegions[0], C.int(len(cDstRegions)), C.int(indexOffset), cBlend, &cUniforms[0], C.int(len(cUniforms)), C.int(fillRule)); !C.ebitengine_IsErrorNil(&err) {
return newPlaystation5Error("(*playstation5.Graphics).DrawTriangles", err)
}
runtime.KeepAlive(args)
return nil
}

View File

@ -34,15 +34,11 @@ static bool ebitengine_IsErrorNil(ebitengine_Error *err) {
return err->Message == NULL && err->Code == 0;
}
typedef struct ebitengine_Rectangle {
typedef struct ebitengine_DstRegion {
int MinX;
int MinY;
int MaxX;
int MaxY;
} ebitengine_Rectangle;
typedef struct ebitengine_DstRegion {
ebitengine_Rectangle Region;
int IndexCount;
} ebitengine_DstRegion;
@ -55,26 +51,17 @@ typedef struct ebitengine_Blend {
uint8_t BlendOperationAlpha;
} ebitengine_Blend;
typedef struct ebitengine_DrawTrianglesArgs {
int Dst;
int *Srcs;
int SrcCount;
int Shader;
ebitengine_DstRegion *DstRegions;
int DstRegionCount;
int IndexOffset;
ebitengine_Blend Blend;
uint32_t *Uniforms;
int UniformCount;
int FillRule;
} ebitengine_DrawTrianglesArgs;
ebitengine_Error ebitengine_InitializeGraphics(void);
ebitengine_Error ebitengine_NewImage(int *image, int width, int height);
ebitengine_Error ebitengine_NewScreenFramebufferImage(int *image, int width,
int height);
void ebitengine_DisposeImage(int id);
ebitengine_Error ebitengine_DrawTriangles(ebitengine_DrawTrianglesArgs *args);
ebitengine_Error
ebitengine_DrawTriangles(int dst, int *srcs, int srcCount, int shader,
ebitengine_DstRegion *dstRegions, int dstRegionCount,
int indexOffset, ebitengine_Blend blend,
uint32_t *uniforms, int uniformCount, int fillRule);
ebitengine_Error ebitengine_NewShader(int *shader, const char *source);
void ebitengine_DisposeShader(int id);

32
vector/export_test.go Normal file
View File

@ -0,0 +1,32 @@
// Copyright 2024 The Ebitengine Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package vector
type Point struct {
X, Y float32
}
func IsPointCloseToSegment(p, p0, p1 Point, allow float32) bool {
return isPointCloseToSegment(point{
x: p.X,
y: p.Y,
}, point{
x: p0.X,
y: p0.Y,
}, point{
x: p1.X,
y: p1.Y,
}, allow)
}

View File

@ -232,12 +232,17 @@ func lineForTwoPoints(p0, p1 point) (a, b, c float32) {
}
// isPointCloseToSegment detects the distance between a segment (x0, y0)-(x1, y1) and a point (x, y) is less than allow.
// If p0 and p1 are the same, isPointCloseToSegment returns true when the distance between p0 and p is less than allow.
func isPointCloseToSegment(p, p0, p1 point, allow float32) bool {
if p0 == p1 {
return allow*allow >= (p0.x-p.x)*(p0.x-p.x)+(p0.y-p.y)*(p0.y-p.y)
}
a, b, c := lineForTwoPoints(p0, p1)
// The distance between a line ax+by+c=0 and (x0, y0) is
// |ax0 + by0 + c| / √(a² + b²)
return allow*allow*(a*a+b*b) > (a*p.x+b*p.y+c)*(a*p.x+b*p.y+c)
return allow*allow*(a*a+b*b) >= (a*p.x+b*p.y+c)*(a*p.x+b*p.y+c)
}
// crossingPointForTwoLines returns a crossing point for two lines.

88
vector/path_test.go Normal file
View File

@ -0,0 +1,88 @@
// Copyright 2024 The Ebitengine Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package vector_test
import (
"testing"
"github.com/hajimehoshi/ebiten/v2/vector"
)
func TestIsPointCloseToSegment(t *testing.T) {
testCases := []struct {
p vector.Point
p0 vector.Point
p1 vector.Point
allow float32
want bool
}{
{
p: vector.Point{0.5, 0.5},
p0: vector.Point{0, 0},
p1: vector.Point{1, 0},
allow: 1,
want: true,
},
{
p: vector.Point{0.5, 1.5},
p0: vector.Point{0, 0},
p1: vector.Point{1, 0},
allow: 1,
want: false,
},
{
p: vector.Point{0.5, 0.5},
p0: vector.Point{0, 0},
p1: vector.Point{1, 1},
allow: 0,
want: true,
},
{
p: vector.Point{0, 1},
p0: vector.Point{0, 0},
p1: vector.Point{1, 1},
allow: 0.7,
want: false,
},
{
p: vector.Point{0, 1},
p0: vector.Point{0, 0},
p1: vector.Point{1, 1},
allow: 0.8,
want: true,
},
{
// p0 and p1 are the same.
p: vector.Point{0, 1},
p0: vector.Point{0.5, 0.5},
p1: vector.Point{0.5, 0.5},
allow: 0.7,
want: false,
},
{
// p0 and p1 are the same.
p: vector.Point{0, 1},
p0: vector.Point{0.5, 0.5},
p1: vector.Point{0.5, 0.5},
allow: 0.8,
want: true,
},
}
for _, tc := range testCases {
if got := vector.IsPointCloseToSegment(tc.p, tc.p0, tc.p1, tc.allow); got != tc.want {
t.Errorf("got: %v, want: %v", got, tc.want)
}
}
}