PGui

When I tried to create a button for the main menu of my game, I get this error:

Unhandled exception at0x7855af68 in Zomboonies.exe: 0xC0000005:
Access Violation reading location 656b614d.

In this code:

void mainMenu(WindowFramework *window){
	PT(Texture) tex = TexturePool::load_texture("Menu-3.png");
	CardMaker cm("cardMaker");
	cm.set_frame(-1, 1, -1, 1);
	PT(PandaNode) readyCard = cm.generate();
	NodePath path(readyCard);
	path.set_texture( tex );
	path.reparent_to(window->get_render_2d());
	PT(PGButton) PlayButton = new PGButton("PlayButton");
	PlayButton->setup("PlayButton");
	PT(Texture) ButtonReady=TexturePool::load_texture("btn_play.png");
	PT(Texture) ButtonRollover=TexturePool::load_texture("btn_play_active.png");
	PT(Texture) ButtonPressed=TexturePool::load_texture("btn_play_pressed.png");
 
	// PGFrameStyle is a powerful way to change the appearance of the button:
	PGFrameStyle PlayBtnStyle = PlayButton->get_frame_style(0);
	PlayBtnStyle.set_type(PGFrameStyle::T_flat);
 
	PlayBtnStyle.set_texture(ButtonReady);    PlayButton->set_frame_style(0,PlayBtnStyle);
	PlayBtnStyle.set_texture(ButtonRollover); PlayButton->set_frame_style(1,PlayBtnStyle);
	PlayBtnStyle.set_texture(ButtonPressed);  PlayButton->set_frame_style(2,PlayBtnStyle);
	PlayBtnStyle.set_texture(ButtonReady); PlayButton->set_frame_style(3,PlayBtnStyle); 
	NodePath defbutNP = window->get_aspect_2d().attach_new_node(PlayButton);
	defbutNP.set_scale(0.1);
}

In particular, it highlights the following line:

CardMaker cm("cardMaker");

Which is weird, because without all the PGui code, it runs fine.

Can someone help me build a working menu? :frowning:

Hmm, I have to admit, I’m baffled. I don’t see anything obviously wrong there. Perhaps another part of your code is inadvertently corrupting memory?

David

The only other code is the main thread where the framework initialises and opens the window and loop.

Heres the only other code… :confused:

#include "pandaFramework.h"
#include "cardMaker.h"
#include "texture.h" 
#include "texturePool.h"
#include "pandaSystem.h"
#include "PGButton.h"
#include "PGFrameStyle.h"
 
PandaFramework framework;

//Load our task manager
PT(AsyncTaskManager) taskMgr = AsyncTaskManager::get_global_ptr();

enum controlMode{
	Menu,
	firstPerson,
	thirdPerson,
	Camera
};

controlMode curCtrlMode = Menu;
 
void changeControlMode(controlMode ctrl){
	curCtrlMode = ctrl;
}

//This is where the "mainMenu" function is.

int main(int argc, char *argv[]) {
    //open a new window framework
  framework.open_framework(argc, argv);
    //set the window title to My Panda3D Window
    //open the window
  WindowFramework *window = framework.open_window();
  mainMenu(window);
	
    // Run the engine.
    framework.main_loop();
    // Shut down the engine when done.
    framework.close_framework();
    return (0);
}

You’re not, by chance, using MSVS 2010, are you? We’ve had other reports of strange behavior by that compiler, particularly when used to compile against the Panda3D dll’s distributed here (which were built with MSVS 2008).

If you build your own Panda from scratch using makepanda, that shouldn’t be an issue.

David

Yeah, I’m using MSVS2010 :frowning:
I guess this means I gotta go download 2008 and start again? :frowning:

That’s one approach. Another would be to download Panda form source and build it with MSVS2010.

I’d guess the easier approach would be to download MSVS2008.

David

Would I get any major advantages from downloading Panda’s source instead of using msvs2008?

I don’t know. It depends on what you want to do. The standard advantages of having easy access to the source code in case you want to tinker under the hood, of course.

But as far as the code itself, no, there’s not a big difference between the source code on the trunk and the 1.7.0 version. If you’re not confident you can build the Panda source without headaches, then save yourself the bother and use the pre-built version.

David

Alright, well I’m using the pre-built version in msvs2008 and everything seems to be working so far.

Just a couple questions about PGUI:

  1. How do I get the button to scale itself according to the texture? I tried using Texture.X / Window.X with set_frame using ints, doubles and a float, but apparently 360/1024 = 0. :imp:

  2. Can I get rid of the default label/how do I get rid of it?

  3. If I can’t get rid of the label, how do I change the font colour etc? I tried manipulating button->get_text_node(), but there was no changes :frowning:

Thanks in advance. :smiley:

You can scale the button to whatever size you like. Texture.X / Window.X is a reasonable choice. 360/1024 = 0 in C++ (and in Python for that matter), but float(360) / float(1024) = 0.3515625.

If you want a custom button, then you should create the geometry you want to use to render it, in the four different states (ready, depressed, rollover, inactive), and pass those four NodePaths to PGButton::setup(). This will completely replace the default card with whatever you specify.

If you don’t want to go that far, you can change the properties you like on button->get_text_node(), but you have to do this before you call setup().

Oh, so I have to use float(x) to declare a float?
I was using float x = y.

(float)x is the C way to perform what’s known as a “cast”, to convert the expression x to the type float. This may be necessary in the middle of an expression if you want the expression to be computed using floating-point arithmetic instead of integer arithmetic.

David

Ok, so I got that working, but what is the most efficient way of moving a TextNode around the screen?

nodePath.set_pos(). Or a LerpInterval. Or a task. Or whatever. Panda provides lots of interfaces for moving nodes around.

David

Oh, true. I didn’t think to move the nodepath itself, I kept trying to add the label onto a card maker >_>

Is there a way I can stop buttons from adjusting their size to fit the text?

And for some reason, when I create a text node with a space in it the space is HUGE.

You can call button->set_frame() to the desired size after you have called button->setup(); or you can avoid calling button->setup() in the first place and just attach the desired geometry to each state_def instance yourself.

I don’t know why this would be. Are you scaling the font, instead of the node, or something?

David