Compare commits

...

3 commits

Author SHA1 Message Date
Orangerot fe42371c8c refactor: move terrain generation and drawing to own file 2025-06-05 03:43:25 +02:00
Orangerot af5ee99682 feat: asset loader 2025-06-05 01:40:21 +02:00
Orangerot 603f5da7b7 feat: trees (WIP) 2025-06-05 00:13:22 +02:00
5 changed files with 290 additions and 170 deletions

View file

@ -1,3 +1,3 @@
tuxtown: main.c tuxtown: main.c assets.h world.c world.h
gcc -ggdb -Wall -Wextra -lraylib -lm -o tuxtown main.c gcc -ggdb -Wall -Wextra -lraylib -lm -o tuxtown main.c world.c

60
assets.h Normal file
View file

@ -0,0 +1,60 @@
/*
* Tux-Town is a chill life-simulation game.
* Copyright (C) 2025 orangerot <me@orangerot.dev>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#include <raylib.h>
#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 AS_ENUM(name) name,
enum Asset {
ASSETS(AS_ENUM)
ASSET_LEN
};
#ifdef ASSET_IMPLEMENTATION
Model assets[ASSET_LEN];
#define AS_ARRAY(name) assets[name] = LoadModel(ASSET_PATH #name ".obj");
void LoadModels() {
ASSETS(AS_ARRAY)
}
#else
extern Model assets[];
#endif
#endif /* ASSETS_H */

194
main.c
View file

@ -16,70 +16,18 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include "raylib.h" #include <raylib.h>
#include <raymath.h> #include <raymath.h>
#include <stdbool.h> #include <stdbool.h>
#define RCAMERA_IMPLEMENTATION #define RCAMERA_IMPLEMENTATION
#define RL_CULL_DISTANCE_NEAR 0.01 #define RL_CULL_DISTANCE_NEAR 0.01
#define RL_CULL_DISTANCE_FAR 1000.0 #define RL_CULL_DISTANCE_FAR 1000.0
#include "rcamera.h" #include <rcamera.h>
#include <stddef.h> #include <stddef.h>
#define MIN(a,b) (((a)<(b))?(a):(b)) #define ASSET_IMPLEMENTATION
#define MAX(a,b) (((a)>(b))?(a):(b)) #include "assets.h"
#define CLAMP2(val, min, max) MIN(max, MAX(min, val)) #include "world.h"
#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 {
Model model;
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) { int main(void) {
const int screenWidth = 800; const int screenWidth = 800;
@ -97,91 +45,25 @@ int main(void) {
Vector3 player_pos = (Vector3) {0.f, 0.f, 0.f}; Vector3 player_pos = (Vector3) {0.f, 0.f, 0.f};
struct ModelDirection rivers[] = {
/* 0b12345678 enum Asset tree = tree_oak;
* 1 | 2 | 3 enum Asset house = tent_detailedOpen;
* 8 | | 4
* 7 | 6 | 5
*/
{
.model = LoadModel("assets/kenney_nature-kit/Models/OBJ format/ground_riverOpen.obj"),
.pattern = 0b11111111
},
// edge
{
.model = LoadModel("assets/kenney_nature-kit/Models/OBJ format/ground_riverCornerSmall.obj"),
.pattern = 0b11011111
},
{
.model = LoadModel("assets/kenney_nature-kit/Models/OBJ format/ground_riverSideOpen.obj"),
.pattern = 0b01011111
},
{
.model = LoadModel("assets/kenney_nature-kit/Models/OBJ format/ground_riverSide.obj"),
.pattern = 0b11110001
},
{
.model = LoadModel("assets/kenney_nature-kit/Models/OBJ format/ground_riverCross.obj"),
.pattern = 0b01010101
},
{
.model = LoadModel("assets/kenney_nature-kit/Models/OBJ format/ground_riverSplit.obj"),
.pattern = 0b01010001
},
// STRAIGHT
{
.model = LoadModel("assets/kenney_nature-kit/Models/OBJ format/ground_riverStraight.obj"),
.pattern = 0b01000100
},
// corner
{
.model = LoadModel("assets/kenney_nature-kit/Models/OBJ format/ground_riverCorner.obj"),
.pattern = 0b11000001
},
// L SHAPE
{
.model = LoadModel("assets/kenney_nature-kit/Models/OBJ format/ground_riverBend.obj"),
.pattern = 0b01000001
},
// closed
{
.model = LoadModel("assets/kenney_nature-kit/Models/OBJ format/ground_riverEndClosed.obj"),
.pattern = 0b01000000
},
{
.model = LoadModel("assets/kenney_nature-kit/Models/OBJ format/ground_riverTile.obj"),
.pattern = 0b00000000
},
};
Model ground = LoadModel("assets/kenney_nature-kit/Models/OBJ format/ground_grass.obj");
Model cliff = LoadModel("assets/kenney_nature-kit/Models/OBJ format/cliff_top_rock.obj");
Vector3 position = {0}; 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++) { LoadModels();
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; struct World world_terrain = {
generate_river(map_data, global_minimum_map_i); .floor = assets[ground_grass],
generate_river(map_data, global_minimum_map_i); .wall = assets[cliff_top_rock],
.size = 32
};
gen_terrain(&world_terrain);
// #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); // SetTargetFPS(60);
DisableCursor(); DisableCursor();
@ -206,42 +88,18 @@ int main(void) {
BeginDrawing(); BeginDrawing();
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
BeginMode3D(camera); BeginMode3D(camera);
for (int i = 0; i < MAP_SIZE * MAP_SIZE; i++) { draw_world(&world_terrain);
int x = i % MAP_SIZE, y = i / MAP_SIZE; // for (int tree_i = 0; tree_i < NUM_TREES; tree_i++) {
int gradients[4][2] = {{0,-1},{-1,0},{0,1},{1,0}}; // DrawModel(assets[tree], (Vector3) {trees_x[tree_i], 0, trees_y[tree_i]}, 1.f, WHITE);
for (int gradient_i = 0; gradient_i < 4; gradient_i++) { // }
int dx = CLAMP(x + gradients[gradient_i][0], 0, MAP_SIZE - 1); DrawModel(assets[house], (Vector3) {-1, 0, 0}, 1.f, WHITE);
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(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(map_data[i].r ? rivers[river_i].model : 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);
}
DrawGrid(20, 10.0f); DrawGrid(20, 10.0f);
Vector3 capsule_top = player_pos; Vector3 capsule_top = player_pos;
capsule_top.y += 0.2f; capsule_top.y += 0.2f;
DrawCapsule(Vector3Add(player_pos, (Vector3){0,.1f,0}), capsule_top, .1f, 8, 8, BLUE); DrawCapsule(Vector3Add(player_pos, (Vector3){0,.1f,0}), capsule_top, .1f, 8, 8, BLUE);
EndMode3D(); EndMode3D();
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY); DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
DrawTexture(map_texture, 0, 0, WHITE); DrawTexture(world_terrain.map_texture, 0, 0, WHITE);
EndDrawing(); EndDrawing();
} }

181
world.c Normal file
View file

@ -0,0 +1,181 @@
/*
* Tux-Town is a chill life-simulation game.
* Copyright (C) 2025 orangerot <me@orangerot.dev>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#include <raylib.h>
#include <stddef.h>
#include "assets.h"
#include "world.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;
};
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 },
};
void generate_river(struct World *world, int previous) {
Color *map_data = world->map_data;
int map_size = world->size;
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(world, local_minimum_map_i);
}
void select_river_tile(struct World *world, int x, int y, size_t *river_i, size_t *direction) {
Color *map_data = world->map_data;
int map_size = world->size;
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;
}
}
}
}
void gen_terrain(struct World *world) {
int map_size = world->size;
size_t global_minimum_map_i;
size_t global_minimum_val = 255;
world->map = GenImagePerlinNoise(map_size, map_size, 0, 0, 1.f);
world->map_texture = LoadTextureFromImage(world->map);
world->map_data = LoadImageColors(world->map);
for (size_t i = 0; i < map_size * map_size; i++) {
int x = i % map_size, y = i / map_size;
Color c = world->map_data[i];
if (c.r < global_minimum_val) {
global_minimum_map_i = i;
global_minimum_val = world->map_data[i].b;
}
world->map_data[i] = (Color) {
.r = 0,
.g = MAX(0, c.g - 64) / 32,
.b = c.b,
.a = 255
};
}
world->map_data[global_minimum_map_i].r = 1;
generate_river(world, global_minimum_map_i);
generate_river(world, global_minimum_map_i);
}
// void gen_room() {
//
// }
//
// void unload_world() {}
void draw_world(struct World *world) {
int map_size = world->size;
Color *map_data = world->map_data;
Model wall = world->wall;
Model ground = world->floor;
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(wall,
(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(world, x, y, &river_i, &direction);
}
DrawModelEx(map_data[i].r ? assets[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);
}
}

21
world.h Normal file
View file

@ -0,0 +1,21 @@
#include <raylib.h>
#include "assets.h"
#include <stddef.h>
#ifndef WORLD_H
#define WORLD_H
struct World {
Image map;
Texture map_texture;
Color *map_data;
size_t size;
Model floor;
Model wall;
struct Decoration {} decoration[256];
};
void gen_terrain(struct World *world);
void draw_world(struct World *world);
#endif