Skip to content

Commit

Permalink
egl render window done (does not work with NVIDIA)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtiapko committed Nov 25, 2018
1 parent 7e46d6a commit 1f2e4a9
Show file tree
Hide file tree
Showing 26 changed files with 593 additions and 93 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
CC := g++
LD := g++

CC_FLAGS := -std=c++2a -MMD -MP
CC_FLAGS := -std=c++2a -MMD -MP -g
CC_INCLUDES := -I./include
CC_DEFINES :=

LD_FLAGS :=
LD_LIBS := -lEGL -lX11
LD_LIBS := -lEGL -lX11 -lGLEW -lGL

SRC_DIR := src src/graphic
OBJ_DIR := obj
Expand Down
20 changes: 20 additions & 0 deletions include/app.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef __DK_APPLICATION_H__
#define __DK_APPLICATION_H__

#include "ret_code.h"

namespace dk
{

class application
{
public:
virtual void update() = 0;
virtual void render() = 0;

virtual ret_code create() = 0;
};

}

#endif // !__DK_APPLICATION_H__
14 changes: 14 additions & 0 deletions include/assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef __DK_ASSERT_H__
#define __DK_ASSERT_H__

#include <stdlib.h>
#include "log.h"

#define DK_ASSERT(expr, ...) \
if (!(expr)) { \
DK_LOG_PRINT("ASSERT!", __FILE__, __func__, __LINE__, \
"Assertion failed: '", #expr, "'", ##__VA_ARGS__, '\n'); \
std::exit(1); \
}

#endif // !__DK_ASSERT_H__
2 changes: 1 addition & 1 deletion include/containers/hash_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <unordered_map>

namespace lao
namespace dk
{

template<typename KeyT, typename ValT>
Expand Down
2 changes: 1 addition & 1 deletion include/containers/pair.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <utility>

namespace lao
namespace dk
{

template<typename FirstT, typename SecondT>
Expand Down
2 changes: 1 addition & 1 deletion include/containers/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <iterator>
#include <cstddef>

namespace lao
namespace dk
{

inline constexpr std::ptrdiff_t dynamic_extent = -1;
Expand Down
2 changes: 1 addition & 1 deletion include/containers/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <stack>

namespace lao
namespace dk
{

template<typename T>
Expand Down
2 changes: 1 addition & 1 deletion include/containers/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <string>

namespace lao
namespace dk
{

using string = std::string;
Expand Down
2 changes: 1 addition & 1 deletion include/containers/string_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <string_view>

namespace lao
namespace dk
{

using string_view = std::string_view;
Expand Down
2 changes: 1 addition & 1 deletion include/containers/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <vector>

namespace lao
namespace dk
{

template<typename T>
Expand Down
11 changes: 7 additions & 4 deletions include/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
#define __DK_CORE_H__

#include <type_traits>
#include "graphic/render_system.h"
#include "ret_code.h"

namespace dk
{

class core
{
private:
static inline render_system s_active_render_sys;
static bool s_running;
static class render_system s_active_render_sys;
static class application* s_app;

public:
static ret_code create() { return s_active_render_sys.create(); }
static ret_code run();
static ret_code create(class application* app);

static render_system* get_render_sys() { return &s_active_render_sys; }
static class render_system* get_render_sys() { return &s_active_render_sys; }
};

}
Expand Down
16 changes: 16 additions & 0 deletions include/graphic/gl_call.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef __DK_GL_CALL_H__
#define __DK_GL_CALL_H__

#define GL_CALL(call) \
call; \
if (auto err = glGetError(); err != GL_NO_ERROR) \
dk::gl_print_error(__FILE__, __func__, __LINE__, err, #call)

namespace dk
{

void gl_print_error(const char* file, const char* func, int line, int err, const char* call);

}

#endif // !__DK_GL_CALL_H__
18 changes: 11 additions & 7 deletions include/graphic/render_system.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
#ifndef __DK_RENDER_SYSTEM_H__
#define __DK_RENDER_SYSTEM_H__

#include <EGL/egl.h>
#include "ret_code.h"
#include "graphic/shader.h"
#include "graphic/shader_program.h"
#include "graphic/render_window.h"
#include "containers/vector.h"

namespace dk
{

class render_system
{
private:
Display* m_display;
EGLDisplay m_egl_display;
static bool s_is_init;

vector<render_window> m_windows;
vector<shader> m_sahders;

public:
render_system();
render_window* create_window();

Display* get_display() { return m_display; }
EGLDisplay get_egl_display() { return m_egl_display; }
bool is_init() const { return s_is_init; }

ret_code init();
ret_code create();
};

Expand Down
4 changes: 4 additions & 0 deletions include/graphic/render_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ namespace dk
class render_window
{
private:
Display* m_display;
EGLDisplay m_egl_display;
EGLConfig m_config;
EGLContext m_context;
EGLSurface m_surface;
Window m_window;

public:
void render() const { eglSwapBuffers(m_egl_display, m_surface); }

ret_code create();
};

Expand Down
32 changes: 32 additions & 0 deletions include/graphic/shader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef __DK_SHADER_H__
#define __DK_SHADER_H__

#include <GL/gl.h>
#include "ret_code.h"
#include "containers/string_view.h"

namespace dk
{

enum class shader_type
{
VERTEX_SHADER = 0,
FRAGMENT_SHADER
};

class shader
{
private:
GLuint m_shader;

static GLenum convert_type(shader_type type);

public:
GLuint get_native() const { return m_shader; }

ret_code create(shader_type type, string_view src);
};

}

#endif // !__DK_SAHDER_H__
28 changes: 28 additions & 0 deletions include/graphic/shader_program.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef __DK_SHADER_PROGRAM_H__
#define __DK_SHADER_PROGRAM_H__

#include "graphic/shader.h"

namespace dk
{

class shader_program
{
private:
GLuint m_shader_program;

public:
shader_program() : m_shader_program(0) {}

void enable() const;
void disable() const;

ret_code add_shader(const shader& shader) const;

ret_code create();
ret_code link();
};

}

#endif // !__DK_SADHER_PROGRAM_H__
46 changes: 36 additions & 10 deletions include/log.h
Original file line number Diff line number Diff line change
@@ -1,29 +1,55 @@
#ifndef __DK_LOG_H__
#define __DK_LOG_H__

#include <chrono>
#include <iomanip>
#include <iostream>
#include "ret_code.h"
#include "term.h"

#define DK_LOG_HEADER(title) "[" title "] " __FILE__ "::", __func__, " (", __LINE__, ") - "
#define DK_LOG_PRINT(title, ...) dk::log::print(DK_LOG_HEADER(title), __VA_ARGS__)
#define DK_LOG_HEADER(title, file, func, line) "[" title "] ", dk::log_timestamp{}, " - ", file, "::", func, " (", line, ") - "
#define DK_LOG_PRINT(title, file, func, line, ...) dk::log::print(DK_LOG_HEADER(title, file, func, line), __VA_ARGS__)

#define DK_LOG(...) DK_LOG_PRINT(" MSG ", __VA_ARGS__, '\n');
#define DK_LOG_OK(...) DK_LOG_PRINT(" OK! ", __VA_ARGS__, '\n');
#define DK_LOG_WARNING(...) DK_LOG_PRINT("WARNING", __VA_ARGS__, '\n');
#define DK_LOG_ERROR(...) DK_LOG_PRINT(" ERROR ", __VA_ARGS__, '\n');
#define DK_LOG_IMPL(file, func, line, ...) DK_LOG_PRINT(" MSG ", file, func, line, __VA_ARGS__, '\n')
#define DK_LOG_OK_IMPL(file, func, line, ...) DK_LOG_PRINT(" OK! ", file, func, line, __VA_ARGS__, '\n')
#define DK_LOG_WARNING_IMPL(file, func, line, ...) DK_LOG_PRINT("WARNING", file, func, line, __VA_ARGS__, '\n')
#define DK_LOG_ERROR_IMPL(file, func, line, ...) DK_LOG_PRINT(" ERROR ", file, func, line, __VA_ARGS__, '\n')

#define DK_LOG(...) DK_LOG_IMPL(__FILE__, __func__, __LINE__, __VA_ARGS__)
#define DK_LOG_OK(...) DK_LOG_OK_IMPL(__FILE__, __func__, __LINE__, __VA_ARGS__)
#define DK_LOG_WARNING(...) DK_LOG_WARNING_IMPL(__FILE__, __func__, __LINE__, __VA_ARGS__)
#define DK_LOG_ERROR(...) DK_LOG_ERROR_IMPL(__FILE__, __func__, __LINE__, __VA_ARGS__)

namespace dk
{

struct log_timestamp {};

class log
{
private:
static inline std::chrono::steady_clock::time_point s_start;

public:
template<typename T> log operator<<(const T& t) { std::clog << t; return {}; }
log operator<<(term_text_attrib attrib) { term::set(attrib); return {}; }
log operator<<(term_text_color color) { term::set(color); return {}; }
log operator<<(term_back_color color) { term::set(color); return {}; }
template<typename T> log operator<<(const T& t) const { std::clog << t; return {}; }
log operator<<(term_text_attrib attrib) const { term::set(attrib); return {}; }
log operator<<(term_text_color color) const { term::set(color); return {}; }
log operator<<(term_back_color color) const { term::set(color); return {}; }

log operator<<(log_timestamp) const {
auto now = std::chrono::steady_clock::now();
auto dur = std::chrono::duration_cast<std::chrono::microseconds>(now - s_start).count();
std::clog << dur / 1000.0f << " ms";
return {};
}

template<typename... Args> static void print(const Args&... args) { (log{} << ... << args); }

static ret_code create()
{
s_start = std::chrono::steady_clock::now();
return ret_code::OK;
}
};

}
Expand Down
2 changes: 1 addition & 1 deletion include/ret_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ enum class ret_code
{
OK = 0,
WARNING,
ERORR,
ERROR,
FATAL
};

Expand Down
48 changes: 48 additions & 0 deletions include/utils/clock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef __DK_CLOCK_H__
#define __DK_CLOCK_H__

#include <chrono>
#include <thread>

namespace dk
{

class clock
{
private:
std::chrono::high_resolution_clock::time_point m_last;
std::chrono::high_resolution_clock::duration m_update_interval;

public:
clock(uint32_t UPS)
{
m_update_interval = std::chrono::high_resolution_clock::duration(std::chrono::seconds(1)) / UPS;
m_last = std::chrono::high_resolution_clock::now();
}

bool is_elapsed()
{
auto now = std::chrono::high_resolution_clock::now();
auto diff = now - m_last;
if (diff > m_update_interval) {
m_last += m_update_interval;
return true;
}

return false;
}

void wait()
{
auto now = std::chrono::high_resolution_clock::now();
auto diff = now - m_last;
if (diff < m_update_interval)
std::this_thread::sleep_for(m_update_interval - diff);

m_last += m_update_interval;
}
};

}

#endif // !__DK_CLOCK_H__
Loading

0 comments on commit 1f2e4a9

Please sign in to comment.