Dec 20, 2012

How to display a score(variable value) in opengl ?






It was a big task when I started using opengl to design a 3 D game to display a score board based on a variable. Most of my friends thought it would be a tough task to display a scoreboard which will be ticking each and every second of the game. Then I thought of generalizing the task and wanted to utilize my knowledge in basic C to code for the scenario.

The problem with displaying characters in opengl was that, One can only print just single character at a time on to the screen using the command

--> -->

GLvoid *font_style1 = GLUT_BITMAP_TIMES_ROMAN_24;

glRasterPos3f (0.0, 0.0, 0.0);

glutBitmapCharacter(font_style1, ch);


Here, the first line sets the Font type and its size. The second line adjusts the position of the character. Third line plots the character on to the screen, where 'ch' is the ascii value of required character. 

So here arises the biggest question:
How can one find the ascii value of the required score and then print ?
Solution is simple:
Split the digits of the score into single digits. Then take the value and add it to 48, which gives the ascii value of that digit.Print the digit, adjust the next character position, find the next digit , repeat the same procedure.

So code wise we can say as,
void scoredisplay (int posx, int posy, int posz, int space_char, int scorevar)
{
        int j=0,p,k;
        GLvoid *font_style1 = GLUT_BITMAP_TIMES_ROMAN_24;
       
        p = scorevar;
        j = 0;
        k = 0;
        while(p > 9)
        {
            k = p % 10;
            glRasterPos3f ((posx-(j*space_char)),posy, posz);   
            glutBitmapCharacter(font_style1,48+k);
            j++;
            p /= 10;
        }
            glRasterPos3f ((posx-(j*space_char)), posy, posz);   
            glutBitmapCharacter(font_style1,48+p);
      
}
here, the Scoredisplay() have folowing arguments
   initial X position(Extreme right)
   initial Y position
   initial Z position
   space in between each digits
   variable which need to be displayed
Now what ever the value on the variable, the score board will be shown at the required X,Y,Z co-ordinates. I searched net a lot to find this sort of code during my project,to get a hint of how to display my scoreboard, but was disappointed. So when I was successful in implementing this on my project and many of my friend's project, I thought of sharing with you all.









Dec 19, 2012

A simpler way to custom your files in ubuntu




I usually work with lot of files related to my project,  majority of them being .c or .cpp files. I always have to right click,open new document file and then type all usual headers like
#include<stdio.h> 
#include<string.h> 
#include<iostream> 
using namespace std;
. . . 
This used to make things boring. Then one of my friends shared a new way to custom your templates. Just thought of shring with you all. Its a kind of common pattern used my professionals to make their files look elegant and customized.    There has been a folder called templates in ubuntu(/home/user/Templates). There you can create your custom templates.   

Step 1   Right click there to create a new file and type what all you need for your frequent usage.    

Step 2   Now save the file  as required name and extension.     
Now see the magic. Just right click to create a new document(You can right click where ever you wish,not only the document folder as shown). You will be surprised to see your custom made file too, there in the list of Create New Document.     
So simply select it and you can have all your frequent code part typed in it already.  

So similarly, if you want many different varieties of customized files, repeat the same and save them as new names. This can really help one in easy coding as well as in customized manner. Then you needn't want to paste your name and the frequent codes into project files again and again. Hope you all will like this way of customization.