Some more hopefully working progress

This commit is contained in:
2025-05-31 23:54:55 +02:00
parent 0c9698879b
commit 6b6e7df035
26 changed files with 659 additions and 393 deletions

View File

@@ -4,10 +4,17 @@
#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 = 1;
uint16_t tileTypeIndex = 0;
TileType TileRegistry[TILEREGISTRY_SIZE];
@@ -31,6 +38,13 @@ void generateTestMap() {
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);
}
}
}
@@ -45,23 +59,25 @@ void registerTile(char name[20], SDL_Renderer *renderer) {
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 = 30;
TileRegistry[tileTypeIndex].breakTime = 15;
tileTypeIndex++;
}
void loadTiles(SDL_Renderer *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, 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) {
@@ -69,4 +85,67 @@ uint16_t getBreakTime(int type) {
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);
}