99 lines
3.1 KiB
C
99 lines
3.1 KiB
C
//
|
|
// Created by bruno on 4/24/25.
|
|
//
|
|
|
|
#include "belt.h"
|
|
|
|
#include "../util/util.h"
|
|
#include "tile.h"
|
|
#include "../player/player.h"
|
|
|
|
static int scrollFrame = 0;
|
|
unsigned long beltFrames = 0;
|
|
|
|
|
|
void renderBelt(int x, int y, int w, int h, OrientDirection dir, SDL_Renderer *renderer) {
|
|
|
|
int px = x * TILE_SIZE;
|
|
int py = y * TILE_SIZE;
|
|
|
|
uint16_t tileType = tileMap[y][x].type;
|
|
|
|
SDL_Rect src1, src2, dst1, dst2;
|
|
|
|
if (dir == ORIENT_LEFT || dir == ORIENT_RIGHT) {
|
|
int offset = scrollFrame % TILE_SIZE;
|
|
|
|
if (dir == ORIENT_RIGHT) {
|
|
offset = TILE_SIZE - offset; // reverse scroll
|
|
}
|
|
|
|
src1 = (SDL_Rect) {offset, 0, TILE_SIZE - offset, TILE_SIZE};
|
|
dst1 = (SDL_Rect) {px, py, (w - offset), h};
|
|
|
|
src2 = (SDL_Rect) {0, 0, offset, TILE_SIZE};
|
|
dst2 = (SDL_Rect) {px + (w - offset), py, offset, h};
|
|
|
|
adjustRect(&dst1);
|
|
adjustRect(&dst2);
|
|
|
|
SDL_RenderCopy(renderer, TileRegistry[tileType].textures[dir], &src1, &dst1);
|
|
SDL_RenderCopy(renderer, TileRegistry[tileType].textures[dir], &src2, &dst2);
|
|
} else {
|
|
int offset = scrollFrame % TILE_SIZE;
|
|
|
|
if (dir == ORIENT_DOWN) {
|
|
offset = TILE_SIZE - offset; // reverse scroll
|
|
}
|
|
|
|
src1 = (SDL_Rect) {0, offset, TILE_SIZE, TILE_SIZE - offset};
|
|
dst1 = (SDL_Rect) {px, py, w, h - offset};
|
|
|
|
src2 = (SDL_Rect) {0, 0, TILE_SIZE, offset};
|
|
dst2 = (SDL_Rect) {px, py + (h - offset), w, offset};
|
|
|
|
adjustRect(&dst1);
|
|
adjustRect(&dst2);
|
|
|
|
|
|
// Rotate to make the belt vertical
|
|
SDL_RenderCopy(renderer, TileRegistry[tileType].textures[dir], &src1, &dst1);
|
|
SDL_RenderCopy(renderer, TileRegistry[tileType].textures[dir], &src2, &dst2);
|
|
}
|
|
}
|
|
|
|
|
|
void renderAllBelts(SDL_Renderer *renderer) {
|
|
int scrollSpeed = 0; // pixels per step
|
|
int scrollDelay = 1; // frames between steps
|
|
|
|
if (beltFrames++ % scrollDelay == 0) {
|
|
scrollFrame += scrollSpeed;
|
|
}
|
|
|
|
int tileSize = TILE_SIZE;
|
|
for (int y = (playerY / TILE_SIZE) - (DISPLAY_MAP_HEIGHT / 2);
|
|
y < (playerY / TILE_SIZE) + (DISPLAY_MAP_HEIGHT / 2); y++) {
|
|
for (int x = (playerX / TILE_SIZE) - (DISPLAY_MAP_WIDTH / 2);
|
|
x < (playerX / TILE_SIZE) + (DISPLAY_MAP_WIDTH / 2); x++) {
|
|
Tile t = tileMap[y][x];
|
|
if (t.type != TYPE_BELT) continue;
|
|
renderBelt(x, y, tileSize, tileSize, t.direction, renderer);
|
|
}
|
|
}
|
|
for (int y = (playerY / TILE_SIZE) - (DISPLAY_MAP_HEIGHT / 2);
|
|
y < (playerY / TILE_SIZE) + (DISPLAY_MAP_HEIGHT / 2); y++) {
|
|
for (int x = (playerX / TILE_SIZE) - (DISPLAY_MAP_WIDTH / 2);
|
|
x < (playerX / TILE_SIZE) + (DISPLAY_MAP_WIDTH / 2); x++) {
|
|
Tile t = tileMap[y][x];
|
|
if (t.type != TYPE_BELT) continue;
|
|
for (uint8_t lane = 0; lane < 2; lane++) {
|
|
for (uint8_t itemIndex = 0; itemIndex < 2; itemIndex++) {
|
|
if (t.items[lane][itemIndex].active) {
|
|
renderItem(t.items[lane][itemIndex], renderer);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |