All pastes #46333 Raw Edit

Uatec

public text v1 · immutable
#46333 ·published 2006-03-20 14:11 UTC
rendered paste body
#include "human.h"

Human::Human(){
}

Human::~Human(){
}

int Human::init(const char * fn, SDL_Surface *screen, int x, int y, CollisionHandler *ch) {
	this->screen = screen;
	this->ch = ch;
	
	SDL_Surface *temp;
	
	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);
	
	//the image is loaded on a temporary surface
	if(!(temp = IMG_Load(fn))) {
		printf("SDL error: %s\n",IMG_GetError());
		return 0;
	}
	
	//the  image is adapted to local display so that it can obtains better graphics performance 
	bg = SDL_DisplayFormat(temp);
	
	//free temp surface
	SDL_FreeSurface(temp);
	
	//set the transparent color of cursor surface
	Uint8 R, B, G;
	SDL_GetRGB(((unsigned int*) bg->pixels)[0], bg->format, &R, &G, &B);
	SDL_SetColorKey(bg, SDL_SRCCOLORKEY|SDL_RLEACCEL, SDL_MapRGB(bg->format, R, G, B));
 	
 	glTexImage2D(GL_TEXTURE_2D,0,bg->format->BytesPerPixel,bg->w, bg->h,0,GL_BGRA,GL_UNSIGNED_BYTE,bg->pixels);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);	// Linear Filtering
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	// Linear Filtering	

	dest_rect = new SDL_Rect;
	dest_rect->x = x;
	dest_rect->y = y;
	dest_rect->w = bg->w;
	dest_rect->h = bg->h;
	return 1;
}

SDL_Rect* Human::getRect(){
	return dest_rect;
}

void Human::update(){
	int dx = destx-dest_rect->x;
	int dy = desty-dest_rect->y;
	//printf("%d, %d\n", dx, dy);
	float magnitude = sqrt( dx*dx + dy*dy ); 
	float xvel = dx / magnitude; 
	float yvel = dy / magnitude; 

	SDL_Rect *test = new SDL_Rect;
	test->x=dest_rect->x + ( int ) ( xvel * SPEED );
	test->y=dest_rect->y + ( int ) ( yvel * SPEED );
	test->w = dest_rect->w;
	test->h = dest_rect->h;
		
	bool colliding = false;
	for ( int i = 0; i < ch->num_objects; i++ ) {
	//	SDL_FillRect(bg, ch->member_rects[i], SDL_MapRGB(bg->format, 0x0, 0xff, 0x0));
	//	SDL_FillRect(bg, test, SDL_MapRGB(bg->format, 0xff, 0x0, 0x0));
		colliding = ch->test(test, ch->member_rects[i]);
		
	}
		
	if ( !colliding && dx != 0 && dy != 0 )
	{
		printf("m\n");
		//printf("  (%d, %d)\n", test->x, test->y);
		dest_rect = test;
	} else {
		//if (dx != 0 && dy != 0) printf("c (%d, %d)\n", test->x, test->y);
	}
}

void Human::draw(){
	//SDL_BlitSurface(bg, NULL, screen, dest_rect);
	glLoadIdentity();
	glBindTexture(GL_TEXTURE_2D, texture);	
	glTranslatef(dest_rect->x-1, dest_rect->y, 0.0f);
	//glScalef((float) (1.0f/3.0f), (float) (1.0f/3.0f), 0.0f); 
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f);
		glVertex2f(0.0f, 0.0f);
		glTexCoord2f(0.0f, 1.0f);
		glVertex2f(0.0f, 1.0f);
		glTexCoord2f(1.0f, 1.0f);
		glVertex2f(1.0f, 1.0f);
		glTexCoord2f(1.0f, 0.0f);
		glVertex2f(1.0f, 0.0f);
	glEnd();	
}

int Human::newDest(int x, int y) {
	destx = x;
	desty = y;
	return 0; //if we reach this point we?ve searched all reachable nodes and still haven?t found the solution, therefore one doesn?t exist)

}