[HELP]Crosshair

Posts 1–11 of 11 · Page 1 of 1
[HELP]Crosshair
Thanks to Hell_Demon for this code to make a simple crosshair but atm its wrking fine although it does flicker alot when trying to play a game eg CA. Im wondering if you could possibly make the crosshair flicker less? But im not sure how.

Code:
#include <windows.h>
#include <iostream>
#include <math.h>
using namespace std;

bool crosshairon=false;
HDC ragedc = NULL;
int crosshairsize=0;
int cx=0;
int cy=0;

void CrossThread(void)
{
	while(1)
	{
		if(GetAsyncKeyState(VK_NUMPAD0)&1)
		{
			crosshairon=!crosshairon;
			ragedc = GetDC(HWND_DESKTOP);
			cx=GetSystemMetrics(SM_CXSCREEN)/2-((crosshairsize-1)/2);
			cy=GetSystemMetrics(SM_CYSCREEN)/2-((crosshairsize-1)/2);
		}
		Sleep(1);
	}
}

int main()
{
	cout<<"Crosshair size in pixels:\n";
	cin>>crosshairsize;
	if(crosshairsize%2==0)
	{
		crosshairsize+=1; 
	}
	system("cls"); 
	cout<<"Press numpad0 to toggle the crosshair on and off\n";
	CreateThread(0,0,(LPTHREAD_START_ROUTINE)CrossThread,0,0,0);
	while(1)
	{
		if(crosshairon==true)
		{
			for(int i=0;i<crosshairsize;i++)
			{
				SetPixel(ragedc, cx+i, cy+((crosshairsize-1)/2), RGB(255,0,0));
				SetPixel(ragedc, cx+((crosshairsize-1)/2), cy+i, RGB(255,0,0));
			}

			if(crosshairon==false)
		
			for(int i=1;i<crosshairsize;i++)
			{
				SetPixel(ragedc, cx+i, cy+((crosshairsize-1)/2), RGB(0,0,0));
				SetPixel(ragedc, cx+((crosshairsize-1)/2), cy+i, RGB(0,0,0));
			}
		}
		Sleep(1);
	}
}
Thanks.
Take off sleep.

It would be best if you could hook the game's present function since then you can time the crosshair to display on each frame, but since you can't do that just try turning sleep off. It'll loop like crazy, but it might work.

If that doesn't work or lags too much. Try setting Sleep to 33 which should roughly match the 30 fps games usually render at.
Quote Originally Posted by why06 View Post
Take off sleep.

It would be best if you could hook the game's present function since then you can time the crosshair to display on each frame, but since you can't do that just try turning sleep off. It'll loop like crazy, but it might work.

If that doesn't work or lags too much. Try setting Sleep to 33 which should roughly match the 30 fps games usually render at.
Yea to add on to what Shaun said, when you Sleep(1) it might at sometimes render AFTER the frame renders. Hooking the present function would help, but since your function is just basic then you can try removing sleep, or turning it to ~30ish
Quote Originally Posted by Astral Witch View Post
to make windows applications in C++?

Tutorial: Getting Started
No sorry i didnt specify what i mean, I mean the Different types of crosshairs my bad.
Quote Originally Posted by @osma8 View Post
Thanks guys it wrks, I want to try and make it more advanced as in to make something like this : http://www.mpgh.net/forum/245-call-d...sshair-v2.html, Would it be possible to do this in C++?
Math math math. Use some creativity.
Snippet for making a circle:
Code:
    private final void circlePoints(int cx, int cy, int x, int y, int pix)
    {
        int act = Color.red.getRGB();
        
        if (x == 0) {
            raster.setPixel(act, cx, cy + y);
            raster.setPixel(pix, cx, cy - y);
            raster.setPixel(pix, cx + y, cy);
            raster.setPixel(pix, cx - y, cy);
        } else 
        if (x == y) {
            raster.setPixel(act, cx + x, cy + y);
            raster.setPixel(pix, cx - x, cy + y);
            raster.setPixel(pix, cx + x, cy - y);
            raster.setPixel(pix, cx - x, cy - y);
        } else 
        if (x < y) {
            raster.setPixel(act, cx + x, cy + y);
            raster.setPixel(pix, cx - x, cy + y);
            raster.setPixel(pix, cx + x, cy - y);
            raster.setPixel(pix, cx - x, cy - y);
            raster.setPixel(pix, cx + y, cy + x);
            raster.setPixel(pix, cx - y, cy + x);
            raster.setPixel(pix, cx + y, cy - x);
            raster.setPixel(pix, cx - y, cy - x);
        }
    }
I believe this is in C# so convert it to C++, not very hard, Just simple math. I did the same when I added it to my VB app.
Quote Originally Posted by master131 View Post
Math math math. Use some creativity.
Snippet for making a circle:
Code:
    private final void circlePoints(int cx, int cy, int x, int y, int pix)
    {
        int act = Color.red.getRGB();
        
        if (x == 0) {
            raster.setPixel(act, cx, cy + y);
            raster.setPixel(pix, cx, cy - y);
            raster.setPixel(pix, cx + y, cy);
            raster.setPixel(pix, cx - y, cy);
        } else 
        if (x == y) {
            raster.setPixel(act, cx + x, cy + y);
            raster.setPixel(pix, cx - x, cy + y);
            raster.setPixel(pix, cx + x, cy - y);
            raster.setPixel(pix, cx - x, cy - y);
        } else 
        if (x < y) {
            raster.setPixel(act, cx + x, cy + y);
            raster.setPixel(pix, cx - x, cy + y);
            raster.setPixel(pix, cx + x, cy - y);
            raster.setPixel(pix, cx - x, cy - y);
            raster.setPixel(pix, cx + y, cy + x);
            raster.setPixel(pix, cx - y, cy + x);
            raster.setPixel(pix, cx + y, cy - x);
            raster.setPixel(pix, cx - y, cy - x);
        }
    }
I believe this is in C# so convert it to C++, not very hard, Just simple math. I did the same when I added it to my VB app.
Yes it's C#. IDK for the others but for me it's kinda hard to convert. Sorry I'm a prooolly novice =P
Turn VSYNC on ingame for less flickering. Or lower max fps
lol, or you can use the win32 funtion Ellipse();


Winapi wins again, lol
Thats gotta be the worst circle code i've ever seen =3

Code:
for(int i = 0; i<360;i+=3)
{
    int x = cos(i/180*3.141592f) * radius + centerx;
    int y = sin(i/180*3.141592f) * radius + centery;
    SetPixel(x, y, RGB(255, 0, 0));
}
change the steps of i to make it smoother, radius should be obvious, nd centerx/y are just the center screen coordinates.
Posts 1–11 of 11 · Page 1 of 1

Post a Reply

Tags for this Thread

None

Talk with us