How do I draw a certain pixel on the console?
Thanks (:
Printable View
How do I draw a certain pixel on the console?
Thanks (:
Without changing the mode (which is only available in DOS) to graphics mode, you can't (or at least, you ain't supposed to do that - I'm sure there is some sneaky way of getting the handle of the window and using PutPixel or some such - but that's the same as asking "How do I add my own drawing in Internet Explorer - you aren't supposed to do that".
--
Mats
I don know what you are talking about but it doesn't matter right now - I got it. (SetPixel)
thanks anyway [:
Wait something is wierd with this function.. it seems to putpixel into my screen not into my console O_o
Help XD
GetDesktopWindow() you mean... and I don't think there is a concept of security when it comes to writing to another window's DC (or various other things). There is usually just, 1. you can write to another processes' window (attribute wise) with this function or 2. you can't :)
You need a handle to the console window, ie the window in which the command prompt appears... which should be easy to get (try FindWindow()). And then you can get the child-window area -- if that's what it's called, haven't done windows programming for a while.
The only downside is, you have to have a good understanding of how Win32 all ties together. And note that the window will probably redraw itself, thus you'd have to re-draw your pixels -- double buffering can help here.
GetConsoleWindow(): http://msdn.microsoft.com/en-us/libr...75(VS.85).aspx
O:
I used SetPixel, GetConsoleWindow, GetDC, RGB.
but after I draw the pixel if I press another apllication or window the pixel will be "removed" from from the screen - why?!
Because the redrawing of the window is done by a process that isn't aware that you have drawn on top of what it knows about - that is exactly the point I was trying to make earlier - you can draw in another window, but you won't be able to make the application know that the drawing is there and make the other application maintain it. So, now you also need to intercept the "WM_PAINT" messages for that process, and we get into a really big hairy mess of "hooking" things.
--
Mats
Ohh so what is the solution? (if there is..)
humm and one more thing - what is the difference between the coordinates of gotoxy (the struct CROOD) and the coordinates of SetPixel? - it seems that SetPixel related somehow with my screen resolution and gotoxy (normally) related to the console pixels only..
thank you (:
What are you actually trying to do? I'm sure there is some solution, but perhaps it's not along the lines of what you are currently doing... You can't really draw within another applications windows and expect those applications to maintain the content - that's like putting your pot-plants in your neighbours garden, and when you come back from holiday, expect them to still be there, without asking your neighbour to look after them - except that if your neighbour is kind, it's likely to work to do this. Applications are NEVER kind in this sense.
--
Mats
XD
All I'm trying to do is to create functions for the console for drawing lines, rectangles, circles and so on..
and as I said it's actually works but not as I expected.
now, any solution\alternative way for doing this?
Yes, use the proper Windows API in a GUI application - that's the RIGHT way to solve this type of problem.
Of course, that already has primitives to draw lines, rectangles, etc. But if you want to just practice, then you can always use the SetPixel to draw a pixel at a time.
--
Mats
You could use FindWindow() and use the console window name, but its really not worth it. Just learn Win32 basics if you wan't to get into graphics.
Well ok.. (any links?)
and talking about primitives - I tried to think how to draw a circle but I have nothing helpefull \:
any ideas?
thank you again (:
I would recommend Programming Windows, but if your not into books there are a few windows tutorials online. Just search "Windows api tutorials" in google and you will get some hits. It will be very difficult at first but it gets easier later.
Charles Petzold's "Programming Windows" is by far the best book - even if it's a few years old now, it's still applies to 99.9% of the stuff it describes.
As to how to draw a circle, you can use sin/cos to find the coordinates for X,Y at a certain angle. x = cos(angle) * radius, y = sin(angle) * radius. Obviously, you'll also have to add the offset of the center-point. [You can half or quarter the number of points you have to calculate by using reflections and only draw the 90/180 degrees + the opposites].
For a much faster (but slightly more complex) circle drawing mechanism, look up Brezenhem's circle algorithm - it only uses simple add, subtract and I think there is one division at the beginning. Again, you can use reflections to draw more points at once.
--
Mats
Thank you both I'll try [=
I succssed creating this function but it's very slow I can see every single pixel drawing \:
any ideas? (the accurate is 3 digits after the point)
So, what are you doing?
And yes, SetPixel in itself isn't very fast. Sin and Cos are probably the slowest instructions in the x86 FPU instruction set - and the C library version of sin/cos may be slower yet if the compiler uses discrete instructions (like gcc does if you don't use -ffast-math), and even slower yet if you use debug version of the code.
--
Mats
What do you think? the circle function.. XD
The algorithem you mentioned above might be faster?