
J´ai des probleme avec la SDL d´affichage l´image va du noir à l´image(elle clignote en quelque sorte). Je cherche depuis quelque heure mais je trouve rien voici le code:
Main.cpp:
- include "Moteur.h"
extern int xsouris,xrelsouris,yrelsouris,ysouris;
extern char boutons[3];
extern char key[400];
ofstream test;
Map *Map1;
void Init()
{
GameEngine("Ground Fighters", false);
Map1=new
Map("Source/Data/Map/1.txt","Source/Data/ChipSet.b
mp");
}
void Loop()
{
}
void Paint()
{
Map1->Draw();
}
bool Endloop()
{
test.open("test.txt");
test << "Checking if we pass the Enter phase." ;
if(key[SDLK_RETURN]){return true;}
test << "Passed it. Got another probleme." ;
return false;
}
Moteur.cpp:
- include "Moteur.h"
- include <SDL/SDL.h>
SDL_Surface *Screen;
ofstream Error;
int xsouris=0,xrelsouris=0,yrelsouris=0,ysouris=0;
char boutons[3]={0,0,0};
char key[400];
//--Class GameEngine--//
GameEngine::GameEngine(char *cName, bool bFullScreen)
{
Error.open("Error.txt");
if(SDL_Init(SDL_INIT_VIDEO)==-1)
{
Error << "Enable to init sdl : " << SDL_GetError() ;
Error.close();
}
//atexit(SDL_Quit);
if(bFullScreen){Screen=SDL_SetVideoMode(320, 240, 16, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);}
else{Screen=SDL_SetVideoMode(320, 240, 16, SDL_HWSURFACE | SDL_DOUBLEBUF);}
if(Screen==NULL)
{
Error << "Enable to make window : " << SDL_GetError() ;
Error.close();
}
SDL_WM_SetCaption(cName, NULL);
}
//--Class Sprite--//
Sprite::Sprite(char *cDestination, int iX, int iY)
{
Image=LoadBMP(cDestination);
Rect.x=iX;
Rect.y=iY;
}
void Sprite::Surface(int iSrcX, int iSrcY, int iSrcW, int iSrcH)
{
Src.x=iSrcX;
Src.y=iSrcY;
Src.w=iSrcW;
Src.h=iSrcH;
}
void Sprite::Move(int iMoveX, int iMoveY)
{
Rect.x=Rect.x+iMoveX;
Rect.y=Rect.y+iMoveY;
}
void Sprite::Draw()
{
SDL_BlitSurface(Image, &Src, Screen, &Rect);
}
Map::Map(char *Mapname, char *Image)
{
short y, x;
MapName=Mapname;
map.open(MapName);
for(y=0;y<30;y++)
{
for(x=0;x<40;x++)
{
map >> MapTile[x][y];
}
}
for(y=0;y<30;y++)
{
for(x=0;x<40;x++)
{
tile[x][y]=new Sprite(Image, x*16, y*16);
surface.x=(MapTile[x][y]-1)*16;
surface.y=0;
tile[x][y]->Surface(surface.x, surface.y, 16, 16);
tile[x][y]->Draw();
}
}
}
void Map::Draw()
{
short y, x;
for(y=0;y<30;y++)
{
for(x=0;x<40;x++)
{
surface.x=MapTile[x][y]*16;
surface.y=MapTile[x][y]*16;
tile[x][y]->Surface(surface.x, surface.y, 16, 16); tile[x][y]->Draw();
}
}
}
//--Fonctions--//
SDL_Surface *LoadBMP(const char *filename)//Du tutorial de [DmX]
{
SDL_Surface *temp, *image;
temp=SDL_LoadBMP(filename);
image=SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
if(image==NULL)
{
Error << "Can´t load image\n";
}
return image;
}
void UpdateEvents()
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
key[event.key.keysym.sym]=1;
break;
case SDL_KEYUP:
key[event.key.keysym.sym]=0;
break;
case SDL_MOUSEMOTION:
xsouris=event.motion.x;
ysouris=event.motion.y;
xrelsouris=event.motion.xrel;
yrelsouris=event.motion.yrel;
break;
case SDL_MOUSEBUTTONDOWN:
boutons[event.button.button-1]=1;
break;
case SDL_MOUSEBUTTONUP:
boutons[event.button.button-1]=0;
break;
}
}
}
int main(int argc, char **argv)
{
long Framedelay=50;
bool StopLoop=false;
while(!StopLoop)
{
Init();
if(SDL_GetTicks()>=Framedelay)
{
Loop();
Framedelay=SDL_GetTicks()+50;
Paint();
}
UpdateEvents();
SDL_Flip(Screen);
StopLoop=Endloop();
}
return 0;
}
Moteur.h:
- include <SDL/SDL.h>
//#include <SDl/SDL_tff.h> //-Not used until in version 1.0.1-
//#include <fmod/fmod.h> //-Not used until in version 1.0.1-
- include <fstream>
class GameEngine
{
private:
short iFrameSet;//-Not used until in Versions 1.0.2-
short iWindowX;//-Not used until in Versions 1.0.2-
short iWindowY;//-Not used until in Versions 1.0.2-
public:
GameEngine(char *cName, bool bFullScreen);
};
class Sprite
{
private:
SDL_Surface *Image;
SDL_Rect Rect, Src;
public:
Sprite(char *cDestination, int iX, int iY);
void Surface(int iSrcX, int iSrcY, int iSrcW, int iSrcH);
void Move(int iMoveX, int iMoveY);
void Draw();
};
class Map
{
private:
int MapSize[2];//-Not used until in Versions 1.0.2-
int MapTile[40][30];
SDL_Rect surface;
Sprite *tile[40][30];
char *MapName;
ifstream map;
public:
Map(char *Mapname, char *Image);
void Draw();
void ChangeTile(short x, short y, short TileX, short TileY);//-Not used until in Versions 1.0.3-
};
void Init();
void Loop();
void Paint();
void UpdateEvents();
bool Endloop();
SDL_Surface *LoadBMP(const char *filename);