PDA

Voir la version complète : Probleme avec l'affichage du text


bougie
03/08/2005, 22h41
Bonsoir à tous :)
Voila, je viens encore demander votre aide :00000020:

J'ai donc decider de commencer à coder mon jeux, ça sera un rpg :)
Tous se passe comme prevu, l'affichage des images marche, l'effacement de l'ecran marche, le blitage des surfaces de la liste aussi
Enfin, tous porte à croire que je n'aurais aucun probleme du coté de l'affichage :)

Mais, c'est impossible ça :D

Donc, j'ai evidemment un probleme.
Et ce probleme concerne l'affichage du text :s

Je fais pour le text exactement comme je fait pour une image, c'est à dire que j'ajoute la SDL_Surface et le SDL_Rect dans ma liste d'entity, puis je reblit toutes les surfaces
Sauf que là, bah mon text ne s'affiche pas à l'ecran :s


Voila mon code, j'espere que vous pouvez m'aider, je seche, je ne vois pas dutout d'ou ça peut venire

CTextManager.cpp

#include "CTextManager.hpp"

CTextManager::CTextManager()
{
InitFont();
}

CTextManager::~CTextManager()
{
}

void CTextManager::InitFont()
{
TTF_Init();
m_pFont = TTF_OpenFont("../font/arial.ttf", 26);
}

void CTextManager::DrawString(char* pszString, int iXPos, int iYPos)
{
m_TextPos = CreateRect(iXPos, iYPos, 200, 30);

SDL_Color Black = {0, 0, 0, 0};
m_pTextSurface = TTF_RenderText_Solid(m_pFont, pszString, Black);
AddEntity(m_pTextSurface, m_TextPos);
}



CTextManager.hpp

#ifndef CTextManager_HPP
#define CTextManager_HPP

#include <iostream>
#include "SDL.h"
#include "SDL_ttf.h"
#include "CVideoManager.hpp"

class CTextManager : public CVideoManager
{
private:
SDL_Surface* m_pTextSurface;
SDL_Rect m_TextPos;
TTF_Font* m_pFont;

public:
CTextManager();
~CTextManager();
void InitFont();
void DrawString(char* pszString, int iXPos, int iYPos);
};
#endif

CEntityManager.cpp
#include "CEntityManager.hpp"

CEntityManager::CEntityManager()
{
}

CEntityManager::~CEntityManager()
{
}

void CEntityManager::AddEntity(SDL_Surface* pEntity, SDL_Rect EntityRect)
{
CEntityManager *Entity = new CEntityManager;

Entity->m_pEntitySurface = pEntity;
Entity->m_EntityRect = EntityRect;

EntityList.push_back(Entity);
}

CEntityManager* CEntityManager::GetEntity(int iNb)
{
Iter = EntityList.begin();
std::advance(Iter, EntityList.size() - iNb);
return (*Iter);
}



CEntityManager.hpp

#ifndef CEntityManager_HPP
#define CEntityManager_HPP

#include <iostream>
#include <list>
#include "SDL.h"

class CEntityManager
{
friend class CVideoManager;
private:
SDL_Surface* m_pEntitySurface;
SDL_Rect m_EntityRect;
std::list<CEntityManager*> EntityList;
std::list<CEntityManager*>::iterator Iter;

public:
CEntityManager();
~CEntityManager();
void AddEntity(SDL_Surface* pEntity, SDL_Rect EntityRect);
CEntityManager* GetEntity(int iNb);
};
#endif



CVideoManager.cpp

#include "CVideoManager.hpp"

CVideoManager::CVideoManager()
{
InitScr(800, 600, 0);
}

CVideoManager::~CVideoManager()
{
FreeSurface(m_pMainScr);
}

void CVideoManager::InitScr(int iWidth, int iHeight, int iColorDepth)
{
SDL_Init(SDL_INIT_VIDEO);
m_pMainScr = SDL_SetVideoMode(iWidth, iHeight, iColorDepth, SDL_SWSURFACE);
}

void CVideoManager::RefreshScr()
{
for(Iter = EntityList.begin(); Iter != EntityList.end(); Iter++)
Blit((*Iter)->m_pEntitySurface, (*Iter)->m_EntityRect);
UpdateScr();
}

void CVideoManager::Blit(SDL_Surface* pSurface, SDL_Rect SurfacePos)
{
SDL_BlitSurface(pSurface, NULL, m_pMainScr, &SurfacePos);
}

void CVideoManager::UpdateScr()
{
SDL_Flip(m_pMainScr);
}

void CVideoManager::ClearScr()
{
SDL_FillRect(m_pMainScr, NULL, SDL_MapRGB(m_pMainScr->format, 0, 0, 0));
}

void CVideoManager::SetAlpha(int Surface, Uint8 u8Alpha)
{
CEntityManager *pSurface = GetEntity(Surface);
SDL_SetAlpha(pSurface->m_pEntitySurface, SDL_SRCALPHA, u8Alpha);
}

void CVideoManager::FreeSurface(SDL_Surface* pSurface)
{
SDL_FreeSurface(pSurface);
}

void CVideoManager::DrawPicture(char* pszFilePath, int iXPos, int iYPos, int iWidth, int iHeight)
{
m_Rect = CreateRect(iXPos, iYPos, iWidth, iHeight);

m_pPicture = IMG_Load(pszFilePath);
AddEntity(m_pPicture, m_Rect);
}

void CVideoManager::MovePicture(int iLenght, int iHeight)
{
CEntityManager *Picture = GetEntity(1);
Picture->m_EntityRect = ChangePos(iLenght, iHeight);
}

SDL_Rect CVideoManager::CreateRect(int iX, int iY, int iW, int iH)
{
m_Rect.x = iX;
m_Rect.y = iY;
m_Rect.w = iW;
m_Rect.h = iH;

return m_Rect;
}

SDL_Rect CVideoManager::ChangePos(int iWidth, int iHeight)
{
m_Rect.x += iWidth;
m_Rect.y += iHeight;

return m_Rect;
}



CVideoManager.hpp

#ifndef CVideoManager_HPP
#define CVideoManager_HPP

#include <iostream>
#include "SDL.h"
#include "SDL_image.h"
#include "CEntityManager.hpp"

class CVideoManager : public CEntityManager
{
private:
SDL_Surface* m_pMainScr;
SDL_Surface* m_pPicture;
SDL_Rect m_Rect;

public:
CVideoManager();
~CVideoManager();
void InitScr(int iWidth, int iHeight, int iColorDepth);
void RefreshScr();
void UpdateScr();
void Blit(SDL_Surface* pSurface, SDL_Rect SurfacePos);
void ClearScr();
void SetAlpha(int Surface, Uint8 u8Alpha);
void FreeSurface(SDL_Surface* pSurface);
void DrawPicture(char* pszFilePath, int iXPos, int iYPos, int iWidth, int iHeight);
void MovePicture(int iLenght, int iHeight);
SDL_Rect CreateRect(int iX, int iY, int iW, int iH);
SDL_Rect ChangePos(int iWidth, int iHeight);
};
#endif



test.cpp

#include "CVideoManager.hpp"
#include "CTextManager.hpp"

int main(int argc, char *argv[])
{
CVideoManager* Video = new CVideoManager;
CTextManager* Text = new CTextManager;

Video->DrawPicture("../pixmaps/test.jpg", 0, 0, 800, 600);
Video->DrawPicture("../pixmaps/test2.jpg", 200, 120, 800, 600);
Video->RefreshScr();
SDL_Delay(1000);
Video->ClearScr();
Video->SetAlpha(1, 60);
Video->MovePicture(100, 0);
Text->DrawString("HELLO !", 400, 200);
Video->RefreshScr();
SDL_Delay(3000);
return (0);
}



Merci d'avance pour vos nombreuses reponse :D

Corkus
04/08/2005, 00h06
Avant tout, si tu mettais ton texte en blanc au lieu d'en noir tu verrais. Car avec ton FillRect() tout devient noir et bon, l'alpha ne servira pas à grand chose. À moins que MovePicture fasse changer la couleur.

bougie
04/08/2005, 10h44
Bah quand le text doit apparaitre, il n'apparait pas sur le fond noir, il apparai sur une image
Donc que j'ecrive en blanc ou en noirn je devrai voir le text :)

bougie
04/08/2005, 11h07
Bon, apres moult essais ce matin, j'ai trouvé la cause :)
Mais toujours pas la solution, pour que se soit propre :mad:

La Methode DrawString n'a pas acces, ou ne passe pas les arguments correctement
Ce qui fait que l'entity concernant le text ne s'ajoute pas à la list :s

Mais comme ma class CTextManager herite de CVideoManager, et CVideoManager herite de CEntityManager, normalement, j'ai acces à CEntityManager depuis CTextManager

Donc, à par mettre la methode DrawString Dans videoManager pour que ça marche, je vois pas trop comment faire
Mais bon, j'aimerai eviter de mettre cette methode dans CVideoManager quand meme :)