Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C++ game programming

Name: Anonymous 2010-08-25 7:45

So I have a hopefully simple problem.

I have a mainWindow and Window class in my C++ project. For debugging purposes, I want to add 20 points to the player's score every second.

So, in my main loop, I do:


if(event.type == SDL_KEYDOWN)
{
            window.getGUI().addPoints(20);
}


The getGUI() is just a normal getter function of type GUI that returns the gui object from the Window class. However, it doesn't update the score correctly. If I do a direct gui.addPoints(20); in the Window class, the score does update correctly, so I'm assuming it's a problem with accessing the gui object from outside the Window class. Any ideas?

Name: Anonymous 2010-08-25 7:59

>>1
Um, it's probably because you're doing it on a keydown??

Name: Anonymous 2010-08-25 8:06

Yes, you just press a key every second.

Name: Anonymous 2010-08-25 8:10

Ok. Well are you repainting the screen when this happens?

Name: Anonymous 2010-08-25 8:12

Yes, I'm reprinting, and it DOES work correctly and displays when I directly do a gui.addPoints(20) in the Window class. However it doesn't when I access it from the mainWindow class.

Name: Anonymous 2010-08-25 8:27

>>5
Then what the fuck made you think the code you posted in >>1 was the relevant piece, as opposed to, say, the getGUI method?
Goddammit people like you piss me off.

Name: Anonymous 2010-08-25 8:29

>>6

I said how the getGUI method is just a simple getter.

GUI Window::getGUI() {
return gui;
}


Are you content now?

Name: Anonymous 2010-08-25 8:42

>>7
What is GUI? Is it a typedef to a pointer? You aren't copying your GUI by value now are you?

Name: Anonymous 2010-08-25 8:45

>>7

Found your problem.

GUI& Window::getGUI() {
    return gui;
}


You're welcome.

Name: Anonymous 2010-08-25 9:31

>>1
For future reference, use DISALLOW_COPY_AND_ASSIGN on all your classes:

http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml?showone=Copy_Constructors#Copy_Constructors

Name: Anonymous 2010-08-25 10:26

here is your problem:
  window.getGUI().addPoints(20);
you are using dont notation to call class method on another class method

methods dont contain methods in C++, this is not Lisp

Name: Anonymous 2010-08-25 10:32

>>11
mistake: you are using *dot notation

Name: Anonymous 2010-08-25 10:38

>>10
There's only one thing I don't agree with in that link:

you can store pointers rather than objects in an STL container
This is only valid for Google because they disable exceptions. It isn't exception-safe to do this.

Your best bet if you want to do this is to enable C++0x and use unique_ptr<>. If you can't, add Boost and use the pointer containers. If you can't use Boost, use shared_ptr<>. And if you don't have access to even that, well, you're fucked. You've got to write your own smart pointer or pointer container, and you're probably going to introduce bugs into it.

Name: Anonymous 2010-08-25 11:27

>>11,12
Please learn C++. It is perfectly valid to use a method of a method if it returns a class.

Name: Anonymous 2010-08-25 11:47

>>13
Exception-safe?  The program will terminate shortly is an exception is thrown, all the memory will be released when the program terminates.  In before misuse of exceptions.

Name: Anonymous 2010-08-25 11:51

All you need to do is re-write it in an algorithmic language.

Name: Anonymous 2010-08-25 15:19

>>15
WTF are you talking about? When people program with exceptions, they tend to, you know, *catch* them. If an exception passes through an STL container that is shuffling pointers around, it may leak memory.

Name: Anonymous 2010-08-25 15:27

>>15
Scary.

Name: Anonymous 2010-08-25 16:25

>>17
Catch it, and then exit(2).

Name: Anonymous 2010-08-25 20:23

>>17
STL containers in most modern Standard C++ Library implementations are exception safe and can roll back when an element's constructor, copy-constructor, or assignment operator throws an exception when moving elements around.

When an exception is thrown from the ctor of an object in a container though, you generally have a bigger problem on your hands and you'll need to fix it.

Name: Anonymous 2010-08-25 21:44

>>20
when an element's constructor, copy-constructor, or assignment operator throws an exception when moving elements around.
None of which happen when you put pointers in it. Way to completely miss the point.

Name: Anonymous 2010-08-26 6:25

>>21
>put pointers in it
>miss the point

Oh you.

Name: Anonymous 2010-12-24 1:18

Name: Anonymous 2011-02-03 4:31

Don't change these.
Name: Email:
Entire Thread Thread List