From d60ceb45cd5e6f01f51ab161cb195c17f595a630 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 29 Mar 2023 19:04:41 +0200 Subject: [PATCH] gg: make create_image() return !Image --- examples/flappylearning/game.v | 32 ++++++++++++++++------------ examples/gg/additive.v | 4 ++-- examples/gg/rectangles.v | 2 +- examples/gg/rotating_textured_quad.v | 4 +++- examples/gg/stars.v | 2 +- vlib/gg/image.c.v | 13 ++++++----- 6 files changed, 31 insertions(+), 26 deletions(-) diff --git a/examples/flappylearning/game.v b/examples/flappylearning/game.v index 97c57f219e..76b4576f60 100644 --- a/examples/flappylearning/game.v +++ b/examples/flappylearning/game.v @@ -190,7 +190,7 @@ fn main() { frame_fn: frame event_fn: on_event user_data: app - init_fn: init_images + init_fn: app.init_images_wrapper font_path: font_path ) app.nv = neuroevolution.Generations{ @@ -209,21 +209,25 @@ fn (mut app App) run() { } } -fn init_images(mut app App) { +fn (mut app App) init_images_wrapper() { + app.init_images() or { panic(err) } +} + +fn (mut app App) init_images() ! { $if android { - background := os.read_apk_asset('img/background.png') or { panic(err) } - app.background = app.gg.create_image_from_byte_array(background) - bird := os.read_apk_asset('img/bird.png') or { panic(err) } - app.bird = app.gg.create_image_from_byte_array(bird) - pipetop := os.read_apk_asset('img/pipetop.png') or { panic(err) } - app.pipetop = app.gg.create_image_from_byte_array(pipetop) - pipebottom := os.read_apk_asset('img/pipebottom.png') or { panic(err) } - app.pipebottom = app.gg.create_image_from_byte_array(pipebottom) + background := os.read_apk_asset('img/background.png')! + app.background = app.gg.create_image_from_byte_array(background)! + bird := os.read_apk_asset('img/bird.png')! + app.bird = app.gg.create_image_from_byte_array(bird)! + pipetop := os.read_apk_asset('img/pipetop.png')! + app.pipetop = app.gg.create_image_from_byte_array(pipetop)! + pipebottom := os.read_apk_asset('img/pipebottom.png')! + app.pipebottom = app.gg.create_image_from_byte_array(pipebottom)! } $else { - app.background = app.gg.create_image(os.resource_abs_path('assets/img/background.png')) - app.bird = app.gg.create_image(os.resource_abs_path('assets/img/bird.png')) - app.pipetop = app.gg.create_image(os.resource_abs_path('assets/img/pipetop.png')) - app.pipebottom = app.gg.create_image(os.resource_abs_path('assets/img/pipebottom.png')) + app.background = app.gg.create_image(os.resource_abs_path('assets/img/background.png'))! + app.bird = app.gg.create_image(os.resource_abs_path('assets/img/bird.png'))! + app.pipetop = app.gg.create_image(os.resource_abs_path('assets/img/pipetop.png'))! + app.pipebottom = app.gg.create_image(os.resource_abs_path('assets/img/pipebottom.png'))! } } diff --git a/examples/gg/additive.v b/examples/gg/additive.v index edae97635c..fb56d956e1 100644 --- a/examples/gg/additive.v +++ b/examples/gg/additive.v @@ -12,9 +12,9 @@ pub mut: image gg.Image } -pub fn (mut window Window) init(_ voidptr) { +pub fn (mut window Window) init() { logo_path := os.resource_abs_path(os.join_path('..', 'assets', 'logo.png')) - window.image = window.ctx.create_image(logo_path) + window.image = window.ctx.create_image(logo_path) or { panic(err) } } pub fn (mut window Window) draw(_ voidptr) { diff --git a/examples/gg/rectangles.v b/examples/gg/rectangles.v index 0baaee23cb..1a99efb879 100644 --- a/examples/gg/rectangles.v +++ b/examples/gg/rectangles.v @@ -30,7 +30,7 @@ fn main() { init_fn: init_images ) mut logo_path := os.resource_abs_path(os.join_path('..', 'assets', 'logo.png')) - app.image = app.gg.create_image(logo_path).id + app.image = app.gg.create_image(logo_path)!.id app.gg.run() } diff --git a/examples/gg/rotating_textured_quad.v b/examples/gg/rotating_textured_quad.v index 20c150e564..ba12834565 100644 --- a/examples/gg/rotating_textured_quad.v +++ b/examples/gg/rotating_textured_quad.v @@ -10,7 +10,9 @@ pub mut: } pub fn (mut window Window) init() { - window.img = window.ctx.create_image(os.resource_abs_path('../assets/logo.png')) + window.img = window.ctx.create_image(os.resource_abs_path('../assets/logo.png')) or { + panic(err) + } } pub fn (mut window Window) draw() { diff --git a/examples/gg/stars.v b/examples/gg/stars.v index cde572834b..ea4f593b8a 100644 --- a/examples/gg/stars.v +++ b/examples/gg/stars.v @@ -82,7 +82,7 @@ fn main() { fn init_images(mut app App) { mut logo_path := os.resource_abs_path(os.join_path('..', 'assets', 'logo.png')) - app.image = app.gg.create_image(logo_path) + app.image = app.gg.create_image(logo_path) or { panic(err) } } fn frame(mut app App) { diff --git a/vlib/gg/image.c.v b/vlib/gg/image.c.v index f8c72dfd03..504bd8fddc 100644 --- a/vlib/gg/image.c.v +++ b/vlib/gg/image.c.v @@ -25,11 +25,10 @@ pub mut: } // create_image creates an `Image` from `file`. -// TODO return !Image -pub fn (ctx &Context) create_image(file string) Image { +pub fn (ctx &Context) create_image(file string) !Image { // println('\ncreate_image("$file")') if !os.exists(file) { - return Image{} + return error('image file "${file}" not found') } $if macos { if ctx.native_rendering { @@ -47,7 +46,7 @@ pub fn (ctx &Context) create_image(file string) Image { if !gfx.is_valid() { // Sokol is not initialized yet, add stbi object to a queue/cache // ctx.image_queue << file - stb_img := stbi.load(file) or { return Image{} } + stb_img := stbi.load(file)! img := Image{ width: stb_img.width height: stb_img.height @@ -220,8 +219,8 @@ fn create_image(file string) Image { // memory buffer `buf` of size `bufsize`. // // See also: create_image_from_byte_array -pub fn (mut ctx Context) create_image_from_memory(buf &u8, bufsize int) Image { - stb_img := stbi.load_from_memory(buf, bufsize) or { return Image{} } +pub fn (mut ctx Context) create_image_from_memory(buf &u8, bufsize int) !Image { + stb_img := stbi.load_from_memory(buf, bufsize)! mut img := Image{ width: stb_img.width height: stb_img.height @@ -239,7 +238,7 @@ pub fn (mut ctx Context) create_image_from_memory(buf &u8, bufsize int) Image { // byte array `b`. // // See also: create_image_from_memory -pub fn (mut ctx Context) create_image_from_byte_array(b []u8) Image { +pub fn (mut ctx Context) create_image_from_byte_array(b []u8) !Image { return ctx.create_image_from_memory(b.data, b.len) }