Archive

Archive for the ‘C++’ Category

QPasm 1.1 RC1

September 9th, 2009 Wesley 8 comments

The last few days I’ve been improving the small pseudo-assembler interpreter/IDE that I had created. I am quite happy with the result.

The new version of QPasm has the following new features:

  • Code editor with intelligent syntax highlighting, line numbering, visual breakpoints and undo/redo functionality
  • Debugging features: breakpoints, manual step, timed step, pausing
  • On-the-fly editing of data in the register or the memory
  • On-the-fly symbol resolving: labels can be used in assembler apps, and when modifying memory when the program is running the labels are resolved automatically
  • Integrates well with light and dark system themes. Highlighter chooses its color theme based on the darkness of the theme automatically, but colors and fonts can be configured manually as well
  • Layout, font and color settings are stored locally in a portable config.ini file
  • Input format is very flexible: white space may occur before, after and between instructions, instructions are case insensitive, comments are supported anywhere
  • Pseudo-assembler apps which are run using the run-function run in a separate thread which has a system preventing the GUI from freezing by limiting the amount of simultaneous signals to the GUI. Assembler apps which cause an endless loop cannot freeze the GUI

More information, binaries and source code are available at http://code.google.com/p/qpasm/

VN:F [1.6.3_896]
Rating: +3 (from 3 votes)
Categories: C++, Open Source, Programming, Qt, School

Small pseudo-assembler interpreter

August 30th, 2009 Wesley 2 comments

I had to develop a pseudo-assembler interpreter for the course Microprocessing.

Since it was just lying around on my hard drive I figured I could just as well put it on-line. It contains a few things that might be interesting to developers:

  • Using a C library in C++/Qt applications and translating C function callbacks into Qt signals
  • Implementation of virtual static and virtual dynamic memory
  • Converting between virtual signed and unsigned values (system independent)
  • Saving data and instructions in same virtual memory (Von Neumann architecture)

More information (and source code) is available here: http://wesley.vidiqatch.org/files/qpasm/

VN:F [1.6.3_896]
Rating: +1 (from 1 vote)
Categories: C, C++, Open Source, Programming, Qt, School

NeHe OpenGL lessons in Qt – Chapter 4

August 8th, 2009 Wesley No comments

As promised, the fourth chapter of the NeHe OpenGL lessons ported to make use of the Qt toolkit.

Fourth chapter: fog, fonts revisited, quadrics, particle engine, triangle strips, masking

In the fourth chapter you will learn a few cooler tricks. You will learn how to create good-looking fog effects and how certain objects can easily be constructed using quadrics. But the coolest thing that you will learn is how to create a simple particle engine (during lesson 19). To end off the chapter, you will learn how you can use masking to create partial transparency using bitmap textures.

Some minor modifications were made to improve the visual appearance of some of the lessons.

Videos and source code

This video shows the fog effect (lesson 16)
This video shows what you can achieve using quadrics (lesson 18)
This video shows the very cool particle engine that you will create! (lesson 19)
This video shows which effect masking has (lesson 20)
You can download the Qt 4 source code for this chapter here.

PS: The port of chapter 5 will take a while… I will be a bit busy the coming weeks, and the first lesson of chapter 5 is HUGE, so will require a lot of time to port.
Oh, and apparently I forgot to upload the source code for the third chapter. I have uploaded it now :)

VN:F [1.6.3_896]
Rating: +8 (from 8 votes)
Categories: C++, OpenGL, Programming, Qt

NeHe OpenGL lessons in Qt – Chapter 3

August 4th, 2009 Wesley 3 comments

Here is the third chapter of the NeHe OpenGL lessons ported to make use of the Qt toolkit.

Third chapter: waving texture, display lists and a lot of fonts

The third chapter starts off with a cool looking waving flag effect. After that you will learn about display lists. The last three lessons focus on different ways of displaying fonts. It is worth noting that rendering basic text at a chosen location, or translated into the OpenGL scene is extremely easy in Qt/OpenGL thanks to QGLWidget::renderText(). For other basic text effects (such as rotated, skewed or otherwise transformed text) you could also use QPainter directly on a QGLWidget. We won’t be doing this in our examples, but I just wanted to point out that it is possible.

I should also note that I have used an extra library in lesson 14 to display the 3D text. Qt itself is not able to display 3D text and doing this in a cross-platform way yourself would be rather hard. That’s why I have used the FTGL library for this. FTGL is a very easy to use cross-platform library with the sole purpose of rendering (3D) text in OpenGL.

Videos and source code

This video shows the waving flag effect (lesson 11)
This video shows the effect of using display lists (lesson 12)
This video shows rotated 3D text (lesson 14)
You can download the Qt 4 source code for this chapter here.

PS: You can expect chapter 4 in a few days :)

VN:F [1.6.3_896]
Rating: +7 (from 7 votes)
Categories: C++, OpenGL, Programming, Qt

Resizable photo frames in Qt

August 4th, 2009 Wesley No comments

Today someone asked how to copy one image into another image in Qt. I thought it was a nice idea to write an example about how to do that, plus a few extra things. We will create a resizable photo frame from just one simple image of a photo frame! It works like this:

  • Reimplement QWidget::paintEvent() and construct a QPainter(this) in the reimplementation so we can draw on the widget
  • Load the image of the photo frame and define 4 QRect objects to define the position of the frame borders – these borders are not allowed to scale
  • Draw something which will be contained inside of the frame – for example another image using QPainter::drawPixmap() or QPainter::drawImage()
  • Generate and draw the frame bars (the pieces between the borders)
    • Make use of the QImage::mirrored() function and another QPainter to create a new pixmap which contains the frame bar plus the mirrored frame bar.
    • This will make the bar look great when the frame bar is enlarged by tiling. This method is actually a very popular one in basic photo manipulation.
  • Draw the four borders

The result looks like this:

Photo frame in native size – versus – Enlarged photo frame

The code looks like this:

photowidget.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef PHOTOWIDGET_H
#define PHOTOWIDGET_H

#include <QtGui/QWidget>

class PhotoWidget : public QWidget {
    Q_OBJECT

public:
    PhotoWidget(QWidget *parent = 0);

protected:
    void paintEvent(QPaintEvent *event);

private:
    QPixmap createBar(Qt::Orientation orientation, const QPixmap &pixmap, const QRect &rect);
};

#endif // PHOTOWIDGET_H

photowidget.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "photowidget.h"
#include <QPainter>
#include <QPaintEvent>

#define SIZE 108

PhotoWidget::PhotoWidget(QWidget *parent) : QWidget(parent) {
    resize(400, 329);
    setWindowTitle("Photo Frame Example");
}

// This function generates a pixmap that can be used as a tiled bar.
// Note: We use a QPainter and the mirrored() function to make sure that the bar looks good when tiled.
//       This technique is often applied in basic photo manipulation.
QPixmap PhotoWidget::createBar(Qt::Orientation orientation, const QPixmap &pixmap, const QRect &rect) {
    QImage barA = pixmap.copy(rect).toImage();
    QImage barB = barA.mirrored(orientation == Qt::Horizontal ? true : false,
                                orientation == Qt::Vertical ? true : false);

    QSize size;
    size.setWidth(orientation == Qt::Horizontal ? barA.width() << 1 : barA.width());
    size.setHeight(orientation == Qt::Vertical ? barA.height() << 1 : barA.height());

    QPixmap bar(size);
    bar.fill(Qt::transparent);
    QPainter merger(&bar);

    merger.drawImage(0, 0, barA);
    if (orientation == Qt::Horizontal)
        merger.drawImage(barA.width(), 0, barB);
    else
        merger.drawImage(0, barA.height(), barB);

    return bar;
}

void PhotoWidget::paintEvent(QPaintEvent *event) {
    QPainter p(this);

    // Our frame as one full image, and an image to put in the frame
    QPixmap frame(":/img/frame.png");
    QPixmap sky(":/img/palmtree.jpg");

    // These four rectangles define the four borders of the frame
    QRect topLeft(0, 0, SIZE, SIZE);
    QRect topRight(frame.width() - SIZE, 0, SIZE, SIZE);
    QRect bottomLeft(0, frame.height() - SIZE, SIZE, SIZE);
    QRect bottomRight(frame.width() - SIZE, frame.height() - SIZE, SIZE, SIZE);

    // Draw the image first
    p.drawPixmap(QRect(40, 40, event->rect().width() - 80, event->rect().height() - 80), sky);

    // Draw the bars
    p.drawTiledPixmap(QRect(QPoint(SIZE, 0), event->rect().topRight() + QPoint(-SIZE, SIZE - 1)),
                      createBar(Qt::Horizontal, frame, QRect(QPoint(SIZE, 0), frame.rect().topRight() + QPoint(-SIZE, SIZE))));
    p.drawTiledPixmap(QRect(event->rect().bottomLeft() + QPoint(SIZE, -SIZE - 1), event->rect().bottomRight() - QPoint(SIZE, 0)),
                      createBar(Qt::Horizontal, frame, QRect(frame.rect().bottomLeft() + QPoint(SIZE, -SIZE), frame.rect().bottomRight() - QPoint(SIZE, 0))));
    p.drawTiledPixmap(QRect(QPoint(0, SIZE), event->rect().bottomLeft() + QPoint(SIZE, -SIZE)),
                      createBar(Qt::Vertical, frame, QRect(QPoint(0, SIZE), frame.rect().bottomLeft() + QPoint(SIZE, -SIZE))));
    p.drawTiledPixmap(QRect(event->rect().topRight() - QPoint(SIZE + 1, -SIZE), event->rect().bottomRight() - QPoint(0, SIZE)),
                      createBar(Qt::Vertical, frame, QRect(frame.rect().topRight() - QPoint(SIZE, -SIZE), frame.rect().bottomRight() - QPoint(0, SIZE))));

    // Draw the borders
    p.drawPixmap(QPoint(0, 0), frame, topLeft);
    p.drawPixmap(event->rect().topRight() - QPoint(SIZE, 0), frame, topRight);
    p.drawPixmap(event->rect().bottomLeft() - QPoint(0, SIZE), frame, bottomLeft);
    p.drawPixmap(event->rect().bottomRight() - QPoint(SIZE, SIZE), frame, bottomRight);
}
VN:F [1.6.3_896]
Rating: +2 (from 2 votes)
Categories: C++, Programming, Qt

NeHe OpenGL lessons in Qt – Chapter 1 and 2

August 3rd, 2009 Wesley 2 comments

Last week I have ported two chapters (the first 10 lessons) of the NeHe OpenGL lessons to make use of the Qt toolkit. You will notice that the code in Qt is much cleaner and simpler than the code in the original NeHe lessons. There is no need to create a rendering or device context yourself or anything like that, and input support like input from keyboard or mouse can simply be implemented by reimplementing one function. As an added bonus, your 3D applications will run on pretty much any platform.

First chapter: setting up an OpenGL window, polygons, colors, rotation, 3D shapes

After completing the first chapter, you will end up with a rotating pyramid and cube…

This video shows the end result (lesson 5).
You can download the Qt 4 source code for this chapter here.

Second chapter: texture mapping, texture filters, lighting, keyboard control, blending, moving bitmaps in 3D space, loading and moving through a 3D world

In the second chapter you will learn a lot about textures, among some other things. Sometimes we make use of QGLContext::bindTexture() to load an image, morph it into a texture and bind it to OpenGL all in one go. Other times we will do it manually because we want to specify the texture filter ourselves (but we do have QGLWidget::convertToGLFormat() which makes that easy as well). To handle keypresses we simply reimplement keyPressEvent(). To load in the 3D world from the file we make use of the excellent QFile and QTextStream classes. As an added bonus, we also make use of QGLWidget::renderText() to show the user what has changed when a key is pressed inside the OpenGL window. Lesson 9 also features a bonus fade-in effect for the revived stars. This makes the effect look much cooler.

This video shows the result of adding texture filters, lighting and keyboard control (lesson 7)
This video shows the result of all the above plus blending (lesson 8)
This video shows the result of moving bitmaps in 3D space (lesson 9)
This video shows the result of moving through a very simple 3D world (lesson 10)
You can download the Qt 4 source code for this chapter here.

The third and fourth chapter are almost complete as well, but I will give you the Qt ports of those chapters another time. Perhaps tomorrow.

VN:F [1.6.3_896]
Rating: +6 (from 6 votes)
Categories: C++, OpenGL, Programming, Qt

Open source DJ mixxx’ing

January 6th, 2008 Wesley 3 comments

Sinds vorige week ben ik begonnen met mee te helpen aan de ontwikkeling van het open source DJ programma mixxx. Mixxx is een stabiel programma waarmee men live muziek kan mixen. Het heeft een aantal zeer interessante features, zoals bijvoorbeeld automatische ritmedetectie en ondersteuning voor een heleboel hardware.

Mixxx 1.6.0 with Collusion/WS/Green skin

Waar staan we vandaag? 1.6.0

Ik werk mee aan de nieuwe versie waarvan twee weken geleden een eerste bètaversie werd gelanceerd. Voor de nieuwe 1.6.0 versie zijn een heleboel nieuwe features gepland. Ik som even de belangrijkste veranderingen op:

  • Scratchen via timecoded vinyl-platen [ link naar flash video 1, video 2 ]
  • Kleurenschema’s voor skins
  • Nieuwe muziekbibliotheek (muziekbrowser)
  • Verbeterde ritmedetectie
  • Verbeterde ondersteuning voor MIDI-controllers (hardware)
  • HQ-equalizer toegevoegd
  • Audio core herschreven/vernieuwd
  • Ondersteuning voor LADSPA geluidseffecten
  • Live broadcasten over internet (Icecast, Shoutcast)
  • Rechtstreeks opnemen naar MP3, Ogg Vorbis, Wav, Flac

Ik werk momenteel aan de laatste twee punten. Het is niet zeker of het helemaal af zal raken voor de Hardy freeze in februari (het moment waarop programma’s naar Ubuntu 8.04 Hardy Heron geupload worden en niet meer aangepast mogen worden) maar Ogg Vorbis Icecast/Shoutcast-ondersteuning is zo goed als af, dus dat zal er waarschijnlijk zeker inzitten.

Zelf kijk ik uit naar de ondersteuning voor LADSPA geluidseffecten, maar ik vermoed dat dat niet af zal raken voor de 1.6.0-versie. Dan maar wat langer wachten…

Evolutie van de broadcasting code

Mixxx Live Broadcasting Preferences

Zoals eerder gezegd ben ik momenteel bezig met het implementeren van Icecast/Shoutcast ondersteuning zodat we een mix rechtstreeks over internet kunnen broadcasten. Aanvankelijk dachten we dat het simpel zou zijn om dit systeem te implementeren (gewoon libshout gebruiken) maar al snel bleek dat we eerst nog een encoder moesten schrijven. Ik ben de laatste dagen dus vooral bezig geweest met het schrijven van een Ogg Vorbis-encoder met behulp van libvorbis, libogg en libvorbisenc.

Afgezien van het feit dat er voor libvorbis geen technische API-documentatie beschikbaar is, is het ons uiteindelijk toch gelukt om de encoder werkend te krijgen. Alles ging goed, maar de audio latency was nu wel verhoogd omdat de ‘audio callback thread’ voor een korte periode geblokkeerd werd wanneer de encoder zijn werk deed. Toen we ook nog beseften dat we om metadata te updaten een nieuwe stream moesten initialiseren was het onvermijdelijk om een nieuw systeem te ontwerpen om de encoder parallel in een aparte thread te laten draaien (multithreaded) met een eigen buffer.

Albert ging hiermee aan de slag en na twee of drie dagen knoeien presenteerde hij een nieuwe ‘engine’ (de SideChain-engine) aan ons die de audio buffert voor andere engines (zoals de broadcast engine) en deze engines in een aparte thread draait. Het resultaat is lage audio latency en geen enkel performanceprobleem meer. Ook werd het totale CPU-verbruik verlaagd omdat de encoder nu iets minder vaak aangeroepen wordt.

Om het schematisch voor te stellen:

voor: [ afbeelding: Oorspronkelijke Broadcast Implementatie ]
na:
[ afbeelding: Threaded Broadcast Implementatie ]

Wat moet er nog gedaan worden? De SideChain-engine moet nog een klein beetje aangepast worden, er moet nog een MP3-encoder worden geschreven, het instellingenvenster moet afgemaakt worden, en tenslotte moet de code wat opgeschoond worden, en moet alles grondig getest worden!

VN:F [1.6.3_896]
Rating: 0 (from 0 votes)

SMPlayer Overlay Patches

December 9th, 2007 Wesley 2 comments

SMPlayer Time Overlay Patch

Ik ben bezig aan een patch voor SMPlayer voor mezelf:

  1. ik wil de huidige tijd kunnen zien zonder full-screen te verlaten (en zonder een klok te kopen) – zoals je kan zien op de bovenstaande screenshot heb ik dat reeds voor elkaar gekregen.
  2. ik wil de afspeeltijd van de film als een klein balkje op het scherm zien, niet via het tekstuele OSD, en ik wil eveneens de film kunnen doorspoelen door op een willekeurige plaats op mijn mooie alpha-transparante balk te klikken… want dat is veel mooier dan zo’n uitschuivend balkje dat je video naar boven duwt :)

Technische details:

  • mplayer gepatched en gecompileerd met vf_overlay patch
  • QPainter (van Qt 4.3) tekent de klok en tekst
  • getekende bitmap wordt vertaald naar het benodigde formaat en naar een gedeelde geheugenplek geschreven
  • mplayer (vf_overlay) leest de bitmap uit en legt hem over de video heen
  • mediaspeler waar de patch in wordt ontwikkeld is SMPlayer (mplayer frontend)

Ik denk niet dat ik deze patch upstream zal opsturen, tenzij er veel vraag naar is. De reden daarvoor is simpelweg dat je een onofficiële mplayer patch nodig hebt (vf_overlay patch) om van deze patch gebruik te maken, en (ik maak een gok) 95% van de smplayer-gebruikers zijn dus sowieso niets met deze patch.

Ik ben wel van plan om de patch op het forum van SMPlayer te plaatsen voor degenen die het eens willen bekijken of zelf willen proberen, maar de patch is momenteel nog niet in een “releasebare” status.

VN:F [1.6.3_896]
Rating: 0 (from 0 votes)
Categories: C++, Linux, Multimedia, Programming, Qt