From 0292059f4a16434600564cfa3f0ad2309a508a54 Mon Sep 17 00:00:00 2001 From: tpearson Date: Wed, 24 Feb 2010 17:43:19 +0000 Subject: Added libksquirrel for KDE3 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/libraries/libksquirrel@1095624 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- examples/qtapp/main.cpp | 24 +++++++ examples/qtapp/myqt.cpp | 111 +++++++++++++++++++++++++++++ examples/qtapp/myqt.h | 33 +++++++++ examples/qtapp/qtapp.pro | 11 +++ examples/qtgl/main.cpp | 30 ++++++++ examples/qtgl/myqgl.cpp | 182 +++++++++++++++++++++++++++++++++++++++++++++++ examples/qtgl/myqgl.h | 40 +++++++++++ examples/qtgl/qtgl.pro | 12 ++++ examples/w3.bmp | Bin 0 -> 49202 bytes 9 files changed, 443 insertions(+) create mode 100644 examples/qtapp/main.cpp create mode 100644 examples/qtapp/myqt.cpp create mode 100644 examples/qtapp/myqt.h create mode 100644 examples/qtapp/qtapp.pro create mode 100644 examples/qtgl/main.cpp create mode 100644 examples/qtgl/myqgl.cpp create mode 100644 examples/qtgl/myqgl.h create mode 100644 examples/qtgl/qtgl.pro create mode 100644 examples/w3.bmp (limited to 'examples') diff --git a/examples/qtapp/main.cpp b/examples/qtapp/main.cpp new file mode 100644 index 0000000..91c4052 --- /dev/null +++ b/examples/qtapp/main.cpp @@ -0,0 +1,24 @@ +#include "myqt.h" + +#include +#include +#include + +int main(int argc, char **argv) +{ + QApplication a(argc, argv); + + QVBox *v = new QVBox; + QPushButton *b = new QPushButton("Show w3.bmp !", v); + MyQT *qt = new MyQT(v); + + QVBox::connect(b, SIGNAL(clicked()), qt, SLOT(bind())); + + v->setStretchFactor(qt, 1); + + a.setMainWidget(v); + v->resize(512, 256); + v->show(); + + return a.exec(); +} diff --git a/examples/qtapp/myqt.cpp b/examples/qtapp/myqt.cpp new file mode 100644 index 0000000..9cd4b80 --- /dev/null +++ b/examples/qtapp/myqt.cpp @@ -0,0 +1,111 @@ +#include +#include +#include + +#include + +#include "myqt.h" + +#include "ksquirrel-libs/fmt_utils.h" +#include "ksquirrel-libs/error.h" + +MyQT::MyQT(QWidget *parent, const char *name) : QLabel(parent, name) +{ + setAlignment(Qt::AlignCenter); +} + +MyQT::~MyQT() +{} + +QPixmap MyQT::loadImage() +{ + QLibrary lib("/usr/lib/ksquirrel-libs/libkls_bmp.so"); + lib.load(); + + if(!lib.isLoaded()) + { + qWarning("Can't load BMP library."); + qApp->quit(); + } + + int i = 0; + fmt_info finfo; + RGBA *image; + int current = 0; + + codec_create = (fmt_codec_base*(*)())lib.resolve("codec_create"); + codec_destroy = (void (*)(fmt_codec_base*))lib.resolve("codec_destroy"); + + if(!codec_create || !codec_destroy) + { + qWarning("Library corrupted."); + lib.unload(); + qApp->quit(); + } + + const char *s = "../w3.bmp"; + + if(!QFile::exists(s)) + { + qWarning("Can't find example image."); + lib.unload(); + qApp->quit(); + } + + codeK = codec_create(); + + i = codeK->read_init(s); + + if(i != SQE_OK) + { + codeK->read_close(); + return QPixmap(); + } + + i = codeK->read_next(); + + finfo = codeK->information(); + + if(i != SQE_OK) + { + codeK->read_close(); + return QPixmap(); + } + + image = (RGBA*)calloc(finfo.image[current].w * finfo.image[current].h, sizeof(RGBA)); + + if(!image) + { + codeK->read_close(); + return QPixmap(); + } + + memset(image, 255, finfo.image[current].w * finfo.image[current].h * sizeof(RGBA)); + + RGBA *scan; + + for(int pass = 0;pass < finfo.image[current].passes;pass++) + { + codeK->read_next_pass(); + + for(int j = 0;j < finfo.image[current].h;j++) + { + scan = image + j * finfo.image[current].w; + codeK->read_scanline(scan); + } + } + + if(finfo.image[current].needflip) + fmt_utils::flipv((char*)image, finfo.image[current].w * sizeof(RGBA), finfo.image[current].h); + + codeK->read_close(); + + QImage im((unsigned char*)image, finfo.image[current].w, finfo.image[current].h, 32, 0, 0, QImage::LittleEndian); + + return QPixmap(im.swapRGB()); +} + +void MyQT::bind() +{ + setPixmap(loadImage()); +} diff --git a/examples/qtapp/myqt.h b/examples/qtapp/myqt.h new file mode 100644 index 0000000..9e63d4f --- /dev/null +++ b/examples/qtapp/myqt.h @@ -0,0 +1,33 @@ +#ifndef GLWIDGET_H +#define GLWIDGET_H + +#include + +#include + +#include "ksquirrel-libs/fmt_types.h" +#include "ksquirrel-libs/fileio.h" +#include "ksquirrel-libs/fmt_codec_base.h" + +class MyQT : public QLabel +{ + Q_OBJECT + + public: + MyQT(QWidget *parent = 0, const char *name = 0); + ~MyQT(); + + QPixmap loadImage(); + + public slots: + void bind(); + + private: + fmt_codec_base* (*codec_create)(); + void (*codec_destroy)(fmt_codec_base*); + + fmt_codec_base *codeK; + +}; + +#endif diff --git a/examples/qtapp/qtapp.pro b/examples/qtapp/qtapp.pro new file mode 100644 index 0000000..9c658c0 --- /dev/null +++ b/examples/qtapp/qtapp.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (1.06c) Thu Mar 3 02:54:53 2005 +###################################################################### + +TEMPLATE = app +INCLUDEPATH += . +INCLUDEPATH += ../../kernel/include + +# Input +HEADERS += myqt.h +SOURCES += main.cpp myqt.cpp ../../kernel/ksquirrel-libs/fmt_utils.cpp diff --git a/examples/qtgl/main.cpp b/examples/qtgl/main.cpp new file mode 100644 index 0000000..a85d421 --- /dev/null +++ b/examples/qtgl/main.cpp @@ -0,0 +1,30 @@ +#include "myqgl.h" + +#include +#include +#include + +int main(int argc, char **argv) +{ + QApplication a(argc, argv); + + if(!QGLFormat::hasOpenGL()) + { + qWarning( "This system has no OpenGL support. Exiting." ); + return -1; + } + + QVBox *v = new QVBox; + QPushButton *b = new QPushButton("Show w3.bmp !", v); + MyQGL *gl = new MyQGL(v); + + QVBox::connect(b, SIGNAL(clicked()), gl, SLOT(bind())); + + v->setStretchFactor(gl, 1); + + a.setMainWidget(v); + v->resize(512, 256); + v->show(); + + return a.exec(); +} diff --git a/examples/qtgl/myqgl.cpp b/examples/qtgl/myqgl.cpp new file mode 100644 index 0000000..42fe1da --- /dev/null +++ b/examples/qtgl/myqgl.cpp @@ -0,0 +1,182 @@ +#include +#include +#include + +#include + +#include "myqgl.h" + +#include "ksquirrel-libs/fmt_utils.h" +#include "ksquirrel-libs/error.h" + +MyQGL::MyQGL(QWidget *parent, const char *name) : QGLWidget(parent, name), bits(0), w(0), h(0) +{} + +MyQGL::~MyQGL() +{} + +void MyQGL::initializeGL() +{ + qglClearColor(white); + glClearDepth(1.0f); + glEnable(GL_DEPTH_TEST); + glEnable(GL_ALPHA_TEST); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glShadeModel(GL_FLAT); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glMatrixMode(GL_MODELVIEW); + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); +} + +void MyQGL::resizeGL(int width, int height) +{ + glViewport(0, 0, width, height); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(-width/2, width/2, -height/2, height/2, 0.1f, 100.0f); + gluLookAt(0,0,1, 0,0,0, 0,1,0); + glMatrixMode(GL_MODELVIEW); +} + +void MyQGL::paintGL() +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glEnable(GL_TEXTURE_2D); + + glBindTexture(GL_TEXTURE_2D, tex); + + glBegin(GL_QUADS); + glTexCoord2f(0.0f, 0.0f); glVertex2f(-w/2, h/2); + glTexCoord2f(1.0f, 0.0f); glVertex2f(w/2, h/2); + glTexCoord2f(1.0f, 1.0f); glVertex2f(w/2, -h/2); + glTexCoord2f(0.0f, 1.0f); glVertex2f(-w/2, -h/2); + glEnd(); + + glDisable(GL_TEXTURE_2D); +} + +void MyQGL::loadImage() +{ + // try to load BMP library + QLibrary lib("/usr/lib/ksquirrel-libs/libkls_bmp.so"); + lib.load(); + + // no such library + if(!lib.isLoaded()) + { + qWarning("Can't load BMP library."); + qApp->quit(); + } + + int i = 0; + fmt_info finfo; + RGBA *image; + int current = 0; + + // resolve neccessary functions + codec_create = (fmt_codec_base*(*)())lib.resolve("codec_create"); + codec_destroy = (void (*)(fmt_codec_base*))lib.resolve("codec_destroy"); + + // library corrupted! + if(!codec_create || !codec_destroy) + { + qWarning("Library corrupted."); + lib.unload(); + qApp->quit(); + } + + const char *s = "../w3.bmp"; + + // if the image doesn't exist + if(!QFile::exists(s)) + { + qWarning("Can't find example image."); + lib.unload(); + qApp->quit(); + } + + // OK, create decoder + codeK = codec_create(); + + // init library + i = codeK->read_init(s); + + if(i != SQE_OK) + { + codeK->read_close(); + return; + } + + // move to the next image + i = codeK->read_next(); + + // save retrieved information + finfo = codeK->information(); + + if(i != SQE_OK) + { + codeK->read_close(); + return; + } + + image = (RGBA*)calloc(finfo.image[current].w * finfo.image[current].h, sizeof(RGBA)); + + if(!image) + { + codeK->read_close(); + return; + } + + memset(image, 255, finfo.image[current].w * finfo.image[current].h * sizeof(RGBA)); + + RGBA *scan; + + // OK, let's decode the image line-by-line, pass-by-pass + for(int pass = 0;pass < finfo.image[current].passes;pass++) + { + codeK->read_next_pass(); + + for(int j = 0;j < finfo.image[current].h;j++) + { + scan = image + j * finfo.image[current].w; + codeK->read_scanline(scan); + } + } + + // flip, if neccessary (BMP requires flipping) + if(finfo.image[current].needflip) + fmt_utils::flipv((char*)image, finfo.image[current].w * sizeof(RGBA), finfo.image[current].h); + + // close library + codeK->read_close(); + + w = finfo.image[current].w; + h = finfo.image[current].h; + bits = image; +} + +void MyQGL::bind() +{ + loadImage(); + + if(!bits) + return; + + glGenTextures(1, &tex); + glBindTexture(GL_TEXTURE_2D, tex); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + + // remember, that texture dimensions should be a power of two, e.g. + // 32, 64, 256, ... + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, bits); + + updateGL(); +} diff --git a/examples/qtgl/myqgl.h b/examples/qtgl/myqgl.h new file mode 100644 index 0000000..be4707f --- /dev/null +++ b/examples/qtgl/myqgl.h @@ -0,0 +1,40 @@ +#ifndef GLWIDGET_H +#define GLWIDGET_H + +#include + +#include + +#include "ksquirrel-libs/fmt_types.h" +#include "ksquirrel-libs/fileio.h" +#include "ksquirrel-libs/fmt_codec_base.h" + +class MyQGL : public QGLWidget +{ + Q_OBJECT + + public: + MyQGL(QWidget *parent = 0, const char *name = 0); + ~MyQGL(); + + public slots: + void bind(); + + protected: + void initializeGL(); + void paintGL(); + void resizeGL(int,int); + void loadImage(); + + private: + void *bits; + unsigned int tex; + int w, h; + fmt_codec_base* (*codec_create)(); + void (*codec_destroy)(fmt_codec_base*); + + fmt_codec_base *codeK; + +}; + +#endif diff --git a/examples/qtgl/qtgl.pro b/examples/qtgl/qtgl.pro new file mode 100644 index 0000000..83a68e4 --- /dev/null +++ b/examples/qtgl/qtgl.pro @@ -0,0 +1,12 @@ +###################################################################### +# Automatically generated by qmake (1.06c) Thu Mar 3 00:43:05 2005 +###################################################################### + +TEMPLATE = app +INCLUDEPATH += . +INCLUDEPATH += ../../kernel/include + +# Input +HEADERS += myqgl.h +SOURCES += main.cpp myqgl.cpp ../../kernel/ksquirrel-libs/fmt_utils.cpp +LIBS += -lGL -lGLU \ No newline at end of file diff --git a/examples/w3.bmp b/examples/w3.bmp new file mode 100644 index 0000000..d27f12d Binary files /dev/null and b/examples/w3.bmp differ -- cgit v1.2.1