/* * Tux-Town is a chill life-simulation game. * Copyright (C) 2025 orangerot * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include #define RCAMERA_IMPLEMENTATION #define RL_CULL_DISTANCE_NEAR 0.01 #define RL_CULL_DISTANCE_FAR 1000.0 #include #include #include "assets.h" #define MIN(a,b) (((a)<(b))?(a):(b)) #define MAX(a,b) (((a)>(b))?(a):(b)) #define CLAMP2(val, min, max) MIN(max, MAX(min, val)) #define CLAMP(val, min, max) ((val) < (min) ? (min) : ((val) > (max) ? (max) : (val))) #define LROT(v,n) ((v << n) | (v >> (sizeof(v)*8 - n))) #define RROT(v,n) ((v >> n) | (v << (sizeof(v)*8 - n))) #define MAP_SIZE 64 struct ModelDirection { enum Asset asset; unsigned char pattern; }; void generate_river(Color *map_data, int previous) { int x = previous % MAP_SIZE, y = previous / MAP_SIZE; if (x == 0 || x == MAP_SIZE -1 || y == 0 || y == MAP_SIZE -1) return; int local_minimum_map_i = previous; int local_minimum_val = 255; int gradients[4][2] = {{0,-1},{-1,0},{0,1},{1,0}}; for (int gradient_i = 0; gradient_i < 4; gradient_i++) { int dx = CLAMP(x + gradients[gradient_i][0], 0, MAP_SIZE - 1); int dy = CLAMP(y + gradients[gradient_i][1], 0, MAP_SIZE - 1); int i = dy * MAP_SIZE + dx; if (i == previous || map_data[i].r == 1) continue; if (map_data[i].b < local_minimum_val) { local_minimum_map_i = i; local_minimum_val = map_data[i].b; } } if (local_minimum_val == 255) return; map_data[local_minimum_map_i].r = 1; generate_river(map_data, local_minimum_map_i); } void select_river_tile(Color *map_data, struct ModelDirection *rivers, int x, int y, size_t *river_i, size_t *direction) { int surrounding[8][2] = {{-1,-1},{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,0}}; unsigned char river_tile = 0; for (int surrounding_i = 0; surrounding_i < 8; surrounding_i++) { int dx = CLAMP(x + surrounding[surrounding_i][0], 0, MAP_SIZE - 1); int dy = CLAMP(y + surrounding[surrounding_i][1], 0, MAP_SIZE - 1); if (map_data[dy * MAP_SIZE + dx].r) river_tile |= 1 << (7 - surrounding_i); } for (*river_i = 0; *river_i < 11; (*river_i)++) { for (*direction = 0; *direction < 4; (*direction)++) { if ((rivers[*river_i].pattern & RROT(river_tile, 2 * *direction)) == rivers[*river_i].pattern) { return; } } } } int main(void) { const int screenWidth = 800; const int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window"); Camera camera = { 0 }; camera.position = (Vector3){ 5.0f, 5.0f, 5.0f }; camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; camera.fovy = 45.0f; camera.projection = CAMERA_PERSPECTIVE; Vector3 player_pos = (Vector3) {0.f, 0.f, 0.f}; struct ModelDirection rivers[] = { /* 0b12345678 * 1 | 2 | 3 * 8 | | 4 * 7 | 6 | 5 */ { .pattern = 0b11111111, .asset = ground_riverOpen }, // edge { .pattern = 0b11011111, .asset = ground_riverCornerSmall }, { .pattern = 0b01011111, .asset = ground_riverSideOpen }, { .pattern = 0b11110001, .asset = ground_riverSide }, { .pattern = 0b01010101, .asset = ground_riverCross }, { .pattern = 0b01010001, .asset = ground_riverSplit }, // STRAIGHT { .pattern = 0b01000100, .asset = ground_riverStraight }, // corner { .pattern = 0b11000001, .asset = ground_riverCorner }, // L SHAPE { .pattern = 0b01000001, .asset = ground_riverBend }, // closed { .pattern = 0b01000000, .asset = ground_riverEndClosed }, { .pattern = 0b00000000, .asset = ground_riverTile }, }; enum Asset ground = ground_grass; enum Asset cliff = cliff_top_rock; enum Asset tree = tree_oak; enum Asset house = tent_detailedOpen; Vector3 position = {0}; Image map = GenImagePerlinNoise(MAP_SIZE, MAP_SIZE, 0, 0, 1.f); Texture2D map_texture = LoadTextureFromImage(map); Color *map_data = LoadImageColors(map); size_t global_minimum_map_i; size_t global_minimum_val = 255; for (size_t i = 0; i < MAP_SIZE * MAP_SIZE; i++) { int x = i % MAP_SIZE, y = i / MAP_SIZE; Color c = map_data[i]; if (c.r < global_minimum_val) { global_minimum_map_i = i; global_minimum_val = map_data[i].b; } map_data[i] = (Color) { .r = 0, .g = MAX(0, c.g - 64) / 32, .b = c.b, .a = 255 }; } map_data[global_minimum_map_i].r = 1; generate_river(map_data, global_minimum_map_i); generate_river(map_data, global_minimum_map_i); LoadModels(); #define NUM_TREES MAP_SIZE * MAP_SIZE / 100 int *trees_x = LoadRandomSequence(NUM_TREES, -MAP_SIZE / 2, MAP_SIZE / 2); int *trees_y = LoadRandomSequence(NUM_TREES, - MAP_SIZE / 2, MAP_SIZE / 2); // SetTargetFPS(60); DisableCursor(); while (!WindowShouldClose()) { Vector2 mousePositionDelta = GetMouseDelta(); // UpdateCamera(&camera, CAMERA_THIRD_PERSON); Vector3 camera_forward = GetCameraForward(&camera); camera.target = Vector3Add(player_pos, (Vector3){0,.2f,0}); camera.position = Vector3Add(camera.target, Vector3Scale(camera_forward, -2.f)); CameraYaw(&camera, -mousePositionDelta.x*CAMERA_MOUSE_MOVE_SENSITIVITY, true); CameraPitch(&camera, -mousePositionDelta.y*CAMERA_MOUSE_MOVE_SENSITIVITY, true, true, false); Vector3 camera_forward_flat = camera_forward; camera_forward_flat.y = 0; camera_forward_flat = Vector3Normalize(camera_forward_flat); float velocity = 1.f * GetFrameTime(); if (IsKeyDown(KEY_UP)) player_pos = Vector3Add(player_pos, Vector3Scale(camera_forward_flat, velocity)); if (IsKeyDown(KEY_DOWN)) player_pos = Vector3Add(player_pos, Vector3Scale(camera_forward_flat, -velocity)); if (IsKeyDown(KEY_RIGHT)) player_pos = Vector3Add(player_pos, Vector3Scale(GetCameraRight(&camera), velocity)); if (IsKeyDown(KEY_LEFT)) player_pos = Vector3Add(player_pos, Vector3Scale(GetCameraRight(&camera), -velocity)); BeginDrawing(); ClearBackground(RAYWHITE); BeginMode3D(camera); for (int i = 0; i < MAP_SIZE * MAP_SIZE; i++) { int x = i % MAP_SIZE, y = i / MAP_SIZE; int gradients[4][2] = {{0,-1},{-1,0},{0,1},{1,0}}; for (int gradient_i = 0; gradient_i < 4; gradient_i++) { int dx = CLAMP(x + gradients[gradient_i][0], 0, MAP_SIZE - 1); int dy = CLAMP(y + gradients[gradient_i][1], 0, MAP_SIZE - 1); for (int height = map_data[i].g; height < map_data[dy * MAP_SIZE + dx].g; height++) { DrawModelEx(assets[cliff], (Vector3){ .x = MAP_SIZE * (x / (float) MAP_SIZE - 0.5f), .y = height, .z = MAP_SIZE * (y / (float) MAP_SIZE - 0.5f) }, (Vector3) {0, 1, 0}, gradient_i * 90.f, (Vector3) {1,1,1}, WHITE); } } size_t river_i, direction; if (map_data[i].r) { select_river_tile(map_data, rivers, x, y, &river_i, &direction); } DrawModelEx(assets[map_data[i].r ? rivers[river_i].asset : ground], (Vector3){ .x = MAP_SIZE * (x / (float) MAP_SIZE - 0.5f), .y = map_data[i].g, //- (map_gradient_magnitude_data[i].g < 2), .z = MAP_SIZE * (y / (float) MAP_SIZE - 0.5f) } , (Vector3) {0,1,0}, map_data[i].r ? direction * 90.f: 0, (Vector3) {1,1,1}, WHITE); } for (int tree_i = 0; tree_i < NUM_TREES; tree_i++) { DrawModel(assets[tree], (Vector3) {trees_x[tree_i], 0, trees_y[tree_i]}, 1.f, WHITE); } DrawModel(assets[house], (Vector3) {-1, 0, 0}, 1.f, WHITE); DrawGrid(20, 10.0f); Vector3 capsule_top = player_pos; capsule_top.y += 0.2f; DrawCapsule(Vector3Add(player_pos, (Vector3){0,.1f,0}), capsule_top, .1f, 8, 8, BLUE); EndMode3D(); DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY); DrawTexture(map_texture, 0, 0, WHITE); EndDrawing(); } CloseWindow(); return 0; }