Skip to content

Commit

Permalink
performance improvements
Browse files Browse the repository at this point in the history
need to reduce the amount of `new` keywords along this codebase.
  • Loading branch information
ericoporto committed Feb 18, 2021
1 parent b6cf9ad commit baf3a92
Showing 1 changed file with 48 additions and 57 deletions.
105 changes: 48 additions & 57 deletions imgi_demo/imgi.asc
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,15 @@ managed struct ImGi_PollItem
managed struct ImGi_Cmd
{
ImGi_CmdType type;
int size;
ImGiCmd_ID id;
int x;
int y;
int w;
int h;
FontType font;
int color;
int dst_idx;
FontType font;
ImGi_Icon icon;
char str[MAX_TEXT_CMD_STR_SIZE];
int dst_idx;

};

managed struct ImGi_Layout
Expand Down Expand Up @@ -295,6 +292,7 @@ struct ImGi_Context {

// Layout
import void LayoutRow(int items, int widths[], int height = 0);
import void LayoutRow1(int width, int height = 0);

// Controls
import ImGi_Res BeginWindow(String title, int x, int y, int width, int height, ImGi_Opt opt = 0);
Expand Down Expand Up @@ -454,33 +452,6 @@ bool _Rect_Overlaps_Point(int r_x, int r_y, int r_w, int r_h, int p_x, int p_y)
);
}

int[] _Rect_Intersect(int r1_x, int r1_y, int r1_w, int r1_h,
int r2_x, int r2_y, int r2_w, int r2_h)
{
int x1 = _max(r1_x, r2_x);
int y1 = _max(r1_y, r2_y);
int x2 = _min(r1_x + r1_w, r2_x + r2_w);
int y2 = _min(r1_y + r1_h, r2_y + r2_h);
if(x2 < x1) x2 = x1;
if(y2 < y1) y2 = y1;
int r[] = new int[4];
r[0] = x1;
r[1] = y1;
r[2] = x2-x1;
r[3] = y2-y1;
return r;
}

int[] _Rect_Expand(int r_x, int r_y, int r_w, int r_h, int n)
{
int r[] = new int[4];
r[0] = r_x - n;
r[1] = r_y - n;
r[2] = r_w + n*2;
r[3] = r_h + n*2;
return r;
}

static Rect* Rect::Create(int x, int y, int w, int h)
{
Rect* r = new Rect;
Expand All @@ -493,8 +464,20 @@ static Rect* Rect::Create(int x, int y, int w, int h)

Rect* _Rect_IntersectR1R2(Rect* r1, Rect* r2)
{
int r[] = _Rect_Intersect(r1.x, r1.y, r1.w, r1.h, r2.x, r2.y, r2.w, r2.h);
return Rect.Create(r[0], r[1], r[2], r[3]);
Rect* r = new Rect;
int x1 = _max(r1.x, r2.x);
int y1 = _max(r1.y, r2.y);
int x2 = _min(r1.x + r1.w, r2.x + r2.w);
int y2 = _min(r1.y + r1.h, r2.y + r2.h);
if(x2 < x1) x2 = x1;
if(y2 < y1) y2 = y1;

r.x = x1;
r.y = y1;
r.w = x2-x1;
r.h = y2-y1;

return r;
}

bool _Rect_Overlaps_P(Rect* r, int p_x, int p_y)
Expand All @@ -509,8 +492,12 @@ Rect* Rect::Intersect(Rect* r)

Rect* Rect::Expand(int n)
{
int r[] = _Rect_Expand(this.x, this.y, this.w, this.h, n);
return Rect.Create(r[0], r[1], r[2], r[3]);
Rect* r = new Rect;
r.x = this.x - n;
r.y = this.y - n;
r.w = this.w + n*2;
r.h = this.h + n*2;
return r;
}

bool Rect::IsOverlappingPoint(Point* p)
Expand Down Expand Up @@ -840,12 +827,20 @@ void ImGi_Context::LayoutRow(int items, int widths[], int height) {
layout.item_index = 0;
}

void ImGi_Context::LayoutRow1(int width, int height) {
ImGi_Layout* layout = this.get_layout();
layout.widths[0] = width;
layout.items = 1;
layout.pos_x = layout.indent;
layout.pos_y = layout.next_row;
layout.size_y = height;
layout.item_index = 0;
}

void push_layout(this ImGi_Context*, Rect* body, Point* scroll)
{
ImGi_Layout* layout = new ImGi_Layout;
int width[];
width = new int[1];
width[0] = 0;

layout.body_x = body.x - scroll.x;
layout.body_y = body.y - scroll.y;
layout.body_w = body.w;
Expand All @@ -855,7 +850,7 @@ void push_layout(this ImGi_Context*, Rect* body, Point* scroll)
layout.max_y = -UNCLIPPED_RECT_H;

this.stk_layout_push(layout);
this.LayoutRow(1, width, 0);
this.LayoutRow1(0, 0);
}

// extras clip stack -->> ////////////////////////////////////////////////
Expand Down Expand Up @@ -929,11 +924,6 @@ void ImGi_Context::Begin()
this.frame++;
}

int _compare_zindex(ImGi_Container* a, ImGi_Container* b)
{
return a.zindex - b.zindex;
}

void ImGi_Context::End()
{
int i, n;
Expand Down Expand Up @@ -1376,13 +1366,11 @@ void ImGi_Context::Text(String text)
{
int start_i, end_i, p_i;
int len = text.Length;
int width[];
width = new int[1];
width[0] = -1;

FontType font = this.style.font;
int color = this.style.colors[eImGi_Col_Text];
this.layout_begin_column();
this.LayoutRow(1, width, GetFontHeight(font));
this.LayoutRow1(-1, GetFontHeight(font));

do {
Rect* r = this.layout_next();
Expand Down Expand Up @@ -2132,7 +2120,7 @@ int _ImGi_Calculate_Command_Hash()
ImGi_Cmd* c = _ImGi.stk_cmd_items[i];
if(c!=null)
{
hn = (hn ^ c.type)* 16777619;
// hn = (hn ^ c.type)* 16777619;
hn = (hn ^ c.dst_idx)* 16777619;
hn = (hn ^ c.x)* 16777619;
hn = (hn ^ c.y)* 16777619;
Expand All @@ -2142,11 +2130,14 @@ int _ImGi_Calculate_Command_Hash()
hn = (hn ^ c.icon)* 16777619;
if(c.str[0] != 0)
{
//this is a hack but should work good enough, better than going through the whole string
String s = String.Format("%s",c.str);
for(int k=0; k<s.Length; k++)
{
hn = (hn ^ s.Chars[k])* 16777619;
}
hn = (hn ^ s.Length)* 16777619;
hn = (hn ^ s.Chars[s.Length-1])* 16777619;
//for(int k=0; k<s.Length; k++)
//{
// hn = (hn ^ s.Chars[k])* 16777619;
//}
}
}
}
Expand Down Expand Up @@ -2220,13 +2211,13 @@ void r_mo_clear()

void r_so_clear()
{
r_c_i = -1;
for(int i=0; i<MAX_CLIPS; i++) {
for(int i=0; i<r_c_i+1; i++) {
if(_clips[i].spr != null) {
_clips[i].spr.Delete();
_clips[i].spr = null;
}
}
r_c_i = -1;
if(_clips[0].ovr != null) {
_clips[0].ovr.Remove();
_clips[0].ovr = null;
Expand Down Expand Up @@ -2292,7 +2283,7 @@ void r_mo_render_clips()

void r_so_render_clips()
{
global_rci = r_c_i;
//hacky way with a single overlay
_clips[0].srf = _clips[0].spr.GetDrawingSurface();
for(int i=1; i<r_c_i+1; i++)
Expand Down

0 comments on commit baf3a92

Please sign in to comment.