From f13cac1a0d67418dbce84ad4baad59af3327117b Mon Sep 17 00:00:00 2001 From: Orangerot Date: Thu, 12 Jun 2025 00:51:42 +0200 Subject: [PATCH] feat: draw world bounds and add world_room --- assets.h | 42 +++++++++++++++++++++++------------------- main.c | 13 +++++++++---- world.c | 23 ++++++++++++++++------- world.h | 19 +++++++++++++++++++ 4 files changed, 67 insertions(+), 30 deletions(-) diff --git a/assets.h b/assets.h index d5de588..ca9deae 100644 --- a/assets.h +++ b/assets.h @@ -21,25 +21,29 @@ #ifndef ASSETS_H #define ASSETS_H -#define ASSET_PATH "assets/kenney_nature-kit/Models/OBJ format/" -#define ASSETS(ASSET) \ - ASSET(ground_riverOpen) \ - ASSET(ground_riverCornerSmall) \ - ASSET(ground_riverSideOpen) \ - ASSET(ground_riverSide) \ - ASSET(ground_riverCross) \ - ASSET(ground_riverSplit) \ - ASSET(ground_riverStraight) \ - ASSET(ground_riverCorner) \ - ASSET(ground_riverBend) \ - ASSET(ground_riverEndClosed) \ - ASSET(ground_riverTile) \ - ASSET(ground_grass) \ - ASSET(cliff_top_rock) \ - ASSET(tree_oak) \ - ASSET(tent_detailedOpen) +#define ASSET_PATH_NATURE "assets/kenney_nature-kit/Models/OBJ format/" +#define ASSET_PATH_FURNITURE "assets/kenney_furniture-kit/Models/OBJ format/" -#define AS_ENUM(name) name, +#define ASSETS(ASSET) \ + ASSET(ASSET_PATH_NATURE, ground_riverOpen) \ + ASSET(ASSET_PATH_NATURE, ground_riverCornerSmall) \ + ASSET(ASSET_PATH_NATURE, ground_riverSideOpen) \ + ASSET(ASSET_PATH_NATURE, ground_riverSide) \ + ASSET(ASSET_PATH_NATURE, ground_riverCross) \ + ASSET(ASSET_PATH_NATURE, ground_riverSplit) \ + ASSET(ASSET_PATH_NATURE, ground_riverStraight) \ + ASSET(ASSET_PATH_NATURE, ground_riverCorner) \ + ASSET(ASSET_PATH_NATURE, ground_riverBend) \ + ASSET(ASSET_PATH_NATURE, ground_riverEndClosed) \ + ASSET(ASSET_PATH_NATURE, ground_riverTile) \ + ASSET(ASSET_PATH_NATURE, ground_grass) \ + ASSET(ASSET_PATH_NATURE, cliff_top_rock) \ + ASSET(ASSET_PATH_NATURE, tree_oak) \ + ASSET(ASSET_PATH_NATURE, tent_detailedOpen) \ + ASSET(ASSET_PATH_FURNITURE, floorFull) \ + ASSET(ASSET_PATH_FURNITURE, wall) + +#define AS_ENUM(path, name) name, enum Asset { ASSETS(AS_ENUM) ASSET_LEN @@ -48,7 +52,7 @@ enum Asset { #ifdef ASSET_IMPLEMENTATION Model assets[ASSET_LEN]; -#define AS_ARRAY(name) assets[name] = LoadModel(ASSET_PATH #name ".obj"); +#define AS_ARRAY(path, name) assets[name] = LoadModel(path #name ".obj"); void LoadModels() { ASSETS(AS_ARRAY) } diff --git a/main.c b/main.c index 606f575..636c812 100644 --- a/main.c +++ b/main.c @@ -49,8 +49,6 @@ int main(void) { enum Asset tree = tree_oak; enum Asset house = tent_detailedOpen; - Vector3 position = {0}; - LoadModels(); struct World world_terrain = { @@ -59,7 +57,14 @@ int main(void) { .size = 32 }; + struct World world_room = { + .floor = assets[floorFull], + .wall = assets[wall], + .size = 8 + }; + gen_terrain(&world_terrain); + gen_room(&world_room); // #define NUM_TREES MAP_SIZE * MAP_SIZE / 100 // int *trees_x = LoadRandomSequence(NUM_TREES, -MAP_SIZE / 2, MAP_SIZE / 2); @@ -88,12 +93,12 @@ int main(void) { BeginDrawing(); ClearBackground(RAYWHITE); BeginMode3D(camera); - draw_world(&world_terrain); + draw_world(IsKeyDown(KEY_SPACE) ? &world_room : &world_terrain); // 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); + DrawGrid(20, 1.f); Vector3 capsule_top = player_pos; capsule_top.y += 0.2f; DrawCapsule(Vector3Add(player_pos, (Vector3){0,.1f,0}), capsule_top, .1f, 8, 8, BLUE); diff --git a/world.c b/world.c index 3b7c6ba..d8d925e 100644 --- a/world.c +++ b/world.c @@ -136,9 +136,13 @@ void gen_terrain(struct World *world) { generate_river(world, global_minimum_map_i); } -// void gen_room() { -// -// } +void gen_room(struct World *world) { + int map_size = world->size; + + world->map = GenImageColor(map_size, map_size, BLACK); + world->map_texture = LoadTextureFromImage(world->map); + world->map_data = LoadImageColors(world->map); +} // // void unload_world() {} @@ -152,13 +156,18 @@ void draw_world(struct World *world) { 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++) { + int dx = x + gradients[gradient_i][0]; + int dy = y + gradients[gradient_i][1]; + int is_border = (dx < 0 || dx >= map_size || dy < 0 || dy >= map_size); + + dx = CLAMP(dx, 0, map_size - 1); + dy = CLAMP(dy, 0, map_size - 1); + int height = MAX(map_data[dy * map_size + dx].g, is_border); + for (int step = map_data[i].g; step < height; step++) { DrawModelEx(wall, (Vector3){ .x = map_size * (x / (float) map_size - 0.5f), - .y = height, + .y = step, .z = map_size * (y / (float) map_size - 0.5f) }, (Vector3) {0, 1, 0}, gradient_i * 90.f, (Vector3) {1,1,1}, WHITE); diff --git a/world.h b/world.h index 2740887..0a88cf4 100644 --- a/world.h +++ b/world.h @@ -1,3 +1,21 @@ +/* + * 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 "assets.h" #include @@ -16,6 +34,7 @@ struct World { }; void gen_terrain(struct World *world); +void gen_room(struct World *world); void draw_world(struct World *world); #endif