Hi, i have the next problem,
I copy a image of size 64*64, Pero se copia la imagen y el demás espacio de la ventana se estropea.
I here get a video.
The duration of the video is 0:38 seconds
Code.
main.cpp#include <iostream>
using namespace std;
#include "game.hpp"
int main(int argc,char *argv[]){
Game g(640,480,"Team D source enginer");
return 0;
}
game.cpp#include "game.hpp"
Game::Game(int w,int h,char *t){
// DECLARACIONES
myWindow = new sf::RenderWindow(sf::VideoMode(w,h,32),t);
myEvent = new sf::Event;
myNameWindow = new char;
GameMap *Map = new GameMap;
GameLevel *Level = new GameLevel(*Map);
GameKey *Key = new GameKey(*myWindow,*myEvent,*Level);
// DEFINICION
myNameWindow=t;
Map->Create("data/level1.txt");
// METODOS
cout << "Ventana "<<t << " creada.\n";
while(1){
myWindow->clear(sf::Color(255,0,0,133));
Map->State();
Key->State();
Level->State(*myWindow);
myWindow->display();
}
}
Game::~Game(){
delete myWindow;
delete myNameWindow;
}
game.hpp#ifndef _game_
#define _game_
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include "gamelevel.hpp"
#include "gamemap.hpp"
#include "gamekey.hpp"
using namespace std;
class Game {
public:
Game(int w,int h,char *t="Window test");
~Game();
private:
sf::RenderWindow *myWindow;
sf::Event *myEvent;
char *myNameWindow;
};
#endif
gamemap.cpp#include "gamemap.hpp"
GameMap::GameMap(){
FileConfig = new char[100];
IMap = new sf::Image;
SMap = new sf::Sprite;
TMap = new sf::Texture;
NameMap = new char[100];
TileMap = new sf::Image;
NameTileMap = new char[100];
TTileMap = new sf::Texture;
STileMap = new sf::Sprite;
}
GameMap::~GameMap(){
delete[] FileConfig;
delete IMap;
delete SMap;
delete TMap;
delete[] NameMap;
delete TileMap;
delete[] NameTileMap;
delete TTileMap;
delete STileMap;
}
void GameMap::Create(char *t){
int smx,smy,st;
FileConfig=t;
ifstream file(FileConfig);
file >> NameMap;
file >> NameTileMap;
file >> SizeTile;
/*file.close();
file.open(NameMap,ios::in);*/
file >> SizeMapX;
file >> SizeMapY;
for(int y=0;y<SizeMapY;y++){
for(int x=0;x<SizeMapX;x++){
file >> StructMap[y][x];
}
}
file.close();
TileMap->loadFromFile(NameTileMap);
SizeTileMapX=TileMap->getSize().x/(SizeTile);
SizeTileMapY=TileMap->getSize().y/(SizeTile);
for(int i=0,y=0;y<SizeTileMapY;y++){
for(int x=0;x<SizeTileMapX;x++,i++){
StructTileMap[i].x=x*(SizeTile);
StructTileMap[i].y=y*(SizeTile);
}
}
IMap->create(SizeMapX*(SizeTile),SizeMapY*(SizeTile));
for(int y=0;y<SizeMapY;y++){
for(int x=0;x<SizeMapX;x++){
IMap->copy(*TileMap,x*(SizeTile),y*(SizeTile),sf::IntRect(StructTileMap[StructMap[y][x]].x,StructTileMap[StructMap[y][x]].y,32,32));
}
}
TMap->loadFromImage(*IMap);
SMap->setTexture(*TMap);
IMap->saveToFile("save/screen01.png");
}
sf::Sprite GameMap::GetSprite(){
return *SMap;
}
void GameMap::Draw(sf::RenderWindow &a){
a.draw(*SMap);
}
void GameMap::State(){
cout << IMap->getSize().x << " "<< TMap->getSize().x << " "<<endl;
}
gamemap.hpp#define _gamemap_
#include <iostream>
#include <fstream>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
using namespace std;
class GameMap {
public:
GameMap();
~GameMap();
void Create(char*);
sf::Sprite GetSprite();
void Draw(sf::RenderWindow &a);
void State();//For test
private:
char *FileConfig;
sf::Image *IMap;
sf::Sprite *SMap;
sf::Texture *TMap;
sf::Image *TileMap;
sf::Texture *TTileMap;
int StructMap[100][100];
sf::Vector2i StructTileMap[100];
int SizeMapX;
int SizeMapY;
char *NameMap;
char *NameTileMap;
int SizeTileMapX;
int SizeTileMapY;
int SizeTile;
sf::Sprite *STileMap;
};
#endif
gamelevel.cpp#include "gamelevel.hpp"
GameLevel::GameLevel(GameMap &m){
NameLevel = new char;
Map= new GameMap;
Map= &m;
}
GameLevel::~GameLevel(){
delete NameLevel;
delete Map;
}
void GameLevel::SetLevel(char *t){
NameLevel=t;
Map->Create(NameLevel);
}
void GameLevel::State(sf::RenderWindow &w){
Map->Draw(w);
}
gamelevel.hpp#ifndef _gamelevel_
#define _gamelevel_
#include <iostream>
#include "gamemap.hpp"
using namespace std;
class GameLevel {
public:
GameLevel(GameMap &);
~GameLevel();
void State(sf::RenderWindow &);
void SetLevel(char *);
private:
char *NameLevel;
GameMap *Map;
};
#endif
gamekey.cpp#include "gamekey.hpp"
GameKey::GameKey(sf::RenderWindow &w,sf::Event &e,GameLevel &l){
myEvent=&e;
myWindow=&w;
myLevel=&l;
}
GameKey::~GameKey(){
delete myEvent;
delete myWindow;
}
void GameKey::State(){
myWindow->pollEvent(*myEvent);
if(myEvent->type == sf::Event::KeyPressed && myEvent->key.code==sf::Keyboard::Escape){
myWindow->close();
}
if(myEvent->type == sf::Event::KeyPressed && myEvent->key.code==sf::Keyboard::A){
myLevel->SetLevel("data/level2.txt");
}
if(myEvent->type == sf::Event::KeyPressed && myEvent->key.code==sf::Keyboard::S){
myLevel->SetLevel("data/level1.txt");
}
}
gamekey.hpp#ifndef _gamekey_
#define _gamekey_
#include <iostream>
using namespace std;
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include "gamelevel.hpp"
class GameKey {
public:
GameKey(sf::RenderWindow &w,sf::Event &t,GameLevel&);
~GameKey();
void State();
private:
sf::Event *myEvent;
sf::RenderWindow *myWindow;
GameLevel *myLevel;
};
#endif