From d288a2db9f4d225b324c65cfa0aa77198d92740c Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Tue, 12 Feb 2013 20:22:35 +0000
Subject: [PATCH] [Fix] Silly collision bug. Pixel-Perfect collision should now
 be *perfect*(tm).

---
 src/collision.c | 14 ++++++++++++--
 src/opengl.c    | 12 +++++++-----
 2 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/src/collision.c b/src/collision.c
index 7fb6e5c..932f0a2 100644
--- a/src/collision.c
+++ b/src/collision.c
@@ -37,12 +37,22 @@ int CollideSprite(const gl_texture* at, const int asx, const int asy, const Vec2
   int inter_x1 = MIN(ax2, bx2);
   int inter_y0 = MAX(ay1, by1);
   int inter_y1 = MIN(ay2, by2);
+  
+  // Real vertical sprite value (flipped).
+  int rasy = at->sy - asy - 1;
+  int rbsy = bt->sy - bsy - 1;
+
+  // Set up the base points.
+  int abx = asx*(int)(at->sw)   - ax1;
+  int aby = rasy*(int)(at->sh)  - ay1;
+  int bbx = bsx*(int)(bt->sw)   - bx1;
+  int bby = rbsy*(int)(bt->sh)  - by1;
 
   for(y = inter_y0; y <= inter_y1; y++)
     for(x = inter_x0; x <= inter_x1; x++)
       // Computer offsets for surface before passing to TransparentPixel test.
-      if((!gl_isTrans(at, asx*(int)(at->sw) + x-ax1, asy*(int)(at->sh) + y-ay1)) &&
-            (!gl_isTrans(bt, bsx*(int)(bt->sw) + x-bx1, bsy*(int)(bt->sh) + y-by1)))
+      if((!gl_isTrans(at, abx + x, aby + y)) &&
+            (!gl_isTrans(bt, bbx + x, bby + y)))
         return 1;
 
   return 0;
diff --git a/src/opengl.c b/src/opengl.c
index fc4be3e..76dc36c 100644
--- a/src/opengl.c
+++ b/src/opengl.c
@@ -148,7 +148,7 @@ static GLuint gl_loadSurface(SDL_Surface* surface, int* rw, int* rh) {
   if(rw)*rw = potw;
   if(rh)*rh = poth;
 
-  if(surface->w != potw || surface->h != poth) {
+  if((surface->w != potw) || (surface->h != poth)) {
     // Size isn't original.
     SDL_Rect rtemp;
     rtemp.x = rtemp.y = 0;
@@ -273,17 +273,19 @@ gl_texture* gl_newImage(const char* path) {
 
   SDL_FreeSurface(tmp); // Free the temp surface.
 
-  SDL_LockSurface(surface);
-  trans = SDL_MapTrans(surface);
-  SDL_UnlockSurface(surface);
 
   if(SDL_VFlipSurface(surface)) {
     WARN("Error flipping surface");
     return NULL;
   }
+  
+  // Do after flipping for collision detection.
+  SDL_LockSurface(surface);
+  trans = SDL_MapTrans(surface);
+  SDL_UnlockSurface(surface);
+
   t = gl_loadImage(surface);
   t->trans = trans;
-
   return t;
 }