Blit transparent images with Allegro - c++

I'm wondering if there is any possibility to draw png with transparent colors, so I can make something half-transparent. I've read this but I can't get anything to work. I also heard about alpng library but I don't know if it's able to load transparency too.
I'm using allegro 4.2.1 with the newest Dev-C++.

You need to load a 32-bit image, call set_alpha_blender() and then draw_trans_sprite(). See this discussion. TGA is the only format that Allegro 4 supports natively that is sufficient for this task. Otherwise, you'll need to look into a PNG loader add-on.
You may also want to consider switching to Allegro 5 if you are just getting started. It has a much more modern API for this type of thing.

Related

Is there any way to load a .png from my resources without using GDI+?

I am trying to load a .png I stored in my projects resources, in order to set it in a picture control, but I can't quite figure out why. I did some research, and it seems like .png is not really supported with the usual LoadImage()-call.
However, I don't really want to convert it to a bitmap if I can get around it.
So far, I only found resources on how to do it with GDI+, or really ancient win32-api code.
Is there any way to load .png files natively by todays standard?
The "new" way to do it would be Direct2D and WIC, which is demonstrated in the Windows Imaging Component and Direct2D Image Viewer Win32 Sample.
But if the rest of your application is basic controls, Direct2D is overkill. The image has to be converted to a bitmap at some point to be displayed – whether in your GPU or in memory – and GDI+ fits this use case.
If these resources are icons or some other small-ish file (<2mp), I would reccomend embedding the resource as a bitmap. You can keep your asset pipeline as PNG, just add a pre-build step to convert your PNGs to pre-multiplied BGRA bitmaps and use LoadResource. There are pre-built tools to meet this need.

Embedding an OpenCV window into a Qt GUI

OpenCV recently upgraded its display window, when it is used in Qt.
It looks very good, however I did not find any possibility for it to be embedded into an existing Qt GUI window. The only possibility seems to be the creation of a cvNamedWindow or cv::namedWindow, but it creates a free-floating independent window.
Is there any possibility to create that OpenCV window inside an existing GUI? All I could find on the OpenCV forums is an unanswered question, somewhat similar to my own.
There is a straight-forward possibility to show an OpenCV image in Qt, but it has two major problems:
it involves copying the image pixel by pixel, and it's quite slow. It has function calls for every pixel! (in my test application, if I create a video out of the images, and display it in a cvNamedWindow, it runs very smoothly even for multiple videos the same time, but if I go through the IplImage --> QImage --> QPixmap --> QLabel route, it has severe lag even for one single video)
I can't use those nice new controls of the cvNamedWindow with it.
First of all, the image conversion is not as inefficient as you think. The 'function calls' per pixel at least in my code (one of the answers to the question you referenced) are inlined by optimized compilation.
Second, the code in highgui/imshow does the same. You have to get from the matrix to an ARGB image either way. The conversion QImage -> QPixmap is essentially nothing else than moving the data from main memory to GPU memory. That's also the reason why you cannot access the QPixmap data directly and have to go through QImage.
Third, it is several times faster if you use a QGLWidget to draw the image, and I assume you have QT_OPENGL enabled in your OpenCV build. I use QPainter to draw the QPixmap within a QGLWidget, and speed is no issue. Here is example code:
http://sourceforge.net/p/gerbil/svn/19/tree/gerbil-gui/scaledview.h
http://sourceforge.net/p/gerbil/svn/19/tree/gerbil-gui/scaledview.cpp
Now to your original question: Your current option is to take the code from OpenCV, include into your project under a different namespace, and alter it to fit your needs. Apart from that you have no alternative right now. OpenCV's highgui uses its own event loop, connection to the X server etc. and it is nothing you can intercept.
My first guess is to want to say this: I'm sure that if you dig into the code for namedWindow, you will find that they use some sort of standard, albeit not oft-referenced, object for painting said window (that's in the openCV code). If you were ambitious enough, you could extend this class yourself, to interface directly to a frame or custom widget in Qt. There might even be a way to take the entire window and embed it, using a similar method of a Qt frame, or an extension of the (general) widget class. This is a very interesting question and relates rather directly to work I've been doing of late, so I'll continue to think about and research it and see if I can't come up with something else more helpful.
[EDIT] What specific new controls are you so interested in? It might be more efficient on the part of the programmer to extend a Qt control to emulate that, as opposed to my first suggestion.[/EDIT]
simply check out the opencv highgui implementation. as i recall it uses qt.

How to load a bmp without background

So, I was trying to make game in allegro but I'm currently stuck with this damn blank background which is making me very mad, as I know PNG images have transparency in the background already, but I can't load pngs, i have already download devpaks, installed libraries and stilll nothing good happened, if the best option for me is to use PNG so please tell me how to load then and use then correctly.
If the best option is still to use BMP and there is a algorithm, function or a little code which will make the blank background go away please tell me.
For those who didn't understand what I want there is a better explanation:
http://3.bp.blogspot.com/-r9BaUuMLirc/ThjzRHOMBKI/AAAAAAAAAJI/kUilPnIPJLg/s400/bola_azul.png
its currently in .png, but I transformed to .bmp in paint, so it makes me a blank background and in allegro it shows the whole picture, i want to have only the ball.
As you've commented, with Allegro 4, the color 0xFF00FF is treated as transparent when used with masked_blit() or draw_sprite().
To load PNGs in Allegro 4, you'll want to use loadpng with libpng. You can use the 8-bit alpha channel by enabling the alpha blender with set_alpha_blender().
If you're just starting out, you should be using Allegro 5, which has a modern API and native support for PNG files.

Firebreath placing JPEG image inside the plugin window

I'm writing a plugin in firebreath, C++.
I don't have any experience with both, so my question may be very basic.
How do I place a JPEG image inside my plugin window?
Or at least, how do I do it in C++ simple program?
Thanks,
RRR
There are a couple of other questions that may help you better understand this:
How to write a web browser plugin for IE, Firefox and Chrome
Directx control in browser plugin
Basically you'll get a drawing model from FireBreath with the AttachedEvent. Depending on your platform, you will draw to that window using platform-specific drawing APIs. On Windows, for example, you would get the HWND from the PluginWindow (cast it to a PluginWindowWin) and then draw to that. Just make sure you stop drawing when DetachedEvent shows up.
For more information, you'll need to be a lot more specific; but follow those links and do some reading, then you'll know better what questions to ask.
FireBreath 1.5.2 was just released, btw! Good luck!
Good luck!
You can also use OpenGL to display images in plugin. You can get several tutorials to load jpeg image in OpenGL as texture. Same code can be ported into the Firebreath plugin using the already given OpenGL sample plugin for windows. Though OpenGL context creation will vary from one OS to the other. If you want to load jpeg images from web, you'll have to download image before converting it into opengl texture.

Render a vector graphic (.svg) in C++

My and a friend are working on a 2D game where the graphics will be .svg files and we will scale them appropriately either by rasterizing them first, or rendering them directly on a surface (which still would require rasterization at some point).
The problem is, I've been looking all day to find a library that will allow me to take an .svg file and eventually get it to render in allegro. As far as I know, it would involve rasterization into some sort of format that allegro can read and then allegro could render the "flattened" image.
So what are some C++ libraries I could use for taking an .SVG file and "flattening" it so I can render it? The library obviously needs to support scaling too so I can scale the vector graphic then rasterize it.
I'm using Windows and Visual C++ Express 2010.
I've tried Cairo, but it only allows writing of .svg files and doesn't allow you to read the .svg file. I've also looked into librsvg which works with Cario, but I was having a lot of trouble getting it to work properly on Windows (because it has loads of GNOME dependencies). If you have any guides for getting these to work (on Windows) that would be great too.
The wxsvg library allows loading and manipulating SVG files. Qt also has an SVG module.
I'm coming a little late to the conversation, but I would suggest you to look at Nano SVG, an extremely lightweight svg renderer that doesn't need cairo/libsvg. I got nanosvg compiled and working in a couple of hours. It's very basic, but it gets the job done.
https://github.com/sammycage/lunasvg is a nice svg parsing, rendering and manipulating library. It is written with pure c++
SVG++ library provides advanced support for SVG reading, so that rendering SVG with allegro can be implemented in reasonable time.
I have recently put together an SVG renderer library in C++:
https://github.com/igagis/svgren
It uses AGG for rendering to off-screen surface.
Supports gradients and all kind of shapes.
Personally, I using NanoSVG in my Simple Viewer GL. It's allowing me easy to load and rasterize SVG images in few lines of code. But this library has weak SVG support.
With the help of nanosvg and many other c++ svg parsers, adding svg rendering capability to your application should be trivial. The recipe is as follows: svg parser + vector rendering library = trivial svg rendering. The vector rendering library can be cairo and a number of other libraries (nanovg comes to mind, as well as a number of other vg libraries). Here's an example of how to support svg rendering with cairo + fltk + nanosvg combo. Now, all the svg parsers, as well as cairo itself, along with other renderers, have bugs/shortcomings, but basic svg support should never present a problem.
I was looking for a real quick way to render SVG file in a Windows OS based MFC project.
In that case, Microsoft Browser Web Browser ActiveX Control is found to be an ideal solution.
And here is the result of loading SVG file using the Browser control.