Forráskód Böngészése

Started work on an API documentation file builder

Karl Zylinski 6 hónapja
szülő
commit
7e344c10bf
3 módosított fájl, 245 hozzáadás és 6 törlés
  1. 70 0
      api_doc_builder/api_doc_builder.odin
  2. 167 0
      karl2d.doc.odin
  3. 8 6
      karl2d.odin

+ 70 - 0
api_doc_builder/api_doc_builder.odin

@@ -0,0 +1,70 @@
+package karl2d_api_doc_builder
+
+import os "core:os/os2"
+import os1 "core:os"
+import vmem "core:mem/virtual"
+import "core:log"
+import "core:fmt"
+import "core:odin/parser"
+import "core:odin/ast"
+import "core:strings"
+import "core:slice"
+import "core:hash"
+
+main :: proc() {
+	arena: vmem.Arena
+	context.allocator = vmem.arena_allocator(&arena)
+	context.temp_allocator = context.allocator
+	context.logger = log.create_console_logger()
+
+
+	plug_ast, plug_ast_ok := parser.parse_package_from_path(".")
+	log.ensuref(plug_ast_ok, "Could not generate AST for package")
+
+	b := strings.builder_make()
+
+	strings.write_string(&b, "// This file is purely documentational and never built.\n")
+	strings.write_string(&b, "#+ignore\n")
+	strings.write_string(&b, "package karl2d\n\n")
+
+	for n, &f in plug_ast.files {
+		if !strings.ends_with(n, "karl2d.odin") {
+			continue
+		}
+
+		decl_loop: for &d in f.decls {
+			#partial switch &dd in d.derived {
+			case ^ast.Value_Decl:
+				val: string
+				for v, vi in dd.values {
+					#partial switch vd in v.derived {
+					case ^ast.Proc_Lit:
+						name := f.src[dd.names[vi].pos.offset:dd.names[vi].end.offset]
+						type := f.src[vd.type.pos.offset:vd.type.end.offset]
+						docs := dd.docs == nil ? "" : f.src[dd.docs.pos.offset:dd.docs.end.offset]
+						val = fmt.tprintf("%v :: %v", name, type)
+					}
+				}
+
+				if val == "" {
+					val = f.src[dd.pos.offset:dd.end.offset]
+				}
+
+				if val == "API_END :: true" {
+					break decl_loop
+				}
+
+				if dd.docs != nil {
+					strings.write_string(&b, f.src[dd.docs.pos.offset:dd.docs.end.offset])
+					strings.write_rune(&b, '\n')
+				}
+
+				strings.write_string(&b, val)
+				strings.write_rune(&b, '\n')
+				strings.write_rune(&b, '\n')
+			}
+		}
+	}
+
+	_ = os.write_entire_file("karl2d.doc.odin", transmute([]u8)(strings.to_string(b)))
+}

+ 167 - 0
karl2d.doc.odin

@@ -0,0 +1,167 @@
+// This file is purely documentational and never built.
+#+ignore
+package karl2d
+
+Handle :: hm.Handle
+
+Texture_Handle :: distinct Handle
+
+TEXTURE_NONE :: Texture_Handle {}
+
+// Opens a window and initializes some internal state. The internal state will use `allocator` for
+// all dynamically allocated memory. The return value can be ignored unless you need to later call
+// `set_state`.
+init :: proc(window_width: int, window_height: int, window_title: string,
+             allocator := context.allocator, loc := #caller_location) -> ^State
+
+VERTEX_BUFFER_MAX :: 1000000
+
+make_default_projection :: proc(w, h: int) -> matrix[4,4]f32
+
+DEFAULT_SHADER_SOURCE :: #load("shader.hlsl")
+
+// Closes the window and cleans up the internal state.
+shutdown :: proc()
+
+// Clear the backbuffer with supplied color.
+clear :: proc(color: Color)
+
+// Present the backbuffer. Call at end of frame to make everything you've drawn appear on the screen.
+present :: proc()
+
+// Call at start or end of frame to process all events that have arrived to the window.
+//
+// WARNING: Not calling this will make your program impossible to interact with.
+process_events :: proc()
+
+/* Flushes the current batch. This sends off everything to the GPU that has been queued in the
+current batch. Normally, you do not need to do this manually. It is done automatically when these
+procedures run:
+	present
+	set_camera
+	set_shader
+
+TODO: complete this list and motivate why it needs to happen on those procs (or do that in the
+docs for those procs).
+*/
+draw_current_batch :: proc()
+
+// Can be used to restore the internal state using the pointer returned by `init`. Useful after
+// reloading the library (for example, when doing code hot reload).
+set_internal_state :: proc(state: ^State)
+
+get_screen_width :: proc() -> int
+
+get_screen_height :: proc() -> int
+
+key_went_down :: proc(key: Keyboard_Key) -> bool
+
+key_went_up :: proc(key: Keyboard_Key) -> bool
+
+key_is_held :: proc(key: Keyboard_Key) -> bool
+
+shutdown_wanted :: proc() -> bool
+
+set_window_position :: proc(x: int, y: int)
+
+set_window_size :: proc(width: int, height: int)
+
+set_camera :: proc(camera: Maybe(Camera))
+
+load_texture_from_file :: proc(filename: string) -> Texture
+
+destroy_texture :: proc(tex: Texture)
+
+draw_rect :: proc(r: Rect, c: Color)
+
+draw_rect_ex :: proc(r: Rect, origin: Vec2, rot: f32, c: Color)
+
+draw_rect_outline :: proc(r: Rect, thickness: f32, color: Color)
+
+draw_circle :: proc(center: Vec2, radius: f32, color: Color, segments := 16)
+
+draw_line :: proc(start: Vec2, end: Vec2, thickness: f32, color: Color)
+
+draw_texture :: proc(tex: Texture, pos: Vec2, tint := WHITE)
+
+draw_texture_rect :: proc(tex: Texture, rect: Rect, pos: Vec2, tint := WHITE)
+
+draw_texture_ex :: proc(tex: Texture, src: Rect, dst: Rect, origin: Vec2, rotation: f32, tint := WHITE)
+
+load_shader :: proc(shader_source: string, layout_formats: []Shader_Input_Format = {}) -> Shader
+
+get_shader_input_default_type :: proc(name: string, type: Shader_Input_Type) -> Shader_Default_Inputs
+
+get_shader_input_format :: proc(name: string, type: Shader_Input_Type) -> Shader_Input_Format
+
+destroy_shader :: proc(shader: Shader)
+
+set_shader :: proc(shader: Maybe(Shader))
+
+maybe_handle_equal :: proc(m1: Maybe($T), m2: Maybe(T)) -> bool
+
+set_shader_constant :: proc(shd: Shader, loc: Shader_Constant_Location, val: $T)
+
+set_shader_constant_mat4 :: proc(shader: Shader, loc: Shader_Constant_Location, val: matrix[4,4]f32)
+
+set_shader_constant_f32 :: proc(shader: Shader, loc: Shader_Constant_Location, val: f32)
+
+set_shader_constant_vec2 :: proc(shader: Shader, loc: Shader_Constant_Location, val: Vec2)
+
+get_default_shader :: proc() -> Shader
+
+set_scissor_rect :: proc(scissor_rect: Maybe(Rect))
+
+screen_to_world :: proc(pos: Vec2, camera: Camera) -> Vec2
+
+draw_text :: proc(text: string, pos: Vec2, font_size: f32, color: Color)
+
+mouse_button_went_down :: proc(button: Mouse_Button) -> bool
+
+mouse_button_went_up :: proc(button: Mouse_Button) -> bool
+
+mouse_button_is_held :: proc(button: Mouse_Button) -> bool
+
+get_mouse_wheel_delta :: proc() -> f32
+
+get_mouse_position :: proc() -> Vec2
+
+_batch_vertex :: proc(v: Vec2, uv: Vec2, color: Color)
+
+shader_input_format_size :: proc(f: Shader_Input_Format) -> int
+
+State :: struct {
+	allocator: runtime.Allocator,
+	custom_context: runtime.Context,
+	win: Window_Interface,
+	window_state: rawptr,
+	rb: Rendering_Backend_Interface,
+	rb_state: rawptr,
+	
+	shutdown_wanted: bool,
+
+	mouse_position: Vec2,
+	mouse_delta: Vec2,
+	mouse_wheel_delta: f32,
+
+	keys_went_down: #sparse [Keyboard_Key]bool,
+	keys_went_up: #sparse [Keyboard_Key]bool,
+	keys_is_held: #sparse [Keyboard_Key]bool,
+
+	window: Window_Handle,
+	width: int,
+	height: int,
+
+	shape_drawing_texture: Texture_Handle,
+	batch_camera: Maybe(Camera),
+	batch_shader: Maybe(Shader),
+	batch_texture: Texture_Handle,
+
+	view_matrix: Mat4,
+	proj_matrix: Mat4,
+
+	vertex_buffer_cpu: []u8,
+	vertex_buffer_cpu_used: int,
+	default_shader: Shader,
+}
+

+ 8 - 6
karl2d.odin

@@ -15,10 +15,6 @@ import "core:image/tga"
 
 import hm "handle_map"
 
-_ :: bmp
-_ :: png
-_ :: tga
-
 Handle :: hm.Handle
 Texture_Handle :: distinct Handle
 TEXTURE_NONE :: Texture_Handle {}
@@ -760,7 +756,6 @@ _batch_vertex :: proc(v: Vec2, uv: Vec2, color: Color) {
 	s.vertex_buffer_cpu_used += shd.vertex_size
 }
 
-
 shader_input_format_size :: proc(f: Shader_Input_Format) -> int {
 	switch f {
 	case .Unknown: return 0
@@ -810,6 +805,9 @@ State :: struct {
 	default_shader: Shader,
 }
 
+// Used by API builder. Everything after this constant will not be in karl2d_api.txt
+API_END :: true
+
 @(private="file")
 s: ^State
 win: Window_Interface
@@ -1058,4 +1056,8 @@ create_vertex_input_override :: proc(val: $T) -> Shader_Input_Value_Override {
 
 temp_cstring :: proc(str: string, loc := #caller_location) -> cstring {
 	return strings.clone_to_cstring(str, context.temp_allocator, loc)
-}
+}
+
+_ :: bmp
+_ :: png
+_ :: tga