Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Member Since 05 Nov 2008
Offline Last Active Dec 09 2014 07:45 AM

Topics I've Started

SQL: Best way to insert data with one to many relationship

02 July 2011 - 10:35 PM

I have an issue which should be very common, but I haven't been able to find any information on the internet about it. How do I insert a row into a table and then return the primary key which is subsequently used as a foreign key for rows in a second table?

Suppose I have a website where people can enter their name and details, and a list of CDs they own. The database has 2 tables - Person, and CDsOwnedByPerson. The relationship is one to many - i.e. each person can own many CDs.

When a user adds their collection to the database, the backend needs to do something like this:

1) Insert a new row into the Person table, containing the user's details
2) Retreive the primary key (PersonID) of the row just added (Where the primary key is set to auto-increment)
3) Add rows to the CDsOwnedByPerson table, using PersonID as the foreign key linking back to the person table

Easy stuff right? The question is, how do you guarantee that step 2) will return the desired key, when there could be multiple people submitting to the database at once? Clearly "select top (1) id from person order by id desc" is not going to be reliable in an environment with concurrent users. So what can I do instead?

This has got to be one of the most common comcurrency problems in any SQL based system, so is there a preferred way of dealing with it?

Windows API modal popups

14 January 2011 - 12:16 PM

I just ran into a bug caused by displaying modal dialog boxes for debug purposes during the execution of some code. The dialog box seems to suspend the current thread it runs on, but certain messages in the message loop are still passed through, so it was possible to use the mouse to begin a new operation while the old operation was still in progress.

I'd like to know exactly how modal popups work in the Windows API so I don't run into these problems again.

While a modal popup is showing, what's actually happening with the message loop that called the winProc which resulted in the dialog box appearing in the first place? Does Windows spawn a worker thread and somehow activate the original message loop again, or are future user input events redirected to some other message loop behind the scenes? If I have code inside the loop between GetMessage and DispatchMessage, will this code execute while a modal dialog box is blocking the original thread? If not, that would make it unsafe to put any sort of custom code inside your message loop if you plan to use modal popups at all.

SSE2 optimisation

11 January 2011 - 03:47 AM

I've never used the SSE intrinsics in MSVC before, so I'm looking for advice on whether my particular algorithm can be made faster. It involves determining if a plane intersects a convex polygon in 3D space. While the algorithm runs a very large number of times, I don't think I can parallelise across separate operations, as subsequent stages of the process modify the data set. Plus I'm using MSVC's STL so I don't have 16 byte alignment on my Vec3s.

The plane to convex polygon algorithm is as follows:

- Iterate over the polygon's points, calculating the point-to-plane distance of each
- The plane intersects the polygon if:
a) Distance p and p+1 have opposite signs and have magnitude greater than epsilon
B) Distance p is within epsilon of zero, but p-1 and p+1 have magnitude greater than epsilon and opposite signs
- An edge of the polygon lies on the plane if
a) Distance p and p+1 are both within epsilon of zero

First I was thinking of using intrinsics to evaluate dv[i] = (planePt - v[i]).dot(planeN) each iteration, but maybe it would be better to calculate the operation for 4 points in parallel. But I don't know if the above process can be adapted to SSE, or if it's even worth trying.

Could anyone with experience using SSE give me some advice here? Is this an avenue worth persuing at all?

Curiously Recurring Template Pattern

10 December 2010 - 06:25 PM

Just finished reading the article on Gamedev about it

I'm confused as to why the compiler (specifically VS2010) doesn't perform what seems to be very trivial optimisations.

class A{
virtual int call()=0;

class B : public A{
int call(){
return 10;

class C : public A{
int call(){
return 50;

C* cp1 = new C();
cout << cp1->call(); // Virtual call
C c;
C* cp2 = &c;
cout << cp2->call(); // Virtual call

According to the article, cp1 and cp2 could be of types that inherit from C, so the compiler must do a VTable lookup just in case. But even if it wasn't possible to infer the type from the code above, wouldn't the compiler know that there are no classes inheriting from C? And the object can't be of type A, as A is pure virtual. Is there a technical reason for the compiler not doing this sort of optimisation?

Intersection of 3D convex polygons using separating axes

27 November 2010 - 07:39 PM

I want to determine if two 3D convex polygons are intersecting, where each polygon is a 2D shape lying on a 3D plane. I've been told that the method of separating axes provides an efficient solution.
The information I've read says to use both plane normals as separating axes, but then goes on to say that you also need to test against the axes formed by taking the cross product of each edge in either polygon. This requirement seems to defeat the purpose of using separating axes altogether as it's extremely slow - the running time being O(n^3) when the polygons intersect!

There's another method I've been thinking of, where I intersect polygon A by B's plane to form a line segment. I then project B's vertices onto the line segment's vector and test for overlap. This is merely O(m + n), but I'm worried that I'm overlooking something in assuming a solution that simple could actually work. Surely if it did then one of the half dozen articles I read would have mentioned it?