-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
146 lines (99 loc) · 4.49 KB
/
main.cpp
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
//
// Created by Will on 2020-01-19.
//
#define GL_SILENCE_DEPRECATION
#define STB_IMAGE_IMPLEMENTATION
#ifdef __APPLE__
# define __gl_h_
# define GL_DO_NOT_WARN_IF_MULTI_GL_VERSION_HEADERS_INCLUDED
#endif
#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
#include <glad/glad.h>
#include <glfw/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <gfx/shader.hpp>
#include <gfx/vao.hpp>
#include <gfx/texture.hpp>
#include <gfx/gfx.hpp>
#include "craft/craft.h"
#define SCREEN_WIDTH 1280
#define SCREEN_HEIGHT 720
////////////////////////////////////////////////////////////////////////////////
// //
// Function Declarations //
// //
////////////////////////////////////////////////////////////////////////////////
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
void handle_input(GLFWwindow *window, glm::vec3 &cpos, const glm::vec3 cfwd, const glm::vec3 cup);
void mouse_callback(GLFWwindow *window, double xpos, double ypos);
int main();
////////////////////////////////////////////////////////////////////////////////
// //
// Function Definitions //
// //
////////////////////////////////////////////////////////////////////////////////
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
glViewport(0, 0, width, height);
}
craft::Player *player;
void mouse_callback(GLFWwindow *window, double xpos, double ypos) {
player->mouseCallback(window, xpos, ypos);
}
int main() {
std::cout << "start" << std::endl;
gfx::start(SCREEN_WIDTH, SCREEN_HEIGHT, "willcraft");
GLFWwindow *window = gfx::get_window();
// mouse stuff
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPosCallback(window, mouse_callback);
// Create world --------------
std::cout << "pre world" << std::endl;
craft::World world = craft::World(16, 64);
std::cout << "post world" << std::endl;
// Load Texture --------------------------
gfx::Texture texture_sheet = gfx::Texture("assets/blocks.png");
texture_sheet.apply_parameter(GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
// for that pixelated minecraft look
texture_sheet.apply_parameter(GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Camera --------------------------------
//gfx::Camera cam = gfx::Camera(100.0f, gfx::get_aspect_ratio(), 0.05f, 150.0f);
//cam.get_transform()->set_position(glm::vec3(0.0f, 30.0f, 0.0f));
//cam.set_up(glm::vec3(0.0f, 1.0f, 0.0f));
//cam.set_direction(glm::vec3(0.0f, 0.0f, -1.0f));
player = new craft::Player();
// Prep ------------------------------------------
// Load shaders
gfx::Shader shader_brightness = gfx::Shader("assets/shader_brightness.vert", "assets/shader_brightness.frag");
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glEnable(GL_CULL_FACE);
shader_brightness.use();
shader_brightness.set_int("tex", 0);
// Main loop -----------------------------------------------------------
while (!glfwWindowShouldClose(window)) {
// Input -------------------------------------------------
player->handleInput(window);
// Render -----------------------------------------------
glClearColor(143.0f / 255.0f, 186.0f / 255.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
texture_sheet.bind_to_texture_unit(0);
shader_brightness.use();
player->getCamera()->use_shader(&shader_brightness);
int px = ((int) player->getCamera()->get_transform()->get_position().x/16);
int pz = ((int) player->getCamera()->get_transform()->get_position().z/16);
world.draw(&shader_brightness);
world.load_at(px, pz, 3);
// rest
glfwSwapBuffers(window);
glfwPollEvents();
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
break;
}
}
glfwTerminate();
return 0;
}