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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
// ZiRC, a Zig raycaster for fun
//
// Copyright (C) 2021, tslil clingman
//
// 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/>.
const sf = struct {
usingnamespace @import("sfml");
usingnamespace sf.graphics;
usingnamespace sf.window;
};
const isKeyPressed = sf.window.keyboard.isKeyPressed;
const std = @import("std");
const constants = @import("constants.zig");
const player = @import("player.zig");
const render = @import("render.zig");
const level = @import("level.zig");
pub fn main() !void {
const PI = std.math.pi;
// Setup player and map
//--------------------------------------------------------------------------
var renderer = render.Renderer(constants.PlaneWidth, constants.PlaneHeight).new();
var plyr = player.Player.new(
7, // x coord
1.5, // y coord
PI / 2.0, // angle
);
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
var map = try level.Map.new(16, 16, &gpa.allocator);
defer map.deinit();
var y: u32 = 5;
while (y < 10) : (y += 1) {
// HACK
const vert_coords0 = [_][2]f32{
.{ 0.00, 0.00 },
.{ 1.00, 0.00 },
.{ 1.00, 1.00 },
.{ 0.00, 1.00 },
.{ 0.00, 0.00 },
};
var verts = try std.BoundedArray([2]f32, level.Layer.MAX_VERTS).fromSlice(vert_coords0[0..]);
var layer = level.Layer{ .height = 0.5, .vertices = verts };
try map.cells.items[07 * 16 + y].lower_layers.append(layer);
const vert_coords1 = [_][2]f32{
.{ 0.75, 0.5 },
.{ 0.72838634, 0.60168415 },
.{ 0.66728264, 0.68578625 },
.{ 0.57725424, 0.7377641 },
.{ 0.47386786, 0.74863046 },
.{ 0.375, 0.71650636 },
.{ 0.29774573, 0.6469463 },
.{ 0.2554631, 0.5519779 },
.{ 0.25546312, 0.44802207 },
.{ 0.29774576, 0.35305366 },
.{ 0.37500003, 0.28349364 },
.{ 0.47386792, 0.25136954 },
.{ 0.5772543, 0.26223588 },
.{ 0.6672827, 0.3142138 },
.{ 0.7283864, 0.39831588 },
.{ 0.75, 0.50 },
};
verts = try std.BoundedArray([2]f32, level.Layer.MAX_VERTS).fromSlice(vert_coords1[0..]);
layer = level.Layer{ .height = 1, .vertices = verts, .vertical_texture = 3 };
try map.cells.items[07 * 16 + y].lower_layers.append(layer);
// map.cells.items[y * 16 + 15] = level.Cell{ .floor_height = level.Cell.DEFAULT_HEIGHT, .lower_texture = 2 };
// map.cells.items[15 * 16 + y] = level.Cell{ .floor_height = level.Cell.DEFAULT_HEIGHT };
// map.cells.items[00 * 16 + y] = level.Cell{ .floor_height = level.Cell.DEFAULT_HEIGHT };
// if (y < 15) {
// map.cells.items[y * 16 + 01] = level.Cell{
// .ceiling_height = 3,
// .upper_texture = 3,
// .draw_down = true,
// };
// }
}
// map.cells.items[16 * 7 + 7] = level.Cell{
// .floor_height = 0.2,
// .ceiling_height = 2,
// .lower_texture = 1,
// .upper_texture = 1,
// .floor_texture = 1,
// .ceiling_texture = 2,
// .draw_down = true,
// .vertices = &[_][2]f32{
// .{ 0.00, 0.50 },
// .{ 0.25, 0.66 },
// .{ 0.25, 1.00 },
// .{ 0.50, 0.66 },
// .{ 0.75, 1.00 },
// .{ 0.75, 0.66 },
// .{ 1.00, 0.50 },
// .{ 0.75, 0.33 },
// .{ 0.75, 0.00 },
// .{ 0.50, 0.33 },
// .{ 0.25, 0.00 },
// .{ 0.25, 0.33 },
// .{ 0.00, 0.50 },
// },
// };
try map.objects.append(level.Object{ .pos_x = 1.5, .pos_y = 7.5, .pos_z = 2, .texture = 2, .height = 1, .width = 1 });
try map.objects.append(level.Object{ .pos_x = 4, .pos_y = 8.5, .texture = 0, .height = 1, .width = 1 });
try map.objects.append(level.Object{ .pos_x = 6, .pos_y = 8.5, .texture = 1, .height = 1, .width = 1 });
// Initialise SFML
//--------------------------------------------------------------------------
var window = try sf.RenderWindow.create(.{ .x = constants.ScreenWidth, .y = constants.ScreenHeight }, 32, "zirc", sf.window.Style.none);
defer window.destroy();
window.setFramerateLimit(40);
// Load the background skybox, really `skycylinder', and assign it to a
// sprite -- it's set to repeating so that it goes on forever. We need to
// scale the sprite so that drawing 1/4 of backTextureWidth fills the Screen
// horizontally, and drawing backTextureHeight fills half of the Screen
// vertically.
var back_texture = try sf.Texture.createFromFile("back.png");
defer back_texture.destroy();
back_texture.setSmooth(false); // looks worse => better!
back_texture.setRepeated(true);
var back_sprite = try sf.Sprite.createFromTexture(back_texture);
defer back_sprite.destroy();
back_sprite.setScale(.{ .x = 4.0 * constants.ScreenWidth / constants.BackTextureWidth, .y = constants.ScreenHeight / constants.BackTextureHeight });
// Load wall textures
var walls_image = try sf.Image.createFromFile("walls.png");
defer walls_image.destroy();
// Load sprite textures
var objects_image = try sf.Image.createFromFile("objects.png");
defer objects_image.destroy();
// Load floor image
var surfaces_image = try sf.Image.createFromFile("surfaces.png");
defer surfaces_image.destroy();
var rendered_surfaces_texture = try sf.Texture.create(.{ .x = constants.PlaneWidth, .y = constants.PlaneHeight });
defer rendered_surfaces_texture.destroy();
rendered_surfaces_texture.setSmooth(false);
var rendered_surfaces_sprite = try sf.Sprite.createFromTexture(rendered_surfaces_texture);
defer rendered_surfaces_sprite.destroy();
rendered_surfaces_sprite.setPosition(.{ .x = 0, .y = 0 });
rendered_surfaces_sprite.setScale(.{ .x = constants.HFact, .y = constants.VFact });
// Start your engines!
//--------------------------------------------------------------------------
while (window.isOpen()) {
while (window.pollEvent()) |event| {
if (event == .closed) window.close();
}
if (isKeyPressed(.Escape)) window.close();
if (isKeyPressed(.C)) plyr.height -= 0.1;
if (isKeyPressed(.V)) plyr.height += 0.1;
const rotation = 2.0 * PI / 200.0;
if (isKeyPressed(.Left)) plyr.ang += rotation;
if (isKeyPressed(.Right)) plyr.ang -= rotation;
const cos = std.math.cos(plyr.ang);
const sin = std.math.sin(plyr.ang);
plyr.acc_x = 0;
plyr.acc_y = 0;
const acc_mag = 30;
if (isKeyPressed(.R)) {
plyr.acc_x -= sin;
plyr.acc_y += cos;
}
if (isKeyPressed(.T)) {
plyr.acc_x += sin;
plyr.acc_y -= cos;
}
if (isKeyPressed(.Up) or isKeyPressed(.F)) {
plyr.acc_x += cos;
plyr.acc_y += sin;
}
if (isKeyPressed(.Down) or isKeyPressed(.S)) {
plyr.acc_x -= cos;
plyr.acc_y -= sin;
}
// normalise
const a = std.math.sqrt(plyr.acc_x * plyr.acc_x + plyr.acc_y * plyr.acc_y);
if (a > 0) {
plyr.acc_x *= acc_mag / a;
plyr.acc_y *= acc_mag / a;
}
// First the background
const back_scroll = @floatToInt(i32, -constants.BackTextureWidth * plyr.ang / (2 * PI));
back_sprite.setTextureRect(sf.IntRect{
.left = back_scroll, // fix location of texture in the sky
.top = 0,
// scaling ensures that the next two dimensions paint it across the
// width of the Screen, and for half the height of the screen
.width = constants.BackTextureWidth / 4.0, // 1/4 of texture per screen
.height = constants.BackTextureHeight, // full height of texture visible
});
window.draw(back_sprite, null);
try renderer.renderWorld(
plyr,
window,
objects_image,
walls_image,
surfaces_image,
rendered_surfaces_texture,
rendered_surfaces_sprite,
map,
);
window.display();
plyr.tick(map);
}
}
|