WinAPI create PatternBrush with offset - c++

Sorry for the unclear title, but I'm having a problem with WinAPI.
I have created a window and added double buffering for graphics, and now I'm trying to draw a rectangle using FillRect(). The only problem is when I'm using CreatePatternBrush() to texture the rectangle, the texture then always starts at (0, 0) no matter where the rectangle starts.
I would like to, for example, be able to draw a rectangle between (10, 10) and (60, 60) and have the texture's corners match the rectangle's corners. Is that possible or do I have to use BitBlt() instead?

Related

DrawingArea: fill area outside a region

I have followed this gtkmm tutorial on how to draw shapes and fill them with colors (e.g. A red disc on a transparent background). I was also able, from this example, to derive another example with a red disc on a blue background.
However, what I would really need is a transparent disc with a blue background that fills everything minus the disc area, which should stay transparent.
So with cairo, the usual workflow is:
Create a surface
Draw a shape (e.g. draw a circle)
Fill the circle, so that it becomes a disc.
I would need some workflow that achieves something like this instead:
Create a surface
Draw a shape (e.g. draw a circle)
Fill the area outside the circle, so that I have a colored background with a transparent "hole" in the middle.
I have done some research on this on the web but all examples seem to assume that we want to fill the inner region of a shape (which I must admit is more typical).
How could I do this?
P.S. I have added the C tag because I don't mind if you prefer to use C (or even Python).
Draw your circle and draw a rectangle containing all the visible area. Set the cairo fill rule to even/odd. Fill. Done.
cairo_save(cr); // Save the state
cairo_arc(cr, 42, 42, 21, 0, 2*M_PI); // Draw circle
cairo_rectangle(cr, 0, 0, width, height); // Rectangle containing everything
cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
cairo_fill(cr);
cairo_restore(cr); // Restore default fill rule (optional; pairs with save above)
IMHO, The function of 'Draw outside the circle' is complex to the graphic framework. It may also be ambiguous if you draw more than one circle filled outside.
As graphic shapes drawn later are placed 'on' the ones drawn former. What is needed is that draw a rectangle to fill the entire graphic context before drawing other shapes. This is defined as clear with the background color in some frameworks.
the workflow would seem like:
1. Create the surface.
2. Draw the background colored with what outside the circle.
3. Draw the circle filled with a specific color, e.g. white.
As a result, the circle would cover the background.
If insist on draw the circle first, please search Flood Fill Algorithm, which is used to draw on images. However, it is needless and costly to achieve the screen pixels and play such algorithms when drawing on screen.
I find
Example Application: Creating a Clock with Cairo in the later section of the book you provide.
That seems help.

Draw only visible shapes

I'm developing a 2D graphics viewer by using Direct2D. I have a lot (up to 200,000) of shapes to draw. I would like to be able to draw only those shapes that are actually visible inside my canvas.
So, for example, if my window is 640x480 (starting from 0,0) I don't need to draw a rectangle which has its top-left corner in (1000, 1000).
How can I achieve this result?

OpenGL 2D pixel perfect rendering

I'm trying to render a 2D image so that it will cover the entire window exactly.
For my test, I setup a window so that the client area is exactly 320x240 and the texture is also this size.
I setup my orthographic projection for a 1x1x1 cube centered at the origin, and set my viewport to 0,0,320,240
The texture is mapped to a quad of size 1x1 centered in the origin.
The shader is a trivial shader doing the ProjModelViewPos
I created a test texture that will allow me to verify the rendering, and I see a consistent discrepancy I can't shake.
The results of the rendering always some stretching that puts some of the pixels up and to the right of the window, and seem to be always by the same amount, regardless of the window size (same amount of pixels, if I replace 320x240 by another value)
I think it has to do with window decoration widths, but I'm not sure how to fix it so that the solution is not platform / machine specific.
EDITS:
The code is straight C++ using freeglut and glew
Verified that this doesn't happen if I call glutFullScreen, so it's definitely windowed mode related.
Note: this was answered before the language tag was added
Not sure what module you are using for this.
If you are using Pyglet the easiest way is achieve this is:
import pyglet
width = 320
height = 240
window = pyglet.window.Window(width, height)
image = pyglet.resource.image('image.png')
#window.event
def on_draw():
window.clear()
image.blit(0, 0, 0, width, height)
pyglet.app.run()
You can find more information about this here:
http://www.pyglet.org/doc/programming_guide/size_and_position.html
http://www.pyglet.org/doc/programming_guide/displaying_images.html

SFML Generate isometric tile

I have a sprite, a square, just for orthogonal projection. Now I want to project it in a very basic, simple isometric way. (I know this might not be pretty, but I just want to figure this out)
Given my square, I rotate it 45 degrees. Now if I understand correctly, I should still divide my height by 2. This has been impossible for me in SFML. There is a scale function but if I scale with a factor 0.5 in the y-axis direction, my cube just gets stretched, instead of a diamond shape. It looks as though SFML transforms the sprite according to it's own relative axes (that were rotated before..).
Since you cannot access the height of a sprite, I was wondering if this was even possible?
Can I convert a square sprite to a diamond shape in SFML?
Using a sf::RenderTexture is an option (see other answer). Another option is to fiddle with the sf::View. Double the view's height, and adjust coordinates. It would go something like this:
my_sprite.setRotation(45.f);
//adjust the position for new screen coordinates (once)
my_sprite.setPosition(my_sprite.getPosition().x, my_sprite.getPosition().y * 2);
//...
//when drawing:
sf::View v = my_render_window.getDefaultView();
v.setSize(v.getSize().x, v.getSize().y * 2);
v.setCenter(v.getSize() *.5f);
my_render_window.setView(v);
my_render_window.draw(my_sprite);
my_render_window.setView(my_render_window.getDefaultView());
Rotate your sprite as you are doing now. Render it to an sf::RenderTexture. Use the member function getTexture, and make a new sprite from it, or reuse the old sprite. Scale the sprite along the y-axis. Draw it to the render window.
Some math on your part may be required in order to set the RenderTexture to the right size and to draw the original sprite in the correct location on it.
original_sprite.setRotation(45);
sf::RenderTexture rt;
rt.create(FigureOutWidth(),FigureOutHeight());
original_sprite.setPosition(MoreMathHere());
rt.draw(original_sprite);
sf::Sprite new_sprite(rt.getTexture());
new_sprite.setScale(1.0,0.5);
It should go without saying, but do this once in initialization, not every frame.

Direct3d drawing 2d sprites. scaling issues

I'm new to programming with c++ and direct3d. I try to draw textures(LPDIRECT3DTEXTURE9) on the screen with a sprite(LPD3DXSPRITE). But when I draw a texture it seems to enlarge it, a 100x100 px texture is not drawn from coordinates (100, 100) to (200, 200) but from (100, 100) to (227, 227), I tested that by drawing 1x1 px blocks at those coordinates. I can't figure out why this is the case... Is the creation of the LPDIRECT3DDEVICE9 of any influence on it or could it be something completely different?
This seems to be a scaling issue. When you first call CreateTextureFromFile(), the function causes the texture to be scaled to a power of 2 rule (512, 256 for example). To fix this, simply call the setScale function to 1,1 and this should fix your problem :)