--- /dev/null
+precision mediump float;
+
+COMM vec4 v_col;
+
+#ifdef VERTEX_SHADER
+
+layout(location = 0) in vec2 a_pos;
+layout(location = 1) in vec4 a_col;
+
+layout(std140) uniform u_window_matrix_block {
+ mat4 u_view;
+};
+
+layout(std140) uniform u_world_matrix_block {
+ mat4 u_world;
+ mat4 u_model;
+};
+
+void main() {
+ gl_Position = u_view * u_world * u_model * vec4(a_pos, 0, 1);
+ v_col = a_col;
+}
+
+#endif
+
+#ifdef FRAGMENT_SHADER
+
+out vec4 fragColor;
+
+void main() {
+ fragColor = vec4(v_col.xyz, 1);
+}
+
+#endif
+++ /dev/null
-#version 300 es
-precision mediump float;
-
-in vec4 v_col;
-
-out vec4 fragColor;
-
-void main() {
- fragColor = vec4(v_col.xyz, 1);
-}
\ No newline at end of file
+++ /dev/null
-#version 300 es
-precision mediump float;
-
-layout(location = 0) in vec2 a_pos;
-layout(location = 1) in vec4 a_col;
-
-layout(std140) uniform u_window_matrix_block {
- mat4 u_view;
-};
-
-layout(std140) uniform u_world_matrix_block {
- mat4 u_world;
- mat4 u_model;
-};
-
-out vec4 v_col;
-
-void main() {
- gl_Position = u_view * u_world * u_model * vec4(a_pos, 0, 1);
- v_col = a_col;
-}
\ No newline at end of file
--- /dev/null
+precision mediump float;
+
+COMM vec2 v_texture;
+
+#ifdef VERTEX_SHADER
+
+layout(location = 0) in vec2 a_interp;
+layout(location = 1) in vec2 a_pos_top_left;
+layout(location = 2) in vec2 a_pos_bottom_right;
+layout(location = 3) in vec2 a_tex_top_left;
+layout(location = 4) in vec2 a_tex_bottom_right;
+
+layout(std140) uniform u_window_matrix_block {
+ mat4 u_window;
+};
+
+void main() {
+ gl_Position = u_window * vec4(mix(a_pos_top_left, a_pos_bottom_right, a_interp), 0, 1);
+ v_texture = mix(a_tex_top_left, a_tex_bottom_right, a_interp);
+}
+
+#endif
+
+#ifdef FRAGMENT_SHADER
+
+uniform sampler2D u_texture;
+uniform vec4 u_color;
+
+out vec4 fragColor;
+
+void main() {
+ fragColor = vec4(u_color.rgb, u_color.a * texture(u_texture, v_texture).a);
+}
+
+#endif
+++ /dev/null
-#version 300 es
-precision mediump float;
-
-in vec2 v_texture;
-
-uniform sampler2D u_texture;
-uniform vec4 u_color;
-
-out vec4 fragColor;
-
-void main() {
- fragColor = vec4(u_color.rgb, u_color.a * texture(u_texture, v_texture).a);
-}
+++ /dev/null
-#version 300 es
-precision mediump float;
-layout(location = 0) in vec2 a_interp;
-layout(location = 1) in vec2 a_pos_top_left;
-layout(location = 2) in vec2 a_pos_bottom_right;
-layout(location = 3) in vec2 a_tex_top_left;
-layout(location = 4) in vec2 a_tex_bottom_right;
-
-layout(std140) uniform u_window_matrix_block {
- mat4 u_window;
-};
-
-out vec2 v_texture;
-
-void main() {
- gl_Position = u_window * vec4(mix(a_pos_top_left, a_pos_bottom_right, a_interp), 0, 1);
- v_texture = mix(a_tex_top_left, a_tex_bottom_right, a_interp);
-}
-
--- /dev/null
+precision mediump float;
+
+COMM vec4 v_col;
+COMM vec2 v_tex;
+
+#ifdef VERTEX_SHADER
+
+layout(location = 0) in vec2 a_pos;
+layout(location = 1) in vec2 a_tex;
+layout(location = 2) in vec4 a_col;
+
+layout(std140) uniform u_window_matrix_block {
+ mat4 u_window;
+};
+
+void main() {
+ gl_Position = u_window * vec4(a_pos, 0, 1);
+ v_tex = a_tex;
+ v_col = a_col;
+}
+
+#endif
+
+#ifdef FRAGMENT_SHADER
+
+uniform sampler2D u_texture;
+uniform float u_texture_enabled;
+
+out vec4 fragColor;
+
+void main() {
+ fragColor = v_col * mix(vec4(1), texture(u_texture, v_tex), vec4(u_texture_enabled));
+}
+
+#endif
+++ /dev/null
-#version 300 es
-precision mediump float;
-
-in vec4 v_col;
-in vec2 v_tex;
-
-uniform sampler2D u_texture;
-uniform float u_texture_enabled;
-
-out vec4 fragColor;
-
-void main() {
- fragColor = v_col * mix(vec4(1), texture(u_texture, v_tex), vec4(u_texture_enabled));
-}
+++ /dev/null
-#version 300 es
-precision mediump float;
-layout(location = 0) in vec2 a_pos;
-layout(location = 1) in vec2 a_tex;
-layout(location = 2) in vec4 a_col;
-
-layout(std140) uniform u_window_matrix_block {
- mat4 u_window;
-};
-
-out vec4 v_col;
-out vec2 v_tex;
-
-void main() {
- gl_Position = u_window * vec4(a_pos, 0, 1);
- v_tex = a_tex;
- v_col = a_col;
-}
\ No newline at end of file
fonts_init :: () {
map.init(^font_registry);
- font_shader = shader_make("./assets/shaders/font_vertex.glsl", "./assets/shaders/font_fragment.glsl");
+ font_shader = shader_make("./assets/shaders/font.glsl");
shader_use(font_shader);
shader_link_window_matrix_block(font_shader);
glBindVertexArray(-1);
}
+font_get_width :: (font: Font, msg: str) -> f32 {
+ x_, y_ := 0.0f, 0.0f;
+
+ quad: stbtt_aligned_quad;
+ for msg {
+ stbtt_GetPackedQuad(font.chars.data, font.texture_width, font.texture_height,
+ ~~(it - #char " "), ^x_, ^y_, ^quad, false);
+ }
+
+ return x_;
+}
FontDescriptor :: struct {
memory.alloc_slice(^vertex_data, Maximum_Vertex_Count);
vertex_count = 0;
- imgui_shader = shader_make(Shader_Vertex_Path, Shader_Fragment_Path);
+ imgui_shader = shader_make(Shader_Path);
shader_use(imgui_shader);
shader_link_window_matrix_block(imgui_shader);
shader_set_uniform(imgui_shader, #cstr "u_texture_enabled", 0.0f);
}
#local {
- Shader_Vertex_Path :: "./assets/shaders/imgui_vertex.glsl"
- Shader_Fragment_Path :: "./assets/shaders/imgui_fragment.glsl"
+ Shader_Path :: "./assets/shaders/imgui.glsl"
imgui_shader: Shader;
Maximum_Vertex_Count :: 1023;
glBindBufferBase(GL_UNIFORM_BUFFER, WORLD_MATRIX_BLOCK, world_matrix_block_buffer);
}
-shader_make :: (vertex_path, fragment_path: str) -> Shader {
- vertex_shader := os.get_contents(vertex_path);
- fragment_shader := os.get_contents(fragment_path);
- vs := compile_shader(vertex_shader, GL_VERTEX_SHADER);
- fs := compile_shader(fragment_shader, GL_FRAGMENT_SHADER);
+shader_make :: (shader_path: str) -> Shader {
+ shader_source := os.get_contents(shader_path);
+ vs := compile_shader(shader_source, GL_VERTEX_SHADER);
+ fs := compile_shader(shader_source, GL_FRAGMENT_SHADER);
prog := link_program(vs, fs);
#local {
compile_shader :: (source: str, type: GLenum) -> GLint {
shader := glCreateShader(type);
- source_data := source.data;
- source_len := source.count;
- glShaderSource(shader, 1, ^source_data, ^source_len);
+
+ #persist VERTEX_HEADER := """
+#version 300 es
+#define VERTEX_SHADER 1
+#define COMM out
+ """;
+
+ #persist FRAGMENT_HEADER := """
+#version 300 es
+#define FRAGMENT_SHADER 1
+#define COMM in
+ """;
+
+ header := VERTEX_HEADER if type == GL_VERTEX_SHADER else FRAGMENT_HEADER;
+ sources : [] ^u8 = .[ header.data, source.data ];
+ source_lens : [] i32 = .[ header.count, source.count ];
+
+ glShaderSource(shader, 2, sources.data, source_lens.data);
glCompileShader(shader);
success: GLint;
use package glfw3
#local runtime :: package runtime
+DEBUG :: #defined(runtime.vars.DEBUG)
+
@GlobalVariable
window: GLFWwindow_p
window_width: u32
window_height: u32
-main_font: Font;
+#if DEBUG { debug_font: Font; }
entity_manager: Entity_Manager;
update_view_matrix();
player_texture = texture_make(#cstr "./assets/images/player.png");
- main_font = font_lookup(.{"./assets/fonts/calibri.ttf", 32});
- entity_manager = entity_manager_make();
- player := Player.make();
- entity_manager->add(player);
+ #if DEBUG { debug_font = font_lookup(.{"./assets/fonts/calibri.ttf", 16}); }
+
+ entity_manager = entity_manager_create();
+ player := entity_manager->make(Player, .{
+ pos = .{300, 300}
+ });
- entity_manager->add(Wall.make(.{100, 100}, .{200, 100}));
+ entity_manager->make(Wall, .{ .{100, 100}, .{400, 50} });
+ entity_manager->make(Wall, .{ .{100, 100}, .{50, 400} });
+ entity_manager->make(Wall, .{ .{450, 100}, .{50, 400} });
+ entity_manager->make(Wall, .{ .{100, 450}, .{400, 50} });
+
+ entity_manager->make(Door, .{ .{150, 150}, .{50, 50} });
+ entity_manager->make(Door, .{ .{400, 150}, .{50, 50} });
+ entity_manager->make(Door, .{ .{400, 400}, .{50, 50} });
+ entity_manager->make(Door, .{ .{150, 400}, .{50, 50} });
}
update :: (dt: f32) {
update_world_matrix();
update_model_matrix(.{0,0});
- immediate_set_color(.{0,0,1});
- immediate_rectangle(0, 0, 100, 100);
-
entity_manager->draw();
- mx, my := mouse_get_position();
- immediate_set_color(.{1,1,1});
- immediate_rectangle(~~mx, ~~my, 10, 10);
+ #if false {
+ mx, my := mouse_get_position();
+ immediate_set_color(.{1,1,1});
+ immediate_rectangle(~~mx, ~~my, 10, 10);
+ }
immediate_flush();
- font_set_color(.{1,0,0});
- font_print(main_font, 0, 32, "FPS: {}", game_fps);
+ #if DEBUG {
+ font_set_color(.{1,0,0});
+ font_print(debug_font, 0, 16, "FPS: {}", game_fps);
+ font_print(debug_font, 0, 32, "HEAP: {b16}", alloc.heap.get_watermark());
+ font_print(debug_font, 0, 48, "FREE: {}KB", alloc.heap.get_freed_size() / 1024);
+
+ version_buf : [32] u8;
+ version_str := conv.format(version_buf, "Version: {}.{}", runtime.vars.MAJOR_VERSION, runtime.vars.MINOR_VERSION);
+ font_print(debug_font, ~~window_width - font_get_width(debug_font, version_str), 16, version_str);
+ }
glfwSwapBuffers(window);
}
}
}
-create_window :: () => {
+create_window :: () {
#if runtime.compiler_os == .Linux {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);