C++ Redirect output to a printer or PDF

Please, read the updated version here: http://www.matejtomcik.com/Public/Projects/CPPPrinterStream

I have been wondering quite a long time if there is a way how one can redirect C++ cout to a printer. STD does not support such a method so if you need this behavior, you have to create your own output stream just like I did. And I want to share my source code with you, so start by downloading printerstream class.

This small handy template class is an extension of a standard output stream (std::basic_ostream) class which provides the functionality to output whatever you want to a printer instead of console window. To make it work, first download the printerstream class. Unzip and add printerstream.h and printerstream.cpp to your project. Note that printerstream is implemented for a Windows platform only. Then you can create an instance of the printerstream class in several ways:

  • printerstream<char> output(bool useDefaultPrinter = false);
    will create an instance of the printerstream asking user to choose the printer using standard print dialog. First parameter to this constructor is omited, but if you set it to true, the default printer will be choosen instead of showing print dialog.
  • printerstream<char> output(“PDFCreator”);
    will initialize printerstream using the printer specified by its name. To enumerate local printers on your PC call printerstream<char>::enumerate_printers.
  • printerstream<char> output(handle to a drawing context);
    will initialize printerstream using an already open handle to a drawing context (HDC). You can create a HDC using CreateDC function. Thats how the previous constructor creates a handle to the printer.

Once the printerstream object is initialized you have to call begin(const char * documentName = 0) method to start a new document. Without this call the printerstream will ignore any output written to it. This method basically creates a new print job, so if you need to split your report to multiple documents (PDFs for example), call begin everytime you wish to start a new print job. Specifying a document name is optional.

To explicitly finish the print job, call end method. To abort the print job, call abort method. If a previous call to a begin method was made, printerstream will implicitly call end in its destructor finishing the print job and sending it to the printer. It is also possible to set the page margins calling margins method.
REMEMBER: printerstream class translates page size into pixels, so to set margins to a 5% of the document size, you can use this method:

printer.margins(page_margins((printer.page_width() / 100) * 5));

Click “Read more” for an example code.
Čítať ďalej

Reklamy

Interface Flight Gear using .Net and C#

Please, read the updated version here: http://www.matejtomcik.com/Public/Projects/FlightGear

This topic extends the previous post “FlightGear Simulator”. Here, you will learn how to write a C# application to interface Flight Gear simulator. The method is very simple.

As I described in the previous post, Flight Gear can communicate with the outside world using TCP or UDP sockets. To create your application that will intercept the output from Flight Gear, you have to:

  1. Create a protocol definition file to tell Flight Gear that we want it to output it’s internal variables or accept variables to alter the simulation. Such a file looks like this:

    <?xml version="1.0"?>
    <PropertyList>
      <generic>
        <output>
          <line_separator>#</line_separator>
          <var_separator>;</var_separator>
          <chunk>
            <name>altitude-ft</name>
            <type>float</type>
            <format>%f</format>
            <node>/position/altitude-ft</node>
          </chunk>
        </output>
      </generic>
    </PropertyList>

    The output datagram will contain only one variable, which is the altitude in feets. If you add more variables, they will be separated by a semicolon and the datagram will end with # character. For a complete list of available variables to input/output see the Property Tree/Reference. Then you place your xml file in data\Protocol folder.

  2. Create a UDP or TCP server on a localhost and start receiving datagrams. This can be achieved easily with the System.Net.Sockets.UdpClient class. Just choose a port number.
  3. Start the Flight Gear with additional parameters telling it to work with your protocol definition file and do variables input/output from/to the outside world. Such a command looks like this:
    fgfs.exe –fg-root=”..\..\data” –generic=socket,out,10,localhost,1234,udp,myprotocol
    Explanation:

    1. fg-root sets the root directory, this should be the same in most of the installation
    2. generic creates a pipe between Flight Gear and an outside world application. In this example, we told Flight Gear to create a socket output pipe, firing new datagrams 10 times per seconds to an UDP server at localhost port 1234 using myprotocol.xml file definition.
  4. Process the received datagrams according to the protocol definition file.
    Depending on the rate of incoming datagrams you may face a problem if a previously received datagram is not yet processed but another one is being received. So creating a non-blocking receiver is essential.

Čítať ďalej

How to extract images from Call of Duty – UPDATE !

Please, read the updated version here: http://www.matejtomcik.com/Public/Projects/IWIExtractor

With IWI Extractor you will be able to extract images from Call of Duty and convert images into PND,DDS (DirectDraw Surface),JPEG or BMP. This application will also allow you to convert IWI images to different version (currently supported versions: 5,6,8)

Download IWI Extractor first then install it. You will need .Net framework 2.0 already installed on your PC (note that .Net 2.0 is very common, you may already have this package installed). You may see a warning concerning about iwiextractor_setup.exe, that this application is not commonly downloaded and may harm your computer. Well, you will be one of the few people who downloaded it so it’s not a very common application.

Run IWI Extractor, you will see this window:

You can open IWI files (IWI images) or IWD archives. On the left side you can see a list of opened files. The right side serves as a preview of currently selected IWI image. If you choose to open IWD/IWI files from folder, you will get an option as seen on the second picture. You have to either select a folder to search for IWD/IWI files in or you can select a game to search in. Any Call of Duty game that has been properly installed on your computer should be visible in the selection box.

IWI Extractor will then search in the selected folder looking for either IWI or IWD files. Note that this operation can take a long time if you left the “Search recursively” option checked and you selected a folder with too many subfolders, like the whole Program Files folder or C:\ root, it can take a very long time. Then a window (see picture #3) will open to let you choose which files you want to open. It will list all IWD/IWI files it found.

Once you open files you can click on them, this will generate a preview in the right panel (see picture #4). In the bottom corner there’s an information about image version and type.

In the bottom left corner there is a filter and it works only if you have all the IWD archives in the tree expanded, it’s a bug…

To extract and convert IWI images, check images in the left panel you wish to convert, then click Convert in the menu and select output format (PNG, JPEG, BMP, DDS). A window will open with options for the extraction (see picture #5).

To output images into a folder on your PC, select “Directory” and choose a folder. To put all converted images into a single ZIP file, select “Zip” and choose output filename. To create a separate ZIP file for every IWD archive, check the “Create separate ZIP for every IWD archive” option and choose a folder to place these ZIP files to. Then you can specify whether to keep full file paths (images inside IWD archives are stored under images/ path). If you are converting to JPEG format, you can set the JPEG quality (note that JPEG and BMP will loose transparency).

If you are looking for a command line tool, you can download an older version. You can download it here. This command line tool requires Microsoft Visual C++ 2010 Redistributable Package (x86) (You may already have this package installed (check in Control Panel)).

FlightGear Simulator

Have you ever wanted to control an aircraft in a simulated environment? For example, you have a hobby in making your own RC planes and you would like to program these planes to fly autonomously but you don’t have a chance to test your program. Well, my application FGSim together with FlightGear give you the opportunity to actually test your solution before you put it into the real RC plane.

Čítať ďalej

IWI documentation

Download PDF documentation

You can download the PDF by clicking on the image or this link.

– IWI documentation for IWI files version 8 (Modern Warfare 2, Black Ops, Modern Warfare 3)

– Header structure

– Every header field is described (except for the Unknown fields, note that these fields are specific for the editor and do not affect the IWI file itself)

– Example values and the DXT1 decoder sample (uses external squish for decompressing DXT blocks)

– I will also provide C++ code that you can use to work with IWI files (load multiple formats and compile the directly to the IWI file), below is an example how to load an image and convert it to the IWI file.

Čítať ďalej

How to open IWI files

Please, read the updated version here: http://www.matejtomcik.com/Public/Projects/IWIExtractor

I made a simple application that is able to open IWI files. The IWI file type is used in Call of Duty series to store textures and images. So if you need to view and edit textures, you can use this tool. But be careful, current release supports only IWI version 8 files (Call of Duty Modern Warfare 2) and check twice before you replace any of the textures in game (and make a backup!). I didn’t have time to figure out some IWI specifications, so you have to find it out yourself (when you open an IWI file, right click on the file then select “Edit header”, you’ll see fields from Unknown 1 to Unknown 6, these may be some attributes used ingame or in “level designer”, I don’t know). So if you would like to replace a texture, be sure to copy these “Unknown” attributes to the new file and once again, make a backup of replaced files or the whole IWD archive! or don’t replace any files at all, because I don’t take any responsibility if you mess up your game!

IWI Viewer requires Microsoft Visual C++ 2010 Redistributable Package (x86) (You may already have this package installed (check in Control Panel))

By clicking on “Download” link you agree that the author, Matej Tomcik, is not responsible for any damage that can be caused by using this application in proper or unproper way.

Download

PS: If you need to open IWI files which are different than version 8, let me know (leave me a comment or send me a message) and I will try to implement the functionality for the version you need or make an utility to convert IWI to DDS, BMP, PNG, whatever… (it’s pretty easy)

How to analyze textures

I’ve recenlty made another tool for working with textures. This time it’s an application that is able to load textures (or images) from any kind of a file. Basically you provide offset in the file, width and height of the image and decoding algorithm (DXT1/3/5, RGB, ARGB, A, AG …). The application will then try to allocate appropriate bitmap and decodes file’s content into the bitmap using selected decoder.

texture_analyzer_1

This may be very useless for an ordinary user, but if you want to analyze files and see if they contain any images, it can become very handy. Just imagine my situation a few months ago when I was about to create an application that could load and display Call of Duty’s IWI files. Before I could start doing anything, I had to know the structure of the IWI files. So I was searching the internet for some specifications, but found nothing. So I just had to open the IWI files in a HEX editor and do a detective job to find out what an ordinary IWI file consists of. Thats why I developed predecessor of this texture analyzer, it was called DXTViewer (I think it ships with IWI Viewer files).

Čítať ďalej

C++ & MySQL

Na začiatok je potrebné podotknúť, že v dnešnej dobe, v ktorej je komplikovaný ako samotný návrh, tak aj tvorba aplikácií, je využívanie uý navrhnutých a stabilných systémov veľkou výhodou. MySQL pozná skoro každý programátor, ktorý pracuje či už na vývoji alebo správe webovej stránky. V mnohých prípadoch je potrebné takéto technológie používať aj vo vývoji desktopových aplikácií. Rád by som teraz osvetlil základné kroky k implementácii MySQL do vášho projektu.
Čítať ďalej

Surové (raw) pakety

Skoro každý, kto začínal programovať sieťové aplikácie, sa stretol s WinSock. WinSock je sada hneď niekoľkých vrstiev protokolov, ktoré zjednodušujú prácu so sieťou, používateľ nie je tým pádom nútený napr. spravovať TCP komunikáciu, ktorá sa nepriamo podieľa na prenose aplikačných dát. Taktiež nie ste nútený vedieť, aké sieťové karty máte nainštalované, ich MAC adresy, MAC adresy iných zariadení na sieti a pod. WinSock túto prácu robí za nás. No nie je to zadarmo. „Koho chlieb jete, toho pesničku spievate.“ Týmto mám na mysli obmedzenia, ktoré sú na WinSock aplikované novými verziami Windows alebo záplatami. Na Win XP SP2 a novších verziách máte napr. zakázané posielať nami upravené TCP dáta, čo je pochopiteľné. Taktiež pochopiteľné je znemožnenie používateľovi zmeniť zdrojovú adresu UDP paketu. Tento prípad sa však teraz pokúsime obísť, a to použitím WinPcap (www.winpcap.org), ktorý priamo komunikuje so sieťovou kartou, teda všetky vaše dáta, či už majú zmysel zo sieťového hľadiska alebo nie, sú spracované.

Čítať ďalej

Najbezpečnejšia banka na Slovensku: VÚB

New York, prestížny americký časopis Global Finance určil za najbezpečnejšiu banku v stredoeurópskom a východoeurópskom regióne slovenskú Všeobecnú úverovú banku. Do prvej desiatky tohto rebríčku sa nedostali žiadne iné slovenské banky.

vub

Táto správa ma veľmi milo potešila, pretože vo VÚB mám účet už niekoľko rokov, za tú dobu som nemal ani raz problémy či už s riešením problémov alebo s ústretovosťou pracovníkov. Z VÚB by si mali zobrať príklad aj ostatné banky na Slovensku, v ktorých mnohokrát cena nezodpovedá kvalite a pracovníci sa chovajú ako keby boli vyučení pracovať s dobytkom a nie s ľuďmi a hlavne zákazníkmi.

Pôvodný článok Global Finance

Viem aké to je…

 …byť slimákom v ulite, byť rybou v pustom oceáne, byť slobodný natoľko, že nevidíte hranice, byť človekom. Je fascinujúce, ako sa dokáže človek vnútorne zmeniť, preniesť svoju dušu na inú melódiu, do ticha, tam kde je vidno iba nádej. Viem aké to je byť medzi pomyselnými múrmi, ktoré vás tlačia čoraz viac a viac, až nemôžete dýchať, aké je to byť väzňom svedomia. Každé ráno vstávam plný optimizmu, večer skláňam svoju hlavu zatienenú svetom okolo mňa, svetom, ktorý ma ubíja. Chodím po žeravom uhlí, nevidím koniec, no mne je to už jedno, koniec je všade okolo mňa, záleží len na tom, ako sa na to pozriem. Strácam sa ako kvapka v mori, splývam, nemôžem tomu zabrániť. Umieram…

10 Korún Československých

Ako som si tak prezeral nemenovaný časopis, povšimol som si, že v ich ankete ohodnotili bankovku 10 KČs ako za najkrajšiu, ktorú sme počas éry korún mali. Trošku som sa doma poobzeral a veru že som jednu našiel. Vyzerá tak historicky, perla dávnej minulosti v súčastnej dobe Eura. 

Potreboval by som však Vašu pomoc. Neviem, ako sa o túto nádheru postarať, aby sme sa z nej mohli tešiť ešte pekných pár rokov. Ak by ste mi vedeli poradiť, poprípade ste numizmatik alebo nejakého poznáte, prosím Vás, ozvite sa mi. Kontakt nájdete v sekcii Môj profil. Ďakujem 😉