From 00e67b841996aa45ec3b8824549bd2ad30a1573f Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 14 Jan 2024 00:40:50 +0900 Subject: [PATCH] internal/graphicsdriver/playstation5: improve error handlings Updates #2799 --- .../playstation5/graphics_playstation5.cpp | 16 +++---- .../playstation5/graphics_playstation5.go | 43 +++++++++---------- .../playstation5/graphics_playstation5.h | 13 ++++-- 3 files changed, 37 insertions(+), 35 deletions(-) diff --git a/internal/graphicsdriver/playstation5/graphics_playstation5.cpp b/internal/graphicsdriver/playstation5/graphics_playstation5.cpp index 227963e39..2d95896bc 100644 --- a/internal/graphicsdriver/playstation5/graphics_playstation5.cpp +++ b/internal/graphicsdriver/playstation5/graphics_playstation5.cpp @@ -18,23 +18,23 @@ #include "graphics_playstation5.h" -extern "C" int ebitengine_InitializeGraphics(void) { - return 0; +extern "C" ebitengine_Error ebitengine_InitializeGraphics(void) { + return {}; } -extern "C" int ebitengine_NewImage(int* image, int width, int height) { - return 0; +extern "C" ebitengine_Error ebitengine_NewImage(int* image, int width, int height) { + return {}; } -extern "C" int ebitengine_NewScreenFramebufferImage(int* image, int width, int height) { - return 0; +extern "C" ebitengine_Error ebitengine_NewScreenFramebufferImage(int* image, int width, int height) { + return {}; } extern "C" void ebitengine_DisposeImage(int id) { } -extern "C" int ebitengine_NewShader(int* shader, const char* source) { - return 0; +extern "C" ebitengine_Error ebitengine_NewShader(int* shader, const char* source) { + return {}; } extern "C" void ebitengine_DisposeShader(int id) { diff --git a/internal/graphicsdriver/playstation5/graphics_playstation5.go b/internal/graphicsdriver/playstation5/graphics_playstation5.go index af0d5fa20..fa11ee535 100644 --- a/internal/graphicsdriver/playstation5/graphics_playstation5.go +++ b/internal/graphicsdriver/playstation5/graphics_playstation5.go @@ -28,12 +28,21 @@ import ( ) type playstation5Error struct { - name string - code C.int + name string + code int + message string +} + +func newPlaystation5Error(name string, err C.ebitengine_Error) *playstation5Error { + return &playstation5Error{ + name: name, + code: int(err.code), + message: C.GoString(err.message), + } } func (e *playstation5Error) Error() string { - return fmt.Sprintf("playstation5: error at %s, code: %d", e.name, e.code) + return fmt.Sprintf("playstation5: error at %s, code: %d, message: %s", e.name, e.code, e.message) } type Graphics struct { @@ -44,11 +53,8 @@ func NewGraphics() (*Graphics, error) { } func (g *Graphics) Initialize() error { - if errCode := C.ebitengine_InitializeGraphics(); errCode != 0 { - return &playstation5Error{ - name: "(*playstation5.Graphics).Initialize", - code: errCode, - } + if err := C.ebitengine_InitializeGraphics(); err.code != 0 { + return newPlaystation5Error("(*playstation5.Graphics).Initialize", err) } return nil } @@ -70,11 +76,8 @@ func (g *Graphics) SetVertices(vertices []float32, indices []uint32) error { func (g *Graphics) NewImage(width, height int) (graphicsdriver.Image, error) { var id C.int - if errCode := C.ebitengine_NewImage(&id, C.int(width), C.int(height)); errCode != 0 { - return nil, &playstation5Error{ - name: "(*playstation5.Graphics).NewImage", - code: errCode, - } + if err := C.ebitengine_NewImage(&id, C.int(width), C.int(height)); err.code != 0 { + return nil, newPlaystation5Error("(*playstation5.Graphics).NewImage", err) } return &Image{ id: graphicsdriver.ImageID(id), @@ -83,11 +86,8 @@ func (g *Graphics) NewImage(width, height int) (graphicsdriver.Image, error) { func (g *Graphics) NewScreenFramebufferImage(width, height int) (graphicsdriver.Image, error) { var id C.int - if errCode := C.ebitengine_NewScreenFramebufferImage(&id, C.int(width), C.int(height)); errCode != 0 { - return nil, &playstation5Error{ - name: "(*playstation5.Graphics).NewScreenFramebufferImage", - code: errCode, - } + if err := C.ebitengine_NewScreenFramebufferImage(&id, C.int(width), C.int(height)); err.code != 0 { + return nil, newPlaystation5Error("(*playstation5.Graphics).NewScreenFramebufferImage", err) } return &Image{ id: graphicsdriver.ImageID(id), @@ -108,11 +108,8 @@ func (g *Graphics) MaxImageSize() int { func (g *Graphics) NewShader(program *shaderir.Program) (graphicsdriver.Shader, error) { var id C.int // TODO: Give a source code. - if errCode := C.ebitengine_NewShader(&id, nil); errCode != 0 { - return nil, &playstation5Error{ - name: "(*playstation5.Graphics).NewShader", - code: errCode, - } + if err := C.ebitengine_NewShader(&id, nil); err.code != 0 { + return nil, newPlaystation5Error("(*playstation5.Graphics).NewShader", err) } return &Shader{ id: graphicsdriver.ShaderID(id), diff --git a/internal/graphicsdriver/playstation5/graphics_playstation5.h b/internal/graphicsdriver/playstation5/graphics_playstation5.h index ca9efe916..40f1e8326 100644 --- a/internal/graphicsdriver/playstation5/graphics_playstation5.h +++ b/internal/graphicsdriver/playstation5/graphics_playstation5.h @@ -18,12 +18,17 @@ extern "C" { #endif -int ebitengine_InitializeGraphics(void); -int ebitengine_NewImage(int* image, int width, int height); -int ebitengine_NewScreenFramebufferImage(int* image, int width, int height); +typedef struct ebitengine_Error { + const char* message; + int code; +} ebitengine_Error; + +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); -int ebitengine_NewShader(int* shader, const char* source); +ebitengine_Error ebitengine_NewShader(int* shader, const char* source); void ebitengine_DisposeShader(int id); #ifdef __cplusplus