/* * Voxel-a-tord * main.c: * Copyright (c) 2022 986-Studio. All rights reserved. * * Created by Manoƫl Trapier on 28/09/2022. */ #include #include #include #include #include #include #include #include bool isRunning = false; int previousFrameTime = 0; double deltaTime = 0; static double updateTime, renderTime, waitTime; static int windowHeight; static int windowWidth; uint32_t getMicroSecTime() { struct timeval curTime; gettimeofday(&curTime, NULL); return ((curTime.tv_sec) * 1000 * 1000) + (curTime.tv_usec); } void processInput() { SDL_Event event; while(SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: isRunning = false; break; case SDL_KEYDOWN: switch (event.key.keysym.sym) { case SDLK_ESCAPE: isRunning = false; break; case SDLK_LEFT: break; case SDLK_RIGHT: break; case SDLK_UP: break; case SDLK_DOWN: break; case SDLK_PAGEUP: break; case SDLK_PAGEDOWN: break; } break; default: break; } } } void setup() { isRunning = true; } void update() { int i; uint32_t timeToWait = FRAME_TARGET_TIME - (SDL_GetTicks() - previousFrameTime); uint32_t ticks = getMicroSecTime(); if (timeToWait <= FRAME_TARGET_TIME) { SDL_Delay(timeToWait); } deltaTime = (SDL_GetTicks() - previousFrameTime) / 1000.0; previousFrameTime = SDL_GetTicks(); waitTime = (waitTime + (getMicroSecTime() - ticks) / 1000.) / 2.0; ticks = getMicroSecTime(); /* [...] */ updateTime = (updateTime + (getMicroSecTime() - ticks) / 1000.) / 2.0; } void render() { uint32_t ticks = getMicroSecTime(); renderFrameBuffer(); renderTime = (renderTime + (getMicroSecTime() - ticks) / 1000.) / 2.0; drawText(5, 5, 0x11FF22, "Wait time: %02.2f ms", waitTime); drawText(5, 17, 0x11FF22, "Update time: %02.2f ms", updateTime); drawText(5, 29, 0x11FF22, "Render time: %02.2f ms", renderTime); drawText(5, 41, 0x11FF22, "FPS: %.1f | dT: %.3f", 1000 / (waitTime + updateTime + renderTime), deltaTime); displayWindowRender(); } int main(int argc, char *argv[]) { int param_i, i; bool fullScreen = false; MAX_DEBUG_LEVEL = TLOG_DEBUG; Log(TLOG_ALWAYS, NULL, "Starting Voxel Engine (version %s)!", VERSION); windowWidth = 320; windowHeight = 240; for (param_i = 1 ; (param_i < argc) && (argv[param_i][0] == '-') ; param_i++) { switch (argv[param_i][1]) { default: exit(-1); /* Option not recognized */ case 'f': fullScreen = true; break; case 'w': windowWidth = atoi(argv[++param_i]); break; case 'h': windowHeight = atoi(argv[++param_i]); break; #ifdef DYNA_LOG_LEVEL case 'l': MAX_DEBUG_LEVEL = atoi(argv[++param_i]); break; #endif case '-': goto no_more_params; /* Could use break, but this is more clear */ } } no_more_params: isRunning = initialiseWindow(windowWidth, windowHeight, fullScreen); setup(); while (isRunning) { processInput(); update(); render(); } destroyWindow(); return 0; }