ctaza/game.cpp

166 lines
3.9 KiB
C++

#include "game.hpp"
Game::Game()
:
running(true),
fullscreen(false),
wireframe(false),
rotation(0.0f),
camera(glm::vec3(0.0f, 0.0f, 5.0f))
{
if (SDL_Init(SDL_INIT_EVERYTHING)) {
fprintf(stderr, "Error initializing SDL2: %s\n", SDL_GetError());
exit(1);
}
if (SDL_SetRelativeMouseMode(SDL_TRUE)) {
fprintf(stderr, "Error initializing SDL2: %s\n", SDL_GetError());
exit(1);
}
SDL_GL_LoadLibrary(NULL);
/* GL version 3.3 */
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
/* depth buffer */
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
win_flags = SDL_WINDOW_OPENGL;
win_width = 640;
win_height = 640;
window = SDL_CreateWindow(
"lukin",
0, 0,
win_width, win_height,
win_flags);
assert(window);
context = SDL_GL_CreateContext(window);
/* gives us access to the opengl functions */
gladLoadGLLoader(SDL_GL_GetProcAddress);
/* depth testing is good */
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
}
Game::~Game()
{
for (auto ent : entities)
delete ent;
}
void
Game::update(float dt)
{
// rotation += 0.03; //tmp
float speed = 5; //tmp
float sensitivity = 0.001f; //tmp
float effspeed = speed * dt;
int offset_x, offset_y;
SDL_GetRelativeMouseState(&offset_x, &offset_y);
camera.yaw += offset_x * sensitivity;
camera.pitch -= offset_y * sensitivity;
if (camera.pitch >= M_PI / 2)
camera.pitch = M_PI / 2 - 0.01f;
if (camera.pitch <= -M_PI / 2)
camera.pitch = -M_PI / 2 + 0.01f;
SDL_Event ev;
while (SDL_PollEvent(&ev)) {
if (ev.type == SDL_KEYDOWN) {
switch (ev.key.keysym.sym) {
case SDLK_F1:
toggle_wireframe();
break;
case SDLK_F11:
toggle_fullscreen();
break;
default:
break;
}
}
if (ev.type == SDL_QUIT) {
running = false;
}
}
#define if_key(key) if (key_states[SDL_SCANCODE_##key])
const u8 *key_states = SDL_GetKeyboardState(NULL);
if_key(ESCAPE)
running = false;
if_key(W)
camera.position += effspeed * camera.get_flat_front();
if_key(S)
camera.position -= effspeed * camera.get_flat_front();
if_key(A)
camera.position += effspeed * camera.get_right();
if_key(D)
camera.position -= effspeed * camera.get_right();
if_key(SPACE)
camera.position += effspeed * glm::vec3(0.0f, 1.0f, 0.0f);
if_key(LCTRL)
camera.position -= effspeed * glm::vec3(0.0f, 1.0f, 0.0f);
}
void
Game::render() const
{
glViewport(0, 0, win_width, win_height);
glClearColor(0.2, 0.2, 0.2, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (auto &e : entities) {
// tmp
e->mesh.mesh->shader->set_mat4("model", glm::rotate(
glm::mat4(1.0f),
rotation,
glm::vec3(0.6f, 0.9f, 0.4f)));
e->mesh.mesh->shader->set_mat4("view", camera.get_view());
e->mesh.mesh->draw();
}
SDL_GL_SwapWindow(window);
}
Entity *
Game::add_entity()
{
Entity *ent = new Entity;
entities.push_back(ent);
return ent;
}
void
Game::toggle_fullscreen()
{
fullscreen = !fullscreen;
if (fullscreen)
SDL_SetWindowFullscreen(window, win_flags |
SDL_WINDOW_FULLSCREEN_DESKTOP);
else
SDL_SetWindowFullscreen(window, win_flags);
}
void
Game::toggle_wireframe()
{
wireframe = !wireframe;
if (wireframe)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
else
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}