This commit is contained in:
Stefan Bühler 2009-01-13 14:57:44 +01:00
parent 7acd3fe1a3
commit 439b1822a6
8 changed files with 133 additions and 1 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -77,6 +77,9 @@ namespace toruschess {
void Field3D::paintGL() { void Field3D::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// m_lib->paint_pawn();
// return;
glBindTexture(GL_TEXTURE_2D, m_textureID); glBindTexture(GL_TEXTURE_2D, m_textureID);
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
{ {

View File

@ -12,5 +12,6 @@
<file alias="chess_10.svg">../media/Chess_rdt45.svg</file> <file alias="chess_10.svg">../media/Chess_rdt45.svg</file>
<file alias="chess_11.svg">../media/Chess_qdt45.svg</file> <file alias="chess_11.svg">../media/Chess_qdt45.svg</file>
<file alias="chess_12.svg">../media/Chess_kdt45.svg</file> <file alias="chess_12.svg">../media/Chess_kdt45.svg</file>
<file alias="pawn.3ds">../media/pawn.3ds</file>
</qresource> </qresource>
</RCC> </RCC>

View File

@ -20,15 +20,125 @@
#include "piecelibrary.h" #include "piecelibrary.h"
#include <QPainter> #include <QPainter>
#include <QResource>
#include <lib3ds/io.h>
#include <lib3ds/mesh.h>
#include <GL/gl.h>
namespace toruschess { namespace toruschess {
typedef Lib3dsBool (*Lib3dsIoErrorFunc)(void *self);
typedef long (*Lib3dsIoSeekFunc)(void *self, long offset, Lib3dsIoSeek origin);
typedef long (*Lib3dsIoTellFunc)(void *self);
typedef size_t (*Lib3dsIoReadFunc)(void *self, void *buffer, size_t size);
typedef size_t (*Lib3dsIoWriteFunc)(void *self, const void *buffer, size_t size);
Lib3dsBool io3ds_error(void *self);
long io3ds_seek(void *self, long offset, Lib3dsIoSeek origin);
long io3ds_tell(void *self);
size_t io3ds_read(void *self, void *buffer, size_t size);
size_t io3ds_write(void *self, const void *buffer, size_t size);
class IO3ds {
public:
IO3ds(const QByteArray &data) : m_data(data), m_offset(0) {
m_io = lib3ds_io_new(this, io3ds_error, io3ds_seek, io3ds_tell, io3ds_read, io3ds_write);
}
~IO3ds() {
lib3ds_io_free(m_io);
}
long seek(long offset, Lib3dsIoSeek origin) {
switch (origin) {
case LIB3DS_SEEK_SET:
m_offset = offset;
break;
case LIB3DS_SEEK_CUR:
m_offset += offset;
break;
case LIB3DS_SEEK_END:
m_offset = m_data.length() + offset;
break;
}
return 0;
}
long tell() { return m_offset; }
size_t read(void *buffer, size_t size) {
qDebug("read (size %i): data-len = %i, offset = %i", (int) size, m_data.length(), (int) m_offset);
if (m_offset >= m_data.length()) return 0;
size_t r = qMin(size, (size_t) m_data.length() - m_offset);
memcpy(buffer, m_data.constData() + m_offset, r);
m_offset += r;
qDebug("read: done with %i", (int) r);
return r;
}
size_t write(const void *, size_t ) {
return 0;
}
Lib3dsBool error() {
qDebug("3ds error");
return true;
}
Lib3dsIo* io() { return m_io; }
private:
Lib3dsIo *m_io;
QByteArray m_data;
long m_offset;
};
Lib3dsBool io3ds_error(void *self) {
IO3ds *s = (IO3ds*) self;
return s->error();
}
long io3ds_seek(void *self, long offset, Lib3dsIoSeek origin) {
IO3ds *s = (IO3ds*) self;
return s->seek(offset, origin);
}
long io3ds_tell(void *self) {
IO3ds *s = (IO3ds*) self;
return s->tell();
}
size_t io3ds_read(void *self, void *buffer, size_t size) {
IO3ds *s = (IO3ds*) self;
return s->read(buffer, size);
}
size_t io3ds_write(void *self, const void *buffer, size_t size) {
IO3ds *s = (IO3ds*) self;
return s->write(buffer, size);
}
Lib3dsFile *read_3ds(const QByteArray &data) {
return lib3ds_file_load("media/pawn.3ds");
IO3ds io(data);
Lib3dsFile *f = lib3ds_file_new();
if (!lib3ds_file_read(f, io.io())) {
qDebug("data size: %i", (int) data.size());
lib3ds_file_free(f);
return NULL;
}
return f;
}
QByteArray loadResource(const QString &filename) {
QResource res(filename);
qDebug("res size: %i", (int) res.size());
return QByteArray((const char*) res.data(), (int) res.size());
}
PieceLibrary::PieceLibrary(QObject *parent) PieceLibrary::PieceLibrary(QObject *parent)
: QObject(parent), m_pieces(0), m_buffers(0) { : QObject(parent), m_pieces(0), m_buffers(0) {
m_pawn = read_3ds(loadResource(":/media/pawn.3ds"));
setSize(45, 45); setSize(45, 45);
} }
PieceLibrary::~PieceLibrary() { PieceLibrary::~PieceLibrary() {
lib3ds_file_free(m_pawn);
delete [] m_pieces; delete [] m_pieces;
delete [] m_buffers; delete [] m_buffers;
} }
@ -68,5 +178,15 @@ namespace toruschess {
if (place <= 0 || place > 12) return; if (place <= 0 || place > 12) return;
pt.drawImage(rect, *m_buffers[place-1]); pt.drawImage(rect, *m_buffers[place-1]);
} }
void PieceLibrary::paint_pawn() {
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,0,m_pawn->meshes->pointL);
for (unsigned int i = 0; i < m_pawn->meshes->faces; i++) {
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, m_pawn->meshes->faceL[i].points);
}
glDisableClientState(GL_VERTEX_ARRAY);
}
} }

View File

@ -22,6 +22,8 @@
#include <QSvgRenderer> #include <QSvgRenderer>
#include <lib3ds/file.h>
/** /**
@author Stefan Bühler <stbuehler@web.de> @author Stefan Bühler <stbuehler@web.de>
*/ */
@ -40,10 +42,14 @@ namespace toruschess {
void paint(QPainter &pt, int place) const; void paint(QPainter &pt, int place) const;
void paint(QPainter &pt, int place, const QRect &rect) const; void paint(QPainter &pt, int place, const QRect &rect) const;
void paint_pawn();
private: private:
QSvgRenderer **m_pieces; QSvgRenderer **m_pieces;
QImage **m_buffers; QImage **m_buffers;
int m_width, m_height; int m_width, m_height;
Lib3dsFile *m_pawn;
}; };
} }

View File

@ -23,3 +23,5 @@ toruschess.h \
field3d.h field3d.h
CONFIG -= release CONFIG -= release
LIBS += -l3ds