Sign in to follow this  
blueshogun96

Windows Mouse Handling Sucks

Recommended Posts

blueshogun96    2264

In my opinion, of course...

My game not only needs to know if the mouse is currently out of the game's window, but also the position of the cursor relative to the window as it moves outside.  MacOS and Linux give me this by default, but for Windows, I had quite a bit of trouble finding a reliable way to do it.  This isn't the first time I've had issues dealing with Window's mousing handling.  It's been a pain in my arse multiple projects.

 

#if defined(_WIN32) && !defined(_WINRT)
	/* Because Windows mouse handling sucks in general compared to Mac/Linux, a little extra
	   work is required to find out if the cursor is outside of the game's window to prevent
	   cheating.  In my experience, the reliability of GetCursorPos and ScreenToClient vary
	   from one comp/OS to another, so a better idea is to check every frame and use those
	   resulting coordinates only if it tells us we are off screen.  No disrespect Microsoft,
	   but why do you insist on making such trivial things a pain in my behind sometimes? */

	struct vec2_t<float> bounds;

	This->get_bounds( bounds );

	POINT pt;
	GetCursorPos((POINT*)&pt);
	ScreenToClient(GetActiveWindow(), (POINT*)&pt);

	if( pt.x < 0 || pt.x > bounds.v[0] || pt.y < 0 || pt.y > bounds.v[1] )
	{
		This->mouse_move_func(pt.x, pt.y);
	}
#endif

 

So yeah, I can't believe it took me that long to get this stupid thing figured out.  Anyone else have the same problem before?  Anyone else come up with a better solution?  I'm just glad it works....

Shogun

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By Juliean
      So as the title (hopefully somewhat) says, I'm trying to achieve a spezialisation of a template class, based on whether or not the template-parameter is derived off of another (templated) class:
      // base class, specializations must match this signature template<typename Object> class ObjectSupplier { public: static constexpr size_t objectOffset = 1; static Object& GetClassObject(CallState& state) { return *state.GetAttribute<Object>(0); } }; // possible specialisation for all "Data"-classes, // which are actually derived classes using CRTP template<typename Derived> class ObjectSupplier<ecs::Data<Derived>> { public: static constexpr size_t objectOffset = 1; static Derived& GetClassObject(CallState& state) { return state.GetEntityAttribute(0)->GetData<Derived>(); } }; // ... now here's the problem: // this would work ... ObjectSupplier<ecs::Data<Transform>>::GetClassObject(state); // unfornately the actual object is "Transform", which is merely derived // of "ecs::Data<Transform>" and thus it calls the incorrect overload ObjectSupplier<Transform>::GetClassObject(state); The last two lines show the actual problem. I'm using this ObjectSupplier-class as part of my script-binding system, and thus its not possible/feasable to input the required base-class manually:
      template<typename Return, typename Class, typename... Args> void registerBindFunction(ClassFunction<Return, Class, Args...> pFunction, sys::StringView strFilter, std::wstring&& strDefinition) { using Supplier = ObjectSupplier<Class>; using RegistryEntry = BindFunctionRegistryEntry<ClassFunctionBindCommand<Supplier, Class, Return, Args...>>; registerClassBindEntry<RegistryEntry, Supplier, Return, Args...>(pFunction, false, strFilter, std::move(strDefinition), DEFAULT_INPUT, DEFAULT_OUTPUT); } registerBindFunction(&GenericObject::Func, ...); // needs to access the ObjectSupplier<ecs::Data<Derived>> overload registerBindFunction(&Transform::GetAbsoluteTransform, ...); // thats how it used to be before: registerObjectBindFunction(&GenericObject::Func, ...); // a few overloads that internally used a "EntityDataSupplier"-class registerEntityDataBindFunction(&GenericObject::GetAbsoluteTransform, ...); (well, it were possible, but I want this binding-code to be as simple as humanly possible; which is why I'm trying to not have to manually specify anything other than "I want to bind a function here").
      So, thats the gist of it. Any ideas on how to get this to work? I don't want to (again) have to create manual overloads for "registerBindFunctions", which there would have to be at least 5 (and all have a complex signature); but I'm certainly open to different approaches, if what I'm trying to achieve via template specialization isn't possible.
      Thanks!
       
    • By suliman
      Hi!
      I have two simple functions like this:
      bool within(int var, int min, int max){ if (var>=min && var<=max) return true; else return false; } bool within(float var, float min, float max){ if (var>=min && var<=max) return true; else return false; } Calling it like this gives me "Error    2    error C2666: 'within' : 2 overloads have similar conversions
      float foo = 0.4; if(within(foo, 0, 1)) bar(); I know i can specify constants to be float like so : (within(foo, 0f, 1f)) but i remember I earlier didnt have to do this and I use this ALL over in my projects and now suddenly has loads of compilation errors.
      Can I write this in some way that it just accepts (float, something, something) and that would use the second function? (the one that works with floats?)
      Thanks!
      Erik
    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By amadeus12
      I made my obj parser
      and It also calculate tagent space for normalmap.
      it seems calculation is wrong..
      any good suggestion for this?
      I can't upload my pics so I link my question.
      https://gamedev.stackexchange.com/questions/147199/how-to-debug-calculating-tangent-space
      and I uploaded my code here


      ObjLoader.cpp
      ObjLoader.h
    • By hyyou
      I have recently read T-machine's ECS data structure. (link)
      Its "best" version (4th iteration) can be summarized into a single image (copy from T-machine's web) :-

      This approach also appears in a VDO https://www.youtube.com/watch?v=NTWSeQtHZ9M  , with slide and source code ( https://github.com/CppCon/CppCon2015/tree/master/Tutorials/Implementation of a component-based entity system in modern C++ ) .
      There are many people (including me) believe that the above approach lacks data locality, because Position-1,Position-2,Position-3, ... are not stored in a contiguous array.
      However, many attempts failed to elegantly make it contiguous.  (e.g. stackoverflow's link) 
      I also realized that, to make it contiguous, the cost of query Entity<-->Component is unavoidably significantly higher. (I profiled) I would like to hear that ....
      Is such mega-array (T-machine's 4th) generally better (performance-wise) than storing each type component to its own contiguous array?  (like http://www.randygaul.net/2013/05/20/component-based-engine-design/ )  ? My references (those links) are quite old.   Now, are there any others better approaches? (If you happen to know) I know that its depend on data access pattern (i.e. cache miss).  I still want to hear more opinion/argument about it, because I may need to rewrite a large part of my engine to make it support 30,000+ entity (personal greed).    
      Thank.
       
       
  • Popular Now