Update module structure: Add map.c
=> global config and game struct changed => some functions were dropped/moved - Outsourced data types and functions to map.c / map.h - Update function and module diagram - Adjust custom data types in source code
This commit is contained in:
		
							parent
							
								
									e9df108850
								
							
						
					
					
						commit
						3e40be47cf
					
				@ -36,6 +36,7 @@ set(SOURCES
 | 
				
			|||||||
    src/menu.c
 | 
					    src/menu.c
 | 
				
			||||||
    src/render.c
 | 
					    src/render.c
 | 
				
			||||||
    src/snake.c
 | 
					    src/snake.c
 | 
				
			||||||
 | 
					    src/map.c
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
										
											Binary file not shown.
										
									
								
							@ -2,14 +2,12 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// global configuration macros
 | 
					// global configuration macros
 | 
				
			||||||
#define MAX_MAP_SIZE 10
 | 
					#define MAX_MAP_SIZE 10
 | 
				
			||||||
#define MAX_PORTALS 5              // Maximalanzahl an Portale
 | 
					#define MAX_MAP_FIELDS (MAX_MAP_SIZE*MAX_MAP_SIZE)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// struct for storing game configuration
 | 
					// struct for storing game configuration
 | 
				
			||||||
typedef struct config_t
 | 
					typedef struct config_t
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  const int windowSize;            // feste Größe des Spielfensters
 | 
					  const int windowSize;            // feste Größe des Spielfensters
 | 
				
			||||||
  int mapWidth;                    // =10     //Kartenbreite
 | 
					 | 
				
			||||||
  int mapHeight;                   // =10    //Kartenhöhe
 | 
					 | 
				
			||||||
  //Breite und Höhe sind gleich -> Spielfeld ist quadratisch
 | 
					  //Breite und Höhe sind gleich -> Spielfeld ist quadratisch
 | 
				
			||||||
  int blockSizePx;                 // Pixelgröße von einem Block
 | 
					  int blockSizePx;                 // Pixelgröße von einem Block
 | 
				
			||||||
  //wird über windowSize/mapHeight berechnet
 | 
					  //wird über windowSize/mapHeight berechnet
 | 
				
			||||||
 | 
				
			|||||||
@ -2,34 +2,28 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#include "snake.h"
 | 
					#include "snake.h"
 | 
				
			||||||
#include "config.h"
 | 
					#include "config.h"
 | 
				
			||||||
 | 
					#include "map.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Enum that defines the current game state
 | 
				
			||||||
typedef enum gameState_t
 | 
					typedef enum gameState_t
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    PAUSED = 0,
 | 
					  EXIT = 0,
 | 
				
			||||||
    MENU,
 | 
					  RUNNING,
 | 
				
			||||||
    RUNNING
 | 
					  MENU,
 | 
				
			||||||
 | 
					  PAUSED
 | 
				
			||||||
} gameState_t;
 | 
					} gameState_t;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef struct portal_t
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
  int posX, posY;
 | 
					 | 
				
			||||||
  int targetX, targetY;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  char * color;
 | 
					 | 
				
			||||||
} portal_t;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Struct that stores all data of the running game (all game-related functions access it globally)
 | 
				
			||||||
typedef struct gameData_t
 | 
					typedef struct gameData_t
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    snake_t snake;
 | 
					  snake_t snake;
 | 
				
			||||||
    int mapCollisions[MAX_MAP_SIZE][MAX_MAP_SIZE]; // Position der Wände
 | 
					  map_t map;          // definition der geladenen karte
 | 
				
			||||||
    portal_t mapPortals[MAX_PORTALS];       
 | 
					  bool mapIsLoaded; // true when config.map is valid
 | 
				
			||||||
    int mapPortalCount;   // Anzahl der Portale
 | 
					  int foodX, foodY;   // Positon des Futters (es gibt immer nur 1 Futter)
 | 
				
			||||||
 | 
					  int lifesRemaining; // implementieren wir nicht!!
 | 
				
			||||||
    int foodX, foodY;   // Positon des Futters (es gibt immer nur 1 Futter)
 | 
					  int timestampLastCycle;
 | 
				
			||||||
    int lifesRemaining; // implementieren wir nicht!!
 | 
					  gameState_t gameState;
 | 
				
			||||||
    int timestampLastRun;
 | 
					 | 
				
			||||||
    bool isPaused;
 | 
					 | 
				
			||||||
    gameState_t gameState;
 | 
					 | 
				
			||||||
} gameData_t;
 | 
					} gameData_t;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// global struct for storing all game data (defined in game.c)
 | 
					// global struct for storing all game data (defined in game.c)
 | 
				
			||||||
@ -41,21 +35,14 @@ void gameInit();
 | 
				
			|||||||
// ruft placeFood auf
 | 
					// ruft placeFood auf
 | 
				
			||||||
// platziert Wände
 | 
					// platziert Wände
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool checkIsCollided();
 | 
					void handlePortals(); //(local)
 | 
				
			||||||
// Überprüft, ob Snake mit Gegenstand/Wand kollidiert ist
 | 
					 | 
				
			||||||
// if true -> gibt 1 an runGameCycle zurück
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void handlePortals(); // optional
 | 
					 | 
				
			||||||
// Prüft, ob Snake sich auf einem Portal befindet
 | 
					// Prüft, ob Snake sich auf einem Portal befindet
 | 
				
			||||||
//if true: snakeSetHeadPos auf
 | 
					//if true: snakeSetHeadPos auf
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void runGameCycle();
 | 
					void runGameCycle();
 | 
				
			||||||
// macht immer: handleKeyboardEvents();
 | 
					// checkCollision() auf
 | 
				
			||||||
// optional: ruft checkCollision() auf
 | 
					 | 
				
			||||||
// ruft placeFood() auf
 | 
					// ruft placeFood() auf
 | 
				
			||||||
// ruft checkEaten() auf
 | 
					// ruft checkEaten() auf
 | 
				
			||||||
// if checkEaten then snakeGrow()
 | 
					// if checkEaten then snakeGrow()
 | 
				
			||||||
// if TickDue: Snakemove(), TickTimerReset
 | 
					// Snakemove(), TickTimerReset
 | 
				
			||||||
// ruft snakeMove() auf
 | 
					 | 
				
			||||||
//ruft am Ende vom gameCycle renderGame() auf
 | 
					//ruft am Ende vom gameCycle renderGame() auf
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										42
									
								
								include/map.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								include/map.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,42 @@
 | 
				
			|||||||
 | 
					#pragma once
 | 
				
			||||||
 | 
					#include <stdbool.h>
 | 
				
			||||||
 | 
					#include "config.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Struct that stores all information needed for one Portal on the map
 | 
				
			||||||
 | 
					typedef struct portal_t
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  int posX, posY;
 | 
				
			||||||
 | 
					  int targetX, targetY;
 | 
				
			||||||
 | 
					  char *color;
 | 
				
			||||||
 | 
					} portal_t;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Struct that stores all information needed for one Collision box on the map
 | 
				
			||||||
 | 
					typedef struct collisionBox_t
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  int posX, posY;
 | 
				
			||||||
 | 
					} collisionBox_t;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Struct that describes an entire map
 | 
				
			||||||
 | 
					typedef struct map_t {
 | 
				
			||||||
 | 
					    int width;
 | 
				
			||||||
 | 
					    int height;
 | 
				
			||||||
 | 
					    const char *name[128];
 | 
				
			||||||
 | 
					    collisionBox_t collisions[MAX_MAP_FIELDS];
 | 
				
			||||||
 | 
					    int collisionCount;
 | 
				
			||||||
 | 
					    portal_t portals[MAX_MAP_FIELDS];
 | 
				
			||||||
 | 
					    int portalCount;
 | 
				
			||||||
 | 
					} map_t;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//return true when provided coordinate matches a collision box
 | 
				
			||||||
 | 
					bool checkCollides(int x, int y);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//generate random map based on difficulty level
 | 
				
			||||||
 | 
					map_t generateMap(int difficulty);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//search and load map by name (if not found loads default map)
 | 
				
			||||||
 | 
					void loadMapByName(char *name);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//load map by passed definition
 | 
				
			||||||
 | 
					void loadMap(map_t map);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										47
									
								
								src/game.c
									
									
									
									
									
								
							
							
						
						
									
										47
									
								
								src/game.c
									
									
									
									
									
								
							@ -1,4 +1,49 @@
 | 
				
			|||||||
#include "game.h"
 | 
					#include "game.h"
 | 
				
			||||||
 | 
					#include "map.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// global struct for storing all game data
 | 
					// global struct for storing all game data
 | 
				
			||||||
gameData_t game;
 | 
					gameData_t game;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//========================
 | 
				
			||||||
 | 
					//======= gameInit =======
 | 
				
			||||||
 | 
					//========================
 | 
				
			||||||
 | 
					void gameInit()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    //----- snake -----
 | 
				
			||||||
 | 
					    // defines initial values of game.snake
 | 
				
			||||||
 | 
					    // snakeInit(); FIXME: uncomment when implemented
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    //----- load map -----
 | 
				
			||||||
 | 
					    //load default map if no map loaded yet
 | 
				
			||||||
 | 
					    if (!game.mapIsLoaded){
 | 
				
			||||||
 | 
					        char * defaultName = "default";
 | 
				
			||||||
 | 
					        loadMapByName("default");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    // place initial food
 | 
				
			||||||
 | 
					    //placeFood(); FIXME uncomment when implemented
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    //----- initialize variables -----
 | 
				
			||||||
 | 
					    game.lifesRemaining = 1;
 | 
				
			||||||
 | 
					    // game.lifesRemaining = config.maxLifes; TODO: add maxLifes to config
 | 
				
			||||||
 | 
					    // game.gameState = RUNNING; ??
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    game.timestampLastCycle = -config.cycleDurationMs; // next cycle starts immediately
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//=========================
 | 
				
			||||||
 | 
					//===== handlePortals =====
 | 
				
			||||||
 | 
					//=========================
 | 
				
			||||||
 | 
					void handlePortals()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//==========================
 | 
				
			||||||
 | 
					//====== runGameCycle ======
 | 
				
			||||||
 | 
					//==========================
 | 
				
			||||||
 | 
					void runGameCycle()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    if (checkCollides(game.snake.headX, game.snake.headY))
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										41
									
								
								src/map.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								src/map.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,41 @@
 | 
				
			|||||||
 | 
					#include "map.h"
 | 
				
			||||||
 | 
					#include "game.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// generate random map based on difficulty level
 | 
				
			||||||
 | 
					map_t generateMap(int difficulty)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    map_t newMap;
 | 
				
			||||||
 | 
					    return newMap;
 | 
				
			||||||
 | 
					    // TODO add map generator
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// search and load map by name (if not found loads default map)
 | 
				
			||||||
 | 
					void loadMapByName(char *name)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					    // TODO add map presets
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// load map by passed definition
 | 
				
			||||||
 | 
					void loadMap(map_t map)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    game.map = map;
 | 
				
			||||||
 | 
					    game.mapIsLoaded = true;
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// check if there is collision at certain coordinate
 | 
				
			||||||
 | 
					bool checkCollides(int x, int y)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    // loop through all collision boxes on the map
 | 
				
			||||||
 | 
					    for (int i = 0; i < game.map.collisionCount; i++)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        // return true if match found
 | 
				
			||||||
 | 
					        if (game.map.collisions[i].posX == x && game.map.collisions[i].posY == y)
 | 
				
			||||||
 | 
					            return true;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return false;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//TODO add map presets here:
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user