summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2023-01-02 14:52:26 -0500
committerpommicket <pommicket@gmail.com>2023-01-02 14:52:26 -0500
commitc04562a24e7fbee0839d00a8bfd5253426a98f20 (patch)
treeb41b547ca275d4752ab161e587a93602914e9d15
parentd5f1b97cadb8a1ad2d08cbcdded66e5140b81eb4 (diff)
rename v[234] to vec[234]
-rw-r--r--buffer.c34
-rw-r--r--command.h2
-rw-r--r--ds.h14
-rw-r--r--find.c30
-rw-r--r--gl.c22
-rw-r--r--ide-autocomplete.c22
-rw-r--r--main.c15
-rw-r--r--menu.c12
-rw-r--r--node.c16
-rw-r--r--ted.h20
-rw-r--r--text.c18
-rw-r--r--text.h2
-rw-r--r--ui.c40
-rw-r--r--util.c130
-rw-r--r--util.h131
15 files changed, 255 insertions, 253 deletions
diff --git a/buffer.c b/buffer.c
index 581a447..34fc9a2 100644
--- a/buffer.c
+++ b/buffer.c
@@ -905,7 +905,7 @@ void buffer_scroll(TextBuffer *buffer, double dx, double dy) {
}
// returns the position of the character at the given position in the buffer.
-v2 buffer_pos_to_pixels(TextBuffer *buffer, BufferPos pos) {
+vec2 buffer_pos_to_pixels(TextBuffer *buffer, BufferPos pos) {
buffer_pos_validate(buffer, &pos);
u32 line = pos.line, index = pos.index;
// we need to convert the index to a column
@@ -913,12 +913,12 @@ v2 buffer_pos_to_pixels(TextBuffer *buffer, BufferPos pos) {
Font *font = buffer_font(buffer);
float x = (float)((double)col - buffer->scroll_x) * text_font_char_width(font) + buffer->x1;
float y = (float)((double)line - buffer->scroll_y) * text_font_char_height(font) + buffer->y1;
- return V2(x, y);
+ return Vec2(x, y);
}
// convert pixel coordinates to a position in the buffer, selecting the closest character.
// returns false if the position is not inside the buffer, but still sets *pos to the closest character.
-bool buffer_pixels_to_pos(TextBuffer *buffer, v2 pixel_coords, BufferPos *pos) {
+bool buffer_pixels_to_pos(TextBuffer *buffer, vec2 pixel_coords, BufferPos *pos) {
bool ret = true;
float x = pixel_coords.x, y = pixel_coords.y;
Font *font = buffer_font(buffer);
@@ -962,7 +962,7 @@ bool buffer_clip_rect(TextBuffer *buffer, Rect *r) {
float x1, y1, x2, y2;
rect_coords(*r, &x1, &y1, &x2, &y2);
if (x1 > buffer->x2 || y1 > buffer->y2 || x2 < buffer->x1 || y2 < buffer->y1) {
- r->pos = r->size = V2(0, 0);
+ r->pos = r->size = Vec2(0, 0);
return false;
}
if (x1 < buffer->x1) x1 = buffer->x1;
@@ -2440,7 +2440,7 @@ void buffer_goto_word_at_cursor(TextBuffer *buffer) {
}
// returns true if the buffer "used" this event
-bool buffer_handle_click(Ted *ted, TextBuffer *buffer, v2 click, u8 times) {
+bool buffer_handle_click(Ted *ted, TextBuffer *buffer, vec2 click, u8 times) {
BufferPos buffer_pos;
if (ted->autocomplete.open) {
if (rect_contains_point(ted->autocomplete.rect, click))
@@ -2560,7 +2560,7 @@ void buffer_render(TextBuffer *buffer, Rect r) {
x1 += line_number_width;
x1 += 2; // a little bit of padding
// line separating line numbers from text
- gl_geometry_rect(rect(V2(x1, y1), V2(border_thickness, y2 - y1)), colors[COLOR_LINE_NUMBERS_SEPARATOR]);
+ gl_geometry_rect(rect(Vec2(x1, y1), Vec2(border_thickness, y2 - y1)), colors[COLOR_LINE_NUMBERS_SEPARATOR]);
x1 += border_thickness;
}
@@ -2595,12 +2595,12 @@ void buffer_render(TextBuffer *buffer, Rect r) {
}
// get screen coordinates of cursor
- v2 cursor_display_pos = buffer_pos_to_pixels(buffer, buffer->cursor_pos);
+ vec2 cursor_display_pos = buffer_pos_to_pixels(buffer, buffer->cursor_pos);
// the rectangle that the cursor is rendered as
- Rect cursor_rect = rect(cursor_display_pos, V2(settings->cursor_width, char_height));
+ Rect cursor_rect = rect(cursor_display_pos, Vec2(settings->cursor_width, char_height));
if (!buffer->is_line_buffer) { // highlight line cursor is on
- Rect hl_rect = rect(V2(x1, cursor_display_pos.y), V2(x2-x1-1, char_height));
+ Rect hl_rect = rect(Vec2(x1, cursor_display_pos.y), Vec2(x2-x1-1, char_height));
buffer_clip_rect(buffer, &hl_rect);
gl_geometry_rect(hl_rect, colors[COLOR_CURSOR_LINE_BG]);
}
@@ -2638,10 +2638,10 @@ void buffer_render(TextBuffer *buffer, Rect r) {
if (n_columns_highlighted) {
BufferPos p1 = {.line = line_idx, .index = index1};
- v2 hl_p1 = buffer_pos_to_pixels(buffer, p1);
+ vec2 hl_p1 = buffer_pos_to_pixels(buffer, p1);
Rect hl_rect = rect(
hl_p1,
- V2((float)n_columns_highlighted * char_width, char_height)
+ Vec2((float)n_columns_highlighted * char_width, char_height)
);
buffer_clip_rect(buffer, &hl_rect);
gl_geometry_rect(hl_rect, colors[buffer->view_only ? COLOR_VIEW_ONLY_SELECTION_BG : COLOR_SELECTION_BG]);
@@ -2764,8 +2764,8 @@ void buffer_render(TextBuffer *buffer, Rect r) {
}
if (depth == 0) {
// highlight it
- v2 gl_pos = buffer_pos_to_pixels(buffer, pos);
- Rect hl_rect = rect(gl_pos, V2(char_width, char_height));
+ vec2 gl_pos = buffer_pos_to_pixels(buffer, pos);
+ Rect hl_rect = rect(gl_pos, Vec2(char_width, char_height));
if (buffer_clip_rect(buffer, &hl_rect)) {
gl_geometry_rect(hl_rect, colors[COLOR_MATCHING_BRACKET_HL]);
}
@@ -2990,15 +2990,15 @@ void buffer_highlight_lsp_range(TextBuffer *buffer, LSPRange range) {
BufferPos range_end = buffer_pos_from_lsp(buffer, range.end);
// draw the highlight
if (range_start.line == range_end.line) {
- v2 a = buffer_pos_to_pixels(buffer, range_start);
- v2 b = buffer_pos_to_pixels(buffer, range_end);
+ vec2 a = buffer_pos_to_pixels(buffer, range_start);
+ vec2 b = buffer_pos_to_pixels(buffer, range_end);
b.y += char_height;
Rect r = rect_endpoints(a, b); buffer_clip_rect(buffer, &r);
gl_geometry_rect(r, colors[COLOR_HOVER_HL]);
} else if (range_end.line - range_start.line < 1000) { // prevent gigantic highlights from slowing things down
// multiple lines.
- v2 a = buffer_pos_to_pixels(buffer, range_start);
- v2 b = buffer_pos_to_pixels(buffer, buffer_pos_end_of_line(buffer, range_start.line));
+ vec2 a = buffer_pos_to_pixels(buffer, range_start);
+ vec2 b = buffer_pos_to_pixels(buffer, buffer_pos_end_of_line(buffer, range_start.line));
b.y += char_height;
Rect r1 = rect_endpoints(a, b); buffer_clip_rect(buffer, &r1);
gl_geometry_rect(r1, colors[COLOR_HOVER_HL]);
diff --git a/command.h b/command.h
index eb13898..6408141 100644
--- a/command.h
+++ b/command.h
@@ -1,6 +1,8 @@
#ifndef COMMAND_H_
#define COMMAND_H_
+#include "base.h"
+
// i | ARG_STRING = ted->strings[i]
#define ARG_STRING 0x4000000000000000
diff --git a/ds.h b/ds.h
index a447e4c..397dcfa 100644
--- a/ds.h
+++ b/ds.h
@@ -7,8 +7,7 @@ VARIOUS DATA STRUCTURES
- string builder
- string hash table
-This file is self-contained and should not use anything from any project it's contained in.
-You can just #include it -- it's not huge, the functions are all static, and
+You can just #include this file -- it's not huge, the functions are all static, and
any reasonable compiler will ignore the unused code.
functions in this file suffixed with _ are not meant to be used outside here, unless you
@@ -24,7 +23,12 @@ IMPORTANT NOTE: If you are using this with structures containing `long double`s,
which isnt important unless you're making a lot of arrays.)
*/
-#include <stddef.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef uint32_t u32;
+typedef uint8_t u8;
typedef union {
long num;
@@ -35,10 +39,6 @@ typedef union {
#endif
double flt;
} ArrMaxAlign;
-#if __STDC_VERSION__ < 199901L && !defined inline
-#define inline
-#endif
-
typedef struct {
u32 len;
diff --git a/find.c b/find.c
index f0dfb90..5eba929 100644
--- a/find.c
+++ b/find.c
@@ -347,23 +347,23 @@ void find_menu_frame(Ted *ted, Rect menu_bounds) {
const char *prev_text = "Previous", *next_text = "Next";
const char *replace_text = "Replace", *replace_find_text = "Replace+find", *replace_all_text = "Replace all";
- v2 prev_size = button_get_size(ted, prev_text);
- v2 next_size = button_get_size(ted, next_text);
- v2 replace_size = button_get_size(ted, replace_text);
- v2 replace_find_size = button_get_size(ted, replace_find_text);
- v2 replace_all_size = button_get_size(ted, replace_all_text);
+ vec2 prev_size = button_get_size(ted, prev_text);
+ vec2 next_size = button_get_size(ted, next_text);
+ vec2 replace_size = button_get_size(ted, replace_text);
+ vec2 replace_find_size = button_get_size(ted, replace_find_text);
+ vec2 replace_all_size = button_get_size(ted, replace_all_text);
float x = x1, y = y2 - prev_size.y;
// compute positions of buttons
- Rect button_prev = rect(V2(x, y), prev_size);
+ Rect button_prev = rect(Vec2(x, y), prev_size);
x += button_prev.size.x + padding;
- Rect button_next = rect(V2(x, y), next_size);
+ Rect button_next = rect(Vec2(x, y), next_size);
x += button_next.size.x + padding;
- Rect button_replace = rect(V2(x, y), replace_size);
+ Rect button_replace = rect(Vec2(x, y), replace_size);
x += button_replace.size.x + padding;
- Rect button_replace_find = rect(V2(x, y), replace_find_size);
+ Rect button_replace_find = rect(Vec2(x, y), replace_find_size);
x += button_replace_find.size.x + padding;
- Rect button_replace_all = rect(V2(x, y), replace_all_size);
+ Rect button_replace_all = rect(Vec2(x, y), replace_all_size);
x += button_replace_all.size.x + padding;
@@ -385,8 +385,8 @@ void find_menu_frame(Ted *ted, Rect menu_bounds) {
// highlight matches
BufferPos p1 = result->start, p2 = result->end;
if (p2.line >= first_rendered_line && p1.line <= last_rendered_line) {
- v2 pos1 = buffer_pos_to_pixels(buffer, p1);
- v2 pos2 = buffer_pos_to_pixels(buffer, p2);
+ vec2 pos1 = buffer_pos_to_pixels(buffer, p1);
+ vec2 pos2 = buffer_pos_to_pixels(buffer, p2);
pos2.y += char_height;
Rect hl_rect = rect4(pos1.x, pos1.y, pos2.x, pos2.y);
if (buffer_clip_rect(buffer, &hl_rect))
@@ -400,7 +400,7 @@ void find_menu_frame(Ted *ted, Rect menu_bounds) {
Rect find_buffer_bounds = rect4(x1 + text_width + padding, y1, x2 - padding, y1 + line_buffer_height);
- Rect replace_buffer_bounds = rect_translate(find_buffer_bounds, V2(0, line_buffer_height + padding));
+ Rect replace_buffer_bounds = rect_translate(find_buffer_bounds, Vec2(0, line_buffer_height + padding));
button_render(ted, button_prev, prev_text, colors[COLOR_TEXT]);
@@ -438,8 +438,8 @@ void find_menu_frame(Ted *ted, Rect menu_bounds) {
text_render(font_bold);
x = x1;
- x += checkbox_frame(ted, &ted->find_case_sensitive, "Case sensitive", V2(x, y1)).x + 2*padding;
- x += checkbox_frame(ted, &ted->find_regex, "Regular expression", V2(x, y1)).x + 2*padding;
+ x += checkbox_frame(ted, &ted->find_case_sensitive, "Case sensitive", Vec2(x, y1)).x + 2*padding;
+ x += checkbox_frame(ted, &ted->find_regex, "Regular expression", Vec2(x, y1)).x + 2*padding;
buffer_render(find_buffer, find_buffer_bounds);
if (replace) buffer_render(replace_buffer, replace_buffer_bounds);
diff --git a/gl.c b/gl.c
index 57b716c..e2cfc21 100644
--- a/gl.c
+++ b/gl.c
@@ -186,11 +186,11 @@ GLint gl_uniform_location(GLuint program, const char *uniform) {
}
typedef struct {
- v2 pos;
- v4 color;
+ vec2 pos;
+ vec4 color;
} GLSimpleVertex;
typedef struct {
- GLSimpleVertex v1, v2, v3;
+ GLSimpleVertex vert1, vert2, vert3;
} GLSimpleTriangle;
static GLSimpleTriangle *gl_geometry_triangles;
@@ -228,12 +228,12 @@ void gl_geometry_init(void) {
}
void gl_geometry_rect(Rect r, u32 color_rgba) {
- v4 color = rgba_u32_to_v4(color_rgba);
+ vec4 color = rgba_u32_to_vec4(color_rgba);
- v2 p1 = r.pos;
- v2 p2 = v2_add(r.pos, V2(0, r.size.y));
- v2 p3 = v2_add(r.pos, V2(r.size.x, r.size.y));
- v2 p4 = v2_add(r.pos, V2(r.size.x, 0));
+ vec2 p1 = r.pos;
+ vec2 p2 = vec2_add(r.pos, Vec2(0, r.size.y));
+ vec2 p3 = vec2_add(r.pos, Vec2(r.size.x, r.size.y));
+ vec2 p4 = vec2_add(r.pos, Vec2(r.size.x, 0));
GLSimpleTriangle triangle = {
{p1, color},
@@ -241,9 +241,9 @@ void gl_geometry_rect(Rect r, u32 color_rgba) {
{p3, color}
};
arr_add(gl_geometry_triangles, triangle);
- triangle.v1.pos = p3;
- triangle.v2.pos = p4;
- triangle.v3.pos = p1;
+ triangle.vert1.pos = p3;
+ triangle.vert2.pos = p4;
+ triangle.vert3.pos = p1;
arr_add(gl_geometry_triangles, triangle);
}
diff --git a/ide-autocomplete.c b/ide-autocomplete.c
index a9056f0..05f7844 100644
--- a/ide-autocomplete.c
+++ b/ide-autocomplete.c
@@ -382,7 +382,7 @@ void autocomplete_frame(Ted *ted) {
menu_height = 200.f;
}
- v2 cursor_pos = buffer_pos_to_pixels(buffer, buffer->cursor_pos);
+ vec2 cursor_pos = buffer_pos_to_pixels(buffer, buffer->cursor_pos);
bool open_up = cursor_pos.y > 0.5f * (buffer->y1 + buffer->y2); // should the completion menu open upwards?
bool open_left = cursor_pos.x > 0.5f * (buffer->x1 + buffer->x2);
float x = cursor_pos.x, start_y = cursor_pos.y;
@@ -392,7 +392,7 @@ void autocomplete_frame(Ted *ted) {
else
start_y += char_height; // put menu below cursor
{
- Rect menu_rect = rect(V2(x, start_y), V2(menu_width, menu_height));
+ Rect menu_rect = rect(Vec2(x, start_y), Vec2(menu_width, menu_height));
gl_geometry_rect(menu_rect, colors[COLOR_AUTOCOMPLETE_BG]);
gl_geometry_rect_border(menu_rect, 1, colors[COLOR_AUTOCOMPLETE_BORDER]);
ac->rect = menu_rect;
@@ -404,7 +404,7 @@ void autocomplete_frame(Ted *ted) {
if (ncompletions) {
assert(ac->cursor >= 0 && ac->cursor < (i32)ncompletions);
// highlight cursor entry
- Rect r = rect(V2(x, start_y + (float)(ac->cursor - scroll) * char_height), V2(menu_width, char_height));
+ Rect r = rect(Vec2(x, start_y + (float)(ac->cursor - scroll) * char_height), Vec2(menu_width, char_height));
if (rect_contains_point(ac->rect, rect_center(r))) {
gl_geometry_rect(r, colors[COLOR_AUTOCOMPLETE_HL]);
document = &ac->completions[ac->suggested[ac->cursor]];
@@ -413,7 +413,7 @@ void autocomplete_frame(Ted *ted) {
if (mouse_entry >= 0 && mouse_entry < (i32)ncompletions
&& rect_contains_point(ac->rect, ted->mouse_pos)) {
// highlight moused over entry
- Rect r = rect(V2(x, start_y + (float)(mouse_entry - scroll) * char_height), V2(menu_width, char_height));
+ Rect r = rect(Vec2(x, start_y + (float)(mouse_entry - scroll) * char_height), Vec2(menu_width, char_height));
gl_geometry_rect(r, colors[COLOR_AUTOCOMPLETE_HL]);
ted->cursor = ted->cursor_hand;
document = &ac->completions[ac->suggested[mouse_entry]];
@@ -438,7 +438,7 @@ void autocomplete_frame(Ted *ted) {
float doc_x = open_left ? ac->rect.pos.x - doc_width - padding
: ac->rect.pos.x + ac->rect.size.x + padding;
float doc_y = ac->rect.pos.y;
- Rect r = rect(V2(doc_x, doc_y), V2(doc_width, doc_height));
+ Rect r = rect(Vec2(doc_x, doc_y), Vec2(doc_width, doc_height));
gl_geometry_rect(r, colors[COLOR_AUTOCOMPLETE_BG]);
gl_geometry_rect_border(r, border_thickness, colors[COLOR_AUTOCOMPLETE_BORDER]);
@@ -457,7 +457,7 @@ void autocomplete_frame(Ted *ted) {
for (uint i = 0; i < ted->nmouse_clicks[SDL_BUTTON_LEFT]; ++i) {
- v2 click = ted->mouse_clicks[SDL_BUTTON_LEFT][i];
+ vec2 click = ted->mouse_clicks[SDL_BUTTON_LEFT][i];
if (rect_contains_point(ac->rect, click)) {
i32 entry = scroll + (i32)((click.y - start_y) / char_height);
if (entry >= 0 && entry < (i32)ncompletions) {
@@ -482,8 +482,8 @@ void autocomplete_frame(Ted *ted) {
state.x = x; state.y = y;
if (i != ncompletions_visible-1) {
- gl_geometry_rect(rect(V2(x, y + char_height),
- V2(menu_width, border_thickness)),
+ gl_geometry_rect(rect(Vec2(x, y + char_height),
+ Vec2(menu_width, border_thickness)),
colors[COLOR_AUTOCOMPLETE_BORDER]);
}
@@ -498,7 +498,7 @@ void autocomplete_frame(Ted *ted) {
state.x += padding;
text_utf8_with_state(font, &state, icon_text);
state.x += padding;
- gl_geometry_rect(rect(V2((float)state.x, (float)state.y), V2(border_thickness, char_height)),
+ gl_geometry_rect(rect(Vec2((float)state.x, (float)state.y), Vec2(border_thickness, char_height)),
colors[COLOR_AUTOCOMPLETE_BORDER]);
state.x += padding;
@@ -536,8 +536,8 @@ void autocomplete_frame(Ted *ted) {
}
if (completion->deprecated) {
- gl_geometry_rect(rect(V2(label_x, y + (char_height - border_thickness) * 0.5f),
- V2((float)state.x - label_x, 1)),
+ gl_geometry_rect(rect(Vec2(label_x, y + (char_height - border_thickness) * 0.5f),
+ Vec2((float)state.x - label_x, 1)),
colors[label_color]);
}
diff --git a/main.c b/main.c
index 8d3f28f..8fb9e1c 100644
--- a/main.c
+++ b/main.c
@@ -1,6 +1,5 @@
/*
@TODO:
-- rename v[234] to vec[234]
- make ctrl+up/ctrl+down move to next/prev blank line
- broken session fix: close buffers not in any used node
- handle multiple symbols with same name in go-to-definition menu
@@ -125,8 +124,8 @@ static Rect error_box_rect(Ted *ted) {
float padding = settings->padding;
float window_width = ted->window_width, window_height = ted->window_height;
float char_height = text_font_char_height(font);
- return rect_centered(V2(window_width * 0.5f, window_height * 0.9f),
- V2(menu_get_width(ted), 3 * char_height + 2 * padding));
+ return rect_centered(Vec2(window_width * 0.5f, window_height * 0.9f),
+ Vec2(menu_get_width(ted), 3 * char_height + 2 * padding));
}
#if DEBUG
@@ -611,7 +610,7 @@ int main(int argc, char **argv) {
{ // get mouse position
int mouse_x = 0, mouse_y = 0;
ted->mouse_state = SDL_GetMouseState(&mouse_x, &mouse_y);
- ted->mouse_pos = V2((float)mouse_x, (float)mouse_y);
+ ted->mouse_pos = Vec2((float)mouse_x, (float)mouse_y);
}
bool ctrl_down = keyboard_state[SDL_SCANCODE_LCTRL] || keyboard_state[SDL_SCANCODE_RCTRL];
bool shift_down = keyboard_state[SDL_SCANCODE_LSHIFT] || keyboard_state[SDL_SCANCODE_RSHIFT];
@@ -658,7 +657,7 @@ int main(int argc, char **argv) {
if (button < arr_count(ted->nmouse_clicks)
&& ted->nmouse_clicks[button] < arr_count(ted->mouse_clicks[button])) {
- v2 pos = V2(x, y);
+ vec2 pos = Vec2(x, y);
bool add = true;
if (*ted->error_shown) {
if (rect_contains_point(error_box_rect(ted), pos)) {
@@ -705,7 +704,7 @@ int main(int argc, char **argv) {
case SDL_MOUSEBUTTONUP: {
Uint8 button = event.button.button;
if (button < arr_count(ted->nmouse_releases)) {
- v2 pos = V2((float)event.button.x, (float)event.button.y);
+ vec2 pos = Vec2((float)event.button.x, (float)event.button.y);
if (ted->nmouse_releases[button] < arr_count(ted->mouse_releases[button])) {
ted->mouse_releases[button][ted->nmouse_releases[button]++] = pos;
}
@@ -719,7 +718,7 @@ int main(int argc, char **argv) {
BufferPos pos = {0};
// drag to select
// we don't check the return value here, because it's okay to drag off the screen.
- buffer_pixels_to_pos(ted->drag_buffer, V2(x, y), &pos);
+ buffer_pixels_to_pos(ted->drag_buffer, Vec2(x, y), &pos);
buffer_select_to_pos(ted->drag_buffer, pos);
}
ted->hover.time = 0.0;
@@ -770,7 +769,7 @@ int main(int argc, char **argv) {
{
int mx = 0, my = 0;
ted->mouse_state = SDL_GetMouseState(&mx, &my);
- ted->mouse_pos = V2((float)mx, (float)my);
+ ted->mouse_pos = Vec2((float)mx, (float)my);
}
// default to arrow cursor
ted->cursor = ted->cursor_arrow;
diff --git a/menu.c b/menu.c
index d6d954f..c419e8c 100644
--- a/menu.c
+++ b/menu.c
@@ -56,7 +56,7 @@ void menu_open(Ted *ted, Menu menu) {
ted->menu = menu;
TextBuffer *prev_buf = ted->prev_active_buffer = ted->active_buffer;
if (prev_buf)
- ted->prev_active_buffer_scroll = V2D(prev_buf->scroll_x, prev_buf->scroll_y);
+ ted->prev_active_buffer_scroll = Vec2d(prev_buf->scroll_x, prev_buf->scroll_y);
ted_switch_to_buffer(ted, NULL);
*ted->warn_overwrite = 0; // clear warn_overwrite
@@ -119,8 +119,8 @@ Rect menu_rect(Ted *ted) {
float padding = settings->padding;
float menu_width = menu_get_width(ted);
return rect(
- V2(window_width * 0.5f - 0.5f * menu_width, padding),
- V2(menu_width, window_height - 2 * padding)
+ Vec2(window_width * 0.5f - 0.5f * menu_width, padding),
+ Vec2(menu_width, window_height - 2 * padding)
);
}
@@ -327,7 +327,7 @@ void menu_render(Ted *ted) {
const float line_buffer_height = ted_line_buffer_height(ted);
// render backdrop
- gl_geometry_rect(rect(V2(0, 0), V2(window_width, window_height)), colors[COLOR_MENU_BACKDROP]);
+ gl_geometry_rect(rect(Vec2(0, 0), Vec2(window_width, window_height)), colors[COLOR_MENU_BACKDROP]);
gl_geometry_draw();
if (*ted->warn_overwrite) {
@@ -394,11 +394,11 @@ void menu_render(Ted *ted) {
} break;
case MENU_GOTO_LINE: {
float menu_height = char_height + 2 * padding;
- Rect r = rect(V2(padding, window_height - menu_height - padding), V2(window_width - 2 * padding, menu_height));
+ Rect r = rect(Vec2(padding, window_height - menu_height - padding), Vec2(window_width - 2 * padding, menu_height));
gl_geometry_rect(r, colors[COLOR_MENU_BG]);
gl_geometry_rect_border(r, settings->border_thickness, colors[COLOR_BORDER]);
const char *text = "Go to line...";
- v2 text_size = text_get_size_v2(font_bold, text);
+ vec2 text_size = text_get_size_v2(font_bold, text);
rect_coords(r, &x1, &y1, &x2, &y2);
x1 += padding;
y1 += padding;
diff --git a/node.c b/node.c
index 587b253..9b4fe08 100644
--- a/node.c
+++ b/node.c
@@ -201,7 +201,7 @@ void node_frame(Ted *ted, Node *node, Rect r) {
float tab_width = r.size.x / ntabs;
if (!ted->menu) {
for (u16 c = 0; c < ted->nmouse_clicks[SDL_BUTTON_LEFT]; ++c) {
- v2 click = ted->mouse_clicks[SDL_BUTTON_LEFT][c];
+ vec2 click = ted->mouse_clicks[SDL_BUTTON_LEFT][c];
if (rect_contains_point(tab_bar_rect, click)) {
// click on tab to switch to it
u16 tab_index = (u16)((click.x - r.pos.x) / tab_width);
@@ -217,7 +217,7 @@ void node_frame(Ted *ted, Node *node, Rect r) {
if (ted->dragging_tab_node) {
// check if user dropped tab here
for (u16 c = 0; c < ted->nmouse_releases[SDL_BUTTON_LEFT]; ++c) {
- v2 release = ted->mouse_releases[SDL_BUTTON_LEFT][c];
+ vec2 release = ted->mouse_releases[SDL_BUTTON_LEFT][c];
if (rect_contains_point(tab_bar_rect, release)) {
u16 tab_index = (u16)roundf((release.x - r.pos.x) / tab_width);
if (tab_index <= arr_len(node->tabs)) {
@@ -251,7 +251,7 @@ void node_frame(Ted *ted, Node *node, Rect r) {
}
for (u16 c = 0; c < ted->nmouse_clicks[SDL_BUTTON_MIDDLE]; ++c) {
// middle-click to close tab
- v2 click = ted->mouse_clicks[SDL_BUTTON_MIDDLE][c];
+ vec2 click = ted->mouse_clicks[SDL_BUTTON_MIDDLE][c];
if (rect_contains_point(tab_bar_rect, click)) {
u16 tab_index = (u16)((click.x - r.pos.x) / tab_width);
if (tab_index < arr_len(node->tabs)) {
@@ -280,7 +280,7 @@ void node_frame(Ted *ted, Node *node, Rect r) {
char tab_title[256];
const char *path = buffer_get_filename(buffer);
const char *filename = path ? path_filename(path) : TED_UNTITLED;
- Rect tab_rect = rect(V2(r.pos.x + tab_width * i, r.pos.y), V2(tab_width, tab_bar_height));
+ Rect tab_rect = rect(Vec2(r.pos.x + tab_width * i, r.pos.y), Vec2(tab_width, tab_bar_height));
if (i > 0) {
// make sure tab borders overlap (i.e. don't double the border thickness between tabs)
@@ -290,7 +290,7 @@ void node_frame(Ted *ted, Node *node, Rect r) {
if (node == ted->dragging_tab_node && i == ted->dragging_tab_idx) {
// make tab follow mouse
- tab_rect.pos = v2_add(tab_rect.pos, v2_sub(ted->mouse_pos, ted->dragging_tab_origin));
+ tab_rect.pos = vec2_add(tab_rect.pos, vec2_sub(ted->mouse_pos, ted->dragging_tab_origin));
}
// tab border
@@ -327,7 +327,7 @@ void node_frame(Ted *ted, Node *node, Rect r) {
u16 buffer_index = node->tabs[node->active_tab];
TextBuffer *buffer = &ted->buffers[buffer_index];
assert(ted->buffers_used[buffer_index]);
- Rect buffer_rect = rect_translate(r, V2(0, tab_bar_height));
+ Rect buffer_rect = rect_translate(r, Vec2(0, tab_bar_height));
// make sure buffer border and tab border overlap
buffer_rect.pos.y -= border_thickness;
@@ -362,13 +362,13 @@ void node_frame(Ted *ted, Node *node, Rect r) {
r1.size.y = split_pos - padding;
r2.pos.y += split_pos + padding;
r2.size.y = r.size.y - split_pos - padding;
- r_between = rect(V2(r.pos.x, r.pos.y + split_pos - padding), V2(r.size.x, 2 * padding));
+ r_between = rect(Vec2(r.pos.x, r.pos.y + split_pos - padding), Vec2(r.size.x, 2 * padding));
} else {
float split_pos = r.size.x * node->split_pos;
r1.size.x = split_pos - padding;
r2.pos.x += split_pos + padding;
r2.size.x = r.size.x - split_pos - padding;
- r_between = rect(V2(r.pos.x + split_pos - padding, r.pos.y), V2(2 * padding, r.size.y));
+ r_between = rect(Vec2(r.pos.x + split_pos - padding, r.pos.y), Vec2(2 * padding, r.size.y));
}
if (rect_contains_point(r_between, ted->mouse_pos)) {
ted->cursor = resize_cursor;
diff --git a/ted.h b/ted.h
index 2481ee4..b43d7b1 100644
--- a/ted.h
+++ b/ted.h
@@ -501,14 +501,14 @@ typedef struct Ted {
Settings *default_settings;
float window_width, window_height;
u32 key_modifier; // which of shift, alt, ctrl are down right now.
- v2 mouse_pos;
+ vec2 mouse_pos;
u32 mouse_state;
u8 nmouse_clicks[4]; // nmouse_clicks[i] = length of mouse_clicks[i]
- v2 mouse_clicks[4][32]; // mouse_clicks[SDL_BUTTON_RIGHT], for example, is all the right mouse-clicks that have happened this frame
+ vec2 mouse_clicks[4][32]; // mouse_clicks[SDL_BUTTON_RIGHT], for example, is all the right mouse-clicks that have happened this frame
// number of times mouse was clicked at each position
u8 mouse_click_times[4][32];
u8 nmouse_releases[4];
- v2 mouse_releases[4][32];
+ vec2 mouse_releases[4][32];
int scroll_total_x, scroll_total_y; // total amount scrolled in the x and y direction this frame
Menu menu;
FileSelector file_selector;
@@ -547,7 +547,7 @@ typedef struct Ted {
u32 build_error; // build error we are currently "on"
// used by menus to keep track of the scroll position so we can return to it.
- v2d prev_active_buffer_scroll;
+ vec2d prev_active_buffer_scroll;
SDL_Cursor *cursor_arrow, *cursor_ibeam, *cursor_wait,
*cursor_resize_h, *cursor_resize_v, *cursor_hand, *cursor_move;
@@ -557,7 +557,7 @@ typedef struct Ted {
Node *dragging_tab_node;
// index in dragging_tab_node->tabs
u16 dragging_tab_idx;
- v2 dragging_tab_origin; // where the tab is being dragged from (i.e. mouse pos at start of drag action)
+ vec2 dragging_tab_origin; // where the tab is being dragged from (i.e. mouse pos at start of drag action)
// if not NULL, points to the node whose split the user is currently resizing.
Node *resizing_split;
@@ -644,8 +644,8 @@ void buffer_text_dimensions(TextBuffer *buffer, u32 *lines, u32 *columns);
float buffer_display_lines(TextBuffer *buffer);
float buffer_display_cols(TextBuffer *buffer);
void buffer_scroll(TextBuffer *buffer, double dx, double dy);
-v2 buffer_pos_to_pixels(TextBuffer *buffer, BufferPos pos);
-bool buffer_pixels_to_pos(TextBuffer *buffer, v2 pixel_coords, BufferPos *pos);
+vec2 buffer_pos_to_pixels(TextBuffer *buffer, BufferPos pos);
+bool buffer_pixels_to_pos(TextBuffer *buffer, vec2 pixel_coords, BufferPos *pos);
void buffer_scroll_to_pos(TextBuffer *buffer, BufferPos pos);
void buffer_scroll_center_pos(TextBuffer *buffer, BufferPos pos);
void buffer_scroll_to_cursor(TextBuffer *buffer);
@@ -734,7 +734,7 @@ bool buffer_save_as(TextBuffer *buffer, const char *new_filename);
u32 buffer_first_rendered_line(TextBuffer *buffer);
u32 buffer_last_rendered_line(TextBuffer *buffer);
void buffer_goto_word_at_cursor(TextBuffer *buffer);
-bool buffer_handle_click(Ted *ted, TextBuffer *buffer, v2 click, u8 times);
+bool buffer_handle_click(Ted *ted, TextBuffer *buffer, vec2 click, u8 times);
void buffer_render(TextBuffer *buffer, Rect r);
void buffer_indent_lines(TextBuffer *buffer, u32 first_line, u32 last_line);
void buffer_dedent_lines(TextBuffer *buffer, u32 first_line, u32 last_line);
@@ -1097,12 +1097,12 @@ void file_selector_free(FileSelector *fs);
// the returned pointer should be freed.
char *file_selector_update(Ted *ted, FileSelector *fs);
void file_selector_render(Ted *ted, FileSelector *fs);
-v2 button_get_size(Ted *ted, const char *text);
+vec2 button_get_size(Ted *ted, const char *text);
void button_render(Ted *ted, Rect button, const char *text, u32 color);
// returns true if the button was clicked on.
bool button_update(Ted *ted, Rect button);
PopupOption popup_update(Ted *ted, u32 options);
void popup_render(Ted *ted, u32 options, const char *title, const char *body);
-v2 checkbox_frame(Ted *ted, bool *value, const char *label, v2 pos);
+vec2 checkbox_frame(Ted *ted, bool *value, const char *label, vec2 pos);
#endif
diff --git a/text.c b/text.c
index 6e40733..bd1f3a8 100644
--- a/text.c
+++ b/text.c
@@ -28,13 +28,13 @@ no_warn_end
#define CHAR_PAGE_COUNT UNICODE_CODE_POINTS / CHAR_PAGE_SIZE
typedef struct {
- v2 pos;
- v2 tex_coord;
- v4 color;
+ vec2 pos;
+ vec2 tex_coord;
+ vec4 color;
} TextVertex;
typedef struct {
- TextVertex v1, v2, v3;
+ TextVertex vert1, vert2, vert3;
} TextTriangle;
struct Font {
@@ -387,14 +387,14 @@ void text_utf8_with_state(Font *font, TextRenderState *state, const char *str) {
}
}
-static v2 text_render_utf8_internal(Font *font, const char *text, double x, double y, u32 color, bool render) {
+static vec2 text_render_utf8_internal(Font *font, const char *text, double x, double y, u32 color, bool render) {
TextRenderState render_state = text_render_state_default;
render_state.render = render;
render_state.x = x;
render_state.y = y;
rgba_u32_to_floats(color, render_state.color);
text_utf8_with_state(font, &render_state, text);
- return V2(
+ return Vec2(
(float)(render_state.x_largest - x),
(float)(render_state.y_largest - y)
);
@@ -424,13 +424,13 @@ void text_utf8_anchored(Font *font, const char *text, double x, double y, u32 co
void text_get_size(Font *font, const char *text, float *width, float *height) {
double x = 0, y = 0;
- v2 size = text_render_utf8_internal(font, text, x, y, 0, false);
+ vec2 size = text_render_utf8_internal(font, text, x, y, 0, false);
if (width) *width = size.x;
if (height) *height = size.y + font->char_height;
}
-v2 text_get_size_v2(Font *font, const char *text) {
- v2 v;
+vec2 text_get_size_v2(Font *font, const char *text) {
+ vec2 v;
text_get_size(font, text, &v.x, &v.y);
return v;
}
diff --git a/text.h b/text.h
index 01fc053..d396241 100644
--- a/text.h
+++ b/text.h
@@ -61,7 +61,7 @@ float text_font_char_width(Font *font);
void text_font_set_force_monospace(Font *font, bool force);
// Get the dimensions of some text.
void text_get_size(Font *font, const char *text, float *width, float *height);
-v2 text_get_size_v2(Font *font, const char *text);
+vec2 text_get_size_v2(Font *font, const char *text);
void text_get_size32(Font *font, const char32_t *text, u64 len, float *width, float *height);
void text_utf8(Font *font, const char *text, double x, double y, u32 color);
void text_utf8_anchored(Font *font, const char *text, double x, double y, u32 color, Anchor anchor);
diff --git a/ui.c b/ui.c
index c87596f..d55dfd3 100644
--- a/ui.c
+++ b/ui.c
@@ -41,10 +41,10 @@ static void selector_scroll_to_cursor(Ted *ted, Selector *s) {
static bool selector_entry_pos(Ted *ted, const Selector *s, u32 i, Rect *r) {
Rect bounds = s->bounds;
float char_height = text_font_char_height(ted->font);
- *r = rect(V2(bounds.pos.x, selector_entries_start_y(ted, s)
+ *r = rect(Vec2(bounds.pos.x, selector_entries_start_y(ted, s)
- char_height * s->scroll
+ char_height * (float)i),
- V2(bounds.size.x, char_height));
+ Vec2(bounds.size.x, char_height));
return rect_clip_to_rect(r, bounds);
}
@@ -525,9 +525,9 @@ void file_selector_render(Ted *ted, FileSelector *fs) {
selector_render(ted, sel);
}
-v2 button_get_size(Ted *ted, const char *text) {
+vec2 button_get_size(Ted *ted, const char *text) {
float border_thickness = ted_active_settings(ted)->border_thickness;
- return v2_add_const(text_get_size_v2(ted->font, text), 2 * border_thickness);
+ return vec2_add_const(text_get_size_v2(ted->font, text), 2 * border_thickness);
}
void button_render(Ted *ted, Rect button, const char *text, u32 color) {
@@ -542,7 +542,7 @@ void button_render(Ted *ted, Rect button, const char *text, u32 color) {
gl_geometry_rect_border(button, ted_active_settings(ted)->border_thickness, colors[COLOR_BORDER]);
gl_geometry_draw();
- v2 pos = rect_center(button);
+ vec2 pos = rect_center(button);
text_utf8_anchored(ted->font, text, pos.x, pos.y, color, ANCHOR_MIDDLE);
text_render(ted->font);
}
@@ -559,23 +559,23 @@ bool button_update(Ted *ted, Rect button) {
static void popup_get_rects(Ted const *ted, u32 options, Rect *popup, Rect *button_yes, Rect *button_no, Rect *button_cancel) {
float window_width = ted->window_width, window_height = ted->window_height;
- *popup = rect_centered(V2(window_width * 0.5f, window_height * 0.5f), V2(300, 200));
+ *popup = rect_centered(Vec2(window_width * 0.5f, window_height * 0.5f), Vec2(300, 200));
float button_height = 30;
u16 nbuttons = util_popcount(options);
float button_width = popup->size.x / nbuttons;
- popup->size = v2_clamp(popup->size, V2(0, 0), V2(window_width, window_height));
- Rect r = rect(V2(popup->pos.x, rect_y2(*popup) - button_height), V2(button_width, button_height));
+ popup->size = vec2_clamp(popup->size, Vec2(0, 0), Vec2(window_width, window_height));
+ Rect r = rect(Vec2(popup->pos.x, rect_y2(*popup) - button_height), Vec2(button_width, button_height));
if (options & POPUP_YES) {
*button_yes = r;
- r = rect_translate(r, V2(button_width, 0));
+ r = rect_translate(r, Vec2(button_width, 0));
}
if (options & POPUP_NO) {
*button_no = r;
- r = rect_translate(r, V2(button_width, 0));
+ r = rect_translate(r, Vec2(button_width, 0));
}
if (options & POPUP_CANCEL) {
*button_cancel = r;
- r = rect_translate(r, V2(button_width, 0));
+ r = rect_translate(r, Vec2(button_width, 0));
}
}
@@ -611,16 +611,16 @@ void popup_render(Ted *ted, u32 options, const char *title, const char *body) {
gl_geometry_rect(r, colors[COLOR_MENU_BG]);
gl_geometry_rect_border(r, border_thickness, colors[COLOR_BORDER]);
// line separating text from body
- gl_geometry_rect(rect(V2(r.pos.x, y + char_height_bold), V2(r.size.x, border_thickness)), colors[COLOR_BORDER]);
+ gl_geometry_rect(rect(Vec2(r.pos.x, y + char_height_bold), Vec2(r.size.x, border_thickness)), colors[COLOR_BORDER]);
if (options & POPUP_YES) button_render(ted, button_yes, "Yes", colors[COLOR_YES]);
if (options & POPUP_NO) button_render(ted, button_no, "No", colors[COLOR_NO]);
if (options & POPUP_CANCEL) button_render(ted, button_cancel, "Cancel", colors[COLOR_CANCEL]);
// title text
- v2 title_size = {0};
+ vec2 title_size = {0};
text_get_size(font_bold, title, &title_size.x, &title_size.y);
- v2 title_pos = v2_sub(V2(window_width * 0.5f, y), V2(title_size.x * 0.5f, 0));
+ vec2 title_pos = vec2_sub(Vec2(window_width * 0.5f, y), Vec2(title_size.x * 0.5f, 0));
text_utf8(font_bold, title, title_pos.x, title_pos.y, colors[COLOR_TEXT]);
text_render(font_bold);
@@ -641,7 +641,7 @@ void popup_render(Ted *ted, u32 options, const char *title, const char *body) {
}
// returns the size of the checkbox, including the label
-v2 checkbox_frame(Ted *ted, bool *value, const char *label, v2 pos) {
+vec2 checkbox_frame(Ted *ted, bool *value, const char *label, vec2 pos) {
Font *font = ted->font;
float char_height = text_font_char_height(font);
float checkbox_size = char_height;
@@ -650,7 +650,7 @@ v2 checkbox_frame(Ted *ted, bool *value, const char *label, v2 pos) {
float padding = settings->padding;
float border_thickness = settings->border_thickness;
- Rect checkbox_rect = rect(pos, V2(checkbox_size, checkbox_size));
+ Rect checkbox_rect = rect(pos, Vec2(checkbox_size, checkbox_size));
for (u32 i = 0; i < ted->nmouse_clicks[SDL_BUTTON_LEFT]; ++i) {
if (rect_contains_point(checkbox_rect, ted->mouse_clicks[SDL_BUTTON_LEFT][i])) {
@@ -658,17 +658,17 @@ v2 checkbox_frame(Ted *ted, bool *value, const char *label, v2 pos) {
}
}
- checkbox_rect.pos = v2_add(checkbox_rect.pos, V2(0.5f, 0.5f));
+ checkbox_rect.pos = vec2_add(checkbox_rect.pos, Vec2(0.5f, 0.5f));
gl_geometry_rect_border(checkbox_rect, border_thickness, colors[COLOR_TEXT]);
if (*value) {
gl_geometry_rect(rect_shrink(checkbox_rect, border_thickness + 2), colors[COLOR_TEXT]);
}
- v2 text_pos = v2_add(pos, V2(checkbox_size + padding * 0.5f, 0));
- v2 size = text_get_size_v2(font, label);
+ vec2 text_pos = vec2_add(pos, Vec2(checkbox_size + padding * 0.5f, 0));
+ vec2 size = text_get_size_v2(font, label);
text_utf8(font, label, text_pos.x, text_pos.y, colors[COLOR_TEXT]);
gl_geometry_draw();
text_render(font);
- return v2_add(size, V2(checkbox_size + padding * 0.5f, 0));
+ return vec2_add(size, Vec2(checkbox_size + padding * 0.5f, 0));
}
diff --git a/util.c b/util.c
index 9216bd1..f09da3c 100644
--- a/util.c
+++ b/util.c
@@ -635,88 +635,88 @@ i32 ceildivi32(i32 x, i32 y) {
}
}
-v2 V2(float x, float y) {
- v2 v;
+vec2 Vec2(float x, float y) {
+ vec2 v;
v.x = x;
v.y = y;
return v;
}
-v2 v2_add(v2 a, v2 b) {
- return V2(a.x + b.x, a.y + b.y);
+vec2 vec2_add(vec2 a, vec2 b) {
+ return Vec2(a.x + b.x, a.y + b.y);
}
-v2 v2_add_const(v2 a, float c) {
- return V2(a.x + c, a.y + c);
+vec2 vec2_add_const(vec2 a, float c) {
+ return Vec2(a.x + c, a.y + c);
}
-v2 v2_sub(v2 a, v2 b) {
- return V2(a.x - b.x, a.y - b.y);
+vec2 vec2_sub(vec2 a, vec2 b) {
+ return Vec2(a.x - b.x, a.y - b.y);
}
-v2 v2_scale(v2 v, float s) {
- return V2(v.x * s, v.y * s);
+vec2 vec2_scale(vec2 v, float s) {
+ return Vec2(v.x * s, v.y * s);
}
-v2 v2_mul(v2 a, v2 b) {
- return V2(a.x * b.x, a.y * b.y);
+vec2 vec2_mul(vec2 a, vec2 b) {
+ return Vec2(a.x * b.x, a.y * b.y);
}
-v2 v2_clamp(v2 x, v2 a, v2 b) {
- return V2(clampf(x.x, a.x, b.x), clampf(x.y, a.y, b.y));
+vec2 vec2_clamp(vec2 x, vec2 a, vec2 b) {
+ return Vec2(clampf(x.x, a.x, b.x), clampf(x.y, a.y, b.y));
}
-float v2_dot(v2 a, v2 b) {
+float vec2_dot(vec2 a, vec2 b) {
return a.x * b.x + a.y * b.y;
}
-float v2_len(v2 v) {
- return sqrtf(v2_dot(v, v));
+float vec2_len(vec2 v) {
+ return sqrtf(vec2_dot(v, v));
}
-v2 v2_lerp(float x, v2 a, v2 b) {
- return V2(lerpf(x, a.x, b.x), lerpf(x, a.y, b.y));
+vec2 vec2_lerp(float x, vec2 a, vec2 b) {
+ return Vec2(lerpf(x, a.x, b.x), lerpf(x, a.y, b.y));
}
// rotate v theta radians counterclockwise
-v2 v2_rotate(v2 v, float theta) {
+vec2 vec2_rotate(vec2 v, float theta) {
float c = cosf(theta), s = sinf(theta);
- return V2(
+ return Vec2(
c * v.x - s * v.y,
s * v.x + c * v.y
);
}
-v2 v2_normalize(v2 v) {
- float len = v2_len(v);
+vec2 vec2_normalize(vec2 v) {
+ float len = vec2_len(v);
float mul = len == 0.0f ? 1.0f : 1.0f/len;
- return v2_scale(v, mul);
+ return vec2_scale(v, mul);
}
-float v2_dist(v2 a, v2 b) {
- return v2_len(v2_sub(a, b));
+float vec2_dist(vec2 a, vec2 b) {
+ return vec2_len(vec2_sub(a, b));
}
-float v2_dist_squared(v2 a, v2 b) {
- v2 diff = v2_sub(a, b);
- return v2_dot(diff, diff);
+float vec2_dist_squared(vec2 a, vec2 b) {
+ vec2 diff = vec2_sub(a, b);
+ return vec2_dot(diff, diff);
}
-void v2_print(v2 v) {
+void vec2_print(vec2 v) {
printf("(%f, %f)\n", v.x, v.y);
}
-v2 v2_rand_unit(void) {
+vec2 vec2_rand_unit(void) {
float theta = rand_uniform(0, TAUf);
- return V2(cosf(theta), sinf(theta));
+ return Vec2(cosf(theta), sinf(theta));
}
-v2 v2_polar(float r, float theta) {
- return V2(r * cosf(theta), r * sinf(theta));
+vec2 vec2_polar(float r, float theta) {
+ return Vec2(r * cosf(theta), r * sinf(theta));
}
-v4 V4(float x, float y, float z, float w) {
- v4 v;
+vec4 Vec4(float x, float y, float z, float w) {
+ vec4 v;
v.x = x;
v.y = y;
v.z = z;
@@ -724,8 +724,8 @@ v4 V4(float x, float y, float z, float w) {
return v;
}
-v2d V2D(double x, double y) {
- return (v2d) {
+vec2d Vec2d(double x, double y) {
+ return (vec2d) {
.x = x,
.y = y
};
@@ -738,13 +738,13 @@ void rgba_u32_to_floats(u32 rgba, float floats[4]) {
floats[3] = (float)((rgba >> 0) & 0xFF) / 255.f;
}
-v4 rgba_u32_to_v4(u32 rgba) {
+vec4 rgba_u32_to_vec4(u32 rgba) {
float c[4];
rgba_u32_to_floats(rgba, c);
- return V4(c[0], c[1], c[2], c[3]);
+ return Vec4(c[0], c[1], c[2], c[3]);
}
-u32 rgba_v4_to_u32(v4 color) {
+u32 rgba_vec4_to_u32(vec4 color) {
return (u32)(color.x * 255) << 24
| (u32)(color.y * 255) << 16
| (u32)(color.z * 255) << 8
@@ -757,59 +757,59 @@ float rgba_brightness(u32 color) {
return ((float)r+(float)g+(float)b) * (1.0f / 3);
}
-bool rect_contains_point_v2(v2 pos, v2 size, v2 point) {
+bool rect_contains_point_v2(vec2 pos, vec2 size, vec2 point) {
float x1 = pos.x, y1 = pos.y, x2 = pos.x + size.x, y2 = pos.y + size.y,
x = point.x, y = point.y;
return x >= x1 && x < x2 && y >= y1 && y < y2;
}
-bool centered_rect_contains_point(v2 center, v2 size, v2 point) {
- return rect_contains_point_v2(v2_sub(center, v2_scale(size, 0.5f)), size, point);
+bool centered_rect_contains_point(vec2 center, vec2 size, vec2 point) {
+ return rect_contains_point_v2(vec2_sub(center, vec2_scale(size, 0.5f)), size, point);
}
-Rect rect(v2 pos, v2 size) {
+Rect rect(vec2 pos, vec2 size) {
Rect r;
r.pos = pos;
r.size = size;
return r;
}
-Rect rect_endpoints(v2 e1, v2 e2) {
+Rect rect_endpoints(vec2 e1, vec2 e2) {
Rect r;
r.pos = e1;
- r.size = v2_sub(e2, e1);
+ r.size = vec2_sub(e2, e1);
return r;
}
Rect rect4(float x1, float y1, float x2, float y2) {
assert(x2 >= x1);
assert(y2 >= y1);
- return rect(V2(x1,y1), V2(x2-x1, y2-y1));
+ return rect(Vec2(x1,y1), Vec2(x2-x1, y2-y1));
}
Rect rect_xywh(float x, float y, float w, float h) {
assert(w >= 0);
assert(h >= 0);
- return rect(V2(x, y), V2(w, h));
+ return rect(Vec2(x, y), Vec2(w, h));
}
-Rect rect_centered(v2 center, v2 size) {
+Rect rect_centered(vec2 center, vec2 size) {
Rect r;
- r.pos = v2_sub(center, v2_scale(size, 0.5f));
+ r.pos = vec2_sub(center, vec2_scale(size, 0.5f));
r.size = size;
return r;
}
-v2 rect_center(Rect r) {
- return v2_add(r.pos, v2_scale(r.size, 0.5f));
+vec2 rect_center(Rect r) {
+ return vec2_add(r.pos, vec2_scale(r.size, 0.5f));
}
-bool rect_contains_point(Rect r, v2 point) {
+bool rect_contains_point(Rect r, vec2 point) {
return rect_contains_point_v2(r.pos, r.size, point);
}
-Rect rect_translate(Rect r, v2 by) {
- return rect(v2_add(r.pos, by), r.size);
+Rect rect_translate(Rect r, vec2 by) {
+ return rect(vec2_add(r.pos, by), r.size);
}
float rect_x1(Rect r) { return r.pos.x; }
@@ -841,10 +841,10 @@ float rects_intersect(Rect r1, Rect r2) {
// returns whether or not there is any of the clipped rectangle left
bool rect_clip_to_rect(Rect *clipped, Rect clipper) {
- v2 start_pos = clipped->pos;
+ vec2 start_pos = clipped->pos;
clipped->pos.x = maxf(clipped->pos.x, clipper.pos.x);
clipped->pos.y = maxf(clipped->pos.y, clipper.pos.y);
- clipped->size = v2_add(clipped->size, v2_sub(start_pos, clipped->pos));
+ clipped->size = vec2_add(clipped->size, vec2_sub(start_pos, clipped->pos));
clipped->size.x = clampf(clipped->size.x, 0, clipper.pos.x + clipper.size.x - clipped->pos.x);
clipped->size.y = clampf(clipped->size.y, 0, clipper.pos.y + clipper.size.y - clipped->pos.y);
@@ -871,7 +871,7 @@ Rect rect_grow(Rect r, float amount) {
return r;
}
-v4 color_rgba_to_hsva(v4 rgba) {
+vec4 color_rgba_to_hsva(vec4 rgba) {
float R = rgba.x, G = rgba.y, B = rgba.z, A = rgba.w;
float M = maxf(R, maxf(G, B));
float m = minf(R, minf(G, B));
@@ -888,10 +888,10 @@ v4 color_rgba_to_hsva(v4 rgba) {
H *= 60;
float V = M;
float S = V == 0 ? 0 : C / V;
- return V4(H, S, V, A);
+ return Vec4(H, S, V, A);
}
-v4 color_hsva_to_rgba(v4 hsva) {
+vec4 color_hsva_to_rgba(vec4 hsva) {
float H = hsva.x, S = hsva.y, V = hsva.z, A = hsva.w;
H /= 60;
float C = S * V;
@@ -914,13 +914,13 @@ v4 color_hsva_to_rgba(v4 hsva) {
R += m;
G += m;
B += m;
- return V4(R, G, B, A);
+ return Vec4(R, G, B, A);
}
u32 color_interpolate(float x, u32 color1, u32 color2) {
x = x * x * (3 - 2*x); // hermite interpolation
- v4 c1 = rgba_u32_to_v4(color1), c2 = rgba_u32_to_v4(color2);
+ vec4 c1 = rgba_u32_to_vec4(color1), c2 = rgba_u32_to_vec4(color2);
// to make it interpolate more nicely, convert to hsv, interpolate in that space, then convert back
c1 = color_rgba_to_hsva(c1);
c2 = color_rgba_to_hsva(c2);
@@ -943,9 +943,9 @@ u32 color_interpolate(float x, u32 color1, u32 color2) {
}
h_out = fmodf(h_out, 360);
- v4 c_out = V4(h_out, s_out, v_out, a_out);
+ vec4 c_out = Vec4(h_out, s_out, v_out, a_out);
c_out = color_hsva_to_rgba(c_out);
- return rgba_v4_to_u32(c_out);
+ return rgba_vec4_to_u32(c_out);
}
diff --git a/util.h b/util.h
index 4cfcc04..adf71e0 100644
--- a/util.h
+++ b/util.h
@@ -1,6 +1,7 @@
#ifndef UTIL_H_
#define UTIL_H_
+#include "base.h"
// like snprintf, but not screwed up on windows
#define str_printf(str, size, ...) (str)[(size) - 1] = '\0', snprintf((str), (size) - 1, __VA_ARGS__)
@@ -26,16 +27,16 @@
typedef struct {
float x, y;
-} v2;
+} vec2;
typedef struct {
float x, y, z, w;
-} v4;
+} vec4;
typedef struct {
double x, y;
-} v2d;
+} vec2d;
typedef struct {
- v2 pos, size;
+ vec2 pos, size;
} Rect;
// UTF-32 string
@@ -158,46 +159,46 @@ u32 rand_u32(void);
float rand_uniform(float from, float to);
float sigmoidf(float x);
i32 ceildivi32(i32 x, i32 y);
-v2 V2(float x, float y);
-v2 v2_add(v2 a, v2 b);
-v2 v2_add_const(v2 a, float c);
-v2 v2_sub(v2 a, v2 b);
-v2 v2_scale(v2 v, float s);
-v2 v2_mul(v2 a, v2 b);
-v2 v2_clamp(v2 x, v2 a, v2 b);
-float v2_dot(v2 a, v2 b);
-float v2_len(v2 v);
-v2 v2_lerp(float x, v2 a, v2 b);
-v2 v2_rotate(v2 v, float theta);
-v2 v2_normalize(v2 v);
-float v2_dist(v2 a, v2 b);
-float v2_dist_squared(v2 a, v2 b);
-void v2_print(v2 v);
-v2 v2_rand_unit(void);
-v2 v2_polar(float r, float theta);
-v4 V4(float x, float y, float z, float w);
+vec2 Vec2(float x, float y);
+vec2 vec2_add(vec2 a, vec2 b);
+vec2 vec2_add_const(vec2 a, float c);
+vec2 vec2_sub(vec2 a, vec2 b);
+vec2 vec2_scale(vec2 v, float s);
+vec2 vec2_mul(vec2 a, vec2 b);
+vec2 vec2_clamp(vec2 x, vec2 a, vec2 b);
+float vec2_dot(vec2 a, vec2 b);
+float vec2_len(vec2 v);
+vec2 vec2_lerp(float x, vec2 a, vec2 b);
+vec2 vec2_rotate(vec2 v, float theta);
+vec2 vec2_normalize(vec2 v);
+float vec2_dist(vec2 a, vec2 b);
+float vec2_dist_squared(vec2 a, vec2 b);
+void vec2_print(vec2 v);
+vec2 vec2_rand_unit(void);
+vec2 vec2_polar(float r, float theta);
+vec4 Vec4(float x, float y, float z, float w);
void rgba_u32_to_floats(u32 rgba, float floats[4]);
-v4 rgba_u32_to_v4(u32 rgba);
-u32 rgba_v4_to_u32(v4 color);
+vec4 rgba_u32_to_vec4(u32 rgba);
+u32 rgba_vec4_to_u32(vec4 color);
float rgba_brightness(u32 color);
-bool rect_contains_point_v2(v2 pos, v2 size, v2 point);
-bool centered_rect_contains_point(v2 center, v2 size, v2 point);
-Rect rect(v2 pos, v2 size);
-Rect rect_endpoints(v2 e1, v2 e2);
+bool rect_contains_point_v2(vec2 pos, vec2 size, vec2 point);
+bool centered_rect_contains_point(vec2 center, vec2 size, vec2 point);
+Rect rect(vec2 pos, vec2 size);
+Rect rect_endpoints(vec2 e1, vec2 e2);
Rect rect4(float x1, float y1, float x2, float y2);
Rect rect_xywh(float x, float y, float w, float h);
-Rect rect_centered(v2 center, v2 size);
-v2 rect_center(Rect r);
-bool rect_contains_point(Rect r, v2 point);
-Rect rect_translate(Rect r, v2 by);
+Rect rect_centered(vec2 center, vec2 size);
+vec2 rect_center(Rect r);
+bool rect_contains_point(Rect r, vec2 point);
+Rect rect_translate(Rect r, vec2 by);
void rect_coords(Rect r, float *x1, float *y1, float *x2, float *y2);
void rect_print(Rect r);
float rects_intersect(Rect r1, Rect r2);
bool rect_clip_to_rect(Rect *clipped, Rect clipper);
Rect rect_shrink(Rect r, float amount);
Rect rect_grow(Rect r, float amount);
-v4 color_rgba_to_hsva(v4 rgba);
-v4 color_hsva_to_rgba(v4 hsva);
+vec4 color_rgba_to_hsva(vec4 rgba);
+vec4 color_hsva_to_rgba(vec4 hsva);
u32 color_interpolate(float x, u32 color1, u32 color2);
int timespec_cmp(struct timespec a, struct timespec b);
bool timespec_eq(struct timespec a, struct timespec b);
@@ -278,53 +279,53 @@ u32 rand_u32(void);
float rand_uniform(float from, float to);
float sigmoidf(float x);
i32 ceildivi32(i32 x, i32 y);
-v2 V2(float x, float y);
-v2 v2_add(v2 a, v2 b);
-v2 v2_add_const(v2 a, float c);
-v2 v2_sub(v2 a, v2 b);
-v2 v2_scale(v2 v, float s);
-v2 v2_mul(v2 a, v2 b);
-v2 v2_clamp(v2 x, v2 a, v2 b);
-float v2_dot(v2 a, v2 b);
-float v2_len(v2 v);
-v2 v2_lerp(float x, v2 a, v2 b);
-v2 v2_rotate(v2 v, float theta);
-v2 v2_normalize(v2 v);
-float v2_dist(v2 a, v2 b);
-float v2_dist_squared(v2 a, v2 b);
-void v2_print(v2 v);
-v2 v2_rand_unit(void);
-v2 v2_polar(float r, float theta);
-v4 V4(float x, float y, float z, float w);
-v2d V2D(double x, double y);
+vec2 Vec2(float x, float y);
+vec2 vec2_add(vec2 a, vec2 b);
+vec2 vec2_add_const(vec2 a, float c);
+vec2 vec2_sub(vec2 a, vec2 b);
+vec2 vec2_scale(vec2 v, float s);
+vec2 vec2_mul(vec2 a, vec2 b);
+vec2 vec2_clamp(vec2 x, vec2 a, vec2 b);
+float vec2_dot(vec2 a, vec2 b);
+float vec2_len(vec2 v);
+vec2 vec2_lerp(float x, vec2 a, vec2 b);
+vec2 vec2_rotate(vec2 v, float theta);
+vec2 vec2_normalize(vec2 v);
+float vec2_dist(vec2 a, vec2 b);
+float vec2_dist_squared(vec2 a, vec2 b);
+void vec2_print(vec2 v);
+vec2 vec2_rand_unit(void);
+vec2 vec2_polar(float r, float theta);
+vec4 Vec4(float x, float y, float z, float w);
+vec2d Vec2d(double x, double y);
void rgba_u32_to_floats(u32 rgba, float floats[4]);
-v4 rgba_u32_to_v4(u32 rgba);
-u32 rgba_v4_to_u32(v4 color);
+vec4 rgba_u32_to_vec4(u32 rgba);
+u32 rgba_vec4_to_u32(vec4 color);
float rgba_brightness(u32 color);
-bool rect_contains_point_v2(v2 pos, v2 size, v2 point);
-bool centered_rect_contains_point(v2 center, v2 size, v2 point);
-Rect rect(v2 pos, v2 size);
-Rect rect_endpoints(v2 e1, v2 e2);
+bool rect_contains_point_v2(vec2 pos, vec2 size, vec2 point);
+bool centered_rect_contains_point(vec2 center, vec2 size, vec2 point);
+Rect rect(vec2 pos, vec2 size);
+Rect rect_endpoints(vec2 e1, vec2 e2);
Rect rect4(float x1, float y1, float x2, float y2);
Rect rect_xywh(float x, float y, float w, float h);
-Rect rect_centered(v2 center, v2 size);
+Rect rect_centered(vec2 center, vec2 size);
float rect_x1(Rect r);
float rect_y1(Rect r);
float rect_x2(Rect r);
float rect_y2(Rect r);
float rect_xmid(Rect r);
float rect_ymid(Rect r);
-v2 rect_center(Rect r);
-bool rect_contains_point(Rect r, v2 point);
-Rect rect_translate(Rect r, v2 by);
+vec2 rect_center(Rect r);
+bool rect_contains_point(Rect r, vec2 point);
+Rect rect_translate(Rect r, vec2 by);
void rect_coords(Rect r, float *x1, float *y1, float *x2, float *y2);
void rect_print(Rect r);
float rects_intersect(Rect r1, Rect r2);
bool rect_clip_to_rect(Rect *clipped, Rect clipper);
Rect rect_shrink(Rect r, float amount);
Rect rect_grow(Rect r, float amount);
-v4 color_rgba_to_hsva(v4 rgba);
-v4 color_hsva_to_rgba(v4 hsva);
+vec4 color_rgba_to_hsva(vec4 rgba);
+vec4 color_hsva_to_rgba(vec4 hsva);
u32 color_interpolate(float x, u32 color1, u32 color2);
int timespec_cmp(struct timespec a, struct timespec b);
bool timespec_eq(struct timespec a, struct timespec b);