Wednesday, June 12, 2019

Disable qDebug in a single source

You can disable all qDebug() calls in a single source file by using this little macro:
#undef qDebug
#define qDebug QNoDebug

Tuesday, February 7, 2017

Run Qt application as administrator

If you are trying to run your Qt application with administrator right on Windows you need to do some steps:

1. In your .pro file remove the default manifest generated by Qt because you will get duplicate entry error in linker so do this:
CONFIG -= embed_manifest_exe
2. Create a resource file(.rc) and add it to the .pro file:
win32
{
    RC_FILE = App.rc
}
3. Prepare your resource file:
#include <windows.h>

ID_ICON ICON DISCARDABLE "AppIcon.ico"
CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "App.exe.manifest"
4. Prepare your manifest file (App.exe.manifest):

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <assemblyIdentity version="2.0.0.0" processorArchitecture="X86"
  name="my.App" type="win32" />
  <description>My application description </description>
  <dependency />
  <!-- Identify the application security requirements. -->
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel
          level="requireAdministrator"
          uiAccess="false"/>
        </requestedPrivileges>
       </security>
   </trustInfo>
   <!-- padding to four-byte multiple file size including the byte order mark -->
  <!-- padding 123 -->
</assembly>

Done. Qt 5.7, Windows 7, MSVC2015.

Sunday, February 28, 2016

Unity MovieTexture


Using QuickTime to convert mp4 video files to MovieTexture has some problems in Unity, some times Unity could not detect QuickTime also if you have bunch of videos all team members need to install QuickTime and wait for a long import time but converting videos manually to OGV will resolve these issues.

Using FFmpeg and a simple batch file you can convert files easily:



for %%f in (*.mp4) do  ffmpeg -i %%f -vcodec libtheora -q:v 7 
-acodec libvorbis -ac 2 -y %%~nf.ogv



It loops through all mp4 files and execute ffmpeg, with these paramerters:

-vcodec libtheora  means the video codec is Theora
-q:v 7 means the video quality is 7 from [0-10] range
-acodec libvorbis  means audio codec is Vorbis
-ac 2 means audio channels is 2
-y means overwrite existing ogv file

Saturday, April 26, 2014

Selfish coder!

Don't be selfish!, writing code merely because this is your task and you have to finish it, is not fair, try to think of next programmer that will read your code, in most cases there will be next coder, how he will read your code, don't let your friend alone inside the mess, try not to be a selfish coder, try to be a professional, then learn how to write clean and professional codes.

Thursday, January 3, 2013

C++ Plugin Debug Log

When I was writing a C++ plugin for Unity, I was wondering that it will be very good if I could write some debug logs into the Unity console, after going on and progressing the plugin without logs, suddenly an idea came to my mind! This is an explanation of how to redirect some debug text from a C++ plugin to Unity's console.

The method is based on Delegates in C# that it can be treated as a typedef of a function pointers in C/C++ and you can call them from unmanaged code.

First of all define a delegate like this:

using System.Runtime.InteropServices;
...
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void MyDelegate(string str);
...

Take care of your calling conventions, by default calling convention of a C++ project in Visual Studio is __cdecl (/Gd) you can find it in the project properties -> C/C++ -> Advanced options.
This is a delegate of type a function that has a string argument and returns nothing.

Then write a method that you will call it from C++ to print the log string, this is a member function of a Unity script file:

static void CallBackFunction(string str)
{
    Debug.Log("::CallBaaaaaaack : " + str);
}

In the Start() function of your Unity script instantiate the defied delegate :

MyDelegate callback_delegate = new MyDelegate( CallBackFunction );

// Convert callback_delegate into a function pointer that can be
// used in unmanaged code.
IntPtr intptr_delegate = 
    Marshal.GetFunctionPointerForDelegate(callback_delegate);
 
// Call the API passing along the function pointer.
SetDebugFunction( intptr_delegate );

The SetDebugFunction is a method that assign the function pointer to a pointer that you defined in your C++ code :

typedef void (*FuncPtr)( const char * );

FuncPtr Debug;

You can access this pointer in other source codes by extern modifier or any global access method you know such as writing a singleton classes.

extern "C"
{
    EXPORT_API void SetDebugFunction( FuncPtr fp )
    {
        Debug = fp;
    }
}

Don't forget to import it in your C# code :

[DllImport ("UnityPlugin")]
public static extern void SetDebugFunction( IntPtr fp );

Now you can call this function every where in your C++ plugin :

...
Debug( "String From C++ Dll" );
...

And the resault :

Thursday, December 20, 2012

Will share some code

I added syntax highlighting to the blog with the help of SyntaxHighlighter, maybe I can share some codes and experiences with you.

#include <iostream>

int main()
{
    std::cout << "Hello World!" << std::endl;
    return 0;
}

Wednesday, June 13, 2012

Did he understand?

.
.
.
Human : Did you understand?
Machine : Yes I did.
Human : No, you didn't understand?
Machine : Why?
Human : Because you are a silly machine.
Machine : You made me sad.
.
!