Search code examples
zig

error: use of undeclared identifier 'c_char'


I'm trying to make game engine and game on this on zig (and C++), but I'm getting this following error:

main.zig:75:54: error: use of undeclared identifier 'c_char' const zipArchive = zip.zip_open(std.mem.ptrCast(*c_char, zipFilePath.toSlice().ptr), 0, null);

currently my main.zig look like this:

const std = @import("std");
// const garchive = @import("archive_files.zig");

const zip = @cImport({
    @cInclude("zip.h");
});

const c = @cImport({
    @cInclude("SDL.h");
});

pub fn main() anyerror!void {
    std.log.info("ROOFTOPS 2 PORT.", .{});
    std.log.info("\t\tBY LXCHN1v1", .{});

    //var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    //const allocator = gpa.allocator();

    std.log.info("trying to read rooftops.dconf", .{});
    //var game_config = dconfparser.readDConfig("rooftops.dconf");

    // load_config("rooftops2.dconf");

    const main_archive_path = "main/game_00.garc";
    const init_script_name = "scripts/init.hesl";

    const initData = try loadFileFromZipArchive(main_archive_path, init_script_name);

    std.debug.print("INIT SCRIPT DATA:\n{}\n", .{initData});

    _ = c.SDL_Init(c.SDL_INIT_VIDEO);
    defer c.SDL_Quit();

    var window = c.SDL_CreateWindow("ROOFTOPS2", c.SDL_WINDOWPOS_CENTERED, c.SDL_WINDOWPOS_CENTERED, 800, 600, 0);
    defer c.SDL_DestroyWindow(window);

    var renderer = c.SDL_CreateRenderer(window, 0, c.SDL_RENDERER_PRESENTVSYNC);
    defer c.SDL_DestroyRenderer(renderer);

    mainloop: while (true) {
        var sdl_event: c.SDL_Event = undefined;
        while (c.SDL_PollEvent(&sdl_event) != 0) {
            switch (sdl_event.type) {
                c.SDL_QUIT => break :mainloop,
                else => {},
            }
        }

        _ = c.SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 0xff);
        _ = c.SDL_RenderClear(renderer);
        var rect = c.SDL_Rect{ .x = 0, .y = 0, .w = 60, .h = 60 };
        const a = 0.001 * @intToFloat(f32, c.SDL_GetTicks());
        const t = 2 * std.math.pi / 3.0;
        const r = 100 * @cos(0.1 * a);
        rect.x = 290 + @floatToInt(i32, r * @cos(a));
        rect.y = 170 + @floatToInt(i32, r * @sin(a));
        _ = c.SDL_SetRenderDrawColor(renderer, 0xff, 0, 0, 0xff);
        _ = c.SDL_RenderFillRect(renderer, &rect);
        rect.x = 290 + @floatToInt(i32, r * @cos(a + t));
        rect.y = 170 + @floatToInt(i32, r * @sin(a + t));
        _ = c.SDL_SetRenderDrawColor(renderer, 0, 0xff, 0, 0xff);
        _ = c.SDL_RenderFillRect(renderer, &rect);
        rect.x = 290 + @floatToInt(i32, r * @cos(a + 2 * t));
        rect.y = 170 + @floatToInt(i32, r * @sin(a + 2 * t));
        _ = c.SDL_SetRenderDrawColor(renderer, 0, 0, 0xff, 0xff);
        _ = c.SDL_RenderFillRect(renderer, &rect);
        c.SDL_RenderPresent(renderer);
    }
}


fn loadFileFromZipArchive(zipFilePath: []const u8, fileName: []const u8) ![]u8 {
    //const zipArchive = zip.zip_open(cast(*c_char, zipFilePath.toSlice().ptr), 0, null);
    //const zipArchive = zip.zip_open(zipFilePath.ptr.*c_char, 0, null);
    const zipArchive = zip.zip_open(std.mem.ptrCast(*c_char, zipFilePath.toSlice().ptr), 0, null);

    if (zipArchive == null) {
        return error.FailedToOpenZipArchive;
    }

    const index = zip.zip_name_locate(zipArchive, cast(*c_char, fileName.toSlice().ptr), 0);
    if (index < 0) {
        zip.zip_close(zipArchive);
        return error.FileNotFoundInZipArchive;
    }

    var fileInfo = zip.zip_stat_t{};
    if (zip.zip_stat_index(zipArchive, index, 0, &fileInfo) != 0) {
        zip.zip_close(zipArchive);
        return error.FailedToGetFileInfo;
    }

    const file = zip.zip_fopen_index(zipArchive, index, 0);
    if (file == null) {
        zip.zip_close(zipArchive);
        return error.FailedToOpenFileInZipArchive;
    }

    const fileSize = @intCast(usize, fileInfo.size);
    var buffer: [0]u8 = undefined;
    const bytesRead = zip.zip_fread(file, buffer.ptr, fileSize);
    if (bytesRead < 0) {
        zip.zip_fclose(file);
        zip.zip_close(zipArchive);
        return error.FailedToReadFileFromZipArchive;
    }

    zip.zip_fclose(file);
    zip.zip_close(zipArchive);

    return buffer[0..bytesRead];
}

I don't has idea how to fix this, (chatgpt doesn't help)

by code you can understand what I want to do, if more precisely I want to force it to open a game archive of the zip format, and there take the file for initiation, ie init.hesl ( it's essentially a lua script ) which has everything to initiate, ie parameters for the window, default game parameters, that is, if there is no file in settings/ , and so on


Solution

  • There is no c_char type in Zig. In any case, you don't need to do a cast to convert a []const u8 to a C string. You can use [0..:0] to create a null-terminated slice. So you can just do, e.g.:

    const zipArchive = zip.zip_open(zipFilePath[0..:0], 0, null);