-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
180 lines (164 loc) · 6.41 KB
/
main.cpp
File metadata and controls
180 lines (164 loc) · 6.41 KB
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
#include <cassert>
#include <string>
#include <memory>
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <set>
#include <SDL2/SDL.h>
#include <GL/glew.h>
#include <thread>
#include "shader.hpp"
#include "camera.hpp"
#include "vector3.hpp"
#include "chunk.hpp"
#include "display.hpp"
#include "texture.hpp"
#include "world.hpp"
#include "gizmo.hpp"
#include "network.hpp"
static constexpr auto HALF_PI = 3.1415 / 2.0;
static Vector3 const gposition[] = {
{1, 1, 0},
{-1, 1, 0},
{-1, -1, 0},
{1, -1, 0},
};
static Vector2 const gtexture[] = {
{1, 1},
{0, 1},
{0, 0},
{1, 0},
};
int main(int argc, char** argv) {
std::cout << "Starting Initialization!" << std::endl;
assert(SDL_Init(SDL_INIT_EVERYTHING) == 0);
SDL_Window* window = SDL_CreateWindow("Triangle",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height,
SDL_WINDOW_OPENGL);
assert(window);
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, glcontext);
assert(glcontext);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
glewExperimental = GL_TRUE;
auto status = glewInit();
assert(status == GLEW_OK);
SDL_SetRelativeMouseMode(SDL_TRUE);
std::cout << "opengl version: " << glGetString(GL_VERSION) << std::endl;
// Game Objects
Camera camera(Vector3(0, 32.0, 0), 0, 0);
IndexId aprxpos(0, 0, 0);
World world;
world.lcchunk.insert(std::pair(IndexId(0, 0, 0), generate(IndexId(0, 0, 0))));
world.lcchunk.insert(std::pair(IndexId(0, 0, -1), generate(IndexId(0, 0, -1))));
world.lcchunk.insert(std::pair(IndexId(1, 0, -1), generate(IndexId(1, 0, -1))));
world.lcchunk.insert(std::pair(IndexId(2, 0, -2), generate(IndexId(2, 0, -2))));
//world.entities.emplace_back(Gizmo(gposition, gtexture, 4, Vector3(0, 32, -10)));
// Shaders -------------------
auto vert = create_shader("./shader/vert.glsl", GL_VERTEX_SHADER);
auto frag = create_shader("./shader/frag.glsl", GL_FRAGMENT_SHADER);
unsigned int tmp[] = {vert, frag};
auto program = create_program(tmp);
glDeleteShader(vert);
glDeleteShader(frag);
// Textures
Image etexture("./picture/image.png", 0);
Image atlas("./picture/atlas.png", 1);
// Network
char const* hostip = "127.0.0.1"; // Deafults
uint16_t hostport = 8000;
if (argc >= 2) hostip = argv[1]; // Command line overrides
if (argc >= 3) hostport = atoi(argv[2]);
std::cout << "Connecting to network: Ip: " << hostip << " Port: " << hostport << std::endl;
std::cout << "Network Initialization Successful: " << net::pl_open(hostip, hostport) << std::endl;
net::ClientGetChunkUpdate(IndexId(69, 70, 80));
// Thread Spinning
std::thread netthread(net::spinup, &world);
// SDL
bool running = true;
std::set<int> keys_down;
while (running) {
// Process Inputs
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) running = false;
if (event.type == SDL_KEYDOWN) keys_down.insert(event.key.keysym.scancode);
if (event.type == SDL_KEYUP) keys_down.erase(event.key.keysym.scancode);
if (event.type == SDL_MOUSEMOTION) {
auto dx = event.motion.xrel * 0.005;
auto dy = -event.motion.yrel * 0.005;
camera.yaw -= dx;
camera.pitch += dy;
}
}
camera.clamp(); // Make sure we don't overextend the view angles
// Key Input
if (keys_down.contains(SDL_SCANCODE_ESCAPE)) running = false;
if (keys_down.contains(SDL_SCANCODE_W)) {
camera.position.z -= cos(camera.yaw)*.2;
camera.position.x -= sin(camera.yaw)*.2;
}
if (keys_down.contains(SDL_SCANCODE_S)) {
camera.position.z += cos(camera.yaw)*.1;
camera.position.x += sin(camera.yaw)*.1;
}
if (keys_down.contains(SDL_SCANCODE_A)) {
camera.position.z -= cos(camera.yaw+HALF_PI)*.1;
camera.position.x -= sin(camera.yaw+HALF_PI)*.1;
}
if (keys_down.contains(SDL_SCANCODE_D)) {
camera.position.z -= cos(camera.yaw-HALF_PI)*.1;
camera.position.x -= sin(camera.yaw-HALF_PI)*.1;
}
if (keys_down.contains(SDL_SCANCODE_SPACE)) camera.position.y += 0.1;
if (keys_down.contains(SDL_SCANCODE_LSHIFT)) camera.position.y -= 0.1;
// Clear Buffers before next rendering
glViewport(0, 0, width, height);
glClearColor(0.8, 0.8, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
// Process the chunks - refresh meshes - etc
auto currentx_aprx = static_cast<int>(camera.position.x / (sizex * blocksize));
auto currentz_aprx = static_cast<int>(camera.position.z / (sizez * blocksize));
aprxpos = IndexId(currentx_aprx, 0, currentz_aprx);
world.reload_check(aprxpos);
world.dirty_check();
// Get the current entities
world.entities.clear();
for (auto const& entity_info : world.tocreate_entities) {
world.entities.push_back(Gizmo(gposition, gtexture, 4, entity_info));
}
// Draw the chunks
std::lock_guard<std::mutex> live_chunk_lock(world.localchunk_guard);
set_texture(program, atlas);
std::ranges::for_each(world.live_chunks, [&](LiveChunk const& chunk) {
draw_chunk(chunk, camera, program);
});
set_texture(program, etexture);
std::ranges::for_each(world.entities, [&](Gizmo const& entity) {
draw_gizmo(entity, camera, program);
});
// Send All updates and requests in bulk
net::ClientSection section;
section.kind = net::ClientSectionKind::kClientSendPlayerPosUpdate;
auto loc = Vector3(camera.position.x, camera.position.y, camera.position.z);
section.d.c_sp_update = net::ClientSendPlayerPosUpdate(
EntityTransform(loc, camera.yaw)
);
net::enqueue_section(section);
net::sendall();
SDL_GL_SwapWindow(window);
}
// Cleanup
net::pl_close();
glDeleteProgram(program);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}