Skip to content

Commit f4f698f

Browse files
committed
Switch to dynamic Umka API
1 parent 3f68561 commit f4f698f

File tree

3 files changed

+43
-40
lines changed

3 files changed

+43
-40
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_linux.umi -shared -static-libgcc -L$PWD -lm -lraylib -lumka_static -lpthread
1+
gcc -O3 -fPIC umplot.c -o umplot_linux.umi -shared -static-libgcc -L$PWD -lm -lraylib -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 -DUMKA_STATIC umplot.c -o umplot_windows.umi -shared -Wl,--dll -static-libgcc -static -lumka_static -lraylib -L. -lkernel32 -luser32 -lgdi32 -lwinmm
1+
gcc -O3 -DUMKA_STATIC umplot.c -o umplot_windows.umi -shared -Wl,--dll -static-libgcc -static -lraylib -L. -lkernel32 -luser32 -lgdi32 -lwinmm

umplot.c

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ static Rectangle getClientRectWithLegend()
9090
}
9191

9292

93-
static Rectangle getLegendRect(const Plot *plot)
93+
static Rectangle getLegendRect(const Plot *plot, UmkaAPI *api)
9494
{
9595
const int dashLength = 20, margin = 20;
9696
Rectangle legendRect = {0};
9797

9898
if (!plot->legend.visible)
9999
return legendRect;
100100

101-
for (int iSeries = 0; iSeries < umkaGetDynArrayLen(&plot->series); iSeries++)
101+
for (int iSeries = 0; iSeries < api->umkaGetDynArrayLen(&plot->series); iSeries++)
102102
{
103103
const int labelWidth = MeasureText(plot->series.data[iSeries].name, plot->grid.fontSize);
104104
if (labelWidth > legendRect.width)
@@ -120,10 +120,10 @@ static Rectangle getLegendRect(const Plot *plot)
120120
}
121121

122122

123-
static Rectangle getClientRect(const Plot *plot)
123+
static Rectangle getClientRect(const Plot *plot, UmkaAPI *api)
124124
{
125125
const Rectangle clientRectWithLegend = getClientRectWithLegend();
126-
const Rectangle legendRect = getLegendRect(plot);
126+
const Rectangle legendRect = getLegendRect(plot, api);
127127

128128
Rectangle clientRect = clientRectWithLegend;
129129
clientRect.width -= legendRect.width;
@@ -144,9 +144,9 @@ static Point getGraphPoint(const Vector2 point, const ScreenTransform *transform
144144
}
145145

146146

147-
static void setTransformToMinMax(const Plot *plot, ScreenTransform *transform, const Point *minPt, const Point *maxPt)
147+
static void setTransformToMinMax(const Plot *plot, ScreenTransform *transform, const Point *minPt, const Point *maxPt, UmkaAPI *api)
148148
{
149-
Rectangle rect = getClientRect(plot);
149+
Rectangle rect = getClientRect(plot, api);
150150

151151
transform->xScale = (maxPt->x > minPt->x) ? (rect.width / (maxPt->x - minPt->x)) : 1.0;
152152
transform->yScale = (maxPt->y > minPt->y) ? -(rect.height / (maxPt->y - minPt->y)) : 1.0;
@@ -156,15 +156,15 @@ static void setTransformToMinMax(const Plot *plot, ScreenTransform *transform, c
156156
}
157157

158158

159-
static void resetTransform(const Plot *plot, ScreenTransform *transform)
159+
static void resetTransform(const Plot *plot, ScreenTransform *transform, UmkaAPI *api)
160160
{
161161
Point minPt = (Point){ DBL_MAX, DBL_MAX};
162162
Point maxPt = (Point){-DBL_MAX, -DBL_MAX};
163163

164-
for (int iSeries = 0; iSeries < umkaGetDynArrayLen(&plot->series); iSeries++)
164+
for (int iSeries = 0; iSeries < api->umkaGetDynArrayLen(&plot->series); iSeries++)
165165
{
166166
Series *series = &plot->series.data[iSeries];
167-
for (int iPt = 0; iPt < umkaGetDynArrayLen(&series->points); iPt++)
167+
for (int iPt = 0; iPt < api->umkaGetDynArrayLen(&series->points); iPt++)
168168
{
169169
const Point *pt = &series->points.data[iPt];
170170
if (pt->x > maxPt.x) maxPt.x = pt->x;
@@ -174,16 +174,16 @@ static void resetTransform(const Plot *plot, ScreenTransform *transform)
174174
}
175175
}
176176

177-
setTransformToMinMax(plot, transform, &minPt, &maxPt);
177+
setTransformToMinMax(plot, transform, &minPt, &maxPt, api);
178178
}
179179

180180

181-
static void resizeTransform(const Plot *plot, ScreenTransform *transform, const Rectangle *rect)
181+
static void resizeTransform(const Plot *plot, ScreenTransform *transform, const Rectangle *rect, UmkaAPI *api)
182182
{
183183
const Point minPt = getGraphPoint((Vector2){rect->x, rect->y + rect->height}, transform);
184184
const Point maxPt = getGraphPoint((Vector2){rect->x + rect->width, rect->y}, transform);
185185

186-
setTransformToMinMax(plot, transform, &minPt, &maxPt);
186+
setTransformToMinMax(plot, transform, &minPt, &maxPt, api);
187187
}
188188

189189

@@ -194,38 +194,38 @@ static void panTransform(const Plot *plot, ScreenTransform *transform, const Vec
194194
}
195195

196196

197-
static void zoomTransform(const Plot *plot, ScreenTransform *transform, const Rectangle *zoomRect)
197+
static void zoomTransform(const Plot *plot, ScreenTransform *transform, const Rectangle *zoomRect, UmkaAPI *api)
198198
{
199199
if (zoomRect->width == 0 && zoomRect->height == 0)
200200
return;
201201

202202
if (zoomRect->width < 0 || zoomRect->height < 0)
203203
{
204-
resetTransform(plot, transform);
204+
resetTransform(plot, transform, api);
205205
return;
206206
}
207207

208-
resizeTransform(plot, transform, zoomRect);
208+
resizeTransform(plot, transform, zoomRect, api);
209209
}
210210

211211

212-
static void drawGraph(const Plot *plot, const ScreenTransform *transform)
212+
static void drawGraph(const Plot *plot, const ScreenTransform *transform, UmkaAPI *api)
213213
{
214-
Rectangle clientRect = getClientRect(plot);
214+
Rectangle clientRect = getClientRect(plot, api);
215215
BeginScissorMode(clientRect.x, clientRect.y, clientRect.width, clientRect.height);
216216

217-
for (int iSeries = 0; iSeries < umkaGetDynArrayLen(&plot->series); iSeries++)
217+
for (int iSeries = 0; iSeries < api->umkaGetDynArrayLen(&plot->series); iSeries++)
218218
{
219219
Series *series = &plot->series.data[iSeries];
220220
switch (series->style.kind)
221221
{
222222
case STYLE_LINE:
223223
{
224-
if (umkaGetDynArrayLen(&series->points) > 1)
224+
if (api->umkaGetDynArrayLen(&series->points) > 1)
225225
{
226226
Vector2 prevPt = getScreenPoint(series->points.data[0], transform);
227227

228-
for (int iPt = 1; iPt < umkaGetDynArrayLen(&series->points); iPt++)
228+
for (int iPt = 1; iPt < api->umkaGetDynArrayLen(&series->points); iPt++)
229229
{
230230
Vector2 pt = getScreenPoint(series->points.data[iPt], transform);
231231
DrawLineEx(prevPt, pt, series->style.width, *(Color *)&series->style.color);
@@ -237,7 +237,7 @@ static void drawGraph(const Plot *plot, const ScreenTransform *transform)
237237

238238
case STYLE_SCATTER:
239239
{
240-
for (int iPt = 0; iPt < umkaGetDynArrayLen(&series->points); iPt++)
240+
for (int iPt = 0; iPt < api->umkaGetDynArrayLen(&series->points); iPt++)
241241
{
242242
Vector2 pt = getScreenPoint(series->points.data[iPt], transform);
243243
DrawCircleV(pt, series->style.width, *(Color *)&series->style.color);
@@ -253,15 +253,15 @@ static void drawGraph(const Plot *plot, const ScreenTransform *transform)
253253
}
254254

255255

256-
static void drawGrid(const Plot *plot, const ScreenTransform *transform, const Font *font, int *maxYLabelWidth)
256+
static void drawGrid(const Plot *plot, const ScreenTransform *transform, const Font *font, int *maxYLabelWidth, UmkaAPI *api)
257257
{
258258
if (maxYLabelWidth)
259259
*maxYLabelWidth = 0;
260260

261261
if (plot->grid.xNumLines <= 0 || plot->grid.yNumLines <= 0)
262262
return;
263263

264-
const Rectangle clientRect = getClientRect(plot);
264+
const Rectangle clientRect = getClientRect(plot, api);
265265

266266
const double xSpan = clientRect.width / transform->xScale;
267267
const double ySpan = -clientRect.height / transform->yScale;
@@ -325,12 +325,12 @@ static void drawGrid(const Plot *plot, const ScreenTransform *transform, const F
325325
}
326326

327327

328-
static void drawTitles(const Plot *plot, const ScreenTransform *transform, const Font *font, int maxYLabelWidth)
328+
static void drawTitles(const Plot *plot, const ScreenTransform *transform, const Font *font, int maxYLabelWidth, UmkaAPI *api)
329329
{
330330
if (!plot->titles.visible)
331331
return;
332332

333-
Rectangle clientRect = getClientRect(plot);
333+
Rectangle clientRect = getClientRect(plot, api);
334334

335335
// Horizontal axis
336336
if (plot->titles.x && TextLength(plot->titles.x) > 0)
@@ -367,16 +367,16 @@ static void drawTitles(const Plot *plot, const ScreenTransform *transform, const
367367
}
368368

369369

370-
static void drawLegend(const Plot *plot, const Font *font)
370+
static void drawLegend(const Plot *plot, const Font *font, UmkaAPI *api)
371371
{
372372
if (!plot->legend.visible)
373373
return;
374374

375375
const int dashLength = 20, margin = 20;
376376

377-
const Rectangle legendRect = getLegendRect(plot);
377+
const Rectangle legendRect = getLegendRect(plot, api);
378378

379-
for (int iSeries = 0; iSeries < umkaGetDynArrayLen(&plot->series); iSeries++)
379+
for (int iSeries = 0; iSeries < api->umkaGetDynArrayLen(&plot->series); iSeries++)
380380
{
381381
Series *series = &plot->series.data[iSeries];
382382

@@ -435,6 +435,9 @@ UMPLOT_API void umplot_plot(UmkaStackSlot *params, UmkaStackSlot *result)
435435
{
436436
Plot *plot = (Plot *) params[0].ptrVal;
437437

438+
void *umka = result->ptrVal;
439+
UmkaAPI *api = umkaGetAPI(umka);
440+
438441
SetTraceLogLevel(LOG_ERROR);
439442
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
440443
InitWindow(800, 600, "UmPlot");
@@ -443,12 +446,12 @@ UMPLOT_API void umplot_plot(UmkaStackSlot *params, UmkaStackSlot *result)
443446
Font gridFont = LoadFontFromMemory(".ttf", liberationFont, sizeof(liberationFont), plot->grid.fontSize, NULL, 256);
444447
Font titlesFont = LoadFontFromMemory(".ttf", liberationFont, sizeof(liberationFont), plot->titles.fontSize, NULL, 256);
445448

446-
Rectangle clientRect = getClientRect(plot);
449+
Rectangle clientRect = getClientRect(plot, api);
447450
Rectangle zoomRect = clientRect;
448451
bool showZoomRect = false;
449452

450453
ScreenTransform transform;
451-
resetTransform(plot, &transform);
454+
resetTransform(plot, &transform, api);
452455

453456
while (!WindowShouldClose())
454457
{
@@ -459,8 +462,8 @@ UMPLOT_API void umplot_plot(UmkaStackSlot *params, UmkaStackSlot *result)
459462
// Resizing
460463
if (IsWindowResized())
461464
{
462-
resizeTransform(plot, &transform, &clientRect);
463-
clientRect = zoomRect = getClientRect(plot);
465+
resizeTransform(plot, &transform, &clientRect, api);
466+
clientRect = zoomRect = getClientRect(plot, api);
464467
}
465468

466469
// Zooming
@@ -478,8 +481,8 @@ UMPLOT_API void umplot_plot(UmkaStackSlot *params, UmkaStackSlot *result)
478481

479482
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
480483
{
481-
zoomTransform(plot, &transform, &zoomRect);
482-
zoomRect = getClientRect(plot);
484+
zoomTransform(plot, &transform, &zoomRect, api);
485+
zoomRect = getClientRect(plot, api);
483486
showZoomRect = false;
484487
}
485488

@@ -498,16 +501,16 @@ UMPLOT_API void umplot_plot(UmkaStackSlot *params, UmkaStackSlot *result)
498501

499502
// Grid
500503
int maxYLabelWidth = 0;
501-
drawGrid(plot, &transform, &gridFont, &maxYLabelWidth);
504+
drawGrid(plot, &transform, &gridFont, &maxYLabelWidth, api);
502505

503506
// Graph
504-
drawGraph(plot, &transform);
507+
drawGraph(plot, &transform, api);
505508

506509
// Titles
507-
drawTitles(plot, &transform, &titlesFont, maxYLabelWidth);
510+
drawTitles(plot, &transform, &titlesFont, maxYLabelWidth, api);
508511

509512
// Legend
510-
drawLegend(plot, &gridFont);
513+
drawLegend(plot, &gridFont, api);
511514

512515
// Zoom rectangle
513516
if (showZoomRect)

0 commit comments

Comments
 (0)