first commit

main
sandyx86 1 year ago
commit da72101114

@ -0,0 +1,44 @@
#include <stdio.h>
#include <raylib.h>
#include <stdbool.h>
#include "timer.h"
static inline struct timespec subts(volatile struct timespec t1, volatile struct timespec t2) {
return (struct timespec) {
.tv_sec = t2.tv_sec - t1.tv_sec,
.tv_nsec = t2.tv_nsec - t1.tv_nsec,
};
}
int main(void) {
Timer *t = [[Timer alloc] init];
char buffer[100];
InitWindow(400, 400, "raytime");
SetTargetFPS(30);
Color theColor = BLACK;
[t start];
while (!WindowShouldClose()) {
sprintf(buffer, "%jd", (intmax_t)[t time].tv_sec);
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();
}
}

@ -0,0 +1,15 @@
CC=g++
INCLUDE= -I /home/sandyx/code/objc/wow/include
main: main.o timer.o
g++ main.o timer.o -o main -L /home/sandyx/code/objc/wow/lib/linux64 -lraylib -lobjc -lyeslib
main.o:
g++ $(INCLUDE) main.mm -c
timer.o:
g++ $(INCLUDE) timer.mm -c
clean:
rm main *.o

@ -0,0 +1,34 @@
#ifndef TIMER_H
#define TIMER_H
#include <time.h>
#include <stdint.h>
#include <yeslib.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

@ -0,0 +1,99 @@
#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 negts(struct timespec t) {
return (struct timespec) {
.tv_sec = -t.tv_sec,
.tv_nsec = -t.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,
};
}
void print_ts(struct timespec t) {
printf("time: %ld.%ld\n", (unsigned long)t.tv_sec, (unsigned long)t.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 = {0};
pause_current = {0};
start = {0};
current = {0};
diff = {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, &current);
} else {
clock_gettime(CLOCK_REALTIME, &pause_current);
//pause_current = subts(pause_current, pause_start);
}
}
-(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…
Cancel
Save