Initial release: SDL Windows Tetris
This commit is contained in:
37
src/states/LoadingState.cpp
Normal file
37
src/states/LoadingState.cpp
Normal file
@ -0,0 +1,37 @@
|
||||
// LoadingState.cpp
|
||||
#include "LoadingState.h"
|
||||
#include "../Game.h"
|
||||
#include <SDL3/SDL.h>
|
||||
#include <cstdio>
|
||||
|
||||
LoadingState::LoadingState(StateContext& ctx) : State(ctx) {}
|
||||
|
||||
void LoadingState::onEnter() {
|
||||
loadStart = SDL_GetTicks();
|
||||
loadingProgress = 0.0;
|
||||
musicLoaded = false;
|
||||
currentTrackLoading = 0;
|
||||
totalTracks = 0;
|
||||
// Kick off audio loading if available
|
||||
if (ctx.game) {
|
||||
// audio initialization handled elsewhere (main still creates Audio)
|
||||
}
|
||||
}
|
||||
|
||||
void LoadingState::onExit() {
|
||||
}
|
||||
|
||||
void LoadingState::handleEvent(const SDL_Event& e) {
|
||||
(void)e; // no direct event handling in loading screen
|
||||
}
|
||||
|
||||
void LoadingState::update(double frameMs) {
|
||||
// Progress calculation is done in main; keep this simple
|
||||
// This stub allows later migration of Audio::background loading
|
||||
loadingProgress = std::min(1.0, loadingProgress + frameMs / 1000.0);
|
||||
}
|
||||
|
||||
void LoadingState::render(SDL_Renderer* renderer, float logicalScale, SDL_Rect logicalVP) {
|
||||
(void)renderer; (void)logicalScale; (void)logicalVP;
|
||||
// Rendering is still performed in main for now; this placeholder keeps the API.
|
||||
}
|
||||
20
src/states/LoadingState.h
Normal file
20
src/states/LoadingState.h
Normal file
@ -0,0 +1,20 @@
|
||||
// LoadingState.h
|
||||
#pragma once
|
||||
#include "State.h"
|
||||
|
||||
class LoadingState : public State {
|
||||
public:
|
||||
LoadingState(StateContext& ctx);
|
||||
void onEnter() override;
|
||||
void onExit() override;
|
||||
void handleEvent(const SDL_Event& e) override;
|
||||
void update(double frameMs) override;
|
||||
void render(SDL_Renderer* renderer, float logicalScale, SDL_Rect logicalVP) override;
|
||||
|
||||
private:
|
||||
double loadingProgress = 0.0;
|
||||
Uint64 loadStart = 0;
|
||||
bool musicLoaded = false;
|
||||
int currentTrackLoading = 0;
|
||||
int totalTracks = 0;
|
||||
};
|
||||
33
src/states/MenuState.cpp
Normal file
33
src/states/MenuState.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
// MenuState.cpp
|
||||
#include "MenuState.h"
|
||||
#include "../Scores.h"
|
||||
#include "../Font.h"
|
||||
#include <SDL3/SDL.h>
|
||||
#include <cstdio>
|
||||
|
||||
MenuState::MenuState(StateContext& ctx) : State(ctx) {}
|
||||
|
||||
void MenuState::onEnter() {
|
||||
// nothing for now
|
||||
}
|
||||
|
||||
void MenuState::onExit() {
|
||||
}
|
||||
|
||||
void MenuState::handleEvent(const SDL_Event& e) {
|
||||
// Menu-specific key handling moved from main; main still handles mouse for now
|
||||
if (e.type == SDL_EVENT_KEY_DOWN && !e.key.repeat) {
|
||||
if (ctx.startLevelSelection && *ctx.startLevelSelection >= 0) {
|
||||
// keep simple: allow L/S toggles handled globally in main for now
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MenuState::update(double frameMs) {
|
||||
(void)frameMs;
|
||||
}
|
||||
|
||||
void MenuState::render(SDL_Renderer* renderer, float logicalScale, SDL_Rect logicalVP) {
|
||||
(void)renderer; (void)logicalScale; (void)logicalVP;
|
||||
// Main still performs actual rendering for now; this placeholder keeps the API.
|
||||
}
|
||||
13
src/states/MenuState.h
Normal file
13
src/states/MenuState.h
Normal file
@ -0,0 +1,13 @@
|
||||
// MenuState.h
|
||||
#pragma once
|
||||
#include "State.h"
|
||||
|
||||
class MenuState : public State {
|
||||
public:
|
||||
MenuState(StateContext& ctx);
|
||||
void onEnter() override;
|
||||
void onExit() override;
|
||||
void handleEvent(const SDL_Event& e) override;
|
||||
void update(double frameMs) override;
|
||||
void render(SDL_Renderer* renderer, float logicalScale, SDL_Rect logicalVP) override;
|
||||
};
|
||||
51
src/states/State.h
Normal file
51
src/states/State.h
Normal file
@ -0,0 +1,51 @@
|
||||
// State.h - base class and shared context for app states
|
||||
#pragma once
|
||||
#include <SDL3/SDL.h>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
// Forward declarations for frequently used types
|
||||
class Game;
|
||||
class ScoreManager;
|
||||
class Starfield;
|
||||
class Starfield3D;
|
||||
class FontAtlas;
|
||||
class LineEffect;
|
||||
|
||||
// Shared context passed to states so they can access common resources
|
||||
struct StateContext {
|
||||
// Core subsystems (may be null if not available)
|
||||
Game* game = nullptr;
|
||||
ScoreManager* scores = nullptr;
|
||||
Starfield* starfield = nullptr;
|
||||
Starfield3D* starfield3D = nullptr;
|
||||
FontAtlas* font = nullptr;
|
||||
FontAtlas* pixelFont = nullptr;
|
||||
LineEffect* lineEffect = nullptr;
|
||||
|
||||
// Textures
|
||||
SDL_Texture* logoTex = nullptr;
|
||||
SDL_Texture* backgroundTex = nullptr;
|
||||
SDL_Texture* blocksTex = nullptr;
|
||||
|
||||
// Audio / SFX - forward declared types in main
|
||||
// Pointers to booleans/flags used by multiple states
|
||||
bool* musicEnabled = nullptr;
|
||||
int* startLevelSelection = nullptr;
|
||||
int* hoveredButton = nullptr;
|
||||
};
|
||||
|
||||
class State {
|
||||
public:
|
||||
explicit State(StateContext& ctx) : ctx(ctx) {}
|
||||
virtual ~State() = default;
|
||||
|
||||
virtual void onEnter() {}
|
||||
virtual void onExit() {}
|
||||
virtual void handleEvent(const SDL_Event& e) {}
|
||||
virtual void update(double frameMs) {}
|
||||
virtual void render(SDL_Renderer* renderer, float logicalScale, SDL_Rect logicalVP) {}
|
||||
|
||||
protected:
|
||||
StateContext& ctx;
|
||||
};
|
||||
Reference in New Issue
Block a user