Skip to content

Commit bcdda22

Browse files
committed
Rework dynamic arrays
1 parent 9915f32 commit bcdda22

File tree

4 files changed

+13
-30
lines changed

4 files changed

+13
-30
lines changed

build_umplot_linux.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
gcc -O3 -fPIC umplot.c -o umplot.umi -shared -static-libgcc -L$PWD -lm -lraylib -lpthread
1+
gcc -O3 -fPIC umplot.c -o umplot.umi -shared -static-libgcc -L$PWD -lm -lraylib -lumka -lpthread
22

build_umplot_windows_mingw.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
gcc -O3 umplot.c -o umplot.umi -shared -Wl,--dll -static-libgcc -static -lraylib -L%cd% -lkernel32 -luser32 -lgdi32 -lwinmm
1+
gcc -O3 umplot.c -o umplot.umi -shared -Wl,--dll -static-libgcc -static -lraylib -lumka -L%cd% -lkernel32 -luser32 -lgdi32 -lwinmm

umplot.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ typedef struct
3131
typedef struct
3232
{
3333
UmkaDynArray(Point) points;
34-
int64_t actLen;
3534
char *name;
3635
Style style;
3736
} Series;
@@ -92,7 +91,7 @@ static Rectangle getLegendRect(const Plot *plot)
9291
if (!plot->legend.visible)
9392
return legendRect;
9493

95-
for (int iSeries = 0; iSeries < plot->series.len; iSeries++)
94+
for (int iSeries = 0; iSeries < umkaGetDynArrayLen(&plot->series); iSeries++)
9695
{
9796
const int labelWidth = MeasureText(plot->series.data[iSeries].name, plot->grid.fontSize);
9897
if (labelWidth > legendRect.width)
@@ -155,10 +154,10 @@ static void resetTransform(const Plot *plot, ScreenTransform *transform)
155154
Point minPt = (Point){ DBL_MAX, DBL_MAX};
156155
Point maxPt = (Point){-DBL_MAX, -DBL_MAX};
157156

158-
for (int iSeries = 0; iSeries < plot->series.len; iSeries++)
157+
for (int iSeries = 0; iSeries < umkaGetDynArrayLen(&plot->series); iSeries++)
159158
{
160159
Series *series = &plot->series.data[iSeries];
161-
for (int iPt = 0; iPt < series->points.len; iPt++)
160+
for (int iPt = 0; iPt < umkaGetDynArrayLen(&series->points); iPt++)
162161
{
163162
const Point *pt = &series->points.data[iPt];
164163
if (pt->x > maxPt.x) maxPt.x = pt->x;
@@ -208,18 +207,18 @@ static void drawGraph(const Plot *plot, const ScreenTransform *transform)
208207
Rectangle clientRect = getClientRect(plot);
209208
BeginScissorMode(clientRect.x, clientRect.y, clientRect.width, clientRect.height);
210209

211-
for (int iSeries = 0; iSeries < plot->series.len; iSeries++)
210+
for (int iSeries = 0; iSeries < umkaGetDynArrayLen(&plot->series); iSeries++)
212211
{
213212
Series *series = &plot->series.data[iSeries];
214213
switch (series->style.kind)
215214
{
216215
case STYLE_LINE:
217216
{
218-
if (series->points.len > 1)
217+
if (umkaGetDynArrayLen(&series->points) > 1)
219218
{
220219
Vector2 prevPt = getScreenPoint(series->points.data[0], transform);
221220

222-
for (int iPt = 1; iPt < series->points.len; iPt++)
221+
for (int iPt = 1; iPt < umkaGetDynArrayLen(&series->points); iPt++)
223222
{
224223
Vector2 pt = getScreenPoint(series->points.data[iPt], transform);
225224
DrawLineEx(prevPt, pt, series->style.width, *(Color *)&series->style.color);
@@ -231,7 +230,7 @@ static void drawGraph(const Plot *plot, const ScreenTransform *transform)
231230

232231
case STYLE_SCATTER:
233232
{
234-
for (int iPt = 0; iPt < series->points.len; iPt++)
233+
for (int iPt = 0; iPt < umkaGetDynArrayLen(&series->points); iPt++)
235234
{
236235
Vector2 pt = getScreenPoint(series->points.data[iPt], transform);
237236
DrawCircleV(pt, series->style.width, *(Color *)&series->style.color);
@@ -370,7 +369,7 @@ static void drawLegend(const Plot *plot, const Font *font)
370369

371370
const Rectangle legendRect = getLegendRect(plot);
372371

373-
for (int iSeries = 0; iSeries < plot->series.len; iSeries++)
372+
for (int iSeries = 0; iSeries < umkaGetDynArrayLen(&plot->series); iSeries++)
374373
{
375374
Series *series = &plot->series.data[iSeries];
376375

umplot.um

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ type (
1616

1717
Series* = struct {
1818
points: []Point
19-
actLen: int
2019
name: str
2120
style: Style
2221
}
@@ -48,25 +47,14 @@ type (
4847
)
4948

5049
fn (s: ^Series) clear*() {
51-
s.points = make([]Point, 256)
52-
s.actLen = 0
53-
}
54-
55-
fn (s: ^Series) trim() {
56-
if s.actLen < len(s.points) {
57-
s.points = slice(s.points, 0, s.actLen)
58-
}
50+
s.points = []Point{}
5951
}
6052

6153
fn (s: ^Series) add*(x, y: real) {
62-
if len(s.points) == 0 {
54+
if !valid(s.points) {
6355
s.clear()
6456
}
65-
if s.actLen >= len(s.points) {
66-
s.points = append(s.points, make([]Point, len(s.points)))
67-
}
68-
s.points[s.actLen] = Point{x, y}
69-
s.actLen++
57+
s.points = append(s.points, Point{x, y})
7058
}
7159

7260
fn init*(numSeries: int = 1, kind: int = STYLE_LINE): Plot {
@@ -105,10 +93,6 @@ fn init*(numSeries: int = 1, kind: int = STYLE_LINE): Plot {
10593
fn umplot_plot(p: ^Plot): int
10694

10795
fn (p: ^Plot) plot*() {
108-
for i := 0; i < len(p.series); i++ {
109-
p.series[i].trim()
110-
}
111-
11296
umplot_plot(p)
11397
}
11498

0 commit comments

Comments
 (0)