From ed335c9e0108cd808cad68deef24a00c0b454c89 Mon Sep 17 00:00:00 2001 From: Rysertio Date: Thu, 15 Feb 2024 19:53:51 +0600 Subject: [PATCH 1/9] init --- makefile | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 makefile create mode 100644 src/main.c diff --git a/makefile b/makefile new file mode 100644 index 0000000..2041be1 --- /dev/null +++ b/makefile @@ -0,0 +1,49 @@ +TARGET_EXEC := rts_game + +BUILD_DIR := ./build +SRC_DIRS := ./src + +# Find all the C and C++ files we want to compile +# Note the single quotes around the * expressions. The shell will incorrectly expand these otherwise, but we want to send the * directly to the find command. +SRCS := $(shell find $(SRC_DIRS) -name '*.cpp' -or -name '*.c' -or -name '*.s') + +# Prepends BUILD_DIR and appends .o to every src file +# As an example, ./your_dir/hello.cpp turns into ./build/./your_dir/hello.cpp.o +OBJS := $(SRCS:%=$(BUILD_DIR)/%.o) + +# String substitution (suffix version without %). +# As an example, ./build/hello.cpp.o turns into ./build/hello.cpp.d +DEPS := $(OBJS:.o=.d) + +# Every folder in ./src will need to be passed to GCC so that it can find header files +INC_DIRS := $(shell find $(SRC_DIRS) -type d) +# Add a prefix to INC_DIRS. So moduleA would become -ImoduleA. GCC understands this -I flag +INC_FLAGS := $(addprefix -I,$(INC_DIRS)) + +# The -MMD and -MP flags together generate Makefiles for us! +# These files will have .d instead of .o as the output. +CPPFLAGS := $(INC_FLAGS) -MMD -MP + +# The final build step. +$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS) + $(CXX) $(OBJS) -o $@ $(LDFLAGS) + +# Build step for C source +$(BUILD_DIR)/%.c.o: %.c + mkdir -p $(dir $@) + $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ + +# Build step for C++ source +$(BUILD_DIR)/%.cpp.o: %.cpp + mkdir -p $(dir $@) + $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@ + + +.PHONY: clean +clean: + rm -r $(BUILD_DIR) + +# Include the .d makefiles. The - at the front suppresses the errors of missing +# Makefiles. Initially, all the .d files will be missing, and we don't want those +# errors to show up. +-include $(DEPS) \ No newline at end of file diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..1316ad1 --- /dev/null +++ b/src/main.c @@ -0,0 +1,50 @@ +#include + +// Define constants for screen width and height +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; + + // Event handler + SDL_Event e; + + // Main loop + while (!quit) { + // Handle events on queue + while (SDL_PollEvent(&e) != 0) { + // User requests quit + if (e.type == SDL_QUIT) { + quit = true; + } + } + + // Clear screen + SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); + SDL_RenderClear(renderer); + + // Update screen + SDL_RenderPresent(renderer); + } + + // Destroy window and renderer + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + + // Quit SDL subsystems + SDL_Quit(); + + return 0; +} From 563504af1b33bf1d010fe3d7ad8c9bfcb23c00a0 Mon Sep 17 00:00:00 2001 From: Rysertio Date: Thu, 15 Feb 2024 20:18:37 +0600 Subject: [PATCH 2/9] add makefile --- .gitignore | 57 ++------------------------------- makefile | 47 ++++++--------------------- src/data.h | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/draw.c | 73 ++++++++++++++++++++++++++++++++++++++++++ src/draw.h | 13 ++++++++ src/input.c | 0 src/input.h | 0 src/main.c | 2 +- src/ui.c | 67 ++++++++++++++++++++++++++++++++++++++ src/ui.h | 21 ++++++++++++ 10 files changed, 279 insertions(+), 93 deletions(-) create mode 100644 src/data.h create mode 100644 src/draw.c create mode 100644 src/draw.h create mode 100644 src/input.c create mode 100644 src/input.h create mode 100644 src/ui.c create mode 100644 src/ui.h diff --git a/.gitignore b/.gitignore index cd531cf..802dd78 100644 --- a/.gitignore +++ b/.gitignore @@ -1,54 +1,3 @@ -# ---> C -# Prerequisites -*.d - -# Object files -*.o -*.ko -*.obj -*.elf - -# Linker output -*.ilk -*.map -*.exp - -# Precompiled Headers -*.gch -*.pch - -# Libraries -*.lib -*.a -*.la -*.lo - -# Shared objects (inc. Windows DLLs) -*.dll -*.so -*.so.* -*.dylib - -# Executables -*.exe -*.out -*.app -*.i*86 -*.x86_64 -*.hex - -# Debug files -*.dSYM/ -*.su -*.idb -*.pdb - -# Kernel Module Compile Results -*.mod* -*.cmd -.tmp_versions/ -modules.order -Module.symvers -Mkfile.old -dkms.conf - +build/* +src/*.d +src/*.o \ No newline at end of file diff --git a/makefile b/makefile index 2041be1..73b5a5c 100644 --- a/makefile +++ b/makefile @@ -1,49 +1,20 @@ -TARGET_EXEC := rts_game +TARGET ?= build/rts_game +SRC_DIRS ?= ./src -BUILD_DIR := ./build -SRC_DIRS := ./src - -# Find all the C and C++ files we want to compile -# Note the single quotes around the * expressions. The shell will incorrectly expand these otherwise, but we want to send the * directly to the find command. -SRCS := $(shell find $(SRC_DIRS) -name '*.cpp' -or -name '*.c' -or -name '*.s') - -# Prepends BUILD_DIR and appends .o to every src file -# As an example, ./your_dir/hello.cpp turns into ./build/./your_dir/hello.cpp.o -OBJS := $(SRCS:%=$(BUILD_DIR)/%.o) - -# String substitution (suffix version without %). -# As an example, ./build/hello.cpp.o turns into ./build/hello.cpp.d +SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c -or -name *.s) +OBJS := $(addsuffix .o,$(basename $(SRCS))) DEPS := $(OBJS:.o=.d) -# Every folder in ./src will need to be passed to GCC so that it can find header files INC_DIRS := $(shell find $(SRC_DIRS) -type d) -# Add a prefix to INC_DIRS. So moduleA would become -ImoduleA. GCC understands this -I flag INC_FLAGS := $(addprefix -I,$(INC_DIRS)) -# The -MMD and -MP flags together generate Makefiles for us! -# These files will have .d instead of .o as the output. -CPPFLAGS := $(INC_FLAGS) -MMD -MP - -# The final build step. -$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS) - $(CXX) $(OBJS) -o $@ $(LDFLAGS) - -# Build step for C source -$(BUILD_DIR)/%.c.o: %.c - mkdir -p $(dir $@) - $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ - -# Build step for C++ source -$(BUILD_DIR)/%.cpp.o: %.cpp - mkdir -p $(dir $@) - $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@ +CPPFLAGS ?= $(INC_FLAGS) -MMD -MP +$(TARGET): $(OBJS) + $(CC) $(LDFLAGS) $(OBJS) -o $@ $(LOADLIBES) $(LDLIBS) -lSDL2 .PHONY: clean clean: - rm -r $(BUILD_DIR) + $(RM) $(TARGET) $(OBJS) $(DEPS) -# Include the .d makefiles. The - at the front suppresses the errors of missing -# Makefiles. Initially, all the .d files will be missing, and we don't want those -# errors to show up. --include $(DEPS) \ No newline at end of file +-include $(DEPS) diff --git a/src/data.h b/src/data.h new file mode 100644 index 0000000..1181d6c --- /dev/null +++ b/src/data.h @@ -0,0 +1,92 @@ +#pragma once +#include + +#define MAX_UNITS 100 +#define MAX_BUILDINGS 50 +#define MAX_PLAYERS 2 +#define MAX_MISSIONS 10 +#define MAX_RESOURCES 50 +#define MAP_SIZE 100 + +typedef struct Player { + int playerID; + char playerName[50]; + int gold; + int xp; +} Player; + +typedef struct Unit { + int unitID; + int playerID; + int x, y; // Position + int health; +} Unit; + +typedef struct Building { + int buildingID; + int playerID; + int x, y; // Position + bool isConstructed; + int health; +} Building; + +typedef struct Map { + int mapID; + char mapName[50]; + int mapWidth; + int mapHeight; + int tileData[MAP_SIZE][MAP_SIZE]; // 2D array for map tile indices + int playerStartingPositions[MAX_PLAYERS][2]; // [x, y] coordinates for each player + int resourceLocations[MAX_RESOURCES][2]; // [x, y] coordinates for each resource +} Map; + +typedef struct Mission { + int missionID; + char missionName[50]; + char missionDescription[100]; + bool isCompleted; + + char objectives[5][100]; // Array of objectives with a maximum of 5 objectives + + int experienceReward; + int goldReward; + + int timeLimit; // Time limit for completing the mission (in seconds) + bool hasSpecialConditions; // Flag indicating whether the mission has special conditions +} Mission; + +typedef struct GameData { + Player players[MAX_PLAYERS]; + Unit units[MAX_UNITS]; + Building buildings[MAX_BUILDINGS]; + Map currentMap; + Mission missions[MAX_MISSIONS]; + int mapx, mapy; +} GameData; + +// Structure representing building data +typedef struct { + int buildingID; + int maxHealth; + char name[50]; + int x, y, w, h; // sprite position + int cost; +} BuildingData; + +// Structure representing unit data +typedef struct { + int unitID; + int x, y, w, h; // sprite position + int maxHealth; + char name[50]; // Name of the unit + int range; // Type of attack (Melee, Ranged, Magic) + int damage; // Damage dealt by the unit + int lineOfSight; // Line of sight (LOS) of the unit + int cost; // Resource cost to create the unit +} UnitData; + +typedef struct { + GameData data; + BuildingData buildings[100]; + UnitData units[100]; +} Game; \ No newline at end of file diff --git a/src/draw.c b/src/draw.c new file mode 100644 index 0000000..a2047d6 --- /dev/null +++ b/src/draw.c @@ -0,0 +1,73 @@ +#include +#include "draw.h" +#include "data.h" +// 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); + + // 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]; + + // 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); +} + +void drawMinimap(SDL_Renderer *renderer, const Map *map, int minimapX, int minimapY, int minimapSize) { + // Calculate the size of each minimap tile + int tileSizeX = minimapSize / map->mapWidth; + int tileSizeY = minimapSize / map->mapHeight; + + // Iterate through each map tile and draw a simplified representation on the minimap + for (int i = 0; i < map->mapWidth; i++) { + for (int j = 0; j < map->mapHeight; j++) { + SDL_Rect minimapTileRect = {minimapX + i * tileSizeX, minimapY + j * tileSizeY, tileSizeX, tileSizeY}; + + // Set color based on the type of tile (for example, green for grass, blue for water, etc.) + SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); // Example: Green color for grass tiles + + // Fill the minimap tile + SDL_RenderFillRect(renderer, &minimapTileRect); + } + } + + // Draw borders for the minimap + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_Rect minimapBorderRect = {minimapX, minimapY, minimapSize, minimapSize}; + SDL_RenderDrawRect(renderer, &minimapBorderRect); +} \ No newline at end of file diff --git a/src/draw.h b/src/draw.h new file mode 100644 index 0000000..a0b367d --- /dev/null +++ b/src/draw.h @@ -0,0 +1,13 @@ +#ifndef DRAW_H +#define DRAW_H + +#include + +// Define the size of your tiles in pixels +#define TILE_SIZE 32 +#include "data.h" + +// Function prototype for drawing game data +void drawGameData(SDL_Renderer *renderer, const GameData *gameData, SDL_Texture *tileTexture, SDL_Texture *unitTexture, SDL_Texture *buildingTexture); + +#endif /* DRAW_H */ diff --git a/src/input.c b/src/input.c new file mode 100644 index 0000000..e69de29 diff --git a/src/input.h b/src/input.h new file mode 100644 index 0000000..e69de29 diff --git a/src/main.c b/src/main.c index 1316ad1..bda4270 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,6 @@ #include +#include -// Define constants for screen width and height const int SCREEN_WIDTH = 800; const int SCREEN_HEIGHT = 600; diff --git a/src/ui.c b/src/ui.c new file mode 100644 index 0000000..252f1b3 --- /dev/null +++ b/src/ui.c @@ -0,0 +1,67 @@ +#include "ui.h" +#include +#include + +bool isPointInsideRect(int x, int y, SDL_Rect rect) { + return (x >= rect.x && x <= rect.x + rect.w && y >= rect.y && y <= rect.y + rect.h); +} + +void handleButtonClick(SDL_Event *event, Button *button) { + int mouseX, mouseY; + SDL_GetMouseState(&mouseX, &mouseY); + + if (event->type == SDL_MOUSEBUTTONDOWN && event->button.button == SDL_BUTTON_LEFT) { + // Check if the mouse click is inside the button + if (isPointInsideRect(mouseX, mouseY, (SDL_Rect){button->x, button->y, button->w, button->h})) { + button->clicked = true; + } + } else if (event->type == SDL_MOUSEBUTTONUP && event->button.button == SDL_BUTTON_LEFT) { + if (isPointInsideRect(mouseX, mouseY, (SDL_Rect){button->x, button->y, button->w, button->h})) { + if (button->clicked && button->onClick != NULL) { + button->onClick(); // Call the associated function + } + } + button->clicked = false; + } +} + +void handleButtonEvents(SDL_Event *event, Button buttons[], int numButtons) { + for (int i = 0; i < numButtons; i++) { + handleButtonClick(event, &buttons[i]); + } +} + +void drawButtons(SDL_Renderer *renderer, Button buttons[], int numButtons, TTF_Font *font) { + SDL_Color textColor = {255, 255, 255}; // White color for text + + // Clear the renderer + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_RenderClear(renderer); + + // Loop through each button and draw it + for (int i = 0; i < numButtons; ++i) { + SDL_Rect rect = {buttons[i].x, buttons[i].y, buttons[i].w, buttons[i].h}; + + // Set button color based on whether it's clicked or not + if (buttons[i].clicked) { + SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); // Green if clicked + } else { + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); // Red if not clicked + } + SDL_RenderFillRect(renderer, &rect); + + // Render button text + SDL_Surface* surfaceMessage = TTF_RenderText_Solid(font, buttons[i].text, textColor); + SDL_Texture* message = SDL_CreateTextureFromSurface(renderer, surfaceMessage); + int text_width, text_height; + SDL_QueryTexture(message, NULL, NULL, &text_width, &text_height); + SDL_Rect textRect = {buttons[i].x + (buttons[i].w - text_width) / 2, buttons[i].y + (buttons[i].h - text_height) / 2, text_width, text_height}; + SDL_RenderCopy(renderer, message, NULL, &textRect); + + SDL_FreeSurface(surfaceMessage); + SDL_DestroyTexture(message); + } + + // Present renderer + SDL_RenderPresent(renderer); +} diff --git a/src/ui.h b/src/ui.h new file mode 100644 index 0000000..0265137 --- /dev/null +++ b/src/ui.h @@ -0,0 +1,21 @@ +#ifndef UI_H +#define UI_H + +#include +#include + +// Structure representing a button +typedef struct { + int x, y, w, h; + bool clicked; + void (*onClick)(); + char text[50]; +} Button; + +// Function prototypes +bool isPointInsideRect(int x, int y, SDL_Rect rect); +void handleButtonClick(SDL_Event *event, Button *button); +void handleButtonEvents(SDL_Event *event, Button buttons[], int numButtons); +void drawButtons(SDL_Renderer *renderer, Button buttons[], int numButtons, TTF_Font *font); + +#endif /* UI_H */ From 73ca24c36b404e73ce325f2b4b02652779800d4c Mon Sep 17 00:00:00 2001 From: Rysertio Date: Thu, 15 Feb 2024 21:28:36 +0600 Subject: [PATCH 3/9] add game structure --- src/data.h | 38 +++++++++++++++++++++++++++++++++++++- src/draw.h | 21 +++++++++++++++++++++ src/main.c | 50 +++++++++++++++++++++++++++++++------------------- 3 files changed, 89 insertions(+), 20 deletions(-) diff --git a/src/data.h b/src/data.h index 1181d6c..88f4aca 100644 --- a/src/data.h +++ b/src/data.h @@ -1,28 +1,44 @@ +<<<<<<< HEAD #pragma once +======= +>>>>>>> 8e3d2ca (add game structure) #include #define MAX_UNITS 100 #define MAX_BUILDINGS 50 #define MAX_PLAYERS 2 +<<<<<<< HEAD #define MAX_MISSIONS 10 #define MAX_RESOURCES 50 #define MAP_SIZE 100 typedef struct Player { +======= + +typedef struct { +>>>>>>> 8e3d2ca (add game structure) int playerID; char playerName[50]; int gold; int xp; } Player; +<<<<<<< HEAD typedef struct Unit { +======= +typedef struct { +>>>>>>> 8e3d2ca (add game structure) int unitID; int playerID; int x, y; // Position int health; } Unit; +<<<<<<< HEAD typedef struct Building { +======= +typedef struct { +>>>>>>> 8e3d2ca (add game structure) int buildingID; int playerID; int x, y; // Position @@ -30,17 +46,29 @@ typedef struct Building { int health; } Building; +<<<<<<< HEAD typedef struct Map { +======= +typedef struct { +>>>>>>> 8e3d2ca (add game structure) int mapID; char mapName[50]; int mapWidth; int mapHeight; +<<<<<<< HEAD int tileData[MAP_SIZE][MAP_SIZE]; // 2D array for map tile indices +======= + int tileData[MAX_MAP_WIDTH][MAX_MAP_HEIGHT]; +>>>>>>> 8e3d2ca (add game structure) int playerStartingPositions[MAX_PLAYERS][2]; // [x, y] coordinates for each player int resourceLocations[MAX_RESOURCES][2]; // [x, y] coordinates for each resource } Map; +<<<<<<< HEAD typedef struct Mission { +======= +typedef struct { +>>>>>>> 8e3d2ca (add game structure) int missionID; char missionName[50]; char missionDescription[100]; @@ -55,12 +83,17 @@ typedef struct Mission { bool hasSpecialConditions; // Flag indicating whether the mission has special conditions } Mission; +<<<<<<< HEAD typedef struct GameData { +======= +typedef struct { +>>>>>>> 8e3d2ca (add game structure) Player players[MAX_PLAYERS]; Unit units[MAX_UNITS]; Building buildings[MAX_BUILDINGS]; Map currentMap; Mission missions[MAX_MISSIONS]; +<<<<<<< HEAD int mapx, mapy; } GameData; @@ -89,4 +122,7 @@ typedef struct { GameData data; BuildingData buildings[100]; UnitData units[100]; -} Game; \ No newline at end of file +} Game; +======= +} GameData; +>>>>>>> 8e3d2ca (add game structure) diff --git a/src/draw.h b/src/draw.h index a0b367d..491e86b 100644 --- a/src/draw.h +++ b/src/draw.h @@ -1,3 +1,4 @@ +<<<<<<< HEAD #ifndef DRAW_H #define DRAW_H @@ -11,3 +12,23 @@ void drawGameData(SDL_Renderer *renderer, const GameData *gameData, SDL_Texture *tileTexture, SDL_Texture *unitTexture, SDL_Texture *buildingTexture); #endif /* DRAW_H */ +======= +#define MAP_SIZE 96 + +#define TILE_HEIGHT 30 +#define TILE_WIDTH 60 + +#define MAP_RENDER_SIZE 24 + +#define MAP_RENDER_OFFSET_X ((SCREEN_WIDTH - (TILE_WIDTH * MAP_RENDER_SIZE)) / 2) +#define MAP_RENDER_OFFSET_Y 425 + +typedef struct { + int x; + int y; + int sx; + int sy; + AtlasImage *texture; +} ISOObject; + +>>>>>>> 8e3d2ca (add game structure) diff --git a/src/main.c b/src/main.c index bda4270..e83cb00 100644 --- a/src/main.c +++ b/src/main.c @@ -16,29 +16,41 @@ int main(int argc, char* argv[]) { SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); // Main loop flag - bool quit = false; +bool quit = false; +SDL_Event event; - // Event handler - SDL_Event e; - - // Main loop - while (!quit) { - // Handle events on queue - while (SDL_PollEvent(&e) != 0) { - // User requests quit - if (e.type == SDL_QUIT) { - quit = true; - } +while (!quit) { + // Event handling + while (SDL_PollEvent(&event) != 0) { + if (event.type == SDL_QUIT) { + quit = true; } - - // Clear screen - SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); - SDL_RenderClear(renderer); - - // Update screen - SDL_RenderPresent(renderer); + // 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); SDL_DestroyWindow(window); From 6b2517604ea89b94a5ed0c129422c5275f02353b Mon Sep 17 00:00:00 2001 From: Rysertio Date: Thu, 15 Feb 2024 21:30:27 +0600 Subject: [PATCH 4/9] update gamestructure --- src/data.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/data.h b/src/data.h index 88f4aca..5ea3c35 100644 --- a/src/data.h +++ b/src/data.h @@ -8,6 +8,7 @@ #define MAX_BUILDINGS 50 #define MAX_PLAYERS 2 <<<<<<< HEAD +<<<<<<< HEAD #define MAX_MISSIONS 10 #define MAX_RESOURCES 50 #define MAP_SIZE 100 @@ -17,28 +18,43 @@ typedef struct Player { typedef struct { >>>>>>> 8e3d2ca (add game structure) +======= +#define MAX_MISSIONS 10 +#define MAX_RESOURCES 50 +#define MAP_SIZE 100 + +typedef struct Player { +>>>>>>> 0125c62 (update gamestructure) int playerID; char playerName[50]; int gold; int xp; } Player; +<<<<<<< HEAD <<<<<<< HEAD typedef struct Unit { ======= typedef struct { >>>>>>> 8e3d2ca (add game structure) +======= +typedef struct Unit { +>>>>>>> 0125c62 (update gamestructure) int unitID; int playerID; int x, y; // Position int health; } Unit; +<<<<<<< HEAD <<<<<<< HEAD typedef struct Building { ======= typedef struct { >>>>>>> 8e3d2ca (add game structure) +======= +typedef struct Building { +>>>>>>> 0125c62 (update gamestructure) int buildingID; int playerID; int x, y; // Position @@ -46,29 +62,41 @@ typedef struct { int health; } Building; +<<<<<<< HEAD <<<<<<< HEAD typedef struct Map { ======= typedef struct { >>>>>>> 8e3d2ca (add game structure) +======= +typedef struct Map { +>>>>>>> 0125c62 (update gamestructure) int mapID; char mapName[50]; int mapWidth; int mapHeight; +<<<<<<< HEAD <<<<<<< HEAD int tileData[MAP_SIZE][MAP_SIZE]; // 2D array for map tile indices ======= int tileData[MAX_MAP_WIDTH][MAX_MAP_HEIGHT]; >>>>>>> 8e3d2ca (add game structure) +======= + int tileData[MAP_SIZE][MAP_SIZE]; // Assuming 2D array for resource coordinates +>>>>>>> 0125c62 (update gamestructure) int playerStartingPositions[MAX_PLAYERS][2]; // [x, y] coordinates for each player int resourceLocations[MAX_RESOURCES][2]; // [x, y] coordinates for each resource } Map; +<<<<<<< HEAD <<<<<<< HEAD typedef struct Mission { ======= typedef struct { >>>>>>> 8e3d2ca (add game structure) +======= +typedef struct Mission { +>>>>>>> 0125c62 (update gamestructure) int missionID; char missionName[50]; char missionDescription[100]; @@ -83,11 +111,15 @@ typedef struct { bool hasSpecialConditions; // Flag indicating whether the mission has special conditions } Mission; +<<<<<<< HEAD <<<<<<< HEAD typedef struct GameData { ======= typedef struct { >>>>>>> 8e3d2ca (add game structure) +======= +typedef struct GameData { +>>>>>>> 0125c62 (update gamestructure) Player players[MAX_PLAYERS]; Unit units[MAX_UNITS]; Building buildings[MAX_BUILDINGS]; From 808fd83e6e860353db0b5f3642019eabc3c68954 Mon Sep 17 00:00:00 2001 From: Rysertio Date: Wed, 21 Feb 2024 09:16:04 +0600 Subject: [PATCH 5/9] draw function added --- src/data.h | 8 ++++ src/main.c | 118 ++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 88 insertions(+), 38 deletions(-) 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); From 17fdac54f480cc4c0cbda27542989cffc20b5704 Mon Sep 17 00:00:00 2001 From: Rysertio Date: Wed, 21 Feb 2024 09:31:04 +0600 Subject: [PATCH 6/9] draw function --- src/draw.c | 3 +++ src/draw.h | 28 +++++++++++++++------------- src/main.c | 46 ---------------------------------------------- 3 files changed, 18 insertions(+), 59 deletions(-) diff --git a/src/draw.c b/src/draw.c index a2047d6..e95a825 100644 --- a/src/draw.c +++ b/src/draw.c @@ -46,6 +46,7 @@ void drawGameData(SDL_Renderer *renderer, const GameData *gameData, SDL_Texture // Present the rendered frame SDL_RenderPresent(renderer); +<<<<<<< HEAD } void drawMinimap(SDL_Renderer *renderer, const Map *map, int minimapX, int minimapY, int minimapSize) { @@ -70,4 +71,6 @@ void drawMinimap(SDL_Renderer *renderer, const Map *map, int minimapX, int minim SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); SDL_Rect minimapBorderRect = {minimapX, minimapY, minimapSize, minimapSize}; SDL_RenderDrawRect(renderer, &minimapBorderRect); +======= +>>>>>>> 6abc1cd (draw function) } \ No newline at end of file diff --git a/src/draw.h b/src/draw.h index 491e86b..9fb826f 100644 --- a/src/draw.h +++ b/src/draw.h @@ -1,4 +1,5 @@ <<<<<<< HEAD +<<<<<<< HEAD #ifndef DRAW_H #define DRAW_H @@ -14,21 +15,22 @@ void drawGameData(SDL_Renderer *renderer, const GameData *gameData, SDL_Texture #endif /* DRAW_H */ ======= #define MAP_SIZE 96 +======= +#ifndef DRAW_H +#define DRAW_H +>>>>>>> 6abc1cd (draw function) -#define TILE_HEIGHT 30 -#define TILE_WIDTH 60 +#include -#define MAP_RENDER_SIZE 24 +// Define the size of your tiles in pixels +#define TILE_SIZE 32 +#include "data.h" -#define MAP_RENDER_OFFSET_X ((SCREEN_WIDTH - (TILE_WIDTH * MAP_RENDER_SIZE)) / 2) -#define MAP_RENDER_OFFSET_Y 425 - -typedef struct { - int x; - int y; - int sx; - int sy; - AtlasImage *texture; -} ISOObject; +// Function prototype for drawing game data +void drawGameData(SDL_Renderer *renderer, const GameData *gameData, SDL_Texture *tileTexture, SDL_Texture *unitTexture, SDL_Texture *buildingTexture); +<<<<<<< HEAD >>>>>>> 8e3d2ca (add game structure) +======= +#endif /* DRAW_H */ +>>>>>>> 6abc1cd (draw function) diff --git a/src/main.c b/src/main.c index 05dbc06..41870c7 100644 --- a/src/main.c +++ b/src/main.c @@ -5,52 +5,6 @@ const int SCREEN_WIDTH = 800; const int SCREEN_HEIGHT = 600; -// 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); - - // 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]; - - // 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); -} int main(int argc, char* argv[]) { SDL_Init(SDL_INIT_VIDEO); From c690a9e08389d435b1b26854a6e1032b6cfeb38e Mon Sep 17 00:00:00 2001 From: Rysertio Date: Wed, 21 Feb 2024 09:46:44 +0600 Subject: [PATCH 7/9] basic ui controls --- src/ui.c | 4 ++++ src/ui.h | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/src/ui.c b/src/ui.c index 252f1b3..3ced863 100644 --- a/src/ui.c +++ b/src/ui.c @@ -29,6 +29,7 @@ void handleButtonEvents(SDL_Event *event, Button buttons[], int numButtons) { for (int i = 0; i < numButtons; i++) { handleButtonClick(event, &buttons[i]); } +<<<<<<< HEAD } void drawButtons(SDL_Renderer *renderer, Button buttons[], int numButtons, TTF_Font *font) { @@ -65,3 +66,6 @@ void drawButtons(SDL_Renderer *renderer, Button buttons[], int numButtons, TTF_F // Present renderer SDL_RenderPresent(renderer); } +======= +} +>>>>>>> 206ed0e (basic ui controls) diff --git a/src/ui.h b/src/ui.h index 0265137..137b9b9 100644 --- a/src/ui.h +++ b/src/ui.h @@ -9,13 +9,19 @@ typedef struct { int x, y, w, h; bool clicked; void (*onClick)(); +<<<<<<< HEAD char text[50]; +======= +>>>>>>> 206ed0e (basic ui controls) } Button; // Function prototypes bool isPointInsideRect(int x, int y, SDL_Rect rect); void handleButtonClick(SDL_Event *event, Button *button); void handleButtonEvents(SDL_Event *event, Button buttons[], int numButtons); +<<<<<<< HEAD void drawButtons(SDL_Renderer *renderer, Button buttons[], int numButtons, TTF_Font *font); +======= +>>>>>>> 206ed0e (basic ui controls) #endif /* UI_H */ From 08621ab4896eb94408c2b19b0bddf5a28738c837 Mon Sep 17 00:00:00 2001 From: Rysertio Date: Wed, 21 Feb 2024 13:30:49 +0600 Subject: [PATCH 8/9] ui --- src/data.h | 7 +++++++ src/draw.c | 6 ++++++ src/main.c | 6 ++++-- src/ui.c | 6 ++++++ src/ui.h | 8 ++++++++ 5 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/data.h b/src/data.h index d04879c..b216717 100644 --- a/src/data.h +++ b/src/data.h @@ -134,6 +134,9 @@ typedef struct GameData { Map currentMap; Mission missions[MAX_MISSIONS]; <<<<<<< HEAD +<<<<<<< HEAD +======= +>>>>>>> 4b87dcb (ui) int mapx, mapy; } GameData; @@ -162,7 +165,11 @@ typedef struct { GameData data; BuildingData buildings[100]; UnitData units[100]; +<<<<<<< HEAD } Game; ======= } GameData; >>>>>>> 8e3d2ca (add game structure) +======= +} Game; +>>>>>>> 4b87dcb (ui) diff --git a/src/draw.c b/src/draw.c index e95a825..57ef0ce 100644 --- a/src/draw.c +++ b/src/draw.c @@ -47,6 +47,9 @@ void drawGameData(SDL_Renderer *renderer, const GameData *gameData, SDL_Texture // Present the rendered frame SDL_RenderPresent(renderer); <<<<<<< HEAD +<<<<<<< HEAD +======= +>>>>>>> 4b87dcb (ui) } void drawMinimap(SDL_Renderer *renderer, const Map *map, int minimapX, int minimapY, int minimapSize) { @@ -71,6 +74,9 @@ void drawMinimap(SDL_Renderer *renderer, const Map *map, int minimapX, int minim SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); SDL_Rect minimapBorderRect = {minimapX, minimapY, minimapSize, minimapSize}; SDL_RenderDrawRect(renderer, &minimapBorderRect); +<<<<<<< HEAD ======= >>>>>>> 6abc1cd (draw function) +======= +>>>>>>> 4b87dcb (ui) } \ No newline at end of file diff --git a/src/main.c b/src/main.c index 41870c7..f2021e6 100644 --- a/src/main.c +++ b/src/main.c @@ -1,11 +1,13 @@ #include #include #include "data.h" +#include "draw.h" +#include "input.h" +#include "ui.h" const int SCREEN_WIDTH = 800; const int SCREEN_HEIGHT = 600; - int main(int argc, char* argv[]) { SDL_Init(SDL_INIT_VIDEO); SDL_Window* window = SDL_CreateWindow("RTS Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, @@ -55,4 +57,4 @@ int main(int argc, char* argv[]) { SDL_Quit(); return 0; -} +} \ No newline at end of file diff --git a/src/ui.c b/src/ui.c index 3ced863..d911331 100644 --- a/src/ui.c +++ b/src/ui.c @@ -30,6 +30,9 @@ void handleButtonEvents(SDL_Event *event, Button buttons[], int numButtons) { handleButtonClick(event, &buttons[i]); } <<<<<<< HEAD +<<<<<<< HEAD +======= +>>>>>>> 4b87dcb (ui) } void drawButtons(SDL_Renderer *renderer, Button buttons[], int numButtons, TTF_Font *font) { @@ -66,6 +69,9 @@ void drawButtons(SDL_Renderer *renderer, Button buttons[], int numButtons, TTF_F // Present renderer SDL_RenderPresent(renderer); } +<<<<<<< HEAD ======= } >>>>>>> 206ed0e (basic ui controls) +======= +>>>>>>> 4b87dcb (ui) diff --git a/src/ui.h b/src/ui.h index 137b9b9..2c3ce77 100644 --- a/src/ui.h +++ b/src/ui.h @@ -9,10 +9,14 @@ typedef struct { int x, y, w, h; bool clicked; void (*onClick)(); +<<<<<<< HEAD <<<<<<< HEAD char text[50]; ======= >>>>>>> 206ed0e (basic ui controls) +======= + char text[50]; +>>>>>>> 4b87dcb (ui) } Button; // Function prototypes @@ -20,8 +24,12 @@ bool isPointInsideRect(int x, int y, SDL_Rect rect); void handleButtonClick(SDL_Event *event, Button *button); void handleButtonEvents(SDL_Event *event, Button buttons[], int numButtons); <<<<<<< HEAD +<<<<<<< HEAD void drawButtons(SDL_Renderer *renderer, Button buttons[], int numButtons, TTF_Font *font); ======= >>>>>>> 206ed0e (basic ui controls) +======= +void drawButtons(SDL_Renderer *renderer, Button buttons[], int numButtons, TTF_Font *font); +>>>>>>> 4b87dcb (ui) #endif /* UI_H */ From 0e0a294ece36f9db1d8f505cd38bc58feee61a4d Mon Sep 17 00:00:00 2001 From: Rysertio Date: Wed, 21 Feb 2024 13:39:40 +0600 Subject: [PATCH 9/9] fix draw --- src/draw.h | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/draw.h b/src/draw.h index 9fb826f..e4c0484 100644 --- a/src/draw.h +++ b/src/draw.h @@ -1,5 +1,3 @@ -<<<<<<< HEAD -<<<<<<< HEAD #ifndef DRAW_H #define DRAW_H @@ -13,12 +11,9 @@ void drawGameData(SDL_Renderer *renderer, const GameData *gameData, SDL_Texture *tileTexture, SDL_Texture *unitTexture, SDL_Texture *buildingTexture); #endif /* DRAW_H */ -======= #define MAP_SIZE 96 -======= #ifndef DRAW_H #define DRAW_H ->>>>>>> 6abc1cd (draw function) #include @@ -29,8 +24,4 @@ void drawGameData(SDL_Renderer *renderer, const GameData *gameData, SDL_Texture // Function prototype for drawing game data void drawGameData(SDL_Renderer *renderer, const GameData *gameData, SDL_Texture *tileTexture, SDL_Texture *unitTexture, SDL_Texture *buildingTexture); -<<<<<<< HEAD ->>>>>>> 8e3d2ca (add game structure) -======= #endif /* DRAW_H */ ->>>>>>> 6abc1cd (draw function)