1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
/*
* 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 <raymath.h>
#include <stdbool.h>
#define RCAMERA_IMPLEMENTATION
#define RL_CULL_DISTANCE_NEAR 0.01
#define RL_CULL_DISTANCE_FAR 1000.0
#include <rcamera.h>
#include <stddef.h>
#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;
}
|