GameInstance object, Renderer object, cleaned up Main.
parent
5a366093cd
commit
e57718f257
|
@ -4,6 +4,7 @@
|
|||
<component name="CidrRootsConfiguration">
|
||||
<excludeRoots>
|
||||
<file path="$PROJECT_DIR$/Libraries" />
|
||||
<file path="$PROJECT_DIR$/lib" />
|
||||
</excludeRoots>
|
||||
</component>
|
||||
<component name="JavaScriptSettings">
|
||||
|
|
|
@ -1,27 +1,22 @@
|
|||
cmake_minimum_required(VERSION 3.12)
|
||||
project(src)
|
||||
project(zeus)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
set(CMAKE_CXX_FLAGS "-s -O3")
|
||||
|
||||
include_directories(Libraries/glew/include/GL)
|
||||
include_directories(Libraries/glfw_linux/include)
|
||||
include_directories(Libraries/glm)
|
||||
include_directories(Libraries/stb_image)
|
||||
include_directories(Libraries/cute)
|
||||
include_directories(lib/glew/include/GL)
|
||||
include_directories(lib/glfw_linux/include)
|
||||
include_directories(lib/glm)
|
||||
include_directories(lib/stb_image)
|
||||
include_directories(lib/cute)
|
||||
#include_directories(Libraries/boost_1_68_0)
|
||||
|
||||
find_package(OpenGL REQUIRED)
|
||||
find_package(glfw3 REQUIRED)
|
||||
#find_package(Boost COMPONENTS system thread)
|
||||
|
||||
link_directories(Libraries/glew/lib)
|
||||
link_directories(lib/glew/lib)
|
||||
|
||||
add_executable(src src/Main.cpp src/engine/graphics/Mesh.cpp src/engine/graphics/Mesh.h src/engine/PerlinNoise.cpp src/engine/PerlinNoise.h src/engine/Entity.cpp src/engine/Entity.h src/engine/graphics/Shader.cpp src/engine/graphics/Shader.h src/engine/Window.cpp src/engine/Window.h src/engine/Camera.cpp src/engine/Camera.h src/engine/graphics/Texture.cpp src/engine/graphics/Texture.h src/mesh/MeshGenerator.cpp src/mesh/MeshGenerator.h src/engine/Timer.cpp src/engine/Timer.h src/blocks/BlockAtlas.cpp src/blocks/BlockAtlas.h src/blocks/BlockDef.cpp src/blocks/BlockDef.h src/mesh/MeshPart.cpp src/mesh/MeshPart.h src/mesh/MeshMod.h src/mesh/Vertex.cpp src/mesh/Vertex.h src/mesh/BlockModel.cpp src/mesh/BlockModel.h src/engine/TextureAtlas.cpp src/engine/TextureAtlas.h src/world/World.cpp src/world/World.h src/world/BlockChunk.cpp src/world/BlockChunk.h src/engine/helpers/ArrayTrans3D.h src/world/MeshChunk.cpp src/world/MeshChunk.h)
|
||||
add_executable(zeus zeus/Main.cpp zeus/engine/graphics/Mesh.cpp zeus/engine/graphics/Mesh.h zeus/engine/PerlinNoise.cpp zeus/engine/PerlinNoise.h zeus/engine/Entity.cpp zeus/engine/Entity.h zeus/engine/graphics/Shader.cpp zeus/engine/graphics/Shader.h zeus/engine/Window.cpp zeus/engine/Window.h zeus/engine/Camera.cpp zeus/engine/Camera.h zeus/engine/graphics/Texture.cpp zeus/engine/graphics/Texture.h zeus/mesh/MeshGenerator.cpp zeus/mesh/MeshGenerator.h zeus/engine/Timer.cpp zeus/engine/Timer.h zeus/blocks/BlockAtlas.cpp zeus/blocks/BlockAtlas.h zeus/blocks/BlockDef.cpp zeus/blocks/BlockDef.h zeus/mesh/MeshPart.cpp zeus/mesh/MeshPart.h zeus/mesh/MeshMod.h zeus/mesh/Vertex.cpp zeus/mesh/Vertex.h zeus/mesh/BlockModel.cpp zeus/mesh/BlockModel.h zeus/engine/TextureAtlas.cpp zeus/engine/TextureAtlas.h zeus/world/World.cpp zeus/world/World.h zeus/world/BlockChunk.cpp zeus/world/BlockChunk.h zeus/engine/helpers/ArrayTrans3D.h zeus/world/MeshChunk.cpp zeus/world/MeshChunk.h zeus/game/GameInstance.cpp zeus/game/GameInstance.h zeus/engine/graphics/Renderer.cpp zeus/engine/graphics/Renderer.h)
|
||||
|
||||
target_link_libraries(src
|
||||
${OPENGL_gl_LIBRARY}
|
||||
glfw
|
||||
libGLEW.so
|
||||
pthread
|
||||
lua
|
||||
dl)
|
||||
target_link_libraries(zeus ${OPENGL_gl_LIBRARY} glfw libGLEW.so pthread lua dl)
|
|
@ -1,515 +0,0 @@
|
|||
/*
|
||||
------------------------------------------------------------------------------
|
||||
Licensing information can be found at the end of the file.
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
tinyfiles.h - v1.0
|
||||
|
||||
To create implementation (the function definitions)
|
||||
#define CUTE_FILES_IMPLEMENTATION
|
||||
in *one* C/CPP file (translation unit) that includes this file
|
||||
|
||||
Summary:
|
||||
Utility header for traversing directories to apply a function on each found file.
|
||||
Recursively finds sub-directories. Can also be used to iterate over files in a
|
||||
folder manually. All operations done in a cross-platform manner (thx posix!).
|
||||
|
||||
This header does no dynamic memory allocation, and performs internally safe string
|
||||
copies as necessary. Strings for paths, file names and file extensions are all
|
||||
capped, and intended to use primarily the C run-time stack memory. Feel free to
|
||||
modify the defines in this file to adjust string size limitations.
|
||||
|
||||
Read the header for specifics on each function.
|
||||
|
||||
Here's an example to print all files in a folder:
|
||||
cf_dir_t dir;
|
||||
cf_dir_open(&dir, "a");
|
||||
|
||||
while (dir.has_next)
|
||||
{
|
||||
cf_file_t file;
|
||||
cf_read_file(&dir, &file);
|
||||
printf("%s\n", file.name);
|
||||
cf_dir_next(&dir);
|
||||
}
|
||||
|
||||
cf_dir_close(&dir);
|
||||
*/
|
||||
|
||||
#if !defined(CUTE_FILES_H)
|
||||
|
||||
#define CUTE_FILES_WINDOWS 1
|
||||
#define CUTE_FILES_MAC 2
|
||||
#define CUTE_FILES_UNIX 3
|
||||
|
||||
#if defined(_WIN32)
|
||||
#define CUTE_FILES_PLATFORM CUTE_FILES_WINDOWS
|
||||
#if !defined(_CRT_SECURE_NO_WARNINGS)
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#endif
|
||||
#elif defined(__APPLE__)
|
||||
#define CUTE_FILES_PLATFORM CUTE_FILES_MAC
|
||||
#else
|
||||
#define CUTE_FILES_PLATFORM CUTE_FILES_UNIX
|
||||
#endif
|
||||
|
||||
#include <string.h> // strerror, strncpy
|
||||
|
||||
// change to 0 to compile out any debug checks
|
||||
#define CUTE_FILES_DEBUG_CHECKS 1
|
||||
|
||||
#if CUTE_FILES_DEBUG_CHECKS
|
||||
|
||||
#include <stdio.h> // printf
|
||||
#include <assert.h> // assert
|
||||
#include <errno.h>
|
||||
#define CUTE_FILES_ASSERT assert
|
||||
|
||||
#else
|
||||
|
||||
#define CUTE_FILES_ASSERT(...)
|
||||
|
||||
#endif // CUTE_FILES_DEBUG_CHECKS
|
||||
|
||||
#define CUTE_FILES_MAX_PATH 1024
|
||||
#define CUTE_FILES_MAX_FILENAME 256
|
||||
#define CUTE_FILES_MAX_EXT 32
|
||||
|
||||
struct cf_file_t;
|
||||
struct cf_dir_t;
|
||||
struct cf_time_t;
|
||||
typedef struct cf_file_t cf_file_t;
|
||||
typedef struct cf_dir_t cf_dir_t;
|
||||
typedef struct cf_time_t cf_time_t;
|
||||
typedef void (cf_callback_t)(cf_file_t* file, void* udata);
|
||||
|
||||
// Stores the file extension in cf_file_t::ext, and returns a pointer to
|
||||
// cf_file_t::ext
|
||||
const char* cf_get_ext(cf_file_t* file);
|
||||
|
||||
// Applies a function (cb) to all files in a directory. Will recursively visit
|
||||
// all subdirectories. Useful for asset management, file searching, indexing, etc.
|
||||
void cf_traverse(const char* path, cf_callback_t* cb, void* udata);
|
||||
|
||||
// Fills out a cf_file_t struct with file information. Does not actually open the
|
||||
// file contents, and instead performs more lightweight OS-specific calls.
|
||||
int cf_read_file(cf_dir_t* dir, cf_file_t* file);
|
||||
|
||||
// Once a cf_dir_t is opened, this function can be used to grab another file
|
||||
// from the operating system.
|
||||
void cf_dir_next(cf_dir_t* dir);
|
||||
|
||||
// Performs lightweight OS-specific call to close internal handle.
|
||||
void cf_dir_close(cf_dir_t* dir);
|
||||
|
||||
// Performs lightweight OS-specific call to open a file handle on a directory.
|
||||
int cf_dir_open(cf_dir_t* dir, const char* path);
|
||||
|
||||
// Compares file last write times. -1 if file at path_a was modified earlier than path_b.
|
||||
// 0 if they are equal. 1 if file at path_b was modified earlier than path_a.
|
||||
int cf_compare_file_times_by_path(const char* path_a, const char* path_b);
|
||||
|
||||
// Retrieves time file was last modified, returns 0 upon failure
|
||||
int cf_get_file_time(const char* path, cf_time_t* time);
|
||||
|
||||
// Compares file last write times. -1 if time_a was modified earlier than path_b.
|
||||
// 0 if they are equal. 1 if time_b was modified earlier than path_a.
|
||||
int cf_compare_file_times(cf_time_t* time_a, cf_time_t* time_b);
|
||||
|
||||
// Returns 1 of file exists, otherwise returns 0.
|
||||
int cf_file_exists(const char* path);
|
||||
|
||||
// Returns 1 if the file's extension matches the string in ext
|
||||
// Returns 0 otherwise
|
||||
int cf_match_ext(cf_file_t* file, const char* ext);
|
||||
|
||||
// Prints detected errors to stdout
|
||||
void cf_do_unit_tests();
|
||||
|
||||
#if CUTE_FILES_PLATFORM == CUTE_FILES_WINDOWS
|
||||
|
||||
#if !defined _CRT_SECURE_NO_WARNINGS
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#endif
|
||||
#include <Windows.h>
|
||||
|
||||
struct cf_file_t
|
||||
{
|
||||
char path[CUTE_FILES_MAX_PATH];
|
||||
char name[CUTE_FILES_MAX_FILENAME];
|
||||
char ext[CUTE_FILES_MAX_EXT];
|
||||
int is_dir;
|
||||
int is_reg;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
struct cf_dir_t
|
||||
{
|
||||
char path[CUTE_FILES_MAX_PATH];
|
||||
int has_next;
|
||||
HANDLE handle;
|
||||
WIN32_FIND_DATAA fdata;
|
||||
};
|
||||
|
||||
struct cf_time_t
|
||||
{
|
||||
FILETIME time;
|
||||
};
|
||||
|
||||
#elif CUTE_FILES_PLATFORM == CUTE_FILES_MAC || CUTE_FILES_PLATFORM == CUTE_FILES_UNIX
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
|
||||
struct cf_file_t
|
||||
{
|
||||
char path[CUTE_FILES_MAX_PATH];
|
||||
char name[CUTE_FILES_MAX_FILENAME];
|
||||
char ext[CUTE_FILES_MAX_EXT];
|
||||
int is_dir;
|
||||
int is_reg;
|
||||
int size;
|
||||
struct stat info;
|
||||
};
|
||||
|
||||
struct cf_dir_t
|
||||
{
|
||||
char path[CUTE_FILES_MAX_PATH];
|
||||
int has_next;
|
||||
DIR* dir;
|
||||
struct dirent* entry;
|
||||
};
|
||||
|
||||
struct cf_time_t
|
||||
{
|
||||
time_t time;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#define CUTE_FILES_H
|
||||
#endif
|
||||
|
||||
#ifdef CUTE_FILES_IMPLEMENTATION
|
||||
#ifndef CUTE_FILES_IMPLEMENTATION_ONCE
|
||||
#define CUTE_FILES_IMPLEMENTATION_ONCE
|
||||
|
||||
#define cf_safe_strcpy(dst, src, n, max) cf_safe_strcpy_internal(dst, src, n, max, __FILE__, __LINE__)
|
||||
static int cf_safe_strcpy_internal(char* dst, const char* src, int n, int max, const char* file, int line)
|
||||
{
|
||||
int c;
|
||||
const char* original = src;
|
||||
|
||||
do
|
||||
{
|
||||
if (n >= max)
|
||||
{
|
||||
if (!CUTE_FILES_DEBUG_CHECKS) break;
|
||||
printf("ERROR: String \"%s\" too long to copy on line %d in file %s (max length of %d).\n"
|
||||
, original
|
||||
, line
|
||||
, file
|
||||
, max);
|
||||
CUTE_FILES_ASSERT(0);
|
||||
}
|
||||
|
||||
c = *src++;
|
||||
dst[n] = c;
|
||||
++n;
|
||||
} while (c);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
const char* cf_get_ext(cf_file_t* file)
|
||||
{
|
||||
char* name = file->name;
|
||||
char* period = NULL;
|
||||
while (*name++) if (*name == '.') period = name;
|
||||
if (period) cf_safe_strcpy(file->ext, period, 0, CUTE_FILES_MAX_EXT);
|
||||
else file->ext[0] = 0;
|
||||
return file->ext;
|
||||
}
|
||||
|
||||
void cf_traverse(const char* path, cf_callback_t* cb, void* udata)
|
||||
{
|
||||
cf_dir_t dir;
|
||||
cf_dir_open(&dir, path);
|
||||
|
||||
while (dir.has_next)
|
||||
{
|
||||
cf_file_t file;
|
||||
cf_read_file(&dir, &file);
|
||||
|
||||
if (file.is_dir && file.name[0] != '.')
|
||||
{
|
||||
char path2[CUTE_FILES_MAX_PATH];
|
||||
int n = cf_safe_strcpy(path2, path, 0, CUTE_FILES_MAX_PATH);
|
||||
n = cf_safe_strcpy(path2, "/", n - 1, CUTE_FILES_MAX_PATH);
|
||||
cf_safe_strcpy(path2, file.name, n -1, CUTE_FILES_MAX_PATH);
|
||||
cf_traverse(path2, cb, udata);
|
||||
}
|
||||
|
||||
if (file.is_reg) cb(&file, udata);
|
||||
cf_dir_next(&dir);
|
||||
}
|
||||
|
||||
cf_dir_close(&dir);
|
||||
}
|
||||
|
||||
int cf_match_ext(cf_file_t* file, const char* ext)
|
||||
{
|
||||
return !strcmp(file->ext, ext);
|
||||
}
|
||||
|
||||
#if CUTE_FILES_PLATFORM == CUTE_FILES_WINDOWS
|
||||
|
||||
int cf_read_file(cf_dir_t* dir, cf_file_t* file)
|
||||
{
|
||||
CUTE_FILES_ASSERT(dir->handle != INVALID_HANDLE_VALUE);
|
||||
|
||||
int n = 0;
|
||||
char* fpath = file->path;
|
||||
char* dpath = dir->path;
|
||||
|
||||
n = cf_safe_strcpy(fpath, dpath, 0, CUTE_FILES_MAX_PATH);
|
||||
n = cf_safe_strcpy(fpath, "/", n - 1, CUTE_FILES_MAX_PATH);
|
||||
|
||||
char* dname = dir->fdata.cFileName;
|
||||
char* fname = file->name;
|
||||
|
||||
cf_safe_strcpy(fname, dname, 0, CUTE_FILES_MAX_FILENAME);
|
||||
cf_safe_strcpy(fpath, fname, n - 1, CUTE_FILES_MAX_PATH);
|
||||
|
||||
size_t max_dword = MAXDWORD;
|
||||
file->size = ((size_t)dir->fdata.nFileSizeHigh * (max_dword + 1)) + (size_t)dir->fdata.nFileSizeLow;
|
||||
cf_get_ext(file);
|
||||
|
||||
file->is_dir = !!(dir->fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
|
||||
file->is_reg = !!(dir->fdata.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) ||
|
||||
!(dir->fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void cf_dir_next(cf_dir_t* dir)
|
||||
{
|
||||
CUTE_FILES_ASSERT(dir->has_next);
|
||||
|
||||
if (!FindNextFileA(dir->handle, &dir->fdata))
|
||||
{
|
||||
dir->has_next = 0;
|
||||
DWORD err = GetLastError();
|
||||
CUTE_FILES_ASSERT(err == ERROR_SUCCESS || err == ERROR_NO_MORE_FILES);
|
||||
}
|
||||
}
|
||||
|
||||
void cf_dir_close(cf_dir_t* dir)
|
||||
{
|
||||
dir->path[0] = 0;
|
||||
dir->has_next = 0;
|
||||
if (dir->handle != INVALID_HANDLE_VALUE) FindClose(dir->handle);
|
||||
}
|
||||
|
||||
int cf_dir_open(cf_dir_t* dir, const char* path)
|
||||
{
|
||||
int n = cf_safe_strcpy(dir->path, path, 0, CUTE_FILES_MAX_PATH);
|
||||
n = cf_safe_strcpy(dir->path, "\\*", n - 1, CUTE_FILES_MAX_PATH);
|
||||
dir->handle = FindFirstFileA(dir->path, &dir->fdata);
|
||||
dir->path[n - 3] = 0;
|
||||
|
||||
if (dir->handle == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
printf("ERROR: Failed to open directory (%s): %s.\n", path, strerror(errno));
|
||||
cf_dir_close(dir);
|
||||
CUTE_FILES_ASSERT(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
dir->has_next = 1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int cf_compare_file_times_by_path(const char* path_a, const char* path_b)
|
||||
{
|
||||
FILETIME time_a = { 0 };
|
||||
FILETIME time_b = { 0 };
|
||||
WIN32_FILE_ATTRIBUTE_DATA data;
|
||||
|
||||
if (GetFileAttributesExA(path_a, GetFileExInfoStandard, &data)) time_a = data.ftLastWriteTime;
|
||||
if (GetFileAttributesExA(path_b, GetFileExInfoStandard, &data)) time_b = data.ftLastWriteTime;
|
||||
return CompareFileTime(&time_a, &time_b);
|
||||
}
|
||||
|
||||
int cf_get_file_time(const char* path, cf_time_t* time)
|
||||
{
|
||||
FILETIME initialized_to_zero = { 0 };
|
||||
time->time = initialized_to_zero;
|
||||
WIN32_FILE_ATTRIBUTE_DATA data;
|
||||
if (GetFileAttributesExA(path, GetFileExInfoStandard, &data))
|
||||
{
|
||||
time->time = data.ftLastWriteTime;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cf_compare_file_times(cf_time_t* time_a, cf_time_t* time_b)
|
||||
{
|
||||
return CompareFileTime(&time_a->time, &time_b->time);
|
||||
}
|
||||
|
||||
int cf_file_exists(const char* path)
|
||||
{
|
||||
WIN32_FILE_ATTRIBUTE_DATA unused;
|
||||
return GetFileAttributesExA(path, GetFileExInfoStandard, &unused);
|
||||
}
|
||||
|
||||
#elif CUTE_FILES_PLATFORM == CUTE_FILES_MAC || CUTE_FILES_PLATFORM == CUTE_FILES_UNIX
|
||||
|
||||
int cf_read_file(cf_dir_t* dir, cf_file_t* file)
|
||||
{
|
||||
CUTE_FILES_ASSERT(dir->entry);
|
||||
|
||||
int n = 0;
|
||||
char* fpath = file->path;
|
||||
char* dpath = dir->path;
|
||||
|
||||
n = cf_safe_strcpy(fpath, dpath, 0, CUTE_FILES_MAX_PATH);
|
||||
n = cf_safe_strcpy(fpath, "/", n - 1, CUTE_FILES_MAX_PATH);
|
||||
|
||||
char* dname = dir->entry->d_name;
|
||||
char* fname = file->name;
|
||||
|
||||
cf_safe_strcpy(fname, dname, 0, CUTE_FILES_MAX_FILENAME);
|
||||
cf_safe_strcpy(fpath, fname, n - 1, CUTE_FILES_MAX_PATH);
|
||||
|
||||
if (stat(file->path, &file->info))
|
||||
return 0;
|
||||
|
||||
file->size = file->info.st_size;
|
||||
cf_get_ext(file);
|
||||
|
||||
file->is_dir = S_ISDIR(file->info.st_mode);
|
||||
file->is_reg = S_ISREG(file->info.st_mode);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void cf_dir_next(cf_dir_t* dir)
|
||||
{
|
||||
CUTE_FILES_ASSERT(dir->has_next);
|
||||
dir->entry = readdir(dir->dir);
|
||||
dir->has_next = dir->entry ? 1 : 0;
|
||||
}
|
||||
|
||||
void cf_dir_close(cf_dir_t* dir)
|
||||
{
|
||||
dir->path[0] = 0;
|
||||
if (dir->dir) closedir(dir->dir);
|
||||
dir->dir = 0;
|
||||
dir->has_next = 0;
|
||||
dir->entry = 0;
|
||||
}
|
||||
|
||||
int cf_dir_open(cf_dir_t* dir, const char* path)
|
||||
{
|
||||
cf_safe_strcpy(dir->path, path, 0, CUTE_FILES_MAX_PATH);
|
||||
dir->dir = opendir(path);
|
||||
|
||||
if (!dir->dir)
|
||||
{
|
||||
printf("ERROR: Failed to open directory (%s): %s.\n", path, strerror(errno));
|
||||
cf_dir_close(dir);
|
||||
CUTE_FILES_ASSERT(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
dir->has_next = 1;
|
||||
dir->entry = readdir(dir->dir);
|
||||
if (!dir->dir) dir->has_next = 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Warning : untested code! (let me know if it breaks)
|
||||
int cf_compare_file_times_by_path(const char* path_a, const char* path_b)
|
||||
{
|
||||
time_t time_a;
|
||||
time_t time_b;
|
||||
struct stat info;
|
||||
if (stat(path_a, &info)) return 0;
|
||||
time_a = info.st_mtime;
|
||||
if (stat(path_b, &info)) return 0;
|
||||
time_b = info.st_mtime;
|
||||
return (int)difftime(time_a, time_b);
|
||||
}
|
||||
|
||||
// Warning : untested code! (let me know if it breaks)
|
||||
int cf_get_file_time(const char* path, cf_time_t* time)
|
||||
{
|
||||
struct stat info;
|
||||
if (stat(path, &info)) return 0;
|
||||
time->time = info.st_mtime;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Warning : untested code! (let me know if it breaks)
|
||||
int cf_compare_file_times(cf_time_t* time_a, cf_time_t* time_b)
|
||||
{
|
||||
return (int)difftime(time_a->time, time_b->time);
|
||||
}
|
||||
|
||||
// Warning : untested code! (let me know if it breaks)
|
||||
int cf_file_exists(const char* path)
|
||||
{
|
||||
return access(path, F_OK) != -1;
|
||||
}
|
||||
|
||||
#endif // CUTE_FILES_PLATFORM
|
||||
|
||||
#endif // CUTE_FILES_IMPLEMENTATION_ONCE
|
||||
#endif // CUTE_FILES_IMPLEMENTATION
|
||||
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
This software is available under 2 licenses - you may choose the one you like.
|
||||
------------------------------------------------------------------------------
|
||||
ALTERNATIVE A - zlib license
|
||||
Copyright (c) 2017 Randy Gaul http://www.randygaul.net
|
||||
This software is provided 'as-is', without any express or implied warranty.
|
||||
In no event will the authors be held liable for any damages arising from
|
||||
the use of this software.
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not
|
||||
be misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
------------------------------------------------------------------------------
|
||||
ALTERNATIVE B - Public Domain (www.unlicense.org)
|
||||
This is free and unencumbered software released into the public domain.
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
|
||||
software, either in source code form or as a compiled binary, for any purpose,
|
||||
commercial or non-commercial, and by any means.
|
||||
In jurisdictions that recognize copyright laws, the author or authors of this
|
||||
software dedicate any and all copyright interest in the software to the public
|
||||
domain. We make this dedication for the benefit of the public at large and to
|
||||
the detriment of our heirs and successors. We intend this dedication to be an
|
||||
overt act of relinquishment in perpetuity of all present and future rights to
|
||||
this software under copyright law.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
------------------------------------------------------------------------------
|
||||
*/
|
181
src/Main.cpp
181
src/Main.cpp
|
@ -1,181 +0,0 @@
|
|||
#pragma clang diagnostic push
|
||||
#pragma ide diagnostic ignored "OCUnusedMacroInspection"
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "engine/Window.h"
|
||||
#include "engine/Camera.h"
|
||||
#include "engine/graphics/Shader.h"
|
||||
|
||||
#include "engine/TextureAtlas.h"
|
||||
#include "blocks/BlockAtlas.h"
|
||||
#include "world/World.h"
|
||||
|
||||
#include "engine/PerlinNoise.h"
|
||||
#include "engine/helpers/ArrayTrans3D.h"
|
||||
#include "engine/Timer.h"
|
||||
|
||||
Window* window;
|
||||
Shader* shader;
|
||||
Camera* camera;
|
||||
|
||||
TextureAtlas* textureAtlas;
|
||||
BlockAtlas* blockAtlas;
|
||||
World* world;
|
||||
|
||||
GLfloat deltaTime = 0.0f;
|
||||
GLfloat lastTime = 0.0f;
|
||||
|
||||
void registerBlocks() {
|
||||
auto* bm = BlockModel::Square("default_grass_top", "default_dirt", "default_grass_side", "default_grass_side", "default_grass_side", "default_grass_side", textureAtlas);
|
||||
auto* def = new BlockDef("grass", bm);
|
||||
blockAtlas->registerBlock(def);
|
||||
}
|
||||
|
||||
void genChunks(World* world) {
|
||||
PerlinNoise p(0);
|
||||
|
||||
int VIEW_RANGE = 32;
|
||||
|
||||
for (int i = -VIEW_RANGE; i < VIEW_RANGE; i++) {
|
||||
for (int j = -VIEW_RANGE; j < VIEW_RANGE; j++) {
|
||||
for (int k = -1; k < 1; k++) {
|
||||
|
||||
auto* blocks = new std::vector<int>();
|
||||
blocks->reserve(4096);
|
||||
|
||||
glm::vec3 innerPos, pos;
|
||||
|
||||
for (int ind = 0; ind < 4096; ind++) {
|
||||
ArrayTrans3D::indAssignVec(ind, &innerPos);
|
||||
pos.x = innerPos.x + i * CHUNK_SIZE;
|
||||
pos.y = innerPos.y + k * CHUNK_SIZE;
|
||||
pos.z = innerPos.z + j * CHUNK_SIZE;
|
||||
|
||||
double val = p.noise(pos.x / (double) 32, pos.z / (double) 32, pos.y / (double) 16) - pos.y * 0.08;
|
||||
|
||||
blocks->push_back((int)(val > 0.5));
|
||||
}
|
||||
|
||||
world->newChunk(new glm::vec3(i, k, j), new BlockChunk(blocks));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
#include "lua.h"
|
||||
#include "lualib.h"
|
||||
#include "lauxlib.h"
|
||||
}
|
||||
|
||||
lua_State* L;
|
||||
|
||||
static int average(lua_State *L) {
|
||||
int n = lua_gettop(L);
|
||||
double sum = 0;
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
sum += lua_tonumber(L, i);
|
||||
}
|
||||
|
||||
lua_pushnumber(L, sum / n);
|
||||
lua_pushnumber(L, sum);
|
||||
|
||||
|
||||
|
||||
return 2; //Number of results
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
// int x = 4, y = 3, sum;
|
||||
//
|
||||
// L = luaL_newstate();
|
||||
//
|
||||
// //Open base libraries
|
||||
// luaL_openlibs(L);
|
||||
// lua_register(L, "average", average);
|
||||
//
|
||||
// luaL_dofile(L, "../file.lua");
|
||||
//
|
||||
// //Call the "add" function
|
||||
// lua_getglobal(L, "add");
|
||||
// lua_pushnumber(L, x);
|
||||
// lua_pushnumber(L, y);
|
||||
//
|
||||
// lua_call(L, 2, 1);
|
||||
//
|
||||
// sum = (int)lua_tointeger(L, -1);
|
||||
// lua_pop(L, 1);
|
||||
//
|
||||
// lua_close(L);
|
||||
//
|
||||
// printf("Hold up %i", sum);
|
||||
// getchar();
|
||||
//
|
||||
// return 0;
|
||||
|
||||
Timer boot("Initialization");
|
||||
|
||||
window = new Window(1366, 768);
|
||||
window->initialize();
|
||||
|
||||
camera = new Camera(glm::vec3(0.0f, 16.0f, 0.0f), glm::vec3(0, 1, 0), -90.0f, -45.0f, 10.0f, 0.1f);
|
||||
|
||||
textureAtlas = new TextureAtlas("../tex");
|
||||
blockAtlas = new BlockAtlas(textureAtlas);
|
||||
registerBlocks();
|
||||
|
||||
world = new World(blockAtlas);
|
||||
genChunks(world);
|
||||
|
||||
shader = new Shader();
|
||||
shader->createFromFile("../src/shader/world.vs", "../src/shader/world.fs");
|
||||
|
||||
glm::mat4 projectionMatrix = glm::perspective(45.0f, window->getBufferWidth() / window->getBufferHeight(), 0.1f, 1000.0f);
|
||||
|
||||
glEnable(GL_CULL_FACE);
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
boot.elapsed();
|
||||
|
||||
//Game Loop
|
||||
while (!window->getShouldClose()) {
|
||||
Timer t("Game Loop");
|
||||
|
||||
auto now = (GLfloat)glfwGetTime();
|
||||
deltaTime = now - lastTime;
|
||||
lastTime = now;
|
||||
|
||||
//Get & Handle Input
|
||||
glfwPollEvents();
|
||||
window->update();
|
||||
|
||||
camera->keyControl(window->getKeysArray(), deltaTime);
|
||||
camera->mouseControl(window->getDeltaX(), window->getDeltaY());
|
||||
|
||||
world->update();
|
||||
|
||||
//Clear Window
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
shader->useShader();
|
||||
textureAtlas->getTexture()->use();
|
||||
|
||||
glUniformMatrix4fv(shader->getProjectionLocation(), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
|
||||
glUniformMatrix4fv(shader->getViewLocation(), 1, GL_FALSE, glm::value_ptr(camera->calculateViewMatrix()));
|
||||
|
||||
//Render chunks
|
||||
world->draw(shader->getModelLocation());
|
||||
|
||||
Shader::clearShader();
|
||||
window->swapBuffers();
|
||||
|
||||
// t.elapsedMs(); //Print frame time
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,101 @@
|
|||
#pragma clang diagnostic push
|
||||
#pragma ide diagnostic ignored "OCUnusedMacroInspection"
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "engine/Window.h"
|
||||
#include "engine/Camera.h"
|
||||
|
||||
#include "engine/Timer.h"
|
||||
#include "game/GameInstance.h"
|
||||
#include "engine/graphics/Renderer.h"
|
||||
|
||||
GLfloat deltaTime = 0.0f;
|
||||
GLfloat lastTime = 0.0f;
|
||||
|
||||
//extern "C" {
|
||||
// #include "lua.h"
|
||||
// #include "lualib.h"
|
||||
// #include "lauxlib.h"
|
||||
//}
|
||||
//
|
||||
//lua_State* L;
|
||||
//
|
||||
//static int average(lua_State *L) {
|
||||
// int n = lua_gettop(L);
|
||||
// double sum = 0;
|
||||
//
|
||||
// for (int i = 1; i <= n; i++) {
|
||||
// sum += lua_tonumber(L, i);
|
||||
// }
|
||||
//
|
||||
// lua_pushnumber(L, sum / n);
|
||||
// lua_pushnumber(L, sum);
|
||||
//
|
||||
//
|
||||
//
|
||||
// return 2; //Number of results
|
||||
//}
|
||||
|
||||
// int x = 4, y = 3, sum;
|
||||
//
|
||||
// L = luaL_newstate();
|
||||
//
|
||||
// //Open base libraries
|
||||
// luaL_openlibs(L);
|
||||
// lua_register(L, "average", average);
|
||||
//
|
||||
// luaL_dofile(L, "../file.lua");
|
||||
//
|
||||
// //Call the "add" function
|
||||
// lua_getglobal(L, "add");
|
||||
// lua_pushnumber(L, x);
|
||||
// lua_pushnumber(L, y);
|
||||
//
|
||||
// lua_call(L, 2, 1);
|
||||
//
|
||||
// sum = (int)lua_tointeger(L, -1);
|
||||
// lua_pop(L, 1);
|
||||
//
|
||||
// lua_close(L);
|
||||
//
|
||||
// printf("Hold up %i", sum);
|
||||
// getchar();
|
||||
//
|
||||
// return 0;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
Timer boot("Initialization");
|
||||
|
||||
auto renderer = new Renderer(1366, 768);
|
||||
auto game = new GameInstance();
|
||||
|
||||
game->initialize(renderer);
|
||||
|
||||
boot.elapsed();
|
||||
|
||||
//Game Loop
|
||||
while (!renderer->getWindow()->getShouldClose()) {
|
||||
Timer t("Game Loop");
|
||||
|
||||
auto now = (GLfloat)glfwGetTime();
|
||||
deltaTime = now - lastTime;
|
||||
lastTime = now;
|
||||
|
||||
//Get & Handle Input
|
||||
glfwPollEvents();
|
||||
renderer->update();
|
||||
|
||||
//Update game instance
|
||||
game->update(deltaTime);
|
||||
|
||||
renderer->begin();
|
||||
game->draw();
|
||||
renderer->end();
|
||||
|
||||
t.elapsedMs(); //Print frame time
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
BlockAtlas::BlockAtlas(TextureAtlas *textureAtlas) {
|
||||
this->textureAtlas = textureAtlas;
|
||||
definitions.push_back(nullptr); //Air
|
||||
}
|
||||
|
||||
void BlockAtlas::registerBlock(BlockDef* def) {
|
|
@ -0,0 +1,58 @@
|
|||
//
|
||||
// Created by aurailus on 17/12/18.
|
||||
//
|
||||
|
||||
#include "Renderer.h"
|
||||
|
||||
Renderer::Renderer(GLint winWidth, GLint winHeight) {
|
||||
|
||||
window = new Window(winWidth, winHeight);
|
||||
window->initialize();
|
||||
camera = new Camera(glm::vec3(0.0f, 16.0f, 0.0f), glm::vec3(0, 1, 0), -90.0f, -45.0f, 10.0f, 0.1f);
|
||||
|
||||
shader = new Shader();
|
||||
shader->createFromFile("../zeus/shader/world.vs", "../zeus/shader/world.fs");
|
||||
|
||||
projectionMatrix = glm::perspective(45.0f, window->getBufferWidth() / window->getBufferHeight(), 0.1f, 1000.0f);
|
||||
|
||||
glEnable(GL_CULL_FACE);
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
}
|
||||
|
||||
void Renderer::update() {
|
||||
window->update();
|
||||
}
|
||||
|
||||
void Renderer::begin() {
|
||||
//Clear Window
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
shader->useShader();
|
||||
|
||||
glUniformMatrix4fv(shader->getProjectionLocation(), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
|
||||
glUniformMatrix4fv(shader->getViewLocation(), 1, GL_FALSE, glm::value_ptr(camera->calculateViewMatrix()));
|
||||
}
|
||||
|
||||
void Renderer::draw(Entity* entity) {
|
||||
glUniformMatrix4fv(shader->getModelLocation(), 1, GL_FALSE, glm::value_ptr(entity->getModelMatrix()));
|
||||
entity->draw();
|
||||
}
|
||||
|
||||
void Renderer::end() {
|
||||
Shader::clearShader();
|
||||
window->swapBuffers();
|
||||
}
|
||||
|
||||
Renderer::~Renderer() {
|
||||
|
||||
}
|
||||
|
||||
Window *Renderer::getWindow() {
|
||||
return window;
|
||||
}
|
||||
|
||||
Camera *Renderer::getCamera() {
|
||||
return camera;
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
//
|
||||
// Created by aurailus on 17/12/18.
|
||||
//
|
||||
|
||||
#ifndef SRC_RENDERER_H
|
||||
#define SRC_RENDERER_H
|
||||
|
||||
#include "Shader.h"
|
||||
#include "../Window.h"
|
||||
#include "../Camera.h"
|
||||
#include "../Entity.h"
|
||||
#include <ext.hpp>
|
||||
|
||||
class Renderer {
|
||||
public:
|
||||
Renderer(GLint winWidth, GLint winHeight);
|
||||
|
||||
void update();
|
||||
|
||||
void begin();
|
||||
void draw(Entity* entity);
|
||||
void end();
|
||||
|
||||
Window* getWindow();
|
||||
Camera* getCamera();
|
||||
|
||||
~Renderer();
|
||||
private:
|
||||
Window* window;
|
||||
Camera* camera;
|
||||
|
||||
glm::mat4 projectionMatrix;
|
||||
|
||||
//World Shader
|
||||
Shader* shader;
|
||||
};
|
||||
|
||||
|
||||
#endif //SRC_RENDERER_H
|
|
@ -0,0 +1,79 @@
|
|||
//
|
||||
// Created by aurailus on 17/12/18.
|
||||
//
|
||||
|
||||
#include "GameInstance.h"
|
||||
|
||||
GameInstance::GameInstance() = default;
|
||||
|
||||
//TODO: Remove this function
|
||||
void genChunks(World* world) {
|
||||
PerlinNoise p(0);
|
||||
|
||||
int VIEW_RANGE = 24;
|
||||
|
||||
for (int i = -VIEW_RANGE; i < VIEW_RANGE; i++) {
|
||||
for (int j = -VIEW_RANGE; j < VIEW_RANGE; j++) {
|
||||
for (int k = -1; k < 1; k++) {
|
||||
|
||||
auto* blocks = new std::vector<int>();
|
||||
blocks->reserve(4096);
|
||||
|
||||
glm::vec3 innerPos, pos;
|
||||
|
||||
for (int ind = 0; ind < 4096; ind++) {
|
||||
ArrayTrans3D::indAssignVec(ind, &innerPos);
|
||||
pos.x = innerPos.x + i * CHUNK_SIZE;
|
||||
pos.y = innerPos.y + k * CHUNK_SIZE;
|
||||
pos.z = innerPos.z + j * CHUNK_SIZE;
|
||||
|
||||
double val = p.noise(pos.x / (double) 32, pos.z / (double) 32, pos.y / (double) 16) - pos.y * 0.08;
|
||||
|
||||
blocks->push_back((int)(val > 0.5));
|
||||
}
|
||||
|
||||
world->newChunk(new glm::vec3(i, k, j), new BlockChunk(blocks));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void GameInstance::initialize(Renderer* renderer) {
|
||||
this->renderer = renderer;
|
||||
|
||||
textureAtlas = new TextureAtlas("../tex");
|
||||
blockAtlas = new BlockAtlas(textureAtlas);
|
||||
|
||||
//TODO: Move block registration to lua code
|
||||
auto* def = new BlockDef("grass", BlockModel::Square(
|
||||
"default_grass_top", "default_dirt", "default_grass_side",
|
||||
"default_grass_side", "default_grass_side", "default_grass_side", textureAtlas));
|
||||
blockAtlas->registerBlock(def);
|
||||
|
||||
//The world requires the blockAtlas for meshing and handling inputs.
|
||||
world = new World(blockAtlas);
|
||||
|
||||
genChunks(world);
|
||||
}
|
||||
|
||||
void GameInstance::update(GLfloat deltaTime) {
|
||||
auto camera = renderer->getCamera();
|
||||
auto window = renderer->getWindow();
|
||||
|
||||
camera->keyControl(window->getKeysArray(), deltaTime);
|
||||
camera->mouseControl(window->getDeltaX(), window->getDeltaY());
|
||||
|
||||
world->update();
|
||||
}
|
||||
|
||||
void GameInstance::draw() {
|
||||
textureAtlas->getTexture()->use();
|
||||
|
||||
for (auto &chunk : *world->getMeshChunks()) {
|
||||
renderer->draw(chunk.second);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
GameInstance::~GameInstance() = default;
|
|
@ -0,0 +1,42 @@
|
|||
//
|
||||
// Created by aurailus on 17/12/18.
|
||||
//
|
||||
|
||||
#ifndef SRC_GAMEWORLD_H
|
||||
#define SRC_GAMEWORLD_H
|
||||
|
||||
|
||||
#include "../world/World.h"
|
||||
#include "../engine/Camera.h"
|
||||
#include "../engine/Window.h"
|
||||
#include "../engine/PerlinNoise.h"
|
||||
#include "../engine/graphics/Renderer.h"
|
||||
#include "../engine/helpers/ArrayTrans3D.h"
|
||||
|
||||
class GameInstance {
|
||||
public:
|
||||
GameInstance();
|
||||
|
||||
void initialize(Renderer* renderer);
|
||||
|
||||
void update(GLfloat deltaTime);
|
||||
|
||||
void draw();
|
||||
|
||||
~GameInstance();
|
||||
private:
|
||||
//The renderer contains the camera, window, and draw methods.
|
||||
Renderer* renderer;
|
||||
|
||||
//The World object represents the physical game region that is played in. It handles chunk updates and entities.
|
||||
World* world;
|
||||
|
||||
//The texture atlas makes a single patched texture from an asset folder.
|
||||
TextureAtlas* textureAtlas;
|
||||
|
||||
//The block atlas holds block definitions and models.
|
||||
BlockAtlas* blockAtlas;
|
||||
};
|
||||
|
||||
|
||||
#endif //SRC_GAMEWORLD_H
|
|
@ -25,12 +25,10 @@ void MeshGenerator::build(BlockChunk* chunk, BlockAtlas* atlas,
|
|||
glm::vec3 off;
|
||||
glm::vec3 check;
|
||||
|
||||
//TODO: Move this to the loop and make it use 'i' instead of 0
|
||||
BlockModel* model = atlas->getBlock(0)->getModel();
|
||||
|
||||
for (int i = 0; i < 4096; i++) {
|
||||
if (chunk->getBlock(i) == 1) {
|
||||
ArrayTrans3D::indAssignVec(i, &off);
|
||||
BlockModel* model = atlas->getBlock(chunk->getBlock(i))->getModel();
|
||||
|
||||
check.x = off.x - 1; check.y = off.y; check.z = off.z;
|
||||
if (outOfRange(check) || chunk->getBlock(&check) == 0)
|
|
@ -70,11 +70,8 @@ void World::update() {
|
|||
}
|
||||
}
|
||||
|
||||
void World::draw(GLint modelUni) {
|
||||
for (auto &meshChunk : meshChunks) {
|
||||
glUniformMatrix4fv(modelUni, 1, GL_FALSE, glm::value_ptr(meshChunk.second->getModelMatrix()));
|
||||
meshChunk.second->draw();
|
||||
}
|
||||
std::map<glm::vec3*, MeshChunk*>* World::getMeshChunks() {
|
||||
return &meshChunks;
|
||||
}
|
||||
|
||||
World::ThreadData::ThreadData(glm::vec3 *pos, BlockChunk *chunk, BlockAtlas *atlas) {
|
|
@ -24,7 +24,8 @@ public:
|
|||
void newChunk(glm::vec3* pos, BlockChunk* c);
|
||||
|
||||
void update();
|
||||
void draw(GLint modelUni);
|
||||
|
||||
std::map<glm::vec3*, MeshChunk*>* getMeshChunks();
|
||||
|
||||
~World() = default;
|
||||
|
Loading…
Reference in New Issue