Skip to content

Commit

Permalink
Refactor -- code that calculates the minimum and maximum of given coo…
Browse files Browse the repository at this point in the history
…rdinates has been moved to a function.

- Also added a function to convert vertexes to points.
- Also added a constructor for point that takes in a single number, so both coordinates are filled in, as well as adding some missing arithmetic functions.
  • Loading branch information
Espyo committed Jan 22, 2025
1 parent a937971 commit ee15d65
Show file tree
Hide file tree
Showing 40 changed files with 382 additions and 384 deletions.
2 changes: 1 addition & 1 deletion source/source/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ void animation_database::calculate_hitbox_span() {
for(size_t h = 0; h < s_ptr->hitboxes.size(); h++) {
hitbox* h_ptr = &s_ptr->hitboxes[h];

float d = dist(point(0, 0), h_ptr->pos).to_float();
float d = dist(point(0.0f), h_ptr->pos).to_float();
d += h_ptr->radius;
hitbox_span = std::max(hitbox_span, d);
}
Expand Down
2 changes: 1 addition & 1 deletion source/source/animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class sprite {
point offset;

//Scale multiplier.
point scale = point(1.0, 1.0);
point scale = point(1.0f);

//Angle to rotate the image by.
float angle = 0.0f;
Expand Down
38 changes: 12 additions & 26 deletions source/source/area/area.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -735,16 +735,12 @@ void area_data::generate_blockmap() {
if(vertexes.empty()) return;

//First, get the starting point and size of the blockmap.
point min_coords, max_coords;
min_coords.x = max_coords.x = vertexes[0]->x;
min_coords.y = max_coords.y = vertexes[0]->y;
point min_coords = v2p(vertexes[0]);
point max_coords = min_coords;

for(size_t v = 0; v < vertexes.size(); v++) {
vertex* v_ptr = vertexes[v];
min_coords.x = std::min(v_ptr->x, min_coords.x);
max_coords.x = std::max(v_ptr->x, max_coords.x);
min_coords.y = std::min(v_ptr->y, min_coords.y);
max_coords.y = std::max(v_ptr->y, max_coords.y);
update_min_max_coords(min_coords, max_coords, v2p(v_ptr));
}

bmap.top_left_corner = min_coords;
Expand Down Expand Up @@ -834,24 +830,15 @@ void area_data::generate_edges_blockmap(const vector<edge*> &edge_list) {
//and only then thoroughly test which it is inside of.

edge* e_ptr = edge_list[e];
point min_coords = v2p(e_ptr->vertexes[0]);
point max_coords = min_coords;
update_min_max_coords(min_coords, max_coords, v2p(e_ptr->vertexes[1]));

size_t b_min_x = bmap.get_col(min_coords.x);
size_t b_max_x = bmap.get_col(max_coords.x);
size_t b_min_y = bmap.get_row(min_coords.y);
size_t b_max_y = bmap.get_row(max_coords.y);

size_t b_min_x =
bmap.get_col(
std::min(e_ptr->vertexes[0]->x, e_ptr->vertexes[1]->x)
);
size_t b_max_x =
bmap.get_col(
std::max(e_ptr->vertexes[0]->x, e_ptr->vertexes[1]->x)
);
size_t b_min_y =
bmap.get_row(
std::min(e_ptr->vertexes[0]->y, e_ptr->vertexes[1]->y)
);
size_t b_max_y =
bmap.get_row(
std::max(e_ptr->vertexes[0]->y, e_ptr->vertexes[1]->y)
);

for(size_t bx = b_min_x; bx <= b_max_x; bx++) {
for(size_t by = b_min_y; by <= b_max_y; by++) {

Expand All @@ -863,8 +850,7 @@ void area_data::generate_edges_blockmap(const vector<edge*> &edge_list) {
line_seg_intersects_rectangle(
corner,
corner + GEOMETRY::BLOCKMAP_BLOCK_SIZE,
point(e_ptr->vertexes[0]->x, e_ptr->vertexes[0]->y),
point(e_ptr->vertexes[1]->x, e_ptr->vertexes[1]->y)
v2p(e_ptr->vertexes[0]), v2p(e_ptr->vertexes[1])
)
) {

Expand Down
4 changes: 2 additions & 2 deletions source/source/area/area.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ struct tree_shadow {
//--- Function declarations ---

explicit tree_shadow(
const point &center = point(), const point &size = point(100, 100),
const point &center = point(), const point &size = point(100.0f),
float angle = 0, unsigned char alpha = 255,
const string &file_name = "", const point &sway = point(1, 1)
const string &file_name = "", const point &sway = point(1.0f)
);
~tree_shadow();

Expand Down
10 changes: 2 additions & 8 deletions source/source/area/geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,8 @@ void polygon::clean(bool recursive) {
//this is just a redundant point in the edge prev - next. Delete it.
if(
fabs(
get_angle(
point(cur_v->x, cur_v->y),
point(prev_v->x, prev_v->y)
) -
get_angle(
point(next_v->x, next_v->y),
point(cur_v->x, cur_v->y)
)
get_angle(v2p(cur_v), v2p(prev_v)) -
get_angle(v2p(next_v), v2p(cur_v))
) < 0.000001
) {
should_delete = true;
Expand Down
18 changes: 5 additions & 13 deletions source/source/area/sector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "sector.h"

#include "../game.h"
#include "../functions.h"
#include "geometry.h"


Expand Down Expand Up @@ -59,22 +60,13 @@ void sector::calculate_bounding_box() {
return;
}

bbox[0].x = edges[0]->vertexes[0]->x;
bbox[1].x = bbox[0].x;
bbox[0].y = edges[0]->vertexes[0]->y;
bbox[1].y = bbox[0].y;
bbox[0] = v2p(edges[0]->vertexes[0]);
bbox[1] = bbox[0];

for(size_t e = 0; e < edges.size(); e++) {
for(unsigned char v = 0; v < 2; v++) {
point coords(
edges[e]->vertexes[v]->x,
edges[e]->vertexes[v]->y
);

bbox[0].x = std::min(bbox[0].x, coords.x);
bbox[1].x = std::max(bbox[1].x, coords.x);
bbox[0].y = std::min(bbox[0].y, coords.y);
bbox[1].y = std::max(bbox[1].y, coords.y);
point p = v2p(edges[e]->vertexes[v]);
update_min_max_coords(bbox[0], bbox[1], p);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion source/source/area/sector.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct sector_texture_t {
//--- Members ---

//Texture scale.
point scale = point(1.0f, 1.0f);
point scale = point(1.0f);

//Texture translation.
point translation;
Expand Down
12 changes: 6 additions & 6 deletions source/source/drawing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ void draw_button(
text, font, center, size * GUI::STANDARD_CONTENT_SIZE, color,
ALLEGRO_ALIGN_CENTER, V_ALIGN_MODE_CENTER,
TEXT_SETTING_FLAG_CANT_GROW,
point(1.0 + juicy_grow_amount, 1.0 + juicy_grow_amount)
point(1.0 + juicy_grow_amount)
);

ALLEGRO_COLOR box_tint =
Expand Down Expand Up @@ -246,7 +246,7 @@ void draw_fraction(
i2s(value_nr), game.sys_assets.fnt_value, point(bottom.x, value_nr_y),
point(LARGE_FLOAT, IN_WORLD_FRACTION::ROW_HEIGHT * scale),
color, ALLEGRO_ALIGN_CENTER, V_ALIGN_MODE_TOP, 0,
point(value_nr_scale, value_nr_scale)
point(value_nr_scale)
);

const float bar_y = bottom.y - IN_WORLD_FRACTION::ROW_HEIGHT * 2;
Expand All @@ -263,7 +263,7 @@ void draw_fraction(
point(bottom.x, req_nr_y),
point(LARGE_FLOAT, IN_WORLD_FRACTION::ROW_HEIGHT * scale),
color, ALLEGRO_ALIGN_CENTER, V_ALIGN_MODE_TOP, 0,
point(req_nr_scale, req_nr_scale)
point(req_nr_scale)
);
}

Expand Down Expand Up @@ -807,7 +807,7 @@ void draw_menu_button_icon(
);
draw_bitmap_in_box(
bmp, icon_center,
point(button_size.y, button_size.y),
point(button_size.y),
true
);
al_destroy_bitmap(bmp);
Expand All @@ -827,7 +827,7 @@ void draw_mob_shadow(
float delta_z, float shadow_stretch
) {

point shadow_size = point(m->radius * 2.2f, m->radius * 2.2f);
point shadow_size = point(m->radius * 2.2f);
if(m->rectangular_dim.x != 0) {
shadow_size = m->rectangular_dim * 1.1f;
}
Expand Down Expand Up @@ -1308,7 +1308,7 @@ void draw_string_tokens(
case STRING_TOKEN_CHAR: {
draw_text(
tokens[t].content, text_font, point(caret, where.y),
point(LARGE_FLOAT, LARGE_FLOAT), COLOR_WHITE,
point(LARGE_FLOAT), COLOR_WHITE,
ALLEGRO_ALIGN_LEFT, V_ALIGN_MODE_TOP,
TEXT_SETTING_FLAG_CANT_GROW,
point(x_scale * scale.x, y_scale * scale.y)
Expand Down
2 changes: 1 addition & 1 deletion source/source/drawing.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ void draw_string_tokens(
const vector<string_token> &tokens, const ALLEGRO_FONT* const text_font,
const ALLEGRO_FONT* const control_font, bool controls_condensed,
const point &where, int flags, const point &max_size,
const point &scale = point(1.0f, 1.0f)
const point &scale = point(1.0f)
);
void get_player_input_icon_info(
const player_input &i, bool condensed,
Expand Down
38 changes: 8 additions & 30 deletions source/source/edge_offset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ void get_edge_offset_intersection(
effect1_p0.y + (effect1_p1.y - effect1_p0.y) * r
);
coordinates_to_angle(
p - point(common_vertex->x, common_vertex->y),
p - v2p(common_vertex),
out_angle, out_length
);
} else {
Expand Down Expand Up @@ -525,11 +525,7 @@ void get_next_edge(
unsigned char other_vertex_idx = e_ptr->vertexes[0] == v_ptr ? 1 : 0;
vertex* other_vertex = e_ptr->vertexes[other_vertex_idx];

float angle =
get_angle(
point(v_ptr->x, v_ptr->y),
point(other_vertex->x, other_vertex->y)
);
float angle = get_angle(v2p(v_ptr), v2p(other_vertex));

float diff =
clockwise ?
Expand Down Expand Up @@ -601,11 +597,7 @@ void get_next_offset_effect_edge(
//to what side does the effect go?
bool effect_is_cw = other_vertex_idx != unaffected_sector_idx;

float angle =
get_angle(
point(v_ptr->x, v_ptr->y),
point(other_vertex->x, other_vertex->y)
);
float angle = get_angle(v2p(v_ptr), v2p(other_vertex));

float diff =
clockwise ?
Expand Down Expand Up @@ -679,25 +671,11 @@ void update_offset_effect_buffer(
if(!fully_on_camera) {
//If the sector's fully on-camera, it's faster to not bother
//with the edge-by-edge check.
point edge_tl(
std::min(
s_ptr->edges[e]->vertexes[0]->x,
s_ptr->edges[e]->vertexes[1]->x
),
std::min(
s_ptr->edges[e]->vertexes[0]->y,
s_ptr->edges[e]->vertexes[1]->y
)
);
point edge_br(
std::max(
s_ptr->edges[e]->vertexes[0]->x,
s_ptr->edges[e]->vertexes[1]->x
),
std::max(
s_ptr->edges[e]->vertexes[0]->y,
s_ptr->edges[e]->vertexes[1]->y
)
point edge_tl = v2p(s_ptr->edges[e]->vertexes[0]);
point edge_br = edge_tl;
update_min_max_coords(
edge_tl, edge_br,
v2p(s_ptr->edges[e]->vertexes[1])
);

if(!rectangles_intersect(edge_tl, edge_br, cam_tl, cam_br)) {
Expand Down
34 changes: 23 additions & 11 deletions source/source/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ bool are_walls_between(
const point &p1, const point &p2,
float ignore_walls_below_z, bool* out_impassable_walls
) {
point bb_tl(std::min(p1.x, p2.x), std::min(p1.y, p2.y));
point bb_br(std::max(p1.x, p2.x), std::max(p1.y, p2.y));
point bb_tl = p1;
point bb_br = p1;
update_min_max_coords(bb_tl, bb_br, p2);

set<edge*> candidate_edges;
if(
Expand All @@ -73,8 +74,7 @@ bool are_walls_between(
if(
!line_segs_intersect(
p1, p2,
point(e_ptr->vertexes[0]->x, e_ptr->vertexes[0]->y),
point(e_ptr->vertexes[1]->x, e_ptr->vertexes[1]->y),
v2p(e_ptr->vertexes[0]), v2p(e_ptr->vertexes[1]),
nullptr
)
) {
Expand Down Expand Up @@ -474,9 +474,10 @@ float get_liquid_limit_length(edge* e_ptr) {
//because that would result in many cases of edges that share a first
//vertex. So it wouldn't look as random.
//It is much more rare for two edges to share a topleftmost vertex.
float min_x = std::min(e_ptr->vertexes[0]->x, e_ptr->vertexes[1]->x);
float min_y = std::min(e_ptr->vertexes[0]->y, e_ptr->vertexes[1]->y);
float r = (hash_nr2(min_x, min_y) / (float) UINT32_MAX) * 5.0f;
point min_coords = v2p(e_ptr->vertexes[0]);
update_min_coords(min_coords, v2p(e_ptr->vertexes[1]));
float r =
(hash_nr2(min_coords.x, min_coords.y) / (float) UINT32_MAX) * 5.0f;
return
15.0f +
12.0f * sin((game.states.gameplay->area_time_passed * 2.0f) + r);
Expand Down Expand Up @@ -740,12 +741,12 @@ vector<std::pair<int, string> > get_weather_table(data_node* node) {

/**
* @brief Returns the path to the program's current working directory.
*
*
* @return The path, or an empty string on error.
*/
string get_working_directory_path() {
char buffer[1024];
char *cwd = getcwd(buffer, sizeof(buffer));
char* cwd = getcwd(buffer, sizeof(buffer));
string result;
if(cwd) result = cwd;
return result;
Expand All @@ -754,11 +755,11 @@ string get_working_directory_path() {

/**
* @brief Opens the manual on the user's web browser in the specified page.
*
*
* @param page Page to open, with the .html extension and any anchors.
* @return Whether it succeeded in opening the browser.
*/
bool open_manual(const string& page) {
bool open_manual(const string &page) {
//This function could have a page argument and an anchor argument,
//and it could have included the .html extension automatically, but doing
//it this way makes it so that the string, e.g. "page.html#anchor" is
Expand Down Expand Up @@ -1330,3 +1331,14 @@ string unescape_string(const string &s) {
ret.push_back(s.back());
return ret;
}


/**
* @brief Convertes a vertex to a point.
*
* @param v Vertex to convert.
* @return The point.
*/
point v2p(const vertex* v) {
return point(v->x, v->y);
}
3 changes: 2 additions & 1 deletion source/source/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ ALLEGRO_COLOR get_wall_shadow_color(edge* e_ptr);
float get_wall_shadow_length(edge* e_ptr);
vector<std::pair<int, string> > get_weather_table(data_node* node);
string get_working_directory_path();
bool open_manual(const string& page);
bool open_manual(const string &page);
void print_info(
const string &text,
float total_duration = 5.0f,
Expand Down Expand Up @@ -181,3 +181,4 @@ void update_offset_effect_caches (
offset_effect_length_getter_t length_getter,
offset_effect_color_getter_t color_getter
);
point v2p(const vertex* v);
2 changes: 1 addition & 1 deletion source/source/game_states/animation_editor/drawing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void animation_editor::draw_canvas() {
int bmp_y = -bmp_h / 2.0;
al_draw_bitmap(s->parent_bmp, bmp_x, bmp_y, 0);

point scene_tl = point(-1, -1);
point scene_tl = point(-1.0f);
point scene_br = point(canvas_br.x + 1, canvas_br.y + 1);
al_transform_coordinates(
&game.screen_to_world_transform, &scene_tl.x, &scene_tl.y
Expand Down
Loading

0 comments on commit ee15d65

Please sign in to comment.