// 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" #include "chunkIO.h" #include "world.h" int main(void) { // --- Screen setup --- SetConfigFlags(FLAG_WINDOW_RESIZABLE); InitWindow(800, 600, "voxelThing"); SetExitKey(-1); DisableCursor(); // --- World generation --- World world; InitWorld(&world); // --- 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_STONE; 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, &world, 10.0f); if (hit.hit) { int bx = hit.hitBlockX; int by = hit.hitBlockY; int bz = hit.hitBlockZ; int placeX = bx + (int)hit.normal.x; int placeY = by + (int)hit.normal.y; int placeZ = bz + (int)hit.normal.z; // Handle removal if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) { Chunk *targetChunk = GetChunkContainingBlock(&world, bx, bz); if (targetChunk) { int lx = bx - targetChunk->x * CHUNK_SIZE_X; int lz = bz - targetChunk->z * CHUNK_SIZE_Z; if (lx >= 0 && lx < CHUNK_SIZE_X && lz >= 0 && lz < CHUNK_SIZE_Z && by >= 0 && by < CHUNK_SIZE_Y) { targetChunk->blocks[lx][by][lz].type = BLOCK_AIR; UnloadMesh(targetChunk->mesh); targetChunk->mesh = GenerateChunkMesh(targetChunk); // Mark chunk as changed. targetChunk->hasChanged = true; } } } // Handle placement if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)) { Chunk *targetChunk = GetChunkContainingBlock(&world, placeX, placeZ); if (targetChunk) { int lx = placeX - targetChunk->x * CHUNK_SIZE_X; int lz = placeZ - targetChunk->z * CHUNK_SIZE_Z; if (lx >= 0 && lx < CHUNK_SIZE_X && lz >= 0 && lz < CHUNK_SIZE_Z && placeY >= 0 && placeY < CHUNK_SIZE_Y) { targetChunk->blocks[lx][placeY][lz].type = blockSelection; UnloadMesh(targetChunk->mesh); targetChunk->mesh = GenerateChunkMesh(targetChunk); // Mark chunk as changed. targetChunk->hasChanged = true; } } } } BeginDrawing(); ClearBackground(RAYWHITE); BeginMode3D(player.camera); //DrawMesh(chunkMesh, mat, MatrixIdentity()); for (int x = 0; x < WORLD_SIZE_X; x++) { for (int z = 0; z < WORLD_SIZE_Z; z++) { Chunk *chunk = world.chunks[x][z]; if (chunk && chunk->mesh.vertexCount > 0) { DrawMesh(chunk->mesh, mat, MatrixTranslate(x * CHUNK_SIZE_X, 0, z * CHUNK_SIZE_Z)); } } } if (hit.hit) { Vector3 mid = Vector3Add((Vector3){hit.hitBlockX, hit.hitBlockY, hit.hitBlockZ}, (Vector3){0.5f, 0.5f, 0.5f}); DrawCubeWires(mid, 1.02f, 1.02f, 1.02f, BLACK); //DrawLine3D(mid, Vector3Add(mid, hit.normal), RED); // Normal direction } 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 --- UnloadTexture(atlas); FreeWorld(&world); UnloadMaterial(mat); CloseWindow(); return 0; }