gg: image: return an optional instead of panic(); darwin: fix nsstring()
parent
0bdb096bfa
commit
9e12095111
|
@ -174,6 +174,8 @@ FONS_DEF void fonsDrawDebug(FONScontext* s, float x, float y);
|
|||
|
||||
#undef FONS_USE_FREETYPE
|
||||
|
||||
//#define FONS_USE_FREETYPE
|
||||
|
||||
#ifdef FONS_USE_FREETYPE
|
||||
|
||||
#include <ft2build.h>
|
||||
|
|
|
@ -2978,7 +2978,7 @@ static void stbtt__fill_active_edges_new(float *scanline, float *scanline_fill,
|
|||
float x_top, x_bottom;
|
||||
float sy0,sy1;
|
||||
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
|
||||
// line segment starts on this scanline. x0 is the intersection of the
|
||||
|
|
|
@ -113,11 +113,27 @@ pub fn print(s string) {
|
|||
const (
|
||||
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) {
|
||||
$if windows {
|
||||
print(s)
|
||||
print(new_line_character)
|
||||
} $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.
|
||||
// Probably it is a stdio buffering issue. Needs more testing...
|
||||
// $if linux {
|
||||
|
|
|
@ -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];
|
||||
}
|
|
@ -8,13 +8,18 @@ module darwin
|
|||
|
||||
struct C.NSString { }
|
||||
|
||||
#include "@VROOT/vlib/darwin/darwin.m"
|
||||
|
||||
fn C.nsstring2(s string) voidptr
|
||||
|
||||
// macOS and iOS helpers
|
||||
//pub fn nsstring(s string) *C.NSString {
|
||||
pub fn nsstring(s string) voidptr {
|
||||
return C.nsstring2(s)
|
||||
// println('ns $s len=$s.len')
|
||||
# return [ [ NSString alloc ] initWithBytesNoCopy:s.str length:s.len
|
||||
# encoding:NSUTF8StringEncoding freeWhenDone: false];
|
||||
return 0
|
||||
//# return [ [ NSString alloc ] initWithBytesNoCopy:s.str length:s.len
|
||||
//# encoding:NSUTF8StringEncoding freeWhenDone: false];
|
||||
//return 0
|
||||
|
||||
//ns := C.alloc_NSString()
|
||||
//return ns.initWithBytesNoCopy(s.str, length: s.len,
|
||||
|
|
|
@ -26,10 +26,12 @@ pub mut:
|
|||
|
||||
fn C.sg_isvalid() bool
|
||||
|
||||
// TODO return ?Image
|
||||
pub fn (mut ctx Context) create_image(file string) Image {
|
||||
if !C.sg_isvalid() {
|
||||
// Sokol is not initialized yet, add stbi object to a queue/cache
|
||||
//ctx.image_queue << file
|
||||
stb_img := stbi.load(file)
|
||||
stb_img := stbi.load(file) or { return Image{} }
|
||||
img := Image{
|
||||
width: stb_img.width
|
||||
height: stb_img.height
|
||||
|
@ -55,7 +57,7 @@ fn create_image(file string) Image {
|
|||
println('gg.create_image(): file not found: $file')
|
||||
return Image{} // none
|
||||
}
|
||||
stb_img := stbi.load(file)
|
||||
stb_img := stbi.load(file) or { return Image{} }
|
||||
mut img := Image{
|
||||
width: stb_img.width
|
||||
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 {
|
||||
stb_img := stbi.load_from_memory(buf, bufsize)
|
||||
stb_img := stbi.load_from_memory(buf, bufsize) or { return Image{} }
|
||||
mut img := Image{
|
||||
width: stb_img.width
|
||||
height: stb_img.height
|
||||
|
|
|
@ -6,10 +6,10 @@ pub const (
|
|||
used_import = 1 + fontstash.used_import
|
||||
)
|
||||
|
||||
/*
|
||||
#flag windows -I @VROOT/thirdparty/freetype/include
|
||||
#flag windows -L @VROOT/thirdparty/freetype/win64
|
||||
|
||||
/*
|
||||
#flag linux -I/usr/include/freetype2
|
||||
#flag darwin -I/usr/local/include/freetype2
|
||||
// MacPorts
|
||||
|
|
|
@ -28,7 +28,7 @@ fn init() {
|
|||
set_flip_vertically_on_load(false)
|
||||
}
|
||||
|
||||
pub fn load(path string) Image {
|
||||
pub fn load(path string) ?Image {
|
||||
ext := path.all_after_last('.')
|
||||
mut res := Image {
|
||||
ok: true
|
||||
|
@ -38,12 +38,12 @@ pub fn load(path string) Image {
|
|||
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)
|
||||
if isnil(res.data) {
|
||||
panic('stbi image failed to load from "$path"')
|
||||
return error('stbi image failed to load from "$path"')
|
||||
}
|
||||
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 {
|
||||
ok: true
|
||||
data: 0
|
||||
|
@ -51,7 +51,7 @@ pub fn load_from_memory(buf byteptr, bufsize int) Image {
|
|||
flag := C.STBI_rgb_alpha
|
||||
res.data = C.stbi_load_from_memory(buf, bufsize, &res.width, &res.height, &res.nr_channels, flag)
|
||||
if isnil(res.data) {
|
||||
panic('stbi image failed to load from memory')
|
||||
return error('stbi image failed to load from memory')
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
|
|
@ -274,6 +274,7 @@ fn new_tcp_socket() ?TcpSocket {
|
|||
handle: sockfd
|
||||
}
|
||||
s.set_option_bool(.reuse_addr, true)?
|
||||
//s.set_option_int(.reuse_addr, 1)?
|
||||
$if windows {
|
||||
t := true
|
||||
socket_error(C.ioctlsocket(sockfd, fionbio, &t))?
|
||||
|
|
Loading…
Reference in New Issue