This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm create a game using system("cls") and printf.
I have a two-dimension array of character for the field.
I refresh the screen every 0.5 sec and display the whole field but the screen flashes badly.
Is there anyway I could make it smooth.
My OS is Windows.
Use ncurses (or pdcurses, might be easier to set up on Windows.) If you can't, then don't clear the screen, as that causes flicker. Instead, position the cursor to the top-left (0, 0) again and then draw. To position the cursor at 0, 0, you can do:
#include <windows.h>
// ...
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD position = {0, 0};
SetConsoleCursorPosition(handle, position);
Another alternative is to create a second screen buffer. You can write directly to this, as it's not visible. When you have a new frame ready, swap the visible buffer with this second buffer. This instantly displays the new frame, and you can now draw in the original buffer.
This technique is widely used and is known as double buffering.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Like many software applications today, I want my program to have a frameless window that still has a drop shadow to make it fit better with the OS. The first step was to set the window flags to Qt::FramelessWindowHint to remove the frame. But the trickier part is adding a drop shadow to the outside of the window. I opted to add a QGraphicsDropShadowEffect to a smaller base widget that holds everything and make QMainWindow transparent by setting the attribute Qt::WA_TranslucentBackground to true.
This seems to be a good solution for me, until I added a QSizeGrip to enable resizing of the window. Every time the window gets resized, a paint event is called and the QGraphicsDropShadowEffect needs to be recalculated. This results in SUPER choppy resizing and a big performance hit.
Are there any alternatives to QGraphicsDropShadowEffect that has better performance?
I came up with my own solution that I haven't seen on SO yet.
I ended up making 12 png images that when arranged in a set of layouts, gives the illusion of a drop shadow. I'm sure there are many ways to do this, but what I did was split each side into 3 parts. This accounts for many different ways the drop shadow could be set up (offset and corner rounding)
Here's an exaggerated diagram:
The red areas are QLabel's with pixmap icons aligned to the outer corners. The purple areas are QLabel's with repeating background images. The red areas sizes are fixed but the purple areas can expand freely as set via min/max sizes and layout stretching. The top and bottom edges are horizontal layouts and the left and right edges are vertical layouts.
This works very well for my use case and solved all the choppy problems :)
In my application (cairo and X11), the user can issue a command whereby the drawing is enlarged. To be able to grab the entire drawing as a pattern, I enlarge the drawing surface to match the current scale (the drawing is just a graph, so this can be afforded as far as memory is concerned). Beginning with a certain scale though, the X11 window refuses to refresh until it gets an event (e.g. loss of focus, which is not even handled in my application).
I tried refreshing the window using both XFlush() and XSync().
Does this look like a bug in the windowing system? If not, what should I do? Everything works perfectly with smaller scales.
EDIT 1: After much work with gdb, I found that the problem is not with the window not refreshing. Rather, at a certain point a call to XNextEvent() causes the window to become all black.
EDIT2: It looks like calls to XNextEvent() actually cause the window to be refreshed! And here is the code that caused the problem:
struct PatternLock {
PatternLock(Graphics &g)
: g_(g) {
p_ = cairo_get_source(g_.cr);
cairo_pattern_reference(p_);
}
~PatternLock() {
// The commented lines caused the problem. How come?
// cairo_set_source_rgb(g_.cr, 0, 0, 0);
// cairo_paint(g_.cr);
cairo_set_source(g_.cr, p_);
cairo_paint(g_.cr);
cairo_pattern_destroy(p_);
}
private:
Graphics &g_;
cairo_pattern_t *p_;
};
Suppose the we have this code for moving the drawing:
{
PatternLock lock{g};
... // Change of transformation matrix
}
It somehow happen that the effect of the commented lines in the destructor of PatternLock becomes visible (hence the black screen), but the effect of the following lines does not. I realize that the commented code is actually unneeded. But still, how does this happen?
If my memory serves me correct, there's a limit to Drawables (e.g. Windows and Pixmaps) of 4096x4096 pixels. You should check the return values of your calls to XCreatePixmap() etc.
Either way, just enlarging the pixmap to draw your drawing is Bad Design (tm), and will inevitably lead to a very slow program. Learn how to deal with zoom and pan (tip: work from the center of your viewport, not the corners). Assuming your drawing is vector-based (i.e. lines and curves) you can optimize painting a lot at high zoom factors.
If you must grab a complete graph at a resolutions larger than 4096 pixels you must implement tiling, which isn't that hard if you have zoom and pan already.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to make a timeline in Qt, where a different color stands for a different task.
Ultimately, it should look a bit like this (but only one line);
Now anyone got any ideas how to do this without installing extra libraries, but just with QPaint?
The data it is representing is stored in a self-defined structure.
Any help is welcome.
Thx!
You seem to need something like the code below, although please note that it is just pseudo code, and the exact details depend a lot on how you get the data from your datastructure which you have not yet shared with us.
...
QPainter *painter = new QPainter(MyPaintDevice); // MyPaintDevice could be even 'this'
QPen pen(Qt::gray, 2);
painter->setPen(pen);
int currentX = 0;
const int currentY = 0;
const int height = hardCodedValue; // Coming from some static data initialization
foreach (const Settings& settings, settingsList) {
QBrush brush(QColor(settings.colorString()));
painter->setBrush(brush);
painter->drawRect(currentX, currentY, settings.width(), height);
currentX += settings.width();
}
...
Admittedly, you would be better off going for QML rather than the old QPainter engine for several reasons. That is hardware accelerated these days rather than software rasterization as the QPainter approach, but probably more importantly for you: it is lotta simpler.
You would need to look into the native Rect qml element, and probably the Repeater.
You can subclass Qwidget, and redefine at least paintEvent(QPaintEvent * event) and resizeEvent(QResizeEvent * event).
I assume your widget hold a initial list of pairs of QRect and QColor. When a resize event occurs you compute a rendering list of QRect with basic relative arithmetic. When a paint even occurs you use this rendering list to fill with the color.
You can even allow to modify colors with mouse events for some neat rendering.
All of these can be done with Qml too, and may be even easier.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am now trying to create a screen capture program. Last week I have tried the GetFrontBufferData method and it succeed. However, its performance is terrible and can only catch the desktop images. It means that when I start to run a full screen game, the GetFrontBufferData Method will get nothing. But what I want is to catch the full screen game not the desktop images.
In the GetFrontBufferData solution, I created a device and let the HWND parameter be NULL. I wonder if I can get the handle of the toplevel window (mean, the handle of full screen game), and pass it to the CreateDevice method, I can catch its screen shot.
Actually I have tried several screen capture method based on DX9. I have studied the questions Fastest method of screen capturing and tried the GetRenderTarget() method in conjunction with GetRenderTargetData(). The problem is I can only get the Render data rendered by the program itself. And the same with GetBackBuffer().
So the problem comes to, can I get a specific window image (like some running full screen game) if I can get the Handle of the toplevel window? I have checked the EnumWindow method described in Win32 - Get Main Wnd Handle of application. This code seems to get windows which only created by the program it self according to my test. However I need to get some windows created by the other programs.
So, that is my question. In a word, I want to know two things: 1, Is my approach that get handles of other windows then pass it to the CreateDevice and get its screen shot right? 2, If it is right, how to get the handles; if it is wrong, what is the rigth way to capture the full screen games?
That's not right, the window handle passed to CreateDevice has nothing to do with screen capture, it is the window that alert Direct3D when the app go background. since you want to capture the screen data stream from another app, so the window handle here cannot help you.
Since performance is critical, and GetFrontBufferData was time-consuming, so the only way I think suit for your need is Hooking Direct3D, interrupt the call to Present, and get the data from backbuffer, then you can do what you want, this is just an idea, I am not familiar with hooking and I never hooking Direc3D before.
Here is something about the two window handle used in CreateDevice(from DirectX SDK document)
When you create a Direct3D device, you supply two different window
parameters: a focus window (hFocusWindow) and a device window (the
hDeviceWindow in D3DPRESENT_PARAMETERS). The purpose of each window
is: The focus window alerts Direct3D when an application switches from
foreground mode to background mode (via Alt-Tab, a mouse click, or
some other method). A single focus window is shared by each device
created by an application. The device window determines the location
and size of the back buffer on screen. This is used by Direct3D when
the back buffer contents are copied to the front buffer during
Present.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I implemented Zurb Foundation's Clearing on an e-commerce website and I'm facing an issue under Chrome when the fullscreen is launched. The issue is even present on the Zurb Docs where the first image is shown on the bottom-right corner of the screen.
You can see right here on their gallery example (remember you need to run on Chrome)
If the link is not working or the issue isn't showing to you, please check this link of a screenshot I took from their website.
I posted on support but they don't seem to be there following feedback.
Maybe anyone found the way to solve this...would you share with me?
Thanks!!