Merge branch 'main' of git.vyang.org:yangvincenh/star-invaders
This commit is contained in:
17
.gitignore
vendored
17
.gitignore
vendored
@ -1 +1,18 @@
|
||||
build/
|
||||
.vscode/
|
||||
.idea/
|
||||
*.class
|
||||
*.jar
|
||||
*.pyc
|
||||
__pycache__/
|
||||
*.o
|
||||
*.exe
|
||||
*.out
|
||||
*.log
|
||||
.DS_Store
|
||||
|
||||
star-invaders
|
||||
*.kra
|
||||
*.xcf
|
||||
*.png
|
||||
*.*~
|
||||
@ -1,16 +1,22 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
project(SDL2_Example LANGUAGES C)
|
||||
# Specify the minimum required version of CMake
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
|
||||
# Set the C standard to C99
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
# Specify the C++ standard to use
|
||||
set(CMAKE_C_STANDARD 17)
|
||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_C_COMPILER gcc)
|
||||
|
||||
# Find SDL2
|
||||
find_package(SDL2 REQUIRED)
|
||||
# Define the project name and enable C language
|
||||
project(starinvaders LANGUAGES C CXX)
|
||||
|
||||
# Add your executable target
|
||||
# Find all source files in the "src" directory
|
||||
file(GLOB SOURCES "src/*.c")
|
||||
add_executable(star-invaders ${SOURCES})
|
||||
|
||||
# Link the SDL2 libraries to your executable
|
||||
target_link_libraries(star-invaders PRIVATE SDL2::SDL2)
|
||||
# Find zlib and sdl2
|
||||
find_package(ZLIB REQUIRED)
|
||||
find_package(SDL2 CONFIG REQUIRED)
|
||||
|
||||
# Add an executable target named "starinvaders" from the source files in the "src" directory
|
||||
add_executable(starinvaders ${SOURCES})
|
||||
target_link_libraries(starinvaders PRIVATE ZLIB::ZLIB)
|
||||
target_link_libraries(starinvaders PRIVATE SDL2::SDL2)
|
||||
29
Makefile
Normal file
29
Makefile
Normal file
@ -0,0 +1,29 @@
|
||||
# Simple Makefile using sdl-config for SDL flags
|
||||
CC := clang
|
||||
SDL_CFLAGS := $(shell sdl2-config --cflags)
|
||||
SDL_LIBS := $(shell sdl2-config --libs)
|
||||
CFLAGS := -Wall -Wextra -std=c99 -g $(SDL_CFLAGS)
|
||||
SRCDIR := src
|
||||
SOURCES := $(wildcard $(SRCDIR)/*.c)
|
||||
BUILD_DIR := build
|
||||
OBJECTS := $(patsubst $(SRCDIR)/%.c,$(BUILD_DIR)/%.o,$(SOURCES))
|
||||
TARGET := star-invaders
|
||||
|
||||
.PHONY: all clean
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(TARGET): $(OBJECTS)
|
||||
@echo " Linking $@"
|
||||
@$(CC) $(OBJECTS) -o $@ $(SDL_LIBS)
|
||||
|
||||
$(BUILD_DIR)/%.o: $(SRCDIR)/%.c | $(BUILD_DIR)
|
||||
@echo " Compiling $<"
|
||||
@$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
$(BUILD_DIR):
|
||||
@mkdir -p $(BUILD_DIR)
|
||||
|
||||
clean:
|
||||
@echo "Cleaning up"
|
||||
@rm -rf $(BUILD_DIR) $(TARGET)
|
||||
BIN
res/background.bmp
Normal file
BIN
res/background.bmp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.4 MiB |
BIN
res/bullet.bmp
Normal file
BIN
res/bullet.bmp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.1 MiB |
Binary file not shown.
|
Before Width: | Height: | Size: 4.1 KiB |
47
src/bullet.c
47
src/bullet.c
@ -1,9 +1,21 @@
|
||||
#include "bullet.h"
|
||||
#include "game.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void updateBullets(Bullet* bullets, int* bulletCount, SDL_Renderer* renderer) {
|
||||
void initBullet(Bullet *bullet, int x, int y) {
|
||||
bullet->rect.x = x;
|
||||
bullet->rect.y = y;
|
||||
bullet->rect.w = 50; // Bullet width
|
||||
bullet->rect.h = 50; // Bullet height
|
||||
bullet->active = 0;
|
||||
}
|
||||
|
||||
void updateBullets(Bullet *bullets, int *bulletCount, SDL_Renderer* renderer) {
|
||||
int bulletSpeed = 7;
|
||||
|
||||
for (int i = 0; i < *bulletCount; i++) {
|
||||
for (int i = 0; i < MAX_BULLETS; i++) {
|
||||
if (bullets[i].active) {
|
||||
// Move bullet upward
|
||||
bullets[i].rect.y -= bulletSpeed;
|
||||
@ -11,22 +23,31 @@ void updateBullets(Bullet* bullets, int* bulletCount, SDL_Renderer* renderer) {
|
||||
// Deactivate if bullet goes off screen
|
||||
if (bullets[i].rect.y < 0) {
|
||||
bullets[i].active = 0;
|
||||
(*bulletCount)--;
|
||||
} else {
|
||||
// Draw bullet
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
|
||||
SDL_RenderFillRect(renderer, &bullets[i].rect);
|
||||
// Draw bullet using global textures
|
||||
SDL_RenderCopy(renderer, textures.bulletTexture, NULL, &bullets[i].rect);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void fireBullet(Bullet* bullets, int* bulletCount, SDL_Rect* rect) {
|
||||
if (*bulletCount < MAX_BULLETS) {
|
||||
bullets[*bulletCount].rect.x = rect->x + rect->w / 2 - 2;
|
||||
bullets[*bulletCount].rect.y = rect->y - 10;
|
||||
bullets[*bulletCount].rect.w = 4;
|
||||
bullets[*bulletCount].rect.h = 10;
|
||||
bullets[*bulletCount].active = 1;
|
||||
(*bulletCount)++;
|
||||
void fireBullet(Bullet *bullets, int *bulletCount, SDL_Rect* rect) {
|
||||
for (int i = 0; i < MAX_BULLETS; i++) {
|
||||
if (!bullets[i].active) {
|
||||
bullets[i].rect.x = rect->x;
|
||||
bullets[i].rect.y = rect->y;
|
||||
bullets[i].active = 1;
|
||||
(*bulletCount)++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void cleanupBullets(Bullet *bullets, int *bulletCount) {
|
||||
// Deactivate all bullets and reset count
|
||||
for (int i = 0; i < MAX_BULLETS; i++) {
|
||||
bullets[i].active = 0;
|
||||
}
|
||||
*bulletCount = 0;
|
||||
}
|
||||
16
src/bullet.h
16
src/bullet.h
@ -1,16 +1,20 @@
|
||||
#ifndef BULLET_H
|
||||
#define BULLET_H
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL.h>
|
||||
|
||||
#define MAX_BULLETS 100
|
||||
#define MAX_BULLETS 5
|
||||
|
||||
typedef struct {
|
||||
SDL_Rect rect;
|
||||
int active;
|
||||
SDL_Rect rect;
|
||||
int active;
|
||||
} Bullet;
|
||||
|
||||
void updateBullets(Bullet* bullets, int* bulletCount, SDL_Renderer* renderer);
|
||||
void fireBullet(Bullet* bullets, int* bulletCount, SDL_Rect* rect);
|
||||
/* Initialize an existing Bullet struct */
|
||||
void initBullet(Bullet *bullet, int x, int y);
|
||||
|
||||
void updateBullets(Bullet *bullets, int *bulletCount, SDL_Renderer *renderer);
|
||||
void fireBullet(Bullet *bullets, int *bulletCount, SDL_Rect *rect);
|
||||
void cleanupBullets(Bullet *bullets, int *bulletCount);
|
||||
|
||||
#endif
|
||||
|
||||
25
src/game.h
Normal file
25
src/game.h
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef GAME_H
|
||||
#define GAME_H
|
||||
|
||||
#include <SDL.h>
|
||||
|
||||
#define FPS 60
|
||||
#define FRAME_DELAY (1000 / FPS) // Milliseconds per frame
|
||||
#define SCREEN_WIDTH 800
|
||||
#define SCREEN_HEIGHT 600
|
||||
|
||||
typedef struct {
|
||||
char *spaceshipPath;
|
||||
char *backgroundPath;
|
||||
char *bulletPath;
|
||||
} AssetPaths;
|
||||
|
||||
typedef struct {
|
||||
SDL_Texture *bulletTexture;
|
||||
SDL_Texture *playerTexture;
|
||||
SDL_Texture *backgroundTexture;
|
||||
} Textures;
|
||||
|
||||
extern Textures textures;
|
||||
|
||||
#endif /* GAME_H */
|
||||
376
src/main.c
376
src/main.c
@ -1,121 +1,297 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include "bullet.h"
|
||||
#include "game.h"
|
||||
#include "player.h"
|
||||
#include <SDL.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "bullet.h"
|
||||
#include "player.h"
|
||||
#include <string.h>
|
||||
|
||||
#define FPS 60
|
||||
#define FRAME_DELAY (1000 / FPS) // Milliseconds per frame
|
||||
#define SCREEN_WIDTH 800
|
||||
#define SCREEN_HEIGHT 600
|
||||
Textures textures = {0}; // global textures storage (initialized to NULL pointers)
|
||||
|
||||
void handlePlayer(SDL_Renderer* renderer, Player* player, int speed) {
|
||||
// Get current keyboard state
|
||||
const Uint8* keys = SDL_GetKeyboardState(NULL);
|
||||
int loadTextures(SDL_Renderer *renderer, AssetPaths *paths) {
|
||||
SDL_Surface *bulletSurface = SDL_LoadBMP(paths->bulletPath);
|
||||
if (!bulletSurface) {
|
||||
fprintf(stderr, "Could not load bullet image: %s\n", SDL_GetError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (keys[SDL_SCANCODE_LEFT] || keys[SDL_SCANCODE_A]) {
|
||||
movePlayerLeft(player, speed);
|
||||
}
|
||||
if (keys[SDL_SCANCODE_RIGHT] || keys[SDL_SCANCODE_D]) {
|
||||
movePlayerRight(player, speed, SCREEN_WIDTH);
|
||||
textures.bulletTexture = SDL_CreateTextureFromSurface(renderer, bulletSurface);
|
||||
SDL_FreeSurface(bulletSurface);
|
||||
if (!textures.bulletTexture) {
|
||||
fprintf(stderr, "Could not create bullet texture: %s\n", SDL_GetError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
SDL_Surface *playerSurface = SDL_LoadBMP(paths->spaceshipPath);
|
||||
if (!playerSurface) {
|
||||
fprintf(stderr, "Could not load player image: %s\n", SDL_GetError());
|
||||
SDL_DestroyTexture(textures.bulletTexture);
|
||||
textures.bulletTexture = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
textures.playerTexture = SDL_CreateTextureFromSurface(renderer, playerSurface);
|
||||
SDL_FreeSurface(playerSurface);
|
||||
if (!textures.playerTexture) {
|
||||
fprintf(stderr, "Could not create player texture: %s\n", SDL_GetError());
|
||||
SDL_DestroyTexture(textures.bulletTexture);
|
||||
textures.bulletTexture = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Load background (non-fatal) */
|
||||
SDL_Surface *bgSurface = SDL_LoadBMP(paths->backgroundPath);
|
||||
if (!bgSurface) {
|
||||
fprintf(stderr, "Could not load background %s: %s\n", paths->backgroundPath, SDL_GetError());
|
||||
textures.backgroundTexture = NULL;
|
||||
} else {
|
||||
textures.backgroundTexture = SDL_CreateTextureFromSurface(renderer, bgSurface);
|
||||
SDL_FreeSurface(bgSurface);
|
||||
if (!textures.backgroundTexture) {
|
||||
fprintf(stderr, "Could not create background texture: %s\n", SDL_GetError());
|
||||
textures.backgroundTexture = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// Render player flipped vertically
|
||||
renderPlayerFlipped(renderer, player, SDL_FLIP_VERTICAL);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
||||
fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
|
||||
return EXIT_FAILURE;
|
||||
/*
|
||||
* Parse command-line arguments. Returns:
|
||||
* 0 -> continue normally
|
||||
* 1 -> printed help/version, should exit success
|
||||
* -1 -> error in args, should exit failure
|
||||
*/
|
||||
static int handleArguments(int argc, char *argv[], char **resLocation) {
|
||||
for (int args = 1; args < argc; args++) {
|
||||
if (strcmp(argv[args], "--help") == 0 || strcmp(argv[args], "-h") == 0) {
|
||||
printf("Star Invaders Help:\n");
|
||||
printf("Use arrow keys or A/D to move the spaceship left and right.\n");
|
||||
printf("Press SPACE to fire bullets.\n");
|
||||
printf("Press ESC or close the window to exit the game.\n");
|
||||
printf("Use --res-location or -rl to specify the resource location.\n");
|
||||
return 1;
|
||||
} else if (strcmp(argv[args], "--version") == 0 || strcmp(argv[args], "-v") == 0) {
|
||||
printf("Star Invaders Version 1.0.0\n");
|
||||
return 1;
|
||||
} else if (strcmp(argv[args], "--res-location") == 0 || strcmp(argv[args], "-rl") == 0) {
|
||||
if (args + 1 >= argc) {
|
||||
fprintf(stderr, "Missing value for %s\n", argv[args]);
|
||||
printf("Use --help or -h for usage information.\n");
|
||||
return -1;
|
||||
}
|
||||
*resLocation = argv[++args];
|
||||
} else {
|
||||
printf("Unknown argument: %s\n", argv[args]);
|
||||
printf("Use --help or -h for usage information.\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
SDL_Window* window = SDL_CreateWindow("Star Invaders",
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SCREEN_WIDTH, SCREEN_HEIGHT,
|
||||
SDL_WINDOW_SHOWN);
|
||||
if (!window) {
|
||||
fprintf(stderr, "Could not create window: %s\n", SDL_GetError());
|
||||
SDL_Quit();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
AssetPaths *getResources(const char *resLocation) {
|
||||
AssetPaths *paths = (AssetPaths *)malloc(sizeof(AssetPaths));
|
||||
if (!paths) {
|
||||
fprintf(stderr, "Out of memory\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
if (!renderer) {
|
||||
fprintf(stderr, "Could not create renderer: %s\n", SDL_GetError());
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
size_t pathLen;
|
||||
const char *shipFile = "spaceship.bmp";
|
||||
const char *bgFile = "background.bmp";
|
||||
const char *bulletFile = "bullet.bmp";
|
||||
|
||||
// Rectangle position and size
|
||||
SDL_Rect rect = {400, 500, 50, 50};
|
||||
int speed = 5;
|
||||
pathLen = strlen(resLocation) + strlen(shipFile) + 2;
|
||||
paths->spaceshipPath = (char *)malloc(pathLen);
|
||||
if (!paths->spaceshipPath) {
|
||||
free(paths);
|
||||
fprintf(stderr, "Out of memory\n");
|
||||
return NULL;
|
||||
}
|
||||
if (resLocation[strlen(resLocation) - 1] == '/') {
|
||||
snprintf(paths->spaceshipPath, pathLen, "%s%s", resLocation, shipFile);
|
||||
} else {
|
||||
snprintf(paths->spaceshipPath, pathLen, "%s/%s", resLocation, shipFile);
|
||||
}
|
||||
|
||||
// Bullets array
|
||||
Bullet bullets[MAX_BULLETS];
|
||||
int bulletCount = 0;
|
||||
for (int i = 0; i < MAX_BULLETS; i++) {
|
||||
bullets[i].active = 0;
|
||||
}
|
||||
pathLen = strlen(resLocation) + strlen(bgFile) + 2;
|
||||
paths->backgroundPath = (char *)malloc(pathLen);
|
||||
if (!paths->backgroundPath) {
|
||||
free(paths->spaceshipPath);
|
||||
free(paths);
|
||||
fprintf(stderr, "Out of memory\n");
|
||||
return NULL;
|
||||
}
|
||||
if (resLocation[strlen(resLocation) - 1] == '/') {
|
||||
snprintf(paths->backgroundPath, pathLen, "%s%s", resLocation, bgFile);
|
||||
} else {
|
||||
snprintf(paths->backgroundPath, pathLen, "%s/%s", resLocation, bgFile);
|
||||
}
|
||||
|
||||
// Create player
|
||||
Player* player = createPlayer(renderer, "../res/spaceship.bmp", 400, 500);
|
||||
if (!player) {
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
pathLen = strlen(resLocation) + strlen(bulletFile) + 2;
|
||||
paths->bulletPath = (char *)malloc(pathLen);
|
||||
if (!paths->bulletPath) {
|
||||
free(paths->backgroundPath);
|
||||
free(paths->spaceshipPath);
|
||||
free(paths);
|
||||
fprintf(stderr, "Out of memory\n");
|
||||
return NULL;
|
||||
}
|
||||
if (resLocation[strlen(resLocation) - 1] == '/') {
|
||||
snprintf(paths->bulletPath, pathLen, "%s%s", resLocation, bulletFile);
|
||||
} else {
|
||||
snprintf(paths->bulletPath, pathLen, "%s/%s", resLocation, bulletFile);
|
||||
}
|
||||
|
||||
// Main loop flag
|
||||
int running = 1;
|
||||
SDL_Event event;
|
||||
Uint32 frameStart;
|
||||
int frameTime;
|
||||
int spacePressedLastFrame = 0;
|
||||
return paths;
|
||||
}
|
||||
|
||||
// Main loop
|
||||
while (running) {
|
||||
frameStart = SDL_GetTicks();
|
||||
int main(int argc, char *argv[]) {
|
||||
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
||||
fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_QUIT) {
|
||||
running = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Get current keyboard state for space key
|
||||
const Uint8* keys = SDL_GetKeyboardState(NULL);
|
||||
if (keys[SDL_SCANCODE_SPACE] && !spacePressedLastFrame) {
|
||||
fireBullet(bullets, &bulletCount, &player->rect);
|
||||
}
|
||||
spacePressedLastFrame = keys[SDL_SCANCODE_SPACE];
|
||||
|
||||
// Clear screen with black
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
// Update and draw bullets
|
||||
updateBullets(bullets, &bulletCount, renderer);
|
||||
|
||||
// Handle player movement and rendering
|
||||
handlePlayer(renderer, player, speed);
|
||||
|
||||
// Present the rendered frame
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
// Cap framerate at 60 FPS
|
||||
frameTime = SDL_GetTicks() - frameStart;
|
||||
if (frameTime < FRAME_DELAY) {
|
||||
SDL_Delay(FRAME_DELAY - frameTime);
|
||||
}
|
||||
}
|
||||
|
||||
destroyPlayer(player);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
char *resLocation = "./res/";
|
||||
int argResult = handleArguments(argc, argv, &resLocation);
|
||||
if (argResult == 1) {
|
||||
SDL_Quit();
|
||||
return EXIT_SUCCESS;
|
||||
} else if (argResult == -1) {
|
||||
SDL_Quit();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
AssetPaths *paths = getResources(resLocation);
|
||||
if (!paths) {
|
||||
fprintf(stderr, "Could not load resources\n");
|
||||
SDL_Quit();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
SDL_Window *window = SDL_CreateWindow("Star Invaders", SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
|
||||
SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
|
||||
if (!window) {
|
||||
fprintf(stderr, "Could not create window: %s\n", SDL_GetError());
|
||||
free(paths->backgroundPath);
|
||||
free(paths->bulletPath);
|
||||
free(paths->spaceshipPath);
|
||||
free(paths);
|
||||
SDL_Quit();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
SDL_Renderer *renderer =
|
||||
SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
if (!renderer) {
|
||||
fprintf(stderr, "Could not create renderer: %s\n", SDL_GetError());
|
||||
SDL_DestroyWindow(window);
|
||||
free(paths->backgroundPath);
|
||||
free(paths->bulletPath);
|
||||
free(paths->spaceshipPath);
|
||||
free(paths);
|
||||
SDL_Quit();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (!loadTextures(renderer, paths)) {
|
||||
fprintf(stderr, "Could not create game textures: %s\n", SDL_GetError());
|
||||
free(paths->backgroundPath);
|
||||
free(paths->bulletPath);
|
||||
free(paths->spaceshipPath);
|
||||
free(paths);
|
||||
SDL_Quit();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// Bullets array
|
||||
int bulletCount = 0;
|
||||
Bullet bullets[MAX_BULLETS];
|
||||
for (int i = 0; i < MAX_BULLETS; i++) {
|
||||
initBullet(&bullets[i], 0, 0);
|
||||
}
|
||||
|
||||
// Create player
|
||||
Player *player = createPlayer(400, 500);
|
||||
if (!player) {
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
free(paths->spaceshipPath);
|
||||
free(paths->backgroundPath);
|
||||
free(paths);
|
||||
if (textures.bulletTexture) SDL_DestroyTexture(textures.bulletTexture);
|
||||
if (textures.playerTexture) SDL_DestroyTexture(textures.playerTexture);
|
||||
SDL_Quit();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* background is loaded into textures.backgroundTexture by loadTextures */
|
||||
|
||||
// Main loop flag
|
||||
int running = 1;
|
||||
SDL_Event event;
|
||||
Uint32 frameStart;
|
||||
int frameTime;
|
||||
int spacePressedLastFrame = 0;
|
||||
|
||||
// Main loop
|
||||
while (running) {
|
||||
frameStart = SDL_GetTicks();
|
||||
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_QUIT) {
|
||||
running = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Get current keyboard state for space key
|
||||
const Uint8 *keys = SDL_GetKeyboardState(NULL);
|
||||
if (keys[SDL_SCANCODE_SPACE] && !spacePressedLastFrame) {
|
||||
fireBullet(bullets, &bulletCount, &player->rect);
|
||||
}
|
||||
spacePressedLastFrame = keys[SDL_SCANCODE_SPACE];
|
||||
|
||||
// Clear screen with black
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
// Draw background if available
|
||||
if (textures.backgroundTexture) {
|
||||
SDL_RenderCopy(renderer, textures.backgroundTexture, NULL, NULL);
|
||||
}
|
||||
|
||||
// Update and draw bullets
|
||||
updateBullets(bullets, &bulletCount, renderer);
|
||||
// cleanupUnrenderedBullets(bullets, &bulletCount); // removed
|
||||
|
||||
// Handle player movement and rendering
|
||||
handlePlayer(renderer, player, playerSpeed, keys);
|
||||
|
||||
// Present the rendered frame
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
// Cap framerate at 60 FPS
|
||||
frameTime = SDL_GetTicks() - frameStart;
|
||||
if (frameTime < FRAME_DELAY) {
|
||||
SDL_Delay(FRAME_DELAY - frameTime);
|
||||
}
|
||||
}
|
||||
|
||||
destroyPlayer(player);
|
||||
|
||||
// Cleanup bullets (deactivate all)
|
||||
cleanupBullets(bullets, &bulletCount);
|
||||
|
||||
if (textures.bulletTexture) SDL_DestroyTexture(textures.bulletTexture);
|
||||
if (textures.playerTexture) SDL_DestroyTexture(textures.playerTexture);
|
||||
if (textures.backgroundTexture) SDL_DestroyTexture(textures.backgroundTexture);
|
||||
|
||||
free(paths->spaceshipPath);
|
||||
free(paths->backgroundPath);
|
||||
free(paths);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
80
src/player.c
80
src/player.c
@ -1,57 +1,57 @@
|
||||
#include "player.h"
|
||||
#include "game.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
Player* createPlayer(SDL_Renderer* renderer, const char* imagePath, int x, int y) {
|
||||
Player* player = (Player*)malloc(sizeof(Player));
|
||||
int playerSpeed = 5;
|
||||
|
||||
SDL_Surface* surface = SDL_LoadBMP(imagePath);
|
||||
if (!surface) {
|
||||
fprintf(stderr, "Could not load image %s: %s\n", imagePath, SDL_GetError());
|
||||
free(player);
|
||||
return NULL;
|
||||
}
|
||||
void handlePlayer(SDL_Renderer *renderer, Player *player, int playerSpeed, const Uint8 *keys) {
|
||||
if (keys[SDL_SCANCODE_LEFT] || keys[SDL_SCANCODE_A]) {
|
||||
movePlayerLeft(player, playerSpeed);
|
||||
}
|
||||
if (keys[SDL_SCANCODE_RIGHT] || keys[SDL_SCANCODE_D]) {
|
||||
movePlayerRight(player, playerSpeed, SCREEN_WIDTH);
|
||||
}
|
||||
|
||||
player->texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||
SDL_FreeSurface(surface);
|
||||
|
||||
if (!player->texture) {
|
||||
fprintf(stderr, "Could not create texture: %s\n", SDL_GetError());
|
||||
free(player);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
player->rect.x = x;
|
||||
player->rect.y = y;
|
||||
player->rect.w = 50;
|
||||
player->rect.h = 50;
|
||||
|
||||
return player;
|
||||
// Render player flipped vertically
|
||||
renderPlayerFlipped(renderer, player, SDL_FLIP_VERTICAL);
|
||||
}
|
||||
|
||||
void renderPlayer(SDL_Renderer* renderer, Player* player) {
|
||||
SDL_RenderCopy(renderer, player->texture, NULL, &player->rect);
|
||||
Player *createPlayer(int x, int y) {
|
||||
Player *player = (Player *)malloc(sizeof(Player));
|
||||
|
||||
player->rect.x = x;
|
||||
player->rect.y = y;
|
||||
player->rect.w = 50;
|
||||
player->rect.h = 50;
|
||||
|
||||
return player;
|
||||
}
|
||||
|
||||
void renderPlayerFlipped(SDL_Renderer* renderer, Player* player, SDL_RendererFlip flip) {
|
||||
SDL_RenderCopyEx(renderer, player->texture, NULL, &player->rect, 0, NULL, flip);
|
||||
void renderPlayer(SDL_Renderer *renderer, Player *player) {
|
||||
SDL_RenderCopy(renderer, textures.playerTexture, NULL, &player->rect);
|
||||
}
|
||||
|
||||
void movePlayerLeft(Player* player, int speed) {
|
||||
if (player->rect.x - speed >= 0) {
|
||||
player->rect.x -= speed;
|
||||
}
|
||||
void renderPlayerFlipped(SDL_Renderer *renderer, Player *player,
|
||||
SDL_RendererFlip flip) {
|
||||
SDL_RenderCopyEx(renderer, textures.playerTexture, NULL, &player->rect, 0, NULL,
|
||||
flip);
|
||||
}
|
||||
|
||||
void movePlayerRight(Player* player, int speed, int screenWidth) {
|
||||
if (player->rect.x + player->rect.w + speed <= screenWidth) {
|
||||
player->rect.x += speed;
|
||||
}
|
||||
void movePlayerLeft(Player *player, int playerSpeed) {
|
||||
if (player->rect.x - playerSpeed >= 0) {
|
||||
player->rect.x -= playerSpeed;
|
||||
}
|
||||
}
|
||||
|
||||
void destroyPlayer(Player* player) {
|
||||
if (player) {
|
||||
SDL_DestroyTexture(player->texture);
|
||||
free(player);
|
||||
}
|
||||
void movePlayerRight(Player *player, int playerSpeed, int screenWidth) {
|
||||
if (player->rect.x + player->rect.w + playerSpeed <= screenWidth) {
|
||||
player->rect.x += playerSpeed;
|
||||
}
|
||||
}
|
||||
|
||||
void destroyPlayer(Player *player) {
|
||||
if (player) {
|
||||
free(player);
|
||||
}
|
||||
}
|
||||
|
||||
22
src/player.h
22
src/player.h
@ -1,18 +1,22 @@
|
||||
#ifndef PLAYER_H
|
||||
#define PLAYER_H
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL.h>
|
||||
|
||||
typedef struct {
|
||||
SDL_Texture* texture;
|
||||
SDL_Rect rect;
|
||||
SDL_Rect rect;
|
||||
} Player;
|
||||
|
||||
Player* createPlayer(SDL_Renderer* renderer, const char* imagePath, int x, int y);
|
||||
void renderPlayer(SDL_Renderer* renderer, Player* player);
|
||||
void renderPlayerFlipped(SDL_Renderer* renderer, Player* player, SDL_RendererFlip flip);
|
||||
void movePlayerLeft(Player* player, int speed);
|
||||
void movePlayerRight(Player* player, int speed, int screenWidth);
|
||||
void destroyPlayer(Player* player);
|
||||
extern int playerSpeed;
|
||||
|
||||
void handlePlayer(SDL_Renderer *renderer, Player *player, int playerSpeed, const Uint8 *keys);
|
||||
|
||||
Player *createPlayer(int x, int y);
|
||||
void renderPlayer(SDL_Renderer *renderer, Player *player);
|
||||
void renderPlayerFlipped(SDL_Renderer *renderer, Player *player,
|
||||
SDL_RendererFlip flip);
|
||||
void movePlayerLeft(Player *player, int playerSpeed);
|
||||
void movePlayerRight(Player *player, int playerSpeed, int screenWidth);
|
||||
void destroyPlayer(Player *player);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user