Reformatted all files

This commit is contained in:
Rebekah 2022-04-04 16:56:17 -04:00
parent 8c29b72961
commit 72d8bf1644
Signed by: oneechanhax
GPG Key ID: 183EB7902964DAE5
15 changed files with 856 additions and 748 deletions

View File

@ -1,84 +0,0 @@
#!/bin/bash
#
# clang-format-all: a tool to run clang-format on an entire project
# Copyright (C) 2016 Evan Klitzke <evan@eklitzke.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
function usage {
echo "Usage: $0 DIR..."
exit 1
}
if [ $# -eq 0 ]; then
usage
fi
# Variable that will hold the name of the clang-format command
FMT=""
# Some distros just call it clang-format. Others (e.g. Ubuntu) are insistent
# that the version number be part of the command. We prefer clang-format if
# that's present, otherwise we work backwards from highest version to lowest
# version.
for clangfmt in clang-format{,-{4,3}.{9,8,7,6,5,4,3,2,1,0}}; do
if which "$clangfmt" &>/dev/null; then
FMT="$clangfmt"
break
fi
done
# Check if we found a working clang-format
if [ -z "$FMT" ]; then
echo "failed to find clang-format"
exit 1
fi
# Check all of the arguments first to make sure they're all directories
for dir in "$@"; do
if [ ! -d "${dir}" ]; then
echo "${dir} is not a directory"
usage
fi
done
# Find a dominating file, starting from a given directory and going up.
find-dominating-file() {
if [ -r "$1"/"$2" ]; then
return 0
fi
if [ "$1" = "/" ]; then
return 1
fi
find-dominating-file "$(realpath "$1"/..)" "$2"
return $?
}
# Run clang-format -i on all of the things
for dir in "$@"; do
pushd "${dir}" &>/dev/null
if ! find-dominating-file . .clang-format; then
echo "Failed to find dominating .clang-format starting at $PWD"
continue
fi
find . \
\( -name '*.c' \
-o -name '*.cc' \
-o -name '*.cpp' \
-o -name '*.h' \
-o -name '*.hh' \
-o -name '*.hpp' \) \
-exec "${FMT}" -i '{}' \;
popd &>/dev/null
done

View File

@ -10,11 +10,15 @@ 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)
{}
: 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)
{}
: r(r / 255.0f)
, g(g / 255.0f)
, b(b / 255.0f)
, a(a / 255.0f) { }
float r;
float g;

View File

@ -4,47 +4,45 @@
#pragma once
#include <vector>
#include <cstdint>
#include <glez/texture.hpp>
#include <freetype-gl.h>
#include <glez/font.hpp>
#include <glez/glez.hpp>
#include <glez/texture.hpp>
#include <vector>
#include <vertex-buffer.h>
#include <glez/font.hpp>
#include <freetype-gl.h>
namespace glez::detail::record {
class RecordedCommands {
public:
struct segment
{
std::size_t start{ 0 };
std::size_t size{ 0 };
glez::texture *texture{ nullptr };
glez::font *font{ nullptr };
struct segment {
std::size_t start { 0 };
std::size_t size { 0 };
glez::texture* texture { nullptr };
glez::font* font { nullptr };
};
RecordedCommands();
~RecordedCommands();
void reset();
void store(glez::vertex *vertices, size_t vcount,
uint32_t *indices, size_t icount);
void bindTexture(glez::texture *tx);
void bindFont(glez::font *font);
void store(glez::vertex* vertices, size_t vcount,
uint32_t* indices, size_t icount);
void bindTexture(glez::texture* tx);
void bindFont(glez::font* font);
void render();
void end();
protected:
void cutSegment();
ftgl::vertex_buffer_t *vertex_buffer{};
std::vector<segment> segments{};
segment current{};
ftgl::vertex_buffer_t* vertex_buffer {};
std::vector<segment> segments {};
segment current {};
};
extern RecordedCommands *currentRecord;
extern RecordedCommands* currentRecord;
extern bool isReplaying;
} // namespace glez::detail::record

View File

@ -5,10 +5,10 @@
#pragma once
#include <string>
#include "color.hpp"
#include "font.hpp"
#include "texture.hpp"
#include <string>
namespace glez::draw {
@ -17,12 +17,12 @@ void rect(float x, float y, float w, float h, rgba color);
void triangle(float x, float y, float x2, float y2, float x3, float y3, rgba color);
void rect_outline(float x, float y, float w, float h, rgba color, float thickness);
void rect_textured(float x, float y, float w, float h, rgba color,
texture &texture, float tx, float ty, float tw, float th,
float angle);
texture& texture, float tx, float ty, float tw, float th,
float angle);
void circle(float x, float y, float radius, rgba color, float thickness, int steps);
void string(float x, float y, const std::string &string, font &font, rgba color,
float *width, float *height);
void outlined_string(float x, float y, const std::string &string, font &font,
rgba color, rgba outline, float *width, float *height);
void string(float x, float y, const std::string& string, font& font, rgba color,
float* width, float* height);
void outlined_string(float x, float y, const std::string& string, font& font,
rgba color, rgba outline, float* width, float* height);
} // namespace glez::draw

View File

@ -13,19 +13,20 @@ namespace glez {
class font {
public:
font(){}
static font loadFromFile(const std::string &path, float size);
font() { }
static font loadFromFile(const std::string& path, float size);
font(font&&);
~font();
glez::font& operator=(glez::font&&);
//void stringSize(std::string_view string, float* width, float* height);
// void stringSize(std::string_view string, float* width, float* height);
void stringSize(const std::string& string, float* width, float* height);
inline bool isLoaded() { return this->m_font != nullptr && this->atlas != nullptr; };
inline bool isLoaded() { return this->m_font != nullptr && this->atlas != nullptr; };
public:
texture_font_t* m_font = nullptr;
texture_atlas_t* atlas = nullptr;
};
} // namespace glez::detail::font
} // namespace glez

View File

@ -5,9 +5,9 @@
#pragma once
#include <vertex-buffer.h>
#include <freetype-gl.h>
#include <glez/color.hpp>
#include <vertex-buffer.h>
namespace glez {
@ -23,7 +23,7 @@ enum class mode {
FREETYPE
};
extern ftgl::vertex_buffer_t *buffer;
extern ftgl::vertex_buffer_t* buffer;
struct vertex {
ftgl::vec2 position;
ftgl::vec2 uv;
@ -33,4 +33,4 @@ struct vertex {
void bind(GLuint texture);
}; // namespace glez::detail::program
}; // namespace glez

View File

@ -1,5 +1,5 @@
#pragma once
#include <vector>
int decodePNG(unsigned char *&out_image, int &image_width, int &image_height,
const unsigned char *in_png, size_t in_size,
bool convert_to_rgba32 = true);
int decodePNG(unsigned char*& out_image, int& image_width, int& image_height,
const unsigned char* in_png, size_t in_size,
bool convert_to_rgba32 = true);

View File

@ -4,16 +4,13 @@
#pragma once
namespace glez::detail::record
{
namespace glez::detail::record {
class RecordedCommands;
}
namespace glez::record
{
namespace glez::record {
class Record
{
class Record {
public:
Record();
~Record();
@ -22,7 +19,7 @@ public:
void end();
void replay();
detail::record::RecordedCommands *commands{ nullptr };
detail::record::RecordedCommands* commands { nullptr };
};
} // namespace glez::record

View File

@ -5,18 +5,17 @@
#pragma once
#include <freetype-gl.h>
#include <string>
#include <limits>
#include <cstddef>
#include <freetype-gl.h>
#include <limits>
#include <string>
namespace glez
{
namespace glez {
class texture {
public:
texture(){}
static texture loadFromFile(const std::string &path);
texture() { }
static texture loadFromFile(const std::string& path);
static texture loadFromMemory(const std::byte* mem, std::size_t size, unsigned w, unsigned h);
texture(texture&&);
~texture();
@ -26,6 +25,7 @@ public:
void bind();
inline bool isLoaded() { return this->init; }
public:
bool init = false;
bool bound = false;
@ -33,7 +33,7 @@ public:
int height = 0;
GLuint id;
GLubyte *data;
GLubyte* data;
};
} // namespace glez::texture
} // namespace glez

View File

@ -3,23 +3,20 @@
*/
#include <cstddef>
#include <glez/detail/record.hpp>
#include <glez/record.hpp>
#include <glez/glez.hpp>
#include <cstring>
#include <glez/detail/record.hpp>
#include <glez/glez.hpp>
#include <glez/record.hpp>
namespace glez::detail::record
{
namespace glez::detail::record {
void RecordedCommands::render()
{
void RecordedCommands::render() {
isReplaying = true;
vertex_buffer_render_setup(vertex_buffer, GL_TRIANGLES);
for (const auto &i : segments) {
for (const auto& i : segments) {
if (i.texture) {
i.texture->bind();
}
else if (i.font) {
} else if (i.font) {
if (i.font->atlas->id == 0) {
glGenTextures(1, &i.font->atlas->id);
}
@ -32,18 +29,18 @@ void RecordedCommands::render()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, i.font->atlas->width,
i.font->atlas->height, 0, GL_RED, GL_UNSIGNED_BYTE,
i.font->atlas->data);
i.font->atlas->height, 0, GL_RED, GL_UNSIGNED_BYTE,
i.font->atlas->data);
i.font->atlas->dirty = 0;
}
}
glDrawElements(GL_TRIANGLES, i.size, GL_UNSIGNED_INT, (void *) (i.start * 4));
glDrawElements(GL_TRIANGLES, i.size, GL_UNSIGNED_INT, (void*)(i.start * 4));
}
vertex_buffer_render_finish(vertex_buffer);
isReplaying = false;
}
void RecordedCommands::store(glez::vertex *vertices, size_t vcount, uint32_t *indices, size_t icount) {
void RecordedCommands::store(glez::vertex* vertices, size_t vcount, uint32_t* indices, size_t icount) {
vertex_buffer_push_back(vertex_buffer, vertices, vcount, indices, icount);
}
@ -61,24 +58,21 @@ void RecordedCommands::reset() {
memset(&current, 0, sizeof(current));
}
void RecordedCommands::bindTexture(glez::texture *tx) {
void RecordedCommands::bindTexture(glez::texture* tx) {
if (current.texture != tx) {
cutSegment();
current.texture = tx;
}
}
void RecordedCommands::bindFont(glez::font *font)
{
if (current.font != font)
{
void RecordedCommands::bindFont(glez::font* font) {
if (current.font != font) {
cutSegment();
current.font = font;
}
}
void RecordedCommands::cutSegment()
{
void RecordedCommands::cutSegment() {
current.size = vertex_buffer->indices->size - current.start;
if (current.size)
segments.push_back(current);
@ -86,39 +80,33 @@ void RecordedCommands::cutSegment()
current.start = vertex_buffer->indices->size;
}
void RecordedCommands::end()
{
void RecordedCommands::end() {
cutSegment();
}
RecordedCommands *currentRecord{ nullptr };
bool isReplaying{ false };
RecordedCommands* currentRecord { nullptr };
bool isReplaying { false };
} // namespace glez::detail::record
glez::record::Record::Record()
{
commands = new glez::detail::record::RecordedCommands{};
glez::record::Record::Record() {
commands = new glez::detail::record::RecordedCommands {};
}
glez::record::Record::~Record()
{
glez::record::Record::~Record() {
delete commands;
}
void glez::record::Record::begin()
{
void glez::record::Record::begin() {
detail::record::currentRecord = commands;
commands->reset();
}
void glez::record::Record::end()
{
void glez::record::Record::end() {
commands->end();
detail::record::currentRecord = nullptr;
}
void glez::record::Record::replay()
{
void glez::record::Record::replay() {
commands->render();
}

View File

@ -4,29 +4,28 @@
*/
#include <cassert>
#include <cmath>
#include <cstring>
#include <glez/detail/record.hpp>
#include <glez/detail/render.hpp>
#include <glez/draw.hpp>
#include <glez/font.hpp>
#include <glez/detail/render.hpp>
#include <glez/glez.hpp>
#include <vertex-buffer.h>
#include <cstring>
#include <glez/texture.hpp>
#include <cmath>
#include <glez/detail/record.hpp>
#include <vertex-buffer.h>
namespace indices
{
namespace indices {
static GLuint rectangle[6] = { 0, 1, 2, 2, 3, 0 };
static GLuint triangle[3] = { 0, 1, 2 };
static GLuint triangle[3] = { 0, 1, 2 };
} // namespace indices
void internal_draw_string(float x, float y, const std::string &string,
glez::font& font, glez::rgba color, float *width, float *height) {
void internal_draw_string(float x, float y, const std::string& string,
glez::font& font, glez::rgba color, float* width, float* height) {
assert(font.isLoaded());
auto* fnt = font.m_font;
float pen_x = x;
float pen_y = y + fnt->height / 1.5f;
float pen_x = x;
float pen_y = y + fnt->height / 1.5f;
float size_y = 0;
if (glez::detail::record::currentRecord) {
@ -43,18 +42,18 @@ void internal_draw_string(float x, float y, const std::string &string,
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, fnt->atlas->width,
fnt->atlas->height, 0, GL_RED, GL_UNSIGNED_BYTE,
fnt->atlas->data);
fnt->atlas->height, 0, GL_RED, GL_UNSIGNED_BYTE,
fnt->atlas->data);
fnt->atlas->dirty = 0;
}
}
const char *sstring = string.c_str();
const char* sstring = string.c_str();
bool skipped{ false };
bool skipped { false };
for (size_t i = 0; i < string.size(); ++i) {
texture_glyph_t *glyph = texture_font_find_glyph(fnt, &sstring[i]);
texture_glyph_t* glyph = texture_font_find_glyph(fnt, &sstring[i]);
if (glyph == NULL) {
texture_font_load_glyph(fnt, &sstring[i]);
if (!skipped)
@ -64,7 +63,7 @@ void internal_draw_string(float x, float y, const std::string &string,
}
skipped = false;
glez::vertex vertices[4];
for (auto &vertex : vertices) {
for (auto& vertex : vertices) {
vertex.color = color;
vertex.mode = static_cast<int>(glez::mode::FREETYPE);
}
@ -72,26 +71,26 @@ void internal_draw_string(float x, float y, const std::string &string,
if (i > 0)
x += texture_glyph_get_kerning(glyph, &sstring[i - 1]);
float x0 = (int) (pen_x + glyph->offset_x);
float y0 = (int) (pen_y - glyph->offset_y);
float x1 = (int) (x0 + glyph->width);
float y1 = (int) (y0 + glyph->height);
float x0 = (int)(pen_x + glyph->offset_x);
float y0 = (int)(pen_y - glyph->offset_y);
float x1 = (int)(x0 + glyph->width);
float y1 = (int)(y0 + glyph->height);
float s0 = glyph->s0;
float t0 = glyph->t0;
float s1 = glyph->s1;
float t1 = glyph->t1;
vertices[0].position = { x0, y0 };
vertices[0].uv = { s0, t0 };
vertices[0].uv = { s0, t0 };
vertices[1].position = { x0, y1 };
vertices[1].uv = { s0, t1 };
vertices[1].uv = { s0, t1 };
vertices[2].position = { x1, y1 };
vertices[2].uv = { s1, t1 };
vertices[2].uv = { s1, t1 };
vertices[3].position = { x1, y0 };
vertices[3].uv = { s1, t0 };
vertices[3].uv = { s1, t0 };
pen_x += glyph->advance_x;
// pen_x = (int) pen_x + 1;
@ -123,8 +122,8 @@ void line(float x, float y, float dx, float dy, rgba color, float thickness) {
vertex vertices[4];
for (auto &vertex : vertices) {
vertex.mode = static_cast<int>(mode::PLAIN);
for (auto& vertex : vertices) {
vertex.mode = static_cast<int>(mode::PLAIN);
vertex.color = color;
}
@ -155,17 +154,17 @@ void line(float x, float y, float dx, float dy, rgba color, float thickness) {
if (detail::record::currentRecord)
detail::record::currentRecord->store(vertices, 4, indices::rectangle,
6);
6);
else
ftgl::vertex_buffer_push_back(buffer, vertices, 4,
indices::rectangle, 6);
indices::rectangle, 6);
}
void rect(float x, float y, float w, float h, rgba color) {
vertex vertices[4];
for (auto &vertex : vertices) {
vertex.mode = static_cast<int>(mode::PLAIN);
for (auto& vertex : vertices) {
vertex.mode = static_cast<int>(mode::PLAIN);
vertex.color = color;
}
@ -183,8 +182,8 @@ void rect(float x, float y, float w, float h, rgba color) {
void triangle(float x, float y, float x2, float y2, float x3, float y3, rgba color) {
vertex vertices[3];
for (auto &vertex : vertices) {
vertex.mode = static_cast<int>(mode::PLAIN);
for (auto& vertex : vertices) {
vertex.mode = static_cast<int>(mode::PLAIN);
vertex.color = color;
}
@ -206,7 +205,7 @@ void rect_outline(float x, float y, float w, float h, rgba color, float thicknes
}
void circle(float x, float y, float radius, rgba color, float thickness,
int steps) {
int steps) {
float px = 0;
float py = 0;
for (int i = 0; i <= steps; i++) {
@ -220,19 +219,19 @@ void circle(float x, float y, float radius, rgba color, float thickness,
}
}
void string(float x, float y, const std::string &string, font &font, rgba color, float *width, float *height) {
auto fnt = font.m_font;
fnt->rendermode = RENDER_NORMAL;
void string(float x, float y, const std::string& string, font& font, rgba color, float* width, float* height) {
auto fnt = font.m_font;
fnt->rendermode = RENDER_NORMAL;
fnt->outline_thickness = 0.0f;
internal_draw_string(x, y, string, font, color, width, height);
}
void outlined_string(float x, float y, const std::string& string, font& font, rgba color, rgba outline, float *width, float *height) {
auto fnt = font.m_font;
fnt->rendermode = RENDER_OUTLINE_POSITIVE;
void outlined_string(float x, float y, const std::string& string, font& font, rgba color, rgba outline, float* width, float* height) {
auto fnt = font.m_font;
fnt->rendermode = RENDER_OUTLINE_POSITIVE;
fnt->outline_thickness = 1.0f;
internal_draw_string(x, y, string, font, outline, width, height);
fnt->rendermode = RENDER_NORMAL;
fnt->rendermode = RENDER_NORMAL;
fnt->outline_thickness = 0.0f;
internal_draw_string(x, y, string, font, color, width, height);
}
@ -242,8 +241,8 @@ void rect_textured(float x, float y, float w, float h, rgba color, texture& text
texture.load();*/
assert(texture.isLoaded());
//if (!texture.canLoad())
//return;
// if (!texture.canLoad())
// return;
if (glez::detail::record::currentRecord)
glez::detail::record::currentRecord->bindTexture(&texture);
@ -252,8 +251,8 @@ void rect_textured(float x, float y, float w, float h, rgba color, texture& text
vertex vertices[4];
for (auto &vertex : vertices) {
vertex.mode = static_cast<int>(mode::TEXTURED);
for (auto& vertex : vertices) {
vertex.mode = static_cast<int>(mode::TEXTURED);
vertex.color = color;
}
@ -266,7 +265,7 @@ void rect_textured(float x, float y, float w, float h, rgba color, texture& text
float cx = x + float(w) / 2.0f;
float cy = y + float(h) / 2.0f;
for (auto &v : vertices) {
for (auto& v : vertices) {
float ox = v.position.x;
float oy = v.position.y;

View File

@ -3,14 +3,14 @@
Copyright (c) 2018 nullworks. All rights reserved.
*/
#include <glez/font.hpp>
#include <vector>
#include <memory>
#include <cassert>
#include <glez/font.hpp>
#include <memory>
#include <vector>
namespace glez {
font font::loadFromFile(const std::string &path, float size) {
font font::loadFromFile(const std::string& path, float size) {
assert(size > 0);
font ret;
@ -36,7 +36,7 @@ glez::font& font::operator=(glez::font&& var) {
return *this;
}
void font::stringSize(const std::string& string, float* width, float* height) {
assert(this->isLoaded());
assert(this->isLoaded());
float penX = 0;
float size_x = 0;
@ -44,12 +44,11 @@ void font::stringSize(const std::string& string, float* width, float* height) {
texture_font_load_glyphs(m_font, string.c_str());
const char *sstring = string.c_str();
const char* sstring = string.c_str();
for (size_t i = 0; i < string.size(); ++i)
{
for (size_t i = 0; i < string.size(); ++i) {
// c_str guarantees a NULL terminator
texture_glyph_t *glyph = texture_font_find_glyph(m_font, &sstring[i]);
texture_glyph_t* glyph = texture_font_find_glyph(m_font, &sstring[i]);
if (glyph == nullptr)
continue;
@ -68,4 +67,4 @@ void font::stringSize(const std::string& string, float* width, float* height) {
*height = size_y;
}
} // namespace glez::detail::font
} // namespace glez

View File

@ -3,20 +3,19 @@
Copyright (c) 2018 nullworks. All rights reserved.
*/
#include <freetype-gl.h>
#include <vertex-buffer.h>
#include <mat4.h>
#include <glez/glez.hpp>
#include <cstdio>
#include <stdexcept>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <freetype-gl.h>
#include <glez/detail/record.hpp>
#include <glez/detail/render.hpp>
#include <glez/font.hpp>
#include <cstring>
#include <glez/glez.hpp>
#include <glez/detail/record.hpp>
#include <mat4.h>
#include <stdexcept>
#include <vertex-buffer.h>
static const char *shader_vertex = R"END(
static const char* shader_vertex = R"END(
#version 130
uniform mat4 projection;
in vec2 vertex;
@ -35,7 +34,7 @@ void main()
}
)END";
static const char *shader_fragment = R"END(
static const char* shader_fragment = R"END(
#version 130
uniform sampler2D texture;
in vec4 frag_Color;
@ -60,9 +59,9 @@ void main()
}
)END";
static GLuint shader{ 0 };
static GLuint shader { 0 };
static GLuint compile(const char *source, GLenum type) {
static GLuint compile(const char* source, GLenum type) {
GLint status;
GLuint result = glCreateShader(type);
@ -70,8 +69,7 @@ static GLuint compile(const char *source, GLenum type) {
glCompileShader(result);
glGetShaderiv(result, GL_COMPILE_STATUS, &status);
if (status != GL_TRUE)
{
if (status != GL_TRUE) {
char error[512];
GLsizei length;
glGetShaderInfoLog(result, 512, &length, error);
@ -113,8 +111,7 @@ void resize(int width, int height) {
glUseProgram(0);
}
void init(int width, int height)
{
void init(int width, int height) {
buffer = ftgl::vertex_buffer_new("vertex:2f,tex_coord:2f,color:4f,drawmode:1i");
shader = link(compile(shader_vertex, GL_VERTEX_SHADER), compile(shader_fragment, GL_FRAGMENT_SHADER));
@ -130,8 +127,7 @@ void shutdown() {
glDeleteProgram(shader);
}
static GLuint current_texture{ 0 };
static GLuint current_texture { 0 };
void begin() {
glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT | GL_TEXTURE_BIT | GL_COLOR_BUFFER_BIT);
@ -183,4 +179,4 @@ void bind(GLuint texture) {
}
}
}
} // namespace glez

File diff suppressed because it is too large Load Diff

View File

@ -3,27 +3,25 @@
Copyright (c) 2018 nullworks. All rights reserved.
*/
#include <glez/texture.hpp>
#include <glez/glez.hpp>
#include <cassert>
#include <vector>
#include <glez/picopng/picopng.hpp>
#include <memory>
#include <cstring>
#include <glez/glez.hpp>
#include <glez/picopng/picopng.hpp>
#include <glez/texture.hpp>
#include <memory>
#include <vector>
#include <fstream> // required to load the file
namespace glez
{
namespace glez {
void texture::bind()
{
void texture::bind() {
if (!bound) {
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, data);
GL_RGBA, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
@ -33,7 +31,7 @@ void texture::bind()
glez::bind(id);
}
texture texture::loadFromFile(const std::string &path) {
texture texture::loadFromFile(const std::string& path) {
std::ifstream file(path.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
std::streamsize size = 0;
@ -45,8 +43,8 @@ texture texture::loadFromFile(const std::string &path) {
if (size < 1)
throw std::runtime_error("Unable to load texture from file!");
unsigned char *buffer = new unsigned char[(size_t) size + 1];
file.read((char *) buffer, size);
unsigned char* buffer = new unsigned char[(size_t)size + 1];
file.read((char*)buffer, size);
file.close();
texture ret;
int error = decodePNG(ret.data, ret.width, ret.height, buffer, size);
@ -56,13 +54,12 @@ texture texture::loadFromFile(const std::string &path) {
printf("Error loading texture, error code %i\n", error);
throw std::runtime_error("See above!");
}
ret.init = true;
ret.init = true;
ret.bound = false;
ret.id = 0;
ret.id = 0;
return ret;
}
texture texture::loadFromMemory(const std::byte* mem, std::size_t size, unsigned w, unsigned h) {
if (size < 1)
throw std::runtime_error("Unable to load texture from memory!");
@ -74,9 +71,9 @@ texture texture::loadFromMemory(const std::byte* mem, std::size_t size, unsigned
ret.width = w;
ret.height = h;
ret.data = buffer;
ret.init = true;
ret.init = true;
ret.bound = false;
ret.id = 0;
ret.id = 0;
return ret;
}
@ -89,20 +86,20 @@ texture::~texture() {
}
texture& texture::operator=(texture&& var) {
this->init = var.init;
this->bound = var.bound;
this->width = var.width;
this->height = var.height;
this->id = var.id;
this->data = var.data;
this->init = var.init;
this->bound = var.bound;
this->width = var.width;
this->height = var.height;
this->id = var.id;
this->data = var.data;
var.init = false;
var.bound = false;
var.width = 0;
var.height = 0;
var.id = 0;
var.data = nullptr;
return *this;
var.init = false;
var.bound = false;
var.width = 0;
var.height = 0;
var.id = 0;
var.data = nullptr;
return *this;
}
} // namespace glez::detail::texture
} // namespace glez