New answers tagged

1

Your crashes is mostly because of improper handling of the object's ownership. When you add something to a collection, you should clearly understand who's owning the object. Is that handler owns the added objects now, and may delete those when time has come? Or is that handler only references the objects, and there's some other subsystem that will destroy ...


0

When in doubt, make sure you read the documentation for the functions you use. There are sometimes hidden gems and very useful tips there. In this case, we can see that there is a function called IMG_GetError() which should be used to retrieve a human readable message of the error you got. Using this, you'll be able to narrow down the possible sources of ...


0

Every time you call RenderScore, you leak an SDL_Texture by not calling SDL_DestroyTexture(Texture) before reallocating it with SDL_CreateTextureFromSurface.


1

I think this is more an opinion-based question but here are my thought (I'm not a C++ expert): Create a base class Tile that will handle generic stuff like rendering, animations if any, behavior of the tile... For each of your special tiles, create a new class that inherits from your base class Tile and override the behavior if your new tile has a custom ...


0

Is main.cpp the only cpp file in your program? It's better practice to have a main_menu.hpp and main_menu.cpp in your program instead of putting everything in the main.cpp. That being said I'm not sure what issue you are facing exactly. The general way to access all the 'variable's in your header is to #include it on top of your cpp file: #include "...


-1

To handle multiple events, you need to cache them, so they can be dealt with once per frame. You need an abstraction of a controller, such as a keyboard: class Keyboard { private: bool pressedBuffer[256]; bool heldBuffer[256]; public: void handleEvent(SDL_Event const& e) { if (e.type == SDL_KEYDOWN) { ...


0

You should look at C++ language concepts, such as passing by reference, address, and value. If I have a class, Foo, and another, Bar: class Bar { public: int num; }; class Foo { public: void doSomethingV(Bar bar); void doSomethingR(Bar& bar); void doSomethingA(Bar* bar); }; Look at the doSomething* member functions: the first one ...


1

Java is object oriented, that is, it makes heavy use of inheritance, but the syntax is a little different. In C++ we declare a base class, which can be viewed as an interface, thusly: class Base { public: Base(){} //simple constructor virtual void foo() = 0; //this is known as a pure virtual function virtual ~Base(){} //virtual destructor }; ...


1

In the future, make sure to write the error the compiler gives. In c++, for a function to work, you need (generally) two things: a declaration, and a definition. (In some situations, both can be combined.) However, here, void virtual tick() = 0; will only declare the function. You basically tell the world "I, the mighty gameobject, hereby declare this ...


0

Instead of showing you code, I will just outline an abstract way to perform AABB (axis aligned bounding box) vs AABB collision detection, that you can translate to any language. It may require some changing of how you store your character/tile data. First, construct your AABB (player & tile): cornersA = vec2[4]; halfwith = player.width/2; halfheight = ...


Top 50 recent answers are included