Skip to content

Commit f5922f5

Browse files
committed
Directly use dynamic arrays
1 parent 78e7d35 commit f5922f5

File tree

2 files changed

+22
-43
lines changed

2 files changed

+22
-43
lines changed

umplot.c

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ typedef struct
3030

3131
typedef struct
3232
{
33-
Point *points;
34-
int64_t numPoints;
33+
UmkaDynArray(Point) points;
3534
char *name;
3635
Style style;
36+
int64_t reserved;
3737
} Series;
3838

3939

@@ -63,8 +63,7 @@ typedef struct
6363

6464
typedef struct
6565
{
66-
Series *series;
67-
int64_t numSeries;
66+
UmkaDynArray(Series) series;
6867
Grid grid;
6968
Titles titles;
7069
Legend legend;
@@ -93,9 +92,9 @@ static Rectangle getLegendRect(const Plot *plot)
9392
if (!plot->legend.visible)
9493
return legendRect;
9594

96-
for (int iSeries = 0; iSeries < plot->numSeries; iSeries++)
95+
for (int iSeries = 0; iSeries < plot->series.len; iSeries++)
9796
{
98-
const int labelWidth = MeasureText(plot->series[iSeries].name, plot->grid.fontSize);
97+
const int labelWidth = MeasureText(plot->series.data[iSeries].name, plot->grid.fontSize);
9998
if (labelWidth > legendRect.width)
10099
legendRect.width = labelWidth;
101100
}
@@ -156,12 +155,12 @@ static void resetTransform(const Plot *plot, ScreenTransform *transform)
156155
Point minPt = (Point){ DBL_MAX, DBL_MAX};
157156
Point maxPt = (Point){-DBL_MAX, -DBL_MAX};
158157

159-
for (int iSeries = 0; iSeries < plot->numSeries; iSeries++)
158+
for (int iSeries = 0; iSeries < plot->series.len; iSeries++)
160159
{
161-
Series *series = &plot->series[iSeries];
162-
for (int iPt = 0; iPt < series->numPoints; iPt++)
160+
Series *series = &plot->series.data[iSeries];
161+
for (int iPt = 0; iPt < series->points.len; iPt++)
163162
{
164-
const Point *pt = &series->points[iPt];
163+
const Point *pt = &series->points.data[iPt];
165164
if (pt->x > maxPt.x) maxPt.x = pt->x;
166165
if (pt->x < minPt.x) minPt.x = pt->x;
167166
if (pt->y > maxPt.y) maxPt.y = pt->y;
@@ -209,20 +208,20 @@ static void drawGraph(const Plot *plot, const ScreenTransform *transform)
209208
Rectangle clientRect = getClientRect(plot);
210209
BeginScissorMode(clientRect.x, clientRect.y, clientRect.width, clientRect.height);
211210

212-
for (int iSeries = 0; iSeries < plot->numSeries; iSeries++)
211+
for (int iSeries = 0; iSeries < plot->series.len; iSeries++)
213212
{
214-
Series *series = &plot->series[iSeries];
213+
Series *series = &plot->series.data[iSeries];
215214
switch (series->style.kind)
216215
{
217216
case STYLE_LINE:
218217
{
219-
if (series->numPoints > 1)
218+
if (series->points.len > 1)
220219
{
221-
Vector2 prevPt = getScreenPoint(series->points[0], transform);
220+
Vector2 prevPt = getScreenPoint(series->points.data[0], transform);
222221

223-
for (int iPt = 1; iPt < series->numPoints; iPt++)
222+
for (int iPt = 1; iPt < series->points.len; iPt++)
224223
{
225-
Vector2 pt = getScreenPoint(series->points[iPt], transform);
224+
Vector2 pt = getScreenPoint(series->points.data[iPt], transform);
226225
DrawLineEx(prevPt, pt, series->style.width, *(Color *)&series->style.color);
227226
prevPt = pt;
228227
}
@@ -232,9 +231,9 @@ static void drawGraph(const Plot *plot, const ScreenTransform *transform)
232231

233232
case STYLE_SCATTER:
234233
{
235-
for (int iPt = 0; iPt < series->numPoints; iPt++)
234+
for (int iPt = 0; iPt < series->points.len; iPt++)
236235
{
237-
Vector2 pt = getScreenPoint(series->points[iPt], transform);
236+
Vector2 pt = getScreenPoint(series->points.data[iPt], transform);
238237
DrawCircleV(pt, series->style.width, *(Color *)&series->style.color);
239238
}
240239
break;
@@ -371,9 +370,9 @@ static void drawLegend(const Plot *plot, const Font *font)
371370

372371
const Rectangle legendRect = getLegendRect(plot);
373372

374-
for (int iSeries = 0; iSeries < plot->numSeries; iSeries++)
373+
for (int iSeries = 0; iSeries < plot->series.len; iSeries++)
375374
{
376-
Series *series = &plot->series[iSeries];
375+
Series *series = &plot->series.data[iSeries];
377376

378377
// Legend mark
379378
switch (series->style.kind)

umplot.um

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ type (
2121
reserved: int
2222
}
2323

24-
SeriesImpl = struct {
25-
points: ^Point
26-
numPoints: int
27-
name: str
28-
style: Style
29-
}
30-
3124
Grid* = struct {
3225
xNumLines, yNumLines: int
3326
color: uint32
@@ -52,14 +45,6 @@ type (
5245
titles: Titles
5346
legend: Legend
5447
}
55-
56-
PlotImpl = struct {
57-
series: ^SeriesImpl
58-
numSeries: int
59-
grid: Grid
60-
titles: Titles
61-
legend: Legend
62-
}
6348
)
6449

6550
fn (s: ^Series) clear*() {
@@ -122,19 +107,14 @@ fn init*(numSeries: int = 1, kind: int = STYLE_LINE): Plot {
122107
return plt
123108
}
124109

125-
fn umplot_plot(p: ^PlotImpl): int
110+
fn umplot_plot(p: ^Plot): int
126111

127112
fn (p: ^Plot) plot*() {
128-
seriesImpl := make([]SeriesImpl, len(p.series))
129-
130113
for i := 0; i < len(p.series); i++ {
131-
s := &p.series[i]
132-
s.trim()
133-
seriesImpl[i] = SeriesImpl{&s.points[0], len(s.points), s.name, s.style}
114+
p.series[i].trim()
134115
}
135116

136-
plotImpl := PlotImpl{&seriesImpl[0], len(seriesImpl), p.grid, p.titles, p.legend}
137-
umplot_plot(&plotImpl)
117+
umplot_plot(p)
138118
}
139119

140120

0 commit comments

Comments
 (0)