I have some serious issues with sdkTrays in Ogre.
I have my OgreKinectGame that inherits from BaseApplication. BaseApplication is creating logo etc. using sdkTrayManager. I thought this mTrayMgr was inherited and can be used in my application as well.
I am trying to setup widgets for a HUD, but I'm getting unhandled exception errors.
My setupWidgets() function looks like this.
void OgreKinectGame::setupWidgets()
{
if(!mTrayMgr)
mTrayMgr = new SdkTrayManager("InterfaceName", mWindow, mMouse);
//mTrayMgr->destroyAllWidgets(); this caused exceptions as well
// create check boxes to toggle the visibility of our particle systems
const int WIDTH_UI = 160;
// main menu
mTrayMgr->createLabel(TL_CENTER, "mMainMenuLabel", "Main Menu", WIDTH_UI);
mTrayMgr->createButton(TL_CENTER, "mOptionButton", "Option");
mTrayMgr->createButton(TL_CENTER, "mCreditButton", "About");
mTrayMgr->createButton(TL_CENTER, "mQuitButton", "Quit");
mTrayMgr->showAll();
}
First, where did you initialize your setupWidgets()? , on BaseApplication class, they have setup(), you can create a virtual of this setup() to your main class and then initialize your setupWiget() there, e.g.,
bool OgreKinectGame::setup(void)
{
if (!BaseApplication::setup()) {
return false;
}
// Load fonts for tray captions
FontManager::getSingleton().getByName("SdkTrays/Caption")->load();
setupWidgets();//initialize here for your setupWidget()
}
second, I think your setupWidget() should be like this,
void OgreKinectGame::setupWidgets()
{
const int WIDTH_UI = 160;
// main menu
mTrayMgr->createLabel(TL_CENTER, "mMainMenuLabel", "Main Menu", WIDTH_UI);
mTrayMgr->createButton(TL_CENTER, "mOptionButton", "Option");
mTrayMgr->createButton(TL_CENTER, "mCreditButton", "About");
mTrayMgr->createButton(TL_CENTER, "mQuitButton", "Quit");
}
Can you try this solution and back again if still get crash?
Related
I'm working on a new project and an implementing a basic scene change. I have the different scenes setup as their own classes, with the intialisation function being used to create and reposition different SFML objects. I saw this answer and have written my scene switcher similarly:
// Create scene monitoring variable
int scene[2];
scene[0] = 0; // Set current scene to menu
scene[1] = 0; // Set scene change to no
...
// Check for scene change
if(scene[1] == 0) {
// Run tick function based on current scene
switch(scene[0]) {
case 0:
// Main menu - run tick function
menu.tick();
}
}
if(scene[1] == 1) {
// Reset scene that you've changed to
switch(scene[0]) {
case 0:
// Main menu - reset it
menu = Menu(window, scene); // <-- Reinitialise menu here
}
// Set change variable to 0
scene[1] = 0;
}
You can see the full code on the github repository.
However, this doesn't seem to work properly - as soon as a scene change is made, the screen goes blank. The class is reintialised (I added a cout to check), the draw function is still run and mouse clicks are still processed, yet nothing appears in the window.
Am I doing something wrong here?
Doing things that way can lead into leak memory errors. I suggest you a different approach: the StateStack
How this works?
The basics of having a StateStack object is store each possible state of your game/app into a stack. This way, you can process each one in the stack order.
What is an State?
An State is something that can be updated, drawn and handle events. We can make an interface or an abstract class to make our screens behave like a State.
Which are the advantages?
With a stack structure, you can easily control how your different scenes are going to handle the three different processing methods. For instance. If you have a mouse click while you're in a pause menu, you won't that click event to reach the menu state or the "game" state. To achieve this, the solution is really easy, simply return false in your handleEvent method if you don't want the event go further this particular state. Note that this idea is also expandable to draw or update methods. In your pause menu, you won't update your "game" state. In your "game" state you won't draw tour menu state.
Example
With this points in mind, this is one possible way of implementation. First, the State interface:
class State{
public:
virtual bool update() = 0;
virtual bool draw(sf::RenderTarget& target) const = 0;
// We will use a vector instead a stack because we can iterate vectors (for drawing, update, etc)
virtual bool handleEvent(sf::Event e, std::vector<State*> &stack) = 0;
};
Following this interface we can have a example MenuState and PauseState:
MenuState
class MenuState : public State{
public:
MenuState(){
m_count = 0;
m_font.loadFromFile("Roboto-Regular.ttf");
m_text.setFont(m_font);
m_text.setString("MenuState: " + std::to_string(m_count));
m_text.setPosition(10, 10);
m_text.setFillColor(sf::Color::White);
}
virtual bool update() {
m_count++;
m_text.setString("MenuState: " + std::to_string(m_count));
return true;
}
virtual bool draw(sf::RenderTarget &target) const{
target.draw(m_text);
return true;
}
virtual bool handleEvent(sf::Event e, std::vector<State*> &stack){
if (e.type == sf::Event::KeyPressed){
if (e.key.code == sf::Keyboard::P){
stack.push_back(new PauseState());
return true;
}
}
return true;
}
private:
sf::Font m_font;
sf::Text m_text;
unsigned int m_count;
};
PauseState
class PauseState : public State{
public:
PauseState(){
sf::Font f;
m_font.loadFromFile("Roboto-Regular.ttf");
m_text.setFont(m_font);
m_text.setString("PauseState");
m_text.setPosition(10, 10);
m_text.setFillColor(sf::Color::White);
}
virtual bool update() {
// By returning false, we prevent States UNDER Pause to update too
return false;
}
virtual bool draw(sf::RenderTarget &target) const{
target.draw(m_text);
// By returning false, we prevent States UNDER Pause to draw too
return false;
}
virtual bool handleEvent(sf::Event e, std::vector<State*> &stack){
if (e.type == sf::Event::KeyPressed){
if (e.key.code == sf::Keyboard::Escape){
stack.pop_back();
return true;
}
}
return false;
}
private:
sf::Font m_font;
sf::Text m_text;
};
By the way, while I was doing this, I notice that you must have the fonts as an attribute of the class in order to keep the reference. If not, when your text is drawn, its font is lost ant then it fails. Another way to face this is using a resource holder, which is much more efficient and robust.
Said this, our main will look like:
Main
int main() {
// Create window object
sf::RenderWindow window(sf::VideoMode(720, 720), "OpenTMS");
// Set window frame rate
window.setFramerateLimit(60);
std::vector<State*> stack;
// Create menu
stack.push_back(new MenuState());
// Main window loops
while (window.isOpen()) {
// Create events object
sf::Event event;
// Loop through events
while (window.pollEvent(event)) {
// Close window
if (event.type == sf::Event::Closed) {
window.close();
}
handleEventStack(event, stack);
}
updateStack(stack);
// Clear window
window.clear(sf::Color::Black);
drawStack(window, stack);
// Display window contents
window.display();
}
return 0;
}
The stack functions are simple for-loop but, with the detail that iterate the vector backwards. This is the way to imitate that stack behavior, starting from top (size-1 index) and ending at 0.
Stack functions
void handleEventStack(sf::Event e, std::vector<State*> &stack){
for (int i = stack.size()-1; i >=0; --i){
if (!stack[i]->handleEvent(e, stack)){
break;
}
}
}
void updateStack(std::vector<State*> &stack){
for (int i = stack.size() - 1; i >= 0; --i){
if (!stack[i]->update()){
break;
}
}
}
void drawStack(sf::RenderTarget &target, std::vector<State*> &stack){
for (int i = stack.size() - 1; i >= 0; --i){
if (!stack[i]->draw(target)){
break;
}
}
}
You can learn more about StateStacks and gamedev in general with this book
I am trying to write a program in gtkmm but the buttons will not show up. I've done everything I know to make these buttons show, but nothing has been working. I have even included the 'show all' methods in both the main and win_home.cpp files but still nothing happens. The program DOES however go through the code, as the cout statements are all being printed. Does anyone have any idea why these buttons would not be showing up?
main.cpp:
#include <gtkmm.h>
#include <iostream>
#include "win_home.h"
int main(int argc, char *argv[])
{
auto app = Gtk::Application::create(argc, argv, "com.InIT.InITPortal");
std::cout << "Creating Portal Window" << std::endl;
HomeGUI win_home;
win_home.set_default_size(600,400);
win_home.set_title("St. George InIT Home");
return app->run(win_home);
}
win_home.cpp:
#include "win_home.h"
HomeGUI::HomeGUI()
{
//build interface/gui
this->buildInterface();
//show_all_children();
//register Handlers
//this->registerHandlers();
}
HomeGUI::~HomeGUI()
{
}
void HomeGUI::buildInterface()
{
std::cout << "Building Portal Interface" << std::endl;
m_portal_rowbox = Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 5);
add(m_portal_rowbox);
Gtk::Button m_pia_button = Gtk::Button("Printer Install Assistant");
m_portal_rowbox.pack_start(m_pia_button, false, false, 0);
m_pia_button.show();
Gtk::Button m_inventory_button = Gtk::Button("Inventory");
m_inventory_button.show();
m_portal_rowbox.pack_start(m_inventory_button, false, false, 0);
m_inventory_button.show();
//add(m_portal_rowbox);
//m_portal_rowbox.show_all();
m_portal_rowbox.show();
this->show_all_children();
std::cout << "Completed Portal Interface" << std::endl;
return;
}
void HomeGUI::registerHandlers()
{
}
In void HomeGUI::buildInterface() you have constructed 2 buttons and they are added it to your box container. When the function returns the buttons are destroyed as they are now out of scope. Since they no longer exist they can not be visible.
So for you first button you would use something like this:
Gtk::Button * m_pia_button = Gtk::manage(
new Gtk::Button("Printer Install Assistant"));
m_portal_rowbox.pack_start(&m_pia_button, false, false, 0);
m_pia_button.show();
I expect you would need easy access to your buttons throughout the life time of your window. The easiest way is to have the buttons as a member of your class. It will be constructed as an empty button and you just need to set the label afterwards.
class HomeGUI {
....
// A button (empty)
Gtk::Button m_pia_button;
....
};
....
void HomeGUI::buildInterface()
{
....
m_pia_button.set_label("Printer Install Assistant");
m_portal_rowbox.pack_start(m_pia_button, false, false, 0);
m_pia_button.show();
....
}
The code I've written displays the sf::Drawable objects only for the top state of the state stack. Rendering works fine for everything, except the sf::Text type, that does not change the color of the text when button.getText().setFillColor(sf::Color:Red) is called. However, when I construct a button with a red text, whenever I try to set another color to that button, I only get a white text, no matter what color I request.
Here's where I change the color of a button:
void GameState_MainMenu::handleRealTimeInput()
{
for each (TextButton button in mButtons)
{
if (button.isSpriteClicked())
{
button.getText().setFillColor(sf::Color::Red);
button.triggerAction();
sf::Clock wait;
sf::Time timer = sf::Time::Zero;
timer = sf::seconds(0.15f);
while (wait.getElapsedTime() < timer)
{
}
wait.restart();
}
}
}
and this is my Game::render() method:
void Game::render()
{
GameState *currentState = getActiveState();
if (currentState != nullptr)
{
mWindow.clear();
currentState->draw();
}
mWindow.display();
}
Lastly, this is the draw method of the MainMenu state:
void GameState_MainMenu::draw()
{
game->mWindow.draw(game->mBackground);
game->mWindow.draw(mSelector.mSprite);
for each (TextButton button in mButtons)
{
game->mWindow.draw(button.getText());
}
}
It's probably because you have a while loop in the GameState_MainMenu::handleRealTimeInput that the program is getting stuck in.
You can try to use threads, though that way could get pretty messy. I suggest revising your code.
Okay, so I figured out this had something to do with c++'s for each instruction. As soon as I switched to the classic array-like traversal, my buttons started changing colors. I'm not saying this is THE solution, just that it worked for me. If anyone has the same problem, you might want to check that.
I have made a game in OpenGL, and also have added a menu item. when I right click on the OpenGL Screen, the menu item is displayed and I have added an option "Reset Game" in it. How Can I clear all the variables involved in the game by clicking on this (Any clear or flush function?).
Here is my code
glutCreateMenu(menu);
glutAddMenuEntry("Reset Game", 1);
void menu(int item)
{
switch (item)
{
case 1:
{
//Adding a function here to clear all the variables
}
break;
}
}
You just need to code up the routine to reset all your variables to their default values.
void Reset()
{
score = 0;
lives = 3;
// etc.
}
OpenGL doesn't know what these are so you have to do it yourself.
when I am programming with "gtkmm", there is a widget "Gtk::DrawingArea".
I can program that widget "by hand" (so write the code) or more elegant way is to use "glade" user interface designer, where I can do the same "graphically".
Now I am trying to connect OpenGL with gtkmm through "gtkglextmm" library. In that library, there is a widget "Gtk::GL::DrawingArea" - but this widget "IS NOT" available in glade.
So is there any way to program with "OpenGL + gtkglextmm" using "glade" (for the "graphical user interface" part)?
Thanks.
First of all libglade is an old library. If you are writing new project start with gtk builder.
As you can see here gtkmm provide easy way to create your own widgets and see them (almost) in glade tool. You simply insert plain DrawinArea widget to window and then tell gtk-builder to put in this place yours derived class.
Here is all together:
Setting up gtk-builder:
refBuilder = Gtk::Builder::create_from_file(ui_file);
GlDrawingArea*glArea = NULL;
refBuilder->get_widget_derived("drawing_gl",glArea);
Opengl DrawingArea class:
class GlDrawingArea : public Gtk::DrawingArea ,
public Gtk::GL::Widget<GlDrawingArea>
{
public:
GlDrawingArea(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder);
virtual ~GlDrawingArea();
protected:
void on_realize();
bool on_expose_event(GdkEventExpose* event);
bool on_configure_event(GdkEventConfigure* event);
private:
Glib::RefPtr<Gtk::Builder> refBuilder;
};
Constructing opengl drawingarea:
// GlDrawingArea:
GlDrawingArea::GlDrawingArea(BaseObjectType*cobject, const Glib::RefPtr<Gtk::Builder>& builder)
: Gtk::DrawingArea(cobject),
refBuilder(builder),
screen_tex(0)
{
//
// Configure OpenGL-capable visual.
//
Glib::RefPtr<Gdk::GL::Config> glconfig;
// Try double-buffered visual
glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB |
Gdk::GL::MODE_DEPTH |
Gdk::GL::MODE_DOUBLE);
if (!glconfig) {
std::cerr << "*** Cannot find the double-buffered visual.\n"
<< "*** Trying single-buffered visual.\n";
// Try single-buffered visual
glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB |Gdk::GL::MODE_DEPTH);
if (!glconfig) {
std::cerr << "*** Cannot find any OpenGL-capable visual.\n";
std::exit(1);
}
}
// print frame buffer attributes.
GLConfigUtil::examine_gl_attrib(glconfig);
//
// Set OpenGL-capability to the widget.
//
set_gl_capability(glconfig);
}