compiles (but does not work yet)

This commit is contained in:
Jenny White 2018-04-30 14:40:33 +03:00
parent 53ca2dbc38
commit 4c2e0675cb
21 changed files with 122 additions and 463 deletions

View File

@ -26,6 +26,7 @@ set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS
target_include_directories(${PROJECT_NAME} PRIVATE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include/glez>
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/ftgl>
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/pngpp>
$<BUILD_INTERFACE:${FREETYPE_INCLUDE_DIRS}>
@ -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)

View File

@ -1,5 +0,0 @@
target_sources(glez PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/glez.h")
add_subdirectory(internal)
add_subdirectory(glez)

View File

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

View File

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

View File

@ -0,0 +1,31 @@
/*
Created by Jenny White on 30.04.18.
Copyright (c) 2018 nullworks. All rights reserved.
*/
#pragma once
#include <vector>
#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<render::vertex> vertices{};
};
}

View File

@ -5,7 +5,7 @@
#pragma once
#include <GL/gl.h>
#include <freetype-gl.h>
#include <string>
#include <limits>
#include <glez/types.hpp>

22
include/glez/draw.hpp Normal file
View File

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

View File

@ -5,14 +5,9 @@
#pragma once
#include <string>
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);
}
};

View File

@ -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<handle_type>::max();

View File

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

View File

@ -1,37 +0,0 @@
/*
* draw.h
*
* Created on: Dec 7, 2017
* Author: nullifiedcat
*/
#pragma once
#include "glez.h"
#include <vec234.h>
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);

View File

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

View File

@ -1,29 +0,0 @@
#pragma once
#include <vertex-buffer.h>
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();

View File

@ -1,34 +0,0 @@
/*
* textures.h
*
* Created on: Dec 7, 2017
* Author: nullifiedcat
*/
#pragma once
#include <GL/gl.h>
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);

View File

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

View File

@ -85,7 +85,7 @@ glez::types::handle_type create()
font &get(glez::types::handle_type handle)
{
return <#initializer#>;
return cache->at(0);
}
}

View File

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

View File

@ -6,7 +6,6 @@
#include <glez/detail/texture.hpp>
#include <glez/detail/render.hpp>
#include <cassert>
#include <glez/glez.hpp>
#include <vector>
#include <png.hpp>
#include <memory>

48
src/draw.cpp Normal file
View File

@ -0,0 +1,48 @@
/*
Created by Jenny White on 30.04.18.
Copyright (c) 2018 nullworks. All rights reserved.
*/
#include <glez/draw.hpp>
#include <GL/gl.h>
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)
{
}
}

View File

@ -16,38 +16,6 @@
#include <math.h>
#include <string.h>
/* 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,

View File

@ -1,142 +0,0 @@
#include <GL/glew.h>
#include <GL/gl.h>
#include <assert.h>
#include <stdio.h>
#include <mat4.h>
#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;
}