Start atlas

This commit is contained in:
2025-06-01 22:13:02 +02:00
parent 96a9a45c20
commit 84805b92cb
64 changed files with 954 additions and 243 deletions

258
main.c
View File

@@ -7,28 +7,46 @@
#include "items/item.h"
#include "stdlib.h"
#include "player/player.h"
#include "util/perlin.h"
#include "util/atlas.h"
typedef struct {
typedef struct GameState {
Player player;
Tile tileMap[MAP_HEIGHT][MAP_WIDTH];
BackgroundTile backgroundTileMap[MAP_HEIGHT][MAP_WIDTH];
SynthVoice voices[NUM_SYNTH_VOICES];
} GameState;
GameState gameState;
void loadGameState(char *filename, Player *plr) {
int loadGameState(char *filename, Player *plr) {
printf("hello from load\n");
fflush(stdout);
FILE *gameSave = fopen(filename, "rb");
if (gameSave) {
fseek(gameSave, 0L, SEEK_END);
long sz = ftell(gameSave);
if (sz != sizeof(gameState)) {
return 1;
}
rewind(gameSave);
fread(&gameState, sizeof(gameState), 1, gameSave);
fclose(gameSave);
memcpy(tileMap, gameState.tileMap, sizeof(tileMap));
memcpy(plr, &gameState.player, sizeof(gameState.player));
memcpy(tileMap, gameState.tileMap, sizeof(tileMap));
memcpy(backgroundMap, gameState.backgroundTileMap, sizeof(backgroundMap));
memcpy(audioData.synthVoices, gameState.voices, sizeof(gameState.voices));
plr->cursor.targetTile = NULL;
plr->cursor.prevTargetTile = NULL;
return 0;
}
return 1;
}
void saveGameState(char *filename, Player *plr) {
memcpy(&gameState.player, plr, sizeof(gameState.player));
memcpy(gameState.tileMap, tileMap, sizeof(gameState.tileMap));
memcpy(gameState.backgroundTileMap, backgroundMap, sizeof(gameState.backgroundTileMap));
memcpy(gameState.voices, audioData.synthVoices, sizeof(gameState.voices));
FILE *gameSave = fopen(filename, "wb");
if (!gameSave) {
@@ -51,7 +69,7 @@ const int delayNeeded = 1000 / targetFPS;
#define smallerFont fonts[2]
#define smallestFont fonts[3]
const char *autosaveName = "autosave.dat";
char *autosaveName = "autosave.dat";
unsigned long frames = 0;
@@ -66,6 +84,8 @@ void msleep(unsigned int milliseconds) {
SDL_GLContext glContext;
void genInitMap();
int init() {
//Initialize SDL
@@ -75,7 +95,9 @@ int init() {
screenRect.w = DISPLAY_WIDTH;
screenRect.h = DISPLAY_HEIGHT;
srand(0);
srand(time(NULL));
memset(tileMap, 0, sizeof(tileMap));
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, NULL);
SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "1");
@@ -107,8 +129,17 @@ int init() {
return 1;
}
initAtlas(mainRenderer);
loadTiles(mainRenderer);
loadItems(mainRenderer);
setupTiles();
for (ItemType i = 0; i < ITEMREGISTRY_SIZE; i++) {
if (strlen(ItemRegistry[i].name)) {
printf("%d -> %s\n", i, ItemRegistry[i].name);
}
}
// Create OpenGL context
glContext = SDL_GL_CreateContext(window);
if (!glContext) {
@@ -119,21 +150,24 @@ int init() {
// Use OpenGL context
SDL_GL_MakeCurrent(window, glContext); // Make sure OpenGL context is current before any OpenGL rendering
audioData.playerRect = &player.rect;
audioData.maxPanDistance = DISPLAY_WIDTH / 2;
SDL_AudioSpec spec = {0};
spec.freq = SAMPLE_RATE;
spec.format = AUDIO_F32SYS;
spec.channels = 1;
spec.format = AUDIO_F32;
spec.channels = 2;
spec.samples = 4096;
spec.callback = audio_callback;
spec.userdata = &audioData;
SDL_AudioDeviceID dev = SDL_OpenAudioDevice(NULL, 0, &spec, NULL, 0);
if (dev == 0) {
printf("Failed to open audio: %s\n", SDL_GetError());
SDL_Quit();
}
SDL_PauseAudioDevice(dev, 0);
// SDL_AudioDeviceID dev = SDL_OpenAudioDevice(NULL, 0, &spec, NULL, 0);
// if (dev == 0) {
// printf("Failed to open audio: %s\n", SDL_GetError());
// SDL_Quit();
// }
//
// SDL_PauseAudioDevice(dev, 1);
SDL_SetRenderDrawColor(mainRenderer, 0, 0, 0, 255);
SDL_RenderClear(mainRenderer);
@@ -150,6 +184,10 @@ int init() {
initPlayer(&player);
for (ItemType i = 0; i < 13; i++) {
player.inventory.hotKeySlots[i] = i;
}
initTiles();
//generateTestMap();
@@ -163,20 +201,22 @@ int render() {
SDL_RenderClear(mainRenderer);
SDL_Rect rect2;
rect2.x = 20;
rect2.y = 20;
rect2.x = 0;
rect2.y = 0;
rect2.w = 0;
rect2.h = 0;
renderAllTiles(mainRenderer, player.rect);
SDL_RenderCopy(mainRenderer, tilesTexture, &screenRect, &screenRect);
SDL_RenderCopy(mainRenderer, itemsTexture, &screenRect, &screenRect);
SDL_RenderCopy(mainRenderer, entityTexture, &screenRect, &screenRect);
SDL_RenderCopy(mainRenderer, hudTexture, &screenRect, &screenRect);
renderPlayer(&player);
// SDL_RenderCopy(mainRenderer, backgroundTexture, &screenRect, &screenRect);
// SDL_RenderCopy(mainRenderer, tilesTexture, &screenRect, &screenRect);
// SDL_RenderCopy(mainRenderer, itemsTexture, &screenRect, &screenRect);
// SDL_RenderCopy(mainRenderer, entityTexture, &screenRect, &screenRect);
// SDL_RenderCopy(mainRenderer, hudTexture, &screenRect, &screenRect);
SDL_QueryTexture(atlasTexture, NULL, NULL, &rect2.w, &rect2.h);
SDL_RenderCopy(mainRenderer, atlasTexture, &rect2, &rect2);
SDL_RenderPresent(mainRenderer);
frames++;
@@ -206,11 +246,11 @@ int processEvent(SDL_Event e) {
speed = speed == 0 ? 0.004f : 0;
break;
case SDLK_r:
if (player.cursor.canReach && player.cursor.targetTile->type == TYPE_BELT) {
if (player.cursor.canReach && player.cursor.targetTile->type != TYPE_AIR) {
player.cursor.direction = player.cursor.targetTile->direction;
}
player.cursor.direction = (player.cursor.direction + 2) % ORIENT_DIRECTION_COUNT;
if (player.cursor.canReach && player.cursor.targetTile->type == TYPE_BELT) {
if (player.cursor.canReach) {
player.cursor.targetTile->direction = player.cursor.direction;
}
break;
@@ -220,6 +260,12 @@ int processEvent(SDL_Event e) {
case SDLK_F3:
debugMode = !debugMode;
break;
case SDLK_F4:
Tile *tile = &tileMap[playerTileY][playerTileX];
break;
case SDLK_LALT:
itemViewing = !itemViewing;
break;
default:
break;
}
@@ -274,15 +320,13 @@ void processMousePosition() {
if (player.cursor.targetTile->type < tileTypeIndex) {
player.inventory.slotCounts[player.cursor.targetTile->type]++;
}
if (player.cursor.targetTile->type == TYPE_BELT) {
for (int lane = 0; lane < 2; lane++) {
if (player.cursor.targetTile->items[lane].type != 0) {
int itemType = player.cursor.targetTile->items[lane].type;
if (itemType < itemRegistryIndex) {
player.inventory.slotCounts[itemType]++;
}
player.cursor.targetTile->items[lane].type = 0;
for (int lane = 0; lane < 2; lane++) {
if (player.cursor.targetTile->items[lane].type != 0) {
int itemType = player.cursor.targetTile->items[lane].type;
if (itemType < itemRegistryIndex) {
player.inventory.slotCounts[itemType]++;
}
player.cursor.targetTile->items[lane].type = 0;
}
}
player.cursor.targetTile->type = TYPE_AIR;
@@ -311,6 +355,18 @@ void processMousePosition() {
player.cursor.windowY = (player.cursor.windowY - viewport.y) * DISPLAY_HEIGHT / viewport.h;
player.cursor.tileX = (player.cursor.windowX + player.rect.x) / TILE_SIZE - (DISPLAY_WIDTH / TILE_SIZE / 2);
player.cursor.tileY = (player.cursor.windowY + player.rect.y) / TILE_SIZE - (DISPLAY_HEIGHT / TILE_SIZE / 2);
if (player.cursor.tileX < 0) {
player.cursor.tileX = 0;
}
if (player.cursor.tileY < 0) {
player.cursor.tileY = 0;
}
if (player.cursor.tileX >= MAP_WIDTH) {
player.cursor.tileX = MAP_WIDTH - 1;
}
if (player.cursor.tileY >= MAP_HEIGHT) {
player.cursor.tileY = MAP_HEIGHT - 1;
}
player.cursor.prevTargetTile = player.cursor.targetTile;
player.cursor.targetTile = &tileMap[player.cursor.tileY][player.cursor.tileX];
@@ -329,7 +385,7 @@ void processKeyboardHeld() {
int cameraSpeed = playerSpeed;
if (keyboardState[SDL_SCANCODE_LSHIFT] || keyboardState[SDL_SCANCODE_RSHIFT]) {
cameraSpeed *= 2;
cameraSpeed *= 8;
}
if (keyboardState[SDL_SCANCODE_LCTRL] || keyboardState[SDL_SCANCODE_RCTRL]) {
cameraSpeed /= 2;
@@ -339,39 +395,57 @@ void processKeyboardHeld() {
if (keyboardState[SDL_SCANCODE_W]) {
// Example: move up
player.rect.y -= cameraSpeed;
if (player.rect.y < (DISPLAY_HEIGHT / 2)) {
player.rect.y = (DISPLAY_HEIGHT / 2);
// if (player.rect.y < (DISPLAY_HEIGHT / 2)) {
// player.rect.y = (DISPLAY_HEIGHT / 2);
// }
if (player.rect.y < 0) {
player.rect.y = 0;
}
}
if (keyboardState[SDL_SCANCODE_S]) {
player.rect.y += cameraSpeed;
if (player.rect.y > (MAP_HEIGHT * TILE_SIZE) - (DISPLAY_HEIGHT / 2)) {
player.rect.y = (MAP_HEIGHT * TILE_SIZE) - (DISPLAY_HEIGHT / 2);
// if (player.rect.y > (MAP_HEIGHT * TILE_SIZE) - (DISPLAY_HEIGHT / 2)) {
// player.rect.y = (MAP_HEIGHT * TILE_SIZE) - (DISPLAY_HEIGHT / 2);
// }
if (player.rect.y > (MAP_HEIGHT * TILE_SIZE)) {
player.rect.y = (MAP_HEIGHT * TILE_SIZE);
}
}
if (keyboardState[SDL_SCANCODE_A]) {
player.rect.x -= cameraSpeed;
if (player.rect.x < (DISPLAY_WIDTH / 2)) {
player.rect.x = (DISPLAY_WIDTH / 2);
// if (player.rect.x < (DISPLAY_WIDTH / 2)) {
// player.rect.x = (DISPLAY_WIDTH / 2);
// }
if (player.rect.x < 0) {
player.rect.x = 0;
}
}
if (keyboardState[SDL_SCANCODE_D]) {
player.rect.x += cameraSpeed;
if (player.rect.x > (MAP_WIDTH * TILE_SIZE) - (DISPLAY_WIDTH / 2)) {
player.rect.x = (MAP_WIDTH * TILE_SIZE) - (DISPLAY_WIDTH / 2);
// if (player.rect.x > (MAP_WIDTH * TILE_SIZE) - (DISPLAY_WIDTH / 2)) {
// player.rect.x = (MAP_WIDTH * TILE_SIZE) - (DISPLAY_WIDTH / 2);
// }
if (player.rect.x > (MAP_WIDTH * TILE_SIZE)) {
player.rect.x = (MAP_WIDTH * TILE_SIZE);
}
}
}
if (keyboardState[SDL_SCANCODE_F]) {
for (int x = player.rect.x - 1; x < player.rect.x + 1; player.rect.x++) {
for (int x = playerTileX - 2; x < playerTileX + 2; x++) {
if (x < 0) {
continue;
}
for (int y = player.rect.y - 1; y < player.rect.y + 1; player.rect.y++) {
if (x >= MAP_WIDTH) {
continue;
}
for (int y = playerTileY - 2; y < playerTileY + 2; y++) {
if (y < 0) {
continue;
}
if (y >= MAP_HEIGHT) {
continue;
}
Tile *t = &tileMap[y][x];
if (t->type == TYPE_BELT) {
for (uint8_t lane = 0; lane < 2; lane++) {
@@ -431,8 +505,8 @@ void processKeyboardHeld() {
} else if (keyboardState[SDL_SCANCODE_EQUALS]) {
slot = 13;
}
if (slot > 0 && slot < itemRegistryIndex) {
setActivePlayerSlot(&player, slot);
if (slot > 0 && slot < sizeof(player.inventory.hotKeySlots) / sizeof(player.inventory.hotKeySlots[0])) {
setActivePlayerSlot(&player, player.inventory.hotKeySlots[slot]);
}
}
@@ -441,7 +515,9 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char *args[])
if (status) {
return status;
}
loadGameState(autosaveName, &player);
if (loadGameState(autosaveName, &player)) {
genInitMap();
}
//Hack to get window to stay up
@@ -460,6 +536,7 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char *args[])
updateItems();
updatePlayer(&player);
updateTiles();
status = render();
if (status) {
return status;
@@ -485,4 +562,91 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char *args[])
SDL_Quit();
return 0;
}
}
// Main generator:
void genInitMap() {
const double terrainScale = 2;
const double humidityScale = 1;
const double oreScale = 2;
const int terrainOct = 4;
const int humidityOct = 4;
const int oreOct = 4;
int seedX = rand();
int seedY = rand();
int seedN = rand();
// Min/max trackers
double terrainMin = 1e9, terrainMax = -1e9;
double humidityMin = 1e9, humidityMax = -1e9;
double oreNrmMin = 1e9, oreNrmMax = -1e9;
for (uint16_t y = 0; y < MAP_HEIGHT; y++) {
for (uint16_t x = 0; x < MAP_WIDTH; x++) {
double terrain = pnoise2d(x + 1000 + seedX,
y + 1000 + seedY,
terrainScale, terrainOct, seedN);
double humidity = pnoise2d(x + seedX,
y + seedY,
humidityScale, humidityOct, seedN);
double oreNrm = pnoise2d(x + 9999 + seedX,
y + 1111 + seedY,
oreScale, oreOct, seedN);
// Track min/max
if (terrain < terrainMin) terrainMin = terrain;
if (terrain > terrainMax) terrainMax = terrain;
if (humidity < humidityMin) humidityMin = humidity;
if (humidity > humidityMax) humidityMax = humidity;
if (oreNrm < oreNrmMin) oreNrmMin = oreNrm;
if (oreNrm > oreNrmMax) oreNrmMax = oreNrm;
// [Same as your original terrain generation logic...]
BackgroundType baseType;
if (terrain < 0.30) {
baseType = (humidity < 0.5) ? BGType_WATER0 : BGType_WATER1;
} else if (terrain < 0.35) {
if (humidity < 0.3) baseType = BGType_SAND4;
else if (humidity < 0.6) baseType = BGType_SAND2;
else baseType = BGType_SAND7;
} else if (terrain < 0.7) {
double grassVal = (terrain - 0.35) / (0.70 - 0.35);
int idx = (int) (grassVal * 8.0);
if (idx >= 8) idx = 7;
if (humidity > 0.6 && ((rand() & 0xFF) < 10)) {
int flowerIdx = rand() % 4;
baseType = (BackgroundType) (BGType_GRASS_FLOWER0 + flowerIdx);
} else {
baseType = (BackgroundType) (BGType_GRASS0 + idx);
}
} else if (terrain < 0.85) {
int idx = rand() % 4;
baseType = (BackgroundType) (BGType_COBBLE0 + idx);
} else {
int idx = rand() % 4;
baseType = (BackgroundType) (BGType_TILES0 + idx);
}
BackgroundType finalType = baseType;
if (baseType != BGType_WATER0 && baseType != BGType_WATER1) {
if (oreNrm > 0.86) {
double sub = (oreNrm - 0.86) / (1.0 - 0.86);
if (sub < 0.25) finalType = BGType_PLATINUM_ORE;
else if (sub < 0.50) finalType = BGType_GOLD_ORE;
else if (sub < 0.80) finalType = BGType_SILVER_ORE;
else finalType = BGType_IRON_ORE;
}
}
backgroundMap[y][x].type = finalType;
}
}
// Debug printout
printf("Terrain Noise: min = %f, max = %f\n", terrainMin, terrainMax);
printf("Humidity Noise: min = %f, max = %f\n", humidityMin, humidityMax);
printf("Ore Normalized: min = %f, max = %f\n", oreNrmMin, oreNrmMax);
}