Start atlas

This commit is contained in:
2025-06-01 22:13:02 +02:00
parent 96a9a45c20
commit 84805b92cb
64 changed files with 954 additions and 243 deletions

80
util/atlas.c Normal file
View File

@@ -0,0 +1,80 @@
//
// Created by bruno on 1.6.2025.
//
#include "atlas.h"
#include "util.h"
SDL_Texture *atlasTexture;
int atlasX = 0, atlasY = 0;
int tileIndex = 0; // Which 32x32 tile we're on
int quadrantIndex = 0; // Which 16x16 slot inside that tile
SDL_Rect allocate_16x16(SDL_Texture *srcTexture, SDL_Renderer *renderer) {
SDL_Texture * oldTarget = SDL_GetRenderTarget(renderer);
SDL_SetRenderTarget(renderer, atlasTexture);
int tileX = tileIndex % ATLAS_TILES_PER_ROW;
int tileY = tileIndex / ATLAS_TILES_PER_ROW;
int dx = (quadrantIndex % 2) * QUADRANT_SIZE;
int dy = (quadrantIndex / 2) * QUADRANT_SIZE;
SDL_Rect destRect = {
tileX * TILE_SIZE + dx,
tileY * TILE_SIZE + dy,
QUADRANT_SIZE,
QUADRANT_SIZE
};
SDL_RenderCopy(renderer, srcTexture, NULL, &destRect);
quadrantIndex++;
if (quadrantIndex >= 4) {
tileIndex++;
quadrantIndex = 0;
}
SDL_SetRenderTarget(renderer, oldTarget);
return destRect;
}
SDL_Rect allocate_32x32(SDL_Texture *srcTexture, SDL_Renderer *renderer) {
SDL_Texture * oldTarget = SDL_GetRenderTarget(renderer);
SDL_SetRenderTarget(renderer, atlasTexture);
// If were not at the start of a tile, skip to the next clean one
if (quadrantIndex != 0) {
tileIndex++;
quadrantIndex = 0;
}
int tileX = tileIndex % ATLAS_TILES_PER_ROW;
int tileY = tileIndex / ATLAS_TILES_PER_ROW;
SDL_Rect destRect = {
tileX * TILE_SIZE,
tileY * TILE_SIZE,
TILE_SIZE,
TILE_SIZE
};
SDL_RenderCopy(renderer, srcTexture, NULL, &destRect);
tileIndex++; // Move to next tile
// quadrantIndex stays 0 — new tile is fresh
SDL_SetRenderTarget(renderer, oldTarget);
return destRect;
}
void initAtlas(SDL_Renderer *renderer) {
// Clear atlas with transparent
SDL_SetRenderTarget(renderer, atlasTexture);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
atlasTexture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET,
ATLAS_SIZE, ATLAS_SIZE);
}