2013-05-15 09:07:04 -04:00
|
|
|
/*
|
2013-05-16 23:37:54 -04:00
|
|
|
* Copyright 2013 Jeremie Roy. All rights reserved.
|
|
|
|
* License: http://www.opensource.org/licenses/BSD-2-Clause
|
|
|
|
*/
|
2013-05-15 09:07:04 -04:00
|
|
|
|
2013-05-24 01:07:54 -04:00
|
|
|
#include "common.h"
|
2015-01-23 00:01:09 -05:00
|
|
|
#include "bgfx_utils.h"
|
2013-08-08 00:45:56 -04:00
|
|
|
|
2013-04-22 16:42:11 -04:00
|
|
|
#include <bgfx.h>
|
|
|
|
#include <bx/timer.h>
|
2014-05-26 22:31:37 -04:00
|
|
|
#include <bx/fpumath.h>
|
2013-04-22 16:42:11 -04:00
|
|
|
|
2013-05-24 01:07:54 -04:00
|
|
|
#include "font/font_manager.h"
|
|
|
|
#include "font/text_metrics.h"
|
|
|
|
#include "font/text_buffer_manager.h"
|
|
|
|
#include "imgui/imgui.h"
|
2013-05-22 11:20:23 -04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2013-04-22 16:42:11 -04:00
|
|
|
|
2013-05-22 11:20:23 -04:00
|
|
|
long int fsize(FILE* _file)
|
|
|
|
{
|
|
|
|
long int pos = ftell(_file);
|
|
|
|
fseek(_file, 0L, SEEK_END);
|
|
|
|
long int size = ftell(_file);
|
|
|
|
fseek(_file, pos, SEEK_SET);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2013-08-09 01:18:19 -04:00
|
|
|
static char* loadText(const char* _filePath)
|
|
|
|
{
|
|
|
|
FILE* file = fopen(_filePath, "rb");
|
|
|
|
if (NULL != file)
|
2013-05-22 11:20:23 -04:00
|
|
|
{
|
2013-08-09 01:18:19 -04:00
|
|
|
uint32_t size = (uint32_t)fsize(file);
|
|
|
|
char* mem = (char*)malloc(size+1);
|
|
|
|
size_t ignore = fread(mem, 1, size, file);
|
|
|
|
BX_UNUSED(ignore);
|
|
|
|
fclose(file);
|
|
|
|
mem[size-1] = '\0';
|
|
|
|
return mem;
|
2013-05-22 11:20:23 -04:00
|
|
|
}
|
2013-08-09 01:18:19 -04:00
|
|
|
|
2013-05-22 11:20:23 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-08-09 01:18:19 -04:00
|
|
|
TrueTypeHandle loadTtf(FontManager* _fm, const char* _filePath)
|
2013-05-30 00:53:19 -04:00
|
|
|
{
|
2013-08-09 01:18:19 -04:00
|
|
|
FILE* file = fopen(_filePath, "rb");
|
|
|
|
if (NULL != file)
|
2013-05-30 00:53:19 -04:00
|
|
|
{
|
2013-08-09 01:18:19 -04:00
|
|
|
uint32_t size = (uint32_t)fsize(file);
|
|
|
|
uint8_t* mem = (uint8_t*)malloc(size+1);
|
|
|
|
size_t ignore = fread(mem, 1, size, file);
|
|
|
|
BX_UNUSED(ignore);
|
|
|
|
fclose(file);
|
|
|
|
mem[size-1] = '\0';
|
|
|
|
TrueTypeHandle handle = _fm->createTtf(mem, size);
|
|
|
|
free(mem);
|
|
|
|
return handle;
|
2013-05-30 00:53:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TrueTypeHandle invalid = BGFX_INVALID_HANDLE;
|
|
|
|
return invalid;
|
|
|
|
}
|
|
|
|
|
2013-04-22 16:42:11 -04:00
|
|
|
int _main_(int /*_argc*/, char** /*_argv*/)
|
|
|
|
{
|
2013-05-15 09:21:23 -04:00
|
|
|
uint32_t width = 1280;
|
2013-04-22 16:42:11 -04:00
|
|
|
uint32_t height = 720;
|
|
|
|
uint32_t debug = BGFX_DEBUG_TEXT;
|
2013-05-17 11:45:12 -04:00
|
|
|
uint32_t reset = BGFX_RESET_VSYNC;
|
2013-04-22 16:42:11 -04:00
|
|
|
|
|
|
|
bgfx::init();
|
|
|
|
|
2013-05-17 11:45:12 -04:00
|
|
|
bgfx::reset(width, height, reset);
|
2013-04-22 16:42:11 -04:00
|
|
|
|
|
|
|
// Enable debug text.
|
|
|
|
bgfx::setDebug(debug);
|
|
|
|
|
|
|
|
// Set view 0 clear state.
|
|
|
|
bgfx::setViewClear(0
|
2015-01-11 00:39:45 -05:00
|
|
|
, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
|
2013-05-16 23:35:08 -04:00
|
|
|
, 0x303030ff
|
|
|
|
, 1.0f
|
|
|
|
, 0
|
|
|
|
);
|
|
|
|
|
2015-01-23 00:01:09 -05:00
|
|
|
// Imgui.
|
2015-01-24 02:02:56 -05:00
|
|
|
imguiCreate();
|
2013-05-08 17:55:54 -04:00
|
|
|
|
2013-05-22 11:20:23 -04:00
|
|
|
char* bigText = loadText( "text/sherlock_holmes_a_scandal_in_bohemia_arthur_conan_doyle.txt");
|
2013-05-15 09:21:23 -04:00
|
|
|
|
2013-05-22 11:20:23 -04:00
|
|
|
// Init the text rendering system.
|
|
|
|
FontManager* fontManager = new FontManager(512);
|
|
|
|
TextBufferManager* textBufferManager = new TextBufferManager(fontManager);
|
2013-05-15 09:21:23 -04:00
|
|
|
|
2013-06-04 02:16:02 -04:00
|
|
|
TrueTypeHandle font = loadTtf(fontManager, "font/special_elite.ttf");
|
2013-05-08 17:55:54 -04:00
|
|
|
|
2013-05-22 11:20:23 -04:00
|
|
|
// Create a distance field font.
|
2013-06-04 02:16:02 -04:00
|
|
|
FontHandle fontSdf = fontManager->createFontByPixelSize(font, 0, 48, FONT_TYPE_DISTANCE);
|
2013-05-24 01:07:54 -04:00
|
|
|
|
2013-05-22 11:20:23 -04:00
|
|
|
// Create a scaled down version of the same font (without adding anything to the atlas).
|
2013-06-04 02:16:02 -04:00
|
|
|
FontHandle fontScaled = fontManager->createScaledFontToPixelSize(fontSdf, 14);
|
2013-05-24 01:07:54 -04:00
|
|
|
|
2013-06-04 02:16:02 -04:00
|
|
|
TextLineMetrics metrics(fontManager->getFontInfo(fontScaled) );
|
2013-05-22 11:20:23 -04:00
|
|
|
uint32_t lineCount = metrics.getLineCount(bigText);
|
2013-05-24 01:07:54 -04:00
|
|
|
|
2013-05-22 11:20:23 -04:00
|
|
|
float visibleLineCount = 20.0f;
|
|
|
|
|
|
|
|
const char* textBegin = 0;
|
|
|
|
const char* textEnd = 0;
|
|
|
|
metrics.getSubText(bigText, 0, (uint32_t)visibleLineCount, textBegin, textEnd);
|
|
|
|
|
2013-06-04 02:16:02 -04:00
|
|
|
TextBufferHandle scrollableBuffer = textBufferManager->createTextBuffer(FONT_TYPE_DISTANCE, BufferType::Transient);
|
2013-05-22 11:20:23 -04:00
|
|
|
textBufferManager->setTextColor(scrollableBuffer, 0xFFFFFFFF);
|
|
|
|
|
2013-06-04 02:16:02 -04:00
|
|
|
textBufferManager->appendText(scrollableBuffer, fontScaled, textBegin, textEnd);
|
2013-05-24 01:07:54 -04:00
|
|
|
|
2013-08-08 00:45:56 -04:00
|
|
|
entry::MouseState mouseState;
|
2013-05-22 11:20:23 -04:00
|
|
|
int32_t scrollArea = 0;
|
2013-06-04 02:16:02 -04:00
|
|
|
const int32_t guiPanelWidth = 250;
|
|
|
|
const int32_t guiPanelHeight = 200;
|
|
|
|
float textScroll = 0.0f;
|
|
|
|
float textRotation = 0.0f;
|
|
|
|
float textScale = 1.0f;
|
|
|
|
float textSize = 14.0f;
|
|
|
|
|
2013-08-08 00:45:56 -04:00
|
|
|
while (!entry::processEvents(width, height, debug, reset, &mouseState) )
|
2013-04-22 16:42:11 -04:00
|
|
|
{
|
2013-05-22 11:20:23 -04:00
|
|
|
imguiBeginFrame(mouseState.m_mx
|
|
|
|
, mouseState.m_my
|
|
|
|
, (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
|
|
|
|
| (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
|
2015-06-10 10:53:50 -04:00
|
|
|
, mouseState.m_mz
|
2013-05-22 11:20:23 -04:00
|
|
|
, width
|
|
|
|
, height
|
|
|
|
);
|
|
|
|
|
2014-11-03 22:11:08 -05:00
|
|
|
imguiBeginScrollArea("Text Area"
|
|
|
|
, width - guiPanelWidth - 10
|
|
|
|
, 10
|
|
|
|
, guiPanelWidth
|
|
|
|
, guiPanelHeight
|
|
|
|
, &scrollArea
|
|
|
|
);
|
2013-05-22 11:20:23 -04:00
|
|
|
imguiSeparatorLine();
|
2013-05-24 01:07:54 -04:00
|
|
|
|
|
|
|
bool recomputeVisibleText = false;
|
2014-06-30 00:53:23 -04:00
|
|
|
recomputeVisibleText |= imguiSlider("Number of lines", visibleLineCount, 1.0f, 177.0f , 1.0f);
|
|
|
|
if (imguiSlider("Font size", textSize, 6.0f, 64.0f , 1.0f) )
|
2013-05-22 11:20:23 -04:00
|
|
|
{
|
2013-06-04 02:16:02 -04:00
|
|
|
fontManager->destroyFont(fontScaled);
|
|
|
|
fontScaled = fontManager->createScaledFontToPixelSize(fontSdf, (uint32_t) textSize);
|
|
|
|
metrics = TextLineMetrics(fontManager->getFontInfo(fontScaled) );
|
2013-05-22 11:20:23 -04:00
|
|
|
recomputeVisibleText = true;
|
|
|
|
}
|
|
|
|
|
2014-06-30 00:53:23 -04:00
|
|
|
recomputeVisibleText |= imguiSlider("Scroll", textScroll, 0.0f, (lineCount-visibleLineCount) , 1.0f);
|
2014-10-25 21:07:51 -04:00
|
|
|
imguiSlider("Rotate", textRotation, 0.0f, bx::pi*2.0f , 0.1f);
|
2014-06-30 00:53:23 -04:00
|
|
|
recomputeVisibleText |= imguiSlider("Scale", textScale, 0.1f, 10.0f , 0.1f);
|
2013-05-22 11:20:23 -04:00
|
|
|
|
2013-06-04 02:16:02 -04:00
|
|
|
if (recomputeVisibleText)
|
2013-05-22 11:20:23 -04:00
|
|
|
{
|
|
|
|
textBufferManager->clearTextBuffer(scrollableBuffer);
|
2015-01-23 00:01:09 -05:00
|
|
|
metrics.getSubText(bigText,(uint32_t)textScroll, (uint32_t)(textScroll+visibleLineCount), textBegin, textEnd);
|
2013-06-04 02:16:02 -04:00
|
|
|
textBufferManager->appendText(scrollableBuffer, fontScaled, textBegin, textEnd);
|
2015-01-23 00:01:09 -05:00
|
|
|
}
|
2013-05-24 01:07:54 -04:00
|
|
|
|
2013-05-22 11:20:23 -04:00
|
|
|
imguiEndScrollArea();
|
2013-05-24 01:07:54 -04:00
|
|
|
|
2013-05-22 11:20:23 -04:00
|
|
|
imguiEndFrame();
|
|
|
|
|
2013-04-22 16:42:11 -04:00
|
|
|
// Set view 0 default viewport.
|
|
|
|
bgfx::setViewRect(0, 0, 0, width, height);
|
|
|
|
|
|
|
|
// This dummy draw call is here to make sure that view 0 is cleared
|
|
|
|
// if no other draw calls are submitted to view 0.
|
|
|
|
bgfx::submit(0);
|
|
|
|
|
2013-04-23 08:32:31 -04:00
|
|
|
int64_t now = bx::getHPCounter();
|
|
|
|
static int64_t last = now;
|
|
|
|
const int64_t frameTime = now - last;
|
|
|
|
last = now;
|
|
|
|
const double freq = double(bx::getHPFrequency() );
|
2013-05-15 09:21:23 -04:00
|
|
|
const double toMs = 1000.0 / freq;
|
2013-04-23 08:32:31 -04:00
|
|
|
|
2013-06-04 02:16:02 -04:00
|
|
|
// Use debug font to print32_t information about this example.
|
2013-04-22 16:42:11 -04:00
|
|
|
bgfx::dbgTextClear();
|
2013-04-23 11:23:00 -04:00
|
|
|
bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/11-fontsdf");
|
|
|
|
bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Use a single distance field font to render text of various size.");
|
2013-05-15 09:21:23 -04:00
|
|
|
bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime) * toMs);
|
2013-04-23 08:32:31 -04:00
|
|
|
|
2014-11-03 22:11:08 -05:00
|
|
|
float at[3] = { 0, 0, 0.0f };
|
2013-04-22 16:42:11 -04:00
|
|
|
float eye[3] = {0, 0, -1.0f };
|
2013-05-08 17:55:54 -04:00
|
|
|
|
2013-04-22 16:42:11 -04:00
|
|
|
float view[16];
|
2014-05-26 22:31:37 -04:00
|
|
|
bx::mtxLookAt(view, eye, at);
|
2013-05-16 23:37:54 -04:00
|
|
|
|
2014-11-03 22:11:08 -05:00
|
|
|
const float centering = 0.5f;
|
2013-05-24 01:07:54 -04:00
|
|
|
|
2014-11-03 22:11:08 -05:00
|
|
|
// Setup a top-left ortho matrix for screen space drawing.
|
|
|
|
const bgfx::HMD* hmd = bgfx::getHMD();
|
2015-07-16 23:28:43 -04:00
|
|
|
if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
|
2014-11-03 22:11:08 -05:00
|
|
|
{
|
|
|
|
float proj[16];
|
|
|
|
bx::mtxProj(proj, hmd->eye[0].fov, 0.1f, 100.0f);
|
|
|
|
|
|
|
|
static float time = 0.0f;
|
|
|
|
time += 0.05f;
|
|
|
|
|
|
|
|
const float dist = 10.0f;
|
2014-11-09 01:09:31 -05:00
|
|
|
const float offset0 = -proj[8] + (hmd->eye[0].viewOffset[0] / dist * proj[0]);
|
|
|
|
const float offset1 = -proj[8] + (hmd->eye[1].viewOffset[0] / dist * proj[0]);
|
2014-11-03 22:11:08 -05:00
|
|
|
|
|
|
|
float ortho[2][16];
|
|
|
|
const float viewOffset = width/4.0f;
|
|
|
|
const float viewWidth = width/2.0f;
|
|
|
|
bx::mtxOrtho(ortho[0], centering + viewOffset, centering + viewOffset + viewWidth, height + centering, centering, -1.0f, 1.0f, offset0);
|
|
|
|
bx::mtxOrtho(ortho[1], centering + viewOffset, centering + viewOffset + viewWidth, height + centering, centering, -1.0f, 1.0f, offset1);
|
|
|
|
bgfx::setViewTransform(0, view, ortho[0], BGFX_VIEW_STEREO, ortho[1]);
|
|
|
|
bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
float ortho[16];
|
|
|
|
bx::mtxOrtho(ortho, centering, width + centering, height + centering, centering, -1.0f, 1.0f);
|
|
|
|
bgfx::setViewTransform(0, view, ortho);
|
|
|
|
bgfx::setViewRect(0, 0, 0, width, height);
|
|
|
|
}
|
2013-05-24 01:07:54 -04:00
|
|
|
|
2013-05-22 11:20:23 -04:00
|
|
|
//very crude approximation :(
|
2013-06-04 02:16:02 -04:00
|
|
|
float textAreaWidth = 0.5f * 66.0f * fontManager->getFontInfo(fontScaled).maxAdvanceWidth;
|
2013-05-22 11:20:23 -04:00
|
|
|
|
|
|
|
float textRotMat[16];
|
|
|
|
float textCenterMat[16];
|
|
|
|
float textScaleMat[16];
|
|
|
|
float screenCenterMat[16];
|
2013-05-24 01:07:54 -04:00
|
|
|
|
2014-05-26 22:31:37 -04:00
|
|
|
bx::mtxRotateZ(textRotMat, textRotation);
|
|
|
|
bx::mtxTranslate(textCenterMat, -(textAreaWidth * 0.5f), (-visibleLineCount)*metrics.getLineHeight()*0.5f, 0);
|
|
|
|
bx::mtxScale(textScaleMat, textScale, textScale, 1.0f);
|
|
|
|
bx::mtxTranslate(screenCenterMat, ( (width) * 0.5f), ( (height) * 0.5f), 0);
|
2013-05-22 11:20:23 -04:00
|
|
|
|
|
|
|
//first translate to text center, then scale, then rotate
|
|
|
|
float tmpMat[16];
|
2014-05-26 22:31:37 -04:00
|
|
|
bx::mtxMul(tmpMat, textCenterMat, textRotMat);
|
2013-05-24 01:07:54 -04:00
|
|
|
|
2013-05-22 11:20:23 -04:00
|
|
|
float tmpMat2[16];
|
2014-05-26 22:31:37 -04:00
|
|
|
bx::mtxMul(tmpMat2, tmpMat, textScaleMat);
|
2013-05-24 01:07:54 -04:00
|
|
|
|
2013-05-22 11:20:23 -04:00
|
|
|
float tmpMat3[16];
|
2014-05-26 22:31:37 -04:00
|
|
|
bx::mtxMul(tmpMat3, tmpMat2, screenCenterMat);
|
2013-05-15 09:21:23 -04:00
|
|
|
|
2013-05-08 17:55:54 -04:00
|
|
|
// Set model matrix for rendering.
|
2013-05-22 11:20:23 -04:00
|
|
|
bgfx::setTransform(tmpMat3);
|
2013-05-24 01:07:54 -04:00
|
|
|
|
2013-05-16 23:35:08 -04:00
|
|
|
// Draw your text.
|
2013-05-22 11:20:23 -04:00
|
|
|
textBufferManager->submitTextBuffer(scrollableBuffer, 0);
|
2013-04-22 16:42:11 -04:00
|
|
|
|
2013-05-15 09:21:23 -04:00
|
|
|
// Advance to next frame. Rendering thread will be kicked to
|
2013-04-22 16:42:11 -04:00
|
|
|
// process submitted rendering primitives.
|
|
|
|
bgfx::frame();
|
|
|
|
}
|
|
|
|
|
2013-10-06 23:30:44 -04:00
|
|
|
imguiDestroy();
|
|
|
|
|
2013-08-09 01:18:19 -04:00
|
|
|
free(bigText);
|
|
|
|
|
2013-06-04 02:16:02 -04:00
|
|
|
fontManager->destroyTtf(font);
|
2013-05-16 23:35:08 -04:00
|
|
|
// Destroy the fonts.
|
2013-06-04 02:16:02 -04:00
|
|
|
fontManager->destroyFont(fontSdf);
|
|
|
|
fontManager->destroyFont(fontScaled);
|
2013-05-15 09:21:23 -04:00
|
|
|
|
2013-05-22 11:20:23 -04:00
|
|
|
textBufferManager->destroyTextBuffer(scrollableBuffer);
|
2013-05-07 09:51:19 -04:00
|
|
|
|
2013-04-22 16:42:11 -04:00
|
|
|
delete textBufferManager;
|
2013-05-15 09:21:23 -04:00
|
|
|
delete fontManager;
|
2013-05-16 23:35:08 -04:00
|
|
|
|
2013-04-22 16:42:11 -04:00
|
|
|
// Shutdown bgfx.
|
2013-05-15 09:21:23 -04:00
|
|
|
bgfx::shutdown();
|
2013-04-22 16:42:11 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|