• 15
• 15
• 11
• 9
• 10
• ### Similar Content

• By suliman
Hi!
I have a two floats (pan.x and pan.y) which allows the player to move the view in my tile-based 2d-gameworld. I also have a "float scale" for zooming the world in and out.
For drawing most stuff it works fine. I just (where pos is the tile-position of something and #define TILE 20 is the size of a gametile at scale = 1):
drawStuff(pos.x * scale * TILE, pos.y * scale * TILE); When I change scale it draws to the correct position on screen. But when i draw the lines that shows the grid (all the tiles) they do not offset correctly. It works when scale = 1, but when I lower scale they become off. I tried so many variations but I've got stuck. What am I doing wrong?
int tile2 = TILE * scale; int offY = ((int)pan.y) % tile2; // draw horizontal lines for the grid for (int i = 0; i < h; i++) gfx.lineRelative(0, TILE*i*scale + offY, gfx.screenX, 0, col);

• Trying to get ideas here on how I could do this:
I have created a game for a university coursework assignment using a custom game engine (internal to my university, but it's based on OpenGL).
I know that I can use threading but am unsure how thread-safe the operation would be to init the game on another thread.

• I have a particle system with the following layout:
system / emitter / particle

particle is driven by particle data, which contains a range of over lifetime properties, where some can be random between two values or even two curves. to maintain a smooth evaluation between two ranges, i randomize a "lerp offset" on particle init and use that value when evaluating curves. the issue is that i'm using that same offset value for all properties (10ish) and as a result i'm seeing some patterns, which i'd like to remove. The obvious way is to just add more storage for floats, but i'd like to avoid that. The other way is to generate a seed of some sort and a random table, and use that to generate 10 values, ie: start with short/integer, mask it, then renormalize to float 0-1.

any other ideas?
• By dgi
Hey all ,
For a few days I'm trying to solve some problems with my engine's memory management.Basically what is have is a custom heap with pre allocated memory.Every block has a header and so on.I decided to leave it like that(not cache friendly) because my model is that every block will be large and I will have a pool allocators and stack allocators dealing with those blocks internally. So far so good I figure out how to place my per scene resources . There is one thing that I really don't know how to do and thats dealing with containers.What kind of allocation strategy to use here.
If I use vector for my scene objects(entities , cameras , particle emitters .. ) I will fragment my custom heap if I do it in a standard way , adding and removing objects will cause a lot of reallocations . If I use a linked list this will not fragment the memory but it's not cache friendly.I guess if a reserve large amount of memory for those vectors it will work but then I will waste a lot memory.I was thinking for some sort of mix between a vector and a linked list , where you have block of memory that can contain lets say 40 items and if you go over that number a new one will be created and re location of the data would not be needed.There would be some cache misses but it will reduce the fragmentation.

How you guys deal with that ? Do you just reserve a lot data ?

dgi
• By Hermetix
I am trying to setup the custom wizard for making a 3ds MAX 2018 plug-in (to export a character animation data), but I can't locate the wizard file folder to put the .vsz file in. In the 3ds MAX 2018 docs, it only mentions where the folder is in VS 2015 (VC/vcprojects). It's a VC++ project, but I don't see any folder in VC for the wizard files. I'm using VS 2017 update 15.5.6 Enterprise, and the folders in VC are: Auxiliary, Redist and Tools.

Thanks.

# C++ How would I throw a compiler error if the new function is used?

## Recommended Posts

I want to prohibit the use of new, delete in one of my projects. There are still instances where I need the placement new operator to construct things like containers which need a constructor call.

So I cannot just use the old #define new Error trick. I looked at static_assert but that will throw an error irrespective of whether the function is used or not. In addition, I can't use the #pragma deprecated because it only generates a warning and we have warnings in our project which are out of my control.

So is there any other way to throw a compiler error when a function is used? I am talking about the global new and delete operators.

Thank you

Edited by mark_braga

##### Share on other sites

Do you have to have a compile-time error? You can probably override global new and delete and not implement them (I think) which would at least give you a linker error. This also avoids the macro magic which would clobber placement new.

##### Share on other sites

Yeah but then you cant put a pretty message saying "Please dont use new"

##### Share on other sites

If you are OK with a warning instead of an error, you could use:

[[deprecated("because")]] void func(void)
{
}

Unfortunately attributes aren't unversally supported yet, so you'd have to fallback to vendor-specific implementations ie. for MSVC; which works quite similar though:

__declspec(deprecated("** this is a deprecated function **")) void func2(int) {}

##### Share on other sites
4 hours ago, mark_braga said:

I want to prohibit the use of new, delete in one of my projects. There are still instances where I need the placement new operator to construct things like containers which need a constructor call.

So I cannot just use the old #define new Error trick.

new and delete have pretty simple implementations (based off of malloc() and free(), but with constructor/destructor calls and a few other niceties). You could grab an implementation off the web, maybe even your own compiler's copy if the licensing permits, and give them different function names (e.g. "MyDeleteFunc()").

I just tossed this together and it compiled and ran fine, though I may have made a mistake somewhere:
(feel free to take it as your own, though at your own risk =P)

Spoiler

namespace Memory
{
template<typename Type, typename ...Args>
Type *Allocate(Args&& ...args)
{
//Allocate memory using 'malloc'.
Type *ptr = static_cast<Type*>(malloc(sizeof(Type)));
assert(ptr && "We're probably out of memory.");

//Manually call constructor, forwarding the arguments to the constructor.
new (ptr) Type(std::forward<Args>(args)...);

//Return the pointer.
return ptr;
}

template<typename Type>
void Deallocate(Type* &ptr) //Reference to a pointer. Huh, never had to use that before.
{
//Comment this assert out if you like the ability to pass null to 'delete'.
assert(ptr && "Trying to allocate an already NULL pointer.");

//Call the destructor manually.
ptr->~Type();

//Deallocate the memory.
free(ptr);

//Nullify the pointer (we got a reference to a pointer, so this is nulling the pointer that was passed into the function).
//Comment this line out if you want to do the nulling yourself.
ptr = nullptr;
}

} //End of namespace.


Usage:

MyClass *myClass = Memory::Allocate<MyClass>("Hello", 357);
Memory::Deallocate(myClass);

When some people reimplement new(), they also add things like leak detection and other features.

Edited by Servant of the Lord

##### Share on other sites

As I understand it, the point isn't to reinvent new, it's to prevent it from being used at all.

##### Share on other sites
25 minutes ago, ApochPiQ said:

As I understand it, the point isn't to reinvent new, it's to prevent it from being used at all.

Yea, I definitely misread it. I thought he was asking for a way to disable new for most of the code, but still make it available for his internal classes, so I was suggesting disabling and reimplementing under a new name.

However, he was asking for a way to disable new, but keep *placement* new.

Edited by Servant of the Lord

##### Share on other sites

Some googling turned up the pragmas "push_macro"/"pop_macro", which is compatible with Visual Studio, GCC, and Clang. This means you can do this:

//===========================================
#define new static_assert(false, "'new' is not available");

#pragma push_macro("new")
#undef new

template<typename Type, typename ...Args>
void priv_PlacementNew(Type *ptr, Args&& ...args)
{
new (ptr) Type(std::forward<Args>(args)...);
}

#pragma pop_macro("new")
//===========================================

Here's the Ideone test, which works how I think you are asking.

(or you can push_macro/undef/pop_macro manually each time you want to use placement new)

In practice, you'll want to define 'new' via a compiler parameter, or it won't cover your entire project.

Ofcourse, in real true practice, there's a limit to how far you should go to bubblewrap things to prevent programmers from shooting themselves, and I personally think this crosses that limit.

Edited by Servant of the Lord