voxelThing/source/voxelThing.c

127 lines
4.2 KiB
C

// voxelThing.c (cleaned-up main loop)
#include "raylib.h"
#include "raymath.h"
#include "rlgl.h"
#include "stdio.h"
#include "chunkStructures.h"
#include "chunkRenderer.h"
#include "blockTypes.h"
#include "playerController.h"
#include "chunkGenerator.h"
int main(void) {
// --- Screen setup ---
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(800, 600, "VoxelThing");
SetExitKey(-1);
DisableCursor();
// --- World generation ---
Chunk chunk;
if (!LoadChunk(&chunk, "saves/chunk_0_0_0.dat")) {
// There was no save, gotta generate a fresh chunk to play with.
printf("--- WORLDGEN--- No save, creating new chunk.\n");
GenerateFlatChunk(&chunk);
PlaceTreeAt(&chunk, 8, 64, 8);
SaveChunk(&chunk, "saves/chunk_0_0_0.dat");
}
Mesh chunkMesh = GenerateChunkMesh(&chunk);
// --- Load textures and materials ---
Texture2D atlas = LoadTexture("assets/TextureAtlas.png");
Material mat = LoadMaterialDefault();
mat.maps[MATERIAL_MAP_DIFFUSE].texture = atlas;
// --- Player setup ---
Player player = {
.mapPosition = (Vector3){ 1.0f, 67.0f, 1.0f },
.playerOrientation = (Vector2){ 0.0f, 0.0f },
.moveSpeed = 10.0f
};
player.camera.fovy = 90.0f;
player.camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
player.camera.projection = CAMERA_PERSPECTIVE;
SetTargetFPS(60);
bool paused = false;
RaycastHit hit;
int blockSelection = BLOCK_SAND;
while (!WindowShouldClose()) {
int screenWidth = GetScreenWidth();
int screenHeight = GetScreenHeight();
// Toggle fullscreen
if (IsKeyPressed(KEY_F11)) {
ToggleFullscreen();
SetMousePosition(screenWidth / 2, screenHeight / 2);
}
// Pause toggle
if (IsKeyPressed(KEY_ESCAPE)) {
paused = !paused;
paused ? EnableCursor() : DisableCursor();
}
// Temporary block selection hotkeys
if (IsKeyPressed(KEY_ONE)) blockSelection = BLOCK_STONE;
if (IsKeyPressed(KEY_TWO)) blockSelection = BLOCK_DIRT;
if (IsKeyPressed(KEY_THREE)) blockSelection = BLOCK_GRASS;
if (IsKeyPressed(KEY_FOUR)) blockSelection = BLOCK_SAND;
if (IsKeyPressed(KEY_FIVE)) blockSelection = BLOCK_GRAVEL;
if (IsKeyPressed(KEY_SIX)) blockSelection = BLOCK_LOG;
if (IsKeyPressed(KEY_SEVEN)) blockSelection = BLOCK_LEAF;
if (IsKeyPressed(KEY_EIGHT)) blockSelection = BLOCK_PLANK;
if (!paused) {
UpdatePlayer(&player);
hit = GetPlayerRaycastHit(&player, &chunk, 10.0f);
HandleBlockInteraction(&player, &chunk, hit, blockSelection);
if (hit.hit && (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) || IsMouseButtonPressed(MOUSE_RIGHT_BUTTON))) {
UnloadMesh(chunkMesh);
chunkMesh = GenerateChunkMesh(&chunk);
}
}
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(player.camera);
DrawMesh(chunkMesh, mat, MatrixIdentity());
if (hit.hit) {
DrawCubeWires(Vector3Add(hit.position, (Vector3){0.5f, 0.5f, 0.5f}), 1.02f, 1.02f, 1.02f, BLACK);
//DrawFaceHighlight(hit.position, hit.normal);
}
EndMode3D();
// Draw crosshair
DrawLine(screenWidth / 2 - 5, screenHeight / 2, screenWidth / 2 + 5, screenHeight / 2, DARKGRAY);
DrawLine(screenWidth / 2, screenHeight / 2 - 5, screenWidth / 2, screenHeight / 2 + 5, DARKGRAY);
// Debug info
DrawText(TextFormat("Yaw: %.1f Pitch: %.1f", player.playerOrientation.x * RAD2DEG, player.playerOrientation.y * RAD2DEG), 10, 10, 20, DARKGRAY);
if (paused) {
DrawRectangle(0, 0, screenWidth, screenHeight, Fade(DARKGRAY, 0.5f));
DrawText("Paused", screenWidth / 2 - MeasureText("Paused", 40) / 2, screenHeight / 2 - 20, 40, RAYWHITE);
DrawText("Press ESC to resume", screenWidth / 2 - MeasureText("Press ESC to resume", 20) / 2, screenHeight / 2 + 30, 20, LIGHTGRAY);
}
EndDrawing();
}
// --- Cleanup ---
SaveChunk(&chunk, "saves/chunk_0_0_0.dat");
UnloadTexture(atlas);
UnloadMesh(chunkMesh);
UnloadMaterial(mat);
CloseWindow();
return 0;
}