#include "game.h"
#include <SDL_timer.h>
#include <iostream>
// Entity
Entity::Entity(int x, int y, int w, int h, SDL_Texture* tex, SDL_Rect src, SDL_Rect dest)
: x(x), y(y), width(w), height(h), texture(tex), srcRect(src), destRect(dest) {}
void Entity::move(int& newX, int& newY, Tile map[][SCREEN_WIDTH/TILE_SIZE]) {
if (newX < 0 || newX + width >= SCREEN_WIDTH || // Out of bounds check
newY < 0 || newY + height >= SCREEN_HEIGHT ||
map[newY / TILE_SIZE][newX / TILE_SIZE].type == 1) { // Collision with wall
// Don't move if out of bounds or into a wall
return;
}
x = newX;
y = newY;
destRect.x = x - cameraX; // Adjust position based on camera
destRect.y = y - cameraY;
}
// Player
Player::Player(int x, int y, SDL_Texture* tex, SDL_Rect src, SDL_Rect dest)
: Entity(x, y, 32, 32, tex, src, dest) {}
void Player::handleInput(SDL_Event& e, float deltaTime, Tile map[][SCREEN_WIDTH/TILE_SIZE]) {
int newX = x, newY = y;
if (e.type == SDL_KEYDOWN) {
switch (e.key.keysym.sym) {
case SDLK_UP: newY -= 32; break;
case SDLK_DOWN: newY += 32; break;
case SDLK_LEFT: newX -= 32; break;
case SDLK_RIGHT: newX += 32; break;
}
move(newX, newY, map);
// Update camera if moving off-screen
if (x > SCREEN_WIDTH - width - 100) cameraX = x - (SCREEN_WIDTH - width - 100);
else if (x < 100) cameraX = x - 100;
if (y > SCREEN_HEIGHT - height - 100) cameraY = y - (SCREEN_HEIGHT - height - 100);
else if (y < 100) cameraY = y - 100;
}
}
// Enemy
Enemy::Enemy(int x, int y, SDL_Texture* tex, SDL_Rect src, SDL_Rect dest)
: Entity(x, y, 32, 32, tex, src, dest) {}
void Enemy::update(float deltaTime, Tile map[][SCREEN_WIDTH/TILE_SIZE]) {
// Simple example: Move down
int newY = y + 1;
if (newY / TILE_SIZE < SCREEN_HEIGHT / TILE_SIZE && map[newY / TILE_SIZE][x / TILE_SIZE].type != 1) {
move(x, newY, map);
}
}
// Game
Game::Game() : running(false), cameraX(0), cameraY(0), lastTime(SDL_GetTicks()) {
// Initialize map (simplified)
for (int i = 0; i < SCREEN_HEIGHT / TILE_SIZE; i++) {
for (int j = 0; j < SCREEN_WIDTH / TILE_SIZE; j++) {
if (i == 0 || j == 0 || i == SCREEN_HEIGHT / TILE_SIZE - 1 || j == SCREEN_WIDTH / TILE_SIZE - 1)
gameMap[i][j].type = 1; // Border as walls
else
gameMap[i][j].type = 0; // Rest as empty
}
}
// Example: Place an interactive object
gameMap[SCREEN_HEIGHT / TILE_SIZE / 2][SCREEN_WIDTH / TILE_SIZE / 2].type = 2;
}
Game::~Game() {
clean();
}
void Game::init(const std::string& title) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cerr << "SDL_Init Error: " << SDL_GetError() << std::endl;
return;
}
window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (!window) {
std::cerr << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer) {
std::cerr << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
SDL_DestroyWindow(window);
SDL_Quit();
return;
}
// Load textures (example, you'll need to implement loading)
SDL_Texture* playerTex = nullptr; // Load your player texture here
SDL_Texture* enemyTex = nullptr; // Load your enemy texture here
SDL_Texture* wallTex = nullptr; // Load your wall texture here
SDL_Rect playerSrc = {0, 0, 32, 32};
SDL_Rect playerDest = {100, 100, 32, 32};
SDL_Rect enemySrc = {0, 0, 32, 32};
SDL_Rect enemyDest = {200, 200, 32, 32};
player = Player(100, 100, playerTex, playerSrc, playerDest);
enemy = Enemy(200, 200, enemyTex, enemySrc, enemyDest);
running = true;
}
void Game::handleEvents() {
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT)
running = false;
player.handleInput(e, deltaTime, gameMap);
}
}
void Game::update() {
Uint32 currentTime = SDL_GetTicks();
deltaTime = (currentTime - lastTime) / 1000.0f;
lastTime = currentTime;
enemy.update(deltaTime, gameMap);
}
void Game::render() {
SDL_RenderClear(renderer);
// Draw map (simplified, only draws walls for demonstration)
for (int i = 0; i < SCREEN_HEIGHT / TILE_SIZE; i++) {
for (int j = 0; j < SCREEN_WIDTH / TILE_SIZE; j++) {
if (gameMap[i][j].type == 1) {
// Draw wall tile (you need to load a wall texture)
SDL_Rect wallDest = {(j * TILE_SIZE) - cameraX, (i * TILE_SIZE) - cameraY, TILE_SIZE, TILE_SIZE};
SDL_RenderCopy(renderer, wallTex, &SDL_Rect{0, 0, TILE_SIZE, TILE_SIZE}, &wallDest);
}
}
}
// Draw entities
SDL_RenderCopy(renderer, player.texture, &player.srcRect, &player.destRect);
SDL_RenderCopy(renderer, enemy.texture, &enemy.srcRect, &enemy.destRect