Tagged: C Toggle Comment Threads | Keyboard Shortcuts

  • Ali BaderEddin 6:48 am on December 22, 2009 Permalink | Reply
    Tags: C, , match, Regex, regular expression, xml comment   

    C# Regular Expression Helper 


    One component of an application I’m writing uses a lot of regular expressions. To be sure I was using the right regular expressions, I’ve created this simple tiny tool to help me learn and verify regular expressions against my input data. Nothing fancy, but it might help someone out there.

    For example, it took me a little while trying to find the proper regular expression to match all comments (including those that are not closed) in an Xml file.

    C# Regular Expression Helper

    Download exe or source. I recommend using this tool with this cheat sheet. It’ll definitely speed up the learning curve.

     
  • Ali BaderEddin 8:37 am on February 13, 2008 Permalink | Reply
    Tags: C, ceil, floor, , math.h, round   

    C Round Function 


    It’s really weird that the C math library (math.h) doesn’t support the round function. It only includes the floor function which rounds down a float to an integer (can also be done by implicit or explicit casting) and the ceil function which rounds the value up.

    For example,

    int x;
    
    x = floor(1.2);   //  x is set to 1
    x = floor(1.8);   //  x is set to 1
    x = (int)1.8;     //  x is set to 1 (Explicit Narrowing Conversion)
    x = 1.8;          //  x is set to 1 (Implicit Narrowing Conversion)
    x = ceil(1.2);    //  x is set to 2
    x = ceil(1.8);    //  x is set to 2
    

    The round function is supposed to round the float value to the nearest integer.

    x = round(1.2);    //  x is set to 1
    x = round(1.8);    //  x is set to 2
    

    This can be done adding a 0.5 to the value and then truncating it.

    x = (int)(1.2 + 0.5);  //  x is set to 1
    x = (int)(1.8 + 0.5);  //  x is set to 2
    

    We also have to take negative values into consideration by adding -0.5 and then truncating.

    x = (int)(-1.2 - 0.5);  //  x is set to -1
    x = (int)(-1.8 - 0.5);  //  x is set to -2
    

    Thus, here is the resulting C function:

    int round(double number)
    {
        return (number >= 0) ? (int)(number + 0.5) : (int)(number - 0.5);
    }

    Note that you might want to use long rather than int to include support for larger numbers and avoid integer overflow.

    That’s it, pretty much primitive, but fun!

    Enjoy!
    Ali B

     
    • davidhilton 6:12 pm on February 28, 2008 Permalink | Reply

      Two other options; just use floor(x+.5) or make a macro- #define round(X) floor((X)+.5)

    • alibad 6:38 pm on February 28, 2008 Permalink | Reply

      You are right. floor(x+.5) is just the same as (int)(x+.5). But you forgot to take negative values into consideration.

    • Paul 4:08 am on November 6, 2008 Permalink | Reply

      Interestingly, I even nedumala about it …

    • Cork 4:40 pm on September 5, 2009 Permalink | Reply

      Very useful – thanks. Also, I like davidhilton’s idea of turning your function into a macro.

    • atrujillo 3:26 pm on February 16, 2010 Permalink | Reply

      I like to see the code applied to rounding of number to the nearest decimal place or to adjustable decimal places.

      • alibad 7:04 pm on February 16, 2010 Permalink | Reply

        Someone has already shared this function in the comments to the CodeProject copy of this post. Enjoy :)

        EX:
        Round(1.234,2) = 1.23
        Round(1.234,0) = 1.0
        Round(123.4,-1) = 120.0

  • Ali BaderEddin 9:33 am on January 27, 2008 Permalink | Reply
    Tags: C, , , , , , viewport,   

    GLUI Subwindow Template 


    This article shows you how to create GUI controls for your OpenGL application and organize them into GLUI subwindows. In this article, we will take the source code from the previous article GLUI Window Template, and modify it so that our GLUI controls will be laid out inside two subwindows rather than in a single window. The reason is that having a separate window to contain our GLUI controls could be sometimes annoying for the user, as she or he will have to lose the focus on the OpenGL context window every time she or he wants to do an action. A nice way to avoid this is to place the GLUI controls directly into the GLUT OpenGL window by embedding them into a GLUI subwindow.

    This article can be used in the following ways:

    • Learn how to use GLUI subwindows
    • Understand the Viewport concept in OpenGl
    • Use the program as a template for your OpenGL applications that require GUI controls

    The image below shows how our controls were laid out into a single GLUI window in the previous article:

    GLUI Window

    The image below shows how our controls will be laid out into two separate subwindows placed on the left and the bottom of our main GLUT window:

    GLUI Subwindow

    Check it out, and let me know your feedback.

    Enjoy!
    Ali B

     
    • shule 10:09 am on January 23, 2010 Permalink | Reply

      hi,
      i need to learn something about GLUI_EditText.i want to write something on the screen.but it doesnt appear on the window what i write.still just inside box.this is the code..
      GLUI_EditText *edittext = glui_window->add_edittext_to_panel(op_panel, “Text:”, GLUI_EDITTEXT_TEXT, &text, EDITTEXT_TEXT, glui_callback);

      what should be to make appear on the window?

  • Ali BaderEddin 6:07 pm on January 25, 2008 Permalink | Reply
    Tags: C, control, , , , GUI, , ,   

    GLUI Window Template 


    This article describes in detail how to create your first GLUI window with some basic controls inside it, and provides you with a template for your OpenGL applications.

    When OpenGL applications get more complex, we need something more than a GLUT mouse, keyboard, and\or popup menus to interact with our OpenGL objects drawn on the window. GLUI gives us more flexibility by allowing us to add GUI components to interact with our OpenGL objects, such as buttons, check boxes, radio buttons, spinners, list boxes, lists, trees, file browsers, text fields, text areas, and the special controls: rotation and translation.

    This article can be used in the following ways:

    • Learn how to add GUI components to your OpenGL application in a very straight-forward and simple manner, through
      • Documentation
      • Interactive Program that displays to the user how every event is handled and classifies these events into GLUT and GLUI events.
      • Neat and commented Code that reflects the simplicity of the GLUI library
    • Learn some totally new controls in the GLUI library created specifically for graphical manipulation, such as the rotation and translation controls.
    • Use the code as a template for your OpenGL applications.

    Click below for a screen shot of what the program would look like when its run.

    GLUI Window Template

    Make sure you read the GLUT Window Template article as a prerequisite to this article. One important thing to note is that GLUI is a C++ library, which means that your code must be written in files with .cpp extension rather than .C, or otherwise the linker will complain.

    Check it out, and let me know your feedback.

    Enjoy!
    Ali B

     
    • R. Srivatsan 4:28 pm on June 28, 2008 Permalink | Reply

      Dear Ali,

      Thank you very much for this GLUI window template code. I could make it run in OpenGL Mesa in Fedora linux as well. It really gave me a quick start GUI which I am able to grasp very fast. The entire article was lucidly written, and fantastic!. Thanks to you again. It is very easy to learn further from this article.

      R. Srivatsan

      (Bangalore, India).

  • Ali BaderEddin 7:22 am on January 22, 2008 Permalink | Reply
    Tags: C, , , ,   

    GLUT Subwindow Template 


    This article describes in detail how to set up your first OpenGL GLUT window with multiple sub-windows, and provides you with a template for your OpenGL applications. It can be used in the following ways:

    • Understand GLUT sub-windows
    • Learn new GLUT event handlers
    • Use the code as template if you ever needed a Graphics program with multiple sub-windows

    Click below for a screen shot of what the program would look like when its run.

    glut_subwindow_template.jpg

    Make sure you read the GLUT Window Template article as a prerequisite to this article.

    Check it out, and let me know your feedback.

    Enjoy!
    Ali B

     
    • bushimports 10:30 am on June 11, 2010 Permalink | Reply

      Hey Ali,I Liked your multiple subwindow totorial,I altered it so I can change the size of the subwindows with the mouse sort of like a split window. I am building my own GUI toolkit from scratch but I don’t seem to be able to get focus
      on the GUI objects with the glut passive mouse if they are in a multiple subwindow setup. Do you know how to do that?
      A tutorial on something like that (with an example program)would be great. Also a tutorial on us using gl selection mechanism to heirarchial picking on points,lines & polygons in 3d(with a good example program).I am also building a 3d modeler/editor. Much Thanks, Jody Bush

c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: