Assembly in C++ programs

Foreword

Writing code in assembly language in 2015 seems stupid and meaningless. Yet, it has a few huge benefits:

  1. understanding how computers / compilers / programs work
  2. hardcore optimizations for performance-critical applications
  3. just for fun!

Well, in real life, I’ve never met conditions of such performance requirements when I should be writing some parts of application in ASM. Except, maybe, a handful of ACM ICPC problems.

Those are two merely big benefits to write in assembly. Thus, if you are not getting fun out of coding, you may not be interested in this blog.

This blog is mostly for academical purposes. People who study something like low-level programming at the university may be interested.

Read more

ShootThem!

A few days ago I’ve found my old game, ShootThem! sources.

Half of hour being trying to run it, and viola!

I have hosted sources with all the dependencies on GitHub.

One has no story (in-game, but has one written before the game presentation on a programming competition for students in Dnepropetrovsk). It has (almost) no colors. No code structure or architecture. No animations. 

It is really plain.

Yet, it was and still it is a starting point for me 😁

Here are some of game screenshots:

Running Qt + MySQL application

Yesterday I tried to run my university project made with Qt and MySQL. But all I got was strange error message, saying QMYSQL driver is not loaded whilst loaded driver list actually included that one.

Searching all over the internet up ‘til 2 AM and recompiling the whole Qt gave no result except time being wasted. Yeah, and 90% of search results were tutorials on how to recompile Qt MySQL plugin under Windows.

Yet in the morning I found solution and it was beautifully simple! I just performed one step from Deploying Qt applications, actually just copied the plugins/ directory to the dir where the application executable lives (build-debug/ or build-release/ for my project; I hate the default build-#{ProjectName}-Desktop_5_4_1-Debug/ paths); included the sqldrivers/ directory there (just copied) and created (a bit tuned although) the qt.conf file. That file just pointed the plugins path to the custom one:

[Paths]
Prefix=.
Plugins=./plugins

To sum everything up:

  1. create a plugins/ directory in your build directory
  2. copy Qt/5.4/Src/qtbase/plugins/platforms/ there
  3. copy Qt/5.4/gcc_64/plugins/sqldrivers/ there too
  4. create qt.conf file in build directory and fill it as mentioned above

And one more hint before the end: to debug why your Qt plugins fail use this application startup switch: QT_DEBUG_PLUGINS=1 ./app_name - this will display plugins debug information.

Tiny java code...

This one was super easy so I did a quick comparison of a couple of ways you can do it; the goal is to count all the alphanumeric (a-zA-Z0-9) characters in the text of a Sherlock Holmes book. There’s probably other ways do do it because there’s lots of ways to do literally anything, but the fastest one here is stripping out all non-alphanumeric characters (\W).

//*********************************************//
//                                             //
//        EASY DAILY PROGRAMMING TASK #19      //
//        ALPHANUMERIC CHARACTER COUNTING      //
//                                             //
//*********************************************//

public void count1(String text) {
    long start = System.currentTimeMillis();
    int count = 0;

    for (char c : text.toCharArray()) {
        if ((c + "").matches("[a-zA-Z0-9]")) {
            count++;
        }
    }

    long end = System.currentTimeMillis();
    System.out.println("Count 1: " + count + " Time taken: " + (end - start));
}

public void count2(String text) {
    long start = System.currentTimeMillis();
    int count1 = text.length();
    text = text.replaceAll("\\w", "");
    long end = System.currentTimeMillis();
    System.out.println("Count 2: " + (count1 - text.length()) + " Time taken: " + (end - start));
}

public void count3(String text) {
    long start = System.currentTimeMillis();
    text = text.replaceAll("\\W", "");
    long end = System.currentTimeMillis();
    System.out.println("Count 3: " + text.length() + " Time taken: " + (end - start));
}
Countiung all alphanumeric characters in Sherlock.txt:
Count 1: 432188 Time taken: 269
Count 2: 432188 Time taken: 53
Count 3: 432188 Time taken: 22

tinyjavacode:

The new hardcore

Trying some new hardcore - Assembly in Emacs 😁

How to adjust widget’ size in Qt

Have you ever thought on how this:

could be turned to this:

without manually setting per-pixel sizes?

It’s simple as 1-2! Just take a look at the top of the UI editor and you’ll find a set of buttons, dedicated to do this adjustment for you:

Yet, there is just one small tip: first you should select a containing widget, whose children will be rearranged.

Like this:

Note the selected parent widget for vertical_layout.

Decorator pattern in Python

Just found some interesting way of implementing Decorator design pattern in Python.

As Jason Smith said in his book (Elemental Design Patterns), “design patterns may be implemened in different ways in different programming languages”.

That’s said, design patterns are not some set of classes which will be implemented in a very similar way in different languages - they are just a way of doing something.

Thus, Decorator pattern is a way of wrapping some method’s or class’ behaviour. In Python it may be done with Context Managers:

from contextlib import contextmanager

@contextmanager
def tag(name):
    print "<%s>" % name,
    yield
    print "</%s>" % name,

with tag("h1"):
    print "moo",

print

with tag("div"):
    print "foo",

This code will end up wrapping print "foo" and print "moo" methods with printing some HTML tags around ‘em:

<h1>moo</h1>
<div>foo</div>

That is interesting as it implements Decorator design pattern in a bit hard-coded way, but using language features, not OOP ones.

Compare it to the “standard” OOP implementation in Python:

# -*-coding:utf-8 -*-
class SimpleText(object):
    def __init__(self, text):
        self.text = text

    def content(self):
        return self.text

    def __str__(self):
        return self.content()

class TagDecorator(SimpleText):
    def __init__(self, text, tag):
        super(TagDecorator, self).__init__(text)
        self.tag = tag

    def content(self):
        return '<{0}>{1}</{0}>'.format(self.tag, super(TagDecorator, self).content())

a = SimpleText('moo')
print('SimpleText: %s' % a)

b = TagDecorator('moo', 'h1')
print('TagDecorator (h1): %s' % b)

This one looks a bit… ugly… right? And though Python does not really care of which type are a and b, we may not need all this class hierarchy.

This is the might of context managers!

Modelling in Blender

How did I came over from that to this…

NB: this is a 3D model potentially to be used in my ShootThem! game. Inspired by the Chicken Hat from Fable:

Chicken Hat from Fable

Trying sculpting in Blender

Chicken body sculpt in Blender Chicken head in Blender

Have had some free time and tried sculpting in Blender 😜 With some other nice stuff 😁

creating some gun from a rough primitive model

Textured rifle made in Blender Rifle model with few material properties and a bit of sculpting

Creating some gun from a rough primitive model with some texture and sculpting. Yet, trying to get some pretty materials on this…