something
This commit is contained in:
85
tiles/belt.c
Normal file
85
tiles/belt.c
Normal file
@@ -0,0 +1,85 @@
|
||||
//
|
||||
// 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 = 1; // 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);
|
||||
if (t.item.active) {
|
||||
renderItem(t.item, renderer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
16
tiles/belt.h
Normal file
16
tiles/belt.h
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// Created by bruno on 4/24/25.
|
||||
//
|
||||
|
||||
#ifndef FACTORYGAME_BELT_H
|
||||
#define FACTORYGAME_BELT_H
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include "tile.h"
|
||||
#include "../util/util.h"
|
||||
|
||||
void renderBelt(int px, int py, int w, int h, OrientDirection dir, SDL_Renderer * renderer);
|
||||
|
||||
void renderAllBelts(SDL_Renderer * renderer);
|
||||
|
||||
#endif //FACTORYGAME_BELT_H
|
67
tiles/tile.c
Normal file
67
tiles/tile.c
Normal file
@@ -0,0 +1,67 @@
|
||||
//
|
||||
// Created by bruno on 4/24/25.
|
||||
//
|
||||
|
||||
#include <dirent.h>
|
||||
#include "tile.h"
|
||||
#include "../player/player.h"
|
||||
#include "../util/util.h"
|
||||
|
||||
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 = (playerX / TILE_SIZE) - (DISPLAY_MAP_WIDTH / 2);
|
||||
x < (playerX / TILE_SIZE) + (DISPLAY_MAP_WIDTH / 2); x += 1) {
|
||||
for (int y = (playerY / TILE_SIZE) - (DISPLAY_MAP_HEIGHT / 2);
|
||||
y < (playerY / TILE_SIZE) + (DISPLAY_MAP_HEIGHT / 2); 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
49
tiles/tile.h
Normal file
49
tiles/tile.h
Normal file
@@ -0,0 +1,49 @@
|
||||
//
|
||||
// Created by bruno on 4/24/25.
|
||||
//
|
||||
|
||||
#ifndef FACTORYGAME_TILE_H
|
||||
#define FACTORYGAME_TILE_H
|
||||
|
||||
#include "belt.h"
|
||||
#include "../items/item.h"
|
||||
#include "../util/util.h"
|
||||
|
||||
#define MAP_WIDTH 600
|
||||
#define MAP_HEIGHT 340
|
||||
|
||||
#define DISPLAY_MAP_WIDTH 30
|
||||
#define DISPLAY_MAP_HEIGHT 16
|
||||
|
||||
#define TILE_SIZE 32
|
||||
|
||||
#define DISPLAY_WIDTH DISPLAY_MAP_WIDTH * TILE_SIZE
|
||||
#define DISPLAY_HEIGHT DISPLAY_MAP_HEIGHT * TILE_SIZE
|
||||
|
||||
typedef struct {
|
||||
uint16_t type;
|
||||
char name[20];
|
||||
SDL_Texture *textures[ORIENT_DIRECTION_COUNT];
|
||||
} TileType;
|
||||
|
||||
#define TILEREGISTRY_SIZE 512
|
||||
|
||||
extern TileType TileRegistry[TILEREGISTRY_SIZE];
|
||||
|
||||
#define TYPE_BELT 0
|
||||
|
||||
typedef struct {
|
||||
OrientDirection direction;
|
||||
uint16_t type;
|
||||
int frameOffset;
|
||||
ItemOnBelt item;
|
||||
int x;
|
||||
int y;
|
||||
} Tile;
|
||||
|
||||
extern Tile tileMap[MAP_HEIGHT][MAP_WIDTH];
|
||||
|
||||
void generateTestMap();
|
||||
void loadTiles(SDL_Renderer *renderer);
|
||||
|
||||
#endif //FACTORYGAME_TILE_H
|
Reference in New Issue
Block a user