add makefile

This commit is contained in:
Rysertio 2024-02-15 20:18:37 +06:00
parent ed335c9e01
commit 563504af1b
10 changed files with 279 additions and 93 deletions

57
.gitignore vendored
View File

@ -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

View File

@ -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)
-include $(DEPS)

92
src/data.h Normal file
View File

@ -0,0 +1,92 @@
#pragma once
#include <stdbool.h>
#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;

73
src/draw.c Normal file
View File

@ -0,0 +1,73 @@
#include <SDL2/SDL.h>
#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);
}

13
src/draw.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef DRAW_H
#define DRAW_H
#include <SDL2/SDL.h>
// 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 */

0
src/input.c Normal file
View File

0
src/input.h Normal file
View File

View File

@ -1,6 +1,6 @@
#include <SDL2/SDL.h>
#include <stdbool.h>
// Define constants for screen width and height
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;

67
src/ui.c Normal file
View File

@ -0,0 +1,67 @@
#include "ui.h"
#include <stdbool.h>
#include <SDL2/SDL.h>
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);
}

21
src/ui.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef UI_H
#define UI_H
#include <stdbool.h>
#include <SDL2/SDL.h>
// 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 */