Archive

Author Archive

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++

Disable intellisense in Visual Studio 8

July 10th, 2009

Hi,

The studio I work in use Visual Assist which is a far better version of Intellisense in my opinion. However Intellisense is on by default on Visual Studio and does a far bit of processing in the background, especially when there are significant project changes. So I’ve needed to disable intellisense for this project, to do so is trivial really.

Close your active solution in Visual Studio.
Navigate to: C:\Program Files (x86)\Microsoft Visual Studio 8\VC\vcpackages (you may need to find the right directory on your machine depending on where Program FIles are…)
Remove or rename(a better option in case you need it again)

Feacp.dll

Intellisense will now be disabled.

edlong Uncategorized

First forays into iPhone development

July 10th, 2009

Hi,

I’ve just started looking into beginning development on the iPhone. Coming from C++, Objective-C seems a strange and foreign language and so I’ve been looking for useful tutorials online. The best I’ve found so far is a Stanford course that is available on iTunes U. Seems to be a very well put together course taught by Apple developers. I’ve looked at one lecture so far but was impressed.

Check it out

Stanford Lectures on Cocoa Programming

Other handy resources are:

iPhone dev blog
More tutorials

Let me know if anyone knows of other good tutorials.

edlong Uncategorized

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++

Champ Man preview

June 22nd, 2009

Hi all,

Really sorry I haven’t blogged in a while, I’ve found it tough with work commitments and just not being motivated enough recently to do in my spare time! I’m going to try and get it all going again soon.

Edit: We’ve just announced the release date and a special new feature, CM Season Live:

http://www.championshipmanager.co.uk/server/show/ConWebDoc.918?PHPSESSID=a23581a233622d5e50d1dfbc20cabc14
http://www.championshipmanager.co.uk/server/show/ConWebDoc.917?PHPSESSID=a23581a233622d5e50d1dfbc20cabc14

In the meanwhile here a couple of previews of the game on GameReactor.

http://gamereactor.co.uk/grtv/?id=4194
http://gamereactor.co.uk/grtv/?id=4210

edlong General

Convert UIF to ISO

April 23rd, 2009

Hi,

I recently had a UIF image that I didn’t really know how to mount. After some digging I found that it seems to mount only with MagicISO, a Windows only application.

However there is a nifty little converter that converts from UIF to ISO which can then be mounted, burned etc.

Check it out at http://vafer.org/projects/uif2iso4mac/.

edlong Mac

Turning off Macbook screen when displaying through TV using a DVI adapter

March 28th, 2009

Hi,

I’ve recently picked up a nice big TV and doing quite a bit of streaming from my laptop through the TV. However one this that has annoyed me is that the display on my Macbook Pro stays on all the time even when the primary display is the TV. After searching around for a little bit I found this work-around. It only really works well however if you have a remote.

Basically you hook up the TV using a DVI adapter and go into display preferences. Go into the ‘Arrangement’ tab and you’ll see two blue boxes. One of those boxes will have a narrow white rectangle on the top of it. Drag this rectangle over to the TV(i.e. the opposite to the one it is on already) and close the preferences pane. Start up FrontRow and it will automatically turn off the secondary display (now the MBP screen) and you can watch TV without having the secondary display annoying you.

Link to detailed explanation.

edlong Mac

Adding favicons to your site

March 28th, 2009

Hi,

After moving my site over to my new host I did a couple of little jobs on it. The first thing I did was to sort out some of the 404s that I’ve been getting. One of those is that my site is missing a favicon. A favicon is the little icon that is displayed on Firefox tabs or next to the URL in Firefox. IE also supports it. It is a 16×16 image that needs to be saved as a ‘.ico’ file. Photoshop doesn’t export to this format natively but there is a free plugin available for it at the Telegraphics site. Once I created a simple image I exported it and FTP’ed it into the webroot of my site. I then edited my skin header and within the head tags I added the following lines of html:

[code lang="html"]

[/code]

The extra couple of lines are to satisfy IE’s oddities.
You can also use png or even gifs for this but if you don’t use the ico format IE doesn’t play nice.

Once thats been done you should be ready to go. Most browsers won’t show the icon unless you clear your cache and restart your browser, IE6 won’t display it unless you actually add the site to your favourites.

Safari also has some problems. Emptying the cache in the regular fashion won’t clear the favicons. You need to go to the ‘Edit’ menu and select Reset Safari. Check Remove all website icons. If this doesn’t work, quit safari and you’ll have to manually clear them by going to User/Library/Safari/Icons and in Windows C:\Documents and Settings\YourUserName\Local Settings\Application Data\Apple Computer\Safari\WebpageIcons.db.

You may see the little grey ‘EL’ icon that II now have on my site, that’s it!

edlong HTML

Lots of new posts

March 24th, 2009

Hi,

For anyone following a feed of the site, sorry about all the new posts!

I’ve had to repost a lot of older archived posts as I completely lost my wordpress database and have been trawling through the Google cache and entering them again.

edlong General