New, delete, and windows.h [RESOLVED *yes!*]

Posted By: the_mehmaster

New, delete, and windows.h [RESOLVED *yes!*] - 12/12/10 00:08

Hey there,

The following code works and is compiling properly in VS 2010 -- But my problem is that I would like to get it working in lite-c...

I'm not really sure where to start though, as lite-c does not support or have a lot of what I am using.. I don't think lite-c has the 'new' or 'delete' operators (Which I am using inefficiently, but I need them none the less). Also, many of the structs are not defined in windows.h.. along with the the integral function 'SendInput'..

How would I go about this task? any help is greatly appreciated smile

Code:
#include <windows.h>

void mouseMove(int dx, int dy)
{
   INPUT* input = new INPUT;
	 
   input->type = INPUT_MOUSE;
	 
   input->mi.dx = dx;
   input->mi.dy = dy;
   input->mi.mouseData = 0;
   input->mi.dwFlags = MOUSEEVENTF_MOVE;
   input->mi.time = 0;
   input->mi.dwExtraInfo = 0;
 
   SendInput(1, input, sizeof(INPUT));
   delete input;
}

int main(int argc, _TCHAR* argv[])
{
	while(1)
	{
		mouseMove(4,0);
		mouseMove(0,4);
		mouseMove(-4,0);
		mouseMove(0,-4);
	}
	return 0;
}



Some links:
SendInput
A forum post (3rd last post on that page)

Thanks in advance...
Posted By: MasterQ32

Re: New, delete, and windows.h - 12/12/10 00:12

you can try sys_malloc and sys_free

Code:
#include <windows.h>

void mouseMove(int dx, int dy)
{
   INPUT* input = sys_malloc(sizeof(INPUT));
	 
   input->type = INPUT_MOUSE;
	 
   input->mi.dx = dx;
   input->mi.dy = dy;
   input->mi.mouseData = 0;
   input->mi.dwFlags = MOUSEEVENTF_MOVE;
   input->mi.time = 0;
   input->mi.dwExtraInfo = 0;
 
   SendInput(1, input, sizeof(INPUT));
   sys_free(input);
}

void main()
{
	while(1)
	{
		mouseMove(4,0);
		mouseMove(0,4);
		mouseMove(-4,0);
		mouseMove(0,-4);
	}
	return;
}


Posted By: the_mehmaster

Re: New, delete, and windows.h - 12/12/10 00:20

Ah, I forgot about that! Thanks smile!

That solves half the problem.. The 'INPUT' struct and most of the members it contains are not in the default 'windows.h' though..

I went around copy-pasting all the bits from 'windows.h' that I needed.. But that didn't work very well (not that I expected it too, though grin)

How do you reckon I would go about gathering the pieces of 'windows.h' that I need?
Posted By: the_mehmaster

Re: New, delete, and windows.h - 12/12/10 07:18

Normally I would use SetCursorPos -- Because that is fully supported within lite-c

But that function does not work for me as it only works in some applications, and also I need relative as apposed to absolute coordinate input (although that part could be accomplished in code quite easily)

Does anyone else know how to do this? Maybe there is an easier way...


EDIT: Well in case anyone wants to know.. I'm using EvilSOB's fantastic serial library (and some more code that he helped me [rather -- he wrote for me XD] write for the IMU smile)to communicate to an external IMU for accelerometer + gyro data and such.. The reason I am doing this in lite-c is because it would take a while to port over to c++...

I want to make an accelerometer-gyro based headtracking thing for use in both acknex, and other games that I have.
Posted By: MasterQ32

Re: New, delete, and windows.h - 12/14/10 15:42

What's about GetCursorPos?
Code:
function move_mouserel(int x,int y)
{
    LPPOINT p;
    GetCursorPos(&p);
    SetCursorPos(p.x + x,p.y + y);
}


Posted By: the_mehmaster

Re: New, delete, and windows.h - 12/15/10 03:32

Thanks, but that's not really what I need..

The thing is, I need the function to not just effect desktop apps, but also full-screen games and such too. The only way I have managed to achieve that as yet is through 'SendInput'.

(I did try your code though BTW -- the 'LPPOINT' needs to only be a 'POINT', and it won't work in full-screen apps)
Posted By: the_mehmaster

Re: New, delete, and windows.h - 12/15/10 04:20

Hmm.. I looked in acknex's windows.h, and look what I found, around line 6945
Code:
long WINAPI VkKeyScan(long ch);
long WINAPI VkKeyScanEx(long ch,long dwhkl);
VOID WINAPI keybd_event(long bVk,long bScan,long dwFlags,long dwExtraInfo);
VOID WINAPI mouse_event(long dwFlags,long dx,long dy,long dwData,long dwExtraInfo);
//long WINAPI SendInput(long cInputs,long pInputs,long cbSize);
long WINAPI GetLastInputInfo(long plii);
long WINAPI MapVirtualKey(long uCode,long uMapType);
long WINAPI MapVirtualKeyEx(long uCode,long uMapType,long dwhkl);
long WINAPI GetInputState(long);
long WINAPI GetQueueStatus(long flags);
long WINAPI GetCapture(long);



It's already there! -- But it has been commented out for some reason.

None of the dependencies for it are there though, as I tried un-commenting it and calling it.. says I'm calling an 'empty prototype'

Any ideas?
Posted By: the_mehmaster

Re: New, delete, and windows.h - 12/15/10 04:24

Okay so I have now added
Code:
API(SendInput,user32)



And something real strange is happening.. nothing! ..

Program finishes compiling then the whole computer freezes
Posted By: the_mehmaster

Re: New, delete, and windows.h - 12/15/10 04:38

GREAT SCOTT

I think I have it!!

Program now does what I want it to .. Yay!!

For anyone else who wants to achieve a similar feat:

First you must go into acknex's windows.h, and uncomment 'long WINAPI SendInput(long cInputs,long pInputs,long cbSize);'

Also, 'API(SendInput,user32)' needs to be added there, and then this code will work:


Code:
#include <acknex.h>
#include <default.c>
#include <windows.h>

typedef unsigned long ULONG_PTR;


typedef struct tagMOUSEINPUT {
    LONG    dx;
    LONG    dy;
    DWORD   mouseData;
    DWORD   dwFlags;
    DWORD   time;
    ULONG_PTR dwExtraInfo;
} MOUSEINPUT;


#define INPUT_MOUSE     0

typedef struct tagINPUT {
    DWORD   type;
    MOUSEINPUT      mi;
} INPUT;

void mouseMove(int dx, int dy)
{
   INPUT* input = sys_malloc(sizeof(INPUT));
	 
   input.type = INPUT_MOUSE;
	 
   input.mi.dx = dx;
   input.mi.dy = dy;
   input.mi.mouseData = 0;
   input.mi.dwFlags = MOUSEEVENTF_MOVE;
   input.mi.time = 0;
   input.mi.dwExtraInfo = 0;
 
   SendInput(1, input, sizeof(INPUT));
   sys_free(input);
}

void main()
{
	level_load("");
	while(1)
	{
		mouseMove(4,0);
		wait(1);
		mouseMove(0,4);
		wait(1);
		mouseMove(-4,0);
		wait(1);
		mouseMove(0,-4);
		wait(1);
	}
}



Yes!!!

Thanks Richi007 for the help wink!!
Posted By: gri

Re: New, delete, and windows.h - 12/15/10 06:40




Thanx for sharing your solution
Posted By: EvilSOB

Re: New, delete, and windows.h - 12/15/10 09:00

Dude!

What does this demo do when you run it, and what are you running it with?

Im using A7.86.0 Comm and the mouse performs the little squares, but it also wanders off to the top-left of the screen...

Did it do that for you?!?


[EDIT] PS: I will be getting to your PM later tonite...

[EDIT 2] For what it is worth, here is a savagely cut down version of meh_masters code....
windows.h no longer requires, ans structs over-simplified...
(still has the drift problem on my machine...)
Click to reveal..
Code:
#include <acknex.h>
#include <default.c>


long __stdcall SendInput(long cInputs,long pInputs,long cbSize);
API(SendInput,user32)


typedef struct MOUSEINPUT
{
    long	type;				//unused : always ZERO for mouse
    long	dx, dy;			//move mouse by dx, dy pixels
    long	unused_0;		//unused
    long	dwFlags;			//static : always 0x1 for mouse-input 
    long unused_1[2];	//unused
} MOUSEINPUT;



void mouseMove(int dx, int dy)
{
   static MOUSEINPUT* input = NULL;	
   if(!input)	{ 	input = sys_malloc(sizeof(MOUSEINPUT));	
   					memset(input, 0, sizeof(MOUSEINPUT));
   					input.dwFlags = 0x1;							}
	//-----------------------------------------------------
   input.dx = dx;
   input.dy = dy;
   SendInput(0x1, input, sizeof(MOUSEINPUT));
}




void main()
{
	level_load("");
	while(1)
	{
		mouseMove(40,0);
		wait(1);
		mouseMove(0,40);
		wait(1);
		mouseMove(-40,0);
		wait(1);
		mouseMove(0,-40);
		wait(1);
	}
}



Posted By: the_mehmaster

Re: New, delete, and windows.h - 12/15/10 10:22

Yeah the code does exactly the same thing for me smile

But that doesn't really matter as that's all it was supposed to do anyway.. I have already integrated it with the IMU code that we had earlier, and it works perfectly.. Using the IMU to control crysis is pretty cool wink

EDIT: I integrated your code.. looks a lot more efficient, and everything still works fine. Thanks! smile
Posted By: EvilSOB

Re: New, delete, and windows.h - 12/15/10 11:20

excellent!
Posted By: WretchedSid

Re: New, delete, and windows.h - 12/15/10 12:20

Question: Why malloc at all? Thats a serious performance penalty when you call this multiple times as its dispatched in many other functions including a context switch to the kernel and memory mapping.
The code from EvilSOB is better, but leaks the memory. In the end it could be done perfectly on the stack.
Posted By: the_mehmaster

Re: New, delete, and windows.h - 12/15/10 20:53

The site (listed in the first post) that I got most of the original code from only used the stack.. But for some reason that method would not compile.. so I just used malloc grin

But for the purposes I am using it for.. Performance is pretty much irrelevant unless the program is REALLY slow (I'm talking ~ 5fps) It currently runs at and around 400(with a level and entities+such).

As for evilSOB's code... I think I'll leave that to him to explain grin
© 2024 lite-C Forums