commit c67b58eb21cd244dd4c3cde872a6f9aa61abd872
parent ee9b0e36a68efd9c821a0200df1b608542cb9bbd
Author: Pavel Renev <an2qzavok@gmail.com>
Date: Sat, 21 Aug 2021 19:42:28 +0000
some refactoring, better fps counter
Diffstat:
6 files changed, 36 insertions(+), 46 deletions(-)
diff --git a/mkfile b/mkfile
@@ -1,7 +1,8 @@
</$objtype/mkfile
TARG=noise1 noise2 noise2rgb
-OFILES=
+OFILES=util.$O
+HFILES=noise.h
BIN=/$objtype/bin
</sys/src/cmd/mkmany
diff --git a/noise.h b/noise.h
@@ -0,0 +1 @@
+void drawfps(Image *);
+\ No newline at end of file
diff --git a/noise1.c b/noise1.c
@@ -7,6 +7,8 @@
#include <draw.h>
#include <mouse.h>
+#include "noise.h"
+
Image *Ibuf;
long bn;
u8int *buf;
@@ -24,18 +26,12 @@ void
threadmain(int, char**)
{
int rd;
- int fps, fn;
- long t, ot;
Mousectl *mctl;
Image *It;
- char text[16];
initdraw(nil, nil, "noise");
mctl = initmouse(0, screen);
rd = open("/dev/random", OREAD);
It = allocimage(display, Rect(0,0,1,1), RGB24, 1, DYellow);
- ot = time(0);
- fn = 0;
- fps = 0;
resize();
for(;;){
if (nbrecv(mctl->resizec, nil) != 0) {
@@ -47,15 +43,7 @@ threadmain(int, char**)
readn(rd, buf, bn);
loadimage(Ibuf, Ibuf->r, buf, bn);
draw(screen, screen->r, Ibuf, 0, Ibuf->r.min);
- fn++;
- t = time(0);
- if (t > ot) {
- ot = t;
- fps = fn;
- fn = 0;
- };
- snprint(text, 16, "fps: %d", fps);
- string(screen, screen->r.min, It, ZP, font, text);
+ drawfps(It);
flushimage(display, 1);
}
}
\ No newline at end of file
diff --git a/noise2.c b/noise2.c
@@ -7,6 +7,8 @@
#include <draw.h>
#include <mouse.h>
+#include "noise.h"
+
Image *Ibuf;
long bn;
u8int *buf;
@@ -37,18 +39,12 @@ procread(void *v)
void
threadmain(int, char**)
{
- int fps, fn;
- long t, ot;
Mousectl *mctl;
Image *It;
- char text[16];
Channel *c;
initdraw(nil, nil, "noise");
mctl = initmouse(0, screen);
It = allocimage(display, Rect(0,0,1,1), RGB24, 1, DYellow);
- ot = time(0);
- fn = 0;
- fps = 0;
resize();
c = chancreate(sizeof(int), 0);
proccreate(procread, c, 8 * 1024);
@@ -62,15 +58,7 @@ threadmain(int, char**)
recv(c, nil);
loadimage(Ibuf, Ibuf->r, buf, bn);
draw(screen, screen->r, Ibuf, 0, Ibuf->r.min);
- fn++;
- t = time(0);
- if (t > ot) {
- ot = t;
- fps = fn;
- fn = 0;
- };
- snprint(text, 16, "fps: %d", fps);
- string(screen, screen->r.min, It, ZP, font, text);
+ drawfps(It);
flushimage(display, 1);
}
}
\ No newline at end of file
diff --git a/noise2rgb.c b/noise2rgb.c
@@ -7,6 +7,8 @@
#include <draw.h>
#include <mouse.h>
+#include "noise.h"
+
Image *Ibuf;
long bn;
u8int *buf;
@@ -37,18 +39,12 @@ procread(void *v)
void
threadmain(int, char**)
{
- int fps, fn;
- long t, ot;
Mousectl *mctl;
Image *It;
- char text[16];
Channel *c;
initdraw(nil, nil, "noise");
mctl = initmouse(0, screen);
It = allocimage(display, Rect(0,0,1,1), RGB24, 1, DYellow);
- ot = time(0);
- fn = 0;
- fps = 0;
resize();
c = chancreate(sizeof(int), 0);
proccreate(procread, c, 8 * 1024);
@@ -62,15 +58,7 @@ threadmain(int, char**)
recv(c, nil);
loadimage(Ibuf, Ibuf->r, buf, bn);
draw(screen, screen->r, Ibuf, 0, Ibuf->r.min);
- fn++;
- t = time(0);
- if (t > ot) {
- ot = t;
- fps = fn;
- fn = 0;
- };
- snprint(text, 16, "fps: %d", fps);
- string(screen, screen->r.min, It, ZP, font, text);
+ drawfps(It);
flushimage(display, 1);
}
}
diff --git a/util.c b/util.c
@@ -0,0 +1,22 @@
+#include <u.h>
+#include <libc.h>
+
+#include <thread.h>
+#include <draw.h>
+
+#include "noise.h"
+
+void
+drawfps(Image *i)
+{
+ static vlong tnew, told, avg;
+ char buf[64];
+
+ told = tnew;
+ tnew = nsec();
+
+ avg = tnew - told;//(avg + tnew - told) / 2;
+
+ snprint(buf, 64, "%f", 1000000000.0/(double)avg);
+ string(screen, screen->r.min, i, ZP, font, buf);
+}
+\ No newline at end of file