From 0d07dc9f7324ed2bd23051034d761f2094f2fee2 Mon Sep 17 00:00:00 2001 From: Cameron Reikes Date: Tue, 2 May 2023 00:00:57 -0700 Subject: [PATCH] Fix issue where clipping in world space not transformed --- assets.mdesk | 2 +- assets/newsmalllevel.json | 23 +++-------------------- assets/testsmalllevel.json | 30 ++++++++++++++++++------------ buff.h | 2 ++ main.c | 38 ++++++++++++++++++++++++-------------- makeprompt.h | 2 ++ 6 files changed, 50 insertions(+), 47 deletions(-) diff --git a/assets.mdesk b/assets.mdesk index 07a15b9..afbfd15 100644 --- a/assets.mdesk +++ b/assets.mdesk @@ -150,5 +150,5 @@ } @level level0: { - filepath: "new_level.json", + filepath: "newsmalllevel.json", } diff --git a/assets/newsmalllevel.json b/assets/newsmalllevel.json index dbdae2e..fe8af31 100644 --- a/assets/newsmalllevel.json +++ b/assets/newsmalllevel.json @@ -96,14 +96,14 @@ "rotation":0, "visible":true, "width":32, - "x":46.4621212121228, - "y":115.727272727277 + "x":173.128787878789, + "y":107.06060606061 }, { "class":"", "height":32, "id":17, - "name":"OldMan", + "name":"TheBlacksmith", "properties":[ { "name":"standing", @@ -115,23 +115,6 @@ "width":32, "x":240, "y":106 - }, - { - "class":"", - "height":32, - "id":18, - "name":"OldMan", - "properties":[ - { - "name":"standing", - "type":"string", - "value":"STANDING_JOINED" - }], - "rotation":0, - "visible":true, - "width":32, - "x":236, - "y":472 }], "opacity":1, "type":"objectgroup", diff --git a/assets/testsmalllevel.json b/assets/testsmalllevel.json index b7da54e..2ff6241 100644 --- a/assets/testsmalllevel.json +++ b/assets/testsmalllevel.json @@ -26,17 +26,6 @@ "id":2, "name":"objects", "objects":[ - { - "class":"", - "height":31.604938271605, - "id":1, - "name":"MERCHANT", - "rotation":0, - "visible":true, - "width":29.9415204678363, - "x":167, - "y":81.5 - }, { "class":"", "height":31.6049, @@ -47,6 +36,23 @@ "width":29.9415, "x":68.02925, "y":88.19755 + }, + { + "class":"", + "height":32, + "id":3, + "name":"TheBlacksmith", + "properties":[ + { + "name":"held_items", + "type":"string", + "value":"{.data = {ITEM_Chalice}, .cur_index = 1}" + }], + "rotation":0, + "visible":true, + "width":32, + "x":165.353383458647, + "y":104.300751879699 }], "opacity":1, "type":"objectgroup", @@ -74,7 +80,7 @@ "y":0 }], "nextlayerid":4, - "nextobjectid":3, + "nextobjectid":4, "orientation":"orthogonal", "renderorder":"right-down", "tiledversion":"1.9.2", diff --git a/buff.h b/buff.h index ac692a7..06c5193 100644 --- a/buff.h +++ b/buff.h @@ -9,6 +9,8 @@ // you can't do &(some func, variable) to get the variable's address in C. Sad! #define GET_TABLE_PTR(table, index) (assert(index < ARRLEN(table) && index >= 0), &table[index]) +#define RANGE_ITER(from, to) for(int i = from; i < to; i++) + // null terminator always built into buffers so can read properly from data #define BUFF_VALID(buff_ptr) assert((buff_ptr)->cur_index <= ARRLEN((buff_ptr)->data)) #define BUFF(type, max_size) struct { int cur_index; type data[max_size]; char null_terminator; } diff --git a/main.c b/main.c index 2c73f65..a814b33 100644 --- a/main.c +++ b/main.c @@ -727,6 +727,7 @@ void reset_level() } update_player_from_entities(); +#ifdef DEVTOOLS if(false) { BUFF_APPEND(&player->held_items, ITEM_WhiteSquare); @@ -738,11 +739,13 @@ void reset_level() { if (it->npc_kind == NPC_TheBlacksmith) { - //BUFF_APPEND(&it->remembered_perceptions, ((Perception) { .type = PlayerDialog, .player_dialog = SENTENCE_CONST("Testing dialog") })); + //RANGE_ITER(0, 20) + //BUFF_APPEND(&it->remembered_perceptions, ((Perception) { .type = PlayerDialog, .player_dialog = SENTENCE_CONST("Testing dialog") })); //BUFF_APPEND(&it->held_items, ITEM_Chalice); } } +#endif } @@ -1438,6 +1441,13 @@ void draw_quad(DrawParams d) points[i] = world_to_screen(points[i]); } } + + if (d.do_clipping && d.world_space) + { + d.clip_to.upper_left = world_to_screen(d.clip_to.upper_left); + d.clip_to.lower_right = world_to_screen(d.clip_to.lower_right); + } + // we've aplied the world space transform d.world_space = false; @@ -2002,6 +2012,7 @@ typedef struct Color *colors; float text_scale; AABB clip_to; + bool do_clipping; bool screen_space; } WrappedTextParams; @@ -2024,7 +2035,7 @@ float draw_wrapped_text(WrappedTextParams p) memset(line_to_draw, 0, MAX_SENTENCE_LENGTH); memcpy(line_to_draw, sentence_to_draw, chars_from_sentence); - line_bounds = draw_text((TextParams) { !p.screen_space, true, line_to_draw, cursor, BLACK, p.text_scale, p.clip_to }); + line_bounds = draw_text((TextParams) { !p.screen_space, true, line_to_draw, cursor, BLACK, p.text_scale, p.clip_to, .do_clipping = p.do_clipping}); if (line_bounds.lower_right.X > p.at_point.X + p.max_width) { // too big @@ -2041,7 +2052,7 @@ float draw_wrapped_text(WrappedTextParams p) //float line_height = line_bounds.upper_left.Y - line_bounds.lower_right.Y; float line_height = font_line_advance * p.text_scale; - AABB drawn_bounds = draw_text((TextParams) { !p.screen_space, p.dry_run, line_to_draw, AddV2(cursor, V2(0.0f, -line_height)), BLACK, p.text_scale, p.clip_to, colors_to_draw }); + AABB drawn_bounds = draw_text((TextParams) { !p.screen_space, p.dry_run, line_to_draw, AddV2(cursor, V2(0.0f, -line_height)), BLACK, p.text_scale, p.clip_to, colors_to_draw, .do_clipping = p.do_clipping}); if (!p.dry_run) dbgrect(drawn_bounds); // caught a random infinite loop in the debugger, this will stop it @@ -2153,12 +2164,15 @@ Dialog produce_dialog(Entity *talking_to, bool character_names) return to_return; } +Vec2 mouse_pos = { 0 }; // in screen space void draw_dialog_panel(Entity *talking_to, float alpha) { float panel_width = 250.0f; float panel_height = 150.0f; float panel_vert_offset = 30.0f; + + AABB dialog_panel = (AABB) { .upper_left = AddV2(talking_to->pos, V2(-panel_width / 2.0f, panel_vert_offset + panel_height)), .lower_right = AddV2(talking_to->pos, V2(panel_width / 2.0f, panel_vert_offset)), @@ -2224,9 +2238,9 @@ void draw_dialog_panel(Entity *talking_to, float alpha) } colors[char_i] = blendalpha(colors[char_i], alpha); } - float measured_line_height = draw_wrapped_text((WrappedTextParams) { true, V2(dialog_panel.upper_left.X, new_line_height), dialog_panel.lower_right.X - dialog_panel.upper_left.X, it->s.data, colors, 0.5f, dialog_panel }); + float measured_line_height = draw_wrapped_text((WrappedTextParams) { true, V2(dialog_panel.upper_left.X, new_line_height), dialog_panel.lower_right.X - dialog_panel.upper_left.X, it->s.data, colors, 0.5f, .clip_to = dialog_panel, .do_clipping = true}); new_line_height += (new_line_height - measured_line_height); - draw_wrapped_text((WrappedTextParams) { false, V2(dialog_panel.upper_left.X, new_line_height), dialog_panel.lower_right.X - dialog_panel.upper_left.X, it->s.data, colors, 0.5f, dialog_panel }); + draw_wrapped_text((WrappedTextParams) { false, V2(dialog_panel.upper_left.X, new_line_height), dialog_panel.lower_right.X - dialog_panel.upper_left.X, it->s.data, colors, 0.5f, dialog_panel, .do_clipping = true }); free(colors); } @@ -2244,7 +2258,6 @@ double elapsed_time = 0.0; double unwarped_elapsed_time = 0.0; double last_frame_processing_time = 0.0; uint64_t last_frame_time; -Vec2 mouse_pos = { 0 }; // in screen space typedef struct { @@ -3795,9 +3808,9 @@ void frame(void) } colors[char_i] = blendalpha(colors[char_i], alpha); } - float measured_line_height = draw_wrapped_text((WrappedTextParams) { true, V2(dialog_text_aabb.upper_left.X, new_line_height), dialog_text_aabb.lower_right.X - dialog_text_aabb.upper_left.X, it->s.data, colors, dialog_text_scale, dialog_text_aabb, .screen_space = true }); + float measured_line_height = draw_wrapped_text((WrappedTextParams) { true, V2(dialog_text_aabb.upper_left.X, new_line_height), dialog_text_aabb.lower_right.X - dialog_text_aabb.upper_left.X, it->s.data, colors, dialog_text_scale, dialog_text_aabb, .screen_space = true, .do_clipping = true}); new_line_height += (new_line_height - measured_line_height); - draw_wrapped_text((WrappedTextParams) { false, V2(dialog_text_aabb.upper_left.X, new_line_height), dialog_text_aabb.lower_right.X - dialog_text_aabb.upper_left.X, it->s.data, colors, dialog_text_scale, dialog_text_aabb, .screen_space = true }); + draw_wrapped_text((WrappedTextParams) { false, V2(dialog_text_aabb.upper_left.X, new_line_height), dialog_text_aabb.lower_right.X - dialog_text_aabb.upper_left.X, it->s.data, colors, dialog_text_scale, dialog_text_aabb, .screen_space = true, .do_clipping = true}); free(colors); } @@ -4011,8 +4024,9 @@ void frame(void) PROFILE_SCOPE("flush rendering") { - ARR_ITER(RenderingQueue, rendering_queues) + ARR_ITER_I(RenderingQueue, rendering_queues, i) { + Layer layer = (Layer)i; RenderingQueue *rendering_queue = it; qsort(&rendering_queue->data[0], rendering_queue->cur_index, sizeof(rendering_queue->data[0]), rendering_compare); @@ -4021,6 +4035,7 @@ void frame(void) DrawParams d = *it; PROFILE_SCOPE("Draw quad") { + assert(!d.world_space); // world space already applied when queued for drawing Vec2 *points = d.quad.points; quad_fs_params_t params = { 0 }; params.tint[0] = d.tint.R; @@ -4030,11 +4045,6 @@ void frame(void) params.alpha_clip_threshold = d.alpha_clip_threshold; if (d.do_clipping) { - if (d.world_space) - { - d.clip_to.upper_left = world_to_screen(d.clip_to.upper_left); - d.clip_to.lower_right = world_to_screen(d.clip_to.lower_right); - } Vec2 aabb_clip_ul = into_clip_space(d.clip_to.upper_left); Vec2 aabb_clip_lr = into_clip_space(d.clip_to.lower_right); params.clip_ul[0] = aabb_clip_ul.x; diff --git a/makeprompt.h b/makeprompt.h index 249be9e..e302af9 100644 --- a/makeprompt.h +++ b/makeprompt.h @@ -241,6 +241,8 @@ typedef struct Entity bool in_conversation_mode; Vec2 to_throw_direction; + BUFF(Vec2, 8) position_history; // so npcs can follow behind the player + CharacterState state; EntityRef talking_to; bool is_rolling; // can only roll in idle or walk states