XtreemNodes/main.cpp

239 lines
5.6 KiB
C++

#include <stdlib.h>
#include <GL/glut.h>
#include "Utilities.h"
#include "LevelGenerator.h"
//#include "MapBlock.h"
//#include "Base.h"
#include "NodeRenderer.h"
#include "TextureHandler.h"
#include <math.h>
#include <cstdio>
#include "include/Camera.h"
#include <random>
#include <glm/ext/matrix_transform.hpp>
#include <SFML/Window.hpp>
#include "include/TitleMenu.h"
NodeRenderer renderer;
BlockManager blockManager;
LevelGenerator levelGen;
TextureHandler textureHandler;
GLfloat playerX = 2.5F;
GLfloat playerY = 20.0F;
GLfloat playerZ = 1.5F;
GLfloat playerRotX = 0;
GLfloat playerRotY = 0;
float light = 1.0F;
sf::Vector2i oldMousePos;
TitleMenu title;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//glRotatef(playerRotX, 1.0F, .0F, .0F);
//glRotatef(playerRotY, .0F, 1.0F, .0F);
Camera* camera;
const glm::mat4& matrix = glm::lookAt(glm::vec3(playerX, playerY, playerZ), glm::vec3(playerX - cos(glm::radians(playerRotY)), playerY - playerRotX, playerZ + sin(glm::radians(playerRotY))), glm::vec3(0, 1, 0));
//https://glm.g-truc.net/0.9.9/api/a00668.html#gaa64aa951a0e99136bba9008d2b59c78e
glMatrixMode(GL_MODELVIEW);
//TODO transform
//glMatrixMode(GL_PROJECTION);
glLoadMatrixf(&matrix[0][0]);
//glTranslatef(playerX, playerY, playerZ);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
for(int x = 0; x < 256; x++)
{
for(int y = 0; y < 80; y++)
{
for(int z = 0; z < 256; z++)
{
if(blockManager.getNodeAt(x, y, z) > 0)
{
renderer.renderNode(x, y, z, textureHandler.getTextureForNode(x, y, z));
}
}
}
}
//title.renderGUI();
glFlush();
glutSwapBuffers();
}
void reshape(int width, int height)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50, width / (float) height, .1F, 512.0F);
glViewport(0, 0, width, height);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
int movementAngle = 0;
int angularVel = 1;
void updateTimer(int time)
{
// Movement
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
movementAngle = 0;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
movementAngle = 180; // 200 grads
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
movementAngle = 90;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
movementAngle = 270;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W) || sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
int oldX = playerX;
int oldZ = playerZ;
playerX = playerX - cos(glm::radians(playerRotY + movementAngle)) * angularVel;
playerZ = playerZ + sin(glm::radians(playerRotY + movementAngle)) * angularVel;
if(blockManager.getNodeAt(playerX, playerY, playerZ) != 0)
{
playerX = oldX;
playerZ = oldZ;
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))
{
if(blockManager.getNodeAt(playerX, playerY - 4, playerZ) == 0)
{
playerY -= .8;
}
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
playerY += .8;
}
// Rotation
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
playerRotY -= 1.8;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
playerRotY += 1.8;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::F1))
{
levelGen.setSeed();
levelGen.generateBlock();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
playerRotX += 1.8;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
playerRotX = 1.8;
}
// Mouse-based movement
sf::Vector2i mousePos = sf::Mouse::getPosition();
sf::Vector2i mousePosDiff = oldMousePos - mousePos;
playerRotX -= mousePosDiff.y;
playerRotY -= mousePosDiff.x;
oldMousePos = sf::Vector2i(1920 / 2, 1080 / 2);
sf::Mouse::setPosition(sf::Vector2i(1920 / 2, 1080 / 2));
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
printf("PlayerX: %f, PlayerZ: %f", playerX, playerZ);
exit(0);
}
glutPostRedisplay();
glutTimerFunc(30, &updateTimer, 0);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(1920, 1080);
glutCreateWindow("XtreemNodes Engine - by MCL Software");
glClearColor(.4, .7, .8 , 255);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
//glEnable(GL_CULL_FACE);
//glCullFace(GL_FRONT);
//glFrontFace(GL_CCW);
// Load textures
textureHandler.loadAllTextures();
/*
for(int y = 0; y < 16; y++)
{
for(int x = 0; x < 16; x++)
{
for(int z = 0; z < 16; z++)
{
Position2D block = BlockUtilities::getBlockFromNodeCoordinates(x, z);
printf("\n|x: %i |y: %i | z: %i | id: %i", x, y, z, blockManager.getNodeAt(x, y, z));
}
}
}
*/
levelGen.generateBlock();
updateTimer(0);
glutDisplayFunc(&display);
glutReshapeFunc(&reshape);
glutMainLoop();
return 0;
}