gg: image: return an optional instead of panic(); darwin: fix nsstring()

pull/6195/head
Alexander Medvednikov 2020-08-22 17:09:22 +02:00
parent 0bdb096bfa
commit 9e12095111
9 changed files with 101 additions and 66 deletions

View File

@ -174,6 +174,8 @@ FONS_DEF void fonsDrawDebug(FONScontext* s, float x, float y);
#undef FONS_USE_FREETYPE #undef FONS_USE_FREETYPE
//#define FONS_USE_FREETYPE
#ifdef FONS_USE_FREETYPE #ifdef FONS_USE_FREETYPE
#include <ft2build.h> #include <ft2build.h>

View File

@ -2978,7 +2978,7 @@ static void stbtt__fill_active_edges_new(float *scanline, float *scanline_fill,
float x_top, x_bottom; float x_top, x_bottom;
float sy0,sy1; float sy0,sy1;
float dy = e->fdy; float dy = e->fdy;
STBTT_assert(e->sy <= y_bottom && e->ey >= y_top); // STBTT_assert(e->sy <= y_bottom && e->ey >= y_top);
// compute endpoints of line segment clipped to this scanline (if the // compute endpoints of line segment clipped to this scanline (if the
// line segment starts on this scanline. x0 is the intersection of the // line segment starts on this scanline. x0 is the intersection of the

View File

@ -113,11 +113,27 @@ pub fn print(s string) {
const ( const (
new_line_character = '\n' new_line_character = '\n'
) )
//#include "@VROOT/vlib/darwin/darwin.m"
//fn C.nsstring2(s string) voidptr
//fn C.NSLog(x voidptr)
#include <asl.h>
fn C.asl_log(voidptr, voidptr, int, charptr)
pub fn println(s string) { pub fn println(s string) {
$if windows { $if windows {
print(s) print(s)
print(new_line_character) print(new_line_character)
} $else { } $else {
// For debugging .app applications (no way to read stdout) so that it's printed to macOS Console
/*
$if macos {
C.asl_log(0, 0, C.ASL_LEVEL_ERR, s.str)
}
*/
// TODO: a syscall sys_write on linux works, except for the v repl. // TODO: a syscall sys_write on linux works, except for the v repl.
// Probably it is a stdio buffering issue. Needs more testing... // Probably it is a stdio buffering issue. Needs more testing...
// $if linux { // $if linux {

View File

@ -0,0 +1,9 @@
///void NSLog(id x);
#include <Cocoa/Cocoa.h>
NSString* nsstring2(string s) {
return [ [ NSString alloc ] initWithBytesNoCopy:s.str length:s.len
encoding:NSUTF8StringEncoding freeWhenDone: false];
}

View File

@ -8,13 +8,18 @@ module darwin
struct C.NSString { } struct C.NSString { }
#include "@VROOT/vlib/darwin/darwin.m"
fn C.nsstring2(s string) voidptr
// macOS and iOS helpers // macOS and iOS helpers
//pub fn nsstring(s string) *C.NSString { //pub fn nsstring(s string) *C.NSString {
pub fn nsstring(s string) voidptr { pub fn nsstring(s string) voidptr {
return C.nsstring2(s)
// println('ns $s len=$s.len') // println('ns $s len=$s.len')
# return [ [ NSString alloc ] initWithBytesNoCopy:s.str length:s.len //# return [ [ NSString alloc ] initWithBytesNoCopy:s.str length:s.len
# encoding:NSUTF8StringEncoding freeWhenDone: false]; //# encoding:NSUTF8StringEncoding freeWhenDone: false];
return 0 //return 0
//ns := C.alloc_NSString() //ns := C.alloc_NSString()
//return ns.initWithBytesNoCopy(s.str, length: s.len, //return ns.initWithBytesNoCopy(s.str, length: s.len,

View File

@ -26,10 +26,12 @@ pub mut:
fn C.sg_isvalid() bool fn C.sg_isvalid() bool
// TODO return ?Image
pub fn (mut ctx Context) create_image(file string) Image { pub fn (mut ctx Context) create_image(file string) Image {
if !C.sg_isvalid() { if !C.sg_isvalid() {
// Sokol is not initialized yet, add stbi object to a queue/cache
//ctx.image_queue << file //ctx.image_queue << file
stb_img := stbi.load(file) stb_img := stbi.load(file) or { return Image{} }
img := Image{ img := Image{
width: stb_img.width width: stb_img.width
height: stb_img.height height: stb_img.height
@ -55,7 +57,7 @@ fn create_image(file string) Image {
println('gg.create_image(): file not found: $file') println('gg.create_image(): file not found: $file')
return Image{} // none return Image{} // none
} }
stb_img := stbi.load(file) stb_img := stbi.load(file) or { return Image{} }
mut img := Image{ mut img := Image{
width: stb_img.width width: stb_img.width
height: stb_img.height height: stb_img.height
@ -70,7 +72,7 @@ fn create_image(file string) Image {
} }
pub fn create_image_from_memory(buf byteptr, bufsize int) Image { pub fn create_image_from_memory(buf byteptr, bufsize int) Image {
stb_img := stbi.load_from_memory(buf, bufsize) stb_img := stbi.load_from_memory(buf, bufsize) or { return Image{} }
mut img := Image{ mut img := Image{
width: stb_img.width width: stb_img.width
height: stb_img.height height: stb_img.height

View File

@ -6,10 +6,10 @@ pub const (
used_import = 1 + fontstash.used_import used_import = 1 + fontstash.used_import
) )
/*
#flag windows -I @VROOT/thirdparty/freetype/include #flag windows -I @VROOT/thirdparty/freetype/include
#flag windows -L @VROOT/thirdparty/freetype/win64 #flag windows -L @VROOT/thirdparty/freetype/win64
/*
#flag linux -I/usr/include/freetype2 #flag linux -I/usr/include/freetype2
#flag darwin -I/usr/local/include/freetype2 #flag darwin -I/usr/local/include/freetype2
// MacPorts // MacPorts

View File

@ -28,7 +28,7 @@ fn init() {
set_flip_vertically_on_load(false) set_flip_vertically_on_load(false)
} }
pub fn load(path string) Image { pub fn load(path string) ?Image {
ext := path.all_after_last('.') ext := path.all_after_last('.')
mut res := Image { mut res := Image {
ok: true ok: true
@ -38,12 +38,12 @@ pub fn load(path string) Image {
flag := if ext == 'png' { C.STBI_rgb_alpha } else { 0 } flag := if ext == 'png' { C.STBI_rgb_alpha } else { 0 }
res.data = C.stbi_load(path.str, &res.width, &res.height, &res.nr_channels, flag) res.data = C.stbi_load(path.str, &res.width, &res.height, &res.nr_channels, flag)
if isnil(res.data) { if isnil(res.data) {
panic('stbi image failed to load from "$path"') return error('stbi image failed to load from "$path"')
} }
return res return res
} }
pub fn load_from_memory(buf byteptr, bufsize int) Image { pub fn load_from_memory(buf byteptr, bufsize int) ?Image {
mut res := Image { mut res := Image {
ok: true ok: true
data: 0 data: 0
@ -51,7 +51,7 @@ pub fn load_from_memory(buf byteptr, bufsize int) Image {
flag := C.STBI_rgb_alpha flag := C.STBI_rgb_alpha
res.data = C.stbi_load_from_memory(buf, bufsize, &res.width, &res.height, &res.nr_channels, flag) res.data = C.stbi_load_from_memory(buf, bufsize, &res.width, &res.height, &res.nr_channels, flag)
if isnil(res.data) { if isnil(res.data) {
panic('stbi image failed to load from memory') return error('stbi image failed to load from memory')
} }
return res return res
} }

View File

@ -274,6 +274,7 @@ fn new_tcp_socket() ?TcpSocket {
handle: sockfd handle: sockfd
} }
s.set_option_bool(.reuse_addr, true)? s.set_option_bool(.reuse_addr, true)?
//s.set_option_int(.reuse_addr, 1)?
$if windows { $if windows {
t := true t := true
socket_error(C.ioctlsocket(sockfd, fionbio, &t))? socket_error(C.ioctlsocket(sockfd, fionbio, &t))?