diff --git a/internal/graphicsdriver/playstation5/graphics_playstation5.cpp b/internal/graphicsdriver/playstation5/graphics_playstation5.cpp index 9eb01959e..45bcb8511 100644 --- a/internal/graphicsdriver/playstation5/graphics_playstation5.cpp +++ b/internal/graphicsdriver/playstation5/graphics_playstation5.cpp @@ -25,6 +25,17 @@ extern "C" ebitengine_Error ebitengine_NewImage(int *image, int width, return {}; } +extern "C" ebitengine_Error +ebitengine_ReadPixels(int image, ebitengine_PixelsArgs *args, int arg_count) { + return {}; +} + +extern "C" ebitengine_Error +ebitengine_WritePixels(int image, const ebitengine_PixelsArgs *args, + int arg_count) { + return {}; +} + extern "C" ebitengine_Error ebitengine_NewScreenFramebufferImage(int *image, int width, int height) { return {}; diff --git a/internal/graphicsdriver/playstation5/graphics_playstation5.go b/internal/graphicsdriver/playstation5/graphics_playstation5.go index 0756684fa..56d3c1669 100644 --- a/internal/graphicsdriver/playstation5/graphics_playstation5.go +++ b/internal/graphicsdriver/playstation5/graphics_playstation5.go @@ -22,6 +22,7 @@ import "C" import ( "fmt" "runtime" + "unsafe" "github.com/hajimehoshi/ebiten/v2/internal/graphics" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" @@ -80,7 +81,7 @@ func (g *Graphics) SetTransparent(transparent bool) { func (g *Graphics) SetVertices(vertices []float32, indices []uint32) error { defer runtime.KeepAlive(vertices) defer runtime.KeepAlive(indices) - C.ebitengine_SetVertices((*C.float)(&vertices[0]), C.int(len(vertices)), (*C.uint32_t)(&indices[0]), C.int(len(indices))) + C.ebitengine_SetVertices((*C.float)(unsafe.SliceData(vertices)), C.int(len(vertices)), (*C.uint32_t)(unsafe.SliceData(indices)), C.int(len(indices))) return nil } @@ -160,7 +161,7 @@ func (g *Graphics) DrawTriangles(dst graphicsdriver.ImageID, srcs [graphics.Shad cUniforms[i] = C.uint32_t(u) } - 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) { + if err := C.ebitengine_DrawTriangles(C.int(dst), unsafe.SliceData(cSrcs), C.int(len(cSrcs)), C.int(shader), unsafe.SliceData(cDstRegions), C.int(len(cDstRegions)), C.int(indexOffset), cBlend, unsafe.SliceData(cUniforms), C.int(len(cUniforms)), C.int(fillRule)); !C.ebitengine_IsErrorNil(&err) { return newPlaystation5Error("(*playstation5.Graphics).DrawTriangles", err) } return nil @@ -179,12 +180,40 @@ func (i *Image) Dispose() { } func (i *Image) ReadPixels(args []graphicsdriver.PixelsArgs) error { - // TODO: Implement this + cArgs := make([]C.ebitengine_PixelsArgs, 0, len(args)) + for _, a := range args { + cArgs = append(cArgs, C.ebitengine_PixelsArgs{ + min_x: C.int(a.Region.Min.X), + min_y: C.int(a.Region.Min.Y), + max_x: C.int(a.Region.Max.X), + max_y: C.int(a.Region.Max.Y), + pixels: (*C.uint8_t)(unsafe.Pointer(unsafe.SliceData(a.Pixels))), + }) + } + defer runtime.KeepAlive(cArgs) + + if err := C.ebitengine_ReadPixels(C.int(i.id), unsafe.SliceData(cArgs), C.int(len(cArgs))); !C.ebitengine_IsErrorNil(&err) { + return newPlaystation5Error("(*playstation5.Image).ReadPixels", err) + } return nil } func (i *Image) WritePixels(args []graphicsdriver.PixelsArgs) error { - // TODO: Implement this + cArgs := make([]C.ebitengine_PixelsArgs, 0, len(args)) + for _, a := range args { + cArgs = append(cArgs, C.ebitengine_PixelsArgs{ + min_x: C.int(a.Region.Min.X), + min_y: C.int(a.Region.Min.Y), + max_x: C.int(a.Region.Max.X), + max_y: C.int(a.Region.Max.Y), + pixels: (*C.uint8_t)(unsafe.Pointer(unsafe.SliceData(a.Pixels))), + }) + } + defer runtime.KeepAlive(cArgs) + + if err := C.ebitengine_WritePixels(C.int(i.id), unsafe.SliceData(cArgs), C.int(len(cArgs))); !C.ebitengine_IsErrorNil(&err) { + return newPlaystation5Error("(*playstation5.Image).WritePixels", err) + } return nil } diff --git a/internal/graphicsdriver/playstation5/graphics_playstation5.h b/internal/graphicsdriver/playstation5/graphics_playstation5.h index d17aa82c8..88709e491 100644 --- a/internal/graphicsdriver/playstation5/graphics_playstation5.h +++ b/internal/graphicsdriver/playstation5/graphics_playstation5.h @@ -34,6 +34,14 @@ static bool ebitengine_IsErrorNil(ebitengine_Error *err) { return err->message == NULL && err->code == 0; } +typedef struct ebitengine_PixelsArgs { + uint8_t *pixels; + int min_x; + int min_y; + int max_x; + int max_y; +} ebitengine_PixelsArgs; + typedef struct ebitengine_DstRegion { int min_x; int min_y; @@ -55,6 +63,11 @@ 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); +ebitengine_Error ebitengine_ReadPixels(int image, ebitengine_PixelsArgs *args, + int arg_count); +ebitengine_Error ebitengine_WritePixels(int image, + const ebitengine_PixelsArgs *args, + int arg_count); void ebitengine_DisposeImage(int id); void ebitengine_Begin();