138 lines
3.8 KiB
C++
138 lines
3.8 KiB
C++
#include "engine.hpp"
|
|
|
|
Engine::Engine(const char* windowTitle, int windowWidth, int windowHeight, int windowFlags) {
|
|
// Initialize SDL
|
|
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
|
SDL_Log("SDL could not initialize! SDL_Error: %s", SDL_GetError());
|
|
return;
|
|
}
|
|
|
|
// Create the window
|
|
window = SDL_CreateWindow(windowTitle, windowWidth, windowHeight, windowFlags);
|
|
if (!window) {
|
|
SDL_Log("Window could not be created! SDL_Error: %s", SDL_GetError());
|
|
return;
|
|
}
|
|
|
|
// Create the renderer
|
|
renderer = SDL_CreateRenderer(window, nullptr);
|
|
if (!renderer) {
|
|
SDL_Log("Renderer could not be created! SDL_Error: %s", SDL_GetError());
|
|
return;
|
|
}
|
|
}
|
|
|
|
Engine::~Engine() {
|
|
// Destroy the asset manager
|
|
assets.clearAssets();
|
|
|
|
// Destroy the renderer
|
|
if (renderer) {
|
|
SDL_DestroyRenderer(renderer);
|
|
}
|
|
|
|
// Destroy the window
|
|
if (window) {
|
|
SDL_DestroyWindow(window);
|
|
}
|
|
|
|
// Quit SDL subsystems
|
|
SDL_Quit();
|
|
}
|
|
|
|
void Engine::clearScreen(const Color& color) {
|
|
// Set the draw color
|
|
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
|
|
|
|
// Clear the screen
|
|
SDL_RenderClear(renderer);
|
|
}
|
|
|
|
void Engine::present() {
|
|
// Present the renderer
|
|
SDL_RenderPresent(renderer);
|
|
|
|
// Calculate frame duration
|
|
frameEndTime = SDL_GetTicks();
|
|
frameDuration = frameEndTime - frameStartTime;
|
|
// Calculate delay to maintain target FPS
|
|
if (targetFPSValue > 0) {
|
|
Uint32 targetFrameTime = 1000 / targetFPSValue;
|
|
if (frameDuration < targetFrameTime) {
|
|
SDL_Delay(targetFrameTime - frameDuration);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Engine::draw(SDL_Surface* surface, Rect destRect) {
|
|
// Draw a surface to the renderer at the specified destination rectangle
|
|
if (!surface) {
|
|
SDL_Log("Cannot draw null surface");
|
|
return;
|
|
}
|
|
|
|
SDL_FRect sdlDestRect = {
|
|
static_cast<float>(destRect.x),
|
|
static_cast<float>(destRect.y),
|
|
static_cast<float>(destRect.width),
|
|
static_cast<float>(destRect.height)
|
|
};
|
|
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
|
|
if (!texture) {
|
|
SDL_Log("Failed to create texture from surface: %s", SDL_GetError());
|
|
return;
|
|
}
|
|
|
|
SDL_RenderTexture(renderer, texture, nullptr, &sdlDestRect);
|
|
SDL_DestroyTexture(texture);
|
|
}
|
|
|
|
void Engine::draw(SDL_Surface* surface, Rect srcRect, Rect destRect) {
|
|
// Draw a surface to the renderer with specified source and destination rectangles
|
|
if (!surface) {
|
|
SDL_Log("Cannot draw null surface");
|
|
return;
|
|
}
|
|
|
|
SDL_FRect sdlSrcRect = {
|
|
static_cast<float>(srcRect.x),
|
|
static_cast<float>(srcRect.y),
|
|
static_cast<float>(srcRect.width),
|
|
static_cast<float>(srcRect.height)
|
|
};
|
|
SDL_FRect sdlDestRect = {
|
|
static_cast<float>(destRect.x),
|
|
static_cast<float>(destRect.y),
|
|
static_cast<float>(destRect.width),
|
|
static_cast<float>(destRect.height)
|
|
};
|
|
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
|
|
if (!texture) {
|
|
SDL_Log("Failed to create texture from surface: %s", SDL_GetError());
|
|
return;
|
|
}
|
|
|
|
SDL_RenderTexture(renderer, texture, &sdlSrcRect, &sdlDestRect);
|
|
SDL_DestroyTexture(texture);
|
|
}
|
|
|
|
void Engine::draw(Tile tile, Rect destRect) {
|
|
// Draw a tile to the renderer at the specified destination rectangle
|
|
SDL_Surface* surface = assets.getAsset(tile.assetName);
|
|
if (!surface) {
|
|
SDL_Log("Tile asset %s not found", tile.assetName.c_str());
|
|
return;
|
|
}
|
|
|
|
draw(surface, tile.srcRect, destRect);
|
|
}
|
|
|
|
void Engine::startFrame() {
|
|
// Start the frame timer
|
|
frameStartTime = SDL_GetTicks();
|
|
}
|
|
|
|
void Engine::targetFPS(int fps) {
|
|
// Set the target frames per second
|
|
targetFPSValue = fps;
|
|
} |