Skip to content

Commit

Permalink
fix freetype-gl
Browse files Browse the repository at this point in the history
  • Loading branch information
structuresound committed Sep 25, 2016
1 parent 24ddfa2 commit 7d452bc
Show file tree
Hide file tree
Showing 11 changed files with 419 additions and 305 deletions.
19 changes: 9 additions & 10 deletions examples/package.cson
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@ build:
to: ""
mac:
frameworks:
AudioUnit: 1
CoreAudio: 1
CoreVideo: 1
Carbon: 1
Cocoa: 1
ForceFeedback: 1
IOKit: 1
OpenGL: 1
AudioUnit: true
CoreAudio: true
CoreVideo: true
Carbon: true
Cocoa: true
ForceFeedback: true
IOKit: true
OpenGL: true
cxxFlags:
std: "c++14"
ldFlags:
liconv: 1
lbz2: 1
liconv: true
path:
install:
mac:
Expand Down
3 changes: 1 addition & 2 deletions examples/source/examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ int main(int, char**){

sdlView->setup();
sdlView->setScene(make_shared<AnimationExample>(sdlView->size.get()));

return sdlView->loop();
}
}
54 changes: 46 additions & 8 deletions package.cson
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build:
sources: matching: ['core/**/*.cpp', 'ext/**/*.cpp', 'ext/**/*.c', '!core/**/*tests*.cpp', '!core/platform/android/**/*.*', '!core/platform/emscripten/**/*.*']
cxxFlags:
std: "c++14"
'fobjc-arc': 1
'fobjc-arc': true
with: "cmake"
path:
install:
Expand Down Expand Up @@ -45,9 +45,34 @@ deps: [
name: "freetype"
fetch: "http://download.savannah.gnu.org/releases/freetype/freetype-2.7.tar.gz"
build:
with: "cmake"
path:
project: "source"
with: "ninja"
mac:
sources:
matching: [
"src/autofit/autofit.c"
"src/base/*.c"
"src/bdf/bdf.c"
"src/bzip2/ftbzip2.c"
"src/cache/ftcache.c"
"src/cff/cff.c"
"src/cid/type1cid.c"
"src/gzip/ftgzip.c"
"src/lzw/ftlzw.c"
"src/pcf/pcf.c"
"src/pfr/pfr.c"
"src/psaux/psaux.c"
"src/pshinter/pshinter.c"
"src/psnames/psnames.c"
"src/raster/raster.c"
"src/sfnt/sfnt.c"
"src/smooth/smooth.c"
"src/truetype/truetype.c"
"src/type1/type1.c"
"src/type42/type42.c"
"src/winfonts/winfnt.c"
]
cFlags:
DFT2_BUILD_LIBRARY: 1
deps: [
git:
repository: "madler/zlib"
Expand All @@ -57,10 +82,23 @@ deps: [
sources: matching: "*.c"
cFlags:
O: 3
Wall: 1
"Wwrite-strings": 1
"Wpointer-arith": 1
"Wconversion": 1
Wall: true
"Wwrite-strings": true
"Wpointer-arith": true
Wconversion: true
,
name: "bz2"
fetch: "http://www.bzip.org/1.0.6/bzip2-1.0.6.tar.gz"
build:
with: "ninja"
sources: matching: "*.c"
cFlags:
O: 2
g: true
Wall: true
D_FILE_OFFSET_BITS: 64
Winline: true
Wconversion: true
]
]
]
145 changes: 98 additions & 47 deletions source/core/gl/fontAtlas.h
Original file line number Diff line number Diff line change
@@ -1,64 +1,115 @@
//
// Created by Leif Shackelford on 4/25/15.
//

#ifndef NODEKITTENX_NKFONTATLAS_H
#define NODEKITTENX_NKFONTATLAS_H
#pragma once

#include "texture.h"
#include "vertexBuffer.h"
#include <freetype-gl/freetype-gl.h>
#include <freetype-gl/texture-atlas.h>
#include <freetype-gl/texture-font.h>
#include <freetype-gl/utf8-utils.h>

class FontAtlas: public Texture {
class FontAtlas : public Texture {

public:

texture_font_t *font = nullptr;
texture_atlas_t *atlas = nullptr;

FontAtlas(std::string fontName, U1t fontSize = 48, I1t width = 512, I1t height = 512){
atlas = texture_atlas_new( width, height, 1);
std::string filename = File::pathForFileNamed(fontName);

nkLog("load font: %s", filename);
font = texture_font_new_from_file( atlas, fontSize, filename.c_str() );

const char * cache = " !\"#$%&'()*+,-./0123456789:;<=>?"
"@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
"`abcdefghijklmnopqrstuvwxyz{|}~";

texture_font_load_glyphs( font, cache );

GetGLError();

bytes = 1;
internal_format = format = GL_RED;
texelType = GL_UNSIGNED_BYTE;
size = width * height * bytes;
target = GL_TEXTURE_2D;
texelArray = atlas->data;
texture_font_t *font = nullptr;
texture_atlas_t *atlas = nullptr;

FontAtlas(std::string fontName, U1t fontSize = 48, I1t width = 512, I1t height = 512) {
atlas = texture_atlas_new(width, height, 1);

const char *cache = " !\"#$%&'()*+,-./0123456789:;<=>?"
"@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
"`abcdefghijklmnopqrstuvwxyz{|}~";

size_t minsize = fontSize, maxsize = fontSize;
size_t count = maxsize - minsize;
size_t i, missed = 0;

auto filename = File::pathForFileNamed(fontName);
nkLog("load font: %s", filename);

font = texture_font_new_from_file(atlas, fontSize, filename.c_str());
missed += texture_font_load_glyphs(font, cache);
// for (i = minsize; i < maxsize; ++i) {
// texture_font_t *font = texture_font_new_from_file(atlas, i, filename.c_str());
// missed += texture_font_load_glyphs(font, cache);
// texture_font_delete(font);
// }

nkLog("Matched font : %s\n", filename);
nkLog("Number of fonts : %ld\n", count);
nkLog("Number of glyphs per font : %ld\n", utf8_strlen(cache));
nkLog("Number of missed glyphs : %ld\n", missed);
nkLog("Total number of glyphs : %ld/%ld\n",
utf8_strlen(cache) * count - missed, utf8_strlen(cache) * count);
nkLog("Texture size : %ldx%ld\n", atlas->width, atlas->height);
nkLog("Texture occupancy : %.2f%%\n",
100.0 * atlas->used / (float) (atlas->width * atlas->height));

GetGLError();

// bytes = 1;
// internal_format = format = GL_RED;
// texelType = GL_UNSIGNED_BYTE;
// size = width * height * bytes;
// target = GL_TEXTURE_2D;
// texelArray = atlas->data;
//
// genGLTexture();
// glTexImage2D(target, 0, internal_format, width, height, 0, format, texelType, texelArray);
// atlas->id = glName;
glName = upload();

nkLog("gen atlas texture : %d", glName);
GetGLError();
}

~FontAtlas() {
if (atlas) {
texture_atlas_delete(atlas);
if (texelArray) {
texelArray = nullptr; // so texture doesn't double free the pointer
}
}
if (font) {
texture_font_delete(font);
}
}

allocate(NK_TEXTURE_CLAMP, 0, 0, 0);
nkLog("gen atlas texture : %d", glName);
int upload() {
assert(atlas->data);

atlas->id = glName;
GetGLError();
if (!atlas->id) {
glGenTextures(1, &atlas->id);
}

~FontAtlas(){
if (atlas) {
texture_atlas_delete(atlas);
if (texelArray) {
texelArray = nullptr; // so texture doesn't double free the pointer
}
}
if (font) {
texture_font_delete(font);
}
glBindTexture(GL_TEXTURE_2D, atlas->id);
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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
if (atlas->depth == 4) {
#ifdef GL_UNSIGNED_INT_8_8_8_8_REV
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, atlas->width, atlas->height,
0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, atlas->data);
#else
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, atlas->width, atlas->height,
0, GL_RGBA, GL_UNSIGNED_BYTE, atlas->data );
#endif
} else if (atlas->depth == 3) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, atlas->width, atlas->height,
0, GL_RGB, GL_UNSIGNED_BYTE, atlas->data);
} else {
#if defined(GL_ES_VERSION_2_0) || defined(GL_ES_VERSION_3_0)
glTexImage2D( GL_TEXTURE_2D, 0, GL_LUMINANCE, atlas->width, atlas->height,
0, GL_LUMINANCE, GL_UNSIGNED_BYTE, atlas->data );
#else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, atlas->width, atlas->height,
0, GL_RED, GL_UNSIGNED_BYTE, atlas->data);
#endif
}
};

return atlas->id;
}

#endif //NODEKITTENX_NKFONTATLAS_H
};
4 changes: 0 additions & 4 deletions source/core/gl/shaderModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@ namespace Shader {
ShaderFunction texFunction;

texFunction.name = "textureMultiplyAlpha";

texFunction.inputType = NKS_TYPE_V4;
texFunction.returnType = NKS_TYPE_V4;

Expand All @@ -406,11 +405,8 @@ namespace Shader {
);
}
#endif

module.fragFunctions.push_back(texFunction);

return module;

}

#pragma mark - POST PROCESS MODULES
Expand Down
2 changes: 1 addition & 1 deletion source/core/gl/shaderProgram.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace Shader {

static shared_ptr <Program> shaderNamed(string name, vector <ShaderModule> modules, int batchSize = 0);

static shared_ptr <Program> shaderNamed(string name, NKS_COLOR_MODE colorMode, U1t numTextures, int numLights,
static shared_ptr <Program> shaderNamed(string name, NKS_COLOR_MODE colorMode, U1t numTextures = 0, int numLights = 0,
int batchSize = 0);

Program();
Expand Down
2 changes: 1 addition & 1 deletion source/core/gl/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ shared_ptr<Texture> Texture::textureWithImageFile(string filePath, bool relative
return nameCache[filePath] = make_shared<Texture>(filePath, relative_path, flags, filter, anistropicFilter);
}

void Texture::genGLTexture(int w, int h){
void Texture::genGLTexture(){

glActiveTexture(GL_TEXTURE0);

Expand Down
4 changes: 2 additions & 2 deletions source/core/gl/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Texture
width = width_;
height = height_;

genGLTexture(width, height);
genGLTexture();
#ifdef GL_UNSIGNED_INT_8_8_8_8_REV
glTexImage2D( target, 0, GL_RGBA, width, height,
0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL );
Expand Down Expand Up @@ -104,7 +104,7 @@ class Texture

bool shouldResizeToTexture {false};
bool keepInMemory {false};
void genGLTexture(int width, int height);
void genGLTexture();


//+(instancetype) textureWithImageNamed:(NSString*)name fromBundleNamed:(NSString*)bundleName;
Expand Down
Loading

0 comments on commit 7d452bc

Please sign in to comment.