From 416dfdd89b67bfd4381138f9eb263de206f65e72 Mon Sep 17 00:00:00 2001 From: sandyx Date: Sat, 31 Aug 2024 16:53:12 -0500 Subject: [PATCH] rewrite it in c --- main.m | 36 -------------------- makefile | 28 +++++++++++----- src/controller.h | 28 ---------------- src/controller.m | 42 ----------------------- src/input.h | 29 ---------------- src/input.m | 25 -------------- src/main.c | 23 +++++++++++++ src/main.m | 83 --------------------------------------------- src/renderer.h | 38 --------------------- src/renderer.m | 62 ---------------------------------- src/splits.h | 49 --------------------------- src/splits.m | 86 ----------------------------------------------- src/timer.c | 23 +++++++++++-- src/timer.c.h | 28 ---------------- src/timer.h | 38 +++++++++++---------- src/timer.m | 78 ------------------------------------------- src/timeutils.c | 19 ----------- src/timeutils.h | 11 ------ timer.h | 35 ------------------- timer.m | 87 ------------------------------------------------ 20 files changed, 85 insertions(+), 763 deletions(-) delete mode 100644 main.m delete mode 100644 src/controller.h delete mode 100644 src/controller.m delete mode 100644 src/input.h delete mode 100644 src/input.m create mode 100644 src/main.c delete mode 100644 src/main.m delete mode 100644 src/renderer.h delete mode 100644 src/renderer.m delete mode 100644 src/splits.h delete mode 100644 src/splits.m delete mode 100644 src/timer.c.h delete mode 100644 src/timer.m delete mode 100644 src/timeutils.c delete mode 100644 src/timeutils.h delete mode 100644 timer.h delete mode 100644 timer.m diff --git a/main.m b/main.m deleted file mode 100644 index 6a8c591..0000000 --- a/main.m +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include -#include -#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(); - } -} diff --git a/makefile b/makefile index a37a2e7..48694d8 100755 --- a/makefile +++ b/makefile @@ -1,15 +1,27 @@ +SHELL=/bin/sh CC=gcc +AR= -INCLUDE= -I /home/sandyx/code/objc/wow/include +APP_NAME=ghost -main: main.o timer.o - $(CC) main.o timer.o -o main -L /home/sandyx/code/objc/wow/lib/linux64 -lraylib -lobjc -lyeslib -lm +SRC=src +BUILD=build -main.o: - $(CC) $(INCLUDE) main.m -c +C_FILES := $(shell find $(SRC) -name '*.c') +O_FILES += $(patsubst $(SRC)/%.c, $(BUILD)/%.o, $(C_FILES)) -timer.o: - $(CC) $(INCLUDE) timer.m -c +CFLAGS := -O2 -march=native +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: - rm main *.o \ No newline at end of file + rm $(BUILD)/*.o diff --git a/src/controller.h b/src/controller.h deleted file mode 100644 index 5325763..0000000 --- a/src/controller.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef CONTROLLER_H -#define CONTROLLER_H - -#import -#import "timer.h" -#import "splits.h" -//#import - -/* - 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 \ No newline at end of file diff --git a/src/controller.m b/src/controller.m deleted file mode 100644 index ea74f88..0000000 --- a/src/controller.m +++ /dev/null @@ -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 \ No newline at end of file diff --git a/src/input.h b/src/input.h deleted file mode 100644 index d3902b3..0000000 --- a/src/input.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef INPUT_H -#define INPUT_H - -#import -#import - -/* - 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 diff --git a/src/input.m b/src/input.m deleted file mode 100644 index 7afb123..0000000 --- a/src/input.m +++ /dev/null @@ -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 \ No newline at end of file diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..071e95f --- /dev/null +++ b/src/main.c @@ -0,0 +1,23 @@ +#include +#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(); + } +} diff --git a/src/main.m b/src/main.m deleted file mode 100644 index a64042d..0000000 --- a/src/main.m +++ /dev/null @@ -1,83 +0,0 @@ -#include -#include -#include -#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(); -} diff --git a/src/renderer.h b/src/renderer.h deleted file mode 100644 index 287ce3a..0000000 --- a/src/renderer.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef RENDERER_H -#define RENDERER_H - -#import -#import -#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 \ No newline at end of file diff --git a/src/renderer.m b/src/renderer.m deleted file mode 100644 index b6e29c4..0000000 --- a/src/renderer.m +++ /dev/null @@ -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 \ No newline at end of file diff --git a/src/splits.h b/src/splits.h deleted file mode 100644 index 3f8c1cc..0000000 --- a/src/splits.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef SPLITS_H -#define SPLITS_H - -#import -#import -#import -#import -#import - -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 \ No newline at end of file diff --git a/src/splits.m b/src/splits.m deleted file mode 100644 index aef648d..0000000 --- a/src/splits.m +++ /dev/null @@ -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 \ No newline at end of file diff --git a/src/timer.c b/src/timer.c index caa0008..e426b9d 100644 --- a/src/timer.c +++ b/src/timer.c @@ -1,4 +1,23 @@ -#include "timer.c.h" +#include "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)); +} + GHTimer ghtimer_init(void) { GHTimer timer = {0}; @@ -64,4 +83,4 @@ void ghtimer_timestring(GHTimer *timer, char *buffer) { struct timespec time = ghtimer_time(timer); double dbl = timespec_to_double(time); sprintf(buffer, "%.02lf", dbl); -} \ No newline at end of file +} diff --git a/src/timer.c.h b/src/timer.c.h deleted file mode 100644 index baacc52..0000000 --- a/src/timer.c.h +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include -#include -#include -#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); \ No newline at end of file diff --git a/src/timer.h b/src/timer.h index 0cc17d3..3c973d3 100644 --- a/src/timer.h +++ b/src/timer.h @@ -1,15 +1,21 @@ #ifndef TIMER_H #define TIMER_H -#include -#include -#include -#include #include +#include +#include +#include + +#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); -@interface Timer : YSObject { -@public +//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; @@ -17,18 +23,16 @@ struct timespec diff; int running; int paused; -} - --(void) start; --(void) pause; --(void) resume; --(void) stop; --(void) tick; +} GHTimer; -//returns current minus start; --(struct timespec) time; --(char *) timeString; +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); -@end +struct timespec ghtimer_time(GHTimer *timer); +void ghtimer_timestring(GHTimer *timer, char *buffer); #endif diff --git a/src/timer.m b/src/timer.m deleted file mode 100644 index 0fd2b6d..0000000 --- a/src/timer.m +++ /dev/null @@ -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 diff --git a/src/timeutils.c b/src/timeutils.c deleted file mode 100644 index 1a83da3..0000000 --- a/src/timeutils.c +++ /dev/null @@ -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)); -} \ No newline at end of file diff --git a/src/timeutils.h b/src/timeutils.h deleted file mode 100644 index ab321b8..0000000 --- a/src/timeutils.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef TIMEUTILS_H -#define TIMEUTILS_H - -#include - -#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 \ No newline at end of file diff --git a/timer.h b/timer.h deleted file mode 100644 index 5e523d2..0000000 --- a/timer.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef TIMER_H -#define TIMER_H - -#include -#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.m b/timer.m deleted file mode 100644 index ddb4237..0000000 --- a/timer.m +++ /dev/null @@ -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