Compare commits

...

9 Commits

Author SHA1 Message Date
Rysertio 0e0a294ece fix draw 2024-02-21 13:39:40 +06:00
Rysertio 08621ab489 ui 2024-02-21 13:38:23 +06:00
Rysertio c690a9e083 basic ui controls 2024-02-21 13:38:17 +06:00
Rysertio 17fdac54f4 draw function 2024-02-21 13:38:07 +06:00
Rysertio 808fd83e6e draw function added 2024-02-21 13:37:56 +06:00
Rysertio 6b2517604e update gamestructure 2024-02-21 13:37:33 +06:00
Rysertio 73ca24c36b add game structure 2024-02-21 13:37:00 +06:00
Rysertio 563504af1b add makefile 2024-02-21 13:36:04 +06:00
Rysertio ed335c9e01 init 2024-02-21 13:31:01 +06:00
10 changed files with 479 additions and 54 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

20
makefile Normal file
View File

@ -0,0 +1,20 @@
TARGET ?= build/rts_game
SRC_DIRS ?= ./src
SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c -or -name *.s)
OBJS := $(addsuffix .o,$(basename $(SRCS)))
DEPS := $(OBJS:.o=.d)
INC_DIRS := $(shell find $(SRC_DIRS) -type d)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
CPPFLAGS ?= $(INC_FLAGS) -MMD -MP
$(TARGET): $(OBJS)
$(CC) $(LDFLAGS) $(OBJS) -o $@ $(LOADLIBES) $(LDLIBS) -lSDL2
.PHONY: clean
clean:
$(RM) $(TARGET) $(OBJS) $(DEPS)
-include $(DEPS)

175
src/data.h Normal file
View File

@ -0,0 +1,175 @@
<<<<<<< HEAD
<<<<<<< HEAD
#pragma once
=======
>>>>>>> 8e3d2ca (add game structure)
=======
#pragma once
>>>>>>> 08f4a9f (draw function added)
#include <stdbool.h>
#define MAX_UNITS 100
#define MAX_BUILDINGS 50
#define MAX_PLAYERS 2
<<<<<<< HEAD
<<<<<<< HEAD
#define MAX_MISSIONS 10
#define MAX_RESOURCES 50
#define MAP_SIZE 100
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
bool isConstructed;
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
<<<<<<< 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 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;
<<<<<<< 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];
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;
<<<<<<< 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];
Map currentMap;
Mission missions[MAX_MISSIONS];
<<<<<<< HEAD
<<<<<<< HEAD
=======
>>>>>>> 4b87dcb (ui)
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];
<<<<<<< HEAD
} Game;
=======
} GameData;
>>>>>>> 8e3d2ca (add game structure)
=======
} Game;
>>>>>>> 4b87dcb (ui)

82
src/draw.c Normal file
View File

@ -0,0 +1,82 @@
#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);
<<<<<<< HEAD
<<<<<<< HEAD
=======
>>>>>>> 4b87dcb (ui)
}
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);
<<<<<<< HEAD
=======
>>>>>>> 6abc1cd (draw function)
=======
>>>>>>> 4b87dcb (ui)
}

27
src/draw.h Normal file
View File

@ -0,0 +1,27 @@
#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 */
#define MAP_SIZE 96
#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

60
src/main.c Normal file
View File

@ -0,0 +1,60 @@
#include <SDL2/SDL.h>
#include <stdbool.h>
#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,
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);
SDL_DestroyWindow(window);
// Quit SDL subsystems
SDL_Quit();
return 0;
}

77
src/ui.c Normal file
View File

@ -0,0 +1,77 @@
#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]);
}
<<<<<<< HEAD
<<<<<<< HEAD
=======
>>>>>>> 4b87dcb (ui)
}
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);
}
<<<<<<< HEAD
=======
}
>>>>>>> 206ed0e (basic ui controls)
=======
>>>>>>> 4b87dcb (ui)

35
src/ui.h Normal file
View File

@ -0,0 +1,35 @@
#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)();
<<<<<<< HEAD
<<<<<<< HEAD
char text[50];
=======
>>>>>>> 206ed0e (basic ui controls)
=======
char text[50];
>>>>>>> 4b87dcb (ui)
} 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
<<<<<<< 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 */