95 lines
2.6 KiB
C++
95 lines
2.6 KiB
C++
#include <iostream>
|
|
|
|
#include <glad/glad.h>
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#include "window.h"
|
|
#include "world.h"
|
|
#include "shader.h"
|
|
#include "texture.h"
|
|
|
|
#include <glm/glm.hpp>
|
|
|
|
extern const char _binary_res_vertex_shader_dat_start[];
|
|
extern const char _binary_res_vertex_shader_dat_end[];
|
|
|
|
extern const char _binary_res_fragment_shader_dat_start[];
|
|
extern const char _binary_res_fragment_shader_dat_end[];
|
|
|
|
extern const unsigned char _binary_res_textures_dat_start[];
|
|
|
|
World* world;
|
|
Camera* c;
|
|
Shader* s;
|
|
|
|
int key_states[GLFW_KEY_LAST];
|
|
|
|
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
|
|
if (key < 0)
|
|
return;
|
|
|
|
if (action == GLFW_PRESS)
|
|
key_states[key] = 1;
|
|
else if (action == GLFW_RELEASE)
|
|
key_states[key] = 0;
|
|
}
|
|
|
|
void loop(float deltatime, GLFWwindow* w) {
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
glClearColor(0.2f, 0.6f, 0.7f, 1.0f);
|
|
std::cout << "Deltatime: " << deltatime << "\n";
|
|
|
|
float speed = 20.0f;
|
|
|
|
if (key_states[GLFW_KEY_W])
|
|
c->move_by(glm::vec3(0.0f, 0.0f, -speed) * deltatime);
|
|
if (key_states[GLFW_KEY_A])
|
|
c->move_by(glm::vec3(-speed, 0.0f, 0.0f) * deltatime);
|
|
if (key_states[GLFW_KEY_S])
|
|
c->move_by(glm::vec3(0.0f, 0.0f, speed) * deltatime);
|
|
if (key_states[GLFW_KEY_D])
|
|
c->move_by(glm::vec3(speed, 0.0f, 0.0f) * deltatime);
|
|
if (key_states[GLFW_KEY_SPACE])
|
|
c->change_position_by(glm::vec3(0.0f, speed, 0.0f) * deltatime);
|
|
if (key_states[GLFW_KEY_LEFT_SHIFT])
|
|
c->change_position_by(glm::vec3(0.0f, -speed, 0.0f) * deltatime);
|
|
|
|
double current_x_pos, current_y_pos;
|
|
glfwGetCursorPos(w, ¤t_x_pos, ¤t_y_pos);
|
|
|
|
double delta_x = (double)(Window::width / 2) - current_x_pos;
|
|
double delta_y = (double)(Window::height / 2) - current_y_pos;
|
|
|
|
c->change_rotation_by(glm::vec3(0.005f * (float)delta_y, 0.005f * (float)delta_x, 0.0f));
|
|
glfwSetCursorPos(w, Window::width / 2, Window::height / 2);
|
|
|
|
world->render(c, s);
|
|
|
|
world->process(c->get_position().x, c->get_position().z);
|
|
}
|
|
|
|
int main() {
|
|
Window w = Window(1280, 720, "xnoecraft", key_callback);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
glDepthFunc(GL_LESS);
|
|
|
|
glEnable(GL_CULL_FACE);
|
|
glFrontFace(GL_CCW);
|
|
glCullFace(GL_BACK);
|
|
|
|
Texture* t = new Texture(_binary_res_textures_dat_start);
|
|
|
|
c = new Camera();
|
|
c->set_position(glm::vec3(0.0f, 256.0f, 0.0f));
|
|
|
|
world = new World();
|
|
world->generate_around(0, 0);
|
|
|
|
ShaderSource vertex_shader(_binary_res_vertex_shader_dat_start, _binary_res_vertex_shader_dat_end);
|
|
ShaderSource fragment_shader(_binary_res_fragment_shader_dat_start, _binary_res_fragment_shader_dat_end);
|
|
|
|
s = new Shader(vertex_shader, fragment_shader);
|
|
|
|
w.mainloop(loop);
|
|
} |