Autor Wątek: [SOLVED] SDL 2 - problem z ładowaniem textur do kontenera  (Przeczytany 1519 razy)

Offline Dethas

  • Użytkownik

# Luty 06, 2014, 13:05:32
Witam ponownie, napotkałem pewien problem z ładowaniem textur do kontenera, textura gracza ładuje mi się bez problemu, jego obiekt nie znajduje się w kontenerze, ale textury obiektów są zapakowane w kontener i z jakiegoś powodu nie wyświetla ich textur oto kod:

Klasa Obiektu w którym textury się nie ładują:
class BallObject
{
public:
SDL_Texture *RealTexture;
SDL_Rect Position;
int R, Speed, WayX, WayY;
double PosX, PosY;
BallObject()
{
RealTexture = NULL;
Position.x = NULL;
Position.y = NULL;
R = NULL;
Speed = NULL;
WayX = NULL;
WayY = NULL;
}
BallObject( int SizeR )
{
RealTexture = NULL;
WayX = 1;
WayY = 1;
Speed = 300;
R = SizeR;
PosX = rand() % SCREEN_SIZE_X + 30;
PosY = rand() % SCREEN_SIZE_Y - 30;
Position.x = PosX;
Position.y = PosY;
}

int Go( double TimePerSec );
};

Metoda sterująca obiektami, działa na pewno dobrze, wiele razy sprawdzana:
int BallObject::Go( double TimePerSec )
{
PosY += ( Speed * TimePerSec ) * WayY;
PosX += ( Speed * TimePerSec ) * WayX;
if( Position.x >= SCREEN_SIZE_X - 30 )
{
WayX = -1;
}
if( Position.x <= 30 )
{
WayX = 1;
}
if( Position.y >= SCREEN_SIZE_Y - 94 )
{
WayY = -1;
}
if( Position.y <= 30 )
{
WayY = 1;
}
Position.x = PosX;
Position.y = PosY;
return 1;
}

Kontener:

class CObjectsContainer
{
private:
unsigned Dist1, Dist2;
public:
int Size;
std::vector<BallObject> Container;

CObjectsContainer()
{
Size = 0;
}
bool AddBall( CRender *Renderer );
bool CheckColisions( int PosX, int PosY, int SizeR );
void Render( double TimePerSec );
void ClearContainer();
};

bool CObjectsContainer::AddBall( CRender *Renderer )
{
if( Size < 10 )
{
switch( rand() % 5 + 1 )
{
case 1:
{
BallObject Ball( 32 );
Ball.RealTexture = Renderer->LoadTexture( IMG_Load( "Graphic\\Ball.png" ) );
Container.push_back(Ball);
break;
}
case 2:
{
BallObject Ball( 32 );
Ball.RealTexture = Renderer->LoadTexture( IMG_Load( "Graphic\\BlueBall.png" ) );
Container.push_back(Ball);
break;
}
case 3:
{
BallObject Ball( 32 );
Ball.RealTexture = Renderer->LoadTexture( IMG_Load( "Graphic\\GreenBall.png" ) );
Container.push_back(Ball);
break;
}
case 4:
{
BallObject Ball( 32 );
Ball.RealTexture = Renderer->LoadTexture( IMG_Load( "Graphic\\OrangeBall.png" ) );
Container.push_back(Ball);
break;
}
case 5:
{
BallObject Ball( 32 );
Ball.RealTexture = Renderer->LoadTexture( IMG_Load( "Graphic\\RedBall.png" ) );
Container.push_back(Ball);
break;
}
}
Size += 1;
return true;
}
}

void CObjectsContainer::Render( double TimePerSec )
{
if( Size != 0 )
for( int x = 0; x < Container.size(); ++x )
{
Container[x].Go( TimePerSec );
}
}

bool CObjectsContainer::CheckColisions( int PosX, int PosY, int SizeR )
{
for( int x = 0; x < Container.size(); ++x )
{
Dist1 = ((( PosX + SizeR ) - ( Container[x].Position.x + Container[x].R )) * (( PosX + SizeR ) - ( Container[x].Position.x + Container[x].R ))) + ((( PosY + SizeR ) - ( Container[x].Position.y + Container[x].R )) * (( PosY + SizeR ) - ( Container[x].Position.y + Container[x].R )));
Dist2 = (SizeR + Container[x].R) * (SizeR + Container[x].R);
if( Dist1 < Dist2 )
return true;
}
return false;
}

void CObjectsContainer::ClearContainer()
{
for( int x = 0; x < Container.size(); ++x )
{
SDL_DestroyTexture( Container[x].RealTexture );
}
Container.clear();
Size = 0;
}

No i teraz kod odpowiedzialny za ładowanie i renderowanie textur:

class CRender
{
private:
int Size;
public:
SDL_Renderer *Renderer;
CRender( SDL_Window *WindowObject )
{
Renderer = SDL_CreateRenderer( WindowObject, -1, SDL_RENDERER_ACCELERATED );
SDL_SetRenderDrawColor( Renderer, 0xFF, 0xFF, 0xFF, 0xFF );
}
void RenderCopy( std::vector<BallObject> Container );
void RenderPresent();
SDL_Texture* CRender::LoadTexture( SDL_Surface *Surface );
};

void CRender::RenderCopy( std::vector<BallObject> Container )
{
if( Container.size() != 0 )
for( int x = 0; x < Container.size(); ++x )
{
SDL_RenderCopy( Renderer, Container[x].RealTexture, NULL, &Container[x].Position );
}
}

void CRender::RenderPresent()
{
SDL_RenderPresent( Renderer );
}

SDL_Texture* CRender::LoadTexture( SDL_Surface *Surface ) //<- działa w przypadku gracza
{
SDL_Texture *dst_Texture;
SDL_Surface *dst_Surface = Surface;
dst_Texture = SDL_CreateTextureFromSurface( Renderer, dst_Surface );
SDL_FreeSurface( dst_Surface );
return dst_Texture;
}

I pętla:

SDL_RenderClear( Renderer->Renderer );
TrueDelta = Frame->DeltaTime( Player->Points );
Player->ChaseMouse( MouseX, MouseY, TrueDelta, Container );
Container->Render( TrueDelta );
SDL_RenderCopy( Renderer->Renderer, BackGroundT, NULL, NULL );
SDL_RenderCopy( Renderer->Renderer, Player->PlayerTexture, NULL, &Player->PlayerPosition );
Renderer->RenderCopy( Container->Container );
Renderer->RenderPresent();

Jeśli ktoś ma jakieś pomysły jak to rozwiązać to byłbym bardzo wdzięczny. Pozdrawiam
« Ostatnia zmiana: Lipiec 04, 2014, 20:55:19 wysłana przez Dethas »

Offline Mr. Spam

  • Miłośnik przetworów mięsnych