From e66567d2325c389f199e8cc3977838ca55934842 Mon Sep 17 00:00:00 2001 From: Julia09055x Date: Sun, 10 Dec 2023 16:52:20 +0100 Subject: [PATCH] Snake bewegt sich, Fenster wird mit Portal und Food erstellt --- include/render.h | 4 +- src/game.c | 5 +- src/main.cpp | 26 ++++++++++- src/render.c | 116 ++++++++++++++++++----------------------------- 4 files changed, 76 insertions(+), 75 deletions(-) diff --git a/include/render.h b/include/render.h index 90cd35e..0c949a7 100644 --- a/include/render.h +++ b/include/render.h @@ -7,4 +7,6 @@ void renderGame(); //erstellt aus Spielfeldstruktur die graphische Anzeige mit SDL-Framework -int CreateSDLWindow(); \ No newline at end of file +int CreateSDLWindow(); + +void DestroyWindwo(); \ No newline at end of file diff --git a/src/game.c b/src/game.c index c72ddaf..1e4b1ab 100644 --- a/src/game.c +++ b/src/game.c @@ -9,13 +9,13 @@ // global struct for storing all game data // default values where needed: gameData_t game = { - .snake.length = 0, + .snake.length = 2, .foodX = 0, .foodY = 0, .mapIsLoaded = false, .lifesRemaining = 1, .timestampLastCycle = 0, - .gameState = MENU + .gameState = RUNNING, }; @@ -108,6 +108,7 @@ void runGameCycle() } //--- update frame --- + renderGame(); //printMap(game.map); (render game to console) return; diff --git a/src/main.cpp b/src/main.cpp index 4745be4..a3000e1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,4 @@ - +#include extern "C" { #include "food.h" @@ -8,6 +8,11 @@ extern "C"{ #include "render.h" } +extern "C"{ + #include "game.h" + #include "common.h" +} + //initialize SDL window //ruft showStartScreen //initialize game @@ -17,6 +22,25 @@ extern "C"{ int main(int argc, char *argv[]) { + gameInit(); + + // Initialisiere SDL + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + printf("SDL konnte nicht initialisiert werden! SDL_Error: %s\n", SDL_GetError()); + return 1; + } CreateSDLWindow(); + + while(1){ + if(game.gameState != EXIT) { + if (game.gameState == RUNNING) { + DELAY(config.cycleDurationMs); + runGameCycle(); + } + + } else {SDL_Quit(); return 0;} + } + + return 0; } diff --git a/src/render.c b/src/render.c index 05de4b9..d6210cb 100644 --- a/src/render.c +++ b/src/render.c @@ -3,116 +3,90 @@ #include "game.h" #include "snake.h" #include "food.h" +#include "config.h" #include -void renderGame(SDL_Renderer* renderer){ - - //Snake-Baustein kreieren_________________________________________________________________ +SDL_Renderer* renderer; +SDL_Window* window; + +void renderGame(){ + + SDL_RenderClear(renderer); + //Snake-Baustein kreieren_________________________________________________________________ + for(int i = game.snake.length; i>0; i--){ SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); //RGB-Farbe Schlange SDL_Rect rect; - rect.x = 500/*game.snake.headX * config_t.blockSizePx*/; //Abstand links - rect.y = 300/*game.snake.headY * config_t.blockSizePx*/; //Abstand rechts - rect.w = 20/*config_t.blockSizePx*/; - rect.h = 20/*config_t.blockSizePx*/; + rect.x = (game.snake.headX * config.blockSizePx) + 350; //Abstand links + rect.y = (game.snake.headY * config.blockSizePx) + 50; //Abstand rechts + rect.w = config.blockSizePx; + rect.h = config.blockSizePx; SDL_RenderFillRect(renderer, &rect); //Rechteck rendern + } //Portal kreieren________________________________________________________________________ SDL_SetRenderDrawColor(renderer, 0, 100, 255, 255); //RGB-Farbe Portal + SDL_Rect rect; rect.x = 500; //Abstand links - rect.y = 200; //Abstand rechts + rect.y = 500; //Abstand rechts + rect.w = config.blockSizePx; + rect.h = config.blockSizePx; SDL_RenderDrawRect(renderer, &rect); //Rechteck rendern //Food kreieren_________________________________________________________________________ SDL_SetRenderDrawColor(renderer, 255, 0, 255, 255); //RGB-Farbe Food - rect.x = 300/*foodX*/; //Abstand links - rect.y = 600/*foodY*/; //Abstand rechts + rect.x = (game.foodX*config.blockSizePx) + 350; //Abstand links + rect.y = (game.foodY* config.blockSizePx) + 50; //Abstand rechts + rect.w = config.blockSizePx; + rect.h = config.blockSizePx; SDL_RenderFillRect(renderer, &rect); //Rechteck rendern + SDL_RenderDrawRect(renderer, &rect); //Rechteck rendern + + //Wand kreieren_________________________________________________________________________ + for(int i = 0; i < game.map.collisionCount; i++){ + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); //RGB-Farbe Wand + + rect.x = (game.map.collisions[i].posX*config.blockSizePx) + 350; //Abstand links + rect.y = (game.map.collisions[i].posX* config.blockSizePx) + 50; //Abstand rechts + rect.w = config.blockSizePx; + rect.h = config.blockSizePx; + + SDL_RenderFillRect(renderer, &rect); //Rechteck rendern + } + SDL_RenderPresent(renderer); //Fenster aktualisieren + } int CreateSDLWindow(){ - // Initialisiere SDL - if (SDL_Init(SDL_INIT_VIDEO) < 0) { - printf("SDL konnte nicht initialisiert werden! SDL_Error: %s\n", SDL_GetError()); - return 1; - } + // Erstelle ein SDL-Fenster - SDL_Window* window = SDL_CreateWindow("Snake", 350, 50, 800, 800, SDL_WINDOW_OPENGL); + SDL_Window* window = SDL_CreateWindow("Snake", 350, 50, config.windowSize, config.windowSize, SDL_WINDOW_OPENGL); if (window == NULL) { printf("Fenster konnte nicht erstellt werden! SDL_Error: %s\n", SDL_GetError()); return 1; } - SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); if (renderer == NULL) { printf("Renderer konnte nicht erstellt werden! SDL_Error: %s\n", SDL_GetError()); return 1; } - renderGame(renderer); - - // Warte - SDL_Delay(5000); - - // Zerstöre das Fenster und beende SDL - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(window); - SDL_Quit(); - return 0; } - - /*SDL_Event event; - SDL_Window* window = NULL; - SDL_Surface* surface = NULL; - int done = 0; - atexit(SDL_Quit); - - if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { - printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() ); - return 1; - } - - window = SDL_CreateWindow( - "SDL Tutorial", - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, - 640, 480, - SDL_WINDOW_SHOWN - ); - if (window == NULL){ - printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); - return 1; - } - - surface = SDL_GetWindowSurface(window); - if (surface == NULL){ - printf( "Surface could not be created! SDL_Error: %s\n", SDL_GetError() ); - return 1; - } - - while (done == 0) - { - while (SDL_PollEvent(&event)) - { - switch (event.type) - { - case SDL_QUIT: - done = 1; - break; - } - } - } - - SDL_DestroyWindow(window);*/ \ No newline at end of file +void DestroyWindow(){ + // Zerstöre das Fenster und beende SDL + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); +} \ No newline at end of file