This commit is contained in:
Branimir Karadžić 2016-12-08 20:45:01 -08:00
Родитель f9aab4c8bf
Коммит a5bb9be4ed
7 изменённых файлов: 153 добавлений и 251 удалений

Просмотреть файл

@ -8,6 +8,8 @@
#include <bx/allocator.h>
#include <bx/string.h>
#include <bx/crtimpl.h>
#include "aviwriter.h"
#include <inttypes.h>
@ -61,64 +63,58 @@ static const uint16_t s_cubeIndices[36] =
6, 3, 7,
};
void saveTga(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, bool _grayscale, bool _yflip)
void imageWriteTga(bx::WriterI* _writer, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, bool _grayscale, bool _yflip, bx::Error* _err)
{
FILE* file = fopen(_filePath, "wb");
if (NULL != file)
BX_ERROR_SCOPE(_err);
uint8_t type = _grayscale ? 3 : 2;
uint8_t bpp = _grayscale ? 8 : 32;
uint8_t header[18] = {};
header[ 2] = type;
header[12] = _width &0xff;
header[13] = (_width >>8)&0xff;
header[14] = _height &0xff;
header[15] = (_height>>8)&0xff;
header[16] = bpp;
header[17] = 32;
bx::write(_writer, header, sizeof(header), _err);
uint32_t dstPitch = _width*bpp/8;
if (_yflip)
{
uint8_t type = _grayscale ? 3 : 2;
uint8_t bpp = _grayscale ? 8 : 32;
putc(0, file);
putc(0, file);
putc(type, file);
putc(0, file);
putc(0, file);
putc(0, file);
putc(0, file);
putc(0, file);
putc(0, file);
putc(0, file);
putc(0, file);
putc(0, file);
putc(_width&0xff, file);
putc( (_width>>8)&0xff, file);
putc(_height&0xff, file);
putc( (_height>>8)&0xff, file);
putc(bpp, file);
putc(32, file);
uint32_t dstPitch = _width*bpp/8;
if (_yflip)
uint8_t* data = (uint8_t*)_src + _pitch*_height - _pitch;
for (uint32_t yy = 0; yy < _height; ++yy)
{
uint8_t* data = (uint8_t*)_src + _srcPitch*_height - _srcPitch;
for (uint32_t yy = 0; yy < _height; ++yy)
{
fwrite(data, dstPitch, 1, file);
data -= _srcPitch;
}
bx::write(_writer, data, dstPitch, _err);
data -= _pitch;
}
else
}
else if (_pitch == dstPitch)
{
bx::write(_writer, _src, _height*_pitch, _err);
}
else
{
uint8_t* data = (uint8_t*)_src;
for (uint32_t yy = 0; yy < _height; ++yy)
{
uint8_t* data = (uint8_t*)_src;
for (uint32_t yy = 0; yy < _height; ++yy)
{
fwrite(data, dstPitch, 1, file);
data += _srcPitch;
}
bx::write(_writer, data, dstPitch, _err);
data += _pitch;
}
fclose(file);
}
}
long int fsize(FILE* _file)
void saveTga(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, bool _grayscale, bool _yflip)
{
long int pos = ftell(_file);
fseek(_file, 0L, SEEK_END);
long int size = ftell(_file);
fseek(_file, pos, SEEK_SET);
return size;
bx::CrtFileWriter writer;
bx::Error err;
if (bx::open(&writer, _filePath, false, &err) )
{
imageWriteTga(&writer, _width, _height, _srcPitch, _src, _grayscale, _yflip, &err);
bx::close(&writer);
}
}
struct BgfxCallback : public bgfx::CallbackI
@ -148,11 +144,12 @@ struct BgfxCallback : public bgfx::CallbackI
bx::snprintf(filePath, sizeof(filePath), "temp/%016" PRIx64, _id);
// Use cache id as filename.
FILE* file = fopen(filePath, "rb");
if (NULL != file)
bx::FileReaderI* reader = entry::getFileReader();
bx::Error err;
if (bx::open(reader, filePath, &err) )
{
uint32_t size = fsize(file);
fclose(file);
uint32_t size = bx::getSize(reader);
bx::close(reader);
// Return size of shader file.
return size;
}
@ -167,12 +164,13 @@ struct BgfxCallback : public bgfx::CallbackI
bx::snprintf(filePath, sizeof(filePath), "temp/%016" PRIx64, _id);
// Use cache id as filename.
FILE* file = fopen(filePath, "rb");
if (NULL != file)
bx::FileReaderI* reader = entry::getFileReader();
bx::Error err;
if (bx::open(reader, filePath, &err) )
{
// Read shader.
size_t result = fread(_data, 1, _size, file);
fclose(file);
uint32_t result = bx::read(reader, _data, _size, &err);
bx::close(reader);
// Make sure that read size matches requested size.
return result == _size;
@ -188,12 +186,13 @@ struct BgfxCallback : public bgfx::CallbackI
bx::snprintf(filePath, sizeof(filePath), "temp/%016" PRIx64, _id);
// Use cache id as filename.
FILE* file = fopen(filePath, "wb");
if (NULL != file)
bx::FileWriterI* writer = entry::getFileWriter();
bx::Error err;
if (bx::open(writer, filePath, false, &err) )
{
// Write shader to cache location.
fwrite(_data, 1, _size, file);
fclose(file);
bx::write(writer, _data, _size, &err);
bx::close(writer);
}
}
@ -202,70 +201,16 @@ struct BgfxCallback : public bgfx::CallbackI
char temp[1024];
// Save screen shot as TGA.
bx::snprintf(temp, BX_COUNTOF(temp), "%s.mip0.tga", _filePath);
bx::snprintf(temp, BX_COUNTOF(temp), "%s.tga", _filePath);
saveTga(temp, _width, _height, _pitch, _data, false, _yflip);
uint32_t width = _width;
uint32_t height = _height;
uint32_t pitch = _pitch;
uint8_t* data = (uint8_t*)_data;
// Generate mip maps.
uint32_t mip = 1;
for (; 2 <= width && 2 <= height; ++mip)
{
bx::snprintf(temp, BX_COUNTOF(temp), "%s.mip%d.tga", _filePath, mip);
bgfx::imageRgba8Downsample2x2(width, height, pitch, data, data);
width >>= 1;
height >>= 1;
pitch = width*4;
saveTga(temp, width, height, pitch, _data, false, _yflip);
}
if (width > height)
{
for (; 2 <= width; ++mip)
{
memcpy(&data[width*4], data, width*4);
bx::snprintf(temp, BX_COUNTOF(temp), "%s.mip%d.tga", _filePath, mip);
bgfx::imageRgba8Downsample2x2(width, 2, pitch, data, data);
width >>= 1;
pitch = width*4;
saveTga(temp, width, 2, pitch, _data, false, _yflip);
}
}
else
{
for (; 2 <= height; ++mip)
{
uint32_t* src = (uint32_t*)data;
for (uint32_t ii = 0; ii < height; ++ii, src += 2)
{
src[1] = src[0];
}
bx::snprintf(temp, BX_COUNTOF(temp), "%s.mip%d.tga", _filePath, mip);
bgfx::imageRgba8Downsample2x2(2, height, 8, data, data);
height >>= 1;
saveTga(temp, 2, height, 8, _data, false, _yflip);
}
}
}
virtual void captureBegin(uint32_t _width, uint32_t _height, uint32_t /*_pitch*/, bgfx::TextureFormat::Enum /*_format*/, bool _yflip) BX_OVERRIDE
{
m_writer = new AviWriter(entry::getFileWriter() );
m_writer = BX_NEW(entry::getAllocator(), AviWriter)(entry::getFileWriter() );
if (!m_writer->open("temp/capture.avi", _width, _height, 60, _yflip) )
{
delete m_writer;
BX_DELETE(entry::getAllocator(), m_writer);
m_writer = NULL;
}
}
@ -275,7 +220,7 @@ struct BgfxCallback : public bgfx::CallbackI
if (NULL != m_writer)
{
m_writer->close();
delete m_writer;
BX_DELETE(entry::getAllocator(), m_writer);
m_writer = NULL;
}
}

Просмотреть файл

@ -20,28 +20,15 @@
#include <stdio.h>
#include <wchar.h>
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;
}
TrueTypeHandle loadTtf(FontManager* _fm, const char* _filePath)
{
FILE* file = fopen(_filePath, "rb");
if (NULL != file)
uint32_t size;
void* data = load(_filePath, &size);
if (NULL != data)
{
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);
TrueTypeHandle handle = _fm->createTtf( (uint8_t*)data, size);
BX_FREE(entry::getAllocator(), data);
return handle;
}

Просмотреть файл

@ -18,45 +18,15 @@
#include <stdio.h>
#include <string.h>
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;
}
static char* loadText(const char* _filePath)
{
FILE* file = fopen(_filePath, "rb");
if (NULL != file)
{
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;
}
return NULL;
}
TrueTypeHandle loadTtf(FontManager* _fm, const char* _filePath)
{
FILE* file = fopen(_filePath, "rb");
if (NULL != file)
uint32_t size;
void* data = load(_filePath, &size);
if (NULL != data)
{
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);
TrueTypeHandle handle = _fm->createTtf( (uint8_t*)data, size);
BX_FREE(entry::getAllocator(), data);
return handle;
}
@ -90,7 +60,7 @@ int _main_(int _argc, char** _argv)
// Imgui.
imguiCreate();
char* bigText = loadText( "text/sherlock_holmes_a_scandal_in_bohemia_arthur_conan_doyle.txt");
char* bigText = (char*)load("text/sherlock_holmes_a_scandal_in_bohemia_arthur_conan_doyle.txt");
// Init the text rendering system.
FontManager* fontManager = new FontManager(512);
@ -267,7 +237,7 @@ int _main_(int _argc, char** _argv)
imguiDestroy();
free(bigText);
BX_FREE(entry::getAllocator(), bigText);
fontManager->destroyTtf(font);
// Destroy the fonts.

Просмотреть файл

@ -674,29 +674,29 @@ struct Mesh
#define BGFX_CHUNK_MAGIC_IB BX_MAKEFOURCC('I', 'B', ' ', 0x0)
#define BGFX_CHUNK_MAGIC_PRI BX_MAKEFOURCC('P', 'R', 'I', 0x0)
bx::CrtFileReader reader;
bx::open(&reader, _filePath);
bx::FileReaderI* reader = entry::getFileReader();
bx::open(reader, _filePath);
Group group;
uint32_t chunk;
while (4 == bx::read(&reader, chunk) )
while (4 == bx::read(reader, chunk) )
{
switch (chunk)
{
case BGFX_CHUNK_MAGIC_VB:
{
bx::read(&reader, group.m_sphere);
bx::read(&reader, group.m_aabb);
bx::read(&reader, group.m_obb);
bx::read(reader, group.m_sphere);
bx::read(reader, group.m_aabb);
bx::read(reader, group.m_obb);
bgfx::read(&reader, m_decl);
bgfx::read(reader, m_decl);
uint16_t stride = m_decl.getStride();
uint16_t numVertices;
bx::read(&reader, numVertices);
bx::read(reader, numVertices);
const bgfx::Memory* mem = bgfx::alloc(numVertices*stride);
bx::read(&reader, mem->data, mem->size);
bx::read(reader, mem->data, mem->size);
group.m_vbh = bgfx::createVertexBuffer(mem, m_decl);
}
@ -705,9 +705,9 @@ struct Mesh
case BGFX_CHUNK_MAGIC_IB:
{
uint32_t numIndices;
bx::read(&reader, numIndices);
bx::read(reader, numIndices);
const bgfx::Memory* mem = bgfx::alloc(numIndices*2);
bx::read(&reader, mem->data, mem->size);
bx::read(reader, mem->data, mem->size);
group.m_ibh = bgfx::createIndexBuffer(mem);
}
break;
@ -715,31 +715,31 @@ struct Mesh
case BGFX_CHUNK_MAGIC_PRI:
{
uint16_t len;
bx::read(&reader, len);
bx::read(reader, len);
std::string material;
material.resize(len);
bx::read(&reader, const_cast<char*>(material.c_str() ), len);
bx::read(reader, const_cast<char*>(material.c_str() ), len);
uint16_t num;
bx::read(&reader, num);
bx::read(reader, num);
for (uint32_t ii = 0; ii < num; ++ii)
{
bx::read(&reader, len);
bx::read(reader, len);
std::string name;
name.resize(len);
bx::read(&reader, const_cast<char*>(name.c_str() ), len);
bx::read(reader, const_cast<char*>(name.c_str() ), len);
Primitive prim;
bx::read(&reader, prim.m_startIndex);
bx::read(&reader, prim.m_numIndices);
bx::read(&reader, prim.m_startVertex);
bx::read(&reader, prim.m_numVertices);
bx::read(&reader, prim.m_sphere);
bx::read(&reader, prim.m_aabb);
bx::read(&reader, prim.m_obb);
bx::read(reader, prim.m_startIndex);
bx::read(reader, prim.m_numIndices);
bx::read(reader, prim.m_startVertex);
bx::read(reader, prim.m_numVertices);
bx::read(reader, prim.m_sphere);
bx::read(reader, prim.m_aabb);
bx::read(reader, prim.m_obb);
group.m_prims.push_back(prim);
}
@ -750,13 +750,13 @@ struct Mesh
break;
default:
DBG("%08x at %d", chunk, reader.seek() );
DBG("%08x at %d", chunk, bx::seek(reader) );
abort();
break;
}
}
bx::close(&reader);
bx::close(reader);
}
void unload()

Просмотреть файл

@ -1026,29 +1026,29 @@ struct Mesh
#define BGFX_CHUNK_MAGIC_IB BX_MAKEFOURCC('I', 'B', ' ', 0x0)
#define BGFX_CHUNK_MAGIC_PRI BX_MAKEFOURCC('P', 'R', 'I', 0x0)
bx::CrtFileReader reader;
bx::open(&reader, _filePath);
bx::FileReaderI* reader = entry::getFileReader();
bx::open(reader, _filePath);
Group group;
uint32_t chunk;
while (4 == bx::read(&reader, chunk) )
while (4 == bx::read(reader, chunk) )
{
switch (chunk)
{
case BGFX_CHUNK_MAGIC_VB:
{
bx::read(&reader, group.m_sphere);
bx::read(&reader, group.m_aabb);
bx::read(&reader, group.m_obb);
bx::read(reader, group.m_sphere);
bx::read(reader, group.m_aabb);
bx::read(reader, group.m_obb);
bgfx::read(&reader, m_decl);
bgfx::read(reader, m_decl);
uint16_t stride = m_decl.getStride();
bx::read(&reader, group.m_numVertices);
bx::read(reader, group.m_numVertices);
const uint32_t size = group.m_numVertices*stride;
group.m_vertices = (uint8_t*)malloc(size);
bx::read(&reader, group.m_vertices, size);
bx::read(reader, group.m_vertices, size);
const bgfx::Memory* mem = bgfx::makeRef(group.m_vertices, size);
group.m_vbh = bgfx::createVertexBuffer(mem, m_decl);
@ -1057,10 +1057,10 @@ struct Mesh
case BGFX_CHUNK_MAGIC_IB:
{
bx::read(&reader, group.m_numIndices);
bx::read(reader, group.m_numIndices);
const uint32_t size = group.m_numIndices*2;
group.m_indices = (uint16_t*)malloc(size);
bx::read(&reader, group.m_indices, size);
bx::read(reader, group.m_indices, size);
const bgfx::Memory* mem = bgfx::makeRef(group.m_indices, size);
group.m_ibh = bgfx::createIndexBuffer(mem);
@ -1070,31 +1070,31 @@ struct Mesh
case BGFX_CHUNK_MAGIC_PRI:
{
uint16_t len;
bx::read(&reader, len);
bx::read(reader, len);
std::string material;
material.resize(len);
bx::read(&reader, const_cast<char*>(material.c_str() ), len);
bx::read(reader, const_cast<char*>(material.c_str() ), len);
uint16_t num;
bx::read(&reader, num);
bx::read(reader, num);
for (uint32_t ii = 0; ii < num; ++ii)
{
bx::read(&reader, len);
bx::read(reader, len);
std::string name;
name.resize(len);
bx::read(&reader, const_cast<char*>(name.c_str() ), len);
bx::read(reader, const_cast<char*>(name.c_str() ), len);
Primitive prim;
bx::read(&reader, prim.m_startIndex);
bx::read(&reader, prim.m_numIndices);
bx::read(&reader, prim.m_startVertex);
bx::read(&reader, prim.m_numVertices);
bx::read(&reader, prim.m_sphere);
bx::read(&reader, prim.m_aabb);
bx::read(&reader, prim.m_obb);
bx::read(reader, prim.m_startIndex);
bx::read(reader, prim.m_numIndices);
bx::read(reader, prim.m_startVertex);
bx::read(reader, prim.m_numVertices);
bx::read(reader, prim.m_sphere);
bx::read(reader, prim.m_aabb);
bx::read(reader, prim.m_obb);
group.m_prims.push_back(prim);
}
@ -1105,13 +1105,13 @@ struct Mesh
break;
default:
DBG("%08x at %d", chunk, reader.seek() );
DBG("%08x at %d", chunk, bx::seek(reader) );
abort();
break;
}
}
bx::close(&reader);
bx::close(reader);
for (GroupArray::iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
{

Просмотреть файл

@ -884,29 +884,29 @@ struct Mesh
#define BGFX_CHUNK_MAGIC_IB BX_MAKEFOURCC('I', 'B', ' ', 0x0)
#define BGFX_CHUNK_MAGIC_PRI BX_MAKEFOURCC('P', 'R', 'I', 0x0)
bx::CrtFileReader reader;
bx::open(&reader, _filePath);
bx::FileReaderI* reader = entry::getFileReader();
bx::open(reader, _filePath);
Group group;
uint32_t chunk;
while (4 == bx::read(&reader, chunk) )
while (4 == bx::read(reader, chunk) )
{
switch (chunk)
{
case BGFX_CHUNK_MAGIC_VB:
{
bx::read(&reader, group.m_sphere);
bx::read(&reader, group.m_aabb);
bx::read(&reader, group.m_obb);
bx::read(reader, group.m_sphere);
bx::read(reader, group.m_aabb);
bx::read(reader, group.m_obb);
bgfx::read(&reader, m_decl);
bgfx::read(reader, m_decl);
uint16_t stride = m_decl.getStride();
uint16_t numVertices;
bx::read(&reader, numVertices);
bx::read(reader, numVertices);
const bgfx::Memory* mem = bgfx::alloc(numVertices*stride);
bx::read(&reader, mem->data, mem->size);
bx::read(reader, mem->data, mem->size);
group.m_vbh = bgfx::createVertexBuffer(mem, m_decl);
}
@ -915,9 +915,9 @@ struct Mesh
case BGFX_CHUNK_MAGIC_IB:
{
uint32_t numIndices;
bx::read(&reader, numIndices);
bx::read(reader, numIndices);
const bgfx::Memory* mem = bgfx::alloc(numIndices*2);
bx::read(&reader, mem->data, mem->size);
bx::read(reader, mem->data, mem->size);
group.m_ibh = bgfx::createIndexBuffer(mem);
}
break;
@ -925,31 +925,31 @@ struct Mesh
case BGFX_CHUNK_MAGIC_PRI:
{
uint16_t len;
bx::read(&reader, len);
bx::read(reader, len);
std::string material;
material.resize(len);
bx::read(&reader, const_cast<char*>(material.c_str() ), len);
bx::read(reader, const_cast<char*>(material.c_str() ), len);
uint16_t num;
bx::read(&reader, num);
bx::read(reader, num);
for (uint32_t ii = 0; ii < num; ++ii)
{
bx::read(&reader, len);
bx::read(reader, len);
std::string name;
name.resize(len);
bx::read(&reader, const_cast<char*>(name.c_str() ), len);
bx::read(reader, const_cast<char*>(name.c_str() ), len);
Primitive prim;
bx::read(&reader, prim.m_startIndex);
bx::read(&reader, prim.m_numIndices);
bx::read(&reader, prim.m_startVertex);
bx::read(&reader, prim.m_numVertices);
bx::read(&reader, prim.m_sphere);
bx::read(&reader, prim.m_aabb);
bx::read(&reader, prim.m_obb);
bx::read(reader, prim.m_startIndex);
bx::read(reader, prim.m_numIndices);
bx::read(reader, prim.m_startVertex);
bx::read(reader, prim.m_numVertices);
bx::read(reader, prim.m_sphere);
bx::read(reader, prim.m_aabb);
bx::read(reader, prim.m_obb);
group.m_prims.push_back(prim);
}
@ -960,12 +960,12 @@ struct Mesh
break;
default:
DBG("%08x at %d", chunk, reader.seek() );
DBG("%08x at %d", chunk, bx::seek(reader) );
break;
}
}
bx::close(&reader);
bx::close(reader);
}
void unload()

Просмотреть файл

@ -486,7 +486,7 @@ namespace entry
WindowHandle defaultWindow = { 0 };
setWindowSize(defaultWindow, m_width, m_height, true);
bx::CrtFileReader reader;
bx::FileReaderI* reader = getEileReader();
if (bx::open(&reader, "gamecontrollerdb.txt") )
{
bx::AllocatorI* allocator = getAllocator();