Added game file structure, game loop handler, game tickrate helper.

This commit is contained in:
Hannah 2026-01-31 06:24:59 +01:00
commit aacfe27f72
11 changed files with 129 additions and 1 deletions

8
.idea/misc.xml generated
View file

@ -4,4 +4,12 @@
<option name="pythonIntegrationState" value="YES" />
</component>
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
<component name="CidrRootsConfiguration">
<sourceRoots>
<file path="$PROJECT_DIR$/src" />
</sourceRoots>
<libraryRoots>
<file path="$PROJECT_DIR$/libs" />
</libraryRoots>
</component>
</project>

View file

@ -1,2 +1,12 @@
cmake_minimum_required(VERSION 4.2)
cmake_minimum_required(VERSION 4.2.0)
project(lifecrystal C)
add_executable(main src/main.c
src/logic/engineHandler.c
src/logic/engineHandler.h
src/engines/physicsEngine.c
src/engines/graphicsEngine.c
src/helper/tickrateHelper.c
src/logic/gameLoop.c
src/logic/gameLoop.h
src/helper/tickrateHelper.h
)

View file

@ -0,0 +1,3 @@
//
// Created by Hannah on 31/01/2026.
//

View file

@ -0,0 +1,3 @@
//
// Created by Hannah on 31/01/2026.
//

View file

@ -0,0 +1,24 @@
//
// Created by Hannah on 31/01/2026.
//
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
int delayTickrate(int ticksPerSecond, long int period) {
int requiredNanoseconds = 10e9/ticksPerSecond;
struct timeval currentTime;
gettimeofday(&currentTime, NULL);
printf("Nanoseconds: %ld\nCurrent:%ld\n", requiredNanoseconds,currentTime.tv_usec*1000 - period*1000);
if (currentTime.tv_usec*1000 - period*1000 < requiredNanoseconds) {
struct timespec remainingTime;
remainingTime.tv_sec = 0;
remainingTime.tv_nsec = requiredNanoseconds - (currentTime.tv_usec*1000 - period*1000);
printf("Sleeping for %dnano and %dsec with %f effective sec\n", remainingTime.tv_nsec, remainingTime.tv_sec, remainingTime.tv_nsec/10e9);
clock_nanosleep(CLOCK_MONOTONIC, 0, &remainingTime, NULL);
return 0;
}
printf("Can't delay, %d larger than required %d", currentTime.tv_usec*1000 - period*1000, requiredNanoseconds);
return 1;
}

View file

@ -0,0 +1,10 @@
//
// Created by Hannah on 31/01/2026.
//
#ifndef LIFECRYSTAL_TICKRATEHELPER_H
#define LIFECRYSTAL_TICKRATEHELPER_H
int delayTickrate(int tickrate, int period);
#endif //LIFECRYSTAL_TICKRATEHELPER_H

11
src/logic/engineHandler.c Normal file
View file

@ -0,0 +1,11 @@
//
// Created by Hannah on 31/01/2026.
//
#include "engineHandler.h"
#include <stdio.h>
int engineHandlerCycle() {
printf("This is an engine handler cycle!\n");
}

10
src/logic/engineHandler.h Normal file
View file

@ -0,0 +1,10 @@
//
// Created by Hannah on 31/01/2026.
//
#ifndef LIFECRYSTAL_ENGINEHANDLER_H
#define LIFECRYSTAL_ENGINEHANDLER_H
int engineHandlerCycle();
#endif //LIFECRYSTAL_ENGINEHANDLER_H

31
src/logic/gameLoop.c Normal file
View file

@ -0,0 +1,31 @@
//
// Created by Hannah on 31/01/2026.
//
#include "gameLoop.h"
#include <stdio.h>
#include <sys/time.h>
#include "../helper/tickrateHelper.h"
struct timeval rateRegulator(struct timeval prevCycleTime, int ticksPerSecond) {
unsigned short int isBehindSchedule = delayTickrate(ticksPerSecond, prevCycleTime.tv_usec);
if (isBehindSchedule) {
printf("\nRunning behind schedule!\n");
}
struct timeval nextCycleTime;
gettimeofday(&nextCycleTime, NULL);
unsigned int deltaTime = nextCycleTime.tv_usec - prevCycleTime.tv_usec;
printf("Cycle took %u micros, %d millis, landing on %d tps\n", deltaTime, deltaTime/1000, (int) 10e6/deltaTime);
return nextCycleTime;
}
int mainGameLoop(int ticksPerSecond, int framesPerSecond) {
printf("Starting mainGameLoop at %dTPS,%dFPS", ticksPerSecond, framesPerSecond);
struct timeval prevCycleTime;
gettimeofday(&prevCycleTime, NULL);
while (true) {
prevCycleTime = rateRegulator(prevCycleTime, ticksPerSecond);
}
}

10
src/logic/gameLoop.h Normal file
View file

@ -0,0 +1,10 @@
//
// Created by Hannah on 31/01/2026.
//
#ifndef LIFECRYSTAL_GAMELOOP_H
#define LIFECRYSTAL_GAMELOOP_H
int mainGameLoop(int ticksPerSecond, int framesPerSecond);
#endif //LIFECRYSTAL_GAMELOOP_H

View file

@ -1 +1,9 @@
#include <stdio.h>
#include "logic/engineHandler.h"
#include "logic/gameLoop.h"
int main() {
printf("Hello World!\n");
engineHandlerCycle();
mainGameLoop(120,120);
}