Archive

Archive for the ‘C/C++’ Category

New site content

December 15th, 2009

Hi all,

I’ve just uploaded two new two new projects to the site.

The first is a little physics simulation I did last week.

The second is an application framework for DirectX that provides a basic scene along with camera setup and manipulation, logging, mesh loading and rendering support and direct input support.

You can get them in my downloads section of the site.

Cheers,
Eddie

admin C/C++

Setting up a basic OpenGL project on Mac OS Leopard and XCode

October 11th, 2009

I’ve recently setup a basic OpenGL project in XCode to have a black window.

In XCode select File->New Project. Select an Empty Project and name it whatever you want, screenie below:




newproject




Within XCode select Project->New Target again. Select Cocoa on the left then Application and name it whatever you’d like.




targetapp




You will now see a window with a whole bunch of settings. In the General tab press the + button in Linked Libraries. Add OpenGL.Framework and Glut.framework from the list of frameworks in the dialog that appears.




generalwindow




Next select Build in the tabs along the top and clear the bottom field GCC_PREFIX_HEADER so that its blank.




targetsettings




In the Groups and Files explorer right click on the top level project and select Add->New File. Choose C and C++ and select C file. Again choose whatever name you wish.




candc_





Paste the following code into the C file:

[code lang="C"]
#include
#include

void myCustomDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glutSwapBuffers();
}

void myCustomReshape(int width, int height)
{
glViewport(0, 0, width, height);
}

void doSomethingWhenIdle(void)
{
glutPostRedisplay();
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(800, 600);

glutCreateWindow("My First Window");

glutDisplayFunc(myCustomDisplay);
glutReshapeFunc(myCustomReshape);
glutIdleFunc(doSomethingWhenIdle);

glutMainLoop();
return EXIT_SUCCESS;
}
[/code]

Next Build the application using ‘Build and Go’ and you’ll be presented with a 800×600 empty window with the title ‘My First Window’, easy! I won’t go into the explanation of the different functions, there are plenty of sites out there for that!

edlong C/C++

Display correct text colour on a background colour

September 10th, 2009

Hi,

I haven’t posted for a bit, back to some handy C++. I’ve been working with a lot of text being displayed on a background which can be various colours. Not knowing the background colour is in advance is problematic as if you specify the text colour to be always white and the background colour changes to white then the text is unreadable. Likewise for black and there are a range of colours in between that suffer with the same problem, light grey or dark purple for example.

I needed a way of determining correct text colour based on a given background colour. After doing a little bit of digging I came across this article on CodeProject which almost gives the solution I was looking for. However I want to just display white or black text depending on the background colour. So here is the code I used to do so, it works remarkably well. I’ve pseudo coded it up a bit to remove various bits and pieces that may not be important but you can just replace bits of it with proper objects. Also ignore the magic numbers, they’re there just to show the numbers that worked for me. This algorithm can potentially be used in any language, I’m using C++ here as it is what I’m currently using.

[code lang="C++"]
COLOUR GetTextColourFromBackground(COLOUR BackgroundColour)
{
uint32 uDarkLightColourThreshold(105);
float fRedThresholdValue(0.299);
float fGreenThresholdValue(0.587);
float fBlueThresholdValue(0.114);

uint8 bgDelta = static_cast((BackgroundColour.GetRed() * fRedThresholdValue) + (BackgroundColour.GetGreen() * fGreenThresholdValue) +
(BackgroundColour.GetBlue() * fBlueThresholdValue));

COLOUR TextColour(GetColour(White));
if((255 - bgDelta) < uDarkLightColourThreshold)
{
TextColour = GetColour(Black);
}

return TextColour;
}
[/code]

So on a white background the text colour returned will be black and on a black background the text colour will be white. Also on a light grey background the colour returned will be black and so on.
The formula used above originally comes from the W3C http://www.w3.org/TR/AERT#color-contrast.

edlong C/C++

Quieten XCode’s output to the console

August 6th, 2009

Hi,

During some iPhone dev i noticed that the console on my macbook had many entries like:

Xcode(19838,0xb0103000) malloc: free_garbage: garbage ptr = 0×319a380, has non-zero refcount = 1
Xcode(19838,0xb0103000) malloc: free_garbage: garbage ptr = 0×31bf6e0, has non-zero refcount = 1
Xcode(19838,0xb0103000) malloc: free_garbage: garbage ptr = 0×3263da0, has non-zero refcount = 1
Xcode(19838,0xb0103000) malloc: free_garbage: garbage ptr = 0×3274eb0, has non-zero refcount = 1
Xcode(19838,0xb0103000) malloc: free_garbage: garbage ptr = 0×319a380, has non-zero refcount = 1
Xcode(19838,0xb0103000) malloc: free_garbage: garbage ptr = 0×31bf6e0, has non-zero refcount = 1

These messages I found out are not actually anything to do with my program, I thought I was doing something wrong and not freeing up resources. It is actually the internal workings of XCode which is automatically freeing up resources. Knowing this, I decided to turn off these annoying console outputs and found this XCode project which basically gobbles up output from XCode in the same mould as above.

Just compile the project and it will install the QuietXCode plugin to ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins. Restart Xcode and fire up the console and you’ll see an inital output of:

06/08/2009 19:34:32 Xcode[5147] <QuietXcode> loaded successfully

You should no longer see the annoying output!

admin C/C++

Using a single number to contain two other numbers

July 3rd, 2009

Hi,

Due to a certain restrictive pattern being used at work, I’ve recently had the need to pass along two numbers but only had a single number to do so. It’s a bit of a nasty hack but is my first experience with using bit masking and is a useful technique. The integer being used in the pattern was an unsigned 32 bit integer and the two other integers I wanted to pass along were both unsigned 8 bit integers. The way we figured to send along the two numbers is to basically use half of the 32 bit integer to contain the two 8 bit integers using masking. The process is very simple there are two functions, Encode and Decode. Encode takes in two integers (I’ve made them unsigned 16 bits for the moment but you can easily cast the 8 bit unsigned integer to 16 bit) and returns a 32 bit unsigned int that contains the two. The encode function basically shifts the iFirstNumber into the lower 16 bits of the 32 bit integer. Then iSecondNumber is added to this to give a full 32 bit number.

[code lang="C++"]
uint32 Encode(const uint16 iFirstNumber, const uint16 iSecondNumber)
{
uint32 iEncodedValue((iFirstNumber<< 16) + iSecondNumber);

return iEncodedValue;
}
[/code]

To explain it a little clearer suppose we have iFirstNumber set to 3 (0x00000011) and iSecondNumber set to 1 (0x00000001) we would end up with the encoded value being 0x0000001100000001

You can see that the first 16 bits contain iFirstNumber and the second 16 bits contain the second.

To get the two numbers back out again we have a similar Decode function:

[code lang="C++"]
void Decode(uint32 iValue, uint16& iFirstNumber, uint16& iSecondNumber)
{
iFirstNumber = (uint16)((iValue & 0xFFFF0000) >> 16);
iSecondNumber= (uint16)(iValue & 0x0000FFFF);
}
[/code]

The first number is the overall value ANDed with 0xFFFF0000 and then shifted 16 bits to the right. ANDing the number with 0xFFFF0000 indicates that we are only interested in the first half of the bits in the overall value. It will turn all the rest of the bits in the value to be 0000000000. So after that we will be left with 0×0000001100000000. Now we need to narrow the number down to a 16 bit number. To do this we shift the bits 16 places to the right and cast to a uint16. Casting will remove the proceeding 16 bits(which are all now 0 anyway) and leave us with 0×00000011 which translates to 3.
For iSecondPosition we say that we want only the last half of the bits in the overall value. This leaves us with 0×0000000000000001. Casting this to a uint16 will leave us with 0×00000001 or 1.

In the end we actually didn’t need to do it this way but got it working anyhow. Might be handy for someone maybe!

edlong C/C++

Visual Assist, Incredibuild and Perforce

March 24th, 2009

Hi,

This is my first post on C++ since the web-development foray. I’ve started working with Beautiful Games Studios on the latest Championship Manager 2009 game. I’ve a few musing since I’ve started.

Visual Assist is the best resource I’ve ever used for Visual Studio, I think our studio has a studio wide license for it and its very much recommended. It provides a far superior intellisense to the built-in one and jumping to and from header/cpp files is very handy(using Alt+O shortcut). It also provides excellent code suggestions for variable names and function definitions. The various shortcuts are great. As the project we’re working on is quite large, a change to a header file and trigger a recompile of almost the whole project. Doing this on a single machine would take ages so we use incredibuild. Incredibuild distributes the build across other computers on the internal network and across your machines CPUs and so the compile time is reduced from hours to minutes.

Perforce is our source control system. Having previously used CVS and SVN I thought it would be easy to figure out. It proved more difficult than I expected as it uses changelists which I wasn’t familiar with, client specs and has other ‘features’ that took me a while to figure out. Syncing and updating you code base can be messy if you have lots of stuff checked out as you need to resolve new changes with your own local changes. However it forces you to check stuff in often. One of the biggest annoyances with its integration with Visual Studio however is when you happen to have a vcproj checked out. Occasionally Perforce will check out a vcproj for no apparent reason whatsoever. If you didn’t know this and perform and update then you can miss new files that have been added to the project if you don’t resolve your vcproj. This will of course throw up a lot of errors next time you compile and can be mysterious unless you figure out there was a vcproj checkout.

From this and for other reasons I’ve figured out one rule of thumb with Perforce. Always check your changelists (refreshing each one of them) whenever you update(get the latest version) or integrate across branches. That way you’ll pick up what conflicts exist and can resolve them and you should be ok.

Another problem I’ve found is that when a change in a vcproj is detected, Visual Studio asks you to reload the project. Obviously you’ll want to do this, however Visual Studio 6 never ceases to crash for me once I compile after reloading the project. So its a good idea to close down Visual Studio before performing an update. That way when you open it again Visual Studio will pick up the new vcproj settings and generally does not crash.

Finally client specs are another confusing aspect of Perforce. A client spec is basically your user profile which manages a section of code/resources you’ve obtained from Perforce. I didn’t quite understand what they were to begin but now I’ve figured out why you can have more than one client-spec. If you have a whole bunch of files checked out and integrate your branch with another(i.e. the mainline) to get the latest changes there can often be a lot of conflicts between your local changes that you haven’t yet checked in and the new changes on the mainline. The only way you can fix these conflicts is to merge your own changes with the new changes. Once this is done you can check in the merged file into the branch and the integration is complete.

However I regularly want to get just integrate to pick up certain changes on the mainline but don’t want any of my latest changes to be checked in. So the merge option isn’t a viable option in this case as you can only merge (meaning you will have to check in your latest changes), accept the source’s version (thus losing yours) or accept your local version (thus losing the source’s changes). Creating a new client spec is the solution.

The client spec is basically a copy of the codebase in a separate location. You use a new client spec for integrating ONLY and use the other client spec for working on latest code. When integrating you get the latest code into your new client spec’s default changelist. Resolve any conflicts there may be(as there’s no local changes made by you on this client spec then there will be fewer local conflicts due to your own changes) and then you can check in the default changelist into your own branch. So you’ve now integrated without having to check in your own latest changes, others in your branch can update also picking up the latest changes and you still have your own latest changes intact.

In your other client spec you can now just update your code base and get the latest version. You could get conflicts but once those are resolved you can just continue working. You don’t need to check in your own changes to integrate the branch with the mainline. It may sound convoluted but is actually very simple and much easier than use than a single client spec. I found this out the hard way by spending about an hour trying to back out of changes I had made by backing up the files locally, reverting them and then integrating. Then I would open up the backed up files and re-apply my changes, very messy you’ll agree!

admin Archive Post, BGS, C/C++, Visual Studio, Windows