Kenny77

Members
  • Content count

    366
  • Joined

  • Last visited

Community Reputation

164 Neutral

About Kenny77

  • Rank
    Member
  1. Simple Inheritance Question

    Awesome, thanks.
  2. Simple Inheritance Question

    I've been reading the book Sams Teach Yourself Cpp In An Hour A Day, and so far it has been amazing, but there's one part in the chapter on inheritance that it doesn't fully explain. // Using a derived object #include <iostream> using std::cout; using std::cin; using std::endl; enum BREED { GOLDEN, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB }; class Mammal { public: // Constructors Mammal() : itsAge(2), itsWeight(5) {} ~Mammal() {} // Accessors int GetAge() const { return itsAge; } void SetAge(int age) { itsAge = age; } int GetWeight() const { return itsWeight; } void SetWeight(int weight) { itsWeight = weight; } // Other methods void Speak() const { cout << "Mammal sound!" << endl; } void Sleep() const { cout << "Shh. I'm sleeping." << endl; } protected: int itsAge; int itsWeight; }; class Dog : public Mammal { public: // Constructors Dog() : itsBreed(GOLDEN) {} ~Dog() {} // Accessors BREED GetBreed() const { return itsBreed; } void SetBreed(BREED breed) { itsBreed = breed; } // Other Methods void WagTail() const { cout << "Tail wagging..." << endl; } void BegForFood() const { cout << "Begging for food..." << endl; } private: BREED itsBreed; }; int main() { Dog Fido; Fido.Speak(); Fido.WagTail(); cout << "Fido is " << Fido.GetAge() << " years old." << endl; system("PAUSE"); return 0; } What's going on with the constructors in the Mammal and Dog class (the ': itsBreed(Golden)')? Is there a name for this function/operator?
  3. SDL Coordinates

    Quote:Original post by Kylotan You return a temporary copy of the SDL_Rect, which you then take a reference to. This might work in many cases but is bad practice as you end up with references to data that has disappeared. You probably want to return a const reference to the actual member of the class. What do you mean by this? I ran my code through the debugger and under the Locals (Visual Studio) section in the treeview found two 'objects' (I'm guessing). There's the 'game' object I'm using to manage resources and states and the game loop, but for some reason all the variables in this node are just random values, or question marks. There's also a 'this' node, which shows all the legit game information. What is this 'this' object?
  4. SDL Coordinates

    I'm having some trouble with getting my images to draw properly. I have an Image class that gets passed the filepath, x and y position, and whether or not the image should be drawn. But if I pass it 100 for both the x and y, it draws at -100, -100. // Drawing the image SDL_BlitSurface( m_playGameButton->GetSurface(), &m_playGameButton->GetRect(), game->GetScreen(), NULL); // In the header SDL_Rect GetRect() { return m_rect; } // In the Image.cpp file Image::Image(char *filepath, int x, int y, bool visible) { m_filepath = filepath; m_rect.x = x; m_rect.y = y; m_visible = visible; m_image = IMG_Load(filepath); } I can't see anything wrong that would make this happen, unless there is something about coordinates in SDL that is doing this. EDIT: Also, I can't use: m_rect.w = m_image.w; For some reason this works (it doesn't draw with the other code): m_rect.w = m_image.w + m_rect.w; What I don't get is how this works, shouldn't m_rect.w be some random value from memory? This has no effect on my negative coordinates problem though. [Edited by - Kenny77 on February 23, 2009 8:28:53 PM]
  5. Learning Theory

    Is there any cheaper alternatives to the books in the sticky (starting your career as a composer-sound designer, I believe it's called)? I'm interested mainly in theory and counterpoint, and the For Dummies series doesn't really get into more advanced topics.
  6. Is This A Memory Leak?

    Quote:Original post by rip-off Quote: Why does the image only show up when I'm debugging (I'm in the debug configuration, and the image file is in the same folder as the project)? When you are not debugging, are you running the program in Release mode from the IDE, or running the program outside the IDE? The IDE will set the working directory of your executable to the project folder, so you must take steps to do the same. I'm running the program from the IDE. This happens no matter what the configuration is, if I'm not debugging it doesn't show up.
  7. Is This A Memory Leak?

    Quote:Original post by Twisol EDIT: Also, careful! You're assigning a char* (filepath) to another char* (m_filepath), so you're not actually copying the string, you're copying its address. This can be bad depending on what datatypes we're dealing with, and other factors. Use strcpy() or use std::string instead. Luckily, I think in the code I see you're okay, but it's still really dangerous. Imagine if you new'd a char* and assigned "logo.png" to it, then used it to create an image, and then deleted it. What happens with the Image? Well, its m_filepath no longer points to valid memory, but it doesn't know that. Very bad. This is why I shouldn't be going anywhere near pointers. :P Thanks for the heads up. One more question and I'll be on my way. Why does the image only show up when I'm debugging (I'm in the debug configuration, and the image file is in the same folder as the project)?
  8. Is This A Memory Leak?

    I initially wanted to use it to pass to SDL_BlitSurface(). I totally forgot I could use '&' in the parameters.
  9. Is This A Memory Leak?

    Quote:Original post by rip-off Quote: ... without hitting any breakpoints I set. Where did you set the breakpoints? Do you have any code that is called before main() - e.g. constructors for globals? I don't, the first thing that happens is a Game object is created. Image::Image(char *filepath, int x, int y, bool visible, int ID) { m_filepath = filepath; m_x = x; m_y = y; m_rect->x = m_x; // What!? m_rect->y = m_y; // m_visible = visible; m_ID = ID; m_image = IMG_Load(filepath); } When I commented out these lines (assigning m_rect) it ran fine. What would cause this? m_x and m_y are both declared as int's.
  10. Is This A Memory Leak?

    I'm using a GameEngine class for a window and resource manager, and I'm not exactly sure if this is creating a memory leak or not (or if I try to access it will it be void)? GameEngine::GameEngine(int windowWidth, int windowHeight, bool fullscreen, char *title) { SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); if (fullscreen) { m_screen = SDL_SetVideoMode( windowWidth, windowHeight, 0, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN); } else { m_screen = SDL_SetVideoMode( windowWidth, windowHeight, 0, SDL_HWSURFACE | SDL_DOUBLEBUF); } SDL_WM_SetCaption(title, 0); m_running = true; Image *intro = new Image("logo.png", 0, 0, true, 1); m_images.push_back(intro); } It's the last 2 lines, where I declare and add the intro Image to the vector of Images. I ask because my SDL appliction crashes immediately when I try to run it, without hitting any breakpoints I set. Note: This is just a temporary 'fix' while I find a better place to put resource loading (I'm open to suggestions).
  11. kid programming

    This is the first thing that comes to mind: http://en.wikipedia.org/wiki/Phrogram EDIT: It costs from $40-$70, DarkBASIC is made strictly for games, and is very easy to understand. http://www.thegamecreators.com
  12. I'm trying to think of a way to manage resources in my game, but the only thing I can come up with is to have a numeric ID for each resource then allow the Game class to manage them, and have the state classes to manipulate them (tell them when/where to draw, when to play/stop playing music). The only thing I can come up with is to use a C#-like dictionary (each resource gets a Key and a Value (ID and filepath)). Is there a struct or class like this for C++? And are there any better ways to organize and use the resources?
  13. Passing Arguments To Base Constructors

    Quote:Original post by Servant of the Lord Part of what's confusing you, probably, is that you probably declare all your variables like this: int myInt = 555; But you can alternatively declare many variables like this: int myInt(555); Once you know that, it's easier to see that m_breed(GOLDEN), is just initializing the variable 'm_breed' to a value of 'GOLDEN', just as 'myInt(555)' is declaring the variable 'myInt' to a value of '555'. Thanks, that's what it was. The book never mentioned that in either the variables section, or this inheritance section.
  14. I'm going through the book "Teach Yourself C++ in 21 Days 6th Edition", and the example if provides for passing arguments to base construstors is this: #include <iostream> using namespace std; enum BREED { GOLDEN, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB }; class Mammal { public: Mammal(); Mammal(int age); ~Mammal(); int GetAge() { return m_age; } void SetAge(int age) { m_age = age; } int GetWeight() { return m_weight; } void SetWeight(int weight) { m_weight = weight; } void Speak() { cout << "Mammal sound!" << endl; } void Sleep() { cout << "Shh, I'm sleeping." << endl; } protected: int m_age; int m_weight; }; class Dog : public Mammal { public: Dog(); Dog(int age); Dog(int age, int weight); Dog(int age, BREED breed); Dog(int age, int weight, BREED breed); ~Dog(); BREED GetBreed() { return m_breed; } void SetBreed(BREED breed) { m_breed = breed; } void WagTail() { cout << "Tail wagging..." << endl; } void BegForFood() { cout << "Begging for food..." << endl; } private: BREED m_breed; }; Mammal::Mammal() : m_age(1), m_weight(5) { cout << "Mammal constructor..." << endl; } Mammal::Mammal(int age) : m_age(age), m_weight(5) { cout << "Mammal(int) constructor..." << endl; } Mammal::~Mammal() { cout << "Mammal destructor..." << endl; } Dog::Dog() : Mammal(), m_breed(GOLDEN) { cout << "Dog constructor..." << endl; } Dog::Dog(int age) : Mammal(age), m_breed(GOLDEN) { cout << "Dog(int) constructor..." << endl; } Dog::Dog(int age, int weight) : Mammal(age), m_breed(GOLDEN) { m_weight = weight; cout << "Dog(int, int) constructor..." << endl; } Dog::Dog(int age, int weight, BREED breed) : Mammal(age), m_breed(breed) { m_weight = weight; cout << "Dog(int, int, BREED) constructor..." << endl; } Dog::Dog(int age, BREED breed) : Mammal(age), m_breed(breed) { cout << "Dog(int, BREED) constructor..." << endl; } Dog::~Dog() { cout << "Dog destructor..." << endl; } int main() { Dog Fido; Dog Rover(5); Dog Buster(6, 8); Dog Yorkie(3, GOLDEN); Dog Dobbie(4, 20, DOBERMAN); Fido.Speak(); Rover.WagTail(); cout << "Yorkie is " << Yorkie.GetAge() << " years old." << endl; cout << "Dobbie weighs " << Dobbie.GetWeight() << " pounds." << endl; return 0; } Is there a better way to picture this? I'm confused about the line: Dog::Dog() : Mammal(), m_breed(GOLDEN) { cout << "Dog constructor..." << endl; } It seems like there is a function called m_breed (the member variable) that accepts the BREED enum, but there isn't.
  15. Writing Past The End Of An Array

    Quote:Original post by M2tM When your program runs it requests memory from the OS which grants it. This answers it, thanks! Quote: Or your computer may attain sentience and share its enlightenment ushering in a new era of peace and prosperity for the human race. The behavior is undefined, it could do anything. I'll keep trying and see what I come up with.