v icon indicating copy to clipboard operation
v copied to clipboard

background image doesn't work

Open nblfikr opened this issue 2 years ago • 16 comments

Describe the bug

I following flappylearning example to create a background image but, it doesn't work.

The below image obtain from depositphotos.com, just for my sample only background

Expected Behavior

The background image is displayed

Current Behavior

sg_image_desc.width must be > 0
sg_image_desc.height must be > 0
sg_image_data: no data (.ptr and/or .size is zero)
sg_image_data: data size doesn't match expected surface size
^^^^  SOKOL-GFX VALIDATION FAILED, TERMINATING ^^^^
Unhandled Exception 0x80000003
C:/Users/asust/AppData/Local/Temp/v_0/game.10954635248484273679.tmp.c:10747: at print_backtrace_skipping_top_frames_tcc: Backtrace
C:/Users/asust/AppData/Local/Temp/v_0/game.10954635248484273679.tmp.c:10714: by print_backtrace_skipping_top_frames
C:/Users/asust/AppData/Local/Temp/v_0/game.10954635248484273679.tmp.c:10772: by unhandled_exception_handler
7fff9b887a2a : by ???
7fff9aa1ee11 : at ???: RUNTIME ERROR: breakpoint/single-step exception:
43206c6175736956 : by ???
Unhandled Exception 0xC0000005
C:/Users/asust/AppData/Local/Temp/v_0/game.10954635248484273679.tmp.c:10747: at print_backtrace_skipping_top_frames_tcc: Backtrace
C:/Users/asust/AppData/Local/Temp/v_0/game.10954635248484273679.tmp.c:10714: by print_backtrace_skipping_top_frames
C:/Users/asust/AppData/Local/Temp/v_0/game.10954635248484273679.tmp.c:10772: by unhandled_exception_handler
7fff9b887a2a : by ???

since I updated the V version to V 0.3.3 6756d28 the error changed to just like this

sg_image_desc.width must be > 0
sg_image_desc.height must be > 0
sg_image_data: no data (.ptr and/or .size is zero)
sg_image_data: data size doesn't match expected surface size
^^^^  SOKOL-GFX VALIDATION FAILED, TERMINATING ^^^^

Reproduction Steps

fn on_frame(mut app App) {
	app.gg.begin()
	app.gg.draw_image(0, 0, 600, 800, app.background)
	app.gg.end()
}

fn on_init(mut app App) {
	path := os.resource_abs_path('assets/img/background.jpg')
	// println(path)
	app.background = app.gg.create_image(path)
}

Possible Solution

No response

Additional Information/Context

There seems to be a problem with my background.jpg. I tried using another image and it worked.

V version

V 0.3.3 6756d28

Environment details (OS name and version, etc.)

V full version: V 0.3.3 d7a418f.6756d28 OS: windows, Microsoft Windows 11 Home Single Language v22621 64-bit

nblfikr avatar Mar 29 '23 14:03 nblfikr

#0 17:48:49 ᛋ master /v/vnew❱convert 228573825-542a3f57-908a-4d6a-86e0-2456a773c900.jpg y.jpg
#0 17:48:57 ᛋ master /v/vnew❱file 228573825-542a3f57-908a-4d6a-86e0-2456a773c900.jpg  y.jpg 
228573825-542a3f57-908a-4d6a-86e0-2456a773c900.jpg: RIFF (little-endian) data, Web/P image, VP8 encoding, 338x600, Scaling: [none]x[none], YUV color, decoders should clamp
y.jpg:                                              JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 338x600, components 3

I think that the problem is that the image has a WEBP format, i.e. it is not actually a JPEG one, and stbi does not currently support loading WEBP images yet.

After a conversion to .jpg, it does work.

spytheman avatar Mar 29 '23 14:03 spytheman

I think that we should change all create_image methods in gg, to return results, so that decoding errors could become normal V errors. i.e. instead of: pub fn (ctx &Context) create_image(file string) Image { it would be: pub fn (ctx &Context) create_image(file string) !Image { and then the callers could have or{} blocks for handling decode failures.

spytheman avatar Mar 29 '23 15:03 spytheman

I second that

larpon avatar Mar 29 '23 15:03 larpon

Sokol simply ending the program instead of handling incorrect image format is not nice...

medvednikov avatar Mar 29 '23 16:03 medvednikov

Yeah, makes sense, let's make it return an !Image.

Other graphics libraries for languages without exceptions do this:

https://github.com/fogleman/gg/blob/master/examples/rotated-image.go#L8

medvednikov avatar Mar 29 '23 16:03 medvednikov

I've just started work on this, fill finish in ~half an hour.

medvednikov avatar Mar 29 '23 16:03 medvednikov

Done: https://github.com/vlang/v/commit/d60ceb45cd5e6f01f51ab161cb195c17f595a630

Still doesn't handle sokol errors of course

medvednikov avatar Mar 29 '23 17:03 medvednikov

Ah looks like Sokol has a way to handle errors: SOKOL_VALIDATE_NON_FATAL

_SOKOL_PRIVATE bool _sg_validate_end(void) {
    if (_sg.validate_error != _SG_VALIDATE_SUCCESS) {
        #if !defined(SOKOL_VALIDATE_NON_FATAL)
            SG_LOG("^^^^  SOKOL-GFX VALIDATION FAILED, TERMINATING ^^^^");
            SOKOL_ASSERT(false);
        #endif
        return false;
    }
    else {
        return true;
    }
}

medvednikov avatar Mar 29 '23 17:03 medvednikov

@nblfikr can you try again?

medvednikov avatar Mar 29 '23 22:03 medvednikov

Do both v up and v update first, of course.

JalonSolov avatar Mar 29 '23 23:03 JalonSolov

i have update my V version and it come with different error

image

nblfikr avatar Mar 30 '23 07:03 nblfikr

Yes this is correct. The error tells you what to do. This function returns an !Image now.

medvednikov avatar Mar 30 '23 09:03 medvednikov

I still don't understand, what ! on!Image means?

I see flappylearning example has been updated, so my code isn't much different

fn (mut app App) init_images_wrapper() {
	app.init_images() or { panic(err) }
}

fn (mut app App) init_images() ! {
	app.background = app.gg.create_image(os.resource_abs_path('assets/img/background.jpg'))!
}

after fixing my code, the error came back again but goes away if I change the image

sg_image_desc.width must be > 0
sg_image_desc.height must be > 0
sg_image_data: no data (.ptr and/or .size is zero)
sg_image_data: data size doesn't match expected surface size
^^^^  SOKOL-GFX VALIDATION FAILED, TERMINATING ^^^^

nblfikr avatar Mar 30 '23 10:03 nblfikr

Looks like it works correctly now.

Can you change app.init_images() or { panic(err) } to app.init_images() or { println('img init error') panic(err) }

Are you getting the new "img init error" message?

medvednikov avatar Mar 30 '23 11:03 medvednikov

@nblfikr The fix was not to make your original background image work - we can't do that since the format is one that Sokol doesn't understand.

The fix was to make it handle the problem better when the format can't be used.

JalonSolov avatar Mar 30 '23 12:03 JalonSolov

@medvednikov Nope.

image

@JalonSolov Ok. It is no problem since I can used another image

nblfikr avatar Mar 30 '23 13:03 nblfikr