SDL_ttf now required. Added basic Font class and default TTF font (Amaranth).

master
kts 2015-03-06 05:53:09 -08:00
parent 482db6b6f1
commit 1318be4fc0
14 changed files with 187 additions and 21 deletions

View File

@ -52,7 +52,7 @@
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>SDL2.lib;SDL2main.lib;opengl32.lib;glew32.lib;SDL2_image.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>SDL2.lib;SDL2main.lib;opengl32.lib;glew32.lib;SDL2_image.lib;SDL2_ttf.lib;%(AdditionalDependencies)</AdditionalDependencies>
<SubSystem>Console</SubSystem>
</Link>
<PostBuildEvent>
@ -60,6 +60,8 @@
xcopy /d /y "..\..\..\..\Dev\SDL2-2.0.3\lib\x86\SDL2.dll" "$(OutDir)"
xcopy /d /y "..\..\..\..\Dev\SDL2-2.0.3\lib\x86\SDL2_image.dll" "$(OutDir)"
xcopy /d /y "..\..\..\..\Dev\SDL2-2.0.3\lib\x86\libpng16-16.dll" "$(OutDir)"
xcopy /d /y "..\..\..\..\Dev\SDL2-2.0.3\lib\x86\SDL2_ttf.dll" "$(OutDir)"
xcopy /d /y "..\..\..\..\Dev\SDL2-2.0.3\lib\x86\libfreetype-6.dll" "$(OutDir)"
xcopy /s /e /d /y "..\..\data" "$(OutDir)data\"</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
@ -84,6 +86,7 @@ xcopy /s /e /d /y "..\..\data" "$(OutDir)data\"</Command>
<ClCompile Include="..\..\src\checksum.cpp" />
<ClCompile Include="..\..\src\Core.cpp" />
<ClCompile Include="..\..\src\fio.cpp" />
<ClCompile Include="..\..\src\Font.cpp" />
<ClCompile Include="..\..\src\Gui.cpp" />
<ClCompile Include="..\..\src\gui\GuiButton.cpp" />
<ClCompile Include="..\..\src\gui\GuiElement.cpp" />
@ -130,6 +133,7 @@ xcopy /s /e /d /y "..\..\data" "$(OutDir)data\"</Command>
<ClInclude Include="..\..\src\common.hpp" />
<ClInclude Include="..\..\src\Core.hpp" />
<ClInclude Include="..\..\src\fio.hpp" />
<ClInclude Include="..\..\src\Font.hpp" />
<ClInclude Include="..\..\src\Gui.hpp" />
<ClInclude Include="..\..\src\gui\GuiButton.hpp" />
<ClInclude Include="..\..\src\gui\GuiElement.hpp" />

View File

@ -111,6 +111,9 @@
<ClCompile Include="..\..\src\gui\GuiList.cpp">
<Filter>Classes\Gui</Filter>
</ClCompile>
<ClCompile Include="..\..\src\Font.cpp">
<Filter>Classes</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="..\..\..\..\Dev\SDL2-2.0.3\lib\x86\SDL2.dll">
@ -220,5 +223,8 @@
<ClInclude Include="..\..\src\gui\GuiList.hpp">
<Filter>Classes\Gui</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Font.hpp">
<Filter>Classes</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -1,10 +0,0 @@
data/shaders/fb_fs.100.glsl 0 259
data/models/cube.obj 825768822 432
data/shaders/fb_vs.100.glsl 0 177
data/shaders/default_fs.100.glsl 0 244
data/shaders/default_vs.100.glsl 0 334
data/shaders/fb_fs.glsl 3713587946 233
data/shaders/fb_vs.glsl 2722618720 177
data/shaders/default_fs.glsl 1466891365 218
data/shaders/default_vs.glsl 685822220 336
data/models/chest.obj 870013261 4806

View File

@ -0,0 +1,11 @@
This directory contains a default font to be used by the program.
To make replacement easier, the following default names are assumed:
default.ttf
defaultb.ttf
defaultbi.ttf
defaulti.ttf
In the current distribution, the SIL licensed "Amaranth" font is used per-default with the following renaming:
Amaranth-Regular.ttf -> default.ttf

View File

@ -0,0 +1,44 @@
Copyright (c) 2011, Gesine Todt (hallo@gesine-todt.de),
with Reserved Font Name Amaranth.
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at: http://scripts.sil.org/OFL
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide development of collaborative font projects, to support the font creation efforts of academic and linguistic communities, and to provide a free and open framework in which fonts may be shared and improved in partnership with others.
The OFL allows the licensed fonts to be used, studied, modified and redistributed freely as long as they are not sold by themselves. The fonts, including any derivative works, can be bundled, embedded, redistributed and/or sold with any software provided that any reserved names are not used by derivative works. The fonts and derivatives, however, cannot be released under any other type of license. The requirement for fonts to remain under this license does not apply to any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright Holder(s) under this license and clearly marked as such. This may include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the copyright statement(s).
"Original Version" refers to the collection of Font Software components as distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to, deleting, or substituting -- in part or in whole -- any of the components of the Original Version, by changing formats or by porting the Font Software to a new environment.
"Author" refers to any designer, engineer, programmer, technical writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining a copy of the Font Software, to use, study, copy, merge, embed, modify, redistribute, and sell modified and unmodified copies of the Font Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components, in Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled, redistributed and/or sold with any software, provided that each copy contains the above copyright notice and this license. These can be included either as stand-alone text files, human-readable headers or in the appropriate machine-readable metadata fields within text or binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font Name(s) unless explicit written permission is granted by the corresponding Copyright Holder. This restriction only applies to the primary font name as presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font Software shall not be used to promote, endorse or advertise any Modified Version, except to acknowledge the contribution(s) of the Copyright Holder(s) and the Author(s) or with their explicit written permission.
5) The Font Software, modified or unmodified, in part or in whole, must be distributed entirely under this license, and must not be distributed under any other license. The requirement for fonts to remain under this license does not apply to any document created using the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE FONT SOFTWARE.

Binary file not shown.

View File

@ -10,6 +10,7 @@ Core is somewhat like a service locator, but with larger engine capabilities - i
#include "State.hpp"
#include "MenuState.hpp"
#include "TestState.hpp"
#include <SDL_ttf.h>
// includes for getting "user" directory
#if defined(__APPLE__) && !defined(__IPHONEOS__)
#include <sys/types.h>
@ -119,6 +120,11 @@ int Core::initSystem() {
#else
shader_version = "120";
#endif
// Okay, let's load up TTF
if (TTF_Init() == -1) {
LOG(LOG_ERROR) << FUNC_NAME << ": TTF_Init(): " << TTF_GetError();
return 1;
}
asset_manager = new AssetManager();
// TODO: load APP cache as first cache (using ApkAssetCache in the case of Android)
@ -168,18 +174,26 @@ int Core::initSystem() {
// Create our main RenderView (which simply wraps around v_fbo, v_width, and v_height)
window_rview = new RenderView(v_fbo, v_width, v_height);
// Create our resource management stuff
// Texture Management
Asset *asset = asset_manager->loadFile("textures/null.png");
null_texture = new Texture("textures/null.png", asset->getData(), asset->getDataLength());
null_texture->buildTexture();
texture_table = new HashTable<Texture*>(256);
texture_table->setnull(null_texture);
asset->unloadData();
// Mesh Management
asset = asset_manager->loadFile("models/null.obj");
null_mesh = new Mesh(asset->getData(), asset->getDataLength());
null_mesh->buildMesh();
mesh_table = new HashTable<Mesh*>(64);
mesh_table->setnull(null_mesh);
asset->unloadData();
// Font Management
asset = asset_manager->loadFile("fonts/default.ttf");
default_font = new Font("fonts/default.ttf", 16, asset->getData(), asset->getDataLength());
font_table = new HashTable<Font*>(8);
font_table->setnull(default_font);
//asset->unloadData();
// Create our basic RenderScene
scene = new RenderScene();
@ -209,6 +223,7 @@ int Core::closeSystem() {
// delete window RenderView
delete window_rview;
TTF_Quit();
SDL_GL_DeleteContext(v_context);
SDL_DestroyWindow(v_window);
SDL_Quit();
@ -353,3 +368,20 @@ Mesh* Core::getMesh(const char *mesh_name) {
}
return mesh;
}
Font* Core::getFont(const char *font_name) {
Font *font = font_table->get(font_name);
// FIXME: we'll always attempt to load a new font if default is chosen..
if (font == default_font) {
// Either the texture isn't loaded or it doesn't exist - we assume the former first
Asset *asset_font = asset_manager->loadFile(font_name);
if (!(asset_font->flags & Asset::IS_NULL)) {
// Asset was not null, that means we probably have a good texture!
font = new Font(font_name, 16, asset_font->getData(), asset_font->getDataLength());
// NOTE: we _DO NOT_ unload font data, as that destroys the font's data as well
// FIXME: this obviously leads to a memory leak :S
//asset_font->unloadData();
font_table->set(font_name, font);
}
}
return font;
}

View File

@ -12,6 +12,7 @@ Core is somewhat like a service locator, but with larger engine capabilities - i
#include "State.hpp"
#include "HashTable.hpp"
#include "Texture.hpp"
#include "Font.hpp"
#include <vector>
class Core {
public:
@ -38,7 +39,10 @@ class Core {
Mesh *getMesh(const char *mesh_name);
HashTable<Mesh*> *mesh_table;
Mesh *null_mesh;
//Mesh *null_mesh;
//
Font* getFont(const char *ttf_name);
HashTable<Font*> *font_table;
Font *default_font;
//
int pushState(State *state);
int popState();

17
src/Font.cpp 100644
View File

@ -0,0 +1,17 @@
#include "Font.hpp"
#include "Log.hpp"
#include <SDL_ttf.h>
/* ======== Constructors/Destructor ======== */
Font::Font(std::string name_, int ptsize, const char *buffer, size_t buffer_size) {
// TODO: copy buffer to new buffer and have SDL_RWFromMem reference that. This will allow us to free the Asset without worry
name.assign(name_);
point_size = ptsize;
if ((font = TTF_OpenFontRW(SDL_RWFromMem((void*)buffer, buffer_size), 1, point_size)) == NULL) {
LOG(LOG_ERROR) << FUNC_NAME << ": Could not load TTF from buffer: " << TTF_GetError();
return;
}
}
Font::~Font() {
TTF_CloseFont(font);
}

19
src/Font.hpp 100644
View File

@ -0,0 +1,19 @@
/* ================================================================
Font
----------------
Font.cpp/Font.hpp provide the Font class. This class is similar to Mesh/Texture
================================================================ */
#ifndef FONT_HPP
#define FONT_HPP
#include <SDL_ttf.h>
#include <string>
class Font {
public:
Font(std::string name_, int ptsize, const char *buffer, size_t buffer_size);
~Font();
TTF_Font *font;
private:
std::string name;
int point_size;
};
#endif

View File

@ -19,7 +19,16 @@ Texture::Texture(std::string name_, RenderView *view) {
w = view->getWidth();
h = view->getHeight();
}
Texture::Texture(std::string name_, SDL_Surface *surface) {
name.assign(name_);
texture = 0;
flags = 0;
w = h = 0;
image = NULL;
loadSurface(surface);
}
Texture::~Texture() {
unloadImg();
destroyTexture();
}
/* ======== Image/Surface loading/unloading ======== */
@ -53,6 +62,19 @@ int Texture::unloadImg() {
}
return 1;
}
int Texture::loadSurface(SDL_Surface *surface) {
if (surface == NULL) return 1;
if (flags & LOADED) {
LOG(LOG_WARNING) << FUNC_NAME << ": surface already loaded - unloading and replacing";
unloadImg();
}
// NOTE: we assume the surface as our's - is this bad?
image = surface;
flags |= LOADED;
w = surface->w;
h = surface->h;
return 0;
}
/* ======== Texture generation/destruction ======== */
/* buildTexture
This function generates the OpenGL texture is an image is loaded. If the texture is already built, it is destroyed and recreated.
@ -77,10 +99,19 @@ GLuint Texture::buildTexture() {
LOG(LOG_WARNING) << FUNC_NAME << ": failed to generate texture!";
return 0;
}
int mode;
if (image->format->BytesPerPixel == 3) { // RGB 24bit
mode = GL_RGB;
} else if (image->format->BytesPerPixel == 4) { // RGBA 32bit
mode = GL_RGBA;
} else {
// TODO: actually handle others - upconvert?
mode = GL_RGB;
}
#ifdef __APPLE__
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, image->pixels);
glTexImage2D(GL_TEXTURE_2D, 0, mode, image->w, image->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, image->pixels);
#else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
glTexImage2D(GL_TEXTURE_2D, 0, mode, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
#endif
/*glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);

View File

@ -12,11 +12,13 @@ class Texture {
public:
Texture(std::string name_, const char *buffer, size_t buffer_size);
Texture(std::string name_, RenderView *view);
Texture(std::string name_, SDL_Surface *surface);
~Texture();
enum Flags {
LOADED = (1 << 1),
BUILT = (1 << 2)
};
int loadSurface(SDL_Surface *surface);
int loadImg(const char *buffer, size_t buffer_size);
int unloadImg();
GLuint buildTexture();

View File

@ -13,7 +13,8 @@ size_t fileToMem(const char *filename, char **buffer) {
char chunk[1024];
#if _WIN32
// _fsopen is used as we pass a FILE ptr back - fopen_s does not allow this
FILE *file = _fsopen(filename, "rb", _SH_DENYNO);
FILE* file;
fopen_s(&file, filename, "rb");
#else
FILE *file = fopen(filename, "rb");
#endif
@ -22,7 +23,7 @@ size_t fileToMem(const char *filename, char **buffer) {
return 0;
}
fseek(file, 0, SEEK_END);
size_t size = ftell(file)+1;
size_t size = ftell(file);
fseek(file, 0, SEEK_SET);
*buffer = (char*)malloc(size*sizeof(char));
@ -32,12 +33,13 @@ size_t fileToMem(const char *filename, char **buffer) {
}
int n = 0;
size_t offset = 0;
while ((n = fread(chunk, 1, 1024, file))) {
/*while ((n = fread(chunk, 1, 1024, file))) {
memcpy((*buffer)+offset, chunk, n);
offset += n;
}
}*/
fread(*buffer, size, 1, file);
fclose(file);
return offset;
return size;
}
/* ======== Asset data access ======== */
// Assets are files that may exist in an external storage directory (iOS/Android), a user data directory(all), or in the application itself (iOS/Android/OSX).

View File

@ -181,9 +181,13 @@ int TestState::onInit() {
scene->addSet(sert);
// GUI testing
GuiElement *element = new GuiButton("test", core.getTexture("ui/icon_perscube.png"));
//GuiElement *element = new GuiButton("test", core.getTexture("ui/icon_perscube.png"));
SDL_Color color = {0, 0, 0};
Texture *text_texture = new Texture("text", TTF_RenderUTF8_Blended(core.default_font->font, "Test", color));
text_texture->buildTexture();
GuiElement *element = new GuiButton("test", text_texture);
element->setPFlags(GuiElement::TOP|GuiElement::RIGHT);
element->setGeometry(0.0f, 0.0f, 128.0f, 128.0f);
element->setGeometry(0.0f, 0.0f, text_texture->w, text_texture->h);
GuiList *element_list = new GuiList("list1");
element_list->addChild(element);