Archive

Archive for the ‘Programming’ 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: +5 (from 5 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: +2 (from 2 votes)
Categories: C, C++, Open Source, Programming, Qt, School

Overriding dynamic library calls (function interposition)

August 18th, 2009 Wesley 7 comments

About function interposition

I was wondering how I could override dynamic library calls in Linux, and I came across this technique known as function interposition. It is a powerful technique that allows you to override dynamic library calls. It might sound dull, but it can be very, very useful. There are some memory trace tools that make use of this technique to work, but perhaps a cooler example is the OpenGL capture system which was created by nullkey: it can capture OpenGL frames by overriding certain OpenGL functions. Another example are cheat tools (wallhacks, aimbots) which also make use of this technique a lot.

Some background

While Googling (did I spell that right?) I came across this recent blog article which explains the background very well. I will quote it here:

First, some background. When a program that uses dynamic libraries is compiled, a list of undefined symbols is included in the binary, along with a list of libraries the program is linked with. There is no correspondence between the symbols and the libraries; the two lists just tell the loader which libraries to load and which symbols need to be resolved. At runtime, each symbol is resolved using the first library that provides it. This means that if we can get a library containing our wrapper functions to load before other libraries, the undefined symbols in the program will be resolved to our wrappers instead of the real functions.

So if we create a custom shared library which overrides some of the functions of the original library, our functions will be called instead of those of the original library.

How to do it

  • Write new functions which override existing functions
  • Compile the written code to a dynamic library that is linked to the dynamic linking interface library
  • Use the LD_PRELOAD environment variable when running an application to preload your custom library before all other dynamic libraries

The article by Jay Conrod has an example which shows you the basic implementation of a simple memory allocation tracer.

I have also cooked up an example myself. Because I’ve been busy learning more about OpenGL, I thought that it shouldn’t be too hard to create a wallhack for one of my favourite games: Soldier of Fortune 2 running in Wine. Just for testing purposes of course! I am no cheater :D It turned out to be relatively simple, although my first tries weren’t so very successful:

  • Epic fail – At least I know my library is used now.
  • Partial success – Disabling depth testing completely was only a partial success.
  • Success – A debugger can tell that players are drawn using glDrawElements(). By knowing the number of elements for each character, we can disable depth testing selectively.
Soldier of Fortune 2 wallhack example

Soldier of Fortune 2 wallhack example

For those of you who are interested in the code:

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
/*
    Simple wallhack example for Soldier of Fortune 2 (Wine) in Linux using function interposition

    This code snippet was written by Wesley Stessens (wesley@ubuntu.com)
    It is released in the Public Domain.

    Compilation: gcc -Wall -ansi -pedantic -shared -ldl -fPIC glhack.c -o glhack.so
    Usage: LD_PRELOAD=glhack.so wine game.exe
*/


#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <stdint.h>
#include <GL/gl.h>

/* Override the glDrawElements function */
GLAPI void GLAPIENTRY glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices) {
    /* Store the actual function in a static function pointer */
    static void (*glDrawElements_)(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices) = NULL;
    if (!glDrawElements_) {
        glDrawElements_ = (void(*)())(intptr_t)dlsym(RTLD_NEXT, "glDrawElements");
        puts("GLHack: glDrawElements call has been overridden");
    }

    /* Disable depth testing if the number of elements to draw is one of the following, which means a player is being drawn */
    /* To avoid abuse of this code by cheaters, I have changed all count constants below to VALUEX */
    if (count == VALUE1 || count == VALUE2 || count == VALUE3 || count == VALUE4)
        glDisable(GL_DEPTH_TEST);
    else
        glEnable(GL_DEPTH_TEST);
    glDrawElements_(mode, count, type, indices);
}

Interesting thought about multiplayer cheats and Wine

If anti-cheat tools would perform a sanity check of the OpenGL or DirectX DLL, they would only find the virtual DLL’s when a game is run in Wine, right? I’m wondering whether this sort of cheats can be made undetectable then. In a way I hope not, because cheaters are very annoying when you’re playing a game, but on the other hand, it would be an amazing technological achievement. Anyway, anti-cheat tools like PunkBuster don’t even work with Wine at the moment, so it might be a non-issue. What are your thoughts?

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

Code snippets

August 12th, 2009 Wesley No comments

I’ve created a new page where I will upload small code snippets that I created and which might be useful for some. Most code snippets are released in the Public Domain. The code snippets might not have the highest quality, so feel free to propose improvements to them if you feel like it.

PS: I have moved the “safe replacement for gets” blogpost to the code snippets page and have moved over all the comments as well.

VN:F [1.6.3_896]
Rating: +3 (from 3 votes)
Categories: Blog, Programming

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: +12 (from 12 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: +11 (from 11 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: +10 (from 10 votes)
Categories: C++, OpenGL, Programming, Qt