Tower of hanoi using Qt [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am trying to create a GUI for a tower of Hanoi application using Qt. I Already wrote the C++ code using recursion but I am a total beginner when it comes to working with Qt and creating GUI's and have just started familiarizing myself with it by creating a simple calculator GUI but don't have a clear idea how to tackle the Tower of Hanoi GUI especially on how to create the disks and pegs graphically.
Any tips or pointers on where to start would be welcome. Thank you.

I suggest to take a look at QGraphicsScene. It will take a while to get familiar, but it is the proper Qt tool for handling these kind of things.
Basically, QGraphicsScene is an abstract representation of, well, a graphics scene. It is a virtual canvas object to which you can add graphics items (QAbstractGraphicsItems) with various shapes and positions. It can then be rendered in a specialized view class called QGraphicsView.
A good starting point is the '40000 chips' example in Qt Creator.

I can recommend 2 options:
One would be using an OpenGL canvas inside your window and have objects loaded into your 2D scene. And when you need to move or rotate the objects, you would just need to know about translate and rotate functionalities.
You wouldn't need to do this with modern OpenGL, as it might be overwhelming at the starts. But OpenGL with fixed pipeline (legacy version) would be quite easy to learn. However, I should mention that QT and OpenGL can be a little trouble to use together for a beginner due to QT.
The other would be the QPainter to load and draw 2D images (of components of towers). It wouldn't be as realistic as an OpenGL equavelent, but it would get the job done as well.
You can find the functions available via QPainter in the following link. It has almost everything you would need and to move a part, just render using the initial position values added to the offset.
http://doc.qt.io/qt-5/qpainter.html
I also recommend watching a few videos on Youtube about translation in computer graphics, to understand the logic behind this common functionality about moving things in a scene in amounts dependent on other conditions.

Related

Simple OpenGL GUI Framework User Interaction Advice? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I'm designing a simple GUI framework from scratch as a project, using OpenGL and nothing else external and need some advice on how I might implement user interaction.
Basically, I've a base class GUIItem from which all elements inherit. This gives each item some basic variables such as position, a vector to contain child elements as well as some basic functions for mouse movement and clicking.
All elements are setup as above, with their relevant member variables.
What I'm struggling with is how to implement user interaction properly. In my window manager I would create a new instance of an item, say GUIButton and call it button1. The window manager would, upon a click occurring, iterate through its list of elements and any child elements they may have, calculating a rectangular area around the object based on its coordinates, height and width, then running any "on click" function associated with said item, like change the value of textlabel1.
Firstly, is there a better way to do this calculation? It would work for rectangular elements but spherical objects and others would have a much larger erroneous area which could be clicked. Ideally I would check pixels but I've no real idea how that would be achieved. I've heard about but never used GLUT (my project only allows use of this for handling mouse/keyboard interaction though). Does GLUT provide anything to assist in this case?
My main issue is with handling what would occur when an "On click" event actually occurred. At the moment GUIButton for example, has an "On click" function built in, so as far as I can see, I'd have to do something like make it a virtual function, meaning that each new button I created would have to have its own class just to overwrite the "on click" function and each instance of a button would be an instance of a unique class that simply inherited off of GUIButton. This seems messy to me, as I've no idea where I would store all those classes, and it seems a lot of extra code. Would I be creating a button1.cpp and button1.h file?
Any advice on this really would be welcome as I'm new to C++, OpenGL and it's the first time I've been exposed to GUI programming and there's not a lot to go on when an existing GUI framework is the usual choice.
if you want something stupidly simple and fast then you could:
create shadow screen buffer containing ID/index/pointer instead of color
pre-render this buffer
Just render each of your visual component to it but instead coloring/texturing just fill in the ID/index/pointer of rendered component. Do not forget to clear this with some NULL first ... After this you should have mask of your components. You need to do this just once ...
On mouse events
you simply convert mouse coordinates to the shadow screen space and pick the value. If it is NULL then you clicked or whatever on empty area. If it contains ID instead update or call the callbacks for component ID. if you have a list of all components then ID can be the list index, otherwise use its actual pointer or encode in style (component_type, component_index). As you can see this is pretty fast O(1) item selection no matter how many components you have ... The shadow screen can have different resolution then your actual screen (to preserve memory).
This have pixel perfect mouse selection accuracy no matter the shape of your components without the need for nested component search loops.
[Notes]
As I did this stuff here are some hints:
create a window class containing configuration of your components for single screen. Programs have usually more screens with different set of components and doing dynamically the screens over and over again just because you switch page/screen sucks.
use separate list of components one list per component type.
create IDE editor for your windows see drag & drop example in C++ it might get handy for this. Add get,set functions controlled by string/enum or flag to easy obtain/change properties to make Object Inspector possible. Also this is how mine IDE looks like:
The window is saved from IDE directly as C++ code I can just copy to my App. This is the above example without the knob (forgot to save it):
//---------------------------------------------------------------------------
// OpenGL VCL window beg: win
win.grid.allocate(0);
win.grid.num=0;
win.scale.allocate(0);
win.scale.num=0;
win.button.allocate(0);
win.button.num=0;
win.knob.allocate(0);
win.knob.num=0;
win.scrollbar.allocate(3);
win.scrollbar.num=3;
win.scrollbar[0].x0=200.0;
win.scrollbar[0].y0=19.0;
win.scrollbar[0].xs=256.0;
win.scrollbar[0].ys=16.0;
win.scrollbar[0].fxs=8.0;
win.scrollbar[0].fys=19.0;
win.scrollbar[0].name="_vcl_scrollbar0";
win.scrollbar[0].hint="";
win.scrollbar[0].min=0.000;
win.scrollbar[0].max=1.000;
win.scrollbar[0].pos=0.000;
win.scrollbar[0].dpos=0.100;
win.scrollbar[0].horizontal=1;
win.scrollbar[0].style=0;
win.scrollbar[0].resize();
win.scrollbar[1].x0=200.0;
win.scrollbar[1].y0=45.0;
win.scrollbar[1].xs=256.0;
win.scrollbar[1].ys=16.0;
win.scrollbar[1].fxs=8.0;
win.scrollbar[1].fys=19.0;
win.scrollbar[1].name="_vcl_scrollbar1";
win.scrollbar[1].hint="";
win.scrollbar[1].min=0.000;
win.scrollbar[1].max=1.000;
win.scrollbar[1].pos=0.000;
win.scrollbar[1].dpos=0.100;
win.scrollbar[1].horizontal=1;
win.scrollbar[1].style=0;
win.scrollbar[1].resize();
win.scrollbar[2].x0=200.0;
win.scrollbar[2].y0=70.0;
win.scrollbar[2].xs=256.0;
win.scrollbar[2].ys=16.0;
win.scrollbar[2].fxs=8.0;
win.scrollbar[2].fys=19.0;
win.scrollbar[2].name="_vcl_scrollbar2";
win.scrollbar[2].hint="";
win.scrollbar[2].min=0.000;
win.scrollbar[2].max=1.000;
win.scrollbar[2].pos=0.000;
win.scrollbar[2].dpos=0.100;
win.scrollbar[2].horizontal=1;
win.scrollbar[2].style=0;
win.scrollbar[2].resize();
win.interpbox.allocate(0);
win.interpbox.num=0;
win.dblist.allocate(0);
win.dblist.num=0;
// OpenGL VCL window end: win
//---------------------------------------------------------------------------
Look at images here plotting real time Data on Oscillocope for some ideas (I got this working for both GDI and OpenGL)
It is better to use pixel units instead of OpenGL <-1,+1> screen units for better visual quality and editing comfort.

Best way to display GUI [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to find out what's the best way to display a custom design GUI for a windows application? (I wouldn't mind cross-platform compatibility but windows is enough for now ;) )
I tried using DirectX but the problem with DX is, that you are forced to either use textured quads or render shapes other than quads or triangles with a lot of vertices... I'd prefer NOT to use bitmaps due to the limited resolution. Also a problem with DirectX is, that it doesnt run on systems without a compatible graphics card...
I don't want to use any librarys like Qt or such... I want to do it by myself, I just don't know where to start... Basicly what I'd like to have as a result is something like the GUI of NI's Traktor... (picture below) I have noticed that Traktor runs basicly everywhere (so I think it does not rely on GPU). Any suggestions?
You could do all your drawing in WM_PAINT
Use BeginPaint
Create your object
SelectObject
Draw your stuff or what ever
Then DeleteObject
You can even make a colour Transparent
If you want to draw your GUI in a different shap
Use SetLayeredWindowAttributes set the colour that you want transparent
Also look up Custom Draw
There are alot of examples out there
If you want to stick with windows, you can use GDI.
I'd prefer NOT to use bitmaps due to the limited resolution.
You can always create bitmaps dynamically. There are lots of open source libraries to help with the drawing, or you can use GDI for this again.

What is the right way to use a QStateMachine for a text adventure game? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have seen a couple topics about this already but they were a bit vague for me so I decided to make this. I'm working on a little adventure game just for fun in Qt, its basically just text on the screen and the player inputs commands into a line edit widget which I then process and do the related action/event. Thing is, I am a little confused on how to approach this. I don't want to dig myself a hole by manually coding in lots of commands and events per room because it seems to just be a pain later on. So then I thought about using some sort of database to store the information but which one should I use? I would love some advice from people who have tried something similar.
And here, these pictures are a rough outline of what I am trying to do.
Flowchart of states
Level tiles
Edit: I should add, the tiles for the level basically work like this. The light gray is a direction the player can move in, the dark gray parts are walls and the colors are various different actions you can do.
I don't particularly care for code, but I would like suggestions on what tools to use for this and maybe how to set them up right. Someone must know.

C++ Making 2D game graphics with GTK+ [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I am interested in making a GUI-intensive strategy game with C++ and GTK+. What I would like to know is how feasible it would be to add 2D game graphics to a GTK program. Basically I would be wanting something like a game screen, with interactable 2D graphics, flanked by menus and the ability to navigate to other screens that would be GUI only.
Note that I have never used GTK before, nor have I before programed a GUI (nor graphics either).
It's certainly possible with GTK, but you have to ask yourself whether you're using the right tool for the job. Use Clutter, which is much more suited to animation and integrates with GTK; or perhaps better yet, use a game programming toolkit.
Here's an example of two non-intensive proof-of-concept games written with Clutter, with links to their source code.
It's possible. I did it with GTK and Vala some time ago. Here is a blog post I wrote about it. Basically it's very much like the games you make with Java and Swing. Just override the expose signal and create a timer for regular redraws. Here's an article on developing a 2D Snake game in PyGTK.
In pseudocode, all you do for the game infrastructure is:
start()
{
tick_timer( 1.0 / FPS );
load_all_sprites_etc();
}
tick()
{
update();
game_board.expose(); // game_board is a GTKWidget, preferably a DrawingArea
}
expose_event() // connected to game_board
{
drawing_code();
}
update()
{
game_physics();
game_logic();
}

lightweight UI library for OpenGL [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
Like the title says, I'm looking for a library that's platform-agnostic and lightweight, for the purpose of displaying buttons, slider bars, check boxes, etc. in OpenGL. Ideally, something that is not directly coupled with OpenGL is best (ie. so that I may replace the rendering with DirectX if need be).
My 'ideal' library would have an event system that supports callbacks or something similar. This is a nice-to-have, but not absolutely required.
I looked into Qt and wxWidgets and both seem very heavy. CEGUI looks like a mess of code. Most other libraries I seen are GLUT based.
There's Dear IMGUI
https://github.com/ocornut/imgui
It's completely graphics API agnostic. It just builds vertices. Its up to you to render them.
There's also nuklear inspired by Dear ImGUI.
You might want to have a look at a game engine such as Irrlicht which provides OpenGL, DirectX and Software renderers and supports a 2D GUI System with Buttons, Lists, Edit boxes. See the feature page to learn more about its capabilities and the user interface sample to try this specific UI feature.
Irrlicht is open source and works on many platforms. From the feature page it supports:
Windows 98, ME, NT 4, 2000, XP, XP64, Vista, CE
Linux
OSX
Sun Solaris/SPARC
All platforms using SDL
Neither wxWidgets nor Qt draw in OpenGL; they don't use OpenGL rendering commands to draw. They allow you to create OpenGL windows, but the regular GUI controls (buttons, lists, etc) are not drawing through OpenGL. They're drawn through the native drawing mechanism of your platform of choice.
You are generally not going to find very many standalone GUI libraries that actually do their rendering in OpenGL. The only one I know of is CeGUI, which fits all of your requirements but you dismissed as being "a mess of code." Everything else is generally going to be "heavyweight", because it's part of a game engine.
Now, if you're not looking for something that actually draws using OpenGL commands, but is simply something that you can use alongside an OpenGL window, then things open up somewhat. However, your dismissal of wxWidgets as being "very heavy" is disconcerting, because GUI libraries aren't really going to get much lighter weight than that. A good GUI system requires a lot of stuff.
The smallest cross-platform GUI library that supports making an OpenGL window is probably FLTK. And even that's pretty big.