commit da72101114a4f0460631e68e76e4d7ffd9646347 Author: sandyx86 Date: Sun Aug 11 16:51:29 2024 -0500 first commit diff --git a/main.mm b/main.mm new file mode 100644 index 0000000..4683ee1 --- /dev/null +++ b/main.mm @@ -0,0 +1,44 @@ +#include +#include +#include +#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(); + } +} diff --git a/makefile b/makefile new file mode 100755 index 0000000..6b027bb --- /dev/null +++ b/makefile @@ -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 \ No newline at end of file diff --git a/timer.h b/timer.h new file mode 100644 index 0000000..de2b70a --- /dev/null +++ b/timer.h @@ -0,0 +1,34 @@ +#ifndef TIMER_H +#define TIMER_H + +#include +#include +#include +#include + +#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 diff --git a/timer.mm b/timer.mm new file mode 100644 index 0000000..d04a6b2 --- /dev/null +++ b/timer.mm @@ -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, ¤t); + } 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