something

This commit is contained in:
2025-04-24 20:04:41 +02:00
parent 8fd5f1cf1e
commit 451e80f750
23 changed files with 565 additions and 161 deletions

176
main.c
View File

@@ -3,42 +3,10 @@
#include <time.h>
#include "util/font.h"
#include "util/audio.h"
#include <SDL2/SDL_image.h>
#define MAP_WIDTH 64
#define MAP_HEIGHT 36
#define TILE_SIZE 16
#define DISPLAY_WIDTH MAP_WIDTH * TILE_SIZE
#define DISPLAY_HEIGHT MAP_HEIGHT * TILE_SIZE
typedef enum {
BELT_LEFT_DOWN,
BELT_LEFT,
BELT_LEFT_UP,
BELT_UP,
BELT_RIGHT_UP,
BELT_RIGHT,
BELT_RIGHT_DOWN,
BELT_DOWN
} BeltDirection;
typedef struct {
float x, y; // local position in tile (0.01.0)
bool active;
} Item;
typedef struct {
bool hasBelt;
BeltDirection direction;
int frameOffset;
Item item;
} Tile;
Tile tileMap[MAP_HEIGHT][MAP_WIDTH];
#include "tiles/tile.h"
#include "tiles/belt.h"
#include "items/item.h"
#include "player/player.h"
//Screen dimension constants
const int SCREEN_WIDTH = DISPLAY_WIDTH;
@@ -63,9 +31,6 @@ BitmapFont fonts[fontCount];
unsigned long frames = 0;
bool cursor = true;
SDL_Texture *beltTex;
void msleep(unsigned int milliseconds) {
struct timespec ts;
ts.tv_sec = milliseconds / 1000;
@@ -73,28 +38,10 @@ void msleep(unsigned int milliseconds) {
nanosleep(&ts, NULL);
}
void generateTestMap() {
for (int y = 0; y < MAP_HEIGHT; y++) {
for (int x = 0; x < MAP_WIDTH; x++) {
tileMap[y][x] = (Tile){0};
}
}
for (int x = 5; x < 5 + (4*2); x+=2) {
tileMap[10][x].hasBelt = true;
tileMap[10][x].frameOffset = 0;
}
tileMap[10][5].direction = BELT_LEFT;
tileMap[10][7].direction = BELT_RIGHT;
tileMap[10][9].direction = BELT_UP;
tileMap[10][11].direction = BELT_DOWN;
tileMap[10][5].item = (Item){.x = 0.5f, .y = 0.5f, .active = true};
}
int init() {
//Initialize SDL
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, NULL);
SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "1");
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
@@ -123,6 +70,8 @@ int init() {
return 1;
}
loadItems(renderer);
loadTiles(renderer);
// Create OpenGL context
SDL_GLContext glContext = SDL_GL_CreateContext(window);
if (!glContext) {
@@ -162,112 +111,12 @@ int init() {
smallerFont = prepText(renderer, 8, "PublicPixel.ttf", 255, 255, 255, 255);
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
beltTex = IMG_LoadTexture(renderer, "../assets/dopravnik.png");
generateTestMap();
return 0;
}
void updateItems() {
for (int y = 0; y < MAP_HEIGHT; y++) {
for (int x = 0; x < MAP_WIDTH; x++) {
Tile *t = &tileMap[y][x];
if (!t->hasBelt || !t->item.active) continue;
float speed = 0.05f;
switch (t->direction) {
case BELT_LEFT: t->item.x -= speed; break;
case BELT_RIGHT: t->item.x += speed; break;
case BELT_UP: t->item.y -= speed; break;
case BELT_DOWN: t->item.y += speed; break;
default: break;
}
if (t->item.x < 0 || t->item.x > 1 || t->item.y < 0 || t->item.y > 1) {
int nx = x, ny = y;
if (t->direction == BELT_LEFT) nx--;
if (t->direction == BELT_RIGHT) nx++;
if (t->direction == BELT_UP) ny--;
if (t->direction == BELT_DOWN) ny++;
if (nx >= 0 && nx < MAP_WIDTH && ny >= 0 && ny < MAP_HEIGHT) {
Tile *next = &tileMap[ny][nx];
if (next->hasBelt && next->direction == t->direction && !next->item.active) {
next->item = (Item){ .x = 0.5f, .y = 0.5f, .active = true };
t->item.active = false;
}
} else {
t->item.active = false;
}
}
}
}
}
void renderBelt(SDL_Texture *tex, int x, int y, int w, int h, BeltDirection dir) {
int texW, texH;
SDL_QueryTexture(tex, NULL, NULL, &texW, &texH);
static int scrollFrame = 0;
int scrollSpeed = 1; // pixels per step
int scrollDelay = 15; // frames between steps
if (frames % scrollDelay == 0) {
scrollFrame += scrollSpeed;
}
SDL_Rect src1, src2, dst1, dst2;
if (dir == BELT_LEFT || dir == BELT_RIGHT) {
int offset = scrollFrame % texW;
if (dir == BELT_LEFT) {
offset = texW - offset; // reverse scroll
}
src1 = (SDL_Rect){offset, 0, texW - offset, texH};
dst1 = (SDL_Rect){x, y, w - offset, h};
src2 = (SDL_Rect){0, 0, offset, texH};
dst2 = (SDL_Rect){x + (w - offset), y, offset, h};
SDL_RenderCopy(renderer, tex, &src1, &dst1);
SDL_RenderCopy(renderer, tex, &src2, &dst2);
} else {
int offset = scrollFrame % texH;
if (dir == BELT_UP) {
offset = texH - offset; // reverse scroll
}
src1 = (SDL_Rect){0, offset, texW, texH - offset};
dst1 = (SDL_Rect){x, y, w, h - offset};
src2 = (SDL_Rect){0, 0, texW, offset};
dst2 = (SDL_Rect){x, y + (h - offset), w, offset};
SDL_RenderCopy(renderer, tex, &src1, &dst1);
SDL_RenderCopy(renderer, tex, &src2, &dst2);
}
}
void renderAllBelts() {
int tileSize = TILE_SIZE;
for (int y = 0; y < MAP_HEIGHT; y++) {
for (int x = 0; x < MAP_WIDTH; x++) {
Tile t = tileMap[y][x];
if (!t.hasBelt) continue;
int px = x * tileSize;
int py = y * tileSize;
renderBelt(beltTex, px, py, tileSize, tileSize, t.direction);
}
}
}
int render() {
SDL_SetRenderDrawColor(renderer, 32, 32, 32, 255);
SDL_RenderClear(renderer);
@@ -278,7 +127,7 @@ int render() {
rect2.w = 0;
rect2.h = 0;
renderAllBelts();
renderAllBelts(renderer);
SDL_RenderPresent(renderer);
@@ -331,6 +180,14 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char *args[])
return status;
}
uint8_t type = 0;
for (int x = 149; x < 152; x++) {
for(int y = 87; y < 90; y++) {
putItem(x, y, type++);
}
}
//Hack to get window to stay up
SDL_Event e;
Uint64 start;
@@ -358,7 +215,6 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char *args[])
for (uint8_t i = 0; i < fontCount; i++) {
destroyFont(&fonts[i]);
}
puts(SDL_GetError());
if (renderer) SDL_DestroyRenderer(renderer);
if (window) SDL_DestroyWindow(window);