From 4f51654b7239788ea9ac4aecc4dcedfb1c78ced0 Mon Sep 17 00:00:00 2001 From: Julia09055x Date: Tue, 12 Dec 2023 11:56:08 +0100 Subject: [PATCH] =?UTF-8?q?main=20mit=20now-Funktion=20(lt.Flowchart)=20un?= =?UTF-8?q?d=20globale=20Variablen=20aus=20game=20f=C3=BCr=20window=20und?= =?UTF-8?q?=20renderer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.cpp | 34 +++++++++++++++--------- src/render.c | 73 ++++++++++++++++++++++++++-------------------------- 2 files changed, 58 insertions(+), 49 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c4366b3..d03e72f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,5 @@ #include +#include extern "C" { @@ -38,21 +39,30 @@ int main(int argc, char *argv[]) } CreateSDLWindow(); - while(1){ - if(game.gameState != EXIT) { - if (game.gameState == RUNNING) { - processInputEvent(); - DELAY(config.cycleDurationMs); - processInputEvent(); - runGameCycle(); - } - } else { - DestroySDLWindow(); - SDL_Quit(); - return 0;} + + + + time_t now; + now = clock(); // Timer startet + game.timestampLastCycle = now; + + while(game.gameState != EXIT) { + if (game.gameState == RUNNING) { + now = clock(); // Timer startet + + if (now - game.timestampLastCycle > config.cycleDurationMs){ + game.timestampLastCycle = now; + runGameCycle(); + } else{ + DELAY(5); //verhindert maximale Durchlaufgeschwindigkeit der Schleife + processInputEvent(); + } + } } + DestroySDLWindow(); + SDL_Quit(); return 0; } diff --git a/src/render.c b/src/render.c index 2ba6e07..57a833d 100644 --- a/src/render.c +++ b/src/render.c @@ -6,109 +6,108 @@ #include "config.h" #include -SDL_Renderer* renderer; -SDL_Window* window; + void renderGame(){ - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); - SDL_RenderClear(renderer); + SDL_SetRenderDrawColor(game.renderer, 0, 0, 0, 255); + SDL_RenderClear(game.renderer); SDL_Rect rect; //Rechteck anlegen rect.w = config.blockSizePx; //Breite festlegen rect.h = config.blockSizePx; //Höhe festlegen //_______Head kreieren__________________________________________________ - SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); //RGB-Farbe Kopf + SDL_SetRenderDrawColor(game.renderer, 0, 255, 0, 255); //RGB-Farbe Kopf rect.x = (game.snake.headX * config.blockSizePx); //Abstand links rect.y = (game.snake.headY * config.blockSizePx); //Abstand rechts - SDL_RenderFillRect(renderer, &rect); //Rechteck rendern + SDL_RenderFillRect(game.renderer, &rect); //Rechteck rendern //______Tail kreieren_________________________________________________________________ for(int i = 1; i