rewrite it in c
parent
e5e287e3e8
commit
416dfdd89b
@ -1,36 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <raylib.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include "timer.h"
|
|
||||||
|
|
||||||
int main(void) {
|
|
||||||
|
|
||||||
Timer *t = [[Timer alloc] init];
|
|
||||||
char buffer[100];
|
|
||||||
|
|
||||||
InitWindow(400, 400, "ghost");
|
|
||||||
SetTargetFPS(60);
|
|
||||||
|
|
||||||
Color theColor = BLACK;
|
|
||||||
|
|
||||||
[t start];
|
|
||||||
while (!WindowShouldClose()) {
|
|
||||||
|
|
||||||
if (IsKeyPressed(KEY_SPACE)) {
|
|
||||||
if (t->paused) {
|
|
||||||
[t resume];
|
|
||||||
theColor = BLACK;
|
|
||||||
} else {
|
|
||||||
theColor = RED;
|
|
||||||
[t pause];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[t tick];
|
|
||||||
|
|
||||||
BeginDrawing();
|
|
||||||
ClearBackground(WHITE);
|
|
||||||
DrawText([t timeString], 400/2 - 50, 400/2 - 50, 100, theColor);
|
|
||||||
EndDrawing();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,15 +1,27 @@
|
|||||||
|
SHELL=/bin/sh
|
||||||
CC=gcc
|
CC=gcc
|
||||||
|
AR=
|
||||||
|
|
||||||
INCLUDE= -I /home/sandyx/code/objc/wow/include
|
APP_NAME=ghost
|
||||||
|
|
||||||
main: main.o timer.o
|
SRC=src
|
||||||
$(CC) main.o timer.o -o main -L /home/sandyx/code/objc/wow/lib/linux64 -lraylib -lobjc -lyeslib -lm
|
BUILD=build
|
||||||
|
|
||||||
main.o:
|
C_FILES := $(shell find $(SRC) -name '*.c')
|
||||||
$(CC) $(INCLUDE) main.m -c
|
O_FILES += $(patsubst $(SRC)/%.c, $(BUILD)/%.o, $(C_FILES))
|
||||||
|
|
||||||
timer.o:
|
CFLAGS := -O2 -march=native
|
||||||
$(CC) $(INCLUDE) timer.m -c
|
INCLUDE :=
|
||||||
|
LIB := -lraylib
|
||||||
|
|
||||||
|
|
||||||
|
ghost: $(APP_NAME)
|
||||||
|
|
||||||
|
$(APP_NAME): $(O_FILES)
|
||||||
|
$(CC) $(CFLAGS) $(O_FILES) -o $(APP_NAME) $(LIB)
|
||||||
|
|
||||||
|
$(BUILD)/%.o: $(SRC)/%.c
|
||||||
|
$(CC) $(INCLUDE) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm main *.o
|
rm $(BUILD)/*.o
|
||||||
|
|||||||
@ -1,28 +0,0 @@
|
|||||||
#ifndef CONTROLLER_H
|
|
||||||
#define CONTROLLER_H
|
|
||||||
|
|
||||||
#import <yeslib.h>
|
|
||||||
#import "timer.h"
|
|
||||||
#import "splits.h"
|
|
||||||
//#import <raylib.h>
|
|
||||||
|
|
||||||
/*
|
|
||||||
this thing just decouples the timer from the splits
|
|
||||||
|
|
||||||
could have a method to check whether the timer
|
|
||||||
should stop, like on the last segment
|
|
||||||
*/
|
|
||||||
|
|
||||||
@interface Controller : YSObject {
|
|
||||||
@public
|
|
||||||
Timer *timer;
|
|
||||||
Segment *splits;
|
|
||||||
}
|
|
||||||
|
|
||||||
+(id) new;
|
|
||||||
-(void) startOrSplit;
|
|
||||||
-(void) pauseUnpause;
|
|
||||||
|
|
||||||
@end
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@ -1,42 +0,0 @@
|
|||||||
#import "controller.h"
|
|
||||||
|
|
||||||
@implementation Controller
|
|
||||||
|
|
||||||
+(id) new {
|
|
||||||
self = [Controller alloc];
|
|
||||||
return [self init];
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) startOrSplit {
|
|
||||||
if (timer->running) {
|
|
||||||
[splits setRealtime: [timer time]];
|
|
||||||
splits = [splits next];
|
|
||||||
|
|
||||||
if (timer->paused) {
|
|
||||||
[timer stop];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (splits == NULL) {
|
|
||||||
[timer pause];
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
[timer start];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) pauseUnpause {
|
|
||||||
if (!timer->running) {
|
|
||||||
[timer start];
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!timer->paused) {
|
|
||||||
[timer pause];
|
|
||||||
} else {
|
|
||||||
[timer resume];
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
|
||||||
@ -1,29 +0,0 @@
|
|||||||
#ifndef INPUT_H
|
|
||||||
#define INPUT_H
|
|
||||||
|
|
||||||
#import <yeslib.h>
|
|
||||||
#import <raylib.h>
|
|
||||||
|
|
||||||
/*
|
|
||||||
the input handler will need a way to know
|
|
||||||
what to do when what key is pressed.
|
|
||||||
|
|
||||||
im thinking it could call a method and do
|
|
||||||
a function pointer
|
|
||||||
*/
|
|
||||||
|
|
||||||
typedef struct Yep {
|
|
||||||
id obj;
|
|
||||||
SEL selector;
|
|
||||||
} Yep;
|
|
||||||
|
|
||||||
@interface Input : YSObject {
|
|
||||||
Yep map[256];
|
|
||||||
}
|
|
||||||
|
|
||||||
+(id) new;
|
|
||||||
-(void) handleInput;
|
|
||||||
-(void) setKey: (int) key toPerform: (SEL) sel on: (id) obj;
|
|
||||||
|
|
||||||
@end
|
|
||||||
#endif
|
|
||||||
@ -1,25 +0,0 @@
|
|||||||
#import "input.h"
|
|
||||||
|
|
||||||
@implementation Input
|
|
||||||
|
|
||||||
+(id) new {
|
|
||||||
self = [Input alloc];
|
|
||||||
[self init];
|
|
||||||
return self;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) handleInput {
|
|
||||||
int key = GetKeyPressed();
|
|
||||||
|
|
||||||
if( IsKeyPressed(key) ) {
|
|
||||||
[map[key].obj performSelector: map[key].selector];
|
|
||||||
printf("%s\n", "t");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) setKey: (int) key toPerform: (SEL) sel on: (id) obj {
|
|
||||||
map[key].selector = sel;
|
|
||||||
map[key].obj = obj;
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
#include <raylib.h>
|
||||||
|
#include "timer.h"
|
||||||
|
|
||||||
|
char timer_string[50];
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
InitWindow(640, 480, "Ghost");
|
||||||
|
SetTargetFPS(60);
|
||||||
|
|
||||||
|
GHTimer timer = {0};
|
||||||
|
|
||||||
|
ghtimer_start(&timer);
|
||||||
|
|
||||||
|
while (!WindowShouldClose()) {
|
||||||
|
ghtimer_tick(&timer);
|
||||||
|
ghtimer_timestring(&timer, timer_string);
|
||||||
|
|
||||||
|
BeginDrawing();
|
||||||
|
ClearBackground(RAYWHITE);
|
||||||
|
DrawText(timer_string , 0, 0, 20, BLACK);
|
||||||
|
EndDrawing();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,83 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <raylib.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include "timer.h"
|
|
||||||
#import "splits.h"
|
|
||||||
#include "input.h"
|
|
||||||
#include "controller.h"
|
|
||||||
#import "renderer.h"
|
|
||||||
|
|
||||||
int main(void) {
|
|
||||||
|
|
||||||
Timer *timer = [[Timer alloc] init];
|
|
||||||
Input *input = [Input new];
|
|
||||||
SegmentLinker *linker = [SegmentLinker new];
|
|
||||||
Segment *segment = [Segment new];
|
|
||||||
[segment setName: "cock"];
|
|
||||||
Segment *segment2 = [Segment new];
|
|
||||||
[segment2 setName: "and"];
|
|
||||||
Segment *segment3 = [Segment new];
|
|
||||||
[segment3 setName: "ball"];
|
|
||||||
Segment *segment4 = [Segment new];
|
|
||||||
[segment4 setName: "torture"];
|
|
||||||
Renderer *renderer = [Renderer new];
|
|
||||||
SegmentRenderer *sr = [SegmentRenderer alloc];
|
|
||||||
Controller *control = [Controller new];
|
|
||||||
|
|
||||||
//testing some strings
|
|
||||||
printf("%s\n", [@"constant string" CString]);
|
|
||||||
printf("%s\n", [[@"reversed string" reverse] CString]);
|
|
||||||
printf("%s\n", [[YSString stringFromString: @"woop"] CString]);
|
|
||||||
printf("%s\n", [[YSString stringFromCString: "poop"] CString]);
|
|
||||||
printf("%s\n", [[YSConstantString stringFromCString: "hmmm"] CString]);
|
|
||||||
YSString *str = [YSString stringFromString: @"ball"];
|
|
||||||
|
|
||||||
[@"println" println];
|
|
||||||
[[str cat: @"cat"] println];
|
|
||||||
|
|
||||||
[linker addObject: segment];
|
|
||||||
[linker addObject: segment2];
|
|
||||||
[linker addObject: segment3];
|
|
||||||
[linker addObject: segment4];
|
|
||||||
[linker link];
|
|
||||||
|
|
||||||
[sr initWithSegment: segment];
|
|
||||||
[renderer addObject: sr];
|
|
||||||
|
|
||||||
control->timer = timer;
|
|
||||||
control->splits = segment;
|
|
||||||
|
|
||||||
[input setKey: KEY_SPACE toPerform: @selector(startOrSplit) on: control];
|
|
||||||
|
|
||||||
InitWindow(400, 400, "ghost");
|
|
||||||
SetTargetFPS(60);
|
|
||||||
|
|
||||||
Color theColor = BLACK;
|
|
||||||
|
|
||||||
while (!WindowShouldClose()) {
|
|
||||||
|
|
||||||
[input handleInput];
|
|
||||||
|
|
||||||
/*
|
|
||||||
if (IsKeyPressed(KEY_SPACE)) {
|
|
||||||
if (timer->paused) {
|
|
||||||
[timer resume];
|
|
||||||
theColor = BLACK;
|
|
||||||
} else {
|
|
||||||
theColor = RED;
|
|
||||||
[timer pause];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
[timer tick];
|
|
||||||
|
|
||||||
BeginDrawing();
|
|
||||||
ClearBackground(WHITE);
|
|
||||||
DrawText([timer timeString], 400/2, (400/3) * 2, 100, theColor);
|
|
||||||
[renderer render];
|
|
||||||
EndDrawing();
|
|
||||||
}
|
|
||||||
|
|
||||||
CloseWindow();
|
|
||||||
}
|
|
||||||
@ -1,38 +0,0 @@
|
|||||||
#ifndef RENDERER_H
|
|
||||||
#define RENDERER_H
|
|
||||||
|
|
||||||
#import <yeslib.h>
|
|
||||||
#import <raylib.h>
|
|
||||||
#import "splits.h"
|
|
||||||
#import "timer.h"
|
|
||||||
#import "timeutils.h"
|
|
||||||
|
|
||||||
//meant to be subclassed
|
|
||||||
@interface RenderComponent : YSArray {
|
|
||||||
Rectangle rect;
|
|
||||||
id obj;
|
|
||||||
double fontSize;
|
|
||||||
double spacing;
|
|
||||||
}
|
|
||||||
+(id) new;
|
|
||||||
-(id) init;
|
|
||||||
|
|
||||||
@end
|
|
||||||
|
|
||||||
@interface SegmentRenderer : RenderComponent {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
-(id) initWithSegment: (id) segment;
|
|
||||||
-(void) render;
|
|
||||||
@end
|
|
||||||
|
|
||||||
@interface Renderer : YSArray {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
+(id) new;
|
|
||||||
-(void) render;
|
|
||||||
@end
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@ -1,62 +0,0 @@
|
|||||||
#import "renderer.h"
|
|
||||||
|
|
||||||
@implementation RenderComponent
|
|
||||||
|
|
||||||
+(id) new {
|
|
||||||
return [[RenderComponent alloc] init];
|
|
||||||
}
|
|
||||||
|
|
||||||
-(id) init {
|
|
||||||
self = [super init];
|
|
||||||
|
|
||||||
if (self) {
|
|
||||||
rect = (Rectangle){
|
|
||||||
.x = 0,
|
|
||||||
.y = 0,
|
|
||||||
.height = 20.0,
|
|
||||||
.width = GetScreenWidth(),
|
|
||||||
};
|
|
||||||
|
|
||||||
fontSize = 20;
|
|
||||||
spacing = 20;
|
|
||||||
}
|
|
||||||
|
|
||||||
return self;
|
|
||||||
}
|
|
||||||
@end
|
|
||||||
|
|
||||||
@class Segment;
|
|
||||||
@implementation SegmentRenderer
|
|
||||||
|
|
||||||
-(id) initWithSegment: (id) segment {
|
|
||||||
obj = segment;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) render {
|
|
||||||
id current = obj;
|
|
||||||
|
|
||||||
int idx = 0;
|
|
||||||
while( current != NULL) {
|
|
||||||
DrawText([current name], rect.x + 1, rect.y + (20 * idx), 20.0, BLACK);
|
|
||||||
DrawText([current realtimeString], 300, rect.y + (20 * idx), 20.0, BLACK);
|
|
||||||
current = [current next];
|
|
||||||
idx++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
|
||||||
|
|
||||||
@implementation Renderer
|
|
||||||
|
|
||||||
+(id) new {
|
|
||||||
return [[Renderer alloc] init];
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) render {
|
|
||||||
int i;
|
|
||||||
for (i = 0; i < count; i++) {
|
|
||||||
[array[i] render];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
|
||||||
@ -1,49 +0,0 @@
|
|||||||
#ifndef SPLITS_H
|
|
||||||
#define SPLITS_H
|
|
||||||
|
|
||||||
#import <yeslib.h>
|
|
||||||
#import <raylib.h>
|
|
||||||
#import <time.h>
|
|
||||||
#import <string.h>
|
|
||||||
#import <stdlib.h>
|
|
||||||
|
|
||||||
enum DisplayMode {
|
|
||||||
REALTIME = 0,
|
|
||||||
GAMETIME = 1,
|
|
||||||
};
|
|
||||||
|
|
||||||
@interface Segment : YSObject {
|
|
||||||
char *name;
|
|
||||||
struct timespec realtime;
|
|
||||||
struct timespec gametime;
|
|
||||||
Segment *next;
|
|
||||||
}
|
|
||||||
|
|
||||||
+(id) new;
|
|
||||||
-(id) init;
|
|
||||||
|
|
||||||
-(Segment *) next;
|
|
||||||
-(void) setNext: (Segment *) n;
|
|
||||||
|
|
||||||
-(char *) name;
|
|
||||||
-(void) setName: (char *) n;
|
|
||||||
|
|
||||||
-(struct timespec) realtime;
|
|
||||||
-(void) setRealtime: (struct timespec) r;
|
|
||||||
|
|
||||||
-(struct timespec) gametime;
|
|
||||||
-(void) setGametime: (struct timespec) r;
|
|
||||||
|
|
||||||
-(char *) realtimeString;
|
|
||||||
-(char *) gametimeString;
|
|
||||||
|
|
||||||
@end
|
|
||||||
|
|
||||||
|
|
||||||
@interface SegmentLinker : YSArray {
|
|
||||||
|
|
||||||
}
|
|
||||||
+(id) new;
|
|
||||||
-(void) link;
|
|
||||||
@end
|
|
||||||
#endif
|
|
||||||
@ -1,86 +0,0 @@
|
|||||||
#import "splits.h"
|
|
||||||
#import "timeutils.h"
|
|
||||||
|
|
||||||
@implementation Segment
|
|
||||||
+(id) new {
|
|
||||||
return [[[self class] alloc] init];
|
|
||||||
}
|
|
||||||
|
|
||||||
-(id) init {
|
|
||||||
self = [super init];
|
|
||||||
|
|
||||||
if (self) {
|
|
||||||
name = malloc(30);
|
|
||||||
memcpy(name, "cocks", 6);
|
|
||||||
}
|
|
||||||
|
|
||||||
return self;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(Segment *) next {
|
|
||||||
return next;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) setNext: (Segment *) n {
|
|
||||||
next = n;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) dealloc {
|
|
||||||
free(name);
|
|
||||||
[super dealloc];
|
|
||||||
}
|
|
||||||
|
|
||||||
-(char *) name {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) setName: (char *) n {
|
|
||||||
name = n;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(struct timespec) realtime {
|
|
||||||
return realtime;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) setRealtime: (struct timespec) r {
|
|
||||||
realtime = r;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(struct timespec) gametime {
|
|
||||||
return gametime;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) setGametime: (struct timespec) r {
|
|
||||||
gametime = r;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(char *) realtimeString {
|
|
||||||
static char buffer[100];
|
|
||||||
sprintf(buffer, "%.02lf", timespec_to_double(realtime));
|
|
||||||
return buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(char *) gametimeString {
|
|
||||||
static char buffer[100];
|
|
||||||
sprintf(buffer, "%.02lf", timespec_to_double(gametime));
|
|
||||||
return buffer;
|
|
||||||
}
|
|
||||||
@end
|
|
||||||
|
|
||||||
@implementation SegmentLinker
|
|
||||||
|
|
||||||
+(id) new {
|
|
||||||
return [[SegmentLinker alloc] init];
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) link {
|
|
||||||
Segment *current = array[0];
|
|
||||||
|
|
||||||
int i;
|
|
||||||
for (i = 1; i < count; i++) {
|
|
||||||
[current setNext: array[i]];
|
|
||||||
current = array[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
|
||||||
@ -1,28 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include "timeutils.h"
|
|
||||||
|
|
||||||
//appended GH just incase theres a conflict with anything
|
|
||||||
//else named "Timer"
|
|
||||||
|
|
||||||
typedef struct GHTimer {
|
|
||||||
struct timespec start;
|
|
||||||
struct timespec current;
|
|
||||||
struct timespec pause_start;
|
|
||||||
struct timespec pause_current;
|
|
||||||
struct timespec diff;
|
|
||||||
int running;
|
|
||||||
int paused;
|
|
||||||
} GHTimer;
|
|
||||||
|
|
||||||
void ghtimer_start(GHTimer *timer);
|
|
||||||
void ghtimer_pause(GHTimer *timer);
|
|
||||||
void ghtimer_resume(GHTimer *timer);
|
|
||||||
void ghtimer_stop(GHTimer *timer);
|
|
||||||
void ghtimer_reset(GHTimer *timer);
|
|
||||||
void ghtimer_tick(GHTimer *timer);
|
|
||||||
|
|
||||||
struct timespec ghtimer_time(GHTimer *timer);
|
|
||||||
void ghtimer_timestring(GHTimer *timer);
|
|
||||||
@ -1,78 +0,0 @@
|
|||||||
#import "timer.h"
|
|
||||||
#import "timeutils.h"
|
|
||||||
|
|
||||||
@implementation Timer
|
|
||||||
-(id) init {
|
|
||||||
self = [super init];
|
|
||||||
running = false;
|
|
||||||
paused = false;
|
|
||||||
clock_gettime(CLOCK_REALTIME, &pause_start);
|
|
||||||
pause_current = pause_start;
|
|
||||||
clock_gettime(CLOCK_REALTIME, &start);
|
|
||||||
current = start;
|
|
||||||
diff = (struct timespec){0};
|
|
||||||
return self;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) start {
|
|
||||||
clock_gettime(CLOCK_REALTIME, &start);
|
|
||||||
running = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) pause {
|
|
||||||
clock_gettime(CLOCK_REALTIME, &pause_start);
|
|
||||||
paused = true;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) resume {
|
|
||||||
struct timespec t = subts(pause_current, pause_start);
|
|
||||||
diff = addts(diff, t);
|
|
||||||
paused = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) stop {
|
|
||||||
running = false;
|
|
||||||
paused = false;
|
|
||||||
clock_gettime(CLOCK_REALTIME, &pause_start);
|
|
||||||
clock_gettime(CLOCK_REALTIME, &start);
|
|
||||||
pause_current = pause_start;
|
|
||||||
current = start;
|
|
||||||
diff = (struct timespec){0};
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) reset {
|
|
||||||
running = false;
|
|
||||||
start = (struct timespec){0};
|
|
||||||
current = start;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) tick {
|
|
||||||
if (!running)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (!paused) {
|
|
||||||
clock_gettime(CLOCK_REALTIME, ¤t);
|
|
||||||
} else {
|
|
||||||
clock_gettime(CLOCK_REALTIME, &pause_current);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
-(struct timespec) time {
|
|
||||||
struct timespec time = subts(current, start);
|
|
||||||
return subts(time, diff);
|
|
||||||
}
|
|
||||||
|
|
||||||
-(char *) timeString {
|
|
||||||
//super simple format
|
|
||||||
static char buffer[100];
|
|
||||||
struct timespec time = subts(current, start);
|
|
||||||
time = subts(time, diff);
|
|
||||||
|
|
||||||
double dbl = timespec_to_double(time);
|
|
||||||
|
|
||||||
sprintf(buffer, "%.02lf", dbl);
|
|
||||||
return buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
|
||||||
@ -1,19 +0,0 @@
|
|||||||
#include "timeutils.h"
|
|
||||||
|
|
||||||
struct timespec subts(struct timespec t1, struct timespec t2) {
|
|
||||||
return (struct timespec) {
|
|
||||||
.tv_sec = t1.tv_sec - t2.tv_sec,
|
|
||||||
.tv_nsec = t1.tv_nsec - t2.tv_nsec,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
struct timespec addts(struct timespec t1, struct timespec t2) {
|
|
||||||
return (struct timespec) {
|
|
||||||
.tv_sec = t1.tv_sec + t2.tv_sec,
|
|
||||||
.tv_nsec = t1.tv_nsec + t2.tv_nsec,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
double timespec_to_double(struct timespec ts) {
|
|
||||||
return ((double)(ts.tv_sec) + ((double)(ts.tv_nsec) / NSEC_PER_SEC));
|
|
||||||
}
|
|
||||||
@ -1,11 +0,0 @@
|
|||||||
#ifndef TIMEUTILS_H
|
|
||||||
#define TIMEUTILS_H
|
|
||||||
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
#define NSEC_PER_SEC 1000000000
|
|
||||||
|
|
||||||
struct timespec subts(struct timespec t1, struct timespec t2);
|
|
||||||
struct timespec addts(struct timespec t1, struct timespec t2);
|
|
||||||
double timespec_to_double(struct timespec ts);
|
|
||||||
#endif
|
|
||||||
@ -1,35 +0,0 @@
|
|||||||
#ifndef TIMER_H
|
|
||||||
#define TIMER_H
|
|
||||||
|
|
||||||
#include <time.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <yeslib.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#define NSEC_PER_SEC 1000000000
|
|
||||||
|
|
||||||
@interface Timer : YSObject {
|
|
||||||
@public
|
|
||||||
struct timespec start;
|
|
||||||
struct timespec current;
|
|
||||||
struct timespec pause_start;
|
|
||||||
struct timespec pause_current;
|
|
||||||
struct timespec diff;
|
|
||||||
int running;
|
|
||||||
int paused;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) start;
|
|
||||||
-(void) pause;
|
|
||||||
-(void) resume;
|
|
||||||
-(void) stop;
|
|
||||||
-(void) tick;
|
|
||||||
|
|
||||||
//returns current minus start;
|
|
||||||
-(struct timespec) time;
|
|
||||||
-(char *) timeString;
|
|
||||||
|
|
||||||
@end
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@ -1,87 +0,0 @@
|
|||||||
#import "timer.h"
|
|
||||||
|
|
||||||
struct timespec subts(struct timespec t1, struct timespec t2) {
|
|
||||||
return (struct timespec) {
|
|
||||||
.tv_sec = t1.tv_sec - t2.tv_sec,
|
|
||||||
.tv_nsec = t1.tv_nsec - t2.tv_nsec,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
struct timespec addts(struct timespec t1, struct timespec t2) {
|
|
||||||
return (struct timespec) {
|
|
||||||
.tv_sec = t1.tv_sec + t2.tv_sec,
|
|
||||||
.tv_nsec = t1.tv_nsec + t2.tv_nsec,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
double timespec_to_double(struct timespec ts) {
|
|
||||||
return ((double)(ts.tv_sec) + ((double)(ts.tv_nsec) / NSEC_PER_SEC));
|
|
||||||
}
|
|
||||||
|
|
||||||
@implementation Timer
|
|
||||||
-(id) init {
|
|
||||||
self = [super init];
|
|
||||||
running = false;
|
|
||||||
paused = false;
|
|
||||||
pause_start = (struct timespec){0};
|
|
||||||
pause_current = (struct timespec){0};
|
|
||||||
start = (struct timespec){0};
|
|
||||||
current = (struct timespec){0};
|
|
||||||
diff = (struct timespec){0};
|
|
||||||
return self;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) start {
|
|
||||||
clock_gettime(CLOCK_REALTIME, &start);
|
|
||||||
running = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) pause {
|
|
||||||
clock_gettime(CLOCK_REALTIME, &pause_start);
|
|
||||||
paused = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) resume {
|
|
||||||
struct timespec t = subts(pause_current, pause_start);
|
|
||||||
diff = addts(diff, t);
|
|
||||||
paused = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) stop {
|
|
||||||
running = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) reset {
|
|
||||||
running = false;
|
|
||||||
start = (struct timespec){0};
|
|
||||||
current = start;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void) tick {
|
|
||||||
if (!paused) {
|
|
||||||
clock_gettime(CLOCK_REALTIME, ¤t);
|
|
||||||
} else {
|
|
||||||
clock_gettime(CLOCK_REALTIME, &pause_current);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
-(struct timespec) time {
|
|
||||||
//time running = (current - start) - pause
|
|
||||||
struct timespec time = subts(current, start);
|
|
||||||
return subts(time, diff);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
-(char *) timeString {
|
|
||||||
//super simple format
|
|
||||||
static char buffer[100];
|
|
||||||
struct timespec time = subts(current, start);
|
|
||||||
time = subts(time, diff);
|
|
||||||
|
|
||||||
double dbl = timespec_to_double(time);
|
|
||||||
|
|
||||||
sprintf(buffer, "%.02lf", dbl);
|
|
||||||
return buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
|
||||||
Loading…
Reference in New Issue