// BlinkGame - Hunt for the Lost Pixels
// an SDL game in honor of the BlinkenArea crew
//
// (C) 2005-06 Rene Schickbauer
//
// See License.txt for licensing information
//
#ifdef NOP_CODE
/*--------------------------------------------------------------------
					CVS AUTO-GENERATED INFO
$Author: cavac $
$Date: 2006/03/02 13:03:41 $
$Name: rel-0-3-patches $
$Revision: 1.12 $
$State: Exp $

$Log: background.cpp,v $
Revision 1.12  2006/03/02 13:03:41  cavac
Re-formatted all .cpp and .h files a bit to get rid of the TAB/SPACE mess
that makes troubles with XCode's "re-format" feature

Revision 1.11  2006/02/17 10:49:42  cavac
Added error-handling

Revision 1.10  2006/02/13 10:03:52  cavac
Added fix for background wrap-around in height (mainly untested)
see Tracker #1430507

Revision 1.9  2006/02/12 13:17:49  dauti
Fixed 64Bit problems. Now it works, when compiled as 64 and as 32 Bit binary
(Tested on AMD64)

Revision 1.8  2006/02/12 11:11:36  cavac
Changed INT to LONG for calculating fastcopy pointers
Added (commented out) section for new background handling

Revision 1.7  2006/02/10 18:56:17  cavac
Code Audit: Fixed many 64Bit based problems (mostly untested)

Revision 1.6  2006/01/27 17:04:02  cavac
Updated VC6 workspace to current release
Added ogg/vorbis support for Win32
Added SDL_Image and SDL_ttf support
Changed build- and installer-system for easier maintenance

Revision 1.5  2006/01/18 01:22:39  cavac
Added SDL_Image to load additional graphics formats
Updated osdef* and xcode-project to to add SDL_Image
Updated all image-loader functions to use SDL_Image
Added level6 as temporary testlevel
Added code to display start- and goal-points with an animation (needs some fixing)
Removed temporary "marker"-tiles from level-definitions, as we have now an
animated display for that.

Revision 1.4  2006/01/09 00:50:34  cavac
Bugfixed CVS autogenerated entries in background.cpp
Added CVS autogenerated entries to remaining .cpp and .h files
Updated copyright from "2005 Rene Schickbauer" to "2005-06 Rene Schickbauer"

Revision 1.3  2006/01/09 00:36:24  cavac
Changed layout of auto-generated code so it is a bit more readable
and secure against having many files spoiled with a single stupid textline
in checkin comment.

Revision 1.2  2006/01/09 00:30:40  cavac
Added various CVS-autogenerated headers


--------------------------------------------------------------------*/
#endif //NOP_CODE

#include "globals.h"
#include "background.h"
#include "memops.h"
#include "errorhandler.h"

SDL_Surface *BG_Surface;

void initBackground(char *fname) {
	char fullfname[MAX_FNAME_LENGTH];
	sprintf(fullfname, "%s%s", RESPATH, fname);
	SDL_Surface* temp = IMG_Load(fullfname);
	if(!temp) {
		DIE(ERROR_IMAGE_READ, fullfname);
	}
	SDL_Surface* bgtemp = SDL_ConvertSurface(temp, gScreen->format, SDL_SWSURFACE);
	SDL_FreeSurface(temp);
	
	/* Create a 32-bit surface with the bytes of each pixel in R,G,B,A order,
		as expected by OpenGL for textures */
	Uint32 rmask, gmask, bmask, amask;
	
	/* SDL interprets each pixel as a 32-bit number, so our masks must depend
		on the endianness (byte order) of the machine */
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
	rmask = 0xff000000;
	gmask = 0x00ff0000;
	bmask = 0x0000ff00;
	amask = 0x000000ff;
#else
	rmask = 0x000000ff;
	gmask = 0x0000ff00;
	bmask = 0x00ff0000;
	amask = 0xff000000;
#endif
	
	SDL_Surface* temp2 = SDL_CreateRGBSurface(SDL_SWSURFACE, bgtemp->w + SCR_WIDTH, bgtemp->h, 32,
								   rmask, gmask, bmask, amask);
	BG_Surface = SDL_ConvertSurface(temp2, gScreen->format, SDL_SWSURFACE);
	SDL_FreeSurface(temp2);

	// Lock Surfaces if needed
	if (SDL_MUSTLOCK(BG_Surface))
		if (SDL_LockSurface(BG_Surface) < 0) 
			return;
	if (SDL_MUSTLOCK(bgtemp))
		if (SDL_LockSurface(bgtemp) < 0) 
			return;

	Uint32 pitch_tmp = bgtemp->pitch / 4;
	Uint32 pitch_bg = BG_Surface->pitch / 4;	
	Uint32 i, j;
	
	// Copy the background into the bigger surface
	for(i=0; i < (Uint32)bgtemp->w; i++) {
		for(j=0; j < (Uint32)bgtemp->h; j++) {
			((Uint32 *)BG_Surface->pixels)[j * pitch_bg + i] = 
			((Uint32 *)bgtemp->pixels)[j * pitch_tmp + i];
		}
	}
	// Replicate the first SCR_WIDTH pixels to the end of the surface for FASTCOPY
	for(i=0; i < SCR_WIDTH; i++) {
		for(j=0; j < (Uint32)bgtemp->h; j++) {
			((Uint32 *)BG_Surface->pixels)[j * pitch_bg + i + bgtemp->w] = 
			((Uint32 *)bgtemp->pixels)[j * pitch_tmp + i];
		}
	}
	
	
	
	// Unlock Surfaces if needed
	if (SDL_MUSTLOCK(bgtemp)) 
		SDL_UnlockSurface(bgtemp);
	if (SDL_MUSTLOCK(BG_Surface)) 
		SDL_UnlockSurface(BG_Surface);
	
	
	SDL_FreeSurface(bgtemp);
		
	
}

void deInitBackground() {
	SDL_FreeSurface(BG_Surface);
}

void drawBackground(Sint32 xoffs, Sint32 yoffs) {
	// Background
	xoffs = xoffs % BG_Surface->w;
	yoffs = yoffs % BG_Surface->h;
	
	/*  Faster copying (not working??)
	SDL_Rect src, dest;
	src.x = 0;
	src.y = 0;
	src.w = SCR_WIDTH;
	src.h = SCR_HEIGHT;
	dest.x = 0;
	dest.y = 0;
	dest.w = SCR_WIDTH;
	dest.h = SCR_HEIGHT;
		
	SDL_BlitSurface(BG_Surface, &src, gScreen, &dest);
	return;
	*/
	
	// Lock Surface if needed
	if (SDL_MUSTLOCK(BG_Surface))
		if (SDL_LockSurface(BG_Surface) < 0) 
			return;	
	
	
	Uint32 bigpitch = gScreen->pitch;
	Uint32 bigpitchBG = BG_Surface->pitch;
	
	long fc_src_long = (long)BG_Surface->pixels + xoffs * 4 + bigpitchBG * yoffs;
	long fc_dst_long = (long)gScreen->pixels;
	
	for(Uint32 j=0; j < SCR_HEIGHT; j++) {
		fc_src = (fastcopy *)fc_src_long;
		fc_dest = (fastcopy *)fc_dst_long;
		*fc_dest = *fc_src;
		fc_dst_long += bigpitch;
		fc_src_long += bigpitchBG;
		yoffs++;
		if(yoffs > BG_Surface->h) {
			yoffs=0;
			fc_src_long = (long)BG_Surface->pixels + xoffs * 4;
		}
	}
	
	// Unlock Surface if needed
	if (SDL_MUSTLOCK(BG_Surface)) 
		SDL_UnlockSurface(BG_Surface);
	
}

Uint32 getBGWidth() {
	return BG_Surface->w;
}

Uint32 getBGHeight() {
	return BG_Surface->h;
}

