cepheid

An Aurora 4X clone
Log | Files | Refs | README

commit 59885cfa5a0d3bf69005f57d7c2dc0b9b40ed974
parent 717a21586094d954f42683e39f66be2abd820371
Author: hhvn <dev@hhvn.uk>
Date:   Sun, 13 Nov 2022 18:38:01 +0000

Move coordinate handling to coords.c [new]

Diffstat:
Asrc/coords.c | 49+++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/gui.c | 24++++++++++++------------
Msrc/main.h | 24++++++++----------------
Msrc/pane.c | 10+++++-----
Msrc/struct.h | 12+++++++-----
Msrc/system.c | 49++++---------------------------------------------
Msrc/ui.c | 30+++++++++++++++---------------
Msrc/views/bodies.c | 2+-
Msrc/views/main.c | 26+++++++++++++-------------
Msrc/views/struct.h | 4++--
10 files changed, 116 insertions(+), 114 deletions(-)

diff --git a/src/coords.c b/src/coords.c @@ -0,0 +1,49 @@ +#include <math.h> +#include "main.h" + +Vector +vectorize(Polar p) { + return (Vector) { + p.r * cosf_d(p.theta), + p.r * sinf_d(p.theta) + }; +} + +Vector +vectorize_at(Vector at, Polar p) { + Vector r = vectorize(p); + return (Vector) { + at.x + r.x, + at.y + r.y + }; +} + +Polar +polarize(Vector v) { + return (Polar) { + hypotf(v.x, v.y), + atan2f_d(v.y, v.x) + }; +} + +Polar +polarize_at(Vector at, Vector vector) { + Vector v = {vector.y - at.y, vector.x - at.x}; + return polarize(v); +} + +#define SQ(x) (x * x) + +/* The previous version of this function utilized other functions in this file, + * however by avoiding the passing around of structs, this version is approx. + * 3x faster */ +Polar +polar_add(Polar abs, Polar rel) { + return (Polar) { + sqrtf(SQ(abs.r) + SQ(rel.r) + 2 * abs.r * rel.r * cosf_d(abs.theta - rel.theta)), + abs.theta + atan2f_d( + rel.r * sinf_d(rel.theta - abs.theta), + abs.r + rel.r * cosf_d(rel.theta - abs.theta) + ) + }; +} diff --git a/src/gui.c b/src/gui.c @@ -4,20 +4,20 @@ static Clickable clickable[CLICKABLE_MAX]; static int clickablei = 0; -static void gui_click_tabs(Vector2 mouse, +static void gui_click_tabs(Vector mouse, MouseButton button, Geom *geom, void *elem); -static void gui_click_checkbox(Vector2 mouse, +static void gui_click_checkbox(Vector mouse, MouseButton button, Geom *geom, void *elem); -static void gui_click_input(Vector2 mouse, +static void gui_click_input(Vector mouse, MouseButton button, Geom *geom, void *elem); -static void gui_click_dropdown(Vector2 mouse, +static void gui_click_dropdown(Vector mouse, MouseButton button, Geom *geom, void *elem); -static void gui_click_treeview(Vector2 mouse, +static void gui_click_treeview(Vector mouse, MouseButton button, Geom *geom, void *elem); static void gui_key_input(void *elem, int *fcount); -static void (*click_handlers[GUI_ELEMS])(Vector2 mouse, +static void (*click_handlers[GUI_ELEMS])(Vector mouse, MouseButton button, Geom *geom, void *elem) = { [GUI_TAB] = gui_click_tabs, @@ -50,7 +50,7 @@ gui_click_register(Geom geom, enum GuiElements type, void *elem) { int gui_click_handle(void) { - Vector2 mouse; + Vector mouse; MouseButton button; Geom *geom; int i; @@ -150,7 +150,7 @@ gui_tabs(int x, int y, int w, int h, Tabs *tabs) { } static void -gui_click_tabs(Vector2 mouse, MouseButton button, Geom *geom, void *elem) { +gui_click_tabs(Vector mouse, MouseButton button, Geom *geom, void *elem) { int ftabw, fw, fn; int tabw, x, i; Tabs *tabs = elem; @@ -196,7 +196,7 @@ gui_checkbox(int x, int y, Checkbox *box) { } static void -gui_click_checkbox(Vector2 mouse, MouseButton button, Geom *geom, void *elem) { +gui_click_checkbox(Vector mouse, MouseButton button, Geom *geom, void *elem) { Checkbox *checkbox = elem; if (button != MOUSE_BUTTON_LEFT) @@ -238,7 +238,7 @@ gui_dropdown(int x, int y, int w, Dropdown *d) { } static void -gui_click_dropdown(Vector2 mouse, MouseButton button, Geom *geom, void *elem) { +gui_click_dropdown(Vector mouse, MouseButton button, Geom *geom, void *elem) { Dropdown *drop = elem; int i; @@ -282,7 +282,7 @@ gui_input(int x, int y, int w, Input *in) { } static void -gui_click_input(Vector2 mouse, MouseButton button, Geom *geom, void *elem) { +gui_click_input(Vector mouse, MouseButton button, Geom *geom, void *elem) { Input *input = elem; int i; @@ -381,7 +381,7 @@ gui_treeview(int x, int y, int w, int h, Treeview *tv) { } static void -gui_click_treeview(Vector2 mouse, MouseButton button, Geom *geom, void *elem) { +gui_click_treeview(Vector mouse, MouseButton button, Geom *geom, void *elem) { Treeview *tv = elem; int depth; Tree *p; diff --git a/src/main.h b/src/main.h @@ -49,7 +49,6 @@ Vector vectorize_at(Vector at, Polar p); Polar polarize(Vector v); Polar polarize_at(Vector at, Vector vector); Polar polar_add(Polar abs, Polar rel); -Polar polar_add_noconv(Polar abs, Polar rel); /* tree.c */ Tree * tree_add_child(Tree *t, char *name, int type, void *data, Tree **ptr); @@ -91,10 +90,10 @@ void ui_title(char *fmt, ...); int ui_textsize(char *text); float ui_get_scroll(void); int ui_checkbox_size(Checkbox *checkbox); -int ui_collides(Geom geom, Vector2 point); -int ui_onscreen(Vector2 point); -int ui_onscreen_ring(Vector2 centre, float r); -int ui_onscreen_circle(Vector2 centre, float r); +int ui_collides(Geom geom, Vector point); +int ui_onscreen(Vector point); +int ui_onscreen_ring(Vector centre, float r); +int ui_onscreen_circle(Vector centre, float r); int ui_keyboard_check(int key, int *fcount); void ui_keyboard_handle(void); void ui_draw_views(void); @@ -105,10 +104,10 @@ void ui_draw_ring(int x, int y, float r, Color col); void ui_draw_texture(Texture2D texture, int x, int y); void ui_draw_circle(int x, int y, float r, Color col); void ui_draw_line(int sx, int sy, int ex, int ey, float thick, Color col); -void ui_draw_line_v(Vector2 start, Vector2 end, float thick, Color col); +void ui_draw_line_v(Vector start, Vector end, float thick, Color col); void ui_draw_tabbed_window(int x, int y, int w, int h, Tabs *tabs); -Vector2 ui_vectordiff(Vector2 a, Vector2 b); -float ui_vectordist(Vector2 a, Vector2 b); +Vector ui_vectordiff(Vector a, Vector b); +float ui_vectordist(Vector a, Vector b); void ui_handle_view_colonies(int nowsel); void ui_handle_view_fleets(int nowsel); void ui_handle_view_design(int nowsel); @@ -145,16 +144,9 @@ void pane_end(void); int pane_visible(float miny, float maxy); /* calls pane_max automatically */ float pane_max(float y); /* returns original y */ float pane_y(float y); -Vector2 pane_v(Vector2 v); +Vector pane_v(Vector v); /* system.c */ -Vector2 sys_vectorize(Polar polar); -Vector2 sys_vectorize_around(Vector2 around, Polar polar); -Polar sys_polarize(Vector2 vector); -Polar sys_polarize_around(Vector2 around, Vector2 vector); -Polar sys_sum_polar(Polar absolute, Polar relative); -Vector2 sys_get_vector(Body *body); -Polar sys_get_polar(Body *body); System *sys_init(char *name); void sys_tree_load(void); char * sys_tree_getter(char *dir, char *group, char *name, int depth, Tree *t); diff --git a/src/pane.c b/src/pane.c @@ -17,7 +17,7 @@ pane_begin(Pane *f) { void pane_end(void) { Pane *f = pane; - Vector2 m = GetMousePosition(); + Vector m = GetMousePosition(); if (!f) return; @@ -69,9 +69,9 @@ pane_y(float y) { return y - pane->off; } -Vector2 -pane_v(Vector2 v) { +Vector +pane_v(Vector v) { if (!pane) - return (Vector2) {v.x, v.y}; - return (Vector2) {v.x, v.y - pane->off}; + return (Vector) {v.x, v.y}; + return (Vector) {v.x, v.y - pane->off}; } diff --git a/src/struct.h b/src/struct.h @@ -24,6 +24,8 @@ typedef void (*Treesetter)(char *dir, char *group, char *name, int depth, Tree * typedef int (*Treefilter)(Tree *t, void *data); typedef void (*Treeprinter)(int x, int y, Treeview *tv, Tree *t); +/* coords.c */ +typedef Vector2 Vector; typedef struct { float r; float theta; @@ -61,8 +63,8 @@ struct Body { char *pname; /* used during sys_tree_load() */ }; Polar polar; - Vector2 vector; - Vector2 pxloc; /* used by ui functions */ + Vector vector; + Vector pxloc; /* used by ui functions */ char *name; enum BodyType type; float radius; @@ -97,7 +99,7 @@ typedef struct { int comets; int moons; } num; - Vector2 lypos; + Vector lypos; } System; /* save.c */ @@ -126,7 +128,7 @@ typedef struct { int w, h; }; struct { - Vector2 centre; + Vector centre; int r; }; }; @@ -145,7 +147,7 @@ typedef struct { typedef struct { float w, h; float diag; - Vector2 centre; + Vector centre; Geom rect; /* for passing to functions */ } Screen; diff --git a/src/system.c b/src/system.c @@ -4,48 +4,7 @@ #include <raylib.h> #include "main.h" -Vector2 -sys_vectorize(Polar polar) { - return (Vector2) { - polar.r * cosf_d(polar.theta), - polar.r * sinf_d(polar.theta) - }; -} - -Vector2 -sys_vectorize_around(Vector2 around, Polar polar) { - Vector2 relative = sys_vectorize(polar); - return (Vector2) { - around.x + relative.x, - around.y + relative.y - }; -} - -Polar -sys_polarize(Vector2 vector) { - return (Polar) { - hypotf(vector.x, vector.y), - atan2f_d(vector.y, vector.x) - }; -} - -Polar -sys_polarize_around(Vector2 around, Vector2 vector) { - Vector2 v = {vector.y - around.y, vector.x - around.x}; - return sys_polarize(v); -} - -Polar -sys_sum_polar(Polar absolute, Polar relative) { - return sys_polarize(sys_vectorize_around(sys_vectorize(absolute), relative)); -} - -Vector2 -sys_get_vector(Body *body) { - return sys_vectorize(sys_get_polar(body)); -} - -Polar +static Polar sys_get_polar(Body *body) { Polar polar; @@ -58,7 +17,7 @@ sys_get_polar(Body *body) { polar.r = body->curdist; polar.theta = body->theta; } else { - polar = sys_sum_polar(sys_get_polar(body->parent), + polar = polar_add(sys_get_polar(body->parent), (Polar){body->curdist, body->theta}); } } else { @@ -66,7 +25,7 @@ sys_get_polar(Body *body) { polar.r = body->dist; polar.theta = body->curtheta; } else { - polar = sys_sum_polar(sys_get_polar(body->parent), + polar = polar_add(sys_get_polar(body->parent), (Polar){body->dist, body->curtheta}); } } @@ -134,7 +93,7 @@ sys_tree_load(void) { sys_get_polar(bp[i]); /* Builds the cache for us: this is more efficient as it can cache the parent too */ - bp[i]->vector = sys_vectorize(bp[i]->polar); + bp[i]->vector = vectorize(bp[i]->polar); /* This could deal with moons, but that's probably not * useful. What about multiple stars in a system? That diff --git a/src/ui.c b/src/ui.c @@ -105,7 +105,7 @@ ui_deinit(void) { void ui_print(int x, int y, Color col, char *fmt, ...) { va_list ap; - Vector2 pos; + Vector pos; char *text; if (!pane_visible(y, y + FONT_SIZE)) @@ -146,7 +146,7 @@ ui_get_scroll(void) { } int -ui_collides(Geom geom, Vector2 point) { +ui_collides(Geom geom, Vector point) { switch (geom.type) { case UI_CIRCLE: return CheckCollisionPointCircle(point, @@ -157,7 +157,7 @@ ui_collides(Geom geom, Vector2 point) { } int -ui_onscreen(Vector2 point) { +ui_onscreen(Vector point) { if (!pane_visible(point.y, point.y)) return 0; point.y = pane_y(point.y); @@ -165,7 +165,7 @@ ui_onscreen(Vector2 point) { } int -ui_onscreen_ring(Vector2 centre, float r) { +ui_onscreen_ring(Vector centre, float r) { float d = ui_vectordist(centre, screen.centre); if (!pane_visible(centre.y - r, centre.y + r)) @@ -178,7 +178,7 @@ ui_onscreen_ring(Vector2 centre, float r) { } int -ui_onscreen_circle(Vector2 centre, float r) { +ui_onscreen_circle(Vector centre, float r) { if (!pane_visible(centre.y - r, centre.y + r)) return 0; centre.y = pane_y(centre.y); @@ -238,7 +238,7 @@ ui_draw_rectangle(int x, int y, int w, int h, Color col) { void ui_draw_ring(int x, int y, float r, Color col) { - Vector2 v = {x, y}; + Vector v = {x, y}; Polar p; float s; float prec = screen.diag * 1.5 / (PI * 2 * r) * 360; @@ -251,7 +251,7 @@ ui_draw_ring(int x, int y, float r, Color col) { if (!pane_visible(v.y - r, v.y + r)) return; - p = sys_polarize_around(v, screen.centre); + p = polarize_at(v, screen.centre); deg = p.theta; /* Draw the section of the ring (+ wriggle room) that will be onscreen @@ -288,13 +288,13 @@ ui_draw_circle(int x, int y, float r, Color col) { void ui_draw_line(int sx, int sy, int ex, int ey, float thick, Color col) { - Vector2 s = {sx, sy}; - Vector2 e = {ex, ey}; + Vector s = {sx, sy}; + Vector e = {ex, ey}; ui_draw_line_v(s, e, thick, col); } void -ui_draw_line_v(Vector2 start, Vector2 end, float thick, Color col) { +ui_draw_line_v(Vector start, Vector end, float thick, Color col) { DrawLineEx(pane_v(start), pane_v(end), thick, col); } @@ -310,20 +310,20 @@ ui_draw_border_around(int x, int y, int w, int h, int px) { ui_draw_border(x - px, y - px, w + px * 2, h + px * 2, px); } -Vector2 -ui_vectordiff(Vector2 a, Vector2 b) { +Vector +ui_vectordiff(Vector a, Vector b) { float x = a.x - b.x; float y = a.y - b.y; if (x < 0) x *= -1; if (y < 0) y *= -1; - return (Vector2) {x, y}; + return (Vector) {x, y}; } float -ui_vectordist(Vector2 a, Vector2 b) { - Vector2 diff = ui_vectordiff(a, b); +ui_vectordist(Vector a, Vector b) { + Vector diff = ui_vectordiff(a, b); return sqrtf(diff.x * diff.x + diff.y * diff.y); } diff --git a/src/views/bodies.c b/src/views/bodies.c @@ -79,7 +79,7 @@ tree_printer(int x, int y, Treeview *tv, Tree *t) { void ui_handle_view_bodies(int nowsel) { - Vector2 m = GetMousePosition(); + Vector m = GetMousePosition(); Tree *t; Body *body; int pos, i; diff --git a/src/views/main.c b/src/views/main.c @@ -51,17 +51,17 @@ View_main view_main = { .sys = NULL, }; -Vector2 -kmtopx(Vector2 km) { - return (Vector2) { +Vector +kmtopx(Vector km) { + return (Vector) { (GetScreenWidth() / 2) + (km.x - view_main.kmx) / view_main.kmperpx, (GetScreenHeight() / 2) + (km.y - view_main.kmy) / view_main.kmperpx }; } -Vector2 -pxtokm(Vector2 vector) { - return (Vector2) { +Vector +pxtokm(Vector vector) { + return (Vector) { ((vector.x - GetScreenWidth() / 2) * view_main.kmperpx) + view_main.kmx, ((vector.y - GetScreenHeight() / 2) * view_main.kmperpx) + view_main.kmy }; @@ -69,8 +69,8 @@ pxtokm(Vector2 vector) { void ui_handle_view_main(int nowsel) { - Vector2 mouse = GetMousePosition(); - Vector2 delta = GetMouseDelta(); + Vector mouse = GetMousePosition(); + Vector delta = GetMouseDelta(); float wheel = ui_get_scroll(); float diff; Body *furth; @@ -131,7 +131,7 @@ should_draw_body_checkbox(Body *body, int type, Checkbox *box) { static void draw_orbit(Body *body) { - Vector2 parent; + Vector parent; float pxrad; if (!body->parent) @@ -179,7 +179,7 @@ draw_body(Body *body) { ui_draw_circle(body->pxloc.x, body->pxloc.y, w, col_body[body->type]); if (body->type == BODY_COMET && view_main.infobox.comettail.val && 10 * view_main.kmperpx < body->curdist) - ui_draw_line_v(body->pxloc, sys_vectorize_around(body->pxloc, + ui_draw_line_v(body->pxloc, vectorize_at(body->pxloc, (Polar){w * 11 / min_body_rad[BODY_COMET], body->inward ? body->theta : body->theta + 180}), w / min_body_rad[BODY_COMET], col_body[BODY_COMET]); @@ -217,9 +217,9 @@ draw_body(Body *body) { void ui_draw_view_main(void) { - Vector2 mouse = GetMousePosition(); - Vector2 mousekm = pxtokm(mouse); - Vector2 ruler; + Vector mouse = GetMousePosition(); + Vector mousekm = pxtokm(mouse); + Vector ruler; Geom geom; Tree *t; Body *body; diff --git a/src/views/struct.h b/src/views/struct.h @@ -20,7 +20,7 @@ typedef struct { int pan; struct { int held; - Vector2 origin; + Vector origin; } ruler; float kmx, kmy; float kmperpx; @@ -56,7 +56,7 @@ typedef struct { Geom geom; } info; int pan; - Vector2 off; + Vector off; float lytopx; System *sel; } View_sys;