From 4c2e0675cbe6d5622439f0c31f2b69ea3f9f7e6d Mon Sep 17 00:00:00 2001 From: Jenny White Date: Mon, 30 Apr 2018 14:40:33 +0300 Subject: [PATCH] compiles (but does not work yet) --- CMakeLists.txt | 3 +- include/CMakeLists.txt | 5 -- include/glez.h | 117 -------------------------- include/glez/CMakeLists.txt | 5 +- include/glez/detail/builder.hpp | 31 +++++++ include/glez/detail/texture.hpp | 2 +- include/glez/draw.hpp | 22 +++++ include/glez/glez.hpp | 17 ---- include/glez/types.hpp | 11 +++ include/internal/CMakeLists.txt | 5 -- include/internal/draw.h | 37 --------- include/internal/fonts.h | 26 ------ include/internal/program.h | 29 ------- include/internal/textures.h | 34 -------- src/CMakeLists.txt | 4 +- src/detail/font.cpp | 2 +- src/detail/program.cpp | 12 +-- src/detail/texture.cpp | 1 - src/draw.cpp | 48 +++++++++++ src/glez.c | 32 ------- src/program.c | 142 -------------------------------- 21 files changed, 122 insertions(+), 463 deletions(-) delete mode 100644 include/CMakeLists.txt delete mode 100644 include/glez.h create mode 100644 include/glez/detail/builder.hpp create mode 100644 include/glez/draw.hpp delete mode 100644 include/internal/CMakeLists.txt delete mode 100644 include/internal/draw.h delete mode 100644 include/internal/fonts.h delete mode 100644 include/internal/program.h delete mode 100644 include/internal/textures.h create mode 100644 src/draw.cpp delete mode 100644 src/program.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 20a2582..7a0e7e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,7 @@ set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS target_include_directories(${PROJECT_NAME} PRIVATE $ + $ $ $ $ @@ -41,7 +42,7 @@ target_compile_definitions(${PROJECT_NAME} PRIVATE ${PNG_DEFINITIONS}) target_link_libraries(${PROJECT_NAME} ${FREETYPE_LIBRARIES} ${PNG_LIBRARIES} ${GLEW_LIBRARIES}) -add_subdirectory(include) +add_subdirectory(include/glez) add_subdirectory(src) add_subdirectory(ftgl) diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt deleted file mode 100644 index 0b989b3..0000000 --- a/include/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -target_sources(glez PRIVATE - "${CMAKE_CURRENT_LIST_DIR}/glez.h") - -add_subdirectory(internal) -add_subdirectory(glez) \ No newline at end of file diff --git a/include/glez.h b/include/glez.h deleted file mode 100644 index 22184c6..0000000 --- a/include/glez.h +++ /dev/null @@ -1,117 +0,0 @@ -/* - * glez.h - * - * Created on: Dec 7, 2017 - * Author: nullifiedcat - */ - -#pragma once - -#ifdef __cplusplus -extern "C" -{ -#endif - -/* Types */ - -typedef struct glez_vec4_s -{ - union { - float data[4]; - struct - { - float r; - float g; - float b; - float a; - }; - struct - { - float x; - float y; - float z; - float w; - }; - }; -} glez_vec4_t, glez_rgba_t; - -typedef unsigned int glez_texture_t; -typedef unsigned int glez_font_t; - -/* State functions */ - -void glez_init(int width, int height); - -void glez_shutdown(); - -void glez_begin(); - -void glez_end(); - -void glez_resize(int width, int height); - -/* Helper functions */ - -static inline glez_rgba_t glez_rgba(unsigned char r, unsigned char g, - unsigned char b, unsigned char a) -{ - glez_rgba_t result; - result.r = (float) r / 255.0f; - result.g = (float) g / 255.0f; - result.b = (float) b / 255.0f; - result.a = (float) a / 255.0f; - return result; -} - -/* Font-related functions */ - -#define GLEZ_FONT_COUNT 64 -#define GLEZ_FONT_INVALID ((glez_font_t) 0xFFFFFFFF) - -glez_font_t glez_font_load(const char *path, float size); - -void glez_font_unload(glez_font_t handle); - -void glez_font_string_size(glez_font_t font, const char *string, float *out_x, - float *out_y); - -/* Texture-related functions */ - -#define GLEZ_TEXTURE_COUNT 64 -#define GLEZ_TEXTURE_INVALID ((glez_texture_t) 0xFFFFFFFF) - -glez_texture_t glez_texture_load_png_rgba(const char *path); - -void glez_texture_unload(glez_texture_t handle); - -void glez_texture_size(glez_texture_t handle, int *width, int *height); - -/* Drawing functions */ - -void glez_line(float x, float y, float dx, float dy, glez_rgba_t color, - float thickness); - -void glez_rect(float x, float y, float w, float h, glez_rgba_t color); - -void glez_rect_outline(float x, float y, float w, float h, glez_rgba_t color, - float thickness); - -void glez_rect_textured(float x, float y, float w, float h, glez_rgba_t color, - glez_texture_t texture, float tx, float ty, float tw, - float th, float angle); - -void glez_string(float x, float y, const char *string, glez_font_t font, - glez_rgba_t color, float *out_x, float *out_y); - -void glez_string_with_outline(float x, float y, const char *string, - glez_font_t font, glez_rgba_t color, - glez_rgba_t outline_color, float outline_width, - int adjust_outline_alpha, float *out_x, - float *out_y); - -void glez_circle(float x, float y, float radius, glez_rgba_t color, - float thickness, int steps); - -#ifdef __cplusplus -} -#endif diff --git a/include/glez/CMakeLists.txt b/include/glez/CMakeLists.txt index f07f4c2..32cbe1b 100644 --- a/include/glez/CMakeLists.txt +++ b/include/glez/CMakeLists.txt @@ -1,5 +1,8 @@ target_sources(glez PRIVATE "${CMAKE_CURRENT_LIST_DIR}/glez.hpp" - "${CMAKE_CURRENT_LIST_DIR}/types.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") add_subdirectory(detail) \ No newline at end of file diff --git a/include/glez/detail/builder.hpp b/include/glez/detail/builder.hpp new file mode 100644 index 0000000..7c016a6 --- /dev/null +++ b/include/glez/detail/builder.hpp @@ -0,0 +1,31 @@ +/* + Created by Jenny White on 30.04.18. + Copyright (c) 2018 nullworks. All rights reserved. +*/ + +#pragma once + +#include +#include "render.hpp" + +namespace glez::detail::builder +{ + +class Builder +{ +public: + Builder(int mode, GLuint texture); + + Builder& setColor(types::rgba color); + + Builder& push(float x, float y, float u, float v); + Builder& push(float x, float y); +protected: + types::rgba color; + + GLuint texture{ 0 }; + int mode{ 0 }; + std::vector vertices{}; +}; + +} \ No newline at end of file diff --git a/include/glez/detail/texture.hpp b/include/glez/detail/texture.hpp index 6845388..a874860 100644 --- a/include/glez/detail/texture.hpp +++ b/include/glez/detail/texture.hpp @@ -5,7 +5,7 @@ #pragma once -#include +#include #include #include #include diff --git a/include/glez/draw.hpp b/include/glez/draw.hpp new file mode 100644 index 0000000..e6a76a5 --- /dev/null +++ b/include/glez/draw.hpp @@ -0,0 +1,22 @@ +/* + Created by Jenny White on 30.04.18. + Copyright (c) 2018 nullworks. All rights reserved. +*/ + +#pragma once + +#include +#include "types.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 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); + +} \ No newline at end of file diff --git a/include/glez/glez.hpp b/include/glez/glez.hpp index 8e5a5c2..74cef53 100644 --- a/include/glez/glez.hpp +++ b/include/glez/glez.hpp @@ -5,14 +5,9 @@ #pragma once -#include - namespace glez { -constexpr unsigned max_textures = 32; -constexpr unsigned max_fonts = 32; - void init(int width, int height); void shutdown(); @@ -21,16 +16,4 @@ void resize(int width, int height); void begin(); void end(); -namespace draw -{ - -void line(int x, int y, int dx, int dy, int color, int thickness); -void rect(int x, int y, int w, int h, int color); -void rect_outline(int x, int y, int w, int h, int color, int thickness); -void circle(int x, int y, int radius, int color, int thickness, int steps); - -void string(int x, int y, const std::string& string, int color, int *width, int *height); -void outlined_string(int x, int y, const std::string& string, int color, int *width, int *height); - -} }; \ No newline at end of file diff --git a/include/glez/types.hpp b/include/glez/types.hpp index b6f201b..b64d4e9 100644 --- a/include/glez/types.hpp +++ b/include/glez/types.hpp @@ -25,6 +25,17 @@ struct rgba }; }; +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(); diff --git a/include/internal/CMakeLists.txt b/include/internal/CMakeLists.txt deleted file mode 100644 index 79ae3c8..0000000 --- a/include/internal/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -target_sources(glez PRIVATE - "${CMAKE_CURRENT_LIST_DIR}/draw.h" - "${CMAKE_CURRENT_LIST_DIR}/fonts.h" - "${CMAKE_CURRENT_LIST_DIR}/program.h" - "${CMAKE_CURRENT_LIST_DIR}/textures.h") \ No newline at end of file diff --git a/include/internal/draw.h b/include/internal/draw.h deleted file mode 100644 index cd13666..0000000 --- a/include/internal/draw.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * draw.h - * - * Created on: Dec 7, 2017 - * Author: nullifiedcat - */ - -#pragma once - -#include "glez.h" - -#include - -struct vertex_main -{ - vec2 position; - vec2 tex_coords; - glez_rgba_t color; - int mode; -}; - -struct draw_state -{ - char dirty; - GLuint texture; - glez_font_t font; -} ds; - -void ds_init(); - -void ds_destroy(); - -void ds_pre_render(); - -void ds_post_render(); - -void ds_bind_texture(GLuint texture); diff --git a/include/internal/fonts.h b/include/internal/fonts.h deleted file mode 100644 index f5b87aa..0000000 --- a/include/internal/fonts.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * fonts.h - * - * Created on: Dec 7, 2017 - * Author: nullifiedcat - */ - -#pragma once - -#include "glez.h" - -#include "freetype-gl.h" - -typedef struct internal_font_s -{ - int init; - - texture_font_t *font; - texture_atlas_t *atlas; -} internal_font_t; - -texture_font_t *internal_font_get(glez_font_t handle); - -void internal_fonts_init(); - -void internal_fonts_destroy(); diff --git a/include/internal/program.h b/include/internal/program.h deleted file mode 100644 index 9df647a..0000000 --- a/include/internal/program.h +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#include - -enum -{ - DRAW_MODE_PLAIN = 1, - DRAW_MODE_TEXTURED, - DRAW_MODE_FREETYPE -}; - -struct program_t -{ - char shader_active; - unsigned shader; - vertex_buffer_t *buffer; -}; - -struct program_t program; - -void shader_screen_size(int width, int height); - -void program_init(int width, int height); - -void program_draw(); - -void program_reset(); - -unsigned program_next_index(); diff --git a/include/internal/textures.h b/include/internal/textures.h deleted file mode 100644 index 4e0cecd..0000000 --- a/include/internal/textures.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * textures.h - * - * Created on: Dec 7, 2017 - * Author: nullifiedcat - */ - -#pragma once - -#include - -typedef struct internal_texture_s -{ - char bound; - char init; - - int width; - int height; - - GLuint texture_id; - char filename[256]; - - GLubyte *data; -} internal_texture_t; - -void internal_textures_init(); - -void internal_textures_destroy(); - -int internal_texture_load_png_rgba(const char *name, internal_texture_t *out); - -internal_texture_t *internal_texture_get(glez_texture_t handle); - -void internal_texture_bind(glez_texture_t handle); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 088a06f..5d70ad2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,7 +1,5 @@ target_sources(glez PRIVATE - "${CMAKE_CURRENT_LIST_DIR}/glez.c" - "${CMAKE_CURRENT_LIST_DIR}/program.c" - + "${CMAKE_CURRENT_LIST_DIR}/draw.cpp" "${CMAKE_CURRENT_LIST_DIR}/glez.cpp" "${CMAKE_CURRENT_LIST_DIR}/font.cpp" "${CMAKE_CURRENT_LIST_DIR}/texture.cpp") diff --git a/src/detail/font.cpp b/src/detail/font.cpp index 4e1a6ea..fb1ac18 100644 --- a/src/detail/font.cpp +++ b/src/detail/font.cpp @@ -85,7 +85,7 @@ glez::types::handle_type create() font &get(glez::types::handle_type handle) { - return <#initializer#>; + return cache->at(0); } } \ No newline at end of file diff --git a/src/detail/program.cpp b/src/detail/program.cpp index 0ef3bfe..331832f 100644 --- a/src/detail/program.cpp +++ b/src/detail/program.cpp @@ -116,7 +116,7 @@ void init(int width, int height) shader = link(compile(shader_vertex, GL_VERTEX_SHADER), compile(shader_fragment, GL_FRAGMENT_SHADER)); - ftgl::mat4 model, view; + mat4 model, view; mat4_set_identity(&model); mat4_set_identity(&view); @@ -136,16 +136,6 @@ void shutdown() glDeleteProgram(shader); } -void resize(int width, int height) -{ - glUseProgram(shader); - ftgl::mat4 projection; - ftgl::mat4_set_identity(&projection); - ftgl::mat4_set_orthographic(&projection, 0, width, height, 0, -1, 1); - glUniformMatrix4fv(glGetUniformLocation(shader, "projection"), 1, 0, projection.data); - glUseProgram(0); -} - void draw() { glUseProgram(shader); diff --git a/src/detail/texture.cpp b/src/detail/texture.cpp index 589a15e..62bd122 100644 --- a/src/detail/texture.cpp +++ b/src/detail/texture.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include #include #include diff --git a/src/draw.cpp b/src/draw.cpp new file mode 100644 index 0000000..3a69f37 --- /dev/null +++ b/src/draw.cpp @@ -0,0 +1,48 @@ +/* + Created by Jenny White on 30.04.18. + Copyright (c) 2018 nullworks. All rights reserved. +*/ + +#include +#include + +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 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) +{ + +} + + +} \ No newline at end of file diff --git a/src/glez.c b/src/glez.c index 0dd23f7..5989dc6 100644 --- a/src/glez.c +++ b/src/glez.c @@ -16,38 +16,6 @@ #include #include -/* State functions */ - -void glez_init(int width, int height) -{ - ds_init(); - program_init(width, height); - internal_fonts_init(); - internal_textures_init(); -} - -void glez_shutdown() -{ - ds_destroy(); - internal_fonts_destroy(); - internal_textures_destroy(); -} - -void glez_begin() -{ - ds_pre_render(); -} - -void glez_end() -{ - ds_post_render(); -} - -void glez_resize(int width, int height) -{ - shader_screen_size(width, height); -} - /* Drawing functions */ void glez_line(float x, float y, float dx, float dy, glez_rgba_t color, diff --git a/src/program.c b/src/program.c deleted file mode 100644 index 92c8631..0000000 --- a/src/program.c +++ /dev/null @@ -1,142 +0,0 @@ -#include -#include - -#include -#include - -#include - -#include "internal/program.h" - -GLuint compile_shader(const char *source, GLenum type) -{ - GLint status; - GLuint shader = glCreateShader(type); - - glShaderSource(shader, 1, &source, 0); - glCompileShader(shader); - glGetShaderiv(shader, GL_COMPILE_STATUS, &status); - - if (status != GL_TRUE) - { - char infolog[512]; - GLsizei length; - glGetShaderInfoLog(shader, 512, &length, infolog); - fprintf(stderr, "GLEZ: Shader compile error: %s\n", infolog); - assert(status == GL_TRUE); - } - - return shader; -} - -const char *shader_ultimate_vert = - "#version 130\n" - "\n" - "uniform mat4 model;\n" - "uniform mat4 view;\n" - "uniform mat4 projection;\n" - "in vec2 vertex;\n" - "in vec2 tex_coord;\n" - "in vec4 color;\n" - "in int drawmode;\n" - "flat out int frag_DrawMode;\n" - "out vec4 frag_Color;\n" - "out vec2 frag_TexCoord;\n" - "void main()\n" - "{\n" - " frag_TexCoord = tex_coord;\n" - " frag_Color = color;\n" - " gl_Position = projection*(view*(model*(vec4(vertex,0.0,1.0))));\n" - " frag_DrawMode = drawmode;\n" - "}"; -const char *shader_ultimate_frag = - "#version 130\n" - "\n" - "uniform sampler2D texture;\n" - "in vec4 frag_Color;\n" - "in vec2 frag_TexCoord;\n" - "flat in int frag_DrawMode;\n" - "void main()\n" - "{\n" - " if (frag_DrawMode == 1)\n" - " gl_FragColor = frag_Color;\n" - " else\n" - " {\n" - " vec4 tex = texture2D(texture, frag_TexCoord);\n" - " if (frag_DrawMode == 2)\n" - " gl_FragColor = frag_Color * tex;\n" - " else if (frag_DrawMode == 3)\n" - " {\n" - " gl_FragColor = vec4(frag_Color.rgb, frag_Color.a * tex.r);\n" - " }\n" - " else\n" - " gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);\n" - " }\n" - "}"; - -void shader_screen_size(int width, int height) -{ - glUseProgram(program.shader); - mat4 projection; - mat4_set_identity(&projection); - mat4_set_orthographic(&projection, 0, width, height, 0, -1, 1); - glUniformMatrix4fv(glGetUniformLocation(program.shader, "projection"), 1, 0, - projection.data); - glUseProgram(0); -} - -void program_init(int width, int height) -{ - program.buffer = - vertex_buffer_new("vertex:2f,tex_coord:2f,color:4f,drawmode:1i"); - program.shader = glCreateProgram(); - GLint status; - GLuint sh_frag = compile_shader(shader_ultimate_frag, GL_FRAGMENT_SHADER); - glAttachShader(program.shader, sh_frag); - glDeleteShader(sh_frag); - - GLuint sh_vert = compile_shader(shader_ultimate_vert, GL_VERTEX_SHADER); - glAttachShader(program.shader, sh_vert); - glDeleteShader(sh_vert); - - glLinkProgram(program.shader); - glGetProgramiv(program.shader, GL_LINK_STATUS, &status); - - assert(status == GL_TRUE); - - glUseProgram(program.shader); - - mat4 model, view, projection; - - mat4_set_identity(&model); - mat4_set_identity(&view); - mat4_set_identity(&projection); - mat4_set_orthographic(&projection, 0, width, height, 0, 0, 1); - - glUniformMatrix4fv(glGetUniformLocation(program.shader, "model"), 1, 0, - model.data); - glUniformMatrix4fv(glGetUniformLocation(program.shader, "view"), 1, 0, - view.data); - glUniformMatrix4fv(glGetUniformLocation(program.shader, "projection"), 1, 0, - projection.data); - glUniform1i(glGetUniformLocation(program.shader, "texture"), 0); - - glUseProgram(0); -} - -void program_draw() -{ - glUseProgram(program.shader); - vertex_buffer_render(program.buffer, GL_TRIANGLES); - glUseProgram(0); -} - -void program_reset() -{ - vertex_buffer_clear(program.buffer); -} - -unsigned program_next_index() -{ - return program.buffer->vertices->size; -}