Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    All life is an experiment. The more experiments you make the better
    MPGH Member
    ExperimentalNex's Avatar
    Join Date
    Nov 2012
    Gender
    male
    Location
    Showcase
    Posts
    622
    Reputation
    10
    Thanks
    132
    My Mood
    Cool

    Post Drawing Crosshairs in DirectX 9

    NOTE: before we begin, if you are using d3d8, dont bother to read on, unless you have a wealth of knolage, and will be able to convert these functions into a d3d8 compatble form.

    Alright, first off you'll need to know the basis on how this works, its simple really. All that you really need to do is define the center of the screen, then offset boxes, lines, dots, circles, or w/e you want to make a neat design.

    Part 1. how to get the center of the screen:

    we declare our center X, and Y like this:
    Code:
    float CenterX=0.0f;
    float CenterY=0.0f; 

    now, with that, since the origin of the screen is NOT the center, if we tried to just use that, our crosshair would appear in the top left hand corner.

    [IMG]https://i1311.photobucke*****m/albums/s666/Albert_Leonz/syy1y9_zps955cec92.png[/IMG]
    [IMG]https://i1311.photobucke*****m/albums/s666/Albert_Leonz/zvbzip_zpsdfd6150e.png[/IMG]



    so, to make this work how we want it to, we need to tell directX where the center of the screen is... and we do that by taking the width and hieght of the WHOLE screen, and cutting it in half, to find the center.

    like this:
    Code:
    CenterX = ( float )pViewport->Width / 2;
    CenterY = ( float )pViewport->Height / 2;

    now, we have the screen's center's x and y, now we can begin drawing.

    part 2. drawing..

    now, drawing is where it all gets fun, because you can do just about anything.. its all up to you.

    for drawing there are a few common methods, we'll cover 2 of those today.

    2.1 drawing with DrawRect.
    i do not reccomend this, because you are limithed to straight lines, up and down, this greatly decreses your possibilities..regaurdless, ill show you how this works anyways..
    NOTE: drawrect does not support alpha in its stock state, im not sure if you can use alpha blending or not, but it would be a waste of time.

    ok, first up, you'll need the drawrect function:

    Code:
    void DrawRect(IDirect3DDevice9* dev, int x, int y, int w, int h, D3DCOLOR color)
    {
    D3DRECT BarRect = { x, y, x + w, y + h }; 
    dev->Clear(1, &BarRect, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, color, 0,  0); 
    }  

    now, you'll need to define a D3DCOLOR to use for your crosshair (one or more)


    Code:
    D3DCOLOR Red = D3DCOLOR_XRGB( 255, 0, 0 );  
    just declare it globally.

    now, we'll need something to tell our crosshair to draw, so just add a boolean like so:

    Code:
    bool UC_Crosshair = false;
    again, just in your globals.
    sone of you perfer intergers, as i do:

    Code:
    int crosshair = 0;
    that way we can switch beetween multiple crosshairs, but for the sake of this tutorial we'll use boolean.
    now we are ready to go.

    NOW! on to drawing our actuall crosshair..

    go into your endscene, and setup your crosshair like this:

    Code:
    HRESULT APIENTRY EndScene()
    {
    if( UC_Crosshair )
    {
    //draw our crosshair
    }
    our crosshair is now to be drawn... ive setup a few examples as follows of what you can do.

    first off is this, a very BASIC crosshair, with 2 lines.

    Code:
    DrawRect(dev,CenterX-20, CenterY,40,2,Red);
            DrawRect(dev,CenterX,CenterY-20,2,40,Red);  
    now, one would think that would work just fine, but in reality, we are forgeting the centerX and centerY are a pixel, so that code will look like this:[IMG]https://i1311.photobucke*****m/albums/s666/Albert_Leonz/1_zpsdf493a49.png[/IMG]

    one side is slightly longer then the other (im reffering to "side" as past the center of the screen)
    so to compensate the above code would need to look like this, to make both "sides" even:
    Code:
    DrawRect(dev,CenterX-20, CenterY,41,2,Red);
             DrawRect(dev,CenterX,CenterY-20,2,41,Red);  
    that would look like this:[IMG]https://i1311.photobucke*****m/albums/s666/Albert_Leonz/2_zps08d44945.png[/IMG]

    now to break down how that works:
    horizontal line:
    CenterX-20, means that the start point of the box is offset 20 pixels from the center of the screen. CenterY means that box starts ON the SAME verticle level as the center of the screen, now this can be troubling, because when you draw a 3 px taall box on the center Y of the screen, it goes down 3 px, so to compensate you must draw said 3 px box at CenterY-1 (the 2 pixels would fall on: CenterY-1, CenterY, and CenterY+1) staying perfectly even. the 41, is the width of the box, the 2 is the hieght of the box, most use 1, but as per this tutorial I used 2 as an example to show that you do not generally want to use an even number because you cannot get it centered, only with 1 px.

    now, i'd rather not get into too much detail with drawrect, as it is a very BASIC function, so i will not show any more examples using drawrect.

    2.2 drawing with Lines.

    now, drawing with lines, is a much better and more efficient way of drawing anything... compared to using the standart drawrect function, and as an added bonus, you can draw lines, points circles and alot more, as well as using alpha.

    now, you'll need to have pLine working before you can draw anything, so you'll need to do a few things first:

    first off declare this globally so your application knows you are using pline:
    Code:
    ID3DXLine *pLine = NULL;  
    you'll need to initialize your pLine like this:
    Code:
    D3DXCreateLine( dev, &pLine); 
    now, you'll also want to prevent crashing when using Pline so in your prereset add this:
    Code:
    pLine->OnLostDevice();  
    and in your postreset add this:
    Code:
    pLine->OnResetDevice();  
    that way if there is a problem you wont crash.

    out pLine is ready to go, but we'll need some functions first!

    first off, my version of a modified fillrgba: our box function:

    Code:
    void fillrgba(int x, int y, int w, int h,DWORD color)
    {
        D3DXVECTOR2 vLine[2];
    
        pLine->SetWidth( w );
        pLine->SetAntialias( false );
        pLine->SetGLLines( true );
        
        vLine[0].x = x + w/2;
        vLine[0].y = y;
        vLine[1].x = x + w/2;
        vLine[1].y = y + h;  
    
        pLine->Begin( );
        pLine->Draw( vLine, 2, color );
        pLine->End( );
    } 
    our point function, for drawing a single pixel:

    Code:
    void DrawPoint(float x, float y, DWORD color)
    {
        fillrgba((int)x, (int)y, 1, 1, color);
    }  

    just a easy way to use the fillrgba function.

    Code:
    void DrawLine(float x, float y, float x2, float y2, float width, DWORD color)
    {
        D3DXVECTOR2 vLine[2];
        pLine->SetWidth( width );
        pLine->SetAntialias( false );
        pLine->SetGLLines( true );
        vLine[0].x = x;
        vLine[0].y = y;
        vLine[1].x = x2;
        vLine[1].y = y2;
        pLine->Begin( );
        pLine->Draw( vLine, 2, color );
        pLine->End( );
    } 
    just used to draw a line from point a to point b, VERY usefull, probably the most usefull function yet.

    now we have all our functions layed out, we'll need to talk about colors again...

    when we defined colors for drawrect, we used this:
    Code:
    D3DCOLOR Red = D3DCOLOR_XRGB( 255, 0, 0 );

    now, thats all fine and dandy, but it used "XRGB" so there is ONLY red, green, and blue. the alpha value is always set at 255, so for defining transparent colors we'll use:
    Code:
    D3DCOLOR redtz = D3DCOLOR_RGBA( 255, 0, 0, 100 );
    now our color will be transparent. on to drawing!

    Now, like before, we'll need something to tell our crosshair to draw, so just add a boolean like before:
    Code:
    bool UC_Crosshair = false;  
    or ian interger, whatever you perfer
    Code:
    int crosshair = 0;
    go into your endscene again, and make a home your crosshair :

    Code:
    HRESULT APIENTRY EndScene()
     {
     if( UC_Crosshair )
     {
     //draw our crosshair
     }  
    now, we get to the fun and creative part!
    we'll start off with a simple crosshair, like before:

    Code:
    fillrgba(CenterX-20, CenterY, 41, 2,Red);
    fillrgba(CenterX, CenterY-20, 2, 41,Red);  
    itll look like this:
    [IMG]https://i1311.photobucke*****m/albums/s666/Albert_Leonz/3_zps6702755c.png[/IMG]

    as you can see the transparency makes a big difference!

    now, ill show you an example using a 1px fillrgba box, and a drawpoint

    Code:
    fillrgba(CenterX-20, CenterY, 17, 1,Red);
    fillrgba(CenterX+3, CenterY, 17, 1,Red);
    fillrgba(CenterX, CenterY-20, 1, 17,Red);
    fillrgba(CenterX, CenterY+3, 1, 17,Red);
    DrawPoint(CenterX,CenterY,Red);
    that come out looking like this:
    [IMG]https://i1311.photobucke*****m/albums/s666/Albert_Leonz/4_zps9912c9fa.png[/IMG]

    why? because we need to remember that center pixel
    i wont waste your time by showing that same example but fixed up, because it is just that, a waste of time. so lets move on to alex's drawline function, we'll draw a simple "X" crosshair:
    Code:
    DrawLine(CenterX+10,CenterY+10,CenterX-10,CenterY-10,1,Red);
    DrawLine(CenterX-10,CenterY+10,CenterX+10,CenterY-10,1,Red); 
    [IMG]https://i1311.photobucke*****m/albums/s666/Albert_Leonz/5_zps445c4c98.png[/IMG]
    notice, i did not try to compensate for the center pixel, when using the drawline function, that is because there is no need, the draw line function lets you choose the start and end point of the line, all you need to do is offset it correctly, the drawline function is by far my favorite for drawing crosshairs.. now before i conclude, ill show you 1 more example.

    using both drawline and drawpoint;
    Code:
    DrawLine(CenterX+15,CenterY+15,CenterX+3,CenterY+3,2,Red);
    DrawLine(CenterX-15,CenterY+15,CenterX-3,CenterY+3,2,Red);
    DrawLine(CenterX+15,CenterY-15,CenterX+3,CenterY-3,2,Red);
    DrawLine(CenterX-15,CenterY-15,CenterX-3,CenterY-3,2,Red);
    DrawPoint(CenterX-1,CenterY-1,Green);  
    will come out looking like this:[IMG]https://i1311.photobucke*****m/albums/s666/Albert_Leonz/6_zps35e003f3.png[/IMG]

    again, there are so many possibilities, i hope that this could help some of you out, and feel free to post what you've come up with using this info =)


    this took a LONG time to write, so please dont flame me or anything, if you have something to say please be polite.

  2. The Following 7 Users Say Thank You to ExperimentalNex For This Useful Post:

    Falbere (12-31-2012),RotiHangus HC (12-30-2012),Scyphozoa (01-29-2013),Sercured (12-31-2012),Sky_____ (02-14-2013),Xiaokg (12-30-2012),xyolord98 (12-30-2012)

  3. #2
    Xiaokg's Avatar
    Join Date
    Nov 2012
    Gender
    male
    Location
    Your Bird
    Posts
    165
    Reputation
    10
    Thanks
    216
    My Mood
    Goofy
    Nice
    I am a selfish in earth but I not need water XD


  4. #3
    xyolord98's Avatar
    Join Date
    Nov 2012
    Gender
    male
    Posts
    310
    Reputation
    10
    Thanks
    276
    My Mood
    Devilish
    AMAZING !!!

    Join Date : 11 August 2012
    Proud To Be A MPGH Member
    Press Button If I helped
    [IMG]https://i622.photobucke*****m/albums/tt303/tbtyphoon883_zpsb36da07b.gif[/img]

  5. #4
    Dash9231's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Posts
    350
    Reputation
    10
    Thanks
    745
    My Mood
    Angry
    Totally cool
    I AM GM_Dash


    Press the button if I helped

    Selling blackshot ID ->https://www.mpgh.net/forum/124-sellin...ml#post8150136

    New link please check

  6. #5
    3xtsniping's Avatar
    Join Date
    Oct 2012
    Gender
    male
    Location
    Private :P
    Posts
    677
    Reputation
    24
    Thanks
    813
    My Mood
    Tired
    Your signature so funny XD




    I am here to help anyone, provided i am able to help them. Feel free to approach me anythime, but please PM i do not like visitor messages.

    (\__/) This is Owl.
    ( o.O) Copy and paste Owl into your signature.
    (")_(") To help him gain world domination.

  7. #6
    Hacker1809's Avatar
    Join Date
    Oct 2012
    Gender
    male
    Posts
    165
    Reputation
    10
    Thanks
    913
    My Mood
    Busy
    @ExperimentalNex can you teach me how to make sound pack
    Proud To Be A Member Of Mpgh /me

    If I Help You In Any Way Remember To Press The Thanks Button

    And Don't Ever Leach Any Thing From Me!!




  8. #7
    limsiyong123's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Location
    In my Bedroom.
    Posts
    14
    Reputation
    10
    Thanks
    1
    My Mood
    Aggressive
    Great Tutorial, bro. Imma try it out later!! It's just too good to be true

  9. #8
    Threadstarter
    All life is an experiment. The more experiments you make the better
    MPGH Member
    ExperimentalNex's Avatar
    Join Date
    Nov 2012
    Gender
    male
    Location
    Showcase
    Posts
    622
    Reputation
    10
    Thanks
    132
    My Mood
    Cool
    Quote Originally Posted by Hacker1809 View Post
    @ExperimentalNex can you teach me how to make sound pack
    Check Out My Thread - https://www.mpgh.net/forum/168-blacks...soundpack.html

  10. #9
    CheytacBoy__'s Avatar
    Join Date
    Nov 2012
    Gender
    male
    Location
    Next to You
    Posts
    404
    Reputation
    10
    Thanks
    793
    My Mood
    Stressed
    Nice Tutorial Buddy...
    [IMG]https://i924.photobucke*****m/albums/ad87/SwiftCase/cyanideharlem.gif[/IMG]

    Contact Me
    Private Message
    Visitor Message
    Banned ur mum pu$$y. UR bloody " hacks" are all leeched idiot

  11. #10
    danialterbaik's Avatar
    Join Date
    Oct 2012
    Gender
    male
    Location
    At your bed
    Posts
    141
    Reputation
    10
    Thanks
    538
    My Mood
    Goofy
    i hope you gonna be a math teacher
    PROUD TO BE MPGH MEMBER

    ADD ME ON FB Danial Ibrahim

    Left 4 dead weapon sound mod
    Left 4 dead 2 loby


    Dont Just see my thread thank me too ^_^

    Noobies still same

  12. #11
    Arcton's Avatar
    Join Date
    Sep 2011
    Gender
    male
    Location
    SomeWhere In Your Heart
    Posts
    2,108
    Reputation
    194
    Thanks
    2,631
    My Mood
    Stressed
    It's a tutorial but i doubt it'll be on sticky

  13. #12
    Janitor's Avatar
    Join Date
    Aug 2011
    Gender
    male
    Location
    MPGH Reports
    Posts
    16,255
    Reputation
    3259
    Thanks
    7,214
    Nice to see someone who understands shit about coding in this section , nice tutorial.

  14. The Following User Says Thank You to Janitor For This Useful Post:

    ExperimentalNex (12-30-2012)

  15. #13
    CooLElement19's Avatar
    Join Date
    Oct 2012
    Gender
    male
    Posts
    51
    Reputation
    10
    Thanks
    18
    My Mood
    Aggressive
    i cant understand..can u make video tutorial.. i want to make straight HS line
    Don't Forget to Press Thanks If i Help You

    Watch your language

  16. #14
    RotiHangus HC's Avatar
    Join Date
    Apr 2012
    Gender
    male
    Posts
    217
    Reputation
    10
    Thanks
    184
    My Mood
    Cool
    Nice tutorial . Gonna create one, if can lol

  17. #15
    Vehrdyn's Avatar
    Join Date
    Apr 2011
    Gender
    male
    Location
    House of house
    Posts
    8,543
    Reputation
    206
    Thanks
    5,531
    Great tutorial. In progress

  18. The Following User Says Thank You to Vehrdyn For This Useful Post:

    ExperimentalNex (12-30-2012)

Page 1 of 2 12 LastLast

Similar Threads

  1. [HELP]Drawing a crosshair
    By ♪~ ᕕ(ᐛ)ᕗ in forum C++/C Programming
    Replies: 23
    Last Post: 04-16-2011, 02:45 AM
  2. [Help] Drawing Crosshair
    By Lyoto Machida in forum Visual Basic Programming
    Replies: 7
    Last Post: 04-12-2011, 06:04 PM
  3. [Help]Drawing a custom crosshair[Solved]
    By ♪~ ᕕ(ᐛ)ᕗ in forum Visual Basic Programming
    Replies: 22
    Last Post: 01-19-2011, 07:04 AM
  4. [new source] drawing crosshair
    By undergroundhack in forum Programming Tutorials
    Replies: 2
    Last Post: 04-02-2010, 02:34 PM
  5. [Release] Advanced Red Dot Drawing System. (Crosshair)
    By IbeDrinking in forum CrossFire Hacks & Cheats
    Replies: 35
    Last Post: 03-06-2010, 05:36 PM