151 lines
5.0 KiB
C
151 lines
5.0 KiB
C
//
|
|
// Created by bruno on 4/24/25.
|
|
//
|
|
|
|
#include <dirent.h>
|
|
#include "tile.h"
|
|
#include "../player/player.h"
|
|
|
|
int scrollFrame = 0;
|
|
unsigned long beltFrames = 0;
|
|
|
|
SDL_Texture *tilesTexture;
|
|
SDL_Texture *itemsTexture;
|
|
|
|
Tile tileMap[MAP_HEIGHT][MAP_WIDTH];
|
|
|
|
uint16_t tileTypeIndex = 0;
|
|
|
|
TileType TileRegistry[TILEREGISTRY_SIZE];
|
|
|
|
void generateTestMap() {
|
|
for (int y = 0; y < DISPLAY_MAP_HEIGHT; y++) {
|
|
for (int x = 0; x < DISPLAY_MAP_WIDTH; x++) {
|
|
Tile tile = {0};
|
|
tile.x = x;
|
|
tile.y = y;
|
|
tileMap[y][x] = tile;
|
|
}
|
|
}
|
|
|
|
for (int x = 0; x < MAP_WIDTH; x += 1) {
|
|
for (int y = 0; y < MAP_HEIGHT; y += 1) {
|
|
|
|
tileMap[y][x].type = TYPE_BELT;
|
|
tileMap[y][x].frameOffset = 0;
|
|
//tileMap[y][x].direction = ((x + y) % 4 * 2) + 1;
|
|
//tileMap[y][x].direction = 5;
|
|
tileMap[y][x].direction = (rand() % 4 * 2) + 1;
|
|
}
|
|
}
|
|
|
|
uint8_t type = 0;
|
|
for (int x = 142; x < 154; x += 3) {
|
|
for (int y = 80; y < 94; y += 3) {
|
|
putItem(x, y, type++ % ITEMREGISTRY_SIZE, 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void registerTile(char name[20], SDL_Renderer *renderer) {
|
|
const char *dot = strchr(name, '.');
|
|
memcpy(TileRegistry[tileTypeIndex].name, name, dot - name);
|
|
char texturePath[80];
|
|
snprintf(texturePath, 80, "./assets/tiles/%s", name);
|
|
SDL_Texture *texture = IMG_LoadTexture(renderer, texturePath);
|
|
TileRegistry[tileTypeIndex].textures[ORIENT_LEFT] = texture;
|
|
TileRegistry[tileTypeIndex].textures[ORIENT_RIGHT] = createFlippedTexture(renderer, texture, SDL_FLIP_HORIZONTAL);
|
|
TileRegistry[tileTypeIndex].textures[ORIENT_UP] = createRotatedTexture(renderer, texture, 90);
|
|
TileRegistry[tileTypeIndex].textures[ORIENT_DOWN] = createRotatedTexture(renderer, texture, 270);
|
|
TileRegistry[tileTypeIndex].type = tileTypeIndex;
|
|
TileRegistry[tileTypeIndex].breakTime = 15;
|
|
|
|
tileTypeIndex++;
|
|
}
|
|
|
|
|
|
void loadTiles(SDL_Renderer *renderer) {
|
|
iterateSortedDir("./assets/tiles", (DirEntryCallback) registerTile, renderer);
|
|
// DIR *dir = opendir("./assets/tiles");
|
|
// if (dir) {
|
|
// struct dirent *entry;
|
|
// while ((entry = readdir(dir)) != NULL) {
|
|
// if (entry->d_name[0] == '.') {
|
|
// continue;
|
|
// }
|
|
// registerTile(entry->d_name, mainRenderer);
|
|
// }
|
|
// }
|
|
TileRegistry[0].breakTime = 0;
|
|
}
|
|
|
|
uint16_t getBreakTime(int type) {
|
|
if (type >= tileTypeIndex) {
|
|
return 0;
|
|
}
|
|
return TileRegistry[type].breakTime;
|
|
}
|
|
|
|
void initTiles() {
|
|
itemsTexture = SDL_CreateTexture(mainRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, screenRect.w,
|
|
screenRect.h);
|
|
tilesTexture = SDL_CreateTexture(mainRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, screenRect.w,
|
|
screenRect.h);
|
|
SDL_SetTextureBlendMode(itemsTexture, SDL_BLENDMODE_BLEND);
|
|
SDL_SetTextureBlendMode(tilesTexture, SDL_BLENDMODE_BLEND);
|
|
}
|
|
|
|
void renderAllTiles(SDL_Renderer *renderer, SDL_Rect playerRect) {
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
|
|
SDL_Texture *oldTarget = SDL_GetRenderTarget(renderer);
|
|
SDL_SetRenderTarget(renderer, itemsTexture);
|
|
SDL_RenderClear(renderer);
|
|
SDL_SetRenderTarget(renderer, tilesTexture);
|
|
SDL_RenderClear(renderer);
|
|
int scrollSpeed = 1; // pixels per step
|
|
int scrollDelay = 1; // frames between steps
|
|
|
|
if (beltFrames++ % scrollDelay == 0) {
|
|
scrollFrame += scrollSpeed;
|
|
}
|
|
|
|
int tileSize = TILE_SIZE;
|
|
for (int y = (playerRect.y / TILE_SIZE) - (DISPLAY_MAP_HEIGHT / 2) - 1;
|
|
y < (playerRect.y / TILE_SIZE) + (DISPLAY_MAP_HEIGHT / 2) + 1; y++) {
|
|
if (y < 0 || y >= MAP_HEIGHT) {
|
|
continue;
|
|
}
|
|
for (int x = (playerRect.x / TILE_SIZE) - (DISPLAY_MAP_WIDTH / 2) - 1;
|
|
x < (playerRect.x / TILE_SIZE) + (DISPLAY_MAP_WIDTH / 2) + 1; x++) {
|
|
if (x < 0 || x >= MAP_WIDTH) {
|
|
continue;
|
|
}
|
|
Tile t = tileMap[y][x];
|
|
SDL_SetRenderTarget(renderer, tilesTexture);
|
|
switch (t.type) {
|
|
case TYPE_BELT:
|
|
renderBelt(x, y, tileSize, tileSize, t.direction, playerRect, renderer);
|
|
break;
|
|
default:
|
|
SDL_Rect dstRect;
|
|
dstRect.x = x * TILE_SIZE;
|
|
dstRect.y = y * TILE_SIZE;
|
|
dstRect.w = TILE_SIZE;
|
|
dstRect.h = TILE_SIZE;
|
|
adjustRect(&dstRect, playerRect);
|
|
SDL_Texture *tex = TileRegistry[t.type].textures[t.direction];
|
|
if (tex == NULL) {
|
|
tex = TileRegistry[t.type].textures[ORIENT_LEFT];
|
|
}
|
|
if (tex != NULL) {
|
|
SDL_RenderCopy(renderer, tex, NULL, &dstRect);
|
|
}
|
|
|
|
}
|
|
if (t.type == TYPE_BELT) {
|
|
}
|
|
}
|
|
}
|
|
SDL_SetRenderTarget(renderer, oldTarget);
|
|
} |