~rycwo/forge

ref: HEAD forge/src/gui_context.c -rw-r--r-- 3.7 KiB
d7ee94d6Ryan Chan Fix missing cd in .build.yml 8 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// This file is part of Forge, the foundation library for Forge tools.
//
// Copyright (C) 2021 Ryan Chan <rycwo@posteo.net>
// SPDX-License-Identifier: GPL-3.0-only
//
// This Source Code Form is subject to the terms of the GNU General Public
// License v3.0 only. You should have received a copy of the license along with
// this program. If not, see <https://www.gnu.org/licenses/>.

#include "forge/gui_context.h"

#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>

#include "forge/expect.h"
#include "forge/gui_input.h"
#include "forge/matrix.h"
#include "forge/type.h"
#include "forge/vector.h"

#include "forge/gui_opengl_46.h"

void
fg_init_gui(
		struct fg_gui_context* context,
		struct fg_allocator const* alloc,
		struct fg_gui_buffer_desc desc) {
	memset(context, 0, sizeof(struct fg_gui_context));
	context->alloc = *alloc;

	context->layout.buf = (fg_vec4*)alloc->alloc(
			sizeof(fg_vec4) * desc.layout_stack_size);
	fg_vec4_copy(context->layout.buf[0], (fg_vec4){0.0, 0.0, 0.0, 0.0});
	context->layout.capacity = desc.layout_stack_size;
	context->layout.size = 1;

	fg_mat4_identity(context->transform);
	context->transform_active = false;

	// FIXME(ryc): Receive clip buffer size as argument
	// Default clip is initialized in begin_gui_frame
	context->clips.buf = (float*)alloc->alloc(sizeof(float) * 4 * 128);
	context->clips.capacity = 128;
	context->clips.size = 0;

	context->rects.prims = (struct fg_gui_rect*)alloc->alloc(
			sizeof(struct fg_gui_rect) * desc.rect_buffer_size);
	context->rects.capacity = desc.rect_buffer_size;
	context->rects.size_front = 0;
	context->rects.size_back = 0;

	// Graphics backend. For now it is hard-coded to use OpenGL 4.6.
	fg_init_gui_opengl_46(context);
}

void
fg_terminate_gui(struct fg_gui_context* context) {
	context->alloc.free(context->clips.buf);
	context->alloc.free(context->rects.prims);
	memset(context, 0, sizeof(struct fg_gui_context));

	fg_term_gui_opengl_46();
}

void
fg_begin_gui_frame(struct fg_gui_context* context) {
	context->hover = context->next_hover;
	context->next_hover = 0;
	context->next_hover_layer = 0;
	context->base_count = 0;
	context->overlay_count = 0;

	fg_vec2 viewport_size;
	fg_gui_viewport_size(context, viewport_size);
	context->clips.buf[0] = 0.0;
	context->clips.buf[1] = 0.0;
	context->clips.buf[2] = viewport_size[0];
	context->clips.buf[3] = viewport_size[1];
	context->clips.size = 1;
	context->rects.size_front = 0;
	context->rects.size_back = 0;
}

void
fg_commit_gui_frame(struct fg_gui_context* context) {
	fg_expect(
			context->layer == 0,
			"Base layer must be active before fg_commit_gui_frame. "
			"Forgot to call fg_set_gui_layer(GUI_LAYER_BASE)?\n");
	fg_expect(
			!context->transform_active,
			"Transform must be reset to identity before fg_commit_gui_frame. "
			"Forgot to call fg_set_gui_transform_identity?\n");
	fg_expect(
			context->clip == 0,
			"Clip must be reset before fg_commit_gui_frame. "
			"Forgot to call fg_end_gui_clip?\n");
	fg_expect(
			context->layout.size == 1,
			"Layout stack must be empty before fg_commit_gui_frame."
			"Forgot to call fg_pop_gui_layout_container?\n");

	// Items in the overlay layer are written to the back of the primitive
	// buffers in reverse order. The order of the overlay items must be reversed
	// to respect the expected draw order. They also need to be contiguous with
	// the base layer items to simplify GPU operations.

	for (int i = 0; i < context->rects.size_back; ++i)
		context->rects.prims[context->rects.size_front + i]
			= context->rects.prims[context->rects.capacity - i - 1];

	fg_commit_gui_opengl_46(context);
}

void
fg_draw_gui_frame(struct fg_gui_context* context) {
	// Forward draw call to graphics backend
	fg_draw_gui_opengl_46(context);
}