aboutsummaryrefslogtreecommitdiffstats
path: root/yage
diff options
context:
space:
mode:
Diffstat (limited to 'yage')
-rw-r--r--yage/CMakeLists.txt19
-rw-r--r--yage/base/camera2d.cpp2
-rw-r--r--yage/base/glslprogram.cpp2
-rw-r--r--yage/base/glslprogram.h2
-rw-r--r--yage/base/imageloader.cpp7
-rw-r--r--yage/base/sprite.cpp2
-rw-r--r--yage/base/sprite.h4
-rw-r--r--yage/base/spritebatch.h5
-rw-r--r--yage/base/spritesheet.cpp122
-rw-r--r--yage/base/spritesheet.h67
-rw-r--r--yage/base/texture.h7
-rw-r--r--yage/base/vertex.h2
-rw-r--r--yage/base/window.cpp55
-rw-r--r--yage/base/window.h9
-rw-r--r--yage/math/matrix.h20
-rw-r--r--yage/physics/README.md13
-rw-r--r--yage/physics/README.org27
-rw-r--r--yage/physics/particlebody.h2
-rw-r--r--yage/yage.cpp36
-rw-r--r--yage/yage.h25
20 files changed, 300 insertions, 128 deletions
diff --git a/yage/CMakeLists.txt b/yage/CMakeLists.txt
index d45ffa90..e9d4071b 100644
--- a/yage/CMakeLists.txt
+++ b/yage/CMakeLists.txt
@@ -2,32 +2,29 @@ cmake_policy(SET CMP0048 NEW)
project(yage
VERSION 0.1.1.0
- LANGUAGES CXX
- )
+ LANGUAGES CXX)
include(base/CMakeLists.txt)
include(physics/CMakeLists.txt)
include(math/CMakeLists.txt)
set(YAGE_SOURCES
+ yage.cpp
${YAGE_BASE_SOURCES}
${YAGE_PHYSICS_SOURCES}
- ${YAGE_MATH_SOURCES}
- )
+ ${YAGE_MATH_SOURCES})
set(${PROJECT_NAME}_DIR
${PROJECT_SOURCE_DIR})
add_library(${PROJECT_NAME}
- ${YAGE_SOURCES}
- )
+ ${YAGE_SOURCES})
target_include_directories(${PROJECT_NAME}
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
- )
+ ${OPENGL_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME}
- ${OPENGL_LIBRARIES}
- ${GLEW_LIBRARIES}
- ${SDL2_LIBRARIES}
- )
+ ${OPENGL_gl_LIBRARY}
+ glfw
+ glad)
diff --git a/yage/base/camera2d.cpp b/yage/base/camera2d.cpp
index e23b75fa..9eda38ee 100644
--- a/yage/base/camera2d.cpp
+++ b/yage/base/camera2d.cpp
@@ -8,7 +8,7 @@
#include "camera2d.h"
-#include <GL/glew.h>
+#include <glad/glad.h>
namespace yage
{
diff --git a/yage/base/glslprogram.cpp b/yage/base/glslprogram.cpp
index 131a1a28..cff84e40 100644
--- a/yage/base/glslprogram.cpp
+++ b/yage/base/glslprogram.cpp
@@ -6,7 +6,7 @@
* ----------------------------------------------------------------------------
*/
-#include <yage/base/glslprogram.h>
+#include "glslprogram.h"
#include <fstream>
#include <stdexcept>
diff --git a/yage/base/glslprogram.h b/yage/base/glslprogram.h
index fbe5ac5c..545dbe54 100644
--- a/yage/base/glslprogram.h
+++ b/yage/base/glslprogram.h
@@ -9,7 +9,7 @@
#ifndef GLSL_PROGRAM_H
#define GLSL_PROGRAM_H
-#include <GL/glew.h>
+#include <glad/glad.h>
#include <string>
diff --git a/yage/base/imageloader.cpp b/yage/base/imageloader.cpp
index 983c350e..0d7d5df9 100644
--- a/yage/base/imageloader.cpp
+++ b/yage/base/imageloader.cpp
@@ -17,8 +17,6 @@ namespace yage
Texture ImageLoader::loadPng(const std::string &file_path)
{
- Texture texture = {};
-
std::vector<unsigned char> in;
std::vector<unsigned char> out;
unsigned long width, height;
@@ -35,6 +33,8 @@ Texture ImageLoader::loadPng(const std::string &file_path)
std::to_string(error_code));
}
+ Texture texture{0, (int)width, (int)height};
+
glGenTextures(1, &texture.id);
glBindTexture(GL_TEXTURE_2D, texture.id);
@@ -51,9 +51,6 @@ Texture ImageLoader::loadPng(const std::string &file_path)
glBindTexture(GL_TEXTURE_2D, 0);
- texture.width = (int)width;
- texture.height = (int)height;
-
return texture;
}
diff --git a/yage/base/sprite.cpp b/yage/base/sprite.cpp
index 767e1000..9ac4dc55 100644
--- a/yage/base/sprite.cpp
+++ b/yage/base/sprite.cpp
@@ -15,8 +15,6 @@
namespace yage
{
-Sprite::Sprite() = default;
-
Sprite::~Sprite()
{
if (vbo_id_ != 0) {
diff --git a/yage/base/sprite.h b/yage/base/sprite.h
index 5b9baf91..725d2160 100644
--- a/yage/base/sprite.h
+++ b/yage/base/sprite.h
@@ -14,7 +14,7 @@
#include "texture.h"
-#include <GL/glew.h>
+#include <glad/glad.h>
#include <string>
@@ -34,7 +34,7 @@ private:
Texture texture_;
public:
- Sprite();
+ Sprite() = default;
Sprite(const Sprite &) = delete;
Sprite(Sprite &&) = delete;
~Sprite();
diff --git a/yage/base/spritebatch.h b/yage/base/spritebatch.h
index 7235bd25..3b4aca76 100644
--- a/yage/base/spritebatch.h
+++ b/yage/base/spritebatch.h
@@ -6,12 +6,15 @@
* ----------------------------------------------------------------------------
*/
+/** @file
+ */
+
#ifndef YAGE_SPRITE_BATCH_H
#define YAGE_SPRITE_BATCH_H
#include "vertex.h"
-#include <GL/glew.h>
+#include <glad/glad.h>
#include <glm/glm.hpp>
#include <vector>
diff --git a/yage/base/spritesheet.cpp b/yage/base/spritesheet.cpp
new file mode 100644
index 00000000..748cdb5c
--- /dev/null
+++ b/yage/base/spritesheet.cpp
@@ -0,0 +1,122 @@
+/* ----------------------------------------------------------------------------
+ * spritesheet.cpp
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
+ * MIT License, see LICENSE file for more details.
+ * ----------------------------------------------------------------------------
+ */
+
+#include "spritesheet.h"
+
+namespace yage
+{
+
+namespace details
+{
+
+bool SpriteSheetHandler::Null()
+{
+ return true;
+}
+
+bool SpriteSheetHandler::Bool(bool)
+{
+ return true;
+}
+
+bool SpriteSheetHandler::Int(int i)
+{
+ return handleNumber(i);
+}
+
+bool SpriteSheetHandler::Uint(unsigned u)
+{
+ return handleNumber(static_cast<int>(u));
+}
+
+bool SpriteSheetHandler::Int64(int64_t i)
+{
+ return handleNumber(static_cast<int>(i));
+}
+
+bool SpriteSheetHandler::Uint64(uint64_t u)
+{
+ return handleNumber(static_cast<int>(u));
+}
+
+bool SpriteSheetHandler::Double(double d)
+{
+ return handleNumber(static_cast<int>(d));
+}
+
+bool SpriteSheetHandler::String(const char *, rapidjson::SizeType, bool)
+{
+ return true;
+}
+
+bool SpriteSheetHandler::Key(const char *str, rapidjson::SizeType length, bool)
+{
+ current_key_ = std::string(str, length);
+ return true;
+}
+
+bool SpriteSheetHandler::StartObject()
+{
+ depth_++;
+
+ if(depth_ == 3) {
+ current_image_ = current_key_;
+ }
+
+ return true;
+}
+
+bool SpriteSheetHandler::EndObject(rapidjson::SizeType)
+{
+ if(depth_ == 3) {
+ map_[current_image_] = coord_;
+ }
+ depth_--;
+ return true;
+}
+
+bool SpriteSheetHandler::StartArray()
+{
+ return true;
+}
+
+bool SpriteSheetHandler::EndArray(rapidjson::SizeType)
+{
+ return true;
+}
+
+SpriteMap SpriteSheetHandler::spriteMap() const
+{
+ return map_;
+}
+
+bool SpriteSheetHandler::handleNumber(int i)
+{
+ if(current_key_ == "width") {
+ if(depth_ == 1) {
+ image_width_ = i;
+ } else {
+ coord_.width = i;
+ }
+ } else if(current_key_ == "height") {
+ if(depth_ == 1) {
+ image_height_ = i;
+ } else {
+ coord_.height = i;
+ }
+ } else if(current_key_ == "x") {
+ coord_.x = i;
+ } else if(current_key_ == "y") {
+ coord_.y = i;
+ }
+ return true;
+}
+
+} // namespace details
+
+} // namespace yage
diff --git a/yage/base/spritesheet.h b/yage/base/spritesheet.h
index 2b70ad8b..bc60f8b9 100644
--- a/yage/base/spritesheet.h
+++ b/yage/base/spritesheet.h
@@ -11,13 +11,78 @@
#include "texture.h"
+#include <rapidjson/reader.h>
+
+#include <map>
+#include <string>
+
namespace yage
{
+namespace details
+{
+
+struct Coordinate {
+ int x;
+ int y;
+ int width;
+ int height;
+
+ Coordinate() = default;
+
+ Coordinate(int x_i, int y_i, int width_i, int height_i)
+ : x(x_i), y(y_i), width(width_i), height(height_i)
+ {
+ }
+};
+
+typedef std::map<std::string, details::Coordinate> SpriteMap;
+
+class SpriteSheetHandler
+ : public rapidjson::BaseReaderHandler<rapidjson::UTF8<>, SpriteSheetHandler>
+{
+public:
+ bool Null();
+ bool Bool(bool b);
+ bool Int(int i);
+ bool Uint(unsigned u);
+ bool Int64(int64_t i);
+ bool Uint64(uint64_t u);
+ bool Double(double d);
+ bool String(const char *str, rapidjson::SizeType length, bool copy);
+
+ bool Key(const char *str, rapidjson::SizeType length, bool copy);
+ bool StartObject();
+ bool EndObject(rapidjson::SizeType memberCount);
+ bool StartArray();
+ bool EndArray(rapidjson::SizeType memberCount);
+
+ SpriteMap spriteMap() const;
+
+private:
+ std::string current_key_;
+ std::string current_image_;
+ Coordinate coord_;
+ int depth_;
+ int image_width_;
+ int image_height_;
+ SpriteMap map_;
+
+ bool handleNumber(int i);
+};
+
+} // namespace details
+
class SpriteSheet
{
+public:
+ SpriteSheet(std::string pngFileName, std::string jsonFileName);
+
+ void sprite(std::string spriteName) const;
+
private:
- Texture texture_;
+ Texture texture_;
+ details::SpriteMap fileLocations_;
};
} // namespace yage
diff --git a/yage/base/texture.h b/yage/base/texture.h
index d1fdcbf2..cc9dc857 100644
--- a/yage/base/texture.h
+++ b/yage/base/texture.h
@@ -9,7 +9,7 @@
#ifndef GL_TEXTURE_H
#define GL_TEXTURE_H
-#include <GL/glew.h>
+#include <glad/glad.h>
namespace yage
{
@@ -18,6 +18,11 @@ struct Texture {
GLuint id;
int width;
int height;
+
+ Texture(GLuint id_i, int width_i, int height_i)
+ : id(id_i), width(width_i), height(height_i)
+ {
+ }
};
} // namespace yage
diff --git a/yage/base/vertex.h b/yage/base/vertex.h
index 15b46ed9..586e8190 100644
--- a/yage/base/vertex.h
+++ b/yage/base/vertex.h
@@ -9,7 +9,7 @@
#ifndef VERTEX_H
#define VERTEX_H
-#include <GL/glew.h>
+#include <glad/glad.h>
namespace yage
{
diff --git a/yage/base/window.cpp b/yage/base/window.cpp
index a8b6fc02..67f32bb2 100644
--- a/yage/base/window.cpp
+++ b/yage/base/window.cpp
@@ -6,8 +6,7 @@
* ----------------------------------------------------------------------------
*/
-#include <yage/base/window.h>
-#include <GL/glew.h>
+#include "window.h"
#include <iostream>
#include <stdexcept>
@@ -19,60 +18,30 @@ Window::Window() = default;
Window::~Window()
{
- SDL_DestroyWindow(window_);
+ glfwDestroyWindow(window_);
}
void Window::create(const std::string &window_name, int width, int height,
unsigned flags)
{
- Uint32 gl_window_states = 0;
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
- // set the correct input flags
- if (flags & WindowFlags::SHOWN) {
- gl_window_states |= SDL_WINDOW_OPENGL;
- }
- if (flags & WindowFlags::HIDDEN) {
- gl_window_states |= SDL_WINDOW_HIDDEN;
- }
- if (flags & WindowFlags::FULLSCREEN) {
- gl_window_states |= SDL_WINDOW_FULLSCREEN;
- }
- if (flags & WindowFlags::BORDERLESS) {
- gl_window_states |= SDL_WINDOW_BORDERLESS;
- }
-
- // SDL_GL options
-
- // SDL_GL_SetAttribute (SDL_GL_CONTEXT_MAJOR_VERSION, 4);
- // SDL_GL_SetAttribute (SDL_GL_CONTEXT_MINOR_VERSION, 5);
- SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
-
- // create the SDL window
- window_ = SDL_CreateWindow(window_name.c_str(), SDL_WINDOWPOS_CENTERED,
- SDL_WINDOWPOS_CENTERED, width, height,
- gl_window_states);
+ window_ = glfwCreateWindow(width, height, window_name.c_str(), nullptr, nullptr);
if (window_ == nullptr) {
- throw std::runtime_error("SDL_CreateWindow failed");
+ throw std::runtime_error("GLFW Window creation failed");
}
- // initialize the GL context in the window
- SDL_GLContext gl_context = SDL_GL_CreateContext(window_);
- if (gl_context == nullptr) {
- throw std::runtime_error("SDL_GL_CreateContext failed");
- }
-
- // initialize glew
- GLenum error = glewInit();
- if (error != GLEW_OK) {
- throw std::runtime_error("glewInit failed");
- }
+ // initialize the gl context
+ glfwMakeContextCurrent(window_);
// print out the current OpenGL version to debug
std::cout << "*** OpenGL version: " << glGetString(GL_VERSION)
<< " ***\n";
- // set vsync on instead of custom fps limiting
- SDL_GL_SetSwapInterval(1);
+ // set vsync on
+ glfwSwapInterval(1);
+
// set the clear color to black
glClearColor(0.f, 0.5f, 0.f, 1.f);
// set alpha blending
@@ -83,7 +52,7 @@ void Window::create(const std::string &window_name, int width, int height,
void Window::swapBuffer()
{
// swap the window buffer
- SDL_GL_SwapWindow(window_);
+ glfwSwapBuffers(window_);
}
void Window::clearBuffer()
diff --git a/yage/base/window.h b/yage/base/window.h
index 8639e075..eb4cf005 100644
--- a/yage/base/window.h
+++ b/yage/base/window.h
@@ -6,10 +6,13 @@
* ----------------------------------------------------------------------------
*/
+/** @file
+ */
+
#ifndef WINDOW_H
#define WINDOW_H
-#include <SDL2/SDL.h>
+#include <GLFW/glfw3.h>
#include <string>
@@ -24,12 +27,12 @@ enum WindowFlags : unsigned {
BORDERLESS = 0x8,
};
-// window wrapper around SDL_Window pointer
+// window wrapper around GLFWwindow pointer
class Window
{
private:
/// window handle
- SDL_Window *window_ = nullptr;
+ GLFWwindow *window_ = nullptr;
public:
Window();
diff --git a/yage/math/matrix.h b/yage/math/matrix.h
index 3992acfe..3df1509d 100644
--- a/yage/math/matrix.h
+++ b/yage/math/matrix.h
@@ -6,7 +6,7 @@
* ----------------------------------------------------------------------------
*/
-/** @file
+/** @file
*/
#ifndef YAGE_MATH_MATRIX_H
@@ -27,12 +27,12 @@ class Matrix;
/** @internal Namespace for internal details.
*
- * Detail Namespace
+ * Details Namespace
* ================
*
- * This is the namespace used for implementation detail.
+ * This is the namespace used for implementation details.
*/
-namespace detail
+namespace details
{
/** @internal Internal Row class used by the Matrix class to return the
@@ -68,7 +68,7 @@ public:
}
};
-} // namespace detail
+} // namespace details
/** Base Matrix class used by other similar classes.
*/
@@ -76,7 +76,7 @@ template <int Rows = 4, int Cols = 4, class Type = double>
class Matrix
{
// friended with the row class so that it can access protected member data.
- friend class detail::Row<Rows, Cols, Type>;
+ friend class details::Row<Rows, Cols, Type>;
protected:
/// Vector containing the data of the matrix.
@@ -158,14 +158,14 @@ public:
return ss.str();
}
- detail::Row<Rows, Cols, Type> operator[](int row)
+ details::Row<Rows, Cols, Type> operator[](int row)
{
- return detail::Row<Rows, Cols, Type>(this, row);
+ return details::Row<Rows, Cols, Type>(this, row);
}
- detail::Row<Rows, Cols, Type> operator[](int row) const
+ details::Row<Rows, Cols, Type> operator[](int row) const
{
- return detail::Row<Rows, Cols, Type>((Matrix<Rows, Cols, Type> *)this,
+ return details::Row<Rows, Cols, Type>((Matrix<Rows, Cols, Type> *)this,
row);
}
diff --git a/yage/physics/README.md b/yage/physics/README.md
new file mode 100644
index 00000000..c1419155
--- /dev/null
+++ b/yage/physics/README.md
@@ -0,0 +1,13 @@
+# Physics Engine
+
+## Acceleration
+
+speed and position:
+
+I have
+```
+a = dv / dt;
+v = dp / dt;
+```
+
+I am going to use the second order runga kutta method with a = 0, b = 1, alpha = 1 / 2 and beta = 1 / 2.
diff --git a/yage/physics/README.org b/yage/physics/README.org
deleted file mode 100644
index 0620cc93..00000000
--- a/yage/physics/README.org
+++ /dev/null
@@ -1,27 +0,0 @@
-#+ TITLE : README
-#+ DATE : <2017 - 04 - 17 Mon>
-#+ AUTHOR:
-#+ EMAIL : yannherklotz @yann - arch
-#+ OPTIONS : ':nil *:t -:t ::t <:t H:3 \n:nil ^:t arch:headline
-#+ OPTIONS : author : t c : nil creator : comment d : (not"LOGBOOK") date : t
-#+ OPTIONS : e : t email : nil f : t inline : t num : t p : nil pri : nil stat : t
-#+ OPTIONS : tags : t tasks : t tex : t timestamp : t toc : t todo : t | : t
-#+ CREATOR : Emacs 25.1.1(Org mode 8.2.10)
-#+ DESCRIPTION:
-#+ EXCLUDE_TAGS : noexport
-#+ KEYWORDS:
-#+ LANGUAGE : en
-#+ SELECT_TAGS : export
-
-*Physics Engine
-
- **Acceleration,
- speed and position
-
- I have a = dv / dt;
-v = dp / dt;
-
-I am going to use the second order runga kutta method with a = 0, b = 1,
- alpha =
- 1 / 2 and beta =
- 1 / 2
diff --git a/yage/physics/particlebody.h b/yage/physics/particlebody.h
index 18cfff0b..c750c1a4 100644
--- a/yage/physics/particlebody.h
+++ b/yage/physics/particlebody.h
@@ -11,7 +11,7 @@
#include "body.h"
-#include <yage/math/matrix.h>
+#include <math/matrix.h>
namespace yage
{
diff --git a/yage/yage.cpp b/yage/yage.cpp
new file mode 100644
index 00000000..a141d9aa
--- /dev/null
+++ b/yage/yage.cpp
@@ -0,0 +1,36 @@
+/* ----------------------------------------------------------------------------
+ * yage.h
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> -- MIT License
+ * See file LICENSE for more details
+ * ----------------------------------------------------------------------------
+ */
+
+#include "yage.h"
+
+#include <GLFW/glfw3.h>
+
+#include <stdexcept>
+
+namespace yage
+{
+
+void glfwErrorCallback(int, const char *description)
+{
+ fprintf(stderr, "ERROR: %s\n", description);
+}
+
+void init()
+{
+ glfwSetErrorCallback(glfwErrorCallback);
+ if (!glfwInit()) {
+ throw std::runtime_error("GLFW couldn't be initialised");
+ }
+}
+
+void quit()
+{
+ glfwTerminate();
+}
+
+} // namespace yage
diff --git a/yage/yage.h b/yage/yage.h
index 286d4784..4fce87dc 100644
--- a/yage/yage.h
+++ b/yage/yage.h
@@ -26,15 +26,10 @@
#include "base/vertex.h"
#include "base/window.h"
+#include "physics/body.h"
+#include "physics/particlebody.h"
#include "physics/rectanglecollider.h"
#include "physics/rigidbody.h"
-#include "physics/particlebody.h"
-#include "physics/body.h"
-
-
-#include <SDL2/SDL.h>
-
-#include <stdexcept>
/** Project namespace.
*
@@ -44,25 +39,21 @@
namespace yage
{
+extern void glfwErrorCallback(int, const char *);
+
/** Initializes yage.
*
- * This is only there to initialize SDL2.
+ * This is only there to initialize glfw.
*
* @return Returns true if the initialization was successful.
*/
-bool init()
-{
- return SDL_Init(SDL_INIT_VIDEO);
-}
+extern void init();
/** Quit and cleanup yage
*
- * SDL2 needs to clean itself up.
+ * glfw needs to clean itself up.
*/
-void quit()
-{
- SDL_Quit();
-}
+extern void quit();
} // namespace yage