Added source files.

This commit is contained in:
Nicolás A. Ortega Froysa 2018-12-08 19:39:23 +01:00
parent 0bede1bee0
commit 16d2bd6466
10 changed files with 3105 additions and 0 deletions

71
CMakeLists.txt Normal file
View File

@ -0,0 +1,71 @@
# Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
# Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
cmake_minimum_required(VERSION 2.8)
project(TTT3D CXX)
# Binary filename
set(TARGET_NAME "ttt3d")
set(TARGET_VERSION "v1.0")
# Use DEBUG by default
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "release")
endif()
string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE)
message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")
# prefer newer versions of OpenGL
set(OpenGL_GL_PREFERENCE "GLVND")
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
find_package(GLEW REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_search_module(SDL2 REQUIRED sdl2)
include_directories(
SYSTEM "include/"
SYSTEM ${SDL2_INCLUDE_DIRS})
set(SRCS
"src/Logger.cpp"
"src/Main.cpp"
"src/System.cpp")
# Define C++ compiler flags
set(CMAKE_CXX_FLAGS "-std=c++14 -Wall -Wextra -Wpedantic -Wfatal-errors -Werror -pedantic-errors -fno-elide-constructors")
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-g -O3")
set(CMAKE_CXX_FLAGS_MINSIZEREL "-Os")
if(CMAKE_BUILD_TYPE STREQUAL "debug" OR CMAKE_BUILD_TYPE STREQUAL "relwithdebinfo")
add_definitions("-DDEBUG")
else()
add_definitions("-DNDEBUG")
endif()
add_definitions("-DVERSION=\"${TARGET_VERSION}\"")
add_executable(${TARGET_NAME}
${SRCS})
target_link_libraries(${TARGET_NAME}
OpenGL::OpenGL
OpenGL::GLU
${GLUT_LIBRARIES}
${GLEW_LIBRARIES}
${SDL2_LIBRARIES})

2550
include/tiny_obj_loader.h Normal file

File diff suppressed because it is too large Load Diff

24
src/AssetManager.cpp Normal file
View File

@ -0,0 +1,24 @@
/*
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "AssetManager.hpp"
void AssetManager::loadOBJ(const std::string &path,
const std::string &name)
{
}

34
src/AssetManager.hpp Normal file
View File

@ -0,0 +1,34 @@
/*
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
class AssetManager {
public:
AssetManager();
void loadOBJ(const std::string &path,
const std::string &name);
void loadSound(const std::string &path,
const std::string &name);
void unloadOBJ(const std::string &name);
void unloadSound(const std::string &name);
private:
};

26
src/GameWorld.hpp Normal file
View File

@ -0,0 +1,26 @@
/*
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
class GameWorld {
public:
GameWorld();
private:
};

65
src/Logger.cpp Normal file
View File

@ -0,0 +1,65 @@
/*
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Logger.hpp"
#include <iostream>
Logger::Logger(const std::string &path) :
#ifdef DEBUG
debug(true)
#else
debug(false)
#endif
{
logFile.open(path);
}
Logger::~Logger() {
logFile.close();
}
void Logger::write(const std::string &message) {
std::cout << message << std::endl;
logFile << message << "\n";
log.push_back(message);
}
void Logger::writeDebug(const std::string &message) {
if(debug)
{
const std::string fullMsg = "[DEBUG] " + message;
std::cout << fullMsg << std::endl;
logFile << fullMsg << "\n";
log.push_back(fullMsg);
}
}
void Logger::writeWarn(const std::string &message) {
const std::string fullMsg = "[WARN] " + message;
std::cout << fullMsg << std::endl;
logFile << fullMsg << "\n";
log.push_back(fullMsg);
}
void Logger::writeError(const std::string &message) {
const std::string fullMsg = "[ERROR] " + message;
std::cerr << fullMsg << std::endl;
logFile << fullMsg << "\n";
log.push_back(fullMsg);
}

51
src/Logger.hpp Normal file
View File

@ -0,0 +1,51 @@
/*
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
#include <fstream>
#include <vector>
/**
* @brief Class used to write lines to the log files as well as the
* standard output.
*/
class Logger {
public:
/**
* @brief Initialize the logger.
*
* @param path Path of the log file.
*/
Logger(const std::string &path);
~Logger();
void write(const std::string &message);
void writeDebug(const std::string &message);
void writeWarn(const std::string &message);
void writeError(const std::string &message);
inline bool getDebug() const { return debug; }
inline void setDebug(bool debug) { this->debug = debug; }
private:
std::vector<std::string> log;
std::ofstream logFile;
bool debug;
};

25
src/Main.cpp Normal file
View File

@ -0,0 +1,25 @@
/*
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "System.hpp"
int main() {
System sys;
sys.run();
return 0;
}

198
src/System.cpp Normal file
View File

@ -0,0 +1,198 @@
/*
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define WINDOW_WIDTH 1200
#define WINDOW_HEIGHT 700
#define FPS 60
#include "System.hpp"
std::unique_ptr<Logger> System::logger;
System::System() : running(true) {
logger = std::make_unique<Logger>("vectorwars.log");
logger->write("Initializing systems.");
if(SDL_Init(/*SDL_INIT_TIMER bitor*/ SDL_INIT_VIDEO bitor
SDL_INIT_AUDIO bitor SDL_INIT_EVENTS))
{
logger->writeError("Failed to initialize SDL2.");
exit(1);
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
SDL_GL_CONTEXT_PROFILE_CORE);
window = SDL_CreateWindow("TTT3D",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WINDOW_WIDTH, WINDOW_HEIGHT,
SDL_WINDOW_OPENGL bitor SDL_WINDOW_SHOWN);
if(not window)
{
logger->writeError("Failed to create window.");
exit(1);
}
//SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
glcontext = SDL_GL_CreateContext(window);
SDL_GL_SetSwapInterval(0);
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glewExperimental = 0;
if(glewInit() not_eq GLEW_OK)
{
logger->writeError("GLEW failed to initialize.");
exit(1);
}
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_CULL_FACE);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
try {
loadShaders();
} catch(std::exception &e) {
logger->writeError(e.what());
exit(1);
}
matrixId = glGetUniformLocation(programId, "MVP");
}
void System::loadShaders() {
logger->writeDebug("Compiling shaders...");
GLuint vshader_id = glCreateShader(GL_VERTEX_SHADER);
GLuint fshader_id = glCreateShader(GL_FRAGMENT_SHADER);
GLint res;
int info_log_length = 0;
// Load/Compile vertex shader
glShaderSource(vshader_id, 1, &vertShader,
nullptr);
glCompileShader(vshader_id);
glGetShaderiv(vshader_id, GL_COMPILE_STATUS, &res);
glGetShaderiv(vshader_id, GL_INFO_LOG_LENGTH,
&info_log_length);
if(info_log_length > 0)
{
char *vshader_error = new char[info_log_length+1];
glGetShaderInfoLog(vshader_id, info_log_length,
nullptr, vshader_error);
std::string error =
"Vector shader compilation failed: ";
error.append(vshader_error);
delete vshader_error;
throw std::runtime_error(error);
}
// Load/Compile fragment shader
glShaderSource(fshader_id, 1, &fragShader, nullptr);
glCompileShader(fshader_id);
glGetShaderiv(fshader_id, GL_COMPILE_STATUS, &res);
glGetShaderiv(fshader_id, GL_INFO_LOG_LENGTH,
&info_log_length);
if(info_log_length > 0)
{
char *fshader_error = new char[info_log_length+1];
glGetShaderInfoLog(fshader_id, info_log_length,
nullptr, fshader_error);
std::string error =
"Fragment shader compilation failed: ";
error.append(fshader_error);
delete fshader_error;
throw std::runtime_error(error);
}
logger->writeDebug("Linking program...");
programId = glCreateProgram();
glAttachShader(programId, vshader_id);
glAttachShader(programId, fshader_id);
glLinkProgram(programId);
// check linkage
glGetProgramiv(programId, GL_LINK_STATUS, &res);
glGetProgramiv(programId, GL_INFO_LOG_LENGTH,
&info_log_length);
if(info_log_length > 0)
{
char *link_error = new char[info_log_length+1];
glGetProgramInfoLog(programId, info_log_length,
nullptr, link_error);
std::string error = "Program linking failed: ";
error.append(link_error);
delete link_error;
throw std::runtime_error(error);
}
// detach
glDetachShader(programId, vshader_id);
glDetachShader(programId, fshader_id);
glDeleteShader(vshader_id);
glDeleteShader(fshader_id);
}
System::~System() {
logger->write("Shutting down systems.");
glDeleteProgram(programId);
SDL_GL_DeleteContext(glcontext);
SDL_DestroyWindow(window);
SDL_Quit();
}
void System::run() {
unsigned int lastTime = SDL_GetTicks();
while(running)
{
syncInputs();
glClear(GL_COLOR_BUFFER_BIT bitor GL_DEPTH_BUFFER_BIT);
glUseProgram(programId);
SDL_GL_SwapWindow(window);
if(SDL_GetTicks() - lastTime < 1000 / FPS)
{
SDL_Delay((1000 / FPS) -
(SDL_GetTicks() - lastTime));
}
lastTime = SDL_GetTicks();
}
}
void System::syncInputs() {
SDL_Event e;
while(SDL_PollEvent(&e))
{
switch(e.type)
{
case SDL_QUIT:
running = false;
break;
}
}
}

61
src/System.hpp Normal file
View File

@ -0,0 +1,61 @@
/*
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "Logger.hpp"
#include <SDL2/SDL.h>
#include <memory>
#include <GL/glew.h>
#include <GL/gl.h>
class System {
public:
System();
~System();
void run();
static std::unique_ptr<Logger> logger;
private:
void loadShaders();
void syncInputs();
const GLchar *vertShader =
"#version 330 core\n"
"layout(location = 0) in vec3 vert_pos;\n"
"layout(location = 1) in vec3 vert_col;\n"
"uniform mat4 MVP;\n"
"out vec3 frag_col;\n"
"void main() {\n"
"gl_Position = MVP * vec4(vert_pos, 1);\n"
"frag_col = vert_col;}\n";
const GLchar *fragShader =
"#version 330 core\n"
"in vec3 frag_col;\n"
"out vec3 color;\n"
"void main() { color = frag_col; }\n";
GLuint programId;
GLuint matrixId;
SDL_Window *window;
SDL_GLContext glcontext;
GLuint vao;
bool running;
};