Thread: Widgets with OpenGL, DirectX or SDL?

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    Widgets with OpenGL, DirectX or SDL?

    I want to make a game loosely based on the Warhammer Fantasy tabletop gaming system. It will be 2D, with mostly rectangles and a few circles, the hardest stuff will probably be in rotating the rectangles for charges and such.

    There will also be a lot of windows forms type elements, like buttons, tables, drop down and combo boxes.

    I've decided to use good ol' C++, and I'm trying to decide on the graphics API/system to use. SDL looks like the easiest, but how hard is it to do things like creating buttons and other widgets? Would it be hard to introduce an object oriented event system (ie Button objects with OnClick events)?

    I also looked a bit at DirectX and OpenGL, maybe it'd be easier with them, as I heard they can use windows forms controls with the winapi.

    Which do you think would be easier?

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    DirectX. But it's a matter of preference.

    Rotating the rectangles is easy in Direct3D.

    1. Simply create your 2 tris to form the quad or rectangle.
    2. Call D3DXMatrixTranslation() with the appropriate translation factors.
    3. Call D3DXMatrixRotationYawPitchRoll() with the appropriate angles.
    4. Concatenate rotation matrix and translation matrix (rotate first or your rotation will be relative to your translation - like a plane orbiting the sun).
    5. Set transform to world transform.
    6. Set the texture for the object.
    7. Setup and needed render states.
    8. Draw the primtive.


    Here is a sample without the class definition and without the actual vertex setups. This code shows how to load the texture and render the object based on its position and rotation angles.

    Code:
    <snip>
    ...
    ...
    void Object::SetTexture(std::string File)
    {
    if (FAILED(D3DXCreateTextureFromFile(&Device,File.c_str(),Texture)))
    {
    ::MessageBox(0,"Failed to load object texture",0,0);
    return;
    }
    }
     
     
    void Object::Render(D3DXVECTOR3 CameraPos)
    {
    //Setup translation matrix
    D3DXMATRIX trans;
    D3DXMatrixTranslation(&trans,CameraPos.x,CameraPos.y,CameraPos.z);
     
    //Setup rotation matrix
    D3DXMATRIX rot;
    D3DXMatrixRotationYawPitchRoll(&rot,Rotation.x,Rotation.y,Rotation.z);
     
    //World matrix is concatenation of rotation and translation matrix
    D3DXMATRIX world;
    world=rot*trans;
     
    //Set Direct3D to use world as transformation matrix for this object
    Device->SetTransform(D3DTS_WORLD,&world);
     
    //Set texture for this object
    Device->SetTexture(0,Texture);
     
    //Draw the textured quad.
    Device->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2);
    }
    I leave the rest as an exercise for you. This is an example in 3D. But you can use it for 2D as well. Just assign a constant z value greater than 1.0f (prob greater than 10.0f to make sure they are not z clipped) to all your vertexes. You can also look into using the ID3DXSprite interface.
    Last edited by VirtualAce; 09-25-2004 at 11:36 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Going from DirectX to OpenGL
    By Wraithan in forum Game Programming
    Replies: 19
    Last Post: 02-24-2006, 11:07 AM
  2. Allegro, OpenGL.. or even DirectX?
    By Zeusbwr in forum C++ Programming
    Replies: 1
    Last Post: 11-14-2004, 08:16 AM
  3. DirectX or SDL?
    By Leeman_s in forum Game Programming
    Replies: 2
    Last Post: 01-19-2003, 10:13 PM
  4. OpenGL 2 or DirectX ?
    By alex6852 in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 01-02-2003, 02:31 PM
  5. So.. what's the difference between DirectX and OpenGL?
    By QuestionC in forum Game Programming
    Replies: 6
    Last Post: 01-19-2002, 06:18 PM