Optimize
This commit is contained in:
parent
f5f7c50d7f
commit
15c1bfda44
2
Makefile
2
Makefile
@ -1,7 +1,7 @@
|
|||||||
OBJ = $(patsubst src/%.c,%.o,$(wildcard src/*.c))
|
OBJ = $(patsubst src/%.c,%.o,$(wildcard src/*.c))
|
||||||
HEADERS = $(wildcard src/*.h)
|
HEADERS = $(wildcard src/*.h)
|
||||||
CC = clang
|
CC = clang
|
||||||
FLAGS = -std=c17 -O2 -pthread
|
FLAGS = -std=c17 -O3 -pthread
|
||||||
LIB = -lncurses -lnotcurses -lnotcurses-core
|
LIB = -lncurses -lnotcurses -lnotcurses-core
|
||||||
|
|
||||||
%.o: src/%.c $(HEADERS)
|
%.o: src/%.c $(HEADERS)
|
||||||
|
6
compile_commands.json
Normal file
6
compile_commands.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"arguments": [ "/usr/bin/gcc", "-c", "-o", "src/main.o", "src/main.c" ],
|
||||||
|
"file": "src/main.c"
|
||||||
|
}
|
||||||
|
]
|
53
src/main.c
53
src/main.c
@ -16,6 +16,7 @@ You should have received a copy of the GNU General Public License along with thi
|
|||||||
#include <notcurses/nckeys.h>
|
#include <notcurses/nckeys.h>
|
||||||
#include <notcurses/notcurses.h>
|
#include <notcurses/notcurses.h>
|
||||||
|
|
||||||
|
// 250000000ns = 250ms, 1/4s
|
||||||
enum {SECS_TO_SLEEP = 0, NSEC_TO_SLEEP = 250000000};
|
enum {SECS_TO_SLEEP = 0, NSEC_TO_SLEEP = 250000000};
|
||||||
|
|
||||||
struct notcurses *_nc;
|
struct notcurses *_nc;
|
||||||
@ -217,7 +218,7 @@ struct bignumber_t *bignumber(int start, uint mimick) {
|
|||||||
void drawbignumber(struct bignumber_t *bn, int sec, int startx, int starty, struct ncplane *plane) {
|
void drawbignumber(struct bignumber_t *bn, int sec, int startx, int starty, struct ncplane *plane) {
|
||||||
for (int x = 0; x < 3; x++) {
|
for (int x = 0; x < 3; x++) {
|
||||||
for (int y = 0; y < 5; y++) {
|
for (int y = 0; y < 5; y++) {
|
||||||
struct string *padded_num = pad_int(bn->matrix[y][x], 2, 2);
|
struct string *padded_num = pad_int(bn->matrix[y][x]);
|
||||||
uint currentSec = (uint)sec == bn->matrix[y][x];
|
uint currentSec = (uint)sec == bn->matrix[y][x];
|
||||||
uint partOfMimick = mimicks[bn->mimick][y][x];
|
uint partOfMimick = mimicks[bn->mimick][y][x];
|
||||||
|
|
||||||
@ -245,53 +246,13 @@ void drawbignumber(struct bignumber_t *bn, int sec, int startx, int starty, stru
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// pad_uint
|
struct string *pad_int(int n) {
|
||||||
struct string *pad_int(int n, uint pad, uint maxsize) {
|
struct string *str = alloc_string(4);
|
||||||
struct string *str = (struct string *)malloc(sizeof (struct string) + 1);
|
snprintf(str->s, 4, "%02i%c", abs(n), 0);
|
||||||
maxsize++;
|
|
||||||
|
|
||||||
str->s = (char *)malloc((sizeof(char) * maxsize) + 1);
|
return str;
|
||||||
str->length = 0;
|
};
|
||||||
|
|
||||||
memset(str->s, 0, maxsize);
|
|
||||||
|
|
||||||
// Figure out how many place values are in n
|
|
||||||
int _n = abs(n);
|
|
||||||
uint places = 1;
|
|
||||||
|
|
||||||
for (uint i = 0; i < pad; i++) {
|
|
||||||
if (_n >= 10) {
|
|
||||||
_n /= 10;
|
|
||||||
places++;
|
|
||||||
}
|
|
||||||
else break;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *padding = (char *)malloc((sizeof(char) * pad) + 2);
|
|
||||||
for (uint i = 0; i < pad + 1; i++)
|
|
||||||
padding[i] = '0';
|
|
||||||
|
|
||||||
uint required_padding = pad - places;
|
|
||||||
if (required_padding > pad) required_padding = 0;
|
|
||||||
|
|
||||||
// Negative value
|
|
||||||
if (n < 0) {
|
|
||||||
required_padding++;
|
|
||||||
padding[0] = '-';
|
|
||||||
}
|
|
||||||
|
|
||||||
snprintf(str->s, maxsize, "%*.*s%i%c", 0, required_padding, padding, abs(n), 0);
|
|
||||||
free(padding);
|
|
||||||
|
|
||||||
for (uint i = 0; i < maxsize + 1; i++) {
|
|
||||||
if (str->s[i] == 0) {
|
|
||||||
str->length = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
// alloc_string
|
// alloc_string
|
||||||
struct string *alloc_string(uint l) {
|
struct string *alloc_string(uint l) {
|
||||||
|
@ -15,7 +15,6 @@ You should have received a copy of the GNU General Public License along with thi
|
|||||||
#define __MAIN_H__
|
#define __MAIN_H__
|
||||||
|
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
#include <math.h>
|
|
||||||
#include <memory.h>
|
#include <memory.h>
|
||||||
// #include <ncurses.h>
|
// #include <ncurses.h>
|
||||||
#include <notcurses/notcurses.h>
|
#include <notcurses/notcurses.h>
|
||||||
@ -45,7 +44,7 @@ struct string *alloc_string(uint l);
|
|||||||
void free_string(struct string *s);
|
void free_string(struct string *s);
|
||||||
|
|
||||||
// pads a number with 0's
|
// pads a number with 0's
|
||||||
struct string *pad_int(int n, uint pad, uint maxsize);
|
struct string *pad_int(int n);
|
||||||
|
|
||||||
// this creates a bignumber_t
|
// this creates a bignumber_t
|
||||||
struct bignumber_t *bignumber(int start, uint mimick);
|
struct bignumber_t *bignumber(int start, uint mimick);
|
||||||
|
Loading…
Reference in New Issue
Block a user