My Goal:
I would like to independently manipulate pixels color of a created window like a bitmap picture.
I would prefer a native library targeting Ubuntu -> Linux. It may be working with a function like:
SetPixel(int x,int y,int Color) {
//Some code that set the color of one pixel by his coordinates x and y
return 0;//May return an error
}
It will set the color of a pixel in the window by his position
Problems:
I already tried many tutorials (which are using Graphics.h or Windows.h) , but none of them were working on my computer, I could be doing something wrong but what I found isn't explicit. Anyway, I wouldn't know how to download a library without instructions. I believe that it doesn't work on Linux.
SDL2 isn't native of Linux. by the way
If any clarification is needed, please add a comment or suggest an edit
You should study SDL2. It's pretty easy to learn and I recommend you to read these articles, they are a great and complety tutorial.
http://lazyfoo.net/tutorials/SDL/index.php
What you need in special is available in this one:
http://lazyfoo.net/tutorials/SDL/08_geometry_rendering/index.php
Only to show you the functions:
SDL_RenderSetDrawColor( myRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderDrawPoint( myRenderer, cordX, cordY);
So you are able to choose the color and change the pixels independently
PS: It's works on ubuntu, the articles will teach you how to configure everything you need.
PS2: If you need help contact me, my OS is also ubuntu.
For me, the easy solution something similar to the code below
#include <X11/Xlib>
bool SetPixel(uint16_t X,uint16_t Y,uint32_t Color){
XSetForeground(_Display,_GraphicCTX,Color);
XDrawPoint(_Display,_Window,_GraphicCTX,X,Y);
return true;
}
bool Setup(){ //Use it first to make every graphical manipulations working :)
Display=XOpenDisplay(0);//Create a display
Window=XCreateSimpleWindow(Display,DefaultRootWindow(Display),0,0,480,360,0,0,0); //Create a Window
XMapWindow(Display,Window);//Make the Window visible
GraphicCTX=XCreateGC(Display,Window,0,NIL);//Create a Graphics Context
//v Wait for a MapNotify XEvent for next commands
XSelectInput(Display,Window,StructureNotifyMask);
while(1){
XEvent E;
XNextEvent(Display,&E);
if(E.type==MapNotify)break;
}
return true;
}
I used X11 as it is the native graphical protocol of Linux.
In fact, I made a library to make everything simpler with a class.
The setup function is required to be run before any graphical operations else it won't do anything!
This can work, but for advanced development, you'll need play with images and more! There is a very great manual for Xlib!
Related
I want to basiically create a window with C++. It should have a control panel like structure, with the option to toggle a button. The button's image should be from a bitmap, one for ON and other for OFF.
It should have an array of such individual control buttons, like a control panel. I should be able to toggle ON/OFF. But I've to build this tiny application using C++. Kindly guide me on to start and proceed?
You should have a look on these:
Qt
GTK+
wxWidgets
and then choose depending on your need and preferencies.
EDIT:
if you are on Windows, you can use WinAPI. But you loose portability of your code among another OSs.
Unfortunately GUI in C++ is not that simple.
You're going to have to interact with the OS at some point to achieve this, which means you will need some sort of library to use.
To better answer your question (as has been mentioned in the comments) you will need to identify what kind of OS you are targeting? The usual three are:
Windows
Mac
Linux
With what information you've given though:
I recommend using the SFML Library or something similar. There are tutorials available and it works quite well cross-platform. To open a window you just instantiate a class like so:
sf::Window window(sf::VideoMode(800, 600), "My window");
Drawing buttons is a little harder.
Many libraries provide their own solutions, so I'll avoid giving a specific example, and the below is only psuedo-code.
In general though, you want to capture when the user clicks, then get their Mouse/Pointer Position (x, y) on screen, and check it against the boundaries of each corner of the button's rectangle:
if (ButtonPressed())
{
int currentX = GetMousePositionX();
int currentY = GetMousePositionY();
if (currentX > button.Left() &&
currentX < button.Right() &&
currentY > button.Top() &&
currentY < button.Bottom())
{
// The button is pressed
}
}
The code example above does assume that the MousePosition starts at 0, 0 at the top left of the window, which different libraries do differently. So you'll want to check how the library you're using implements it.
You might also want to consider foregoing the GUI entirely (if this is really just a small project, GUI in C++ can be surprisingly difficult).
I'm trying to write a program that draws a specific shape on the console screen. I want to try and use coordinate points off the console screen to specify points of the shape, store it in an array, and clearly show up on the console screen as a white (or any color) closed figure. I basically want to know whether it is possible to set up a Cartesian plane on the console screen in such a way I can creates shapes and figures depending on the (x,y) coordinates I provide the program. I don't exactly understand how Windows GDI works, and the only library I've heard of that can do this is the 'curse.h' or 'ncurse.h' libraries. Besides I didn't find a single reference on the internet on how to install these different libraries in my Visual C++ 2010 Express Edition compiler. Thanks to #john, I was able to look up the console functions for Windows Application. I am a beginner with coding, so bear with me, here is a program I wrote based on that which is filled with errors (at least that's what the builder's saying):
#include <WinCon.h>
using namespace std;
int main()
{
char string[] = "#";
char recString[5] = {'\0'};
COORD coordinates;
coordinates.X = 15;
coordinates.Y = 10;
SetConsoleCursorPosition(GENERIC_READ, coordinates);
WriteConsole(GENERIC_WRITE, string, 1, recString, NULL);
char response;
cin >> response;
return 0;
}
The Windows Console API should give you everything you want http://msdn.microsoft.com/en-us/library/windows/desktop/ms682073%28v=vs.85%29.aspx
EDIT:
I don't have much experience with this library but I can see some problems with the code above. Something like this is more like what you should be doing
HANDLE console_out = GetStdHandle(STD_OUTPUT_HANDLE);
...
SetConsoleCursorPosition(console_out, coordinates);
...
WriteConsole(console_out, string, 5, recString, NULL);
This page has some examples http://msdn.microsoft.com/en-us/library/windows/desktop/ms686971%28v=vs.85%29.aspx
See this tutorial on how to draw primitive shapes using the Simple DirectMedia Layer (SDL) library with the SDL_gfx add-on library. This will draw on a window outside of the console window (AFAIK, it's not possible to draw pixel-by-pixel graphics directly inside the Windows console).
If you're just looking for a way to generate plots with geometric shapes in them, and you're already familiar with Matlab (or its free clone Octave), then you should consider using the excellent geom2d Matlab library.
I am new to C++ and I would like to know what the limitations are in graphics for a console application. For example---Could I create something as compicated as some of the
very colorful screensavers that have all kinds of splashes of color?? Could I draw lines
of changing color based on input strings??? I would appreciate any advice someone could
give me.
Thanks Doug
If you want to do some serious animation you'll pribably want hardware accelerated graphics (DirectX, OpenGL). If you just want simple images and animations a GUI app would do. As far as the console it's not really intended for more than text output but it can draw lines and change colors if you really want too.
However none of the three are limitations of C++ ... C++ as a language does not care about graphics that would be an OS limitation primarily and you'll find most of your drawing code however you go about it will be somewhat OS or hardware dependent unless you use a cross platform library with GUI or graphics support like QT, wxWidgets, OpenGL, etc.
As others have said, a console application is for text, not graphics! I don't know of any way (or reason) to do graphics in a console.
To do the kinds of things you are interested in (except maybe Windows screensavers) using Visual C++, I would recommend starting off with the SDL library. The Lazy Foo Productions website has an excellent series of game programming tutorials, and the first lesson gives you a step-by-step guide to build an app that displays stuff on the screen. It even has screenshots showing how to configure Visual Studio 2010, which is pretty important if you're new to this kind of thing.
SDL is free, cross-platform, and will let you (within your program's window):
draw pixels, lines, and rectangles in any color
draw text
draw images
make animations (by changing/redrawing the screen many times per second)
obtain keyboard input (including when keys are released)
It will also let you do 3D graphics with OpenGL, but that's another story.
You could, if you're very desperate- but certainly not platform-independently. From memory, the Windows API is quite good about letting you do a lot of crazy shit to it's console. However, it's probably better advised to get a genuine graphical API for this purpose, such as GDI, DirectX or OpenGL.
C++ does not have any standard facilities for drawing graphics in a console application. Any features (like changing the font color) will depend on your OS. I doubt you will find functions that do much more than changing the text color though. (For example, on Windows you would use system("color xx") to change the foreground and background color.)
Basically, if you want graphics you're going to have to abandon the console application and look for a graphics API.
Could I create something as compicated as some of the very colorful screensavers that have all kinds of splashes of color??
If by "splash" you mean "chunk of text", then yes. Otherwise no.
Could I draw lines of changing color based on input strings???
No, consoles are textual media.
If you want to try to do things to the console you need to use the Windows Console Functions. Standard C++ does not have any way to change console color.
I am currently making an OpenCV project in C++ where I look for motion with a kinect and use that to cue a slideshow (sans recognition). Currently, I am displaying the slideshow using OpenCV (as I've only had about a week to whip this up). It looks good and its quick. The only problem is that this is going to be on display for a big production and I can't really afford to have the window showing (I'm talking window decorations like the title bar and such).
I need to get rid of the title bar. I've done a lot of research, and I have found out that you can magically grab the window handle by calling cvGetWindowHandle("SlideShow"), but that is a void function, so I don't really know how I am supposed to get a handle from that to manipulate.
I'm developing this for both windows AND ubuntu, since it will end up on a windows machine, but I can only demo on a laptop running ubuntu.
If anyone can tell me how to take the window and render it fullscreen with a resized image to fill most if not the entire screen in either Windows or Ubuntu, I will be forever grateful.
I am using OpenCV 2.1 on Ubuntu 11.04. On my system CV_WINDOW_FULLSCREEN and CV_WINDOW_AUTOSIZE flags both map to 1
And both flags behave exactly the same. They give you a fixed size window, which would be expected for AUTOSIZE flag but not the FULLSCREEN. I think these two flags are meant for different functions although their simillar appearance is very confusing. The flag CV_WINDOW_NORMAL maps to value 0 which is what you have used. It gives you a resizable window that you could maximize, but it is not a fullscreen window.
Edit: I just found the solution in another stachoverflow post. Here is the solution from that post which worked great on my system:
cvNamedWindow("Name", CV_WINDOW_NORMAL);
cvSetWindowProperty("Name", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
cvShowImage("Name", your_image);
I get a real fullscreen with no title bar etc.
you can use the cv::setWindowProperty function for your purpose, just set it to CV_WINDOW_FULLSCREEN.
Full documentation in the openCV-WIKI
In opencv/4.5.1, this is how it is done:
namedWindow("Name", WINDOW_NORMAL);
setWindowProperty ("Name", WND_PROP_FULLSCREEN, WINDOW_FULLSCREEN);
Assuming you have added using namespace cv;
How would I draw something on the screen ? not the console window but the entire screen, preferably with the console minimised.
Also, would it show up on a printscreen ?
What I want to do is create something like a layer on top of the screen that only me and my aplication are aware of yet still be able to use aplications as usual.
Here's an example:
Let's say I want 2 yellow squares 5 by 5 pixels in size appearing in the center of the screen on top of all the other applications, unclickable and invisible to a printscreen.
[Edit]
I forgot to mention that I'm using Visual Studio 2010 on Windows XP.
in windows you can use the GetDC-function.
just a minimalistic example:
#include <Windows.h>
#include <iostream>
void drawRect(){
HDC screenDC = ::GetDC(0);
::Rectangle(screenDC, 200, 200, 300, 300);
::ReleaseDC(0, screenDC);
}
int main(void){
char c;
std::cin >> c;
if (c == 'd') drawRect();
std::cin >> c;
return 0;
}
but since Windows Vista it is very slow
C++ has no notion of a "screen" and especially none of "graphics". The functionality needed is provided by your operating system. On many systems you will need a "Window" and draw on it. To do this portably, a library like Qt might help. A Windows solution was given by Oops. Maybe you want to use some OpenGL library, or Windows' DirectDraw/Direct3D from DirectX, in case you want to do some 3D stuff with your graphics.
Windows offers GDI/+, WPF, and DirectX (including Direct2D on Vista+).
The (rather nice but not recently updated) graphics library anti-grain geometry has very simple bindings to display its demos on a variety of windowing systems, you could look at those for examples. But for anything much more involved you're probably talking about operating system specific libraries.