saving generators

This commit is contained in:
Peter Li 2025-04-09 19:02:29 -07:00
parent 641f38f295
commit f3ff4f5755
8 changed files with 178 additions and 56 deletions

3
lib/sdl3/src/c.zig vendored Normal file
View File

@ -0,0 +1,3 @@
pub const c = @cImport({ // todo, should not be constant
@cInclude("SDL3/SDL.h");
});

0
lib/sdl3/src/event.zig vendored Normal file
View File

131
lib/sdl3/src/generators/gpu.py vendored Normal file
View File

@ -0,0 +1,131 @@
import os
import sys
from pprint import pprint
import subprocess
origDir = os.path.abspath(os.path.dirname(__file__))
os.chdir(origDir)
gpufile = "../../SDL/include/SDL3/SDL_gpu.h"
mode_default = 0
mode_collecting = 1
typedefs_pod = []
typedefs_opaque = []
typedefs_enum = []
typedefs_struct = []
typedefs_unknown = []
functions = []
defines = []
lastList = None
with open(gpufile) as f:
mode = mode_default
braceCount = 0
for l in f:
dropped = True
line = l.strip()
if mode == mode_default:
if line.startswith('typedef'):
if ";" in line and 'struct' in line:
typedefs_opaque.append(line)
dropped = False
elif ';' in line:
typedefs_pod.append(line)
dropped = False
elif 'enum' in line:
lastList = typedefs_enum
typedefs_enum.append(line)
dropped = False
elif 'struct' in line:
lastList = typedefs_struct
typedefs_struct.append(line)
dropped = False
else:
lastList = typedefs_unknown
dropped = False
typedefs_unknown.append(line)
elif line.startswith('extern SDL_DECLSPEC'):
lastList = functions
dropped = False
functions.append(line)
elif line.startswith('#define '):
lastList = defines
defines.append(line)
if ';' not in line and not dropped:
mode = mode_collecting
if "{" in l:
braceCount = 1
elif mode == mode_collecting:
lastList[-1] += l.replace('\n', '\n')
if "{" in line:
braceCount += 1
if "}" in line:
braceCount -= 1
if ';' in line and braceCount == 0:
mode = mode_default
print_types = False
print_functions = False
print_defines= False
def plos(l):
for s in l:
print('> ', s, " < ")
print('opaque')
plos(typedefs_opaque)
print('pod')
plos(typedefs_pod)
if print_types:
print('struct')
plos(typedefs_struct)
print('enum')
plos(typedefs_enum)
print('unknown')
plos(typedefs_unknown)
if print_functions:
print('functions')
plos(functions)
if print_defines:
print('defines')
plos(defines)
print(f"\nfunctions: {len(functions)}, structs:{len(typedefs_struct)}, enums:{len(typedefs_enum)}\n")
ostring = 'pub const c = @import("c.zig").c;\n\n'
# time to generate types...
# first the opaques
typeMap = {}
typeMap_r= {}
for t in typedefs_opaque:
c_typename = t.strip().strip(';').split(' ')[3]
typename = c_typename
if typename.startswith("SDL_"):
typename = typename[4:]
typeMap[c_typename] = typename
typeMap_r[typename] = c_typename
brackets = "{}"
definition = f"pub const {typename} = opaque{brackets};\n"
ostring += definition
print (ostring)

View File

1
lib/sdl3/src/gpu.zig vendored Normal file
View File

@ -0,0 +1 @@
const c = @import("c.zig").c;

39
lib/sdl3/src/init.zig vendored Normal file
View File

@ -0,0 +1,39 @@
pub const c = @cImport({ // todo, should not be constant
@cInclude("SDL3/SDL.h");
});
pub const InitOptions = packed struct(c_int) {
pad0: u4 = 0, // 0 - 3
audio: bool = false, // 4
video: bool = false, // 5
pad1: u2 = 0, // 6-7
pad2: u1 = 0, // 8
joystick: bool = false, // 9
pad3: u2 = 0, // 10-11
haptic: bool = false, // 12
gamepad: bool = false,
events: bool = false,
sensor: bool = false,
camera: bool = false,
pad4: u3 = 0,
pad5: u12 = 0,
};
pub const AppResult = enum{
app_continue,
app_success,
app_failure,
};
pub const Window = opaque {};
pub fn createWindow() !*Window {
return @ptrCast();
}
pub fn init(options: InitOptions) !void {
if (!c.SDL_Init(@bitCast(options))) {
return error.InitFailed;
}
}

59
lib/sdl3/src/sdl3.zig vendored
View File

@ -1,57 +1,4 @@
pub const c = @cImport({ // todo, should not be constant const c = @import("c.zig").c;
@cInclude("SDL3/SDL.h");
});
pub const InitOptions = packed struct(c_int) { pub const gpu = @import("gpu.zig");
// nibble 0 pub const init = @import("init.zig");
pad0: u4 = 0, // 0 - 3
// nibble 1
audio: bool = false, // 4
video: bool = false, // 5
pad1: u2 = 0, // 6-7
// nibble 2
pad2: u1 = 0, // 8
joystick: bool = false, // 9
pad3: u2 = 0, // 10-11
// nibble 3
haptic: bool = false, // 12
gamepad: bool = false,
events: bool = false,
sensor: bool = false,
// nibble 4
camera: bool = false,
pad4: u3 = 0,
pad5: u12 = 0,
};
pub const Window = opaque {};
pub fn createWindow() !*Window {}
pub fn init(options: InitOptions) !void {
if (!c.SDL_Init(@bitCast(options))) {
return error.InitFailed;
}
}
pub fn pollEvent() ?c.SDL_Event {
var event: c.SDL_Event = undefined;
if (c.SDL_PollEvent(&event)) {
return event;
}
return null;
}
pub const SDLEvent = struct {};
pub const SDL_CommonEvent = struct {
eventType: u32 = 0, // Event type, shared with all events,
reserved: u32 = 0,
timestamp: u64 = 0, // in nanoseconds, populated using SDL_GetTickNS()
};

1
lib/sdl3/src/video.zig vendored Normal file
View File

@ -0,0 +1 @@
const c = @import("c.zig").c;