diff --git a/src/data.h b/src/data.h index 5ea3c35..d04879c 100644 --- a/src/data.h +++ b/src/data.h @@ -1,7 +1,11 @@ <<<<<<< HEAD +<<<<<<< HEAD #pragma once ======= >>>>>>> 8e3d2ca (add game structure) +======= +#pragma once +>>>>>>> 08f4a9f (draw function added) #include #define MAX_UNITS 100 @@ -76,6 +80,7 @@ typedef struct Map { int mapWidth; int mapHeight; <<<<<<< HEAD +<<<<<<< HEAD <<<<<<< HEAD int tileData[MAP_SIZE][MAP_SIZE]; // 2D array for map tile indices ======= @@ -84,6 +89,9 @@ typedef struct Map { ======= int tileData[MAP_SIZE][MAP_SIZE]; // Assuming 2D array for resource coordinates >>>>>>> 0125c62 (update gamestructure) +======= + int tileData[MAP_SIZE][MAP_SIZE]; // 2D array for map tile indices +>>>>>>> 08f4a9f (draw function added) int playerStartingPositions[MAX_PLAYERS][2]; // [x, y] coordinates for each player int resourceLocations[MAX_RESOURCES][2]; // [x, y] coordinates for each resource } Map; diff --git a/src/main.c b/src/main.c index e83cb00..05dbc06 100644 --- a/src/main.c +++ b/src/main.c @@ -1,55 +1,97 @@ #include -#include +#include +#include "data.h" const int SCREEN_WIDTH = 800; const int SCREEN_HEIGHT = 600; -int main(int argc, char* argv[]) { - // Initialize SDL - SDL_Init(SDL_INIT_VIDEO); - - // Create a window - SDL_Window* window = SDL_CreateWindow("RTS Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, - SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); - - // Create a renderer - SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); - - // Main loop flag -bool quit = false; -SDL_Event event; - -while (!quit) { - // Event handling - while (SDL_PollEvent(&event) != 0) { - if (event.type == SDL_QUIT) { - quit = true; - } - // Handle other events such as key presses, mouse input, etc. - } - - // Update game state - updateGameState(); // You need to define this function to update the game state - +// Function to draw the game data onto the screen +void drawGameData(SDL_Renderer *renderer, const GameData *gameData, SDL_Texture *tileTexture, SDL_Texture *unitTexture, SDL_Texture *buildingTexture) { // Clear the screen SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_RenderClear(renderer); - // Render game objects - renderGame(renderer); // You need to define this function to render the game + // Draw the map tiles + for (int i = 0; i < gameData->currentMap.mapWidth; i++) { + for (int j = 0; j < gameData->currentMap.mapHeight; j++) { + int tileIndex = gameData->currentMap.tileData[i][j]; - // Update the screen + // Calculate destination rectangle for the tile + SDL_Rect destRect = {i * TILE_SIZE, j * TILE_SIZE, TILE_SIZE, TILE_SIZE}; + + // Render the tile texture + SDL_RenderCopy(renderer, tileTexture, NULL, &destRect); + } + } + + // Draw units + for (int i = 0; i < MAX_UNITS; i++) { + // Check if the unit exists (you may have a different condition) + if (gameData->units[i].unitID != -1) { + // Calculate destination rectangle for the unit + SDL_Rect destRect = {gameData->units[i].x, gameData->units[i].y, TILE_SIZE, TILE_SIZE}; + + // Render the unit texture + SDL_RenderCopy(renderer, unitTexture, NULL, &destRect); + } + } + + // Draw buildings + for (int i = 0; i < MAX_BUILDINGS; i++) { + // Check if the building exists (you may have a different condition) + if (gameData->buildings[i].buildingID != -1) { + // Calculate destination rectangle for the building + SDL_Rect destRect = {gameData->buildings[i].x, gameData->buildings[i].y, TILE_SIZE, TILE_SIZE}; + + // Render the building texture + SDL_RenderCopy(renderer, buildingTexture, NULL, &destRect); + } + } + + // Present the rendered frame SDL_RenderPresent(renderer); - - // Cap the frame rate - SDL_Delay(16); // Cap to approximately 60 frames per second } -// Clean up and exit -SDL_DestroyRenderer(renderer); -SDL_DestroyWindow(window); -SDL_Quit(); +int main(int argc, char* argv[]) { + SDL_Init(SDL_INIT_VIDEO); + SDL_Window* window = SDL_CreateWindow("RTS Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, + SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); + SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + // Main loop flag + bool quit = false; + SDL_Event event; + + while (!quit) { + // Event handling + while (SDL_PollEvent(&event) != 0) { + if (event.type == SDL_QUIT) { + quit = true; + } + // Handle other events such as key presses, mouse input, etc. + } + + // Update game state + updateGameState(); // You need to define this function to update the game state + + // Clear the screen + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_RenderClear(renderer); + + // Render game objects + renderGame(renderer); // You need to define this function to render the game + + // Update the screen + SDL_RenderPresent(renderer); + + // Cap the frame rate + SDL_Delay(16); // Cap to approximately 60 frames per second + } + + // Clean up and exit + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); // Destroy window and renderer SDL_DestroyRenderer(renderer);