converted from bmp to jpg
This commit is contained in:
97
src/main.cpp
97
src/main.cpp
@ -3,6 +3,7 @@
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3/SDL_main.h>
|
||||
#include <SDL3_image/SDL_image.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
#include <string>
|
||||
#include <cstdio>
|
||||
@ -30,6 +31,7 @@
|
||||
#include "states/LevelSelectorState.h"
|
||||
#include "states/PlayingState.h"
|
||||
#include "audio/MenuWrappers.h"
|
||||
#include "utils/ImagePathResolver.h"
|
||||
#include "graphics/renderers/GameRenderer.h"
|
||||
|
||||
// Debug logging removed: no-op in this build (previously LOG_DEBUG)
|
||||
@ -74,6 +76,36 @@ static void drawRect(SDL_Renderer *r, float x, float y, float w, float h, SDL_Co
|
||||
SDL_RenderFillRect(r, &fr);
|
||||
}
|
||||
|
||||
static SDL_Texture* loadTextureFromImage(SDL_Renderer* renderer, const std::string& path, int* outW = nullptr, int* outH = nullptr) {
|
||||
if (!renderer) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const std::string resolvedPath = AssetPath::resolveImagePath(path);
|
||||
SDL_Surface* surface = IMG_Load(resolvedPath.c_str());
|
||||
if (!surface) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load image %s (resolved: %s): %s", path.c_str(), resolvedPath.c_str(), SDL_GetError());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (outW) { *outW = surface->w; }
|
||||
if (outH) { *outH = surface->h; }
|
||||
|
||||
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||
SDL_DestroySurface(surface);
|
||||
|
||||
if (!texture) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create texture from %s: %s", resolvedPath.c_str(), SDL_GetError());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (resolvedPath != path) {
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loaded %s via %s", path.c_str(), resolvedPath.c_str());
|
||||
}
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
// Hover state for level popup ( -1 = none, 0..19 = hovered level )
|
||||
// Now managed by LevelSelectorState
|
||||
|
||||
@ -455,41 +487,15 @@ int main(int, char **)
|
||||
LineEffect lineEffect;
|
||||
lineEffect.init(renderer);
|
||||
|
||||
// Load logo using native SDL BMP loading
|
||||
SDL_Texture *logoTex = nullptr;
|
||||
SDL_Surface* logoSurface = SDL_LoadBMP("assets/images/logo.bmp");
|
||||
if (logoSurface) {
|
||||
(void)0;
|
||||
logoTex = SDL_CreateTextureFromSurface(renderer, logoSurface);
|
||||
SDL_DestroySurface(logoSurface);
|
||||
} else {
|
||||
(void)0;
|
||||
}
|
||||
// Load logo assets via SDL_image so we can use compressed formats
|
||||
SDL_Texture* logoTex = loadTextureFromImage(renderer, "assets/images/logo.bmp");
|
||||
|
||||
// Load small logo (used by Menu to show whole logo)
|
||||
SDL_Texture *logoSmallTex = nullptr;
|
||||
SDL_Surface* logoSmallSurface = SDL_LoadBMP("assets/images/logo_small.bmp");
|
||||
int logoSmallW = 0, logoSmallH = 0;
|
||||
if (logoSmallSurface) {
|
||||
// capture surface size before creating the texture (avoids SDL_QueryTexture)
|
||||
logoSmallW = logoSmallSurface->w;
|
||||
logoSmallH = logoSmallSurface->h;
|
||||
logoSmallTex = SDL_CreateTextureFromSurface(renderer, logoSmallSurface);
|
||||
SDL_DestroySurface(logoSmallSurface);
|
||||
} else {
|
||||
// fallback: leave logoSmallTex null so MenuState will use large logo
|
||||
(void)0;
|
||||
}
|
||||
SDL_Texture* logoSmallTex = loadTextureFromImage(renderer, "assets/images/logo_small.bmp", &logoSmallW, &logoSmallH);
|
||||
|
||||
// Load background using native SDL BMP loading
|
||||
SDL_Texture *backgroundTex = nullptr;
|
||||
SDL_Surface* backgroundSurface = SDL_LoadBMP("assets/images/main_background.bmp");
|
||||
if (backgroundSurface) {
|
||||
(void)0;
|
||||
backgroundTex = SDL_CreateTextureFromSurface(renderer, backgroundSurface);
|
||||
SDL_DestroySurface(backgroundSurface);
|
||||
} else {
|
||||
(void)0;
|
||||
}
|
||||
// Load menu background using SDL_image (prefers JPEG)
|
||||
SDL_Texture* backgroundTex = loadTextureFromImage(renderer, "assets/images/main_background.bmp");
|
||||
|
||||
// Note: `backgroundTex` is owned by main and passed into `StateContext::backgroundTex` below.
|
||||
// States should render using `ctx.backgroundTex` rather than accessing globals.
|
||||
@ -505,16 +511,8 @@ int main(int, char **)
|
||||
// Default start level selection: 0 (declare here so it's in scope for all handlers)
|
||||
int startLevelSelection = 0;
|
||||
|
||||
// Load blocks texture using native SDL BMP loading
|
||||
SDL_Texture *blocksTex = nullptr;
|
||||
SDL_Surface* blocksSurface = SDL_LoadBMP("assets/images/blocks90px_001.bmp");
|
||||
if (blocksSurface) {
|
||||
(void)0;
|
||||
blocksTex = SDL_CreateTextureFromSurface(renderer, blocksSurface);
|
||||
SDL_DestroySurface(blocksSurface);
|
||||
} else {
|
||||
(void)0;
|
||||
}
|
||||
// Load blocks texture via SDL_image (falls back to procedural blocks if missing)
|
||||
SDL_Texture* blocksTex = loadTextureFromImage(renderer, "assets/images/blocks90px_001.bmp");
|
||||
// No global exposure of blocksTex; states receive textures via StateContext.
|
||||
|
||||
if (!blocksTex) {
|
||||
@ -1287,18 +1285,17 @@ int main(int, char **)
|
||||
// Load new level background into nextLevelBackgroundTex
|
||||
if (nextLevelBackgroundTex) { SDL_DestroyTexture(nextLevelBackgroundTex); nextLevelBackgroundTex = nullptr; }
|
||||
char bgPath[256];
|
||||
snprintf(bgPath, sizeof(bgPath), "assets/images/tetris_main_back_level%d.bmp", bgLevel);
|
||||
SDL_Surface* levelBgSurface = SDL_LoadBMP(bgPath);
|
||||
if (levelBgSurface) {
|
||||
snprintf(bgPath, sizeof(bgPath), "assets/images/tetris_main_back_level%d.jpg", bgLevel);
|
||||
SDL_Texture* newLevelTex = loadTextureFromImage(renderer, bgPath);
|
||||
if (newLevelTex) {
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loaded background for level %d: %s", bgLevel, bgPath);
|
||||
nextLevelBackgroundTex = SDL_CreateTextureFromSurface(renderer, levelBgSurface);
|
||||
SDL_DestroySurface(levelBgSurface);
|
||||
nextLevelBackgroundTex = newLevelTex;
|
||||
// start fade transition
|
||||
levelFadeAlpha = 0.0f;
|
||||
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());
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load background for level %d: %s", bgLevel, bgPath);
|
||||
// don't change textures if file missing
|
||||
cachedLevel = -1;
|
||||
}
|
||||
@ -1651,10 +1648,14 @@ int main(int, char **)
|
||||
SDL_DestroyTexture(logoTex);
|
||||
if (backgroundTex)
|
||||
SDL_DestroyTexture(backgroundTex);
|
||||
if (nextLevelBackgroundTex)
|
||||
SDL_DestroyTexture(nextLevelBackgroundTex);
|
||||
if (levelBackgroundTex)
|
||||
SDL_DestroyTexture(levelBackgroundTex);
|
||||
if (blocksTex)
|
||||
SDL_DestroyTexture(blocksTex);
|
||||
if (logoSmallTex)
|
||||
SDL_DestroyTexture(logoSmallTex);
|
||||
lineEffect.shutdown();
|
||||
Audio::instance().shutdown();
|
||||
SoundEffectManager::instance().shutdown();
|
||||
|
||||
Reference in New Issue
Block a user