Skip to content

Commit d60ceb4

Browse files
committed
gg: make create_image() return !Image
1 parent 75deb66 commit d60ceb4

File tree

6 files changed

+31
-26
lines changed

6 files changed

+31
-26
lines changed

examples/flappylearning/game.v

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ fn main() {
190190
frame_fn: frame
191191
event_fn: on_event
192192
user_data: app
193-
init_fn: init_images
193+
init_fn: app.init_images_wrapper
194194
font_path: font_path
195195
)
196196
app.nv = neuroevolution.Generations{
@@ -209,21 +209,25 @@ fn (mut app App) run() {
209209
}
210210
}
211211

212-
fn init_images(mut app App) {
212+
fn (mut app App) init_images_wrapper() {
213+
app.init_images() or { panic(err) }
214+
}
215+
216+
fn (mut app App) init_images() ! {
213217
$if android {
214-
background := os.read_apk_asset('img/background.png') or { panic(err) }
215-
app.background = app.gg.create_image_from_byte_array(background)
216-
bird := os.read_apk_asset('img/bird.png') or { panic(err) }
217-
app.bird = app.gg.create_image_from_byte_array(bird)
218-
pipetop := os.read_apk_asset('img/pipetop.png') or { panic(err) }
219-
app.pipetop = app.gg.create_image_from_byte_array(pipetop)
220-
pipebottom := os.read_apk_asset('img/pipebottom.png') or { panic(err) }
221-
app.pipebottom = app.gg.create_image_from_byte_array(pipebottom)
218+
background := os.read_apk_asset('img/background.png')!
219+
app.background = app.gg.create_image_from_byte_array(background)!
220+
bird := os.read_apk_asset('img/bird.png')!
221+
app.bird = app.gg.create_image_from_byte_array(bird)!
222+
pipetop := os.read_apk_asset('img/pipetop.png')!
223+
app.pipetop = app.gg.create_image_from_byte_array(pipetop)!
224+
pipebottom := os.read_apk_asset('img/pipebottom.png')!
225+
app.pipebottom = app.gg.create_image_from_byte_array(pipebottom)!
222226
} $else {
223-
app.background = app.gg.create_image(os.resource_abs_path('assets/img/background.png'))
224-
app.bird = app.gg.create_image(os.resource_abs_path('assets/img/bird.png'))
225-
app.pipetop = app.gg.create_image(os.resource_abs_path('assets/img/pipetop.png'))
226-
app.pipebottom = app.gg.create_image(os.resource_abs_path('assets/img/pipebottom.png'))
227+
app.background = app.gg.create_image(os.resource_abs_path('assets/img/background.png'))!
228+
app.bird = app.gg.create_image(os.resource_abs_path('assets/img/bird.png'))!
229+
app.pipetop = app.gg.create_image(os.resource_abs_path('assets/img/pipetop.png'))!
230+
app.pipebottom = app.gg.create_image(os.resource_abs_path('assets/img/pipebottom.png'))!
227231
}
228232
}
229233

examples/gg/additive.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ pub mut:
1212
image gg.Image
1313
}
1414

15-
pub fn (mut window Window) init(_ voidptr) {
15+
pub fn (mut window Window) init() {
1616
logo_path := os.resource_abs_path(os.join_path('..', 'assets', 'logo.png'))
17-
window.image = window.ctx.create_image(logo_path)
17+
window.image = window.ctx.create_image(logo_path) or { panic(err) }
1818
}
1919

2020
pub fn (mut window Window) draw(_ voidptr) {

examples/gg/rectangles.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn main() {
3030
init_fn: init_images
3131
)
3232
mut logo_path := os.resource_abs_path(os.join_path('..', 'assets', 'logo.png'))
33-
app.image = app.gg.create_image(logo_path).id
33+
app.image = app.gg.create_image(logo_path)!.id
3434
app.gg.run()
3535
}
3636

examples/gg/rotating_textured_quad.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ pub mut:
1010
}
1111

1212
pub fn (mut window Window) init() {
13-
window.img = window.ctx.create_image(os.resource_abs_path('../assets/logo.png'))
13+
window.img = window.ctx.create_image(os.resource_abs_path('../assets/logo.png')) or {
14+
panic(err)
15+
}
1416
}
1517

1618
pub fn (mut window Window) draw() {

examples/gg/stars.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn main() {
8282

8383
fn init_images(mut app App) {
8484
mut logo_path := os.resource_abs_path(os.join_path('..', 'assets', 'logo.png'))
85-
app.image = app.gg.create_image(logo_path)
85+
app.image = app.gg.create_image(logo_path) or { panic(err) }
8686
}
8787

8888
fn frame(mut app App) {

vlib/gg/image.c.v

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ pub mut:
2525
}
2626

2727
// create_image creates an `Image` from `file`.
28-
// TODO return !Image
29-
pub fn (ctx &Context) create_image(file string) Image {
28+
pub fn (ctx &Context) create_image(file string) !Image {
3029
// println('\ncreate_image("$file")')
3130
if !os.exists(file) {
32-
return Image{}
31+
return error('image file "${file}" not found')
3332
}
3433
$if macos {
3534
if ctx.native_rendering {
@@ -47,7 +46,7 @@ pub fn (ctx &Context) create_image(file string) Image {
4746
if !gfx.is_valid() {
4847
// Sokol is not initialized yet, add stbi object to a queue/cache
4948
// ctx.image_queue << file
50-
stb_img := stbi.load(file) or { return Image{} }
49+
stb_img := stbi.load(file)!
5150
img := Image{
5251
width: stb_img.width
5352
height: stb_img.height
@@ -220,8 +219,8 @@ fn create_image(file string) Image {
220219
// memory buffer `buf` of size `bufsize`.
221220
//
222221
// See also: create_image_from_byte_array
223-
pub fn (mut ctx Context) create_image_from_memory(buf &u8, bufsize int) Image {
224-
stb_img := stbi.load_from_memory(buf, bufsize) or { return Image{} }
222+
pub fn (mut ctx Context) create_image_from_memory(buf &u8, bufsize int) !Image {
223+
stb_img := stbi.load_from_memory(buf, bufsize)!
225224
mut img := Image{
226225
width: stb_img.width
227226
height: stb_img.height
@@ -239,7 +238,7 @@ pub fn (mut ctx Context) create_image_from_memory(buf &u8, bufsize int) Image {
239238
// byte array `b`.
240239
//
241240
// See also: create_image_from_memory
242-
pub fn (mut ctx Context) create_image_from_byte_array(b []u8) Image {
241+
pub fn (mut ctx Context) create_image_from_byte_array(b []u8) !Image {
243242
return ctx.create_image_from_memory(b.data, b.len)
244243
}
245244

0 commit comments

Comments
 (0)