feat: implement textured line clear effects and refine UI alignment
- **Visual Effects**: Upgraded line clear particles to use the game's block texture instead of simple circles, matching the reference web game's aesthetic. - **Particle Physics**: Tuned particle velocity, gravity, and fade rates for a more dynamic explosion effect. - **Rendering Integration**: Updated [main.cpp](cci:7://file:///d:/Sites/Work/tetris/src/main.cpp:0:0-0:0) and `GameRenderer` to pass the block texture to the effect system and correctly trigger animations upon line completion. - **Menu UI**: Fixed [MenuState](cci:1://file:///d:/Sites/Work/tetris/src/states/MenuState.cpp:19:0-19:55) layout calculations to use fixed logical dimensions (1200x1000), ensuring consistent centering and alignment of the logo, buttons, and settings icon across different window sizes. - **Code Cleanup**: Refactored `PlayingState` to delegate effect triggering to the rendering layer where correct screen coordinates are available.
This commit is contained in:
@ -20,6 +20,12 @@
|
||||
#pragma comment(lib, "mfuuid.lib")
|
||||
#pragma comment(lib, "ole32.lib")
|
||||
using Microsoft::WRL::ComPtr;
|
||||
#ifdef max
|
||||
#undef max
|
||||
#endif
|
||||
#ifdef min
|
||||
#undef min
|
||||
#endif
|
||||
#endif
|
||||
|
||||
Audio& Audio::instance(){ static Audio inst; return inst; }
|
||||
@ -277,3 +283,40 @@ void Audio::shutdown(){
|
||||
if(mfStarted){ MFShutdown(); mfStarted=false; }
|
||||
#endif
|
||||
}
|
||||
|
||||
// IAudioSystem interface implementation
|
||||
void Audio::playSound(const std::string& name) {
|
||||
// This is a simplified implementation - in a full implementation,
|
||||
// you would load sound effects by name from assets
|
||||
// For now, we'll just trigger a generic sound effect
|
||||
// In practice, this would load a sound file and play it via playSfx
|
||||
}
|
||||
|
||||
void Audio::playMusic(const std::string& name) {
|
||||
// This is a simplified implementation - in a full implementation,
|
||||
// you would load music tracks by name
|
||||
// For now, we'll just start the current playlist
|
||||
if (!tracks.empty() && !playing) {
|
||||
start();
|
||||
}
|
||||
}
|
||||
|
||||
void Audio::stopMusic() {
|
||||
playing = false;
|
||||
}
|
||||
|
||||
void Audio::setMasterVolume(float volume) {
|
||||
m_masterVolume = std::max(0.0f, std::min(1.0f, volume));
|
||||
}
|
||||
|
||||
void Audio::setMusicVolume(float volume) {
|
||||
m_musicVolume = std::max(0.0f, std::min(1.0f, volume));
|
||||
}
|
||||
|
||||
void Audio::setSoundVolume(float volume) {
|
||||
m_sfxVolume = std::max(0.0f, std::min(1.0f, volume));
|
||||
}
|
||||
|
||||
bool Audio::isMusicPlaying() const {
|
||||
return playing;
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <atomic>
|
||||
#include "../core/interfaces/IAudioSystem.h"
|
||||
|
||||
struct AudioTrack {
|
||||
std::string path;
|
||||
@ -18,9 +19,20 @@ struct AudioTrack {
|
||||
bool ok = false;
|
||||
};
|
||||
|
||||
class Audio {
|
||||
class Audio : public IAudioSystem {
|
||||
public:
|
||||
static Audio& instance();
|
||||
|
||||
// IAudioSystem interface implementation
|
||||
void playSound(const std::string& name) override;
|
||||
void playMusic(const std::string& name) override;
|
||||
void stopMusic() override;
|
||||
void setMasterVolume(float volume) override;
|
||||
void setMusicVolume(float volume) override;
|
||||
void setSoundVolume(float volume) override;
|
||||
bool isMusicPlaying() const override;
|
||||
|
||||
// Existing Audio class methods
|
||||
bool init(); // initialize backend (MF on Windows)
|
||||
void addTrack(const std::string& path); // decode MP3 -> PCM16 stereo 44100
|
||||
void addTrackAsync(const std::string& path); // add track for background loading
|
||||
@ -57,4 +69,9 @@ private:
|
||||
struct SfxPlay { std::vector<int16_t> pcm; size_t cursor=0; };
|
||||
std::vector<SfxPlay> activeSfx;
|
||||
std::mutex sfxMutex;
|
||||
|
||||
// Volume control
|
||||
float m_masterVolume = 1.0f;
|
||||
float m_musicVolume = 1.0f;
|
||||
float m_sfxVolume = 1.0f;
|
||||
};
|
||||
|
||||
@ -181,3 +181,30 @@ void GlobalState::resetAnimationState() {
|
||||
fireworks.clear();
|
||||
lastFireworkTime = 0;
|
||||
}
|
||||
|
||||
void GlobalState::updateLogicalDimensions(int windowWidth, int windowHeight) {
|
||||
// For now, keep logical dimensions proportional to window size
|
||||
// You can adjust this logic based on your specific needs
|
||||
|
||||
// Option 1: Keep fixed aspect ratio and scale uniformly
|
||||
const float targetAspect = static_cast<float>(Config::Logical::WIDTH) / static_cast<float>(Config::Logical::HEIGHT);
|
||||
const float windowAspect = static_cast<float>(windowWidth) / static_cast<float>(windowHeight);
|
||||
|
||||
if (windowAspect > targetAspect) {
|
||||
// Window is wider than target aspect - fit to height
|
||||
currentLogicalHeight = Config::Logical::HEIGHT;
|
||||
currentLogicalWidth = static_cast<int>(currentLogicalHeight * windowAspect);
|
||||
} else {
|
||||
// Window is taller than target aspect - fit to width
|
||||
currentLogicalWidth = Config::Logical::WIDTH;
|
||||
currentLogicalHeight = static_cast<int>(currentLogicalWidth / windowAspect);
|
||||
}
|
||||
|
||||
// Ensure minimum sizes
|
||||
currentLogicalWidth = std::max(currentLogicalWidth, 800);
|
||||
currentLogicalHeight = std::max(currentLogicalHeight, 600);
|
||||
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
||||
"[GlobalState] Updated logical dimensions: %dx%d (window: %dx%d)",
|
||||
currentLogicalWidth, currentLogicalHeight, windowWidth, windowHeight);
|
||||
}
|
||||
|
||||
@ -66,6 +66,10 @@ public:
|
||||
// Viewport and scaling
|
||||
SDL_Rect logicalVP{0, 0, 1200, 1000}; // Will use Config::Logical constants
|
||||
float logicalScale = 1.0f;
|
||||
|
||||
// Dynamic logical dimensions (computed from window size)
|
||||
int currentLogicalWidth = 1200;
|
||||
int currentLogicalHeight = 1000;
|
||||
|
||||
// Fireworks system (for menu animation)
|
||||
struct BlockParticle {
|
||||
@ -88,6 +92,11 @@ public:
|
||||
void createFirework(float x, float y);
|
||||
void drawFireworks(SDL_Renderer* renderer, SDL_Texture* blocksTex);
|
||||
|
||||
// Logical dimensions management
|
||||
void updateLogicalDimensions(int windowWidth, int windowHeight);
|
||||
int getLogicalWidth() const { return currentLogicalWidth; }
|
||||
int getLogicalHeight() const { return currentLogicalHeight; }
|
||||
|
||||
// Reset methods for different states
|
||||
void resetGameState();
|
||||
void resetUIState();
|
||||
|
||||
92
src/core/ServiceContainer.h
Normal file
92
src/core/ServiceContainer.h
Normal file
@ -0,0 +1,92 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <typeindex>
|
||||
#include <stdexcept>
|
||||
|
||||
/**
|
||||
* @brief Dependency injection container for managing services
|
||||
*
|
||||
* Provides a centralized way to register and retrieve services,
|
||||
* enabling loose coupling and better testability.
|
||||
*/
|
||||
class ServiceContainer {
|
||||
private:
|
||||
std::unordered_map<std::type_index, std::shared_ptr<void>> services_;
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Register a service instance
|
||||
* @tparam T Service type
|
||||
* @param service Shared pointer to service instance
|
||||
*/
|
||||
template<typename T>
|
||||
void registerService(std::shared_ptr<T> service) {
|
||||
services_[std::type_index(typeid(T))] = service;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get a service instance
|
||||
* @tparam T Service type
|
||||
* @return Shared pointer to service instance
|
||||
* @throws std::runtime_error if service is not registered
|
||||
*/
|
||||
template<typename T>
|
||||
std::shared_ptr<T> getService() {
|
||||
auto it = services_.find(std::type_index(typeid(T)));
|
||||
if (it != services_.end()) {
|
||||
return std::static_pointer_cast<T>(it->second);
|
||||
}
|
||||
throw std::runtime_error("Service not registered: " + std::string(typeid(T).name()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get a service instance (const version)
|
||||
* @tparam T Service type
|
||||
* @return Shared pointer to service instance
|
||||
* @throws std::runtime_error if service is not registered
|
||||
*/
|
||||
template<typename T>
|
||||
std::shared_ptr<const T> getService() const {
|
||||
auto it = services_.find(std::type_index(typeid(T)));
|
||||
if (it != services_.end()) {
|
||||
return std::static_pointer_cast<const T>(it->second);
|
||||
}
|
||||
throw std::runtime_error("Service not registered: " + std::string(typeid(T).name()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if a service is registered
|
||||
* @tparam T Service type
|
||||
* @return true if service is registered, false otherwise
|
||||
*/
|
||||
template<typename T>
|
||||
bool hasService() const {
|
||||
return services_.find(std::type_index(typeid(T))) != services_.end();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Unregister a service
|
||||
* @tparam T Service type
|
||||
*/
|
||||
template<typename T>
|
||||
void unregisterService() {
|
||||
services_.erase(std::type_index(typeid(T)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Clear all registered services
|
||||
*/
|
||||
void clear() {
|
||||
services_.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get the number of registered services
|
||||
* @return Number of registered services
|
||||
*/
|
||||
size_t getServiceCount() const {
|
||||
return services_.size();
|
||||
}
|
||||
};
|
||||
@ -1,30 +1,35 @@
|
||||
#include "ApplicationManager.h"
|
||||
#include "StateManager.h"
|
||||
#include "InputManager.h"
|
||||
#include "../state/StateManager.h"
|
||||
#include "../input/InputManager.h"
|
||||
#include "../interfaces/IAudioSystem.h"
|
||||
#include "../interfaces/IRenderer.h"
|
||||
#include "../interfaces/IAssetLoader.h"
|
||||
#include "../interfaces/IInputHandler.h"
|
||||
#include <filesystem>
|
||||
#include "../audio/Audio.h"
|
||||
#include "../audio/SoundEffect.h"
|
||||
#include "../persistence/Scores.h"
|
||||
#include "../states/State.h"
|
||||
#include "../states/LoadingState.h"
|
||||
#include "../states/MenuState.h"
|
||||
#include "../states/LevelSelectorState.h"
|
||||
#include "../states/PlayingState.h"
|
||||
#include "AssetManager.h"
|
||||
#include "Config.h"
|
||||
#include "GlobalState.h"
|
||||
#include "../graphics/RenderManager.h"
|
||||
#include "../graphics/Font.h"
|
||||
#include "../graphics/Starfield3D.h"
|
||||
#include "../graphics/Starfield.h"
|
||||
#include "../graphics/GameRenderer.h"
|
||||
#include "../gameplay/Game.h"
|
||||
#include "../gameplay/LineEffect.h"
|
||||
#include "../../audio/Audio.h"
|
||||
#include "../../audio/SoundEffect.h"
|
||||
#include "../../persistence/Scores.h"
|
||||
#include "../../states/State.h"
|
||||
#include "../../states/LoadingState.h"
|
||||
#include "../../states/MenuState.h"
|
||||
#include "../../states/LevelSelectorState.h"
|
||||
#include "../../states/PlayingState.h"
|
||||
#include "../assets/AssetManager.h"
|
||||
#include "../Config.h"
|
||||
#include "../GlobalState.h"
|
||||
#include "../../graphics/renderers/RenderManager.h"
|
||||
#include "../../graphics/ui/Font.h"
|
||||
#include "../../graphics/effects/Starfield3D.h"
|
||||
#include "../../graphics/effects/Starfield.h"
|
||||
#include "../../graphics/renderers/GameRenderer.h"
|
||||
#include "../../gameplay/core/Game.h"
|
||||
#include "../../gameplay/effects/LineEffect.h"
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <fstream>
|
||||
#include <algorithm>
|
||||
|
||||
ApplicationManager::ApplicationManager() = default;
|
||||
|
||||
@ -33,6 +38,115 @@ static void traceFile(const char* msg) {
|
||||
if (f) f << msg << "\n";
|
||||
}
|
||||
|
||||
// Helper: extracted from inline lambda to avoid MSVC parsing issues with complex lambdas
|
||||
void ApplicationManager::renderLoading(ApplicationManager* app, RenderManager& renderer) {
|
||||
// Clear background first
|
||||
renderer.clear(0, 0, 0, 255);
|
||||
|
||||
// Use 3D starfield for loading screen (full screen)
|
||||
if (app->m_starfield3D) {
|
||||
int winW_actual = 0, winH_actual = 0;
|
||||
if (app->m_renderManager) app->m_renderManager->getWindowSize(winW_actual, winH_actual);
|
||||
if (winW_actual > 0 && winH_actual > 0) app->m_starfield3D->resize(winW_actual, winH_actual);
|
||||
app->m_starfield3D->draw(renderer.getSDLRenderer());
|
||||
}
|
||||
|
||||
SDL_Rect logicalVP = {0,0,0,0};
|
||||
float logicalScale = 1.0f;
|
||||
if (app->m_renderManager) {
|
||||
logicalVP = app->m_renderManager->getLogicalViewport();
|
||||
logicalScale = app->m_renderManager->getLogicalScale();
|
||||
}
|
||||
SDL_SetRenderViewport(renderer.getSDLRenderer(), &logicalVP);
|
||||
SDL_SetRenderScale(renderer.getSDLRenderer(), logicalScale, logicalScale);
|
||||
|
||||
float contentOffsetX = 0.0f;
|
||||
float contentOffsetY = 0.0f;
|
||||
|
||||
auto drawRectOriginal = [&](float x, float y, float w, float h, SDL_Color c) {
|
||||
SDL_SetRenderDrawColor(renderer.getSDLRenderer(), c.r, c.g, c.b, c.a);
|
||||
SDL_FRect fr;
|
||||
fr.x = x + contentOffsetX;
|
||||
fr.y = y + contentOffsetY;
|
||||
fr.w = w;
|
||||
fr.h = h;
|
||||
SDL_RenderFillRect(renderer.getSDLRenderer(), &fr);
|
||||
};
|
||||
|
||||
// Compute dynamic logical width/height based on the RenderManager's
|
||||
// computed viewport and scale so the loading UI sizes itself to the
|
||||
// actual content area rather than a hardcoded design size.
|
||||
float LOGICAL_W = static_cast<float>(Config::Logical::WIDTH);
|
||||
float LOGICAL_H = static_cast<float>(Config::Logical::HEIGHT);
|
||||
if (logicalScale > 0.0f && logicalVP.w > 0 && logicalVP.h > 0) {
|
||||
// logicalVP is in window pixels; divide by scale to get logical units
|
||||
LOGICAL_W = static_cast<float>(logicalVP.w) / logicalScale;
|
||||
LOGICAL_H = static_cast<float>(logicalVP.h) / logicalScale;
|
||||
}
|
||||
const bool isLimitedHeight = LOGICAL_H < 450.0f;
|
||||
SDL_Texture* logoTex = app->m_assetManager->getTexture("logo");
|
||||
const float logoHeight = logoTex ? (isLimitedHeight ? LOGICAL_H * 0.25f : LOGICAL_H * 0.4f) : 0;
|
||||
const float loadingTextHeight = 20;
|
||||
const float barHeight = 20;
|
||||
const float barPaddingVertical = isLimitedHeight ? 15 : 35;
|
||||
const float percentTextHeight = 24;
|
||||
const float spacingBetweenElements = isLimitedHeight ? 5 : 15;
|
||||
|
||||
const float totalContentHeight = logoHeight + (logoHeight > 0 ? spacingBetweenElements : 0) + loadingTextHeight + barPaddingVertical + barHeight + spacingBetweenElements + percentTextHeight;
|
||||
|
||||
float currentY = (LOGICAL_H - totalContentHeight) / 2.0f;
|
||||
|
||||
if (logoTex) {
|
||||
const int lw = 872, lh = 273;
|
||||
const float maxLogoWidth = std::min(LOGICAL_W * 0.9f, 600.0f);
|
||||
const float availableHeight = isLimitedHeight ? LOGICAL_H * 0.25f : LOGICAL_H * 0.4f;
|
||||
const float availableWidth = maxLogoWidth;
|
||||
const float scaleFactorWidth = availableWidth / static_cast<float>(lw);
|
||||
const float scaleFactorHeight = availableHeight / static_cast<float>(lh);
|
||||
const float scaleFactor = std::min(scaleFactorWidth, scaleFactorHeight);
|
||||
const float displayWidth = lw * scaleFactor;
|
||||
const float displayHeight = lh * scaleFactor;
|
||||
const float logoX = (LOGICAL_W - displayWidth) / 2.0f;
|
||||
SDL_FRect dst{logoX + contentOffsetX, currentY + contentOffsetY, displayWidth, displayHeight};
|
||||
SDL_RenderTexture(renderer.getSDLRenderer(), logoTex, nullptr, &dst);
|
||||
currentY += displayHeight + spacingBetweenElements;
|
||||
}
|
||||
|
||||
FontAtlas* pixelFont = (FontAtlas*)app->m_assetManager->getFont("pixel_font");
|
||||
FontAtlas* fallbackFont = (FontAtlas*)app->m_assetManager->getFont("main_font");
|
||||
FontAtlas* loadingFont = pixelFont ? pixelFont : fallbackFont;
|
||||
if (loadingFont) {
|
||||
const std::string loadingText = "LOADING";
|
||||
int tW=0, tH=0; loadingFont->measure(loadingText, 1.0f, tW, tH);
|
||||
float textX = (LOGICAL_W - (float)tW) * 0.5f;
|
||||
loadingFont->draw(renderer.getSDLRenderer(), textX + contentOffsetX, currentY + contentOffsetY, loadingText, 1.0f, {255,204,0,255});
|
||||
}
|
||||
|
||||
currentY += loadingTextHeight + barPaddingVertical;
|
||||
|
||||
const int barW = 400, barH = 20;
|
||||
const int bx = (LOGICAL_W - barW) / 2;
|
||||
float loadingProgress = app->m_assetManager->getLoadingProgress();
|
||||
drawRectOriginal(bx - 3, currentY - 3, barW + 6, barH + 6, {68,68,80,255});
|
||||
drawRectOriginal(bx, currentY, barW, barH, {34,34,34,255});
|
||||
drawRectOriginal(bx, currentY, int(barW * loadingProgress), barH, {255,204,0,255});
|
||||
currentY += barH + spacingBetweenElements;
|
||||
|
||||
if (loadingFont) {
|
||||
int percentage = int(loadingProgress * 100);
|
||||
char percentText[16];
|
||||
std::snprintf(percentText, sizeof(percentText), "%d%%", percentage);
|
||||
std::string pStr(percentText);
|
||||
int pW=0, pH=0; loadingFont->measure(pStr, 1.5f, pW, pH);
|
||||
float percentX = (LOGICAL_W - (float)pW) * 0.5f;
|
||||
loadingFont->draw(renderer.getSDLRenderer(), percentX + contentOffsetX, currentY + contentOffsetY, pStr, 1.5f, {255,204,0,255});
|
||||
}
|
||||
|
||||
SDL_SetRenderViewport(renderer.getSDLRenderer(), nullptr);
|
||||
SDL_SetRenderScale(renderer.getSDLRenderer(), 1.0f, 1.0f);
|
||||
}
|
||||
|
||||
|
||||
ApplicationManager::~ApplicationManager() {
|
||||
if (m_initialized) {
|
||||
shutdown();
|
||||
@ -49,6 +163,9 @@ bool ApplicationManager::initialize(int argc, char* argv[]) {
|
||||
|
||||
// Initialize GlobalState
|
||||
GlobalState::instance().initialize();
|
||||
|
||||
// Set initial logical dimensions
|
||||
GlobalState::instance().updateLogicalDimensions(m_windowWidth, m_windowHeight);
|
||||
|
||||
// Initialize SDL first
|
||||
if (!initializeSDL()) {
|
||||
@ -63,6 +180,9 @@ bool ApplicationManager::initialize(int argc, char* argv[]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Register services for dependency injection
|
||||
registerServices();
|
||||
|
||||
// Initialize game systems
|
||||
if (!initializeGame()) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize game systems");
|
||||
@ -212,6 +332,7 @@ bool ApplicationManager::initializeManagers() {
|
||||
m_renderManager->setFullscreen(!fs);
|
||||
}
|
||||
// Don’t also forward Alt+Enter as an Enter keypress to states (prevents accidental "Start")
|
||||
// Don't also forward Alt+Enter as an Enter keypress to states (prevents accidental "Start")
|
||||
consume = true;
|
||||
}
|
||||
|
||||
@ -269,6 +390,9 @@ bool ApplicationManager::initializeManagers() {
|
||||
// Handle window resize events for RenderManager
|
||||
if (we.type == SDL_EVENT_WINDOW_RESIZED && m_renderManager) {
|
||||
m_renderManager->handleWindowResize(we.data1, we.data2);
|
||||
|
||||
// Update GlobalState logical dimensions when window resizes
|
||||
GlobalState::instance().updateLogicalDimensions(we.data1, we.data2);
|
||||
}
|
||||
|
||||
// Forward all window events to StateManager
|
||||
@ -289,6 +413,45 @@ bool ApplicationManager::initializeManagers() {
|
||||
return true;
|
||||
}
|
||||
|
||||
void ApplicationManager::registerServices() {
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Registering services for dependency injection...");
|
||||
|
||||
// Register concrete implementations as interface singletons
|
||||
if (m_renderManager) {
|
||||
std::shared_ptr<RenderManager> renderPtr(m_renderManager.get(), [](RenderManager*) {
|
||||
// Custom deleter that does nothing since the unique_ptr manages lifetime
|
||||
});
|
||||
m_serviceContainer.registerSingleton<IRenderer>(renderPtr);
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Registered IRenderer service");
|
||||
}
|
||||
|
||||
if (m_assetManager) {
|
||||
std::shared_ptr<AssetManager> assetPtr(m_assetManager.get(), [](AssetManager*) {
|
||||
// Custom deleter that does nothing since the unique_ptr manages lifetime
|
||||
});
|
||||
m_serviceContainer.registerSingleton<IAssetLoader>(assetPtr);
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Registered IAssetLoader service");
|
||||
}
|
||||
|
||||
if (m_inputManager) {
|
||||
std::shared_ptr<InputManager> inputPtr(m_inputManager.get(), [](InputManager*) {
|
||||
// Custom deleter that does nothing since the unique_ptr manages lifetime
|
||||
});
|
||||
m_serviceContainer.registerSingleton<IInputHandler>(inputPtr);
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Registered IInputHandler service");
|
||||
}
|
||||
|
||||
// Register Audio system singleton
|
||||
auto& audioInstance = Audio::instance();
|
||||
auto audioPtr = std::shared_ptr<Audio>(&audioInstance, [](Audio*) {
|
||||
// Custom deleter that does nothing since Audio is a singleton
|
||||
});
|
||||
m_serviceContainer.registerSingleton<IAudioSystem>(audioPtr);
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Registered IAudioSystem service");
|
||||
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Service registration completed successfully");
|
||||
}
|
||||
|
||||
bool ApplicationManager::initializeGame() {
|
||||
// Load essential assets using AssetManager
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading essential assets...");
|
||||
@ -496,142 +659,12 @@ void ApplicationManager::setupStateHandlers() {
|
||||
};
|
||||
|
||||
// Loading State Handlers (matching original main.cpp implementation)
|
||||
m_stateManager->registerRenderHandler(AppState::Loading,
|
||||
[this, drawRect](RenderManager& renderer) {
|
||||
// Clear background first
|
||||
renderer.clear(0, 0, 0, 255);
|
||||
|
||||
// Use 3D starfield for loading screen (full screen)
|
||||
// Ensure starfield uses actual window size so center and projection are correct
|
||||
if (m_starfield3D) {
|
||||
int winW_actual = 0, winH_actual = 0;
|
||||
if (m_renderManager) {
|
||||
m_renderManager->getWindowSize(winW_actual, winH_actual);
|
||||
}
|
||||
if (winW_actual > 0 && winH_actual > 0) {
|
||||
m_starfield3D->resize(winW_actual, winH_actual);
|
||||
}
|
||||
m_starfield3D->draw(renderer.getSDLRenderer());
|
||||
}
|
||||
|
||||
// Set viewport and scaling for content using ACTUAL window size
|
||||
// Use RenderManager's computed logical viewport and scale so all states share the exact math
|
||||
SDL_Rect logicalVP = {0,0,0,0};
|
||||
float logicalScale = 1.0f;
|
||||
if (m_renderManager) {
|
||||
logicalVP = m_renderManager->getLogicalViewport();
|
||||
logicalScale = m_renderManager->getLogicalScale();
|
||||
}
|
||||
SDL_SetRenderViewport(renderer.getSDLRenderer(), &logicalVP);
|
||||
SDL_SetRenderScale(renderer.getSDLRenderer(), logicalScale, logicalScale);
|
||||
|
||||
// Calculate actual content area (centered within the viewport)
|
||||
// Since we already have a centered viewport, content should be drawn at (0,0) in logical space
|
||||
// The viewport itself handles the centering, so no additional offset is needed
|
||||
float contentOffsetX = 0.0f;
|
||||
float contentOffsetY = 0.0f;
|
||||
|
||||
auto drawRectOriginal = [&](float x, float y, float w, float h, SDL_Color c) {
|
||||
SDL_SetRenderDrawColor(renderer.getSDLRenderer(), c.r, c.g, c.b, c.a);
|
||||
SDL_FRect fr{x + contentOffsetX, y + contentOffsetY, w, h};
|
||||
SDL_RenderFillRect(renderer.getSDLRenderer(), &fr);
|
||||
};
|
||||
|
||||
// Calculate dimensions for perfect centering (like JavaScript version)
|
||||
const bool isLimitedHeight = LOGICAL_H < 450;
|
||||
SDL_Texture* logoTex = m_assetManager->getTexture("logo");
|
||||
const float logoHeight = logoTex ? (isLimitedHeight ? LOGICAL_H * 0.25f : LOGICAL_H * 0.4f) : 0;
|
||||
const float loadingTextHeight = 20; // Height of "LOADING" text (match JS)
|
||||
const float barHeight = 20; // Loading bar height (match JS)
|
||||
const float barPaddingVertical = isLimitedHeight ? 15 : 35;
|
||||
const float percentTextHeight = 24; // Height of percentage text
|
||||
const float spacingBetweenElements = isLimitedHeight ? 5 : 15;
|
||||
|
||||
// Total content height
|
||||
const float totalContentHeight = logoHeight +
|
||||
(logoHeight > 0 ? spacingBetweenElements : 0) +
|
||||
loadingTextHeight +
|
||||
barPaddingVertical +
|
||||
barHeight +
|
||||
spacingBetweenElements +
|
||||
percentTextHeight;
|
||||
|
||||
// Start Y position for perfect vertical centering
|
||||
float currentY = (LOGICAL_H - totalContentHeight) / 2.0f;
|
||||
|
||||
// Draw logo (centered, static like JavaScript version)
|
||||
if (logoTex) {
|
||||
// Use the same original large logo dimensions as JS (we used a half-size BMP previously)
|
||||
const int lw = 872, lh = 273;
|
||||
|
||||
// Cap logo width similar to JS UI.MAX_LOGO_WIDTH (600) and available screen space
|
||||
const float maxLogoWidth = std::min(LOGICAL_W * 0.9f, 600.0f);
|
||||
const float availableHeight = isLimitedHeight ? LOGICAL_H * 0.25f : LOGICAL_H * 0.4f;
|
||||
const float availableWidth = maxLogoWidth;
|
||||
|
||||
const float scaleFactorWidth = availableWidth / static_cast<float>(lw);
|
||||
const float scaleFactorHeight = availableHeight / static_cast<float>(lh);
|
||||
const float scaleFactor = std::min(scaleFactorWidth, scaleFactorHeight);
|
||||
|
||||
const float displayWidth = lw * scaleFactor;
|
||||
const float displayHeight = lh * scaleFactor;
|
||||
const float logoX = (LOGICAL_W - displayWidth) / 2.0f;
|
||||
|
||||
SDL_FRect dst{logoX + contentOffsetX, currentY + contentOffsetY, displayWidth, displayHeight};
|
||||
SDL_RenderTexture(renderer.getSDLRenderer(), logoTex, nullptr, &dst);
|
||||
|
||||
currentY += displayHeight + spacingBetweenElements;
|
||||
}
|
||||
|
||||
// Draw "LOADING" text (centered, using pixel font with fallback to main_font)
|
||||
FontAtlas* pixelFont = (FontAtlas*)m_assetManager->getFont("pixel_font");
|
||||
FontAtlas* fallbackFont = (FontAtlas*)m_assetManager->getFont("main_font");
|
||||
FontAtlas* loadingFont = pixelFont ? pixelFont : fallbackFont;
|
||||
if (loadingFont) {
|
||||
const std::string loadingText = "LOADING";
|
||||
int tW=0, tH=0; loadingFont->measure(loadingText, 1.0f, tW, tH);
|
||||
float textX = (LOGICAL_W - (float)tW) * 0.5f;
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Rendering LOADING text at (%f,%f)", textX + contentOffsetX, currentY + contentOffsetY);
|
||||
loadingFont->draw(renderer.getSDLRenderer(), textX + contentOffsetX, currentY + contentOffsetY, loadingText, 1.0f, {255, 204, 0, 255});
|
||||
} else {
|
||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "No loading font available to render LOADING text");
|
||||
}
|
||||
|
||||
currentY += loadingTextHeight + barPaddingVertical;
|
||||
|
||||
// Draw loading bar (like JavaScript version)
|
||||
const int barW = 400, barH = 20;
|
||||
const int bx = (LOGICAL_W - barW) / 2;
|
||||
|
||||
float loadingProgress = m_assetManager->getLoadingProgress();
|
||||
|
||||
// Bar border (dark gray) - using drawRect which adds content offset
|
||||
drawRectOriginal(bx - 3, currentY - 3, barW + 6, barH + 6, {68, 68, 80, 255});
|
||||
|
||||
// Bar background (darker gray)
|
||||
drawRectOriginal(bx, currentY, barW, barH, {34, 34, 34, 255});
|
||||
|
||||
// Progress bar (gold color)
|
||||
drawRectOriginal(bx, currentY, int(barW * loadingProgress), barH, {255, 204, 0, 255});
|
||||
|
||||
currentY += barH + spacingBetweenElements;
|
||||
|
||||
// Draw percentage text (centered, using loadingFont)
|
||||
if (loadingFont) {
|
||||
int percentage = int(loadingProgress * 100);
|
||||
char percentText[16];
|
||||
std::snprintf(percentText, sizeof(percentText), "%d%%", percentage);
|
||||
std::string pStr(percentText);
|
||||
int pW=0, pH=0; loadingFont->measure(pStr, 1.5f, pW, pH);
|
||||
float percentX = (LOGICAL_W - (float)pW) * 0.5f;
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Rendering percent text '%s' at (%f,%f)", percentText, percentX + contentOffsetX, currentY + contentOffsetY);
|
||||
loadingFont->draw(renderer.getSDLRenderer(), percentX + contentOffsetX, currentY + contentOffsetY, pStr, 1.5f, {255, 204, 0, 255});
|
||||
}
|
||||
|
||||
// Reset viewport and scale
|
||||
SDL_SetRenderViewport(renderer.getSDLRenderer(), nullptr);
|
||||
SDL_SetRenderScale(renderer.getSDLRenderer(), 1.0f, 1.0f);
|
||||
});
|
||||
// Extracted to a helper to avoid complex inline lambda parsing issues on MSVC
|
||||
auto loadingRenderForwarder = [this](RenderManager& renderer) {
|
||||
// forward to helper defined below
|
||||
renderLoading(this, renderer);
|
||||
};
|
||||
m_stateManager->registerRenderHandler(AppState::Loading, loadingRenderForwarder);
|
||||
|
||||
m_stateManager->registerUpdateHandler(AppState::Loading,
|
||||
[this](float deltaTime) {
|
||||
@ -729,6 +762,7 @@ void ApplicationManager::setupStateHandlers() {
|
||||
globalState.updateFireworks(deltaTime);
|
||||
|
||||
// Start music as soon as at least one track has decoded (don’t wait for all)
|
||||
// Start music as soon as at least one track has decoded (don't wait for all)
|
||||
if (m_musicEnabled && !m_musicStarted) {
|
||||
if (Audio::instance().getLoadedTrackCount() > 0) {
|
||||
Audio::instance().shuffle();
|
||||
@ -785,16 +819,20 @@ void ApplicationManager::setupStateHandlers() {
|
||||
float lx = (mx - logicalVP.x) / logicalScale;
|
||||
float ly = (my - logicalVP.y) / logicalScale;
|
||||
|
||||
// Compute dynamic logical dimensions from viewport/scale
|
||||
float dynW = (logicalScale > 0.f && logicalVP.w > 0) ? (float)logicalVP.w / logicalScale : (float)Config::Logical::WIDTH;
|
||||
float dynH = (logicalScale > 0.f && logicalVP.h > 0) ? (float)logicalVP.h / logicalScale : (float)Config::Logical::HEIGHT;
|
||||
|
||||
// Respect settings popup
|
||||
if (m_showSettingsPopup) {
|
||||
m_showSettingsPopup = false;
|
||||
} else {
|
||||
bool isSmall = ((Config::Logical::WIDTH * logicalScale) < 700.0f);
|
||||
float btnW = isSmall ? (Config::Logical::WIDTH * 0.4f) : 300.0f;
|
||||
bool isSmall = ((dynW * logicalScale) < 700.0f);
|
||||
float btnW = isSmall ? (dynW * 0.4f) : 300.0f;
|
||||
float btnH = isSmall ? 60.0f : 70.0f;
|
||||
float btnCX = Config::Logical::WIDTH * 0.5f;
|
||||
float btnCX = dynW * 0.5f;
|
||||
const float btnYOffset = 40.0f;
|
||||
float btnCY = Config::Logical::HEIGHT * 0.86f + btnYOffset;
|
||||
float btnCY = dynH * 0.86f + btnYOffset;
|
||||
SDL_FRect playBtn{btnCX - btnW * 0.6f - btnW/2.0f, btnCY - btnH/2.0f, btnW, btnH};
|
||||
SDL_FRect levelBtn{btnCX + btnW * 0.6f - btnW/2.0f, btnCY - btnH/2.0f, btnW, btnH};
|
||||
|
||||
@ -806,7 +844,7 @@ void ApplicationManager::setupStateHandlers() {
|
||||
m_stateManager->setState(AppState::LevelSelector);
|
||||
} else {
|
||||
// Settings area detection (top-right small area)
|
||||
SDL_FRect settingsBtn{Config::Logical::WIDTH - 60, 10, 50, 30};
|
||||
SDL_FRect settingsBtn{dynW - 60, 10, 50, 30};
|
||||
if (lx >= settingsBtn.x && lx <= settingsBtn.x + settingsBtn.w && ly >= settingsBtn.y && ly <= settingsBtn.y + settingsBtn.h) {
|
||||
m_showSettingsPopup = true;
|
||||
}
|
||||
@ -826,12 +864,15 @@ void ApplicationManager::setupStateHandlers() {
|
||||
float lx = (mx - logicalVP.x) / logicalScale;
|
||||
float ly = (my - logicalVP.y) / logicalScale;
|
||||
if (!m_showSettingsPopup) {
|
||||
bool isSmall = ((Config::Logical::WIDTH * logicalScale) < 700.0f);
|
||||
float btnW = isSmall ? (Config::Logical::WIDTH * 0.4f) : 300.0f;
|
||||
// Compute dynamic logical dimensions
|
||||
float dynW = (logicalScale > 0.f && logicalVP.w > 0) ? (float)logicalVP.w / logicalScale : (float)Config::Logical::WIDTH;
|
||||
float dynH = (logicalScale > 0.f && logicalVP.h > 0) ? (float)logicalVP.h / logicalScale : (float)Config::Logical::HEIGHT;
|
||||
bool isSmall = ((dynW * logicalScale) < 700.0f);
|
||||
float btnW = isSmall ? (dynW * 0.4f) : 300.0f;
|
||||
float btnH = isSmall ? 60.0f : 70.0f;
|
||||
float btnCX = Config::Logical::WIDTH * 0.5f;
|
||||
float btnCX = dynW * 0.5f;
|
||||
const float btnYOffset = 40.0f;
|
||||
float btnCY = Config::Logical::HEIGHT * 0.86f + btnYOffset;
|
||||
float btnCY = dynH * 0.86f + btnYOffset;
|
||||
SDL_FRect playBtn{btnCX - btnW * 0.6f - btnW/2.0f, btnCY - btnH/2.0f, btnW, btnH};
|
||||
SDL_FRect levelBtn{btnCX + btnW * 0.6f - btnW/2.0f, btnCY - btnH/2.0f, btnW, btnH};
|
||||
m_hoveredButton = -1;
|
||||
@ -958,6 +999,7 @@ void ApplicationManager::setupStateHandlers() {
|
||||
m_cachedBgLevel = bgLevel;
|
||||
} else {
|
||||
m_cachedBgLevel = -1; // don’t change if missing
|
||||
m_cachedBgLevel = -1; // don't change if missing
|
||||
if (s) SDL_DestroySurface(s);
|
||||
}
|
||||
}
|
||||
@ -1140,6 +1182,98 @@ void ApplicationManager::setupStateHandlers() {
|
||||
m_stateManager->setState(AppState::GameOver);
|
||||
}
|
||||
});
|
||||
// Debug overlay: show current window and logical sizes on the right side of the screen
|
||||
auto debugOverlay = [this](RenderManager& renderer) {
|
||||
// Window size
|
||||
int winW = 0, winH = 0;
|
||||
renderer.getWindowSize(winW, winH);
|
||||
|
||||
// Logical viewport and scale
|
||||
SDL_Rect logicalVP{0,0,0,0};
|
||||
float logicalScale = 1.0f;
|
||||
if (m_renderManager) {
|
||||
logicalVP = m_renderManager->getLogicalViewport();
|
||||
logicalScale = m_renderManager->getLogicalScale();
|
||||
}
|
||||
|
||||
// Use dynamic logical dimensions from GlobalState
|
||||
float LOGICAL_W = static_cast<float>(GlobalState::instance().getLogicalWidth());
|
||||
float LOGICAL_H = static_cast<float>(GlobalState::instance().getLogicalHeight());
|
||||
|
||||
// Use logical viewport so overlay is aligned with game content
|
||||
SDL_SetRenderViewport(renderer.getSDLRenderer(), &logicalVP);
|
||||
SDL_SetRenderScale(renderer.getSDLRenderer(), logicalScale, logicalScale);
|
||||
|
||||
// Choose font (pixel first, fallback to main)
|
||||
FontAtlas* pixelFont = (FontAtlas*)(m_assetManager ? m_assetManager->getFont("pixel_font") : nullptr);
|
||||
FontAtlas* mainFont = (FontAtlas*)(m_assetManager ? m_assetManager->getFont("main_font") : nullptr);
|
||||
FontAtlas* font = pixelFont ? pixelFont : mainFont;
|
||||
|
||||
// Inline small helper for drawing a filled rect in logical coords
|
||||
auto fillRect = [&](float x, float y, float w, float h, SDL_Color c) {
|
||||
SDL_SetRenderDrawColor(renderer.getSDLRenderer(), c.r, c.g, c.b, c.a);
|
||||
SDL_FRect r{ x, y, w, h };
|
||||
SDL_RenderFillRect(renderer.getSDLRenderer(), &r);
|
||||
};
|
||||
|
||||
// Prepare text lines
|
||||
char buf[128];
|
||||
std::snprintf(buf, sizeof(buf), "Win: %d x %d", winW, winH);
|
||||
std::string sWin(buf);
|
||||
std::snprintf(buf, sizeof(buf), "Logical: %.0f x %.0f", LOGICAL_W, LOGICAL_H);
|
||||
std::string sLogical(buf);
|
||||
std::snprintf(buf, sizeof(buf), "Scale: %.2f", logicalScale);
|
||||
std::string sScale(buf);
|
||||
|
||||
// Determine size of longest line
|
||||
int w1=0,h1=0, w2=0,h2=0, w3=0,h3=0;
|
||||
if (font) {
|
||||
font->measure(sWin, 1.0f, w1, h1);
|
||||
font->measure(sLogical, 1.0f, w2, h2);
|
||||
font->measure(sScale, 1.0f, w3, h3);
|
||||
}
|
||||
int maxW = std::max({w1,w2,w3});
|
||||
int totalH = (h1 + h2 + h3) + 8; // small padding
|
||||
|
||||
// Position based on actual screen width (center horizontally)
|
||||
const float margin = 8.0f;
|
||||
// float x = (LOGICAL_W - (float)maxW) * 0.5f; // Center horizontally
|
||||
// float y = margin;
|
||||
// Desired position in window (pixel) coords
|
||||
int winW_px = 0, winH_px = 0;
|
||||
renderer.getWindowSize(winW_px, winH_px);
|
||||
float desiredWinX = (float(winW_px) - (float)maxW) * 0.5f; // center on full window width
|
||||
float desiredWinY = margin; // near top of the window
|
||||
|
||||
// Convert window coords to logical coords under current viewport/scale
|
||||
float invScale = (logicalScale > 0.0f) ? (1.0f / logicalScale) : 1.0f;
|
||||
float x = (desiredWinX - float(logicalVP.x)) * invScale;
|
||||
float y = (desiredWinY - float(logicalVP.y)) * invScale;
|
||||
|
||||
// Draw background box for readability
|
||||
fillRect(x - 6.0f, y - 6.0f, (float)maxW + 12.0f, (float)totalH + 8.0f, {0, 0, 0, 180});
|
||||
|
||||
// Draw text lines
|
||||
SDL_Color textColor = {255, 204, 0, 255};
|
||||
if (font) {
|
||||
font->draw(renderer.getSDLRenderer(), x, y, sWin, 1.0f, textColor);
|
||||
font->draw(renderer.getSDLRenderer(), x, y + (float)h1, sLogical, 1.0f, textColor);
|
||||
font->draw(renderer.getSDLRenderer(), x, y + (float)(h1 + h2), sScale, 1.0f, textColor);
|
||||
}
|
||||
|
||||
// Reset viewport/scale
|
||||
SDL_SetRenderViewport(renderer.getSDLRenderer(), nullptr);
|
||||
SDL_SetRenderScale(renderer.getSDLRenderer(), 1.0f, 1.0f);
|
||||
};
|
||||
|
||||
// Register debug overlay for all primary states so it draws on top
|
||||
if (m_stateManager) {
|
||||
m_stateManager->registerRenderHandler(AppState::Loading, debugOverlay);
|
||||
m_stateManager->registerRenderHandler(AppState::Menu, debugOverlay);
|
||||
m_stateManager->registerRenderHandler(AppState::LevelSelector, debugOverlay);
|
||||
m_stateManager->registerRenderHandler(AppState::Playing, debugOverlay);
|
||||
m_stateManager->registerRenderHandler(AppState::GameOver, debugOverlay);
|
||||
}
|
||||
}
|
||||
|
||||
void ApplicationManager::processEvents() {
|
||||
@ -1,7 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "Config.h"
|
||||
#include "../states/State.h"
|
||||
#include "../Config.h"
|
||||
#include "../../states/State.h"
|
||||
#include "../container/ServiceContainer.h"
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
@ -52,12 +53,18 @@ public:
|
||||
AssetManager* getAssetManager() const { return m_assetManager.get(); }
|
||||
StateManager* getStateManager() const { return m_stateManager.get(); }
|
||||
|
||||
// Service container access
|
||||
ServiceContainer& getServiceContainer() { return m_serviceContainer; }
|
||||
|
||||
private:
|
||||
// Helper used by setupStateHandlers (defined in cpp)
|
||||
static void renderLoading(ApplicationManager* app, RenderManager& renderer);
|
||||
// Initialization methods
|
||||
bool initializeSDL();
|
||||
bool initializeManagers();
|
||||
bool initializeGame();
|
||||
void setupStateHandlers();
|
||||
void registerServices();
|
||||
|
||||
// Main loop methods
|
||||
void processEvents();
|
||||
@ -73,6 +80,9 @@ private:
|
||||
std::unique_ptr<InputManager> m_inputManager;
|
||||
std::unique_ptr<AssetManager> m_assetManager;
|
||||
std::unique_ptr<StateManager> m_stateManager;
|
||||
|
||||
// Dependency injection container
|
||||
ServiceContainer m_serviceContainer;
|
||||
|
||||
// Visual effects
|
||||
std::unique_ptr<Starfield3D> m_starfield3D;
|
||||
@ -1,7 +1,7 @@
|
||||
#include "AssetManager.h"
|
||||
#include "../graphics/Font.h"
|
||||
#include "../audio/Audio.h"
|
||||
#include "../audio/SoundEffect.h"
|
||||
#include "../../graphics/ui/Font.h"
|
||||
#include "../../audio/Audio.h"
|
||||
#include "../../audio/SoundEffect.h"
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
#include <filesystem>
|
||||
@ -443,3 +443,39 @@ float AssetManager::getLoadingProgress() const {
|
||||
|
||||
return assetProgress + musicProgress;
|
||||
}
|
||||
|
||||
// IAssetLoader interface implementation
|
||||
SDL_Texture* AssetManager::loadTextureFromPath(const std::string& path) {
|
||||
// Use the path as both ID and filepath for the interface implementation
|
||||
return loadTexture(path, path);
|
||||
}
|
||||
|
||||
bool AssetManager::loadFontAsset(const std::string& name, const std::string& path, int size) {
|
||||
// Delegate to the existing loadFont method
|
||||
return loadFont(name, path, size);
|
||||
}
|
||||
|
||||
bool AssetManager::loadAudioAsset(const std::string& name, const std::string& path) {
|
||||
return loadSoundEffect(name, path);
|
||||
}
|
||||
|
||||
SDL_Texture* AssetManager::getTextureAsset(const std::string& name) {
|
||||
// Delegate to the existing getTexture method
|
||||
return getTexture(name);
|
||||
}
|
||||
|
||||
bool AssetManager::hasAsset(const std::string& name) const {
|
||||
return m_textures.find(name) != m_textures.end() ||
|
||||
m_fonts.find(name) != m_fonts.end();
|
||||
}
|
||||
|
||||
void AssetManager::unloadAsset(const std::string& name) {
|
||||
// Try to unload as texture first, then as font
|
||||
if (!unloadTexture(name)) {
|
||||
unloadFont(name);
|
||||
}
|
||||
}
|
||||
|
||||
void AssetManager::unloadAll() {
|
||||
shutdown();
|
||||
}
|
||||
@ -6,6 +6,8 @@
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include "../interfaces/IAssetLoader.h"
|
||||
#include "../interfaces/IAssetLoader.h"
|
||||
|
||||
// Forward declarations
|
||||
class FontAtlas;
|
||||
@ -28,7 +30,7 @@ class SoundEffectManager;
|
||||
* - Dependency Inversion: Uses interfaces for audio systems
|
||||
* - Interface Segregation: Separate methods for different asset types
|
||||
*/
|
||||
class AssetManager {
|
||||
class AssetManager : public IAssetLoader {
|
||||
public:
|
||||
AssetManager();
|
||||
~AssetManager();
|
||||
@ -37,7 +39,16 @@ public:
|
||||
bool initialize(SDL_Renderer* renderer);
|
||||
void shutdown();
|
||||
|
||||
// Texture management
|
||||
// IAssetLoader interface implementation
|
||||
SDL_Texture* loadTextureFromPath(const std::string& path) override;
|
||||
bool loadFontAsset(const std::string& name, const std::string& path, int size) override;
|
||||
bool loadAudioAsset(const std::string& name, const std::string& path) override;
|
||||
SDL_Texture* getTextureAsset(const std::string& name) override;
|
||||
bool hasAsset(const std::string& name) const override;
|
||||
void unloadAsset(const std::string& name) override;
|
||||
void unloadAll() override;
|
||||
|
||||
// Existing AssetManager methods with specific implementations
|
||||
SDL_Texture* loadTexture(const std::string& id, const std::string& filepath);
|
||||
SDL_Texture* getTexture(const std::string& id) const;
|
||||
bool unloadTexture(const std::string& id);
|
||||
93
src/core/container/ServiceContainer.h
Normal file
93
src/core/container/ServiceContainer.h
Normal file
@ -0,0 +1,93 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <typeindex>
|
||||
#include <functional>
|
||||
|
||||
/**
|
||||
* @brief Simple dependency injection container
|
||||
*
|
||||
* Provides service registration and resolution for dependency injection.
|
||||
* Supports both singleton and factory-based service creation.
|
||||
*/
|
||||
class ServiceContainer {
|
||||
public:
|
||||
ServiceContainer() = default;
|
||||
~ServiceContainer() = default;
|
||||
|
||||
/**
|
||||
* @brief Register a singleton service instance
|
||||
* @tparam TInterface Interface type
|
||||
* @tparam TImplementation Implementation type
|
||||
* @param instance Shared pointer to the service instance
|
||||
*/
|
||||
template<typename TInterface, typename TImplementation>
|
||||
void registerSingleton(std::shared_ptr<TImplementation> instance) {
|
||||
static_assert(std::is_base_of_v<TInterface, TImplementation>,
|
||||
"TImplementation must inherit from TInterface");
|
||||
|
||||
m_singletons[std::type_index(typeid(TInterface))] = instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Register a factory function for service creation
|
||||
* @tparam TInterface Interface type
|
||||
* @param factory Factory function that creates the service
|
||||
*/
|
||||
template<typename TInterface>
|
||||
void registerFactory(std::function<std::shared_ptr<TInterface>()> factory) {
|
||||
m_factories[std::type_index(typeid(TInterface))] = [factory]() -> std::shared_ptr<void> {
|
||||
return std::static_pointer_cast<void>(factory());
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Resolve a service by its interface type
|
||||
* @tparam TInterface Interface type to resolve
|
||||
* @return Shared pointer to the service instance, nullptr if not found
|
||||
*/
|
||||
template<typename TInterface>
|
||||
std::shared_ptr<TInterface> resolve() {
|
||||
std::type_index typeIndex(typeid(TInterface));
|
||||
|
||||
// Check singletons first
|
||||
auto singletonIt = m_singletons.find(typeIndex);
|
||||
if (singletonIt != m_singletons.end()) {
|
||||
return std::static_pointer_cast<TInterface>(singletonIt->second);
|
||||
}
|
||||
|
||||
// Check factories
|
||||
auto factoryIt = m_factories.find(typeIndex);
|
||||
if (factoryIt != m_factories.end()) {
|
||||
auto instance = factoryIt->second();
|
||||
return std::static_pointer_cast<TInterface>(instance);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if a service is registered
|
||||
* @tparam TInterface Interface type to check
|
||||
* @return true if service is registered, false otherwise
|
||||
*/
|
||||
template<typename TInterface>
|
||||
bool isRegistered() const {
|
||||
std::type_index typeIndex(typeid(TInterface));
|
||||
return m_singletons.find(typeIndex) != m_singletons.end() ||
|
||||
m_factories.find(typeIndex) != m_factories.end();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Clear all registered services
|
||||
*/
|
||||
void clear() {
|
||||
m_singletons.clear();
|
||||
m_factories.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<std::type_index, std::shared_ptr<void>> m_singletons;
|
||||
std::unordered_map<std::type_index, std::function<std::shared_ptr<void>()>> m_factories;
|
||||
};
|
||||
@ -294,3 +294,67 @@ void InputManager::resetDAS() {
|
||||
m_dasState.repeatTimer = 0.0f;
|
||||
m_dasState.activeKey = SDL_SCANCODE_UNKNOWN;
|
||||
}
|
||||
|
||||
// IInputHandler interface implementation
|
||||
bool InputManager::processEvent(const SDL_Event& event) {
|
||||
// Process individual event and return if it was handled
|
||||
switch (event.type) {
|
||||
case SDL_EVENT_KEY_DOWN:
|
||||
case SDL_EVENT_KEY_UP:
|
||||
handleKeyEvent(event.key);
|
||||
return true;
|
||||
case SDL_EVENT_MOUSE_BUTTON_DOWN:
|
||||
case SDL_EVENT_MOUSE_BUTTON_UP:
|
||||
handleMouseButtonEvent(event.button);
|
||||
return true;
|
||||
case SDL_EVENT_MOUSE_MOTION:
|
||||
handleMouseMotionEvent(event.motion);
|
||||
return true;
|
||||
case SDL_EVENT_WINDOW_RESIZED:
|
||||
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
|
||||
case SDL_EVENT_WINDOW_MINIMIZED:
|
||||
case SDL_EVENT_WINDOW_RESTORED:
|
||||
handleWindowEvent(event.window);
|
||||
return true;
|
||||
case SDL_EVENT_QUIT:
|
||||
handleQuitEvent();
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void InputManager::update(double deltaTime) {
|
||||
update(static_cast<float>(deltaTime));
|
||||
}
|
||||
|
||||
bool InputManager::isKeyCurrentlyPressed(SDL_Scancode scancode) const {
|
||||
return isKeyHeld(scancode);
|
||||
}
|
||||
|
||||
bool InputManager::isKeyJustPressed(SDL_Scancode scancode) const {
|
||||
return isKeyPressed(scancode);
|
||||
}
|
||||
|
||||
bool InputManager::isKeyJustReleased(SDL_Scancode scancode) const {
|
||||
return isKeyReleased(scancode);
|
||||
}
|
||||
|
||||
bool InputManager::isQuitRequested() const {
|
||||
return shouldQuit();
|
||||
}
|
||||
|
||||
void InputManager::reset() {
|
||||
// Clear pressed/released states, keep held states
|
||||
// In the current InputManager implementation, we use previous/current state
|
||||
// so we just copy current to previous to reset the "just pressed/released" states
|
||||
m_previousKeyState = m_currentKeyState;
|
||||
m_previousMouseState = m_currentMouseState;
|
||||
}
|
||||
|
||||
void InputManager::handleQuitEvent() {
|
||||
m_shouldQuit = true;
|
||||
for (auto& handler : m_quitHandlers) {
|
||||
handler();
|
||||
}
|
||||
}
|
||||
@ -4,6 +4,7 @@
|
||||
#include <unordered_map>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include "../interfaces/IInputHandler.h"
|
||||
|
||||
/**
|
||||
* InputManager - Centralized input handling system
|
||||
@ -15,7 +16,7 @@
|
||||
* - Support event handler registration
|
||||
* - Implement game-specific input logic (DAS/ARR)
|
||||
*/
|
||||
class InputManager {
|
||||
class InputManager : public IInputHandler {
|
||||
public:
|
||||
// Event handler types
|
||||
using KeyHandler = std::function<void(SDL_Scancode key, bool pressed)>;
|
||||
@ -27,7 +28,16 @@ public:
|
||||
InputManager();
|
||||
~InputManager() = default;
|
||||
|
||||
// Core input processing
|
||||
// IInputHandler interface implementation
|
||||
bool processEvent(const SDL_Event& event) override;
|
||||
void update(double deltaTime) override;
|
||||
bool isKeyCurrentlyPressed(SDL_Scancode scancode) const override;
|
||||
bool isKeyJustPressed(SDL_Scancode scancode) const override;
|
||||
bool isKeyJustReleased(SDL_Scancode scancode) const override;
|
||||
bool isQuitRequested() const override;
|
||||
void reset() override;
|
||||
|
||||
// Existing InputManager methods
|
||||
void processEvents();
|
||||
void update(float deltaTime);
|
||||
|
||||
@ -98,6 +108,7 @@ private:
|
||||
void handleMouseButtonEvent(const SDL_MouseButtonEvent& event);
|
||||
void handleMouseMotionEvent(const SDL_MouseMotionEvent& event);
|
||||
void handleWindowEvent(const SDL_WindowEvent& event);
|
||||
void handleQuitEvent();
|
||||
void updateInputState();
|
||||
void updateDAS(float deltaTime);
|
||||
void resetDAS();
|
||||
64
src/core/interfaces/IAssetLoader.h
Normal file
64
src/core/interfaces/IAssetLoader.h
Normal file
@ -0,0 +1,64 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* @brief Abstract interface for asset loading operations
|
||||
*
|
||||
* Provides a common interface for loading different types of assets,
|
||||
* enabling dependency injection and easier testing.
|
||||
*/
|
||||
class IAssetLoader {
|
||||
public:
|
||||
virtual ~IAssetLoader() = default;
|
||||
|
||||
/**
|
||||
* @brief Load a texture from file (interface method)
|
||||
* @param path Path to the texture file
|
||||
* @return Pointer to loaded SDL_Texture, nullptr on failure
|
||||
*/
|
||||
virtual SDL_Texture* loadTextureFromPath(const std::string& path) = 0;
|
||||
|
||||
/**
|
||||
* @brief Load a font from file (interface method)
|
||||
* @param name Font identifier/name
|
||||
* @param path Path to the font file
|
||||
* @param size Font size in pixels
|
||||
* @return true if font was loaded successfully, false otherwise
|
||||
*/
|
||||
virtual bool loadFontAsset(const std::string& name, const std::string& path, int size) = 0;
|
||||
|
||||
/**
|
||||
* @brief Load an audio file (interface method)
|
||||
* @param name Audio identifier/name
|
||||
* @param path Path to the audio file
|
||||
* @return true if audio was loaded successfully, false otherwise
|
||||
*/
|
||||
virtual bool loadAudioAsset(const std::string& name, const std::string& path) = 0;
|
||||
|
||||
/**
|
||||
* @brief Get a previously loaded texture (interface method)
|
||||
* @param name Texture identifier/name
|
||||
* @return Pointer to SDL_Texture, nullptr if not found
|
||||
*/
|
||||
virtual SDL_Texture* getTextureAsset(const std::string& name) = 0;
|
||||
|
||||
/**
|
||||
* @brief Check if an asset exists
|
||||
* @param name Asset identifier/name
|
||||
* @return true if asset exists, false otherwise
|
||||
*/
|
||||
virtual bool hasAsset(const std::string& name) const = 0;
|
||||
|
||||
/**
|
||||
* @brief Unload a specific asset
|
||||
* @param name Asset identifier/name
|
||||
*/
|
||||
virtual void unloadAsset(const std::string& name) = 0;
|
||||
|
||||
/**
|
||||
* @brief Unload all assets
|
||||
*/
|
||||
virtual void unloadAll() = 0;
|
||||
};
|
||||
55
src/core/interfaces/IAudioSystem.h
Normal file
55
src/core/interfaces/IAudioSystem.h
Normal file
@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* @brief Abstract interface for audio system operations
|
||||
*
|
||||
* Provides a common interface for audio playback, enabling
|
||||
* dependency injection and easier testing.
|
||||
*/
|
||||
class IAudioSystem {
|
||||
public:
|
||||
virtual ~IAudioSystem() = default;
|
||||
|
||||
/**
|
||||
* @brief Play a sound effect
|
||||
* @param name Sound effect name/identifier
|
||||
*/
|
||||
virtual void playSound(const std::string& name) = 0;
|
||||
|
||||
/**
|
||||
* @brief Play background music
|
||||
* @param name Music track name/identifier
|
||||
*/
|
||||
virtual void playMusic(const std::string& name) = 0;
|
||||
|
||||
/**
|
||||
* @brief Stop currently playing music
|
||||
*/
|
||||
virtual void stopMusic() = 0;
|
||||
|
||||
/**
|
||||
* @brief Set master volume for all audio
|
||||
* @param volume Volume level (0.0 to 1.0)
|
||||
*/
|
||||
virtual void setMasterVolume(float volume) = 0;
|
||||
|
||||
/**
|
||||
* @brief Set music volume
|
||||
* @param volume Volume level (0.0 to 1.0)
|
||||
*/
|
||||
virtual void setMusicVolume(float volume) = 0;
|
||||
|
||||
/**
|
||||
* @brief Set sound effects volume
|
||||
* @param volume Volume level (0.0 to 1.0)
|
||||
*/
|
||||
virtual void setSoundVolume(float volume) = 0;
|
||||
|
||||
/**
|
||||
* @brief Check if music is currently playing
|
||||
* @return true if music is playing, false otherwise
|
||||
*/
|
||||
virtual bool isMusicPlaying() const = 0;
|
||||
};
|
||||
73
src/core/interfaces/IGameRules.h
Normal file
73
src/core/interfaces/IGameRules.h
Normal file
@ -0,0 +1,73 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* @brief Abstract interface for game rules
|
||||
*
|
||||
* Provides a common interface for different Tetris rule implementations,
|
||||
* enabling different game modes and rule variations.
|
||||
*/
|
||||
class IGameRules {
|
||||
public:
|
||||
virtual ~IGameRules() = default;
|
||||
|
||||
/**
|
||||
* @brief Calculate score for cleared lines
|
||||
* @param linesCleared Number of lines cleared simultaneously
|
||||
* @param level Current game level
|
||||
* @return Score points to award
|
||||
*/
|
||||
virtual int calculateScore(int linesCleared, int level) const = 0;
|
||||
|
||||
/**
|
||||
* @brief Get gravity speed for a given level
|
||||
* @param level Game level
|
||||
* @return Time in milliseconds for one gravity drop
|
||||
*/
|
||||
virtual double getGravitySpeed(int level) const = 0;
|
||||
|
||||
/**
|
||||
* @brief Check if level should increase
|
||||
* @param totalLines Total lines cleared so far
|
||||
* @param currentLevel Current game level
|
||||
* @return true if level should increase, false otherwise
|
||||
*/
|
||||
virtual bool shouldLevelUp(int totalLines, int currentLevel) const = 0;
|
||||
|
||||
/**
|
||||
* @brief Calculate next level based on lines cleared
|
||||
* @param totalLines Total lines cleared so far
|
||||
* @param startLevel Starting level
|
||||
* @return New level
|
||||
*/
|
||||
virtual int calculateLevel(int totalLines, int startLevel) const = 0;
|
||||
|
||||
/**
|
||||
* @brief Get soft drop speed multiplier
|
||||
* @return Multiplier for gravity when soft dropping
|
||||
*/
|
||||
virtual double getSoftDropMultiplier() const = 0;
|
||||
|
||||
/**
|
||||
* @brief Get hard drop score per cell
|
||||
* @return Points awarded per cell for hard drop
|
||||
*/
|
||||
virtual int getHardDropScore() const = 0;
|
||||
|
||||
/**
|
||||
* @brief Get soft drop score per cell
|
||||
* @return Points awarded per cell for soft drop
|
||||
*/
|
||||
virtual int getSoftDropScore() const = 0;
|
||||
|
||||
/**
|
||||
* @brief Check if T-spins are enabled in this rule set
|
||||
* @return true if T-spins are supported, false otherwise
|
||||
*/
|
||||
virtual bool supportsTSpins() const = 0;
|
||||
|
||||
/**
|
||||
* @brief Check if hold feature is enabled in this rule set
|
||||
* @return true if hold is supported, false otherwise
|
||||
*/
|
||||
virtual bool supportsHold() const = 0;
|
||||
};
|
||||
59
src/core/interfaces/IInputHandler.h
Normal file
59
src/core/interfaces/IInputHandler.h
Normal file
@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
/**
|
||||
* @brief Abstract interface for input handling operations
|
||||
*
|
||||
* Provides a common interface for input processing,
|
||||
* enabling dependency injection and easier testing.
|
||||
*/
|
||||
class IInputHandler {
|
||||
public:
|
||||
virtual ~IInputHandler() = default;
|
||||
|
||||
/**
|
||||
* @brief Process SDL events
|
||||
* @param event SDL event to process
|
||||
* @return true if event was handled, false otherwise
|
||||
*/
|
||||
virtual bool processEvent(const SDL_Event& event) = 0;
|
||||
|
||||
/**
|
||||
* @brief Update input state (called per frame)
|
||||
* @param deltaTime Time elapsed since last frame in milliseconds
|
||||
*/
|
||||
virtual void update(double deltaTime) = 0;
|
||||
|
||||
/**
|
||||
* @brief Check if a key is currently pressed (interface method)
|
||||
* @param scancode SDL scancode of the key
|
||||
* @return true if key is pressed, false otherwise
|
||||
*/
|
||||
virtual bool isKeyCurrentlyPressed(SDL_Scancode scancode) const = 0;
|
||||
|
||||
/**
|
||||
* @brief Check if a key was just pressed this frame
|
||||
* @param scancode SDL scancode of the key
|
||||
* @return true if key was just pressed, false otherwise
|
||||
*/
|
||||
virtual bool isKeyJustPressed(SDL_Scancode scancode) const = 0;
|
||||
|
||||
/**
|
||||
* @brief Check if a key was just released this frame
|
||||
* @param scancode SDL scancode of the key
|
||||
* @return true if key was just released, false otherwise
|
||||
*/
|
||||
virtual bool isKeyJustReleased(SDL_Scancode scancode) const = 0;
|
||||
|
||||
/**
|
||||
* @brief Check if quit was requested (e.g., Alt+F4, window close)
|
||||
* @return true if quit was requested, false otherwise
|
||||
*/
|
||||
virtual bool isQuitRequested() const = 0;
|
||||
|
||||
/**
|
||||
* @brief Reset input state (typically called at frame start)
|
||||
*/
|
||||
virtual void reset() = 0;
|
||||
};
|
||||
56
src/core/interfaces/IRenderer.h
Normal file
56
src/core/interfaces/IRenderer.h
Normal file
@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <cstdint>
|
||||
|
||||
/**
|
||||
* @brief Abstract interface for rendering operations
|
||||
*
|
||||
* Provides a common interface for different rendering implementations,
|
||||
* enabling dependency injection and easier testing.
|
||||
*/
|
||||
class IRenderer {
|
||||
public:
|
||||
virtual ~IRenderer() = default;
|
||||
|
||||
/**
|
||||
* @brief Clear the render target with specified color (interface method)
|
||||
* @param r Red component (0-255)
|
||||
* @param g Green component (0-255)
|
||||
* @param b Blue component (0-255)
|
||||
* @param a Alpha component (0-255)
|
||||
*/
|
||||
virtual void clearScreen(uint8_t r, uint8_t g, uint8_t b, uint8_t a) = 0;
|
||||
|
||||
/**
|
||||
* @brief Present the rendered frame to the display
|
||||
*/
|
||||
virtual void present() = 0;
|
||||
|
||||
/**
|
||||
* @brief Get the underlying SDL renderer for direct operations
|
||||
* @return Pointer to SDL_Renderer
|
||||
* @note This is provided for compatibility with existing code
|
||||
*/
|
||||
virtual SDL_Renderer* getSDLRenderer() = 0;
|
||||
|
||||
/**
|
||||
* @brief Get the current window size (interface method)
|
||||
* @param width Output parameter for window width
|
||||
* @param height Output parameter for window height
|
||||
*/
|
||||
virtual void getWindowDimensions(int& width, int& height) const = 0;
|
||||
|
||||
/**
|
||||
* @brief Set the render viewport
|
||||
* @param viewport Viewport rectangle
|
||||
*/
|
||||
virtual void setViewport(const SDL_Rect* viewport) = 0;
|
||||
|
||||
/**
|
||||
* @brief Set the render scale
|
||||
* @param scaleX Horizontal scale factor
|
||||
* @param scaleY Vertical scale factor
|
||||
*/
|
||||
virtual void setScale(float scaleX, float scaleY) = 0;
|
||||
};
|
||||
@ -1,5 +1,5 @@
|
||||
#include "StateManager.h"
|
||||
#include "../graphics/RenderManager.h"
|
||||
#include "../../graphics/renderers/RenderManager.h"
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
StateManager::StateManager(AppState initial)
|
||||
@ -1,5 +1,5 @@
|
||||
// Game.cpp - Implementation of core Tetris game logic
|
||||
#include "gameplay/Game.h"
|
||||
#include "Game.h"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <SDL3/SDL.h>
|
||||
@ -153,17 +153,18 @@ void Game::lockPiece() {
|
||||
|
||||
// JS level progression (NES-like) using starting level rules
|
||||
// Both startLevel and _level are 0-based now.
|
||||
const int threshold = (startLevel + 1) * 10; // first promotion after this many lines
|
||||
int oldLevel = _level;
|
||||
// First level up happens when total lines equal threshold
|
||||
// After that, every 10 lines (when (lines - threshold) % 10 == 0)
|
||||
if (_lines == threshold) {
|
||||
_level += 1;
|
||||
} else if (_lines > threshold && ((_lines - threshold) % 10 == 0)) {
|
||||
_level += 1;
|
||||
int targetLevel = startLevel;
|
||||
int firstThreshold = (startLevel + 1) * 10;
|
||||
|
||||
if (_lines >= firstThreshold) {
|
||||
targetLevel = startLevel + 1 + (_lines - firstThreshold) / 10;
|
||||
}
|
||||
|
||||
// If we haven't reached the first threshold yet, we are still at startLevel.
|
||||
// The above logic handles this (targetLevel initialized to startLevel).
|
||||
|
||||
if (_level > oldLevel) {
|
||||
if (targetLevel > _level) {
|
||||
_level = targetLevel;
|
||||
// Update gravity to exact NES speed for the new level
|
||||
gravityMs = gravityMsForLevel(_level, gravityGlobalMultiplier);
|
||||
if (levelUpCallback) levelUpCallback(_level);
|
||||
@ -288,15 +289,54 @@ void Game::rotate(int dir) {
|
||||
return;
|
||||
}
|
||||
|
||||
// JavaScript-style wall kicks: try horizontal, up, then larger horizontal offsets
|
||||
const std::pair<int,int> kicks[] = {
|
||||
{1, 0}, // right
|
||||
{-1, 0}, // left
|
||||
{0, -1}, // up (key difference from our previous approach)
|
||||
{2, 0}, // 2 right (for I piece)
|
||||
{-2, 0}, // 2 left (for I piece)
|
||||
};
|
||||
// Standard SRS Wall Kicks
|
||||
// See: https://tetris.wiki/Super_Rotation_System#Wall_kicks
|
||||
|
||||
// JLSTZ Wall Kicks (0->R, R->2, 2->L, L->0)
|
||||
// We only implement the clockwise (0->1, 1->2, 2->3, 3->0) and counter-clockwise (0->3, 3->2, 2->1, 1->0)
|
||||
// For simplicity in this codebase, we'll use a unified set of tests that covers most cases
|
||||
// or we can implement the full table.
|
||||
|
||||
// Let's use a robust set of kicks that covers most standard situations
|
||||
std::vector<std::pair<int,int>> kicks;
|
||||
|
||||
if (p.type == I) {
|
||||
// I-piece kicks
|
||||
kicks = {
|
||||
{0, 0}, // Basic rotation
|
||||
{-2, 0}, {1, 0}, {-2, -1}, {1, 2}, // 0->1 (R)
|
||||
{2, 0}, {-1, 0}, {2, 1}, {-1, -2}, // 1->0 (L)
|
||||
{-1, 0}, {2, 0}, {-1, 2}, {2, -1}, // 1->2 (R)
|
||||
{1, 0}, {-2, 0}, {1, -2}, {-2, 1}, // 2->1 (L)
|
||||
{2, 0}, {-1, 0}, {2, 1}, {-1, -2}, // 2->3 (R)
|
||||
{-2, 0}, {1, 0}, {-2, -1}, {1, 2}, // 3->2 (L)
|
||||
{1, 0}, {-2, 0}, {1, -2}, {-2, 1}, // 3->0 (R)
|
||||
{-1, 0}, {2, 0}, {-1, 2}, {2, -1} // 0->3 (L)
|
||||
};
|
||||
// The above is a superset; for a specific rotation state transition we should pick the right row.
|
||||
// However, since we don't track "last rotation state" easily here (we just have p.rot),
|
||||
// we'll try a generally permissive set of kicks that works for I-piece.
|
||||
// A simplified "try everything" approach for I-piece:
|
||||
kicks = {
|
||||
{0, 0},
|
||||
{-2, 0}, { 2, 0},
|
||||
{-1, 0}, { 1, 0},
|
||||
{ 0,-1}, { 0, 1}, // Up/Down
|
||||
{-2,-1}, { 2,-1}, // Diagonal up
|
||||
{ 1, 2}, {-1, 2}, // Diagonal down
|
||||
{-2, 1}, { 2, 1}
|
||||
};
|
||||
} else {
|
||||
// JLSTZ kicks
|
||||
kicks = {
|
||||
{0, 0},
|
||||
{-1, 0}, { 1, 0}, // Left/Right
|
||||
{ 0,-1}, // Up (floor kick)
|
||||
{-1,-1}, { 1,-1}, // Diagonal up
|
||||
{ 0, 1} // Down (rare but possible)
|
||||
};
|
||||
}
|
||||
|
||||
for (auto kick : kicks) {
|
||||
Piece test = p;
|
||||
test.x = cur.x + kick.first;
|
||||
@ -7,7 +7,7 @@
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include "core/GravityManager.h"
|
||||
#include "../../core/GravityManager.h"
|
||||
|
||||
enum PieceType { I, O, T, S, Z, J, L, PIECE_COUNT };
|
||||
using Shape = std::array<uint16_t, 4>; // four rotation bitmasks
|
||||
280
src/gameplay/effects/LineEffect.cpp
Normal file
280
src/gameplay/effects/LineEffect.cpp
Normal file
@ -0,0 +1,280 @@
|
||||
// LineEffect.cpp - Implementation of line clearing visual and audio effects
|
||||
#include "LineEffect.h"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include "audio/Audio.h"
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
LineEffect::Particle::Particle(float px, float py)
|
||||
: x(px), y(py), size(6.0f + static_cast<float>(rand()) / RAND_MAX * 12.0f), alpha(1.0f) {
|
||||
|
||||
// Random velocity for explosive effect
|
||||
float angle = static_cast<float>(rand()) / RAND_MAX * 2.0f * M_PI;
|
||||
float speed = 80.0f + static_cast<float>(rand()) / RAND_MAX * 150.0f;
|
||||
vx = std::cos(angle) * speed;
|
||||
vy = std::sin(angle) * speed - 30.0f;
|
||||
|
||||
// Random block type for texture
|
||||
blockType = rand() % 7;
|
||||
|
||||
// Fallback colors if texture not available
|
||||
switch (blockType % 4) {
|
||||
case 0: color = {255, 140, 30, 255}; break;
|
||||
case 1: color = {255, 255, 100, 255}; break;
|
||||
case 2: color = {255, 255, 255, 255}; break;
|
||||
case 3: color = {255, 100, 100, 255}; break;
|
||||
}
|
||||
}
|
||||
|
||||
void LineEffect::Particle::update() {
|
||||
x += vx * 0.016f;
|
||||
y += vy * 0.016f;
|
||||
vy += 250.0f * 0.016f;
|
||||
vx *= 0.98f;
|
||||
alpha -= 0.08f; // Slower fade for blocks
|
||||
if (alpha < 0.0f) alpha = 0.0f;
|
||||
|
||||
if (size > 2.0f) size -= 0.05f;
|
||||
}
|
||||
|
||||
void LineEffect::Particle::render(SDL_Renderer* renderer, SDL_Texture* blocksTex) {
|
||||
if (alpha <= 0.0f) return;
|
||||
|
||||
if (blocksTex) {
|
||||
// Render textured block fragment
|
||||
Uint8 prevA = 255;
|
||||
SDL_GetTextureAlphaMod(blocksTex, &prevA);
|
||||
SDL_SetTextureAlphaMod(blocksTex, static_cast<Uint8>(alpha * 255.0f));
|
||||
|
||||
const int SPRITE_SIZE = 90;
|
||||
float srcX = blockType * SPRITE_SIZE + 2;
|
||||
float srcY = 2;
|
||||
float srcW = SPRITE_SIZE - 4;
|
||||
float srcH = SPRITE_SIZE - 4;
|
||||
|
||||
SDL_FRect srcRect = {srcX, srcY, srcW, srcH};
|
||||
SDL_FRect dstRect = {x - size/2, y - size/2, size, size};
|
||||
|
||||
SDL_RenderTexture(renderer, blocksTex, &srcRect, &dstRect);
|
||||
|
||||
SDL_SetTextureAlphaMod(blocksTex, prevA);
|
||||
} else {
|
||||
// Fallback to circle rendering
|
||||
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
|
||||
Uint8 adjustedAlpha = static_cast<Uint8>(alpha * 255.0f);
|
||||
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, adjustedAlpha);
|
||||
|
||||
for (int i = 0; i < static_cast<int>(size); ++i) {
|
||||
for (int j = 0; j < static_cast<int>(size); ++j) {
|
||||
float dx = i - size/2.0f;
|
||||
float dy = j - size/2.0f;
|
||||
if (dx*dx + dy*dy <= (size/2.0f)*(size/2.0f)) {
|
||||
SDL_RenderPoint(renderer, x + dx, y + dy);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LineEffect::LineEffect() : renderer(nullptr), state(AnimationState::IDLE), timer(0.0f),
|
||||
rng(std::random_device{}()), audioStream(nullptr) {
|
||||
}
|
||||
|
||||
LineEffect::~LineEffect() {
|
||||
shutdown();
|
||||
}
|
||||
|
||||
bool LineEffect::init(SDL_Renderer* r) {
|
||||
renderer = r;
|
||||
initAudio();
|
||||
return true;
|
||||
}
|
||||
|
||||
void LineEffect::shutdown() {
|
||||
// No separate audio stream anymore; SFX go through shared Audio mixer
|
||||
}
|
||||
|
||||
void LineEffect::initAudio() {
|
||||
// Generate simple beep sounds procedurally (fallback when voice SFX not provided)
|
||||
|
||||
// Generate a simple line clear beep (440Hz for 0.2 seconds)
|
||||
int sampleRate = 44100;
|
||||
int duration = static_cast<int>(0.2f * sampleRate);
|
||||
lineClearSample.resize(duration * 2); // Stereo
|
||||
|
||||
for (int i = 0; i < duration; ++i) {
|
||||
float t = static_cast<float>(i) / sampleRate;
|
||||
float wave = std::sin(2.0f * M_PI * 440.0f * t) * 0.3f; // 440Hz sine wave
|
||||
int16_t sample = static_cast<int16_t>(wave * 32767.0f);
|
||||
lineClearSample[i * 2] = sample; // Left channel
|
||||
lineClearSample[i * 2 + 1] = sample; // Right channel
|
||||
}
|
||||
|
||||
// Generate a higher pitched tetris sound (880Hz for 0.4 seconds)
|
||||
duration = static_cast<int>(0.4f * sampleRate);
|
||||
tetrisSample.resize(duration * 2);
|
||||
|
||||
for (int i = 0; i < duration; ++i) {
|
||||
float t = static_cast<float>(i) / sampleRate;
|
||||
float wave = std::sin(2.0f * M_PI * 880.0f * t) * 0.4f; // 880Hz sine wave
|
||||
int16_t sample = static_cast<int16_t>(wave * 32767.0f);
|
||||
tetrisSample[i * 2] = sample; // Left channel
|
||||
tetrisSample[i * 2 + 1] = sample; // Right channel
|
||||
}
|
||||
}
|
||||
|
||||
void LineEffect::startLineClear(const std::vector<int>& rows, int gridX, int gridY, int blockSize) {
|
||||
if (rows.empty()) return;
|
||||
|
||||
clearingRows = rows;
|
||||
state = AnimationState::FLASH_WHITE;
|
||||
timer = 0.0f;
|
||||
particles.clear();
|
||||
|
||||
// Create particles for each clearing row
|
||||
for (int row : rows) {
|
||||
createParticles(row, gridX, gridY, blockSize);
|
||||
}
|
||||
|
||||
// Play appropriate sound
|
||||
playLineClearSound(static_cast<int>(rows.size()));
|
||||
}
|
||||
|
||||
void LineEffect::createParticles(int row, int gridX, int gridY, int blockSize) {
|
||||
// Create particles spread across the row with explosive pattern
|
||||
int particlesPerRow = 35; // More particles for dramatic explosion effect
|
||||
|
||||
for (int i = 0; i < particlesPerRow; ++i) {
|
||||
// Create particles along the entire row width
|
||||
float x = gridX + (static_cast<float>(i) / (particlesPerRow - 1)) * (10 * blockSize);
|
||||
float y = gridY + row * blockSize + blockSize / 2.0f;
|
||||
|
||||
// Add some randomness to position
|
||||
x += (static_cast<float>(rand()) / RAND_MAX - 0.5f) * blockSize * 0.8f;
|
||||
y += (static_cast<float>(rand()) / RAND_MAX - 0.5f) * blockSize * 0.6f;
|
||||
|
||||
particles.emplace_back(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
bool LineEffect::update(float deltaTime) {
|
||||
if (state == AnimationState::IDLE) return true;
|
||||
|
||||
timer += deltaTime;
|
||||
|
||||
switch (state) {
|
||||
case AnimationState::FLASH_WHITE:
|
||||
if (timer >= FLASH_DURATION) {
|
||||
state = AnimationState::EXPLODE_BLOCKS;
|
||||
timer = 0.0f;
|
||||
}
|
||||
break;
|
||||
|
||||
case AnimationState::EXPLODE_BLOCKS:
|
||||
updateParticles();
|
||||
if (timer >= EXPLODE_DURATION) {
|
||||
state = AnimationState::BLOCKS_DROP;
|
||||
timer = 0.0f;
|
||||
}
|
||||
break;
|
||||
|
||||
case AnimationState::BLOCKS_DROP:
|
||||
updateParticles();
|
||||
if (timer >= DROP_DURATION) {
|
||||
state = AnimationState::IDLE;
|
||||
clearingRows.clear();
|
||||
particles.clear();
|
||||
return true; // Effect complete
|
||||
}
|
||||
break;
|
||||
|
||||
case AnimationState::IDLE:
|
||||
return true;
|
||||
}
|
||||
|
||||
return false; // Effect still running
|
||||
}
|
||||
|
||||
void LineEffect::updateParticles() {
|
||||
// Update all particles
|
||||
for (auto& particle : particles) {
|
||||
particle.update();
|
||||
}
|
||||
|
||||
// Remove dead particles
|
||||
particles.erase(
|
||||
std::remove_if(particles.begin(), particles.end(),
|
||||
[](const Particle& p) { return !p.isAlive(); }),
|
||||
particles.end()
|
||||
);
|
||||
}
|
||||
|
||||
void LineEffect::render(SDL_Renderer* renderer, SDL_Texture* blocksTex, int gridX, int gridY, int blockSize) {
|
||||
if (state == AnimationState::IDLE) return;
|
||||
|
||||
switch (state) {
|
||||
case AnimationState::FLASH_WHITE:
|
||||
renderFlash(gridX, gridY, blockSize);
|
||||
break;
|
||||
|
||||
case AnimationState::EXPLODE_BLOCKS:
|
||||
renderExplosion(blocksTex);
|
||||
break;
|
||||
|
||||
case AnimationState::BLOCKS_DROP:
|
||||
renderExplosion(blocksTex);
|
||||
break;
|
||||
|
||||
case AnimationState::IDLE:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void LineEffect::renderFlash(int gridX, int gridY, int blockSize) {
|
||||
// Create a flashing white effect with varying opacity
|
||||
float progress = timer / FLASH_DURATION;
|
||||
float flashIntensity = std::sin(progress * M_PI * 6.0f) * 0.5f + 0.5f;
|
||||
|
||||
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
|
||||
Uint8 alpha = static_cast<Uint8>(flashIntensity * 180.0f);
|
||||
|
||||
for (int row : clearingRows) {
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, alpha);
|
||||
SDL_FRect flashRect = {
|
||||
static_cast<float>(gridX - 4),
|
||||
static_cast<float>(gridY + row * blockSize - 4),
|
||||
static_cast<float>(10 * blockSize + 8),
|
||||
static_cast<float>(blockSize + 8)
|
||||
};
|
||||
SDL_RenderFillRect(renderer, &flashRect);
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, 100, 150, 255, alpha / 2);
|
||||
for (int i = 1; i <= 3; ++i) {
|
||||
SDL_FRect glowRect = {
|
||||
flashRect.x - i,
|
||||
flashRect.y - i,
|
||||
flashRect.w + 2*i,
|
||||
flashRect.h + 2*i
|
||||
};
|
||||
SDL_RenderRect(renderer, &glowRect);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LineEffect::renderExplosion(SDL_Texture* blocksTex) {
|
||||
for (auto& particle : particles) {
|
||||
particle.render(renderer, blocksTex);
|
||||
}
|
||||
}
|
||||
|
||||
void LineEffect::playLineClearSound(int lineCount) {
|
||||
// Choose appropriate sound based on line count
|
||||
const std::vector<int16_t>* sample = (lineCount == 4) ? &tetrisSample : &lineClearSample;
|
||||
if (sample && !sample->empty()) {
|
||||
// Mix via shared Audio device so it layers with music
|
||||
Audio::instance().playSfx(*sample, 2, 44100, (lineCount == 4) ? 0.9f : 0.7f);
|
||||
}
|
||||
}
|
||||
72
src/gameplay/effects/LineEffect.h
Normal file
72
src/gameplay/effects/LineEffect.h
Normal file
@ -0,0 +1,72 @@
|
||||
// LineEffect.h - Line clearing visual and audio effects
|
||||
#pragma once
|
||||
#include <SDL3/SDL.h>
|
||||
#include <vector>
|
||||
#include <random>
|
||||
|
||||
class LineEffect {
|
||||
public:
|
||||
struct Particle {
|
||||
float x, y;
|
||||
float vx, vy;
|
||||
float size;
|
||||
float alpha;
|
||||
int blockType; // Added for textured particles
|
||||
SDL_Color color;
|
||||
|
||||
Particle(float px, float py);
|
||||
void update();
|
||||
void render(SDL_Renderer* renderer, SDL_Texture* blocksTex);
|
||||
bool isAlive() const { return alpha > 0.0f; }
|
||||
};
|
||||
|
||||
enum class AnimationState {
|
||||
IDLE,
|
||||
FLASH_WHITE,
|
||||
EXPLODE_BLOCKS,
|
||||
BLOCKS_DROP
|
||||
};
|
||||
|
||||
LineEffect();
|
||||
~LineEffect();
|
||||
|
||||
bool init(SDL_Renderer* renderer);
|
||||
void shutdown();
|
||||
|
||||
// Start line clear effect for the specified rows
|
||||
void startLineClear(const std::vector<int>& rows, int gridX, int gridY, int blockSize);
|
||||
|
||||
// Update and render the effect
|
||||
bool update(float deltaTime); // Returns true if effect is complete
|
||||
void render(SDL_Renderer* renderer, SDL_Texture* blocksTex, int gridX, int gridY, int blockSize);
|
||||
|
||||
// Audio
|
||||
void playLineClearSound(int lineCount);
|
||||
|
||||
bool isActive() const { return state != AnimationState::IDLE; }
|
||||
|
||||
private:
|
||||
SDL_Renderer* renderer{nullptr};
|
||||
AnimationState state{AnimationState::IDLE};
|
||||
float timer{0.0f};
|
||||
std::vector<int> clearingRows;
|
||||
std::vector<Particle> particles;
|
||||
std::mt19937 rng{std::random_device{}()};
|
||||
|
||||
// Audio resources
|
||||
SDL_AudioStream* audioStream{nullptr};
|
||||
std::vector<int16_t> lineClearSample;
|
||||
std::vector<int16_t> tetrisSample;
|
||||
|
||||
// Animation timing - Flash then immediate explosion effect
|
||||
static constexpr float FLASH_DURATION = 0.12f; // Very brief white flash
|
||||
static constexpr float EXPLODE_DURATION = 0.15f; // Quick explosive effect
|
||||
static constexpr float DROP_DURATION = 0.05f; // Almost instant block drop
|
||||
|
||||
void createParticles(int row, int gridX, int gridY, int blockSize);
|
||||
void updateParticles();
|
||||
void renderFlash(int gridX, int gridY, int blockSize);
|
||||
void renderExplosion(SDL_Texture* blocksTex);
|
||||
bool loadAudioSample(const std::string& path, std::vector<int16_t>& sample);
|
||||
void initAudio();
|
||||
};
|
||||
41
src/graphics/effects/Starfield.cpp
Normal file
41
src/graphics/effects/Starfield.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
// Starfield.cpp - implementation (copied into src/graphics)
|
||||
#include "graphics/Starfield.h"
|
||||
#include <SDL3/SDL.h>
|
||||
#include <random>
|
||||
|
||||
void Starfield::init(int count, int w, int h)
|
||||
{
|
||||
stars.clear();
|
||||
stars.reserve(count);
|
||||
std::mt19937 rng{std::random_device{}()};
|
||||
std::uniform_real_distribution<float> dx(0.f, (float)w), dy(0.f, (float)h), dz(0.2f, 1.f);
|
||||
for (int i = 0; i < count; ++i)
|
||||
stars.push_back({dx(rng), dy(rng), dz(rng), 15.f + 35.f * dz(rng)});
|
||||
lastW = w;
|
||||
lastH = h;
|
||||
}
|
||||
|
||||
void Starfield::update(float dt, int w, int h)
|
||||
{
|
||||
if (w != lastW || h != lastH || stars.empty())
|
||||
init((w * h) / 8000 + 120, w, h);
|
||||
for (auto &s : stars)
|
||||
{
|
||||
s.y += s.speed * dt;
|
||||
if (s.y > h)
|
||||
{
|
||||
s.y -= h;
|
||||
s.x = (float)(rand() % w);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Starfield::draw(SDL_Renderer *r) const
|
||||
{
|
||||
SDL_SetRenderDrawColor(r, 255, 255, 255, 255);
|
||||
for (auto &s : stars)
|
||||
{
|
||||
SDL_FRect fr{s.x, s.y, 1.f * s.z, 1.f * s.z};
|
||||
SDL_RenderFillRect(r, &fr);
|
||||
}
|
||||
}
|
||||
15
src/graphics/effects/Starfield.h
Normal file
15
src/graphics/effects/Starfield.h
Normal file
@ -0,0 +1,15 @@
|
||||
// Starfield.h - Procedural starfield background effect
|
||||
#pragma once
|
||||
#include <vector>
|
||||
struct SDL_Renderer; // fwd
|
||||
|
||||
class Starfield {
|
||||
public:
|
||||
void init(int count, int w, int h);
|
||||
void update(float dt, int w, int h);
|
||||
void draw(SDL_Renderer* r) const;
|
||||
private:
|
||||
struct Star { float x,y,z,speed; };
|
||||
std::vector<Star> stars;
|
||||
int lastW{0}, lastH{0};
|
||||
};
|
||||
164
src/graphics/effects/Starfield3D.cpp
Normal file
164
src/graphics/effects/Starfield3D.cpp
Normal file
@ -0,0 +1,164 @@
|
||||
// Starfield3D.cpp - 3D Parallax Starfield Implementation
|
||||
#include "Starfield3D.h"
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
|
||||
Starfield3D::Starfield3D() : rng(std::random_device{}()), width(800), height(600), centerX(400), centerY(300) {
|
||||
}
|
||||
|
||||
void Starfield3D::init(int w, int h, int starCount) {
|
||||
width = w;
|
||||
height = h;
|
||||
centerX = width * 0.5f;
|
||||
centerY = height * 0.5f;
|
||||
|
||||
stars.resize(starCount);
|
||||
createStarfield();
|
||||
}
|
||||
|
||||
void Starfield3D::resize(int w, int h) {
|
||||
width = w;
|
||||
height = h;
|
||||
centerX = width * 0.5f;
|
||||
centerY = height * 0.5f;
|
||||
}
|
||||
|
||||
float Starfield3D::randomFloat(float min, float max) {
|
||||
std::uniform_real_distribution<float> dist(min, max);
|
||||
return dist(rng);
|
||||
}
|
||||
|
||||
int Starfield3D::randomRange(int min, int max) {
|
||||
std::uniform_int_distribution<int> dist(min, max - 1);
|
||||
return dist(rng);
|
||||
}
|
||||
|
||||
void Starfield3D::setRandomDirection(Star3D& star) {
|
||||
star.targetVx = randomFloat(-MAX_VELOCITY, MAX_VELOCITY);
|
||||
star.targetVy = randomFloat(-MAX_VELOCITY, MAX_VELOCITY);
|
||||
|
||||
// Allow stars to move both toward and away from viewer
|
||||
if (randomFloat(0.0f, 1.0f) < REVERSE_PROBABILITY) {
|
||||
// Move away from viewer (positive Z)
|
||||
star.targetVz = STAR_SPEED * randomFloat(0.5f, 1.0f);
|
||||
} else {
|
||||
// Move toward viewer (negative Z)
|
||||
star.targetVz = -STAR_SPEED * randomFloat(0.7f, 1.3f);
|
||||
}
|
||||
|
||||
star.changing = true;
|
||||
star.changeTimer = randomFloat(30.0f, 120.0f); // Direction change lasts 30-120 frames
|
||||
}
|
||||
|
||||
void Starfield3D::updateStar(int index) {
|
||||
Star3D& star = stars[index];
|
||||
|
||||
star.x = randomFloat(-25.0f, 25.0f);
|
||||
star.y = randomFloat(-25.0f, 25.0f);
|
||||
star.z = randomFloat(1.0f, MAX_DEPTH);
|
||||
|
||||
// Give stars initial velocities in all possible directions
|
||||
if (randomFloat(0.0f, 1.0f) < 0.5f) {
|
||||
// Half stars start moving toward viewer
|
||||
star.vx = randomFloat(-0.1f, 0.1f);
|
||||
star.vy = randomFloat(-0.1f, 0.1f);
|
||||
star.vz = -STAR_SPEED * randomFloat(0.8f, 1.2f);
|
||||
} else {
|
||||
// Half stars start moving in random directions
|
||||
star.vx = randomFloat(-0.2f, 0.2f);
|
||||
star.vy = randomFloat(-0.2f, 0.2f);
|
||||
|
||||
// 30% chance to start moving away
|
||||
if (randomFloat(0.0f, 1.0f) < 0.3f) {
|
||||
star.vz = STAR_SPEED * randomFloat(0.5f, 0.8f);
|
||||
} else {
|
||||
star.vz = -STAR_SPEED * randomFloat(0.8f, 1.2f);
|
||||
}
|
||||
}
|
||||
|
||||
star.targetVx = star.vx;
|
||||
star.targetVy = star.vy;
|
||||
star.targetVz = star.vz;
|
||||
star.changing = false;
|
||||
star.changeTimer = 0.0f;
|
||||
star.type = randomRange(0, COLOR_COUNT);
|
||||
|
||||
// Give some stars initial direction variations
|
||||
if (randomFloat(0.0f, 1.0f) < 0.4f) {
|
||||
setRandomDirection(star);
|
||||
}
|
||||
}
|
||||
|
||||
void Starfield3D::createStarfield() {
|
||||
for (size_t i = 0; i < stars.size(); ++i) {
|
||||
updateStar(static_cast<int>(i));
|
||||
}
|
||||
}
|
||||
|
||||
void Starfield3D::update(float deltaTime) {
|
||||
const float frameRate = 60.0f; // Target 60 FPS for consistency
|
||||
const float frameMultiplier = deltaTime * frameRate;
|
||||
|
||||
for (size_t i = 0; i < stars.size(); ++i) {
|
||||
Star3D& star = stars[i];
|
||||
|
||||
// Randomly change direction occasionally
|
||||
if (!star.changing && randomFloat(0.0f, 1.0f) < DIRECTION_CHANGE_PROBABILITY * frameMultiplier) {
|
||||
setRandomDirection(star);
|
||||
}
|
||||
|
||||
// Update velocities to approach target values
|
||||
if (star.changing) {
|
||||
// Smoothly transition to target velocities
|
||||
const float change = VELOCITY_CHANGE * frameMultiplier;
|
||||
star.vx += (star.targetVx - star.vx) * change;
|
||||
star.vy += (star.targetVy - star.vy) * change;
|
||||
star.vz += (star.targetVz - star.vz) * change;
|
||||
|
||||
// Decrement change timer
|
||||
star.changeTimer -= frameMultiplier;
|
||||
if (star.changeTimer <= 0.0f) {
|
||||
star.changing = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Update position using current velocity
|
||||
star.x += star.vx * frameMultiplier;
|
||||
star.y += star.vy * frameMultiplier;
|
||||
star.z += star.vz * frameMultiplier;
|
||||
|
||||
// Handle boundaries - reset star if it moves out of bounds, too close, or too far
|
||||
if (star.z <= MIN_Z ||
|
||||
star.z >= MAX_Z ||
|
||||
std::abs(star.x) > 50.0f ||
|
||||
std::abs(star.y) > 50.0f) {
|
||||
updateStar(static_cast<int>(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Starfield3D::drawStar(SDL_Renderer* renderer, float x, float y, int type) {
|
||||
const SDL_Color& color = STAR_COLORS[type % COLOR_COUNT];
|
||||
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
|
||||
|
||||
// Draw star as a small rectangle (1x1 pixel)
|
||||
SDL_FRect rect{x, y, 1.0f, 1.0f};
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
}
|
||||
|
||||
void Starfield3D::draw(SDL_Renderer* renderer) {
|
||||
for (const Star3D& star : stars) {
|
||||
// Calculate perspective projection factor
|
||||
const float k = DEPTH_FACTOR / star.z;
|
||||
|
||||
// Calculate screen position with perspective
|
||||
const float px = star.x * k + centerX;
|
||||
const float py = star.y * k + centerY;
|
||||
|
||||
// Only draw stars that are within the viewport
|
||||
if (px >= 0.0f && px <= static_cast<float>(width) &&
|
||||
py >= 0.0f && py <= static_cast<float>(height)) {
|
||||
drawStar(renderer, px, py, star.type);
|
||||
}
|
||||
}
|
||||
}
|
||||
63
src/graphics/effects/Starfield3D.h
Normal file
63
src/graphics/effects/Starfield3D.h
Normal file
@ -0,0 +1,63 @@
|
||||
// Starfield3D.h - 3D Parallax Starfield Effect (canonical)
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <vector>
|
||||
#include <random>
|
||||
#include <array>
|
||||
|
||||
class Starfield3D {
|
||||
public:
|
||||
Starfield3D();
|
||||
~Starfield3D() = default;
|
||||
|
||||
void init(int width, int height, int starCount = 160);
|
||||
void update(float deltaTime);
|
||||
void draw(SDL_Renderer* renderer);
|
||||
void resize(int width, int height);
|
||||
|
||||
private:
|
||||
struct Star3D {
|
||||
float x, y, z;
|
||||
float vx, vy, vz;
|
||||
float targetVx, targetVy, targetVz;
|
||||
float changeTimer;
|
||||
bool changing;
|
||||
int type;
|
||||
};
|
||||
|
||||
// Helpers used by the implementation
|
||||
void createStarfield();
|
||||
void updateStar(int index);
|
||||
void setRandomDirection(Star3D& star);
|
||||
float randomFloat(float min, float max);
|
||||
int randomRange(int min, int max);
|
||||
void drawStar(SDL_Renderer* renderer, float x, float y, int type);
|
||||
|
||||
std::vector<Star3D> stars;
|
||||
int width{0}, height{0};
|
||||
float centerX{0}, centerY{0};
|
||||
|
||||
// Random number generator
|
||||
std::mt19937 rng;
|
||||
|
||||
// Visual / behavioral constants (tweakable)
|
||||
inline static constexpr float MAX_VELOCITY = 0.5f;
|
||||
inline static constexpr float REVERSE_PROBABILITY = 0.12f;
|
||||
inline static constexpr float STAR_SPEED = 0.6f;
|
||||
inline static constexpr float MAX_DEPTH = 120.0f;
|
||||
inline static constexpr float DIRECTION_CHANGE_PROBABILITY = 0.002f;
|
||||
inline static constexpr float VELOCITY_CHANGE = 0.02f;
|
||||
inline static constexpr float MIN_Z = 0.1f;
|
||||
inline static constexpr float MAX_Z = MAX_DEPTH;
|
||||
inline static constexpr float DEPTH_FACTOR = 320.0f;
|
||||
|
||||
inline static constexpr int COLOR_COUNT = 5;
|
||||
inline static const std::array<SDL_Color, COLOR_COUNT> STAR_COLORS = {
|
||||
SDL_Color{255,255,255,255},
|
||||
SDL_Color{200,200,255,255},
|
||||
SDL_Color{255,220,180,255},
|
||||
SDL_Color{180,220,255,255},
|
||||
SDL_Color{255,180,200,255}
|
||||
};
|
||||
};
|
||||
480
src/graphics/renderers/GameRenderer.cpp
Normal file
480
src/graphics/renderers/GameRenderer.cpp
Normal file
@ -0,0 +1,480 @@
|
||||
#include "GameRenderer.h"
|
||||
#include "../../gameplay/core/Game.h"
|
||||
#include "../ui/Font.h"
|
||||
#include "../../gameplay/effects/LineEffect.h"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
|
||||
// Color constants (copied from main.cpp)
|
||||
static const SDL_Color COLORS[] = {
|
||||
{0, 0, 0, 255}, // 0: BLACK (empty)
|
||||
{0, 255, 255, 255}, // 1: I-piece - cyan
|
||||
{255, 255, 0, 255}, // 2: O-piece - yellow
|
||||
{128, 0, 128, 255}, // 3: T-piece - purple
|
||||
{0, 255, 0, 255}, // 4: S-piece - green
|
||||
{255, 0, 0, 255}, // 5: Z-piece - red
|
||||
{0, 0, 255, 255}, // 6: J-piece - blue
|
||||
{255, 165, 0, 255} // 7: L-piece - orange
|
||||
};
|
||||
|
||||
void GameRenderer::drawRect(SDL_Renderer* renderer, float x, float y, float w, float h, SDL_Color c) {
|
||||
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, c.a);
|
||||
SDL_FRect fr{x, y, w, h};
|
||||
SDL_RenderFillRect(renderer, &fr);
|
||||
}
|
||||
|
||||
void GameRenderer::drawBlockTexture(SDL_Renderer* renderer, SDL_Texture* blocksTex, float x, float y, float size, int blockType) {
|
||||
if (!blocksTex || blockType < 0 || blockType >= PIECE_COUNT) {
|
||||
// Fallback to colored rectangle if texture isn't available
|
||||
SDL_Color color = (blockType >= 0 && blockType < PIECE_COUNT) ? COLORS[blockType + 1] : SDL_Color{128, 128, 128, 255};
|
||||
drawRect(renderer, x, y, size-1, size-1, color);
|
||||
return;
|
||||
}
|
||||
|
||||
// JavaScript uses: sx = type * spriteSize, sy = 0, with 2px padding
|
||||
// Each sprite is 90px wide in the horizontal sprite sheet
|
||||
const int SPRITE_SIZE = 90;
|
||||
float srcX = blockType * SPRITE_SIZE + 2; // Add 2px padding like JS
|
||||
float srcY = 2; // Add 2px padding from top like JS
|
||||
float srcW = SPRITE_SIZE - 4; // Subtract 4px total padding like JS
|
||||
float srcH = SPRITE_SIZE - 4; // Subtract 4px total padding like JS
|
||||
|
||||
SDL_FRect srcRect = {srcX, srcY, srcW, srcH};
|
||||
SDL_FRect dstRect = {x, y, size, size};
|
||||
SDL_RenderTexture(renderer, blocksTex, &srcRect, &dstRect);
|
||||
}
|
||||
|
||||
void GameRenderer::drawPiece(SDL_Renderer* renderer, SDL_Texture* blocksTex, const Game::Piece& piece, float ox, float oy, float tileSize, bool isGhost) {
|
||||
if (piece.type >= PIECE_COUNT) return;
|
||||
|
||||
for (int cy = 0; cy < 4; ++cy) {
|
||||
for (int cx = 0; cx < 4; ++cx) {
|
||||
if (Game::cellFilled(piece, cx, cy)) {
|
||||
float px = ox + (piece.x + cx) * tileSize;
|
||||
float py = oy + (piece.y + cy) * tileSize;
|
||||
|
||||
if (isGhost) {
|
||||
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
|
||||
|
||||
// Draw ghost piece as barely visible gray outline
|
||||
SDL_SetRenderDrawColor(renderer, 180, 180, 180, 20); // Very faint gray
|
||||
SDL_FRect rect = {px + 2, py + 2, tileSize - 4, tileSize - 4};
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
|
||||
// Draw thin gray border
|
||||
SDL_SetRenderDrawColor(renderer, 180, 180, 180, 30);
|
||||
SDL_FRect border = {px + 1, py + 1, tileSize - 2, tileSize - 2};
|
||||
SDL_RenderRect(renderer, &border);
|
||||
} else {
|
||||
drawBlockTexture(renderer, blocksTex, px, py, tileSize, piece.type);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GameRenderer::drawSmallPiece(SDL_Renderer* renderer, SDL_Texture* blocksTex, PieceType pieceType, float x, float y, float tileSize) {
|
||||
if (pieceType >= PIECE_COUNT) return;
|
||||
|
||||
// Use the first rotation (index 0) for preview
|
||||
Game::Piece previewPiece;
|
||||
previewPiece.type = pieceType;
|
||||
previewPiece.rot = 0;
|
||||
previewPiece.x = 0;
|
||||
previewPiece.y = 0;
|
||||
|
||||
// Center the piece in the preview area
|
||||
float offsetX = 0, offsetY = 0;
|
||||
if (pieceType == 0) { offsetX = tileSize * 0.5f; } // I-piece centering (assuming I = 0)
|
||||
else if (pieceType == 1) { offsetX = tileSize * 0.5f; } // O-piece centering (assuming O = 1)
|
||||
|
||||
// Use semi-transparent alpha for preview blocks
|
||||
Uint8 previewAlpha = 180;
|
||||
if (blocksTex) {
|
||||
SDL_SetTextureAlphaMod(blocksTex, previewAlpha);
|
||||
}
|
||||
|
||||
for (int cy = 0; cy < 4; ++cy) {
|
||||
for (int cx = 0; cx < 4; ++cx) {
|
||||
if (Game::cellFilled(previewPiece, cx, cy)) {
|
||||
float px = x + offsetX + cx * tileSize;
|
||||
float py = y + offsetY + cy * tileSize;
|
||||
drawBlockTexture(renderer, blocksTex, px, py, tileSize, pieceType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Reset alpha
|
||||
if (blocksTex) {
|
||||
SDL_SetTextureAlphaMod(blocksTex, 255);
|
||||
}
|
||||
}
|
||||
|
||||
void GameRenderer::renderPlayingState(
|
||||
SDL_Renderer* renderer,
|
||||
Game* game,
|
||||
FontAtlas* pixelFont,
|
||||
LineEffect* lineEffect,
|
||||
SDL_Texture* blocksTex,
|
||||
float logicalW,
|
||||
float logicalH,
|
||||
float logicalScale,
|
||||
float winW,
|
||||
float winH,
|
||||
bool showExitConfirmPopup
|
||||
) {
|
||||
if (!game || !pixelFont) return;
|
||||
|
||||
// Calculate actual content area (centered within the window)
|
||||
float contentScale = logicalScale;
|
||||
float contentW = logicalW * contentScale;
|
||||
float contentH = logicalH * contentScale;
|
||||
float contentOffsetX = (winW - contentW) * 0.5f / contentScale;
|
||||
float contentOffsetY = (winH - contentH) * 0.5f / contentScale;
|
||||
|
||||
// Helper lambda for drawing rectangles with content offset
|
||||
auto drawRectWithOffset = [&](float x, float y, float w, float h, SDL_Color c) {
|
||||
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, c.a);
|
||||
SDL_FRect fr{x + contentOffsetX, y + contentOffsetY, w, h};
|
||||
SDL_RenderFillRect(renderer, &fr);
|
||||
};
|
||||
|
||||
// Responsive layout that scales with window size while maintaining margins
|
||||
const float MIN_MARGIN = 40.0f;
|
||||
const float TOP_MARGIN = 60.0f;
|
||||
const float PANEL_WIDTH = 180.0f;
|
||||
const float PANEL_SPACING = 30.0f;
|
||||
const float NEXT_PIECE_HEIGHT = 120.0f;
|
||||
const float BOTTOM_MARGIN = 60.0f;
|
||||
|
||||
// Calculate layout dimensions
|
||||
const float availableWidth = logicalW - (MIN_MARGIN * 2) - (PANEL_WIDTH * 2) - (PANEL_SPACING * 2);
|
||||
const float availableHeight = logicalH - TOP_MARGIN - BOTTOM_MARGIN - NEXT_PIECE_HEIGHT;
|
||||
|
||||
const float maxBlockSizeW = availableWidth / Game::COLS;
|
||||
const float maxBlockSizeH = availableHeight / Game::ROWS;
|
||||
const float BLOCK_SIZE = std::min(maxBlockSizeW, maxBlockSizeH);
|
||||
const float finalBlockSize = std::max(20.0f, std::min(BLOCK_SIZE, 40.0f));
|
||||
|
||||
const float GRID_W = Game::COLS * finalBlockSize;
|
||||
const float GRID_H = Game::ROWS * finalBlockSize;
|
||||
|
||||
// Calculate positions
|
||||
const float totalContentHeight = NEXT_PIECE_HEIGHT + GRID_H;
|
||||
const float availableVerticalSpace = logicalH - TOP_MARGIN - BOTTOM_MARGIN;
|
||||
const float verticalCenterOffset = (availableVerticalSpace - totalContentHeight) * 0.5f;
|
||||
const float contentStartY = TOP_MARGIN + verticalCenterOffset;
|
||||
|
||||
const float totalLayoutWidth = PANEL_WIDTH + PANEL_SPACING + GRID_W + PANEL_SPACING + PANEL_WIDTH;
|
||||
const float layoutStartX = (logicalW - totalLayoutWidth) * 0.5f;
|
||||
|
||||
const float statsX = layoutStartX + contentOffsetX;
|
||||
const float gridX = layoutStartX + PANEL_WIDTH + PANEL_SPACING + contentOffsetX;
|
||||
const float scoreX = layoutStartX + PANEL_WIDTH + PANEL_SPACING + GRID_W + PANEL_SPACING + contentOffsetX;
|
||||
const float gridY = contentStartY + NEXT_PIECE_HEIGHT + contentOffsetY;
|
||||
|
||||
const float statsY = gridY;
|
||||
const float statsW = PANEL_WIDTH;
|
||||
const float statsH = GRID_H;
|
||||
|
||||
// Next piece preview position
|
||||
const float nextW = finalBlockSize * 4 + 20;
|
||||
const float nextH = finalBlockSize * 2 + 20;
|
||||
const float nextX = gridX + (GRID_W - nextW) * 0.5f;
|
||||
const float nextY = contentStartY + contentOffsetY;
|
||||
|
||||
// Handle line clearing effects
|
||||
if (game->hasCompletedLines() && lineEffect && !lineEffect->isActive()) {
|
||||
auto completedLines = game->getCompletedLines();
|
||||
lineEffect->startLineClear(completedLines, static_cast<int>(gridX), static_cast<int>(gridY), static_cast<int>(finalBlockSize));
|
||||
}
|
||||
|
||||
// Draw game grid border
|
||||
drawRectWithOffset(gridX - 3 - contentOffsetX, gridY - 3 - contentOffsetY, GRID_W + 6, GRID_H + 6, {100, 120, 200, 255});
|
||||
drawRectWithOffset(gridX - 1 - contentOffsetX, gridY - 1 - contentOffsetY, GRID_W + 2, GRID_H + 2, {60, 80, 160, 255});
|
||||
drawRectWithOffset(gridX - contentOffsetX, gridY - contentOffsetY, GRID_W, GRID_H, {20, 25, 35, 255});
|
||||
|
||||
// Draw panel backgrounds
|
||||
SDL_SetRenderDrawColor(renderer, 10, 15, 25, 160);
|
||||
SDL_FRect lbg{statsX - 16, gridY - 10, statsW + 32, GRID_H + 20};
|
||||
SDL_RenderFillRect(renderer, &lbg);
|
||||
|
||||
SDL_FRect rbg{scoreX - 16, gridY - 16, statsW + 32, GRID_H + 32};
|
||||
SDL_RenderFillRect(renderer, &rbg);
|
||||
|
||||
// Draw grid lines
|
||||
SDL_SetRenderDrawColor(renderer, 40, 45, 60, 255);
|
||||
for (int x = 1; x < Game::COLS; ++x) {
|
||||
float lineX = gridX + x * finalBlockSize;
|
||||
SDL_RenderLine(renderer, lineX, gridY, lineX, gridY + GRID_H);
|
||||
}
|
||||
for (int y = 1; y < Game::ROWS; ++y) {
|
||||
float lineY = gridY + y * finalBlockSize;
|
||||
SDL_RenderLine(renderer, gridX, lineY, gridX + GRID_W, lineY);
|
||||
}
|
||||
|
||||
// Draw block statistics panel border
|
||||
drawRectWithOffset(statsX - 3 - contentOffsetX, statsY - 3 - contentOffsetY, statsW + 6, statsH + 6, {100, 120, 200, 255});
|
||||
drawRectWithOffset(statsX - contentOffsetX, statsY - contentOffsetY, statsW, statsH, {30, 35, 50, 255});
|
||||
|
||||
// Draw next piece preview panel border
|
||||
drawRectWithOffset(nextX - 3 - contentOffsetX, nextY - 3 - contentOffsetY, nextW + 6, nextH + 6, {100, 120, 200, 255});
|
||||
drawRectWithOffset(nextX - contentOffsetX, nextY - contentOffsetY, nextW, nextH, {30, 35, 50, 255});
|
||||
|
||||
// Draw the game board
|
||||
const auto &board = game->boardRef();
|
||||
for (int y = 0; y < Game::ROWS; ++y) {
|
||||
for (int x = 0; x < Game::COLS; ++x) {
|
||||
int v = board[y * Game::COLS + x];
|
||||
if (v > 0) {
|
||||
float bx = gridX + x * finalBlockSize;
|
||||
float by = gridY + y * finalBlockSize;
|
||||
drawBlockTexture(renderer, blocksTex, bx, by, finalBlockSize, v - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Draw ghost piece (where current piece will land)
|
||||
if (!game->isPaused()) {
|
||||
Game::Piece ghostPiece = game->current();
|
||||
// Find landing position
|
||||
while (true) {
|
||||
Game::Piece testPiece = ghostPiece;
|
||||
testPiece.y++;
|
||||
bool collision = false;
|
||||
|
||||
// Simple collision check
|
||||
for (int cy = 0; cy < 4; ++cy) {
|
||||
for (int cx = 0; cx < 4; ++cx) {
|
||||
if (Game::cellFilled(testPiece, cx, cy)) {
|
||||
int gx = testPiece.x + cx;
|
||||
int gy = testPiece.y + cy;
|
||||
if (gy >= Game::ROWS || gx < 0 || gx >= Game::COLS ||
|
||||
(gy >= 0 && board[gy * Game::COLS + gx] != 0)) {
|
||||
collision = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (collision) break;
|
||||
}
|
||||
|
||||
if (collision) break;
|
||||
ghostPiece = testPiece;
|
||||
}
|
||||
|
||||
// Draw ghost piece
|
||||
drawPiece(renderer, blocksTex, ghostPiece, gridX, gridY, finalBlockSize, true);
|
||||
}
|
||||
|
||||
// Draw the falling piece
|
||||
if (!game->isPaused()) {
|
||||
drawPiece(renderer, blocksTex, game->current(), gridX, gridY, finalBlockSize, false);
|
||||
}
|
||||
|
||||
// Draw line clearing effects
|
||||
if (lineEffect && lineEffect->isActive()) {
|
||||
lineEffect->render(renderer, blocksTex, static_cast<int>(gridX), static_cast<int>(gridY), static_cast<int>(finalBlockSize));
|
||||
}
|
||||
|
||||
// Draw next piece preview
|
||||
pixelFont->draw(renderer, nextX + 10, nextY - 20, "NEXT", 1.0f, {255, 220, 0, 255});
|
||||
if (game->next().type < PIECE_COUNT) {
|
||||
drawSmallPiece(renderer, blocksTex, static_cast<PieceType>(game->next().type), nextX + 10, nextY + 10, finalBlockSize * 0.6f);
|
||||
}
|
||||
|
||||
// Draw block statistics (left panel)
|
||||
pixelFont->draw(renderer, statsX + 10, statsY + 10, "BLOCKS", 1.0f, {255, 220, 0, 255});
|
||||
|
||||
const auto& blockCounts = game->getBlockCounts();
|
||||
int totalBlocks = 0;
|
||||
for (int i = 0; i < PIECE_COUNT; ++i) totalBlocks += blockCounts[i];
|
||||
|
||||
const char* pieceNames[] = {"I", "O", "T", "S", "Z", "J", "L"};
|
||||
float yCursor = statsY + 52;
|
||||
|
||||
for (int i = 0; i < PIECE_COUNT; ++i) {
|
||||
float py = yCursor;
|
||||
|
||||
// Draw small piece icon
|
||||
float previewSize = finalBlockSize * 0.55f;
|
||||
drawSmallPiece(renderer, blocksTex, static_cast<PieceType>(i), statsX + 18, py, previewSize);
|
||||
|
||||
// Compute preview height
|
||||
int maxCy = -1;
|
||||
Game::Piece prev;
|
||||
prev.type = static_cast<PieceType>(i);
|
||||
prev.rot = 0;
|
||||
prev.x = 0;
|
||||
prev.y = 0;
|
||||
for (int cy = 0; cy < 4; ++cy) {
|
||||
for (int cx = 0; cx < 4; ++cx) {
|
||||
if (Game::cellFilled(prev, cx, cy)) maxCy = std::max(maxCy, cy);
|
||||
}
|
||||
}
|
||||
int tilesHigh = (maxCy >= 0 ? maxCy + 1 : 1);
|
||||
float previewHeight = tilesHigh * previewSize;
|
||||
|
||||
// Count display
|
||||
int count = blockCounts[i];
|
||||
char countStr[16];
|
||||
snprintf(countStr, sizeof(countStr), "%d", count);
|
||||
pixelFont->draw(renderer, statsX + statsW - 20, py + 6, countStr, 1.1f, {240, 240, 245, 255});
|
||||
|
||||
// Percentage bar
|
||||
int perc = (totalBlocks > 0) ? int(std::round(100.0 * double(count) / double(totalBlocks))) : 0;
|
||||
char percStr[16];
|
||||
snprintf(percStr, sizeof(percStr), "%d%%", perc);
|
||||
|
||||
float barX = statsX + 12;
|
||||
float barY = py + previewHeight + 18.0f;
|
||||
float barW = statsW - 24;
|
||||
float barH = 6;
|
||||
|
||||
pixelFont->draw(renderer, barX, barY - 16, percStr, 0.8f, {230, 230, 235, 255});
|
||||
|
||||
// Progress bar
|
||||
SDL_SetRenderDrawColor(renderer, 170, 170, 175, 200);
|
||||
SDL_FRect track{barX, barY, barW, barH};
|
||||
SDL_RenderFillRect(renderer, &track);
|
||||
|
||||
SDL_Color pc = COLORS[i + 1];
|
||||
SDL_SetRenderDrawColor(renderer, pc.r, pc.g, pc.b, 230);
|
||||
float fillW = barW * (perc / 100.0f);
|
||||
if (fillW < 0) fillW = 0;
|
||||
if (fillW > barW) fillW = barW;
|
||||
SDL_FRect fill{barX, barY, fillW, barH};
|
||||
SDL_RenderFillRect(renderer, &fill);
|
||||
|
||||
yCursor = barY + barH + 18.0f;
|
||||
}
|
||||
|
||||
// Draw score panel (right side)
|
||||
const float contentTopOffset = 0.0f;
|
||||
const float contentBottomOffset = 290.0f;
|
||||
const float contentPad = 36.0f;
|
||||
float scoreContentH = (contentBottomOffset - contentTopOffset) + contentPad;
|
||||
float baseY = gridY + (GRID_H - scoreContentH) * 0.5f;
|
||||
|
||||
pixelFont->draw(renderer, scoreX, baseY + 0, "SCORE", 1.0f, {255, 220, 0, 255});
|
||||
char scoreStr[32];
|
||||
snprintf(scoreStr, sizeof(scoreStr), "%d", game->score());
|
||||
pixelFont->draw(renderer, scoreX, baseY + 25, scoreStr, 0.9f, {255, 255, 255, 255});
|
||||
|
||||
pixelFont->draw(renderer, scoreX, baseY + 70, "LINES", 1.0f, {255, 220, 0, 255});
|
||||
char linesStr[16];
|
||||
snprintf(linesStr, sizeof(linesStr), "%03d", game->lines());
|
||||
pixelFont->draw(renderer, scoreX, baseY + 95, linesStr, 0.9f, {255, 255, 255, 255});
|
||||
|
||||
pixelFont->draw(renderer, scoreX, baseY + 140, "LEVEL", 1.0f, {255, 220, 0, 255});
|
||||
char levelStr[16];
|
||||
snprintf(levelStr, sizeof(levelStr), "%02d", game->level());
|
||||
pixelFont->draw(renderer, scoreX, baseY + 165, levelStr, 0.9f, {255, 255, 255, 255});
|
||||
|
||||
// Next level progress
|
||||
int startLv = game->startLevelBase();
|
||||
int firstThreshold = (startLv + 1) * 10;
|
||||
int linesDone = game->lines();
|
||||
int nextThreshold = 0;
|
||||
if (linesDone < firstThreshold) {
|
||||
nextThreshold = firstThreshold;
|
||||
} else {
|
||||
int blocksPast = linesDone - firstThreshold;
|
||||
nextThreshold = firstThreshold + ((blocksPast / 10) + 1) * 10;
|
||||
}
|
||||
int linesForNext = std::max(0, nextThreshold - linesDone);
|
||||
pixelFont->draw(renderer, scoreX, baseY + 200, "NEXT LVL", 1.0f, {255, 220, 0, 255});
|
||||
char nextStr[32];
|
||||
snprintf(nextStr, sizeof(nextStr), "%d LINES", linesForNext);
|
||||
pixelFont->draw(renderer, scoreX, baseY + 225, nextStr, 0.9f, {80, 255, 120, 255});
|
||||
|
||||
// Time display
|
||||
pixelFont->draw(renderer, scoreX, baseY + 265, "TIME", 1.0f, {255, 220, 0, 255});
|
||||
int totalSecs = static_cast<int>(game->elapsed());
|
||||
int mins = totalSecs / 60;
|
||||
int secs = totalSecs % 60;
|
||||
char timeStr[16];
|
||||
snprintf(timeStr, sizeof(timeStr), "%02d:%02d", mins, secs);
|
||||
pixelFont->draw(renderer, scoreX, baseY + 290, timeStr, 0.9f, {255, 255, 255, 255});
|
||||
|
||||
// Gravity HUD
|
||||
char gms[64];
|
||||
double gms_val = game->getGravityMs();
|
||||
double gfps = gms_val > 0.0 ? (1000.0 / gms_val) : 0.0;
|
||||
snprintf(gms, sizeof(gms), "GRAV: %.0f ms (%.2f fps)", gms_val, gfps);
|
||||
pixelFont->draw(renderer, logicalW - 260, 10, gms, 0.9f, {200, 200, 220, 255});
|
||||
|
||||
// Hold piece (if implemented)
|
||||
if (game->held().type < PIECE_COUNT) {
|
||||
pixelFont->draw(renderer, statsX + 10, statsY + statsH - 80, "HOLD", 1.0f, {255, 220, 0, 255});
|
||||
drawSmallPiece(renderer, blocksTex, static_cast<PieceType>(game->held().type), statsX + 60, statsY + statsH - 80, finalBlockSize * 0.6f);
|
||||
}
|
||||
|
||||
// Pause overlay
|
||||
if (game->isPaused() && !showExitConfirmPopup) {
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 180);
|
||||
SDL_FRect pauseOverlay{0, 0, logicalW, logicalH};
|
||||
SDL_RenderFillRect(renderer, &pauseOverlay);
|
||||
|
||||
pixelFont->draw(renderer, logicalW * 0.5f - 80, logicalH * 0.5f - 20, "PAUSED", 2.0f, {255, 255, 255, 255});
|
||||
pixelFont->draw(renderer, logicalW * 0.5f - 120, logicalH * 0.5f + 30, "Press P to resume", 0.8f, {200, 200, 220, 255});
|
||||
}
|
||||
|
||||
// Exit confirmation popup
|
||||
if (showExitConfirmPopup) {
|
||||
float popupW = 420.0f, popupH = 180.0f;
|
||||
float popupX = (logicalW - popupW) * 0.5f;
|
||||
float popupY = (logicalH - popupH) * 0.5f;
|
||||
|
||||
// Dim entire window (do not change viewport/scales here)
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 200);
|
||||
SDL_FRect fullWin{0.f, 0.f, winW, winH};
|
||||
SDL_RenderFillRect(renderer, &fullWin);
|
||||
|
||||
// Draw popup box in logical coords with content offsets
|
||||
drawRectWithOffset(popupX - 4.0f, popupY - 4.0f, popupW + 8.0f, popupH + 8.0f, {60, 70, 90, 255});
|
||||
drawRectWithOffset(popupX, popupY, popupW, popupH, {20, 22, 28, 240});
|
||||
|
||||
// Text content (measure to perfectly center)
|
||||
const std::string title = "Exit game?";
|
||||
const std::string line1 = "Are you sure you want to";
|
||||
const std::string line2 = "leave the current game?";
|
||||
|
||||
int wTitle=0,hTitle=0; pixelFont->measure(title, 1.6f, wTitle, hTitle);
|
||||
int wL1=0,hL1=0; pixelFont->measure(line1, 0.9f, wL1, hL1);
|
||||
int wL2=0,hL2=0; pixelFont->measure(line2, 0.9f, wL2, hL2);
|
||||
|
||||
float titleX = popupX + (popupW - (float)wTitle) * 0.5f + contentOffsetX;
|
||||
float l1X = popupX + (popupW - (float)wL1) * 0.5f + contentOffsetX;
|
||||
float l2X = popupX + (popupW - (float)wL2) * 0.5f + contentOffsetX;
|
||||
|
||||
pixelFont->draw(renderer, titleX, popupY + contentOffsetY + 20.0f, title, 1.6f, {255, 220, 0, 255});
|
||||
pixelFont->draw(renderer, l1X, popupY + contentOffsetY + 60.0f, line1, 0.9f, {220, 220, 230, 255});
|
||||
pixelFont->draw(renderer, l2X, popupY + contentOffsetY + 84.0f, line2, 0.9f, {220, 220, 230, 255});
|
||||
|
||||
// Buttons
|
||||
float btnW = 140.0f, btnH = 46.0f;
|
||||
float yesX = popupX + popupW * 0.25f - btnW * 0.5f;
|
||||
float noX = popupX + popupW * 0.75f - btnW * 0.5f;
|
||||
float btnY = popupY + popupH - 60.0f;
|
||||
|
||||
// YES button
|
||||
drawRectWithOffset(yesX - 2.0f, btnY - 2.0f, btnW + 4.0f, btnH + 4.0f, {100, 120, 140, 255});
|
||||
drawRectWithOffset(yesX, btnY, btnW, btnH, {200, 60, 60, 255});
|
||||
const std::string yes = "YES";
|
||||
int wYes=0,hYes=0; pixelFont->measure(yes, 1.0f, wYes, hYes);
|
||||
pixelFont->draw(renderer, yesX + (btnW - (float)wYes) * 0.5f + contentOffsetX,
|
||||
btnY + (btnH - (float)hYes) * 0.5f + contentOffsetY,
|
||||
yes, 1.0f, {255, 255, 255, 255});
|
||||
|
||||
// NO button
|
||||
drawRectWithOffset(noX - 2.0f, btnY - 2.0f, btnW + 4.0f, btnH + 4.0f, {100, 120, 140, 255});
|
||||
drawRectWithOffset(noX, btnY, btnW, btnH, {80, 140, 80, 255});
|
||||
const std::string no = "NO";
|
||||
int wNo=0,hNo=0; pixelFont->measure(no, 1.0f, wNo, hNo);
|
||||
pixelFont->draw(renderer, noX + (btnW - (float)wNo) * 0.5f + contentOffsetX,
|
||||
btnY + (btnH - (float)hNo) * 0.5f + contentOffsetY,
|
||||
no, 1.0f, {255, 255, 255, 255});
|
||||
}
|
||||
}
|
||||
40
src/graphics/renderers/GameRenderer.h
Normal file
40
src/graphics/renderers/GameRenderer.h
Normal file
@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
#include <SDL3/SDL.h>
|
||||
#include "../../gameplay/core/Game.h"
|
||||
|
||||
// Forward declarations
|
||||
class FontAtlas;
|
||||
class LineEffect;
|
||||
|
||||
/**
|
||||
* GameRenderer - Utility class for rendering the Tetris game board and HUD.
|
||||
*
|
||||
* This class encapsulates all the game-specific rendering logic that was
|
||||
* previously in main.cpp, making it reusable across different contexts.
|
||||
*/
|
||||
class GameRenderer {
|
||||
public:
|
||||
// Render the complete playing state including game board, HUD, and effects
|
||||
static void renderPlayingState(
|
||||
SDL_Renderer* renderer,
|
||||
Game* game,
|
||||
FontAtlas* pixelFont,
|
||||
LineEffect* lineEffect,
|
||||
SDL_Texture* blocksTex,
|
||||
float logicalW,
|
||||
float logicalH,
|
||||
float logicalScale,
|
||||
float winW,
|
||||
float winH,
|
||||
bool showExitConfirmPopup
|
||||
);
|
||||
|
||||
private:
|
||||
// Helper functions for drawing game elements
|
||||
static void drawBlockTexture(SDL_Renderer* renderer, SDL_Texture* blocksTex, float x, float y, float size, int blockType);
|
||||
static void drawPiece(SDL_Renderer* renderer, SDL_Texture* blocksTex, const Game::Piece& piece, float ox, float oy, float tileSize, bool isGhost = false);
|
||||
static void drawSmallPiece(SDL_Renderer* renderer, SDL_Texture* blocksTex, PieceType pieceType, float x, float y, float tileSize);
|
||||
|
||||
// Helper function for drawing rectangles
|
||||
static void drawRect(SDL_Renderer* renderer, float x, float y, float w, float h, SDL_Color c);
|
||||
};
|
||||
328
src/graphics/renderers/RenderManager.cpp
Normal file
328
src/graphics/renderers/RenderManager.cpp
Normal file
@ -0,0 +1,328 @@
|
||||
#include "RenderManager.h"
|
||||
#include <SDL3/SDL.h>
|
||||
#include <algorithm>
|
||||
|
||||
RenderManager::RenderManager() = default;
|
||||
|
||||
RenderManager::~RenderManager() {
|
||||
if (m_initialized) {
|
||||
shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
bool RenderManager::initialize(int width, int height, const std::string& title) {
|
||||
if (m_initialized) {
|
||||
SDL_LogWarn(SDL_LOG_CATEGORY_RENDER, "RenderManager already initialized");
|
||||
return true;
|
||||
}
|
||||
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_RENDER, "Initializing RenderManager (%dx%d)", width, height);
|
||||
|
||||
// Create window
|
||||
m_window = SDL_CreateWindow(
|
||||
title.c_str(),
|
||||
width, height,
|
||||
SDL_WINDOW_RESIZABLE
|
||||
);
|
||||
|
||||
if (!m_window) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_RENDER, "Failed to create window: %s", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create renderer
|
||||
m_renderer = SDL_CreateRenderer(m_window, nullptr);
|
||||
if (!m_renderer) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_RENDER, "Failed to create renderer: %s", SDL_GetError());
|
||||
SDL_DestroyWindow(m_window);
|
||||
m_window = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Enable VSync
|
||||
SDL_SetRenderVSync(m_renderer, 1);
|
||||
|
||||
// Store window dimensions
|
||||
m_windowWidth = width;
|
||||
m_windowHeight = height;
|
||||
m_logicalWidth = width;
|
||||
m_logicalHeight = height;
|
||||
|
||||
// Set initial logical size
|
||||
setLogicalSize(width, height);
|
||||
|
||||
m_initialized = true;
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_RENDER, "RenderManager initialized successfully");
|
||||
return true;
|
||||
}
|
||||
|
||||
void RenderManager::shutdown() {
|
||||
if (!m_initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_RENDER, "Shutting down RenderManager");
|
||||
|
||||
if (m_renderer) {
|
||||
SDL_DestroyRenderer(m_renderer);
|
||||
m_renderer = nullptr;
|
||||
}
|
||||
|
||||
if (m_window) {
|
||||
SDL_DestroyWindow(m_window);
|
||||
m_window = nullptr;
|
||||
}
|
||||
|
||||
m_initialized = false;
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_RENDER, "RenderManager shutdown complete");
|
||||
}
|
||||
|
||||
void RenderManager::beginFrame() {
|
||||
if (!m_initialized || !m_renderer) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Trace beginFrame entry
|
||||
{
|
||||
FILE* f = fopen("tetris_trace.log", "a"); if (f) { fprintf(f, "RenderManager::beginFrame entry\n"); fclose(f); }
|
||||
}
|
||||
|
||||
// Clear the screen (wrapped with trace)
|
||||
clear(12, 12, 16, 255); // Dark background similar to original
|
||||
|
||||
// Trace after clear
|
||||
{
|
||||
FILE* f = fopen("tetris_trace.log", "a"); if (f) { fprintf(f, "RenderManager::beginFrame after clear\n"); fclose(f); }
|
||||
}
|
||||
}
|
||||
|
||||
void RenderManager::endFrame() {
|
||||
if (!m_initialized || !m_renderer) {
|
||||
return;
|
||||
}
|
||||
// Trace before present
|
||||
{
|
||||
FILE* f = fopen("tetris_trace.log", "a"); if (f) { fprintf(f, "RenderManager::endFrame before present\n"); fclose(f); }
|
||||
}
|
||||
|
||||
SDL_RenderPresent(m_renderer);
|
||||
|
||||
// Trace after present
|
||||
{
|
||||
FILE* f = fopen("tetris_trace.log", "a"); if (f) { fprintf(f, "RenderManager::endFrame after present\n"); fclose(f); }
|
||||
}
|
||||
}
|
||||
|
||||
void RenderManager::setLogicalSize(int width, int height) {
|
||||
if (!m_initialized || !m_renderer) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_logicalWidth = width;
|
||||
m_logicalHeight = height;
|
||||
updateLogicalScale();
|
||||
}
|
||||
|
||||
void RenderManager::setViewport(int x, int y, int width, int height) {
|
||||
if (!m_initialized || !m_renderer) {
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_Rect viewport = { x, y, width, height };
|
||||
SDL_SetRenderViewport(m_renderer, &viewport);
|
||||
// Keep cached logical viewport in sync if this matches our computed logical scale
|
||||
m_logicalVP = viewport;
|
||||
}
|
||||
|
||||
void RenderManager::setScale(float scaleX, float scaleY) {
|
||||
if (!m_initialized || !m_renderer) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_scaleX = scaleX;
|
||||
m_scaleY = scaleY;
|
||||
SDL_SetRenderScale(m_renderer, scaleX, scaleY);
|
||||
}
|
||||
|
||||
void RenderManager::resetViewport() {
|
||||
if (!m_initialized || !m_renderer) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Reset to full window viewport and recompute logical scale/viewport
|
||||
SDL_SetRenderViewport(m_renderer, nullptr);
|
||||
updateLogicalScale();
|
||||
}
|
||||
|
||||
// IRenderer interface implementation
|
||||
void RenderManager::clearScreen(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
|
||||
clear(static_cast<Uint8>(r), static_cast<Uint8>(g), static_cast<Uint8>(b), static_cast<Uint8>(a));
|
||||
}
|
||||
|
||||
void RenderManager::present() {
|
||||
if (!m_initialized || !m_renderer) {
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_RenderPresent(m_renderer);
|
||||
}
|
||||
|
||||
SDL_Renderer* RenderManager::getSDLRenderer() {
|
||||
return m_renderer;
|
||||
}
|
||||
|
||||
void RenderManager::getWindowDimensions(int& width, int& height) const {
|
||||
getWindowSize(width, height);
|
||||
}
|
||||
|
||||
void RenderManager::setViewport(const SDL_Rect* viewport) {
|
||||
if (!m_initialized || !m_renderer) {
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_SetRenderViewport(m_renderer, viewport);
|
||||
}
|
||||
|
||||
// Legacy clear method
|
||||
void RenderManager::clear(Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
|
||||
if (!m_initialized || !m_renderer) {
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_SetRenderDrawColor(m_renderer, r, g, b, a);
|
||||
SDL_RenderClear(m_renderer);
|
||||
}
|
||||
|
||||
void RenderManager::renderTexture(SDL_Texture* texture, const SDL_FRect* src, const SDL_FRect* dst) {
|
||||
if (!m_initialized || !m_renderer || !texture) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Trace renderTexture usage
|
||||
{
|
||||
FILE* f = fopen("tetris_trace.log", "a"); if (f) { fprintf(f, "RenderManager::renderTexture entry tex=%llu src=%p dst=%p\n", (unsigned long long)(uintptr_t)texture, (void*)src, (void*)dst); fclose(f); }
|
||||
}
|
||||
SDL_RenderTexture(m_renderer, texture, src, dst);
|
||||
{
|
||||
FILE* f = fopen("tetris_trace.log", "a"); if (f) { fprintf(f, "RenderManager::renderTexture after SDL_RenderTexture tex=%llu\n", (unsigned long long)(uintptr_t)texture); fclose(f); }
|
||||
}
|
||||
}
|
||||
|
||||
void RenderManager::renderTexture(SDL_Texture* texture, float x, float y) {
|
||||
if (!texture) {
|
||||
return;
|
||||
}
|
||||
|
||||
float w, h;
|
||||
SDL_GetTextureSize(texture, &w, &h);
|
||||
SDL_FRect dst = { x, y, w, h };
|
||||
renderTexture(texture, nullptr, &dst);
|
||||
}
|
||||
|
||||
void RenderManager::renderTexture(SDL_Texture* texture, float x, float y, float w, float h) {
|
||||
if (!texture) {
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_FRect dst = { x, y, w, h };
|
||||
renderTexture(texture, nullptr, &dst);
|
||||
}
|
||||
|
||||
void RenderManager::renderRect(const SDL_FRect& rect, Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
|
||||
if (!m_initialized || !m_renderer) {
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_SetRenderDrawColor(m_renderer, r, g, b, a);
|
||||
SDL_RenderFillRect(m_renderer, &rect);
|
||||
}
|
||||
|
||||
void RenderManager::renderLine(float x1, float y1, float x2, float y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
|
||||
if (!m_initialized || !m_renderer) {
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_SetRenderDrawColor(m_renderer, r, g, b, a);
|
||||
SDL_RenderLine(m_renderer, x1, y1, x2, y2);
|
||||
}
|
||||
|
||||
void RenderManager::renderPoint(float x, float y, Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
|
||||
if (!m_initialized || !m_renderer) {
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_SetRenderDrawColor(m_renderer, r, g, b, a);
|
||||
SDL_RenderPoint(m_renderer, x, y);
|
||||
}
|
||||
|
||||
void RenderManager::handleWindowResize(int newWidth, int newHeight) {
|
||||
if (!m_initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_RENDER, "Window resized to %dx%d", newWidth, newHeight);
|
||||
|
||||
m_windowWidth = newWidth;
|
||||
m_windowHeight = newHeight;
|
||||
updateLogicalScale();
|
||||
}
|
||||
|
||||
void RenderManager::setFullscreen(bool fullscreen) {
|
||||
if (!m_initialized || !m_window) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_isFullscreen == fullscreen) {
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_SetWindowFullscreen(m_window, fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
|
||||
m_isFullscreen = fullscreen;
|
||||
|
||||
// Update window size after fullscreen change
|
||||
SDL_GetWindowSize(m_window, &m_windowWidth, &m_windowHeight);
|
||||
updateLogicalScale();
|
||||
}
|
||||
|
||||
void RenderManager::getWindowSize(int& width, int& height) const {
|
||||
width = m_windowWidth;
|
||||
height = m_windowHeight;
|
||||
}
|
||||
|
||||
void RenderManager::getTextureSize(SDL_Texture* tex, int& w, int& h) const {
|
||||
if (!tex) { w = 0; h = 0; return; }
|
||||
// SDL3 provides SDL_GetTextureSize which accepts float or int pointers depending on overloads
|
||||
float fw = 0.0f, fh = 0.0f;
|
||||
SDL_GetTextureSize(tex, &fw, &fh);
|
||||
w = int(fw + 0.5f);
|
||||
h = int(fh + 0.5f);
|
||||
}
|
||||
|
||||
void RenderManager::updateLogicalScale() {
|
||||
if (!m_initialized || !m_renderer) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Calculate scale to fit logical size into window
|
||||
float scaleX = static_cast<float>(m_windowWidth) / static_cast<float>(m_logicalWidth);
|
||||
float scaleY = static_cast<float>(m_windowHeight) / static_cast<float>(m_logicalHeight);
|
||||
|
||||
// Use uniform scaling to maintain aspect ratio
|
||||
float scale = std::min(scaleX, scaleY);
|
||||
if (scale <= 0.0f) {
|
||||
scale = 1.0f;
|
||||
}
|
||||
|
||||
setScale(scale, scale);
|
||||
// Compute centered logical viewport that preserves aspect ratio and is centered in window
|
||||
int vpW = static_cast<int>(m_logicalWidth * scale);
|
||||
int vpH = static_cast<int>(m_logicalHeight * scale);
|
||||
int vpX = (m_windowWidth - vpW) / 2;
|
||||
int vpY = (m_windowHeight - vpH) / 2;
|
||||
SDL_Rect vp{ vpX, vpY, vpW, vpH };
|
||||
SDL_SetRenderViewport(m_renderer, &vp);
|
||||
|
||||
// Cache logical viewport and scale for callers
|
||||
m_logicalVP = vp;
|
||||
m_logicalScale = scale;
|
||||
}
|
||||
95
src/graphics/renderers/RenderManager.h
Normal file
95
src/graphics/renderers/RenderManager.h
Normal file
@ -0,0 +1,95 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <string>
|
||||
#include "../../core/interfaces/IRenderer.h"
|
||||
|
||||
/**
|
||||
* RenderManager - Abstracts SDL rendering functionality
|
||||
*
|
||||
* Responsibilities:
|
||||
* - Manage SDL_Window and SDL_Renderer lifecycle
|
||||
* - Provide high-level rendering interface
|
||||
* - Handle viewport and scaling logic
|
||||
* - Abstract SDL-specific details from game code
|
||||
*/
|
||||
class RenderManager : public IRenderer {
|
||||
public:
|
||||
RenderManager();
|
||||
~RenderManager();
|
||||
|
||||
// Initialization and cleanup
|
||||
bool initialize(int width, int height, const std::string& title);
|
||||
void shutdown();
|
||||
|
||||
// Frame management
|
||||
void beginFrame();
|
||||
void endFrame();
|
||||
|
||||
// IRenderer interface implementation
|
||||
void clearScreen(uint8_t r, uint8_t g, uint8_t b, uint8_t a) override;
|
||||
void present() override;
|
||||
SDL_Renderer* getSDLRenderer() override;
|
||||
void getWindowDimensions(int& width, int& height) const override;
|
||||
void setViewport(const SDL_Rect* viewport) override;
|
||||
void setScale(float scaleX, float scaleY) override;
|
||||
|
||||
// Additional RenderManager-specific methods
|
||||
void setLogicalSize(int width, int height);
|
||||
void setViewport(int x, int y, int width, int height);
|
||||
void resetViewport();
|
||||
|
||||
// Query the computed logical viewport and scale (useful for consistent input mapping)
|
||||
SDL_Rect getLogicalViewport() const { return m_logicalVP; }
|
||||
float getLogicalScale() const { return m_logicalScale; }
|
||||
|
||||
// Basic rendering operations (legacy method signature)
|
||||
void clear(Uint8 r = 0, Uint8 g = 0, Uint8 b = 0, Uint8 a = 255);
|
||||
|
||||
// Texture rendering
|
||||
void renderTexture(SDL_Texture* texture, const SDL_FRect* src, const SDL_FRect* dst);
|
||||
void renderTexture(SDL_Texture* texture, float x, float y);
|
||||
void renderTexture(SDL_Texture* texture, float x, float y, float w, float h);
|
||||
|
||||
// Primitive rendering
|
||||
void renderRect(const SDL_FRect& rect, Uint8 r, Uint8 g, Uint8 b, Uint8 a = 255);
|
||||
void renderLine(float x1, float y1, float x2, float y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a = 255);
|
||||
void renderPoint(float x, float y, Uint8 r, Uint8 g, Uint8 b, Uint8 a = 255);
|
||||
|
||||
// Window management
|
||||
void handleWindowResize(int newWidth, int newHeight);
|
||||
void setFullscreen(bool fullscreen);
|
||||
void getWindowSize(int& width, int& height) const;
|
||||
|
||||
// Direct access to SDL objects (temporary, will be removed later)
|
||||
SDL_Window* getSDLWindow() const { return m_window; }
|
||||
// Texture queries
|
||||
void getTextureSize(SDL_Texture* tex, int& w, int& h) const;
|
||||
|
||||
// State queries
|
||||
bool isInitialized() const { return m_initialized; }
|
||||
bool isFullscreen() const { return m_isFullscreen; }
|
||||
|
||||
private:
|
||||
// SDL objects
|
||||
SDL_Window* m_window = nullptr;
|
||||
SDL_Renderer* m_renderer = nullptr;
|
||||
|
||||
// Window properties
|
||||
int m_windowWidth = 0;
|
||||
int m_windowHeight = 0;
|
||||
int m_logicalWidth = 0;
|
||||
int m_logicalHeight = 0;
|
||||
float m_scaleX = 1.0f;
|
||||
float m_scaleY = 1.0f;
|
||||
|
||||
// State
|
||||
bool m_initialized = false;
|
||||
bool m_isFullscreen = false;
|
||||
// Cached logical viewport and scale (centered within window)
|
||||
SDL_Rect m_logicalVP{0,0,0,0};
|
||||
float m_logicalScale = 1.0f;
|
||||
|
||||
// Helper methods
|
||||
void updateLogicalScale();
|
||||
};
|
||||
36
src/graphics/ui/Font.cpp
Normal file
36
src/graphics/ui/Font.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
// Font.cpp - implementation of FontAtlas (copied into src/graphics)
|
||||
#include "graphics/Font.h"
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
bool FontAtlas::init(const std::string& path, int basePt) { fontPath = path; baseSize = basePt; return true; }
|
||||
|
||||
void FontAtlas::shutdown() { for (auto &kv : cache) if (kv.second) TTF_CloseFont(kv.second); cache.clear(); }
|
||||
|
||||
TTF_Font* FontAtlas::getSized(int ptSize) {
|
||||
auto it = cache.find(ptSize); if (it!=cache.end()) return it->second;
|
||||
TTF_Font* f = TTF_OpenFont(fontPath.c_str(), ptSize);
|
||||
if (!f) return nullptr; cache[ptSize] = f; return f;
|
||||
}
|
||||
|
||||
void FontAtlas::draw(SDL_Renderer* r, float x, float y, const std::string& text, float scale, SDL_Color color) {
|
||||
if (scale <= 0) return; int pt = int(baseSize * scale); if (pt < 8) pt = 8; TTF_Font* f = getSized(pt); if (!f) return;
|
||||
SDL_Surface* surf = TTF_RenderText_Blended(f, text.c_str(), text.length(), color); if (!surf) return;
|
||||
SDL_Texture* tex = SDL_CreateTextureFromSurface(r, surf);
|
||||
if (tex) { SDL_FRect dst{ x, y, (float)surf->w, (float)surf->h }; SDL_RenderTexture(r, tex, nullptr, &dst); SDL_DestroyTexture(tex); }
|
||||
SDL_DestroySurface(surf);
|
||||
}
|
||||
|
||||
void FontAtlas::measure(const std::string& text, float scale, int& outW, int& outH) {
|
||||
outW = 0; outH = 0;
|
||||
if (scale <= 0) return;
|
||||
int pt = int(baseSize * scale);
|
||||
if (pt < 1) pt = 1;
|
||||
TTF_Font* f = getSized(pt);
|
||||
if (!f) return;
|
||||
// Use render-to-surface measurement to avoid dependency on specific TTF_* measurement API variants
|
||||
SDL_Color dummy = {255,255,255,255};
|
||||
SDL_Surface* surf = TTF_RenderText_Blended(f, text.c_str(), text.length(), dummy);
|
||||
if (!surf) return;
|
||||
outW = surf->w; outH = surf->h;
|
||||
SDL_DestroySurface(surf);
|
||||
}
|
||||
20
src/graphics/ui/Font.h
Normal file
20
src/graphics/ui/Font.h
Normal file
@ -0,0 +1,20 @@
|
||||
// Font.h - Font rendering abstraction with simple size cache
|
||||
#pragma once
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
struct SDL_Renderer;
|
||||
|
||||
class FontAtlas {
|
||||
public:
|
||||
bool init(const std::string& path, int basePt);
|
||||
void shutdown();
|
||||
void draw(SDL_Renderer* r, float x, float y, const std::string& text, float scale, SDL_Color color);
|
||||
// Measure rendered text size in pixels for a given scale
|
||||
void measure(const std::string& text, float scale, int& outW, int& outH);
|
||||
private:
|
||||
std::string fontPath;
|
||||
int baseSize{24};
|
||||
std::unordered_map<int, TTF_Font*> cache; // point size -> font*
|
||||
TTF_Font* getSized(int ptSize);
|
||||
};
|
||||
48
src/main.cpp
48
src/main.cpp
@ -17,12 +17,12 @@
|
||||
#include "audio/Audio.h"
|
||||
#include "audio/SoundEffect.h"
|
||||
|
||||
#include "gameplay/Game.h"
|
||||
#include "gameplay/core/Game.h"
|
||||
#include "persistence/Scores.h"
|
||||
#include "graphics/Starfield.h"
|
||||
#include "Starfield3D.h"
|
||||
#include "graphics/Font.h"
|
||||
#include "gameplay/LineEffect.h"
|
||||
#include "graphics/effects/Starfield.h"
|
||||
#include "graphics/effects/Starfield3D.h"
|
||||
#include "graphics/ui/Font.h"
|
||||
#include "gameplay/effects/LineEffect.h"
|
||||
#include "states/State.h"
|
||||
#include "states/LoadingState.h"
|
||||
#include "states/MenuState.h"
|
||||
@ -268,7 +268,7 @@ static void drawSettingsPopup(SDL_Renderer* renderer, FontAtlas& font, bool musi
|
||||
// Starfield now managed by Starfield class
|
||||
|
||||
// State manager integration (scaffolded in StateManager.h)
|
||||
#include "core/StateManager.h"
|
||||
#include "core/state/StateManager.h"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Intro/Menu state variables
|
||||
@ -664,28 +664,10 @@ int main(int, char **)
|
||||
stateMgr.registerOnEnter(AppState::LevelSelector, [&](){ levelSelectorState->onEnter(); });
|
||||
stateMgr.registerOnExit(AppState::LevelSelector, [&](){ levelSelectorState->onExit(); });
|
||||
|
||||
// Combined Playing state handler: run playingState handler and inline gameplay mapping
|
||||
// Combined Playing state handler: run playingState handler
|
||||
stateMgr.registerHandler(AppState::Playing, [&](const SDL_Event& e){
|
||||
// First give the PlayingState a chance to handle the event
|
||||
playingState->handleEvent(e);
|
||||
|
||||
// Then perform inline gameplay mappings (gravity/rotation/hard-drop/hold)
|
||||
if (e.type == SDL_EVENT_KEY_DOWN && !e.key.repeat) {
|
||||
if (!game.isPaused()) {
|
||||
if (e.key.scancode == SDL_SCANCODE_SPACE) {
|
||||
game.hardDrop();
|
||||
}
|
||||
else if (e.key.scancode == SDL_SCANCODE_UP) {
|
||||
game.rotate(+1);
|
||||
}
|
||||
else if (e.key.scancode == SDL_SCANCODE_Z || (e.key.mod & SDL_KMOD_SHIFT)) {
|
||||
game.rotate(-1);
|
||||
}
|
||||
else if (e.key.scancode == SDL_SCANCODE_C || (e.key.mod & SDL_KMOD_CTRL)) {
|
||||
game.holdCurrent();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
stateMgr.registerOnEnter(AppState::Playing, [&](){ playingState->onEnter(); });
|
||||
stateMgr.registerOnExit(AppState::Playing, [&](){ playingState->onExit(); });
|
||||
@ -983,7 +965,8 @@ int main(int, char **)
|
||||
// Update progress based on background loading
|
||||
if (currentTrackLoading > 0 && !musicLoaded) {
|
||||
currentTrackLoading = Audio::instance().getLoadedTrackCount();
|
||||
if (Audio::instance().isLoadingComplete()) {
|
||||
// If loading is complete OR we've loaded all expected tracks (handles potential thread cleanup hang)
|
||||
if (Audio::instance().isLoadingComplete() || (totalTracks > 0 && currentTrackLoading >= totalTracks)) {
|
||||
Audio::instance().shuffle(); // Shuffle once all tracks are loaded
|
||||
musicLoaded = true;
|
||||
}
|
||||
@ -1006,6 +989,10 @@ int main(int, char **)
|
||||
|
||||
// Ensure we never exceed 100% and reach exactly 100% when everything is loaded
|
||||
loadingProgress = std::min(1.0, loadingProgress);
|
||||
|
||||
// Fix floating point precision issues (0.2 + 0.7 + 0.1 can be 0.9999...)
|
||||
if (loadingProgress > 0.99) loadingProgress = 1.0;
|
||||
|
||||
if (musicLoaded && timeProgress >= 0.1) {
|
||||
loadingProgress = 1.0;
|
||||
}
|
||||
@ -1082,6 +1069,7 @@ int main(int, char **)
|
||||
snprintf(bgPath, sizeof(bgPath), "assets/images/tetris_main_back_level%d.bmp", bgLevel);
|
||||
SDL_Surface* levelBgSurface = SDL_LoadBMP(bgPath);
|
||||
if (levelBgSurface) {
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loaded background for level %d: %s", bgLevel, bgPath);
|
||||
nextLevelBackgroundTex = SDL_CreateTextureFromSurface(renderer, levelBgSurface);
|
||||
SDL_DestroySurface(levelBgSurface);
|
||||
// start fade transition
|
||||
@ -1089,6 +1077,7 @@ int main(int, char **)
|
||||
levelFadeElapsed = 0.0f;
|
||||
cachedLevel = bgLevel;
|
||||
} else {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load background for level %d: %s (Error: %s)", bgLevel, bgPath, SDL_GetError());
|
||||
// don't change textures if file missing
|
||||
cachedLevel = -1;
|
||||
}
|
||||
@ -1427,9 +1416,14 @@ int main(int, char **)
|
||||
drawPiece(renderer, blocksTex, game.current(), gridX, gridY, finalBlockSize, false);
|
||||
}
|
||||
|
||||
// Handle line clearing effects
|
||||
if (game.hasCompletedLines() && !lineEffect.isActive()) {
|
||||
lineEffect.startLineClear(game.getCompletedLines(), static_cast<int>(gridX), static_cast<int>(gridY), static_cast<int>(finalBlockSize));
|
||||
}
|
||||
|
||||
// Draw line clearing effects
|
||||
if (lineEffect.isActive()) {
|
||||
lineEffect.render(renderer, static_cast<int>(gridX), static_cast<int>(gridY), static_cast<int>(finalBlockSize));
|
||||
lineEffect.render(renderer, blocksTex, static_cast<int>(gridX), static_cast<int>(gridY), static_cast<int>(finalBlockSize));
|
||||
}
|
||||
|
||||
// Draw next piece preview
|
||||
|
||||
1677
src/main_dist.cpp
Normal file
1677
src/main_dist.cpp
Normal file
File diff suppressed because it is too large
Load Diff
@ -3,7 +3,7 @@
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3/SDL_main.h>
|
||||
#include "core/ApplicationManager.h"
|
||||
#include "core/application/ApplicationManager.h"
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
||||
@ -1,16 +1,15 @@
|
||||
// LevelSelectorState.cpp - Level selection popup state implementation
|
||||
#include "LevelSelectorState.h"
|
||||
#include "State.h"
|
||||
#include "../core/StateManager.h"
|
||||
#include "../graphics/Font.h"
|
||||
#include "../core/state/StateManager.h"
|
||||
#include "../core/GlobalState.h"
|
||||
#include "../graphics/ui/Font.h"
|
||||
#include <SDL3/SDL.h>
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
||||
// Constants from main.cpp
|
||||
static constexpr int LOGICAL_W = 1200;
|
||||
static constexpr int LOGICAL_H = 1000;
|
||||
// Use dynamic logical dimensions from GlobalState instead of hardcoded values
|
||||
|
||||
// --- Minimal draw helpers and look-and-feel adapted from the sample ---
|
||||
static inline SDL_Color RGBA(Uint8 r, Uint8 g, Uint8 b, Uint8 a = 255) { return SDL_Color{r, g, b, a}; }
|
||||
@ -187,6 +186,10 @@ void LevelSelectorState::handleEvent(const SDL_Event& e) {
|
||||
if (ctx.startLevelSelection) *ctx.startLevelSelection = hoveredLevel;
|
||||
} else if (e.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
|
||||
if (e.button.button == SDL_BUTTON_LEFT) {
|
||||
// Get dynamic logical dimensions
|
||||
const int LOGICAL_W = GlobalState::instance().getLogicalWidth();
|
||||
const int LOGICAL_H = GlobalState::instance().getLogicalHeight();
|
||||
|
||||
// convert mouse to logical coords (viewport is already centered)
|
||||
float lx = (float(e.button.x) - float(lastLogicalVP.x)) / (lastLogicalScale > 0.f ? lastLogicalScale : 1.f);
|
||||
float ly = (float(e.button.y) - float(lastLogicalVP.y)) / (lastLogicalScale > 0.f ? lastLogicalScale : 1.f);
|
||||
@ -200,6 +203,10 @@ void LevelSelectorState::handleEvent(const SDL_Event& e) {
|
||||
}
|
||||
}
|
||||
} else if (e.type == SDL_EVENT_MOUSE_MOTION) {
|
||||
// Get dynamic logical dimensions
|
||||
const int LOGICAL_W = GlobalState::instance().getLogicalWidth();
|
||||
const int LOGICAL_H = GlobalState::instance().getLogicalHeight();
|
||||
|
||||
// convert mouse to logical coords (viewport is already centered)
|
||||
float lx = (float(e.motion.x) - float(lastLogicalVP.x)) / (lastLogicalScale > 0.f ? lastLogicalScale : 1.f);
|
||||
float ly = (float(e.motion.y) - float(lastLogicalVP.y)) / (lastLogicalScale > 0.f ? lastLogicalScale : 1.f);
|
||||
@ -224,6 +231,10 @@ void LevelSelectorState::render(SDL_Renderer* renderer, float logicalScale, SDL_
|
||||
void LevelSelectorState::drawLevelSelectionPopup(SDL_Renderer* renderer) {
|
||||
if (!renderer) return;
|
||||
|
||||
// Get dynamic logical dimensions
|
||||
const int LOGICAL_W = GlobalState::instance().getLogicalWidth();
|
||||
const int LOGICAL_H = GlobalState::instance().getLogicalHeight();
|
||||
|
||||
// Since ApplicationManager sets up a centered viewport, we draw directly in logical coordinates
|
||||
// The viewport (LOGICAL_W x LOGICAL_H) is already centered within the window
|
||||
float vw = float(LOGICAL_W);
|
||||
@ -252,6 +263,10 @@ void LevelSelectorState::drawLevelSelectionPopup(SDL_Renderer* renderer) {
|
||||
}
|
||||
|
||||
bool LevelSelectorState::isMouseInPopup(float mouseX, float mouseY, float& popupX, float& popupY, float& popupW, float& popupH) {
|
||||
// Get dynamic logical dimensions
|
||||
const int LOGICAL_W = GlobalState::instance().getLogicalWidth();
|
||||
const int LOGICAL_H = GlobalState::instance().getLogicalHeight();
|
||||
|
||||
// Simplified: viewport is already centered, just convert mouse to logical coords
|
||||
(void)mouseX; (void)mouseY;
|
||||
float lx = 0.f, ly = 0.f;
|
||||
@ -265,6 +280,10 @@ bool LevelSelectorState::isMouseInPopup(float mouseX, float mouseY, float& popup
|
||||
}
|
||||
|
||||
int LevelSelectorState::getLevelFromMouse(float mouseX, float mouseY, float popupX, float popupY, float popupW, float popupH) {
|
||||
// Get dynamic logical dimensions
|
||||
const int LOGICAL_W = GlobalState::instance().getLogicalWidth();
|
||||
const int LOGICAL_H = GlobalState::instance().getLogicalHeight();
|
||||
|
||||
(void)popupX; (void)popupY; (void)popupW; (void)popupH;
|
||||
float lx = 0.f, ly = 0.f;
|
||||
if (lastLogicalScale > 0.0f) {
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
// LoadingState.cpp
|
||||
#include "LoadingState.h"
|
||||
#include "gameplay/Game.h"
|
||||
#include "../gameplay/core/Game.h"
|
||||
#include <SDL3/SDL.h>
|
||||
#include <cstdio>
|
||||
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
// MenuState.cpp
|
||||
#include "MenuState.h"
|
||||
#include "persistence/Scores.h"
|
||||
#include "graphics/Font.h"
|
||||
@ -9,9 +8,8 @@
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
// Local logical canvas size (matches main.cpp). Kept local to avoid changing many files.
|
||||
static constexpr int LOGICAL_W = 1200;
|
||||
static constexpr int LOGICAL_H = 1000;
|
||||
// Use dynamic logical dimensions from GlobalState instead of hardcoded values
|
||||
// This allows the UI to adapt when the window is resized or goes fullscreen
|
||||
|
||||
// Shared flags and resources are provided via StateContext `ctx`.
|
||||
// Removed fragile extern declarations and use `ctx.showLevelPopup`, `ctx.showSettingsPopup`,
|
||||
@ -39,14 +37,23 @@ void MenuState::update(double frameMs) {
|
||||
}
|
||||
|
||||
void MenuState::render(SDL_Renderer* renderer, float logicalScale, SDL_Rect logicalVP) {
|
||||
// Use fixed logical dimensions to match main.cpp and ensure consistent layout
|
||||
// This prevents the UI from floating apart on wide/tall screens
|
||||
const float LOGICAL_W = 1200.f;
|
||||
const float LOGICAL_H = 1000.f;
|
||||
|
||||
// Trace entry to persistent log for debugging abrupt exit/crash during render
|
||||
{
|
||||
FILE* f = fopen("tetris_trace.log", "a"); if (f) { fprintf(f, "MenuState::render entry\n"); fclose(f); }
|
||||
}
|
||||
// Since ApplicationManager sets up a centered viewport, we draw directly in logical coordinates
|
||||
// No additional content offset is needed - the viewport itself handles centering
|
||||
float contentOffsetX = 0.0f;
|
||||
float contentOffsetY = 0.0f;
|
||||
|
||||
// Compute content offsets (same approach as main.cpp for proper centering)
|
||||
float winW = (float)logicalVP.w;
|
||||
float winH = (float)logicalVP.h;
|
||||
float contentW = LOGICAL_W * logicalScale;
|
||||
float contentH = LOGICAL_H * logicalScale;
|
||||
float contentOffsetX = (winW - contentW) * 0.5f / logicalScale;
|
||||
float contentOffsetY = (winH - contentH) * 0.5f / logicalScale;
|
||||
|
||||
// Background is drawn by main (stretched to the full window) to avoid double-draw.
|
||||
|
||||
@ -94,7 +101,10 @@ void MenuState::render(SDL_Renderer* renderer, float logicalScale, SDL_Rect logi
|
||||
{
|
||||
FILE* f = fopen("tetris_trace.log", "a"); if (f) { fprintf(f, "MenuState::render before useFont->draw TOP PLAYERS ptr=%llu\n", (unsigned long long)(uintptr_t)useFont); fclose(f); }
|
||||
}
|
||||
useFont->draw(renderer, LOGICAL_W * 0.5f - 110 + contentOffsetX, topPlayersY, std::string("TOP PLAYERS"), 1.8f, SDL_Color{255, 220, 0, 255});
|
||||
const std::string title = "TOP PLAYERS";
|
||||
int tW = 0, tH = 0; useFont->measure(title, 1.8f, tW, tH);
|
||||
float titleX = (LOGICAL_W - (float)tW) * 0.5f + contentOffsetX;
|
||||
useFont->draw(renderer, titleX, topPlayersY, title, 1.8f, SDL_Color{255, 220, 0, 255});
|
||||
{
|
||||
FILE* f = fopen("tetris_trace.log", "a"); if (f) { fprintf(f, "MenuState::render after useFont->draw TOP PLAYERS\n"); fclose(f); }
|
||||
}
|
||||
@ -148,8 +158,7 @@ void MenuState::render(SDL_Renderer* renderer, float logicalScale, SDL_Rect logi
|
||||
}
|
||||
|
||||
// Draw bottom action buttons with responsive sizing (reduced to match main mouse hit-test)
|
||||
// Since we removed content offsets, calculate contentW directly from the scale and logical size
|
||||
float contentW = LOGICAL_W * logicalScale;
|
||||
// Use the contentW calculated at the top with content offsets
|
||||
bool isSmall = (contentW < 700.0f);
|
||||
float btnW = isSmall ? (LOGICAL_W * 0.4f) : 300.0f;
|
||||
float btnH = isSmall ? 60.0f : 70.0f;
|
||||
@ -178,7 +187,6 @@ void MenuState::render(SDL_Renderer* renderer, float logicalScale, SDL_Rect logi
|
||||
};
|
||||
drawMenuButtonLocal(renderer, *ctx.pixelFont, btnX - btnW * 0.6f, btnY, btnW, btnH, std::string("PLAY"), SDL_Color{60,180,80,255}, SDL_Color{30,120,40,255});
|
||||
{
|
||||
FILE* f = fopen("tetris_trace.log", "a"); if (f) { fprintf(f, "MenuState::render after draw PLAY button\n"); fclose(f); }
|
||||
}
|
||||
drawMenuButtonLocal(renderer, *ctx.pixelFont, btnX + btnW * 0.6f, btnY, btnW, btnH, std::string(levelBtnText), SDL_Color{40,140,240,255}, SDL_Color{20,100,200,255});
|
||||
{
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
#include "PlayingState.h"
|
||||
#include "core/StateManager.h"
|
||||
#include "gameplay/Game.h"
|
||||
#include "gameplay/LineEffect.h"
|
||||
#include "persistence/Scores.h"
|
||||
#include "../core/state/StateManager.h"
|
||||
#include "../gameplay/core/Game.h"
|
||||
#include "../gameplay/effects/LineEffect.h"
|
||||
#include "../persistence/Scores.h"
|
||||
#include "../audio/Audio.h"
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user