summaryrefslogtreecommitdiffstats
path: root/examples/qtgl
diff options
context:
space:
mode:
authortpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2010-02-24 17:43:19 +0000
committertpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2010-02-24 17:43:19 +0000
commit0292059f4a16434600564cfa3f0ad2309a508a54 (patch)
treed95953cd53011917c4df679b96aedca39401b54f /examples/qtgl
downloadlibksquirrel-0292059f4a16434600564cfa3f0ad2309a508a54.tar.gz
libksquirrel-0292059f4a16434600564cfa3f0ad2309a508a54.zip
Added libksquirrel for KDE3
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/libraries/libksquirrel@1095624 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'examples/qtgl')
-rw-r--r--examples/qtgl/main.cpp30
-rw-r--r--examples/qtgl/myqgl.cpp182
-rw-r--r--examples/qtgl/myqgl.h40
-rw-r--r--examples/qtgl/qtgl.pro12
4 files changed, 264 insertions, 0 deletions
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 <qapplication.h>
+#include <qvbox.h>
+#include <qpushbutton.h>
+
+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 <qlibrary.h>
+#include <qapplication.h>
+#include <qfile.h>
+
+#include <GL/gl.h>
+
+#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 <qgl.h>
+
+#include <csetjmp>
+
+#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