rect works!

This commit is contained in:
Jenny White 2018-04-30 15:40:49 +03:00
parent 4c2e0675cb
commit 7423347860
15 changed files with 135 additions and 100 deletions

View File

@ -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})

View File

@ -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")

41
include/glez/color.hpp Normal file
View File

@ -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);
}
}

View File

@ -8,7 +8,6 @@
#include <freetype-gl.h>
#include <limits>
#include <string>
#include <glez/types.hpp>
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);
}

View File

@ -5,6 +5,8 @@
#pragma once
#include <vertex-buffer.h>
namespace glez::detail::program
{
@ -25,4 +27,6 @@ void reset();
unsigned next_index();
extern vertex_buffer_t *buffer;
};

View File

@ -6,7 +6,7 @@
#pragma once
#include <freetype-gl.h>
#include "glez/types.hpp"
#include <glez/color.hpp>
namespace glez::detail::render
{
@ -15,7 +15,7 @@ struct vertex
{
ftgl::vec2 position;
ftgl::vec2 uv;
types::rgba color;
rgba color;
int mode;
};

View File

@ -8,7 +8,6 @@
#include <freetype-gl.h>
#include <string>
#include <limits>
#include <glez/types.hpp>
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);
}

View File

@ -6,17 +6,18 @@
#pragma once
#include <string>
#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);
}

View File

@ -6,7 +6,7 @@
#pragma once
#include <string>
#include "types.hpp"
#include <limits>
namespace glez
{
@ -38,7 +38,7 @@ protected:
bool loaded{ false };
glez::types::handle_type handle{ glez::types::undefined };
unsigned handle{ std::numeric_limits<unsigned>::max() };
};
}

View File

@ -6,7 +6,7 @@
#pragma once
#include <string>
#include "types.hpp"
#include <limits>
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<unsigned>::max() };
};
}

View File

@ -1,42 +0,0 @@
/*
Created by Jenny White on 30.04.18.
Copyright (c) 2018 nullworks. All rights reserved.
*/
#pragma once
#include <limits>
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<handle_type>::max();
}

View File

@ -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];
}
}

View File

@ -12,39 +12,32 @@
#include <cassert>
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");

View File

@ -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)

View File

@ -4,42 +4,64 @@
*/
#include <glez/draw.hpp>
#include <GL/gl.h>
#include <glez/detail/render.hpp>
#include <glez/detail/program.hpp>
#include <vertex-buffer.h>
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<int>(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)
{
}