From 742334786069cf2a8dff7dcf5ec25a9fb18d6068 Mon Sep 17 00:00:00 2001 From: Jenny White Date: Mon, 30 Apr 2018 15:40:49 +0300 Subject: [PATCH] rect works! --- CMakeLists.txt | 4 +-- include/glez/CMakeLists.txt | 2 +- include/glez/color.hpp | 41 ++++++++++++++++++++++++++++ include/glez/detail/font.hpp | 5 ++-- include/glez/detail/program.hpp | 4 +++ include/glez/detail/render.hpp | 4 +-- include/glez/detail/texture.hpp | 5 ++-- include/glez/draw.hpp | 15 ++++++----- include/glez/font.hpp | 4 +-- include/glez/texture.hpp | 4 +-- include/glez/types.hpp | 42 ----------------------------- src/detail/font.cpp | 14 +++++++--- src/detail/program.cpp | 39 +++++++++++++++------------ src/detail/texture.cpp | 4 +-- src/draw.cpp | 48 ++++++++++++++++++++++++--------- 15 files changed, 135 insertions(+), 100 deletions(-) create mode 100644 include/glez/color.hpp delete mode 100644 include/glez/types.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a0e7e8..afda586 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS ${CMAKE_BUILD_TYPE_VALUES}) cmake_minimum_required(VERSION 3.0) -project(glez VERSION 0.0.1) +project(glez VERSION 0.1.0) set(CMAKE_CXX_STANDARD 17) @@ -47,6 +47,6 @@ add_subdirectory(src) add_subdirectory(ftgl) install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME} DESTINATION "${lib_dest}") -install(FILES "include/glez.h" DESTINATION "${include_dest}") +install(DIRECTORY "include/glez" DESTINATION "${include_dest}") install(EXPORT ${PROJECT_NAME} DESTINATION "${lib_dest}") install(FILES ${PROJECT_NAME}-config.cmake DESTINATION ${export_dest}) \ No newline at end of file diff --git a/include/glez/CMakeLists.txt b/include/glez/CMakeLists.txt index 32cbe1b..9173a0c 100644 --- a/include/glez/CMakeLists.txt +++ b/include/glez/CMakeLists.txt @@ -1,6 +1,6 @@ target_sources(glez PRIVATE + "${CMAKE_CURRENT_LIST_DIR}/color.hpp" "${CMAKE_CURRENT_LIST_DIR}/glez.hpp" - "${CMAKE_CURRENT_LIST_DIR}/types.hpp" "${CMAKE_CURRENT_LIST_DIR}/font.hpp" "${CMAKE_CURRENT_LIST_DIR}/texture.hpp" "${CMAKE_CURRENT_LIST_DIR}/draw.hpp") diff --git a/include/glez/color.hpp b/include/glez/color.hpp new file mode 100644 index 0000000..a01862f --- /dev/null +++ b/include/glez/color.hpp @@ -0,0 +1,41 @@ +/* + Created by Jenny White on 30.04.18. + Copyright (c) 2018 nullworks. All rights reserved. +*/ + +#pragma once + +namespace glez +{ + +struct rgba +{ + rgba() = default; + inline constexpr rgba(int r, int g, int b) + : r(r / 255.0f), g(g / 255.0f), b(b / 255.0f), a(1.0f) + { + } + inline constexpr rgba(int r, int g, int b, int a) + : r(r / 255.0f), g(g / 255.0f), b(b / 255.0f), a(a / 255.0f) + { + } + + float r; + float g; + float b; + float a; +}; + +namespace color +{ + +constexpr rgba white(255, 255, 255); +constexpr rgba black(0, 0, 0); + +constexpr rgba red(255, 0, 0); +constexpr rgba green(0, 255, 0); +constexpr rgba blue(0, 0, 255); + +} + +} \ No newline at end of file diff --git a/include/glez/detail/font.hpp b/include/glez/detail/font.hpp index 1cc42ba..85eb503 100644 --- a/include/glez/detail/font.hpp +++ b/include/glez/detail/font.hpp @@ -8,7 +8,6 @@ #include #include #include -#include namespace glez::detail::font { @@ -28,7 +27,7 @@ struct font void init(); void shutdown(); -glez::types::handle_type create(); -font& get(glez::types::handle_type handle); +unsigned create(); +font& get(unsigned handle); } \ No newline at end of file diff --git a/include/glez/detail/program.hpp b/include/glez/detail/program.hpp index 7af16c6..89c1799 100644 --- a/include/glez/detail/program.hpp +++ b/include/glez/detail/program.hpp @@ -5,6 +5,8 @@ #pragma once +#include + namespace glez::detail::program { @@ -25,4 +27,6 @@ void reset(); unsigned next_index(); +extern vertex_buffer_t *buffer; + }; \ No newline at end of file diff --git a/include/glez/detail/render.hpp b/include/glez/detail/render.hpp index 366b338..7f60c86 100644 --- a/include/glez/detail/render.hpp +++ b/include/glez/detail/render.hpp @@ -6,7 +6,7 @@ #pragma once #include -#include "glez/types.hpp" +#include namespace glez::detail::render { @@ -15,7 +15,7 @@ struct vertex { ftgl::vec2 position; ftgl::vec2 uv; - types::rgba color; + rgba color; int mode; }; diff --git a/include/glez/detail/texture.hpp b/include/glez/detail/texture.hpp index a874860..14252bf 100644 --- a/include/glez/detail/texture.hpp +++ b/include/glez/detail/texture.hpp @@ -8,7 +8,6 @@ #include #include #include -#include namespace glez::detail::texture { @@ -32,7 +31,7 @@ struct texture void init(); void shutdown(); -glez::types::handle_type create(); -texture& get(glez::types::handle_type handle); +unsigned create(); +texture& get(unsigned handle); } \ No newline at end of file diff --git a/include/glez/draw.hpp b/include/glez/draw.hpp index e6a76a5..aadd5fa 100644 --- a/include/glez/draw.hpp +++ b/include/glez/draw.hpp @@ -6,17 +6,18 @@ #pragma once #include -#include "types.hpp" +#include "color.hpp" +#include "font.hpp" namespace glez::draw { -void line(int x, int y, int dx, int dy, types::rgba color, int thickness); -void rect(int x, int y, int w, int h, types::rgba color); -void rect_outline(int x, int y, int w, int h, types::rgba color, int thickness); -void circle(int x, int y, int radius, types::rgba color, int thickness, int steps); +void line(int x, int y, int dx, int dy, rgba color, int thickness); +void rect(int x, int y, int w, int h, rgba color); +void rect_outline(int x, int y, int w, int h, rgba color, int thickness); +void circle(int x, int y, int radius, rgba color, int thickness, int steps); -void string(int x, int y, const std::string& string, types::handle_type font, types::rgba color, int *width, int *height); -void outlined_string(int x, int y, const std::string& string, types::handle_type font, types::rgba color, types::rgba outline, int *width, int *height); +void string(int x, int y, const std::string& string, const font& font, rgba color, int *width, int *height); +void outlined_string(int x, int y, const std::string& string, const font& font, rgba color, rgba outline, int *width, int *height); } \ No newline at end of file diff --git a/include/glez/font.hpp b/include/glez/font.hpp index f25ffb3..5131871 100644 --- a/include/glez/font.hpp +++ b/include/glez/font.hpp @@ -6,7 +6,7 @@ #pragma once #include -#include "types.hpp" +#include namespace glez { @@ -38,7 +38,7 @@ protected: bool loaded{ false }; - glez::types::handle_type handle{ glez::types::undefined }; + unsigned handle{ std::numeric_limits::max() }; }; } \ No newline at end of file diff --git a/include/glez/texture.hpp b/include/glez/texture.hpp index 4f8cf8b..595b309 100644 --- a/include/glez/texture.hpp +++ b/include/glez/texture.hpp @@ -6,7 +6,7 @@ #pragma once #include -#include "types.hpp" +#include namespace glez { @@ -44,7 +44,7 @@ protected: int height{ 0 }; bool loaded{ false }; - glez::types::handle_type handle{ glez::types::undefined }; + unsigned handle{ std::numeric_limits::max() }; }; } \ No newline at end of file diff --git a/include/glez/types.hpp b/include/glez/types.hpp deleted file mode 100644 index b64d4e9..0000000 --- a/include/glez/types.hpp +++ /dev/null @@ -1,42 +0,0 @@ -/* - Created by Jenny White on 30.04.18. - Copyright (c) 2018 nullworks. All rights reserved. -*/ - -#pragma once - -#include - -namespace glez::types -{ - -struct rgba -{ - union - { - struct - { - float r; - float g; - float b; - float a; - }; - float data[4]; - }; -}; - -struct cached_shape -{ -private: - cached_shape(); -public: - ~cached_shape(); - - const unsigned texture; - const void *const data; -}; - -using handle_type = unsigned; -constexpr handle_type undefined = std::numeric_limits::max(); - -} \ No newline at end of file diff --git a/src/detail/font.cpp b/src/detail/font.cpp index fb1ac18..9153887 100644 --- a/src/detail/font.cpp +++ b/src/detail/font.cpp @@ -78,14 +78,20 @@ void font::stringSize(const std::string &string, float *width, float *height) *height = size_y; } -glez::types::handle_type create() +unsigned create() { - return 0; + for (auto i = 0u; i < cache->size(); ++i) + if (not (*cache)[i].init) + return i; + auto result = cache->size() - 1; + cache->push_back(font{}); + return result; } -font &get(glez::types::handle_type handle) +font &get(unsigned handle) { - return cache->at(0); + assert(cache->at(handle).init); + return (*cache)[handle]; } } \ No newline at end of file diff --git a/src/detail/program.cpp b/src/detail/program.cpp index 331832f..f9e7a91 100644 --- a/src/detail/program.cpp +++ b/src/detail/program.cpp @@ -12,39 +12,32 @@ #include static const char *shader_vertex = R"END( -#version 130 - +#version 150 uniform mat4 model; uniform mat4 view; uniform mat4 projection; - in vec2 vertex; in vec2 tex_coord; in vec4 color; in int drawmode; - flat out int frag_DrawMode; - out vec4 frag_Color; -out vec4 frag_TexCoord; - +out vec2 frag_TexCoord; void main() { + gl_Position = projection*(view*(model*vec4(vertex,0.0,1.0))); frag_TexCoord = tex_coord; frag_Color = color; frag_DrawMode = drawmode; - gl_Position = projection * (view * (model * vec4(vertex, 0.0, 1.0))); } )END"; static const char *shader_fragment = R"END( #version 130 - uniform sampler2D texture; in vec4 frag_Color; in vec2 frag_TexCoord; flat in int frag_DrawMode; - void main() { if (frag_DrawMode == 1) @@ -64,7 +57,6 @@ void main() } )END"; -static vertex_buffer_t *buffer{ nullptr }; static GLuint shader{ 0 }; GLuint compile(const char *source, GLenum type) @@ -72,17 +64,17 @@ GLuint compile(const char *source, GLenum type) GLint status; GLuint result = glCreateShader(type); - glShaderSource(shader, 1, &source, 0); - glCompileShader(shader); - glGetShaderiv(shader, GL_COMPILE_STATUS, &status); + glShaderSource(result, 1, &source, 0); + glCompileShader(result); + glGetShaderiv(result, GL_COMPILE_STATUS, &status); if (status != GL_TRUE) { char error[512]; GLsizei length; - glGetShaderInfoLog(shader, 512, &length, error); - fprintf(stderr, "GLEZ: Shader compile error: %s\n", error); - throw std::runtime_error("Shader compile error"); + glGetShaderInfoLog(result, 512, &length, error); + fprintf(stderr, "GLEZ: Shader compile error: %s, %s\n", error, source); + throw std::runtime_error("Shader compile error: " + std::string(error)); } return result; @@ -110,6 +102,19 @@ GLuint link(GLuint vertex, GLuint fragment) namespace glez::detail::program { +vertex_buffer_t *buffer{ nullptr }; + +void resize(int width, int height) +{ + glUseProgram(shader); + mat4 projection; + mat4_set_identity(&projection); + mat4_set_orthographic(&projection, 0, width, height, 0, -1, 1); + glUniformMatrix4fv(glGetUniformLocation(shader, "projection"), 1, 0, + projection.data); + glUseProgram(0); +} + void init(int width, int height) { buffer = ftgl::vertex_buffer_new("vertex:2f,tex_coord:2f,color:4f,drawmode:1i"); diff --git a/src/detail/texture.cpp b/src/detail/texture.cpp index 62bd122..7a07eed 100644 --- a/src/detail/texture.cpp +++ b/src/detail/texture.cpp @@ -66,13 +66,13 @@ void texture::unload() init = false; } -texture& get(glez::types::handle_type handle) +texture& get(unsigned handle) { assert(cache->at(handle).init); return (*cache)[handle]; } -glez::types::handle_type create() +unsigned create() { for (auto i = 0u; i < cache->size(); ++i) if (not (*cache)[i].init) diff --git a/src/draw.cpp b/src/draw.cpp index 3a69f37..8a67ee2 100644 --- a/src/draw.cpp +++ b/src/draw.cpp @@ -4,42 +4,64 @@ */ #include -#include +#include +#include +#include + +namespace indices +{ + +static GLuint rectangle[6] = { 0, 1, 2, 2, 3, 0 }; + +} namespace glez::draw { -void line(int x, int y, int dx, int dy, types::rgba color, int thickness) +void line(int x, int y, int dx, int dy, rgba color, int thickness) { } -void rect(int x, int y, int w, int h, types::rgba color) +void rect(int x, int y, int w, int h, rgba color) +{ + detail::render::vertex vertices[4]; + + for (auto &vertex : vertices) + { + vertex.mode = static_cast(detail::program::mode::PLAIN); + vertex.color = color; + } + + vertices[0].position = { x, y }; + vertices[1].position = { x, y + h }; + vertices[2].position = { x + w, y + h }; + vertices[3].position = { x + w, y }; + + ftgl::vertex_buffer_push_back(detail::program::buffer, vertices, 4, indices::rectangle, 6); +} + +void rect_outline(int x, int y, int w, int h, rgba color, int thickness) { } -void rect_outline(int x, int y, int w, int h, types::rgba color, int thickness) -{ - -} - -void circle(int x, int y, int radius, types::rgba color, int thickness, +void circle(int x, int y, int radius, rgba color, int thickness, int steps) { } -void string(int x, int y, const std::string &string, types::handle_type font, - types::rgba color, int *width, int *height) +void string(int x, int y, const std::string &string, const font& font, + rgba color, int *width, int *height) { } void outlined_string(int x, int y, const std::string &string, - types::handle_type font, types::rgba color, - types::rgba outline, int *width, int *height) + const font& font, rgba color, + rgba outline, int *width, int *height) { }