rework on dat and spr loader

This commit is contained in:
Eduardo Bart
2011-08-15 16:15:49 -03:00
parent b21ccd16f9
commit 2e1a96c2df
43 changed files with 191 additions and 581 deletions

View File

@@ -89,10 +89,9 @@ void ResourceManager::loadFile(const std::string& fileName, std::iostream& out)
} else {
int fileSize = PHYSFS_fileLength(file);
if(fileSize > 0) {
char* buffer = new char[fileSize];
PHYSFS_read(file, (void*)buffer, 1, fileSize);
out.write(buffer, fileSize);
delete[] buffer;
std::vector<char> buffer(fileSize);
PHYSFS_read(file, (void*)&buffer[0], 1, fileSize);
out.write(&buffer[0], fileSize);
} else
out.clear(std::ios::eofbit);
PHYSFS_close(file);
@@ -124,10 +123,9 @@ bool ResourceManager::saveFile(const std::string& fileName, std::istream& in)
in.seekg(0, std::ios::end);
std::streampos size = in.tellg();
in.seekg(0, std::ios::beg);
char* buffer = new char[size];
in.read(buffer, size);
bool ret = saveFile(fileName, (const uchar*)buffer, size);
delete[] buffer;
std::vector<char> buffer(size);
in.read(&buffer[0], size);
bool ret = saveFile(fileName, (const uchar*)&buffer[0], size);
in.seekg(oldPos, std::ios::beg);
return ret;
}
@@ -145,10 +143,10 @@ bool ResourceManager::deleteFile(const std::string& fileName)
std::list<std::string> ResourceManager::listDirectoryFiles(const std::string& directoryPath)
{
std::list<std::string> files;
char** rc = PHYSFS_enumerateFiles(resolvePath(directoryPath).c_str());
auto rc = PHYSFS_enumerateFiles(resolvePath(directoryPath).c_str());
for(char** i = rc; *i != NULL; i++)
files.push_back(*i);
for(int i = 0; rc[i] != NULL; i++)
files.push_back(rc[i]);
PHYSFS_freeList(rc);
return files;

View File

@@ -29,6 +29,7 @@
#include <typeinfo>
#include <array>
#include <iomanip>
#include <unordered_map>
// boost utilities
#include <boost/algorithm/string.hpp>

View File

@@ -12,8 +12,8 @@ AnimatedTexture::AnimatedTexture(int width, int height, int channels, int numFra
{
m_size.setSize(width, height);
m_framesTextureId = new uint[numFrames];
m_framesDelay = new int[numFrames];
m_framesTextureId.resize(numFrames);
m_framesDelay.resize(numFrames);
for(int i=0;i<numFrames;++i) {
uchar *framePixels = framesPixels + (i*height*width* channels);
@@ -29,9 +29,8 @@ AnimatedTexture::AnimatedTexture(int width, int height, int channels, int numFra
AnimatedTexture::~AnimatedTexture()
{
glDeleteTextures(m_numFrames, m_framesTextureId);
delete[] m_framesTextureId;
delete[] m_framesDelay;
assert(!g_graphics.isDrawing());
glDeleteTextures(m_numFrames, &m_framesTextureId[0]);
m_textureId = 0;
}

View File

@@ -7,14 +7,14 @@ class AnimatedTexture : public Texture
{
public:
AnimatedTexture(int width, int height, int channels, int numFrames, uchar *framesPixels, int *framesDelay);
~AnimatedTexture();
virtual ~AnimatedTexture();
void enableBilinearFilter();
void processAnimation();
private:
uint *m_framesTextureId;
int *m_framesDelay;
std::vector<uint> m_framesTextureId;
std::vector<int> m_framesDelay;
int m_numFrames;
int m_currentFrame;
int m_lastAnimCheckTicks;

View File

@@ -224,7 +224,7 @@ Size Font::calculateTextRectSize(const std::string& text)
void Font::calculateGlyphsWidthsAutomatically(const Size& glyphSize)
{
int numHorizontalGlyphs = m_texture->getSize().width() / glyphSize.width();
uchar *texturePixels = m_texture->getPixels();
auto texturePixels = m_texture->getPixels();
// small AI to auto calculate pixels widths
for(int glyph = m_firstGlyph; glyph< 256; ++glyph) {
@@ -256,6 +256,4 @@ void Font::calculateGlyphsWidthsAutomatically(const Size& glyphSize)
// store glyph size
m_glyphsSize[glyph].setSize(width, m_glyphHeight);
}
delete[] texturePixels;
}

View File

@@ -26,13 +26,16 @@ void Graphics::init()
logInfo("OpenGL ", glGetString(GL_VERSION));
m_drawing = false;
bindColor(Color::white);
m_opacity = 255;
m_emptyTexture = TexturePtr(new Texture);
bindColor(Color::white);
}
void Graphics::terminate()
{
g_fonts.releaseFonts();
m_emptyTexture.reset();
}
bool Graphics::isExtensionSupported(const char *extension)
@@ -110,7 +113,7 @@ void Graphics::drawTexturedRect(const Rect& screenCoords,
const TexturePtr& texture,
const Rect& textureCoords)
{
if(screenCoords.isEmpty())
if(screenCoords.isEmpty() || texture->getId() == 0)
return;
// rect correction for opengl
@@ -156,7 +159,7 @@ void Graphics::drawRepeatedTexturedRect(const Rect& screenCoords,
const TexturePtr& texture,
const Rect& textureCoords)
{
if(screenCoords.isEmpty() || textureCoords.isEmpty())
if(screenCoords.isEmpty() || texture->getId() == 0 || textureCoords.isEmpty())
return;
if(!m_drawing) {

View File

@@ -50,14 +50,17 @@ public:
void startDrawing();
void stopDrawing();
bool isDrawing() const { return m_drawing; }
int getOpacity() const { return m_opacity; }
void setOpacity(int opacity) { m_opacity = opacity; }
TexturePtr getEmptyTexture() { return m_emptyTexture; }
private:
bool m_drawing;
int m_opacity;
Size m_screenSize;
TexturePtr m_emptyTexture;
};
extern Graphics g_graphics;

View File

@@ -3,14 +3,29 @@
#include <GL/gl.h>
Texture::Texture()
{
m_textureId = 0;
}
Texture::Texture(int width, int height, int channels, uchar *pixels)
{
// generate opengl texture
m_textureId = internalLoadGLTexture(pixels, channels, width, height);
}
Texture::~Texture()
{
assert(!g_graphics.isDrawing());
// free texture from gl memory
if(m_textureId > 0)
glDeleteTextures(1, &m_textureId);
}
uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int height)
{
assert(!g_graphics.isDrawing());
m_size.setSize(width, height);
// gets max texture size supported by the driver
@@ -30,7 +45,8 @@ uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int
GLuint id;
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
bool mustFree = false;
std::vector<uchar> tmp;
// old opengl drivers only accept power of two dimensions
if(!g_graphics.isExtensionSupported("GL_ARB_texture_non_power_of_two") && pixels) {
@@ -43,15 +59,13 @@ uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int
glHeight = glHeight << 1;
if(m_size != m_glSize) {
uchar *tmp = new uchar[glHeight*glWidth*channels];
memset(tmp, 0, glHeight*glWidth*channels);
tmp.resize(glHeight*glWidth*channels, 0);
if(pixels)
for(int y=0; y<height; ++y)
for(int x=0; x<width; ++x)
for(int i=0; i<channels; ++i)
tmp[y*glWidth*channels+x*channels+i] = pixels[y*width*channels+x*channels+i];
pixels = tmp;
mustFree = true;
pixels = &tmp[0];
}
m_glSize.setSize(glWidth, glHeight);
@@ -86,19 +100,9 @@ uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
if(mustFree)
delete[] pixels;
return id;
}
Texture::~Texture()
{
// free texture from gl memory
if(m_textureId)
glDeleteTextures(1, &m_textureId);
}
void Texture::enableBilinearFilter()
{
// enable smooth texture
@@ -107,12 +111,12 @@ void Texture::enableBilinearFilter()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
uchar* Texture::getPixels()
std::vector<uint8> Texture::getPixels()
{
// copy pixels from opengl memory
uchar* pixels = new uchar[m_glSize.area()*4];
std::vector<uint8> pixels(m_glSize.area()*4, 0);
glBindTexture(GL_TEXTURE_2D, m_textureId);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, &pixels[0]);
// convert pixels to the real texture size
if(m_size != m_glSize)

View File

@@ -7,6 +7,7 @@ class Texture : public std::enable_shared_from_this<Texture>
{
public:
/// Create a texture, width and height must be a multiple of 2
Texture();
Texture(int width, int height, int channels, uchar* pixels = NULL);
virtual ~Texture();
@@ -17,15 +18,16 @@ public:
virtual uint getId() const { return m_textureId; }
/// Copy pixels from OpenGL texture
uchar* getPixels();
std::vector<uint8> getPixels();
int getWidth() const { return m_size.width(); }
int getHeight() const { return m_size.height(); }
const Size getSize() const { return m_size; }
const Size& getGlSize() const { return m_glSize; }
bool isEmpty() const { return m_textureId == 0; }
protected:
Texture() { }
uint internalLoadGLTexture(uchar* pixels, int channels, int w, int h);
uint m_textureId;

View File

@@ -23,25 +23,6 @@ Rsa::~Rsa()
mpz_clear(m_mod);
}
bool Rsa::setKey(const std::string& file)
{
//loads p,q and d from a file
FILE* f = fopen(file.c_str(), "r");
if(!f){
return false;
}
char p[512];
char q[512];
char d[512];
delete fgets(p, 512, f);
delete fgets(q, 512, f);
delete fgets(d, 512, f);
setKey(p, q, d);
return true;
}
void Rsa::setKey(const char* p, const char* q, const char* d)
{
mpz_set_str(m_p, p, 10);

View File

@@ -9,8 +9,8 @@ class Rsa
public:
Rsa();
~Rsa();
void setKey(const char* p, const char* q, const char* d);
bool setKey(const std::string& file);
bool decrypt(char* msg, int32_t size);
static bool encrypt(char* msg, int32_t size, const char* key);
@@ -20,6 +20,4 @@ protected:
mpz_t m_p, m_q, m_u, m_d, m_dp, m_dq, m_mod;
};
typedef std::shared_ptr<Rsa> RsaPtr;
#endif //RSA_H
#endif

View File

@@ -151,7 +151,7 @@ enum InputKeyCode {
KC_MEDIASELECT = 0xED // Media Select
};
enum InputEventType {
enum PlatformEventType {
EventNone = 0,
EventMouseAction = 1,
EventKeyboardAction = 2,
@@ -175,7 +175,7 @@ enum InputEventType {
EventMouseWheelDown = EventMouseAction | EventMouseWheel | EventDown
};
struct InputEvent {
struct PlatformEvent {
int type;
Point mousePos;
Point mouseMoved;

View File

@@ -11,7 +11,7 @@ public:
/// Fired when user resize the window
virtual void onResize(const Size& size) = 0;
/// Fired when user press a key or move the mouse
virtual void onInputEvent(const InputEvent& event) = 0;
virtual void onPlatformEvent(const PlatformEvent& event) = 0;
};
#endif

View File

@@ -260,7 +260,7 @@ void Platform::terminate()
void Platform::poll()
{
XEvent event, peekevent;
static InputEvent inputEvent;
static PlatformEvent inputEvent;
while(XPending(x11.display) > 0) {
XNextEvent(x11.display, &event);
@@ -335,7 +335,7 @@ void Platform::poll()
inputEvent.type = EventTextEnter;
inputEvent.keychar = buf[0];
inputEvent.keycode = KC_UNKNOWN;
m_listener->onInputEvent(inputEvent);
m_listener->onPlatformEvent(inputEvent);
}
}
@@ -348,7 +348,7 @@ void Platform::poll()
inputEvent.keycode = x11.keyMap[keysym];
inputEvent.type = (event.type == KeyPress) ? EventKeyDown : EventKeyUp;
inputEvent.keychar = (len > 0) ? buf[0] : 0;
m_listener->onInputEvent(inputEvent);
m_listener->onPlatformEvent(inputEvent);
}
break;
}
@@ -371,7 +371,7 @@ void Platform::poll()
inputEvent.type = EventMouseWheelDown;
break;
}
m_listener->onInputEvent(inputEvent);
m_listener->onPlatformEvent(inputEvent);
break;
case MotionNotify:
@@ -380,7 +380,7 @@ void Platform::poll()
Point newMousePos(event.xbutton.x, event.xbutton.y);
inputEvent.mouseMoved = newMousePos - inputEvent.mousePos;
inputEvent.mousePos = newMousePos;
m_listener->onInputEvent(inputEvent);
m_listener->onPlatformEvent(inputEvent);
break;
}

View File

@@ -37,7 +37,7 @@ void UIManager::resize(const Size& size)
m_rootWidget->resize(size);
}
void UIManager::inputEvent(const InputEvent& event)
void UIManager::inputEvent(const PlatformEvent& event)
{
// translate input event to ui events
if(m_rootWidget) {

View File

@@ -13,7 +13,7 @@ public:
void render();
void resize(const Size& size);
void inputEvent(const InputEvent& event);
void inputEvent(const PlatformEvent& event);
bool importStyles(const std::string& file);
void importStyleFromOTML(const OTMLNodePtr& styleNode);

View File

@@ -26,6 +26,7 @@ public:
void setGreen(uint8 g) { color = ((g & 0xff)<<8) | (color & 0xffff00ff); }
void setRed(uint8 r) { color = (r & 0xff) | (color & 0xffffff00); }
void setRGBA(uint8 r, uint8 g, uint8 b, uint8 a = 0xFF) { color = ((a & 0xff)<<24) | ((b & 0xff)<<16) | ((g & 0xff)<<8) | (r & 0xff); }
void setABGR(uint32 abgr) { color = ((abgr>>24) & 0xff) | ((abgr>>16) & 0xff) << 8 | ((abgr>>8) & 0xff) << 16 | (abgr & 0xff) << 24; }
void setRGBA(uint32 rgba) { color = rgba; }
Color& operator=(const Color& other) { color = other.color; return *this; }
@@ -47,17 +48,26 @@ private:
inline std::ostream& operator<<(std::ostream& out, const Color& color)
{
out << (int)color.r() << " "<< (int)color.g() << " "<< (int)color.b() << " " << (int)color.a();
using namespace std;
out << "#" << hex << setfill('0')
<< setw(2) << (int)color.r()
<< setw(2) << (int)color.g()
<< setw(2) << (int)color.b()
<< setw(2) << (int)color.a();
out << dec << setfill(' ');
return out;
}
inline std::istream& operator>>(std::istream& in, Color& color)
{
int r, g, b, a = 255;
in >> r >> g >> b;
if(!in.eof())
in >> a;
color.setRGBA(r, g, b, a);
using namespace std;
if(in.get() == '#') {
uint32 tmp;
in >> hex >> tmp;
color.setABGR(tmp);
in >> dec;
}
return in;
}

View File

@@ -10,6 +10,25 @@
namespace fw {
// read utilities for istream
inline uint8 getu8(std::istream& in) {
uint8 tmp;
in.read((char*)&tmp, 1);
return tmp;
}
inline uint16 getu16(std::istream& in) {
uint16 tmp;
in.read((char*)&tmp, 2);
return tmp;
}
inline uint32 getu32(std::istream& in) {
uint32 tmp;
in.read((char*)&tmp, 4);
return tmp;
}
/// Fill an ostream by concatenating args
/// Usage:
/// fw::fill_ostream(stream, a1, a2, ..., aN);