Merged with last 'dev' branch commit.

This commit is contained in:
Quentin Bazin 2018-06-05 16:51:58 +02:00
commit 5a969f055a
19 changed files with 190 additions and 250 deletions

27
README.md Executable file
View File

@ -0,0 +1,27 @@
# KubKraft
### By gnidmoo
- KubKraft is a Minecraft-like game.
![](http://sdz-upload.s3.amazonaws.com/prod/upload/screenshot-20150103233518.png)
## Keys
- View: mouse controlled
- Movement: <kbd>W</kbd>, <kbd>A</kbd>, <kbd>S</kbd> and <kbd>D</kbd>
- Land: <kbd>Shift</kbd>
- Fly: <kbd>Space</kbd>
- Exit: <kbd>Escape</kbd>
## How to compile
- Dependencies:
- [CMake](http://www.cmake.org/download/)
- [SDL2](https://www.libsdl.org/download-2.0.php), [SDL2_image](https://www.libsdl.org/projects/SDL_image/), [SDL2_mixer](https://www.libsdl.org/projects/SDL_mixer/)
- OpenGL >= 2.1, [glm](http://sourceforge.net/projects/ogl-math/files/latest/download?source=files)
- [glew](http://sourceforge.net/projects/glew/files/latest/download) *(only required for Windows)*
- _Linux users: Check your distribution repositories for packages._
- Run `cmake .` at the root of the game folder
- Run `make`, wait, run the game and enjoy!

View File

@ -1,27 +0,0 @@
h1. KubKraft
h3. By gnidmoo
* KubKraft is a Minecraft-like game.
!http://sdz-upload.s3.amazonaws.com/prod/upload/screenshot-20150103233518.png!
h2. Keys
* View: mouse controlled
* Movement: @W@, @A@, @S@ and @D@
* Land: @Shift@
* Fly: @Space@
* Exit: @Escape@
h2. How to compile
* Dependencies:
- "CMake":http://www.cmake.org/download/
- "SDL2":https://www.libsdl.org/download-2.0.php, "SDL2_image":https://www.libsdl.org/projects/SDL_image/, "SDL2_mixer":https://www.libsdl.org/projects/SDL_mixer/
- OpenGL >= 3.0, "glm":http://sourceforge.net/projects/ogl-math/files/latest/download?source=files
- "glew":http://sourceforge.net/projects/glew/files/latest/download _(only required for Windows)_
* Run @cmake .@ at the root of the game folder
* Run @make@, wait, run the game and enjoy!

View File

@ -32,13 +32,8 @@ class Application {
Window &window() { return m_window; }
static Application &getInstance() {
static Application instance;
return instance;
}
private:
ApplicationStateStack *m_applicationStateStack;
ApplicationStateStack &m_stateStack;
GameClock m_clock;

View File

@ -21,14 +21,14 @@
class ApplicationStateStack {
public:
ApplicationStateStack();
~ApplicationStateStack();
ApplicationState &top() { return *m_stack.top().get(); }
void push(ApplicationState *state) { m_stack.push(std::unique_ptr<ApplicationState>(state)); }
void pop() { m_stack.pop(); }
static ApplicationStateStack &getInstance();
static ApplicationStateStack &getInstance() {
static ApplicationStateStack instance;
return instance;
}
private:
std::stack<std::unique_ptr<ApplicationState>> m_stack;

View File

@ -0,0 +1,35 @@
/*
* =====================================================================================
*
* Filename: SDLLoader.hpp
*
* Description:
*
* Created: 15/09/2014 00:06:50
*
* Author: Quentin Bazin, <gnidmoo@gmail.com>
*
* =====================================================================================
*/
#ifndef SDLLOADER_HPP_
#define SDLLOADER_HPP_
class SDLLoader {
public:
SDLLoader() = default;
SDLLoader(const SDLLoader &) = delete;
SDLLoader(SDLLoader &&) = delete;
~SDLLoader();
SDLLoader& operator=(const SDLLoader &) = delete;
SDLLoader& operator=(SDLLoader &&) = delete;
void load();
private:
bool m_sdlInitialized = false;
bool m_imgInitialized = false;
bool m_mixInitialized = false;
};
#endif // SDLLOADER_HPP_

View File

@ -1,32 +0,0 @@
/*
* =====================================================================================
*
* Filename: SDLManager.hpp
*
* Description:
*
* Version: 1.0
* Created: 20/12/2014 01:24:24
* Revision: none
* Compiler: gcc
*
* Author: Quentin BAZIN, <quent42340@gmail.com>
* Company:
*
* =====================================================================================
*/
#ifndef SDLMANAGER_HPP_
#define SDLMANAGER_HPP_
class SDLManager {
public:
static void init();
static void free();
private:
static bool sdlInitialized;
static bool imgInitialized;
static bool mixInitialized;
};
#endif // SDLMANAGER_HPP_

View File

@ -5,13 +5,9 @@
*
* Description:
*
* Version: 1.0
* Created: 15/12/2014 16:30:20
* Revision: none
* Compiler: gcc
*
* Author: Quentin BAZIN, <quent42340@gmail.com>
* Company:
*
* =====================================================================================
*/
@ -28,24 +24,24 @@
class Shader {
public:
Shader();
Shader(const char *vertexFilename, const char *fragementFilename);
Shader(const std::string &vertexFilename, const std::string &fragementFilename);
~Shader();
void loadFromFile(const char *vertexFilename, const char *fragementFilename);
void loadFromFile(const std::string &vertexFilename, const std::string &fragementFilename);
void createProgram();
void linkProgram();
void addShader(GLenum type, const char *filename);
void addShader(GLenum type, const std::string &filename);
GLint attrib(std::string name);
GLint uniform(std::string name);
GLint attrib(const std::string &name);
GLint uniform(const std::string &name);
void enableVertexAttribArray(std::string name);
void disableVertexAttribArray(std::string name);
void enableVertexAttribArray(const std::string &name);
void disableVertexAttribArray(const std::string &name);
void setUniform(std::string name, int n);
void setUniform(std::string name, const glm::mat4 &matrix);
void setUniform(const std::string &name, int n);
void setUniform(const std::string &name, const glm::mat4 &matrix);
static void bind(const Shader *shader);
@ -54,6 +50,7 @@ class Shader {
private:
std::vector<GLuint> m_vertexShaders;
std::vector<GLuint> m_fragmentShaders;
GLuint m_program;
};

View File

@ -16,13 +16,15 @@
class ApplicationState {
public:
ApplicationState(ApplicationState *parent = nullptr) : m_parent(parent) {}
virtual ~ApplicationState() = default;
virtual void handleEvents();
virtual void update() = 0;
virtual void draw() = 0;
protected:
ApplicationState *m_parent = nullptr;
};
#endif // APPLICATIONSTATE_HPP_

View File

@ -16,13 +16,13 @@ vec4 fog(vec4 color, float fogCoord, float fogStart, float fogEnd);
void main() {
// Discard if the pixel is too far away
if(v_dist > u_renderDistance) discard;
vec4 color = getColor();
color *= light(vec3(1.0, 1.0, 1.0), vec4(0.0, 48.0, 0.0, 1.0), 0.5, 0.5);
color = fog(color, v_dist, u_renderDistance - 8, u_renderDistance);
gl_FragColor = color;
}

View File

@ -22,13 +22,13 @@ void main() {
// Used for lighting
v_coord3d = u_modelMatrix * vec4(coord3d, 1.0);
v_normal = vec4(normal, 1.0);
v_color = vec4(color, 1.0);
v_texCoord = texCoord;
// Distance from eye
v_dist = length(u_viewMatrix * v_coord3d);
gl_Position = u_projectionMatrix * u_viewMatrix * v_coord3d;
}

View File

@ -23,8 +23,11 @@
#include "Application.hpp"
#include "Config.hpp"
#include "Exception.hpp"
#include "GameState.hpp"
#include "Mouse.hpp"
#include "OpenGL.hpp"
Application::Application() {
Application::Application() : m_stateStack(ApplicationStateStack::getInstance()) {
srand(time(NULL));
m_window.open(APP_NAME, SCREEN_WIDTH, SCREEN_HEIGHT);
@ -34,24 +37,46 @@ Application::Application() {
m_renderer.init(m_window);
//ResourceHandler::getInstance().loadResources();
m_applicationStateStack = &ApplicationStateStack::getInstance();
m_stateStack.push(new GameState());
}
void Application::handleEvents() {
Mouse::reset();
SDL_Event event;
while(SDL_PollEvent(&event) != 0) {
switch(event.type) {
case SDL_QUIT:
m_window.close();
break;
case SDL_KEYDOWN:
if(event.key.keysym.sym == SDLK_ESCAPE) {
m_window.close();
}
break;
case SDL_MOUSEMOTION:
Mouse::update(event.motion.x, event.motion.y, event.motion.xrel, event.motion.yrel);
break;
default:
break;
}
}
}
void Application::run() {
while(m_window.isOpen()) {
m_clock.measureLastFrameDuration();
m_applicationStateStack->top().handleEvents();
handleEvents();
m_clock.updateGame([&]{
m_applicationStateStack->top().update();
m_stateStack.top().update();
});
m_clock.drawGame([&]{
m_window.clear();
m_applicationStateStack->top().draw();
m_stateStack.top().draw();
m_window.display();
});

View File

@ -1,28 +0,0 @@
/*
* =====================================================================================
*
* Filename: ApplicationStateStack.cpp
*
* Description:
*
* Created: 14/12/2014 13:49:05
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#include "ApplicationStateStack.hpp"
#include "GameState.hpp"
ApplicationStateStack::ApplicationStateStack() {
push(new GameState);
}
ApplicationStateStack::~ApplicationStateStack() {
}
ApplicationStateStack &ApplicationStateStack::getInstance() {
static ApplicationStateStack instance;
return instance;
}

49
source/core/SDLLoader.cpp Normal file
View File

@ -0,0 +1,49 @@
/*
* =====================================================================================
*
* Filename: SDLLoader.cpp
*
* Description:
*
* Created: 15/09/2014 00:07:14
*
* Author: Quentin Bazin, <gnidmoo@gmail.com>
*
* =====================================================================================
*/
#include "Exception.hpp"
#include "SDLHeaders.hpp"
#include "SDLLoader.hpp"
SDLLoader::~SDLLoader() {
if(m_mixInitialized) Mix_CloseAudio();
if(m_imgInitialized) IMG_Quit();
if(m_sdlInitialized) SDL_Quit();
}
void SDLLoader::load() {
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
throw EXCEPTION("SDL init error:", SDL_GetError());
} else {
m_sdlInitialized = true;
}
int imgFlags = IMG_INIT_PNG;
if((!IMG_Init(imgFlags)) & imgFlags) {
throw EXCEPTION("SDL image init error:", IMG_GetError());
} else {
m_imgInitialized = true;
}
if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 1024) == -1) {
throw EXCEPTION("SDL mixer init error:", Mix_GetError());
} else {
m_mixInitialized = true;
}
Mix_AllocateChannels(32);
Mix_VolumeMusic(MIX_MAX_VOLUME / 3);
Mix_Volume(-1, MIX_MAX_VOLUME);
}

View File

@ -1,56 +0,0 @@
/*
* =====================================================================================
*
* Filename: SDLManager.cpp
*
* Description:
*
* Version: 1.0
* Created: 20/12/2014 01:24:53
* Revision: none
* Compiler: gcc
*
* Author: Quentin BAZIN, <quent42340@gmail.com>
* Company:
*
* =====================================================================================
*/
#include "Exception.hpp"
#include "SDLHeaders.hpp"
#include "SDLManager.hpp"
bool SDLManager::sdlInitialized = false;
bool SDLManager::imgInitialized = false;
bool SDLManager::mixInitialized = false;
void SDLManager::init() {
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
EXCEPTION("SDL init error:", SDL_GetError());
} else {
sdlInitialized = true;
}
int imgFlags = IMG_INIT_PNG;
if(!IMG_Init(imgFlags) & imgFlags) {
EXCEPTION("SDL image init error:", SDL_GetError());
} else {
imgInitialized = true;
}
if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 1024) == -1) {
EXCEPTION("SDL image init error:", SDL_GetError());
} else {
mixInitialized = true;
}
Mix_AllocateChannels(32);
Mix_VolumeMusic(MIX_MAX_VOLUME / 3);
Mix_Volume(-1, MIX_MAX_VOLUME);
}
void SDLManager::free() {
if(mixInitialized) Mix_CloseAudio();
if(imgInitialized) IMG_Quit();
if(sdlInitialized) SDL_Quit();
}

View File

@ -11,6 +11,7 @@
*
* =====================================================================================
*/
#include "Exception.hpp"
#include "OpenGL.hpp"
#include "Renderer.hpp"
@ -32,6 +33,5 @@ void Renderer::init(Window &window) {
glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST);
glClearColor(0.196078, 0.6, 0.8, 1.0); // Skyblue
//glClearColor(0.0, 0.0, 0.0, 1.0);
}

View File

@ -27,7 +27,7 @@
Shader::Shader() {
}
Shader::Shader(const char *vertexFilename, const char *fragmentFilename) {
Shader::Shader(const std::string &vertexFilename, const std::string &fragmentFilename) {
loadFromFile(vertexFilename, fragmentFilename);
}
@ -45,7 +45,7 @@ Shader::~Shader() {
glDeleteProgram(m_program);
}
void Shader::loadFromFile(const char *vertexFilename, const char *fragmentFilename) {
void Shader::loadFromFile(const std::string &vertexFilename, const std::string &fragmentFilename) {
createProgram();
addShader(GL_VERTEX_SHADER, vertexFilename);
@ -81,7 +81,7 @@ void Shader::linkProgram() {
}
}
void Shader::addShader(GLenum type, const char *filename) {
void Shader::addShader(GLenum type, const std::string &filename) {
GLuint shader = glCreateShader(type);
if(type == GL_VERTEX_SHADER) {
@ -105,7 +105,7 @@ void Shader::addShader(GLenum type, const char *filename) {
const GLchar *sourceCodeString = sourceCode.c_str();
glShaderSource(shader, 1, &sourceCodeString, NULL);
glShaderSource(shader, 1, &sourceCodeString, nullptr);
glCompileShader(shader);
@ -131,7 +131,7 @@ void Shader::addShader(GLenum type, const char *filename) {
glAttachShader(m_program, shader);
}
GLint Shader::attrib(std::string name) {
GLint Shader::attrib(const std::string &name) {
GLint attrib = glGetAttribLocation(m_program, name.c_str());
if(attrib == -1) {
@ -141,7 +141,7 @@ GLint Shader::attrib(std::string name) {
return attrib;
}
GLint Shader::uniform(std::string name) {
GLint Shader::uniform(const std::string &name) {
GLint uniform = glGetUniformLocation(m_program, name.c_str());
if(uniform == -1) {
@ -151,19 +151,19 @@ GLint Shader::uniform(std::string name) {
return uniform;
}
void Shader::enableVertexAttribArray(std::string name) {
void Shader::enableVertexAttribArray(const std::string &name) {
glEnableVertexAttribArray(attrib(name));
}
void Shader::disableVertexAttribArray(std::string name) {
void Shader::disableVertexAttribArray(const std::string &name) {
glDisableVertexAttribArray(attrib(name));
}
void Shader::setUniform(std::string name, int n) {
void Shader::setUniform(const std::string &name, int n) {
glUniform1i(uniform(name), n);
}
void Shader::setUniform(std::string name, const glm::mat4 &matrix) {
void Shader::setUniform(const std::string &name, const glm::mat4 &matrix) {
glUniformMatrix4fv(uniform(name), 1, GL_FALSE, glm::value_ptr(matrix));
}

View File

@ -18,24 +18,25 @@
#include <iostream>
#include "Application.hpp"
#include "Debug.hpp"
#include "Exception.hpp"
#include "SDLManager.hpp"
#include "SDLLoader.hpp"
int main(int, char *[]) {
try {
SDLManager::init();
SDLLoader sdlLoader;
Application &app = Application::getInstance();
try {
sdlLoader.load();
Application app;
app.run();
}
catch(const Exception &err) {
std::cerr << Debug::textColor(Debug::TextColor::Red, true) << "Fatal error " << Debug::textColor() << err.what() << std::endl;
catch(const Exception &e) {
std::cerr << "Fatal error " << e.what() << std::endl;
return 1;
}
catch(const std::exception &e) {
std::cerr << Debug::textColor(Debug::TextColor::Red, true) << "Exception caught: " << Debug::textColor(0, true) << e.what() << Debug::textColor() << std::endl;
std::cerr << "Exception caught: " << e.what() << std::endl;
return 1;
}
catch(...) {
@ -43,8 +44,6 @@ int main(int, char *[]) {
return 1;
}
SDLManager::free();
return 0;
}

View File

@ -1,45 +0,0 @@
/*
* =====================================================================================
*
* Filename: ApplicationState.cpp
*
* Description:
*
* Version: 1.0
* Created: 14/12/2014 13:50:04
* Revision: none
* Compiler: gcc
*
* Author: Quentin BAZIN, <quent42340@gmail.com>
* Company:
*
* =====================================================================================
*/
#include "Application.hpp"
#include "ApplicationState.hpp"
#include "Config.hpp"
#include "Mouse.hpp"
void ApplicationState::handleEvents() {
Mouse::reset();
SDL_Event event;
while(SDL_PollEvent(&event) != 0) {
switch(event.type) {
case SDL_QUIT:
Application::getInstance().window().close();
break;
case SDL_KEYDOWN:
if(event.key.keysym.sym == SDLK_ESCAPE) {
Application::getInstance().window().close();
}
break;
case SDL_MOUSEMOTION:
Mouse::update(event.motion.x, event.motion.y, event.motion.xrel, event.motion.yrel);
break;
default:
break;
}
}
}

View File

@ -15,8 +15,8 @@
*
* =====================================================================================
*/
#include "Application.hpp"
#include "GameClock.hpp"
#include "SDLHeaders.hpp"
u32 GameClock::ticks = 0;
@ -56,8 +56,7 @@ void GameClock::measureLastFrameDuration() {
void GameClock::updateGame(std::function<void(void)> updateFunc) {
m_numUpdates = 0;
while(m_lag >= m_timestep && m_numUpdates < 10 && Application::getInstance().window().isOpen()) {
while(m_lag >= m_timestep && m_numUpdates < 10) {
ticks += m_timestep;
updateFunc();