#raylib, c_int - error
1 messages · Page 1 of 1 (latest)
wanted to send it as normal message but discord wouldn't let me, im sorry
build.zig:
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const lib = b.addStaticLibrary(.{
.name = "space-game",
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
b.installArtifact(lib);
const exe = b.addExecutable(.{
.name = "space-game",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const raylib_dep = b.dependency("raylib", .{});
exe.addIncludePath(raylib_dep.path("include"));
exe.addLibraryPath(raylib_dep.path("lib"));
exe.linkSystemLibrary("raylib");
exe.linkLibC();
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const lib_unit_tests = b.addTest(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
const exe_unit_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
test_step.dependOn(&run_exe_unit_tests.step);
}
to turn an f32 into a c_int you have to use @intFromFloat, @as only performs coercions, and a float cant ceorce to an int. so itd be: rl.InitWindow(@intFromFloat(settings.screen_width), @intFromFloat(settings.screen_height), ...)
you could also just parse the screen_width and screen_height as c_int instead of f32
I did, just now and then it is giving me following:
❯ zig build run
run
└─ run space-game
└─ install
└─ install space-game
└─ zig build-exe space-game Debug native 1 errors
/usr/lib/zig/std/json/static.zig:503:25: error: Unable to parse into type '[*c]const u8'
else => @compileError("Unable to parse into type '" ++ @typeName(T) ++ "'"),
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
innerParse__anon_10717: /usr/lib/zig/std/json/static.zig:361:63
parseFromTokenSourceLeaky__anon_10568: /usr/lib/zig/std/json/static.zig:140:33
remaining reference traces hidden; use '-freference-trace' to see all reference traces
error: the following command failed with 1 compilation errors:
/usr/bin/zig build-exe -lraylib -ODebug -I /home/Akitoo/.cache/zig/p/122041e7e8965f8a9ea4f8e9e1259620649e466dd5dbe51fc4fb541dc41b67c205e1/include -L /home/Akitoo/.cache/zig/p/122041e7e8965f8a9ea4f8e9e1259620649e466dd5dbe51fc4fb541dc41b67c205e1/lib -Mroot=/run/media/Akitoo/Windows/CODEBASE-2024/ZIG/space-game/src/main.zig -lc --cache-dir /run/media/Akitoo/Windows/CODEBASE-2024/ZIG/space-game/.zig-cache --global-cache-dir /home/Akitoo/.cache/zig --name space-game --listen=-
Build Summary: 2/7 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run space-game transitive failure
├─ zig build-exe space-game Debug native 1 errors
└─ install transitive failure
└─ install space-game transitive failure
└─ zig build-exe space-game Debug native (reused)
error: the following build command failed with exit code 1:
/run/media/Akitoo/Windows/CODEBASE-2024/ZIG/space-game/.zig-cache/o/7a3e3a504a47140b9dcc08034e1736d4/build /usr/bin/zig /run/media/Akitoo/Windows/CODEBASE-2024/ZIG/space-game /run/media/Akitoo/Windows/CODEBASE-2024/ZIG/space-game/.zig-cache /home/Akitoo/.cache/zig --seed 0xe41c329d -Z2ae6ddab8fe9232d run
you cut off the error, its above the stacktrace
I did that too, then Vector2, like 43 gives me error expecting f32 instead of c_int
then you'd have to @floatFromInt
edited!
oh
also, the @as in your vector2 initializations aren't needed. @as provides a result type, but assigning a structs fields already provides a result type, so you can just take those out
why did it worked before tho, where I marked the screen, height as f32
ok !
when I didn't had all those json parsing and stuff
did you have it like this?
const width: f32 = 10.0;
const height: f32 = 10.0;
rl.InitWindow(width, height, ...);
change title to a [:0]const u8, which is a better type to use as its more descriptive. i guess json parsing just doesnt support that type lol
you generally shouldn't use c pointers in zig code anyways
how can I do that?
should i use raylib-zig?
just dont use [*c]T, instead use a more descriptive pointer like ?[*:0]T
i meant like, dont write out [*c]T yourself, for auto-generated bindings its perfectly fine
ok ok!!
thank you a lot
now, another error..
i dont even know why th i used it-
well, raylib's InitWindow asks for a [*c]const u8, but it actually wants a [*:0]const u8, but theres no way to know that from c's type system so zig has a catch-all pointer for c pointers
❯ zig build run
INFO: Initializing raylib 5.0
INFO: Platform backend: DESKTOP (GLFW)
INFO: Supported raylib modules:
INFO: > rcore:..... loaded (mandatory)
INFO: > rlgl:...... loaded (mandatory)
INFO: > rshapes:... loaded (optional)
INFO: > rtextures:. loaded (optional)
INFO: > rtext:..... loaded (optional)
INFO: > rmodels:... loaded (optional)
INFO: > raudio:.... loaded (optional)
Segmentation fault at address 0x75a467868010
???:?:?: 0x75a46767bf0f in ??? (libraylib.so.500)
Unwind information for `libraylib.so.500:0x75a46767bf0f` was not available, trace may be incomplete
run
└─ run space-game failure
error: the following command terminated unexpectedly:
/run/media/Akitoo/Windows/CODEBASE-2024/ZIG/space-game/zig-out/bin/space-game
Build Summary: 5/7 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run space-game failure
error: the following build command failed with exit code 1:
/run/media/Akitoo/Windows/CODEBASE-2024/ZIG/space-game/.zig-cache/o/7a3e3a504a47140b9dcc08034e1736d4/build /usr/bin/zig /run/media/Akitoo/Windows/CODEBASE-2024/ZIG/space-game /run/media/Akitoo/Windows/CODEBASE-2024/ZIG/space-game/.zig-cache /home/Akitoo/.cache/zig --seed 0x9fc7a27e -Z453abe6568a0db90 run
im actually very noob, im very sorry
we all are at some point 😄 seg fault inside libraylib usually means you're passing something bad to raylib
oof
ah its probably defer allocator.free(json_slice);, you're freeing the slice that the parsed GameSettings's title points to
you could use always_alloc in your json parsing settings
oh you're also calling defer parsed.deinit()
you generally want that to be in the scope that parsed is last used, since right now it's being freed right after it returns it
so how should i change that json.zig?..
should i defer the allocator in my main.zig?
i know you are typing just wondering!
the fix that'd require the least amount of changes would be changing the return in parseFileAttributes to
var settings = parsed.value;
settings.title = try allocator.dupeZ(u8, game_settings.title);
return settings;
then you'd have to do this in main:
defer allocator.free(settings.title);
kinda, you could return a std.json.Parsed(GameSettings) and then defer settings.deinit() in main instead of in the function that creates it
you'd also have to defer allocator.free(json_slice) in main too, so you'd have to return the backing slice to main as well
ok!
i will try all this
im sorry but I'm very confused, can you if possible try running code in your machine and see what works in the end, i get errors after erros
settings.json
{
"title": "SPACE-GAME",
"screen_width": 800,
"screen_height": 450,
"fps": 60
}
oh forgot to remove the unnecessary @as's too
i returned the file contents slice and the parsed GameSettings as a tuple, and defer deinit'ed them in main isntead of their creation functions
woah
it actually worked-
nice 
I have to understand the code a lot, but really thanks for putting time with me and helping me !!
people like you make me feel okay to ask questions here, really thank you!
np! im glad i could help. the main issue was when you use defer x.deinit(); return x;, x has been destroyed and you're returning an x with an invalid state.
oh
you pretty much always deinit something when youre done using it, so defer x.deinit(); should go in the last scope you use it.
I will be very honest I only know python language and I always wanted to low level language and zig attracted me? so I'm jumping from python (2-yrs) to zig
so is there any youtube channel or any resources(free, im sry) that you can recommend me if possible?
yeah manual memory management will probably be the biggest hurdle for you. I dont know any specific ones but anything applicable to memory management (like c tutorials, of which there are plenty) should be useful
try https://github.com/pedropark99/zig-book , it covers stack vs. heap memory, that's easy to not be aware of when writing Python
ok!
ok!!
I will check em all!