Skip to content

Commit

Permalink
Merge pull request #144 from DreamHollow/master
Browse files Browse the repository at this point in the history
General QOL improvements, new features
  • Loading branch information
Hopson97 authored Mar 12, 2023
2 parents c77271c + 7b74a1e commit f7187be
Show file tree
Hide file tree
Showing 64 changed files with 281 additions and 121 deletions.
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,13 @@ Release/

# Builds
bin/
Makefile
Makefile
build/

# Manually Added
CMakeFiles/
cmake_install.cmake
CMakeCache.txt
mc-one-week
libglad.a
development_notes.txt
22 changes: 18 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ cmake_minimum_required(VERSION 3.10)
#Set up project
project(mc-one-week VERSION 1.0)

configure_file(config.txt ${CMAKE_BINARY_DIR}/config.txt COPYONLY)

# Copy the resource files to the new binary dir
add_custom_target(copy_resources ALL
COMMAND ${CMAKE_COMMAND} -E copy_directory
${PROJECT_SOURCE_DIR}/Res
${PROJECT_BINARY_DIR}/Res
COMMENT "Copying game resources into Res folder")


add_custom_target(copy_shaders ALL
COMMAND ${CMAKE_COMMAND} -E copy_directory
${PROJECT_SOURCE_DIR}/Shaders
${PROJECT_BINARY_DIR}/Shaders
COMMENT "Copying shaders into Shaders folder")

#Set executable
add_executable(mc-one-week
Source/Item/Material.cpp
Expand All @@ -29,8 +45,7 @@ add_executable(mc-one-week
Source/World/Chunk/Chunk.cpp
Source/World/Chunk/ChunkSection.cpp
Source/World/Chunk/ChunkMeshBuilder.cpp
Source/States/PlayingState.cpp
Source/Physics/AABB.cpp
Source/States/PlayState.cpp
Source/Player/Player.cpp
Source/Maths/Ray.cpp
Source/Maths/Frustum.cpp
Expand Down Expand Up @@ -62,7 +77,6 @@ add_executable(mc-one-week
Source/Renderer/WaterRenderer.cpp
Source/Renderer/ChunkRenderer.cpp
Source/Renderer/SkyboxRenderer.cpp
Source/Renderer/SFMLRenderer.cpp
Source/Renderer/FloraRenderer.cpp
Source/Model.cpp
)
Expand Down Expand Up @@ -107,4 +121,4 @@ target_link_libraries(mc-one-week
${SFML_LIBRARIES}
${SFML_DEPENDENCIES}
${CMAKE_DL_LIBS}
)
)
29 changes: 27 additions & 2 deletions Source/Application.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Application.h"
#include "States/PlayingState.h"
#include "States/PlayState.h"
#include "World/Block/BlockDatabase.h"
#include <iostream>

Expand All @@ -9,18 +9,39 @@ Application::Application(const Config &config)
, m_config(config)
{
BlockDatabase::get();
pushState<StatePlaying>(*this, config);
pushState<StatePlay>(*this, config);
}

float g_timeElapsed = 0;

/// @brief Game loop utilizing a mixture of SFML events and GL rendering.
void Application::runLoop()
{
sf::Clock dtTimer;
sf::Clock dt;
sf::Vector2i win_center;

sf::Time m;

// Grab the context window and force it to a certain position.
// This prevents the window from sticking to the bottom of the visible screen like it does
// in some Linux distros. Especially Arch.

// If the window is small, use these parameters
if(m_context.window.getSize().x <= 640)
{
win_center = {
sf::VideoMode::getDesktopMode().width / 3.5,
sf::VideoMode::getDesktopMode().height / 4
};
}
else // Else force it to the upper-leftgit p
{
win_center = { 0,0 };
}

m_context.window.setPosition(win_center);

while (m_context.window.isOpen() && !m_states.empty()) {
auto deltaTime = dtTimer.restart();
auto &state = *m_states.back();
Expand All @@ -44,6 +65,7 @@ void Application::runLoop()
}
}

/// @brief Handles window events, especially window polling and keyboard inputs.
void Application::handleEvents()
{
sf::Event e;
Expand Down Expand Up @@ -71,16 +93,19 @@ void Application::handleEvents()
}
}

/// @brief Tell the program stack to pop off the state.
void Application::popState()
{
m_isPopState = true;
}

/// @brief Makes the mouse invisible, doesn't actually turn off the mouse.
void Application::turnOffMouse()
{
m_context.window.setMouseCursorVisible(false);
}

/// @brief Makes the mouse visible again.
void Application::turnOnMouse()
{
m_context.window.setMouseCursorVisible(true);
Expand Down
1 change: 1 addition & 0 deletions Source/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

float extern g_timeElapsed;

/// @brief The main game application itself.
class Application {

public:
Expand Down
3 changes: 2 additions & 1 deletion Source/Config.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#ifndef CONFIG_H_INCLUDED
#define CONFIG_H_INCLUDED

/// @brief Default configuration for program.
struct Config {
int windowX = 1280;
int windowY = 720;
bool isFullscreen = false;
int renderDistance = 16;
int renderDistance = 8; // Set initial RD low to prevent long load times
int fov = 90;
};

Expand Down
1 change: 1 addition & 0 deletions Source/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "Config.h"

/// @brief Struct related to window and application context.
struct Context {
Context(const Config &config);

Expand Down
2 changes: 2 additions & 0 deletions Source/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "Maths/glm.h"
#include <SFML/Graphics.hpp>

/* Vestigial class that was going to be used for player movement. */

// WIP
class Controller {
glm::vec3 translateInput();
Expand Down
1 change: 1 addition & 0 deletions Source/Input/Keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <array>

/// @brief Handles keyboard inputs and events.
class Keyboard {
public:
Keyboard();
Expand Down
1 change: 1 addition & 0 deletions Source/Input/ToggleKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <SFML/Graphics.hpp>

/// @brief A keyboard related subclass that determines if a key remains pressed.
class ToggleKey {
public:
ToggleKey(sf::Keyboard::Key);
Expand Down
1 change: 1 addition & 0 deletions Source/Item/ItemStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "Material.h"

/// @brief Determines if a player character is holding blocks or items, also determines placement behavior.
class ItemStack {
public:
ItemStack(const Material &material, int amount);
Expand Down
2 changes: 2 additions & 0 deletions Source/Item/ItemType.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#ifndef ITEMTYPE_H_INCLUDED
#define ITEMTYPE_H_INCLUDED

// This class isn't currently being used.

#endif // ITEMTYPE_H_INCLUDED
1 change: 1 addition & 0 deletions Source/Item/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "../Util/NonCopyable.h"

/// @brief Determines case-by-case properties and behaviors of known block types.
struct Material : public NonCopyable {
enum ID {
Nothing,
Expand Down
93 changes: 71 additions & 22 deletions Source/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,85 @@ int main()
}

namespace {
/// @brief Self declared function that loads in configuration files as needed.
/// @param config
void loadConfig(Config &config)
{
std::ifstream configFile("config.txt");
std::string key;

if (configFile.is_open()) {
while (configFile >> key) {
if (key == "renderdistance") {
configFile >> config.renderDistance;
std::cout << "Config: Render Distance: "
<< config.renderDistance << '\n';
}
else if (key == "fullscreen") {
configFile >> config.isFullscreen;
std::cout << "Config: Full screen mode: " << std::boolalpha
<< config.isFullscreen << '\n';
}
else if (key == "windowsize") {
configFile >> config.windowX >> config.windowY;
std::cout << "Config: Window Size: " << config.windowX << " x "
<< config.windowY << '\n';
}
else if (key == "fov") {
configFile >> config.fov;
std::cout << "Config: Field of Vision: " << config.fov << '\n';
// If the config file is missing or "bad"
if(!configFile.good())
{
std::cout << "Configuration file invalid,\n";
std::cout << "writing 'new' configuration." << "\n";
std::cout << "\n";

std::ofstream outfile("config.txt");

if(outfile.is_open())
{
outfile << "renderdistance " << "8";
outfile << "fullscreen " << "0";
outfile << "windowsize " << "1600 " << "900";
outfile << "fov " << "105";

outfile.close();
configFile.close(); // Close so it can be reopened safely.
}

std::cout << "\n";
std::cout << "New configuration file created." << "\n";
}

try
{
// Open 'new' config file.
if(!configFile.is_open())
{
configFile.open("config.txt");
}

// If the file is still creating errors
if(configFile.fail())
{
std::cout << "Error: The program failed to load the configuration files." << "\n";
std::cout << "To understand why this error may have occured,\n";
std::cout << "please examine your 'config.txt' file. Thank you." << "\n";

// Because this is thrown before runtime, no memory needs to be freed.
throw "Unable to load configuration file.";
}

if (configFile.is_open())
{
while (configFile >> key)
{
if (key == "renderdistance") {
configFile >> config.renderDistance;
std::cout << "Config: Render Distance: "
<< config.renderDistance << '\n';
}
else if (key == "fullscreen") {
configFile >> config.isFullscreen;
std::cout << "Config: Full screen mode: " << std::boolalpha
<< config.isFullscreen << '\n';
}
else if (key == "windowsize") {
configFile >> config.windowX >> config.windowY;
std::cout << "Config: Window Size: " << config.windowX << " x "
<< config.windowY << '\n';
}
else if (key == "fov") {
configFile >> config.fov;
std::cout << "Config: Field of Vision: " << config.fov << '\n';
}
}
}
}
else {
std::cerr << "Error: Could not find config.txt file! Using defaults.\n";
catch(const std::exception& e)
{
std::cerr << e.what();
}
}

Expand Down
5 changes: 5 additions & 0 deletions Source/Maths/Frustum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ float Plane::distanceToPoint(const glm::vec3 &point) const
return glm::dot(point, normal) + distanceToOrigin;
}

/// @brief Updates the Frustrum relative between player and observed surface.
/// @param mat
void ViewFrustum::update(const glm::mat4 &mat) noexcept
{
// left
Expand Down Expand Up @@ -61,6 +63,9 @@ void ViewFrustum::update(const glm::mat4 &mat) noexcept
}
}

/// @brief Determines if a collision box is present in the Frustrum.
/// @param box
/// @return result
bool ViewFrustum::isBoxInFrustum(const AABB &box) const noexcept
{
bool result = true;
Expand Down
3 changes: 2 additions & 1 deletion Source/Maths/Frustum.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

struct AABB;

/// @brief Vertex based construct, usually flat.
struct Plane {
float distanceToPoint(const glm::vec3 &point) const;

float distanceToOrigin;
glm::vec3 normal;
glm::vec3 normal; // Vector3 normals
};

class ViewFrustum {
Expand Down
5 changes: 5 additions & 0 deletions Source/Maths/GeneralMaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ float smoothstep(float edge0, float edge1, float x)
return (edge0 * x) + (edge1 * (1 - x));
}

/// @brief Clamp function that regulates values between limits.
/// @param x
/// @param lowerlimit
/// @param upperlimit
/// @return x
float clamp(float x, float lowerlimit, float upperlimit)
{
if (x < lowerlimit)
Expand Down
Loading

0 comments on commit f7187be

Please sign in to comment.