aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2017-04-21 18:26:22 +0200
committerAlex Auvolat <alex@adnab.me>2017-04-21 18:26:22 +0200
commited153e6a5b9f001bbe49128e6bc53053f55dd37b (patch)
tree9a9bbe3551ea1b5de71aa3c358fd13f46c603191 /src/lib
parentf8334e283c5eb0efeb4bb8a134041e18388d5f01 (diff)
downloadkogata-ed153e6a5b9f001bbe49128e6bc53053f55dd37b.tar.gz
kogata-ed153e6a5b9f001bbe49128e6bc53053f55dd37b.zip
Can plot on a surface from lua
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/bam.lua4
-rw-r--r--src/lib/include/kogata/draw.h12
-rw-r--r--src/lib/libkogata/draw.c30
3 files changed, 32 insertions, 14 deletions
diff --git a/src/lib/bam.lua b/src/lib/bam.lua
index b7bee20..5d749b9 100644
--- a/src/lib/bam.lua
+++ b/src/lib/bam.lua
@@ -9,7 +9,7 @@ return function(s, common)
return {
libc = libc,
- libkogata = {lib('libkogata'), libc},
- liblua = {lib('lua'), libc}
+ libkogata = {lib('libkogata')},
+ liblua = {lib('lua')}
}
end
diff --git a/src/lib/include/kogata/draw.h b/src/lib/include/kogata/draw.h
index fb9fe05..d00036f 100644
--- a/src/lib/include/kogata/draw.h
+++ b/src/lib/include/kogata/draw.h
@@ -12,6 +12,9 @@ typedef struct {
fd_t fd;
uint8_t* data;
+
+ int nrefs;
+ bool own_data;
} fb_t;
typedef struct font font_t;
@@ -21,9 +24,10 @@ typedef uint32_t color_t; // a color is always linked to a FB on which it is to
// ---- Buffer creation
fb_t *g_fb_from_file(fd_t file, fb_info_t *geom);
-fb_t *g_fb_from_mem(uint8_t* region, fb_info_t *geom);
+fb_t *g_fb_from_mem(uint8_t* region, fb_info_t *geom, bool own_data);
-void g_delete_fb(fb_t *fb);
+void g_incref_fb(fb_t *fb);
+void g_decref_fb(fb_t *fb);
// ---- Color manipulation
@@ -39,8 +43,8 @@ void g_line(fb_t *fb, int x1, int y1, int x2, int y2, color_t c);
void g_rect(fb_t *fb, int x, int y, int w, int h, color_t c);
void g_fillrect(fb_t *fb, int x, int y, int w, int h, color_t c);
-void g_rect_r(fb_t *fb, fb_region_t reg, color_t c);
-void g_fillrect_r(fb_t *fb, fb_region_t reg, color_t c);
+void g_rectregion(fb_t *fb, fb_region_t reg, color_t c);
+void g_fillregion(fb_t *fb, fb_region_t reg, color_t c);
void g_circle(fb_t *fb, int cx, int cy, int r, color_t c);
void g_fillcircle(fb_t *fb, int cx, int cy, int r, color_t c);
diff --git a/src/lib/libkogata/draw.c b/src/lib/libkogata/draw.c
index 6aa3387..d9a4268 100644
--- a/src/lib/libkogata/draw.c
+++ b/src/lib/libkogata/draw.c
@@ -22,6 +22,8 @@ fb_t *g_fb_from_file(fd_t file, fb_info_t *geom) {
bool map_ok = sc_mmap_file(file, 0, ret->data, geom->height * geom->pitch, MM_READ | MM_WRITE);
if (!map_ok) goto error;
+ ret->nrefs = 1;
+
return ret;
error:
@@ -30,7 +32,7 @@ error:
return 0;
}
-fb_t *g_fb_from_mem(uint8_t* data, fb_info_t *geom) {
+fb_t *g_fb_from_mem(uint8_t* data, fb_info_t *geom, bool own_data) {
fb_t *ret = (fb_t*)malloc(sizeof(fb_t));
if (ret == 0) return 0;
@@ -38,15 +40,27 @@ fb_t *g_fb_from_mem(uint8_t* data, fb_info_t *geom) {
ret->fd = 0;
ret->data = data;
+ ret->nrefs = 1;
+ ret->own_data = own_data;
+
return ret;
}
-void g_delete_fb(fb_t *fb) {
- if (fb->fd != 0) {
- sc_munmap(fb->data);
- region_free(fb->data);
+void g_incref_fb(fb_t *fb) {
+ fb->nrefs++;
+}
+
+void g_decref_fb(fb_t *fb) {
+ fb->nrefs--;
+ if (fb->nrefs == 0) {
+ if (fb->fd != 0) {
+ sc_munmap(fb->data);
+ region_free(fb->data);
+ } else if (fb->own_data) {
+ free(fb->data);
+ }
+ free(fb);
}
- free(fb);
}
// ---- Color management
@@ -168,11 +182,11 @@ void g_fillrect(fb_t *fb, int x, int y, int w, int h, color_t c) {
}
}
-void g_rect_r(fb_t *fb, fb_region_t reg, color_t c) {
+void g_rectregion(fb_t *fb, fb_region_t reg, color_t c) {
g_rect(fb, reg.x, reg.y, reg.w, reg.h, c);
}
-void g_fillrect_r(fb_t *fb, fb_region_t reg, color_t c) {
+void g_fillregion(fb_t *fb, fb_region_t reg, color_t c) {
g_fillrect(fb, reg.x, reg.y, reg.w, reg.h, c);
}