Immediate Code Cleanup
This commit is contained in:
179
src/core/GlobalState.cpp
Normal file
179
src/core/GlobalState.cpp
Normal file
@ -0,0 +1,179 @@
|
||||
#include "GlobalState.h"
|
||||
#include "Config.h"
|
||||
#include <SDL3/SDL.h>
|
||||
#include <algorithm>
|
||||
#include <random>
|
||||
|
||||
GlobalState& GlobalState::instance() {
|
||||
static GlobalState instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
void GlobalState::initialize() {
|
||||
if (m_initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialize timing
|
||||
lastMs = SDL_GetTicks();
|
||||
loadStart = SDL_GetTicks();
|
||||
|
||||
// Initialize viewport to logical dimensions
|
||||
logicalVP = {0, 0, Config::Logical::WIDTH, Config::Logical::HEIGHT};
|
||||
|
||||
// Initialize fireworks system
|
||||
fireworks.clear();
|
||||
lastFireworkTime = 0;
|
||||
|
||||
m_initialized = true;
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "[GlobalState] Initialized");
|
||||
}
|
||||
|
||||
void GlobalState::shutdown() {
|
||||
if (!m_initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Clear fireworks
|
||||
fireworks.clear();
|
||||
|
||||
m_initialized = false;
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "[GlobalState] Shutdown complete");
|
||||
}
|
||||
|
||||
void GlobalState::updateFireworks(double frameMs) {
|
||||
const Uint64 currentTime = SDL_GetTicks();
|
||||
|
||||
// Create new fireworks occasionally
|
||||
if (currentTime - lastFireworkTime > 800 + (rand() % 1200)) {
|
||||
float x = Config::Logical::WIDTH * 0.2f + (rand() % (int)(Config::Logical::WIDTH * 0.6f));
|
||||
float y = Config::Logical::HEIGHT * 0.3f + (rand() % (int)(Config::Logical::HEIGHT * 0.4f));
|
||||
createFirework(x, y);
|
||||
lastFireworkTime = currentTime;
|
||||
}
|
||||
|
||||
// Update existing fireworks
|
||||
for (auto& firework : fireworks) {
|
||||
if (!firework.active) continue;
|
||||
|
||||
bool hasActiveParticles = false;
|
||||
for (auto& particle : firework.particles) {
|
||||
if (particle.life <= 0) continue;
|
||||
|
||||
// Update physics
|
||||
particle.x += particle.vx * (frameMs / 1000.0f);
|
||||
particle.y += particle.vy * (frameMs / 1000.0f);
|
||||
particle.vy += 150.0f * (frameMs / 1000.0f); // Gravity
|
||||
particle.life -= frameMs;
|
||||
|
||||
// Fade size over time
|
||||
float lifeRatio = particle.life / particle.maxLife;
|
||||
particle.size = 20.0f + 10.0f * lifeRatio;
|
||||
|
||||
if (particle.life > 0) {
|
||||
hasActiveParticles = true;
|
||||
}
|
||||
}
|
||||
|
||||
firework.active = hasActiveParticles;
|
||||
}
|
||||
}
|
||||
|
||||
void GlobalState::createFirework(float x, float y) {
|
||||
// Find an inactive firework to reuse
|
||||
TetrisFirework* firework = nullptr;
|
||||
for (auto& fw : fireworks) {
|
||||
if (!fw.active) {
|
||||
firework = &fw;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// If no inactive firework found, create a new one
|
||||
if (!firework) {
|
||||
fireworks.emplace_back();
|
||||
firework = &fireworks.back();
|
||||
}
|
||||
|
||||
// Initialize firework
|
||||
firework->active = true;
|
||||
firework->particles.clear();
|
||||
|
||||
// Create particles
|
||||
const int particleCount = 12 + (rand() % 8);
|
||||
for (int i = 0; i < particleCount; ++i) {
|
||||
BlockParticle particle;
|
||||
particle.x = x;
|
||||
particle.y = y;
|
||||
|
||||
// Random velocity in all directions
|
||||
float angle = (float)(rand() % 360) * 3.14159f / 180.0f;
|
||||
float speed = 80.0f + (rand() % 120);
|
||||
particle.vx = cos(angle) * speed;
|
||||
particle.vy = sin(angle) * speed - 50.0f; // Slight upward bias
|
||||
|
||||
particle.type = 1 + (rand() % 7); // Random tetris piece color
|
||||
particle.maxLife = 1500.0f + (rand() % 1000); // 1.5-2.5 seconds
|
||||
particle.life = particle.maxLife;
|
||||
particle.size = 15.0f + (rand() % 15);
|
||||
|
||||
firework->particles.push_back(particle);
|
||||
}
|
||||
}
|
||||
|
||||
void GlobalState::drawFireworks(SDL_Renderer* renderer, SDL_Texture* blocksTex) {
|
||||
if (!blocksTex) return;
|
||||
|
||||
for (const auto& firework : fireworks) {
|
||||
if (!firework.active) continue;
|
||||
|
||||
for (const auto& particle : firework.particles) {
|
||||
if (particle.life <= 0) continue;
|
||||
|
||||
// Calculate alpha based on remaining life
|
||||
float lifeRatio = particle.life / particle.maxLife;
|
||||
Uint8 alpha = (Uint8)(255 * std::min(1.0f, lifeRatio * 2.0f));
|
||||
|
||||
// Set texture alpha
|
||||
SDL_SetTextureAlphaMod(blocksTex, alpha);
|
||||
SDL_SetTextureBlendMode(blocksTex, SDL_BLENDMODE_BLEND);
|
||||
|
||||
// Draw particle as a small block
|
||||
SDL_FRect srcRect = {(particle.type - 1) * 90.0f, 0, 90.0f, 90.0f};
|
||||
SDL_FRect dstRect = {
|
||||
particle.x - particle.size / 2,
|
||||
particle.y - particle.size / 2,
|
||||
particle.size,
|
||||
particle.size
|
||||
};
|
||||
|
||||
SDL_RenderTexture(renderer, blocksTex, &srcRect, &dstRect);
|
||||
}
|
||||
}
|
||||
|
||||
// Reset texture alpha
|
||||
SDL_SetTextureAlphaMod(blocksTex, 255);
|
||||
SDL_SetTextureBlendMode(blocksTex, SDL_BLENDMODE_BLEND);
|
||||
}
|
||||
|
||||
void GlobalState::resetGameState() {
|
||||
// Reset game-related state
|
||||
leftHeld = false;
|
||||
rightHeld = false;
|
||||
moveTimerMs = 0.0;
|
||||
startLevelSelection = 0;
|
||||
}
|
||||
|
||||
void GlobalState::resetUIState() {
|
||||
// Reset UI state
|
||||
showSettingsPopup = false;
|
||||
showExitConfirmPopup = false;
|
||||
hoveredButton = -1;
|
||||
}
|
||||
|
||||
void GlobalState::resetAnimationState() {
|
||||
// Reset animation state
|
||||
logoAnimCounter = 0.0;
|
||||
fireworks.clear();
|
||||
lastFireworkTime = 0;
|
||||
}
|
||||
Reference in New Issue
Block a user