From 5befe97969e2f089e4354dea3048e80b7480d5d8 Mon Sep 17 00:00:00 2001
From: Tamir Atias <engine.games@gmail.com>
Date: Thu, 29 Dec 2011 22:10:31 +0200
Subject: [PATCH] [FIX] Map drawing based on camera offset. [FIX] Speech bubble
 not following camera.

---
 Win32/Unuk/Unuk/Unuk.vcproj |  4 ++--
 src/Unuk/Game.cpp           |  1 +
 src/Unuk/main.cpp           |  2 +-
 src/libUnuk/Character.cpp   | 20 ++++++++++----------
 src/libUnuk/Map.cpp         | 21 +++++++++++++++++----
 5 files changed, 31 insertions(+), 17 deletions(-)

diff --git a/Win32/Unuk/Unuk/Unuk.vcproj b/Win32/Unuk/Unuk/Unuk.vcproj
index 9588387..e6c7e63 100644
--- a/Win32/Unuk/Unuk/Unuk.vcproj
+++ b/Win32/Unuk/Unuk/Unuk.vcproj
@@ -65,7 +65,7 @@
 				LinkIncremental="2"
 				AdditionalLibraryDirectories="..\..\Dependencies\Lib\"
 				GenerateDebugInformation="true"
-				SubSystem="2"
+				SubSystem="1"
 				TargetMachine="1"
 			/>
 			<Tool
@@ -166,7 +166,7 @@
 			/>
 			<Tool
 				Name="VCPostBuildEventTool"
-				CommandLine="copy ..\..\Dependencies\Bin\jpeg.dll ..\..\..\Bin\&#x0D;&#x0A;copy ..\..\Dependencies\Bin\libfreetype-6.dll ..\..\..\Bin\&#x0D;&#x0A;copy ..\..\Dependencies\Bin\libpng12-0.dll ..\..\..\Bin\&#x0D;&#x0A;copy ..\..\Dependencies\Bin\libtiff-3.dll ..\..\..\Bin\&#x0D;&#x0A;copy ..\..\Dependencies\Bin\SDL.dll ..\..\..\Bin\&#x0D;&#x0A;copy ..\..\Dependencies\Bin\SDL_gfx.dll ..\..\..\Bin\&#x0D;&#x0A;copy ..\..\Dependencies\Bin\SDL_image.dll ..\..\..\Bin\&#x0D;&#x0A;copy ..\..\Dependencies\Bin\SDL_ttf.dll ..\..\..\Bin\&#x0D;&#x0A;copy ..\..\Dependencies\Bin\zlib1.dll ..\..\..\Bin\"
+				CommandLine="copy ..\..\Dependencies\Bin\jpeg.dll ..\..\..\Bin\&#x0D;&#x0A;copy ..\..\Dependencies\Bin\libfreetype-6.dll ..\..\..\Bin\&#x0D;&#x0A;copy ..\..\Dependencies\Bin\libpng12-0.dll ..\..\..\Bin\&#x0D;&#x0A;copy ..\..\Dependencies\Bin\libtiff-3.dll ..\..\..\Bin\&#x0D;&#x0A;copy ..\..\Dependencies\Bin\SDL.dll ..\..\..\Bin\&#x0D;&#x0A;copy ..\..\Dependencies\Bin\SDL_gfx.dll ..\..\..\Bin\&#x0D;&#x0A;copy ..\..\Dependencies\Bin\SDL_image.dll ..\..\..\Bin\&#x0D;&#x0A;copy ..\..\Dependencies\Bin\SDL_ttf.dll ..\..\..\Bin\&#x0D;&#x0A;copy ..\..\Dependencies\Bin\zlib1.dll ..\..\..\Bin\&#x0D;&#x0A;"
 			/>
 		</Configuration>
 	</Configurations>
diff --git a/src/Unuk/Game.cpp b/src/Unuk/Game.cpp
index fade9e9..49d74fd 100644
--- a/src/Unuk/Game.cpp
+++ b/src/Unuk/Game.cpp
@@ -174,6 +174,7 @@ void Game::UpdateGame(void) {
 }
 
 void Game::Render(void) {
+ // SDL_FillRect(screen, NULL, 0); //  You might want to clear the buffer! --konom
   if(_ingameMenu.GetStatus() == false) {
     _map.Render();
 
diff --git a/src/Unuk/main.cpp b/src/Unuk/main.cpp
index 2718317..cadfee1 100644
--- a/src/Unuk/main.cpp
+++ b/src/Unuk/main.cpp
@@ -23,7 +23,7 @@
 #endif
 #endif
 
-#ifndef _WIN32
+#if !defined(_WIN32) || defined(_DEBUG)
 int main() {
 #else
 int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int) {
diff --git a/src/libUnuk/Character.cpp b/src/libUnuk/Character.cpp
index 08f0df3..dd998f2 100644
--- a/src/libUnuk/Character.cpp
+++ b/src/libUnuk/Character.cpp
@@ -65,18 +65,18 @@ void Character::Render(void) {
   // Draw some fancy speach bubbles. It is a bit of a mess, I am playing.
   if(_speachBubble.size() != 0) {
     if(_speachBubbleTimer.GetTicks() < SPEACH_BUBBLE_DISPLAY_LENGTH) {
-      roundedBoxRGBA(screen, (x + w / 2) - 100,
-                     y - 100,
-                     (x + w / 2) + 100,
-                     y - 35,
+      roundedBoxRGBA(screen, (x + w / 2) - 100 - camera.x,
+                     y - 100 - camera.y,
+                     (x + w / 2) + 100 - camera.x,
+                     y - 35 - camera.y,
                      5, 255, 255, 255, 255);
 
-      filledTrigonRGBA(screen, (x + w / 2) - 100,
-                       y - 100,
-                       (x + w / 2) - 10,
-                       y - 40,
-                       (x + w / 2) + 10,
-                       y - 40,
+      filledTrigonRGBA(screen, (x + w / 2) - 100 - camera.x,
+                       y - 100 - camera.y,
+                       (x + w / 2) - 10 - camera.x,
+                       y - 40 - camera.y,
+                       (x + w / 2) + 10 - camera.x,
+                       y - 40 - camera.y,
                        255, 255, 255, 255);
 
       _speachBubbleText.Render((x + w / 2) - 90, y - 90);
diff --git a/src/libUnuk/Map.cpp b/src/libUnuk/Map.cpp
index 4222442..3969b5b 100644
--- a/src/libUnuk/Map.cpp
+++ b/src/libUnuk/Map.cpp
@@ -133,12 +133,16 @@ void Map::Update(void) {
 }
 
 void Map::Render(void) {
-  int xOrig = (camera.x / TILE_WIDTH);
-  int yOrig = (camera.y / TILE_HEIGHT);
+  int xOrig = (camera.x / TILE_WIDTH) - 1;
+  int yOrig = (camera.y / TILE_HEIGHT) - 1;
 
-  int xEnd = (SCREEN_WIDTH  / TILE_WIDTH);
-  int yEnd = (SCREEN_HEIGHT / TILE_HEIGHT);
+  if (xOrig < 0) xOrig = 0;
+  if (yOrig < 0) yOrig = 0;
 
+  int xEnd = xOrig + (SCREEN_WIDTH  / TILE_WIDTH) + 3;
+  int yEnd = yOrig + (SCREEN_HEIGHT / TILE_HEIGHT) + 3;
+
+  /* the fuck is this sara? --konom
   if(xEnd < x)
     xEnd++;
   else
@@ -148,6 +152,15 @@ void Map::Render(void) {
     yEnd++;
   else
     yEnd = y;
+  */
+
+  if (xEnd > x) xEnd = x;
+  if (yEnd > y) yEnd = y;
+  if (xEnd < 0) xEnd = 0;
+  if (yEnd < 0) yEnd = 0;
+
+  if (xOrig > xEnd) xOrig = xEnd - 1;
+  if (yOrig > yEnd) yOrig = yEnd - 1;
 
   for(int i = xOrig; i < xEnd; i++) {
     for(int j = yOrig; j < yEnd; j++) {