31
Graphics / Draw map from text file
« on: July 04, 2011, 06:22:46 am »Quote from: "WitchD0ctor"
You never call DrawMap
well here is the problem i cant do Game.Draw(Map[a]) it gives me error
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
You never call DrawMap
#include <SFML\Graphics.hpp>
#include <iostream>
#include <string>
#include <fstream>
int Map[100][100];
int loadCounterX=0;
int loadCounterY=0;
int mapSizeX;
int mapSizeY;
bool once=false;
void LoadMap (const char *filename)
{
std::ifstream openfile(filename);
if(openfile.is_open())
{
while(!openfile.eof())
{
if(once==false)
{
openfile>>mapSizeX>>mapSizeY;
once = true;
}
openfile >> Map[loadCounterX][loadCounterY];
loadCounterX++;
if(loadCounterX>=mapSizeX)
{
loadCounterX=0;
loadCounterY++;
}
}
}
}
void DrawMap()
{
for(int i=0; i<mapSizeX; i++)
for(int a=0; a<mapSizeY; a++)
Game.Draw(Map[i][a]);
}
int WindowWidth = 800, WindowHight = 600, ColorDepth = 32;
std::string WindowTitle = "The Final Game !!!";
sf::RenderWindow Game(sf::VideoMode(WindowWidth, WindowHight, ColorDepth), WindowTitle);
int main()
{
sf::Event Event;
const char *tiles[2];
tiles[1]=("Data/Images/grass.png");
tiles[2]=("Data/Images/dirt.png");
Game.SetFramerateLimit(60);
while(Game.IsOpened())
{
while(Game.GetEvent(Event))
{
}
Game.Clear();
Game.Display();
}
return 0;
}
text file
10 2
1111111111
0000000000
Put "int dir=1" outside the game loopCode: [Select]
...
int dir=1;
...
while (App.IsOpened())
{
...
if(X>=700)dir=-1;
if(X<=0)dir=1;
sprbox.Move(100 * dir * ElapsedTime, 0);
...
}
int main()
{
RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Window");
Event Event;
bullet bullet;
App.SetFramerateLimit(60);
//----------------------------------------------------------------------------------------------------------------
Image imgbox,imghero;
Sprite sprbox,sprhero;
int HeroHP=100,EnemyHP=100 , HeroDmg=10,EnemyDmg=10;
imgbox.LoadFromFile("e.png");
imgbox.CreateMaskFromColor(Color(255,0,255));
imghero.LoadFromFile("h.png");
imghero.CreateMaskFromColor(Color(255,0,255));
sprbox.SetImage(imgbox);
sprhero.SetImage(imghero);
sprbox.SetPosition(20,200);
bullet.Loadfile();
//----------------------------------------------------------------------------------------------------------------
while (App.IsOpened())
{
float ElapsedTime = App.GetFrameTime();
while (App.GetEvent(Event))
{
if (Event.Type == Event::Closed)
App.Close();
// Escape key pressed
if ((Event.Type == Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
if (Event.Type == sf::Event::KeyReleased && Event.Key.Code == sf::Key::Space)
bullet.moveme(App,ElapsedTime);
}
if (App.GetInput().IsKeyDown(Key::Right)) sprhero.Move(100 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(Key::Left)) sprhero.Move(-100 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(Key::Up)) sprhero.Move( 0, -100 * ElapsedTime);
if (App.GetInput().IsKeyDown(Key::Down)) sprhero.Move(0, 100 * ElapsedTime);
App.Clear();
int x=10;
bullet.DrawBullt(App);
if(EnemyHP>=0)
{
if(CheckCollision(sprhero,sprbox))
EnemyHP=EnemyHP-HeroDmg;
int BoxX = sprbox.GetPosition().x;
int BoxY = sprbox.GetPosition().y;
int HeroX = sprhero.GetPosition().x;
int HeroY = sprhero.GetPosition().y;
std::cout<< "HeroHP= " << HeroHP << "\t" << "EnemyHP= " << EnemyHP << "\t" << "X= " << BoxX << "\t" << "Y=" << BoxY << "\t"<< std::endl;
/*
//------------------------ Make enemy follow Hero -------------------------------------------------
if(BoxX<HeroX)
sprbox.Move(20 * ElapsedTime, 0);
if(BoxY<HeroY)
sprbox.Move(0, 20 * ElapsedTime);
if(BoxY>HeroY)
sprbox.Move(0, -20 * ElapsedTime);
if(BoxX>HeroX)
sprbox.Move(-20 * ElapsedTime, 0);
//------------------------ Make enemy follow Hero -------------------------------------------------
*/
int dir=1;
if(BoxX>=700)
{
dir=-1;
//sprbox.Move(100 * dir * ElapsedTime, 0);
}
if(BoxX<=20)
{
dir=1;
sprbox.Move(100 * dir * ElapsedTime, 0);
}
App.Draw(sprbox);
}
App.Draw(sprhero);
App.Display();
}
return 0;
}
HiCode: [Select]
if(X<=700)
sprbox.Move(100* ElapsedTime, 0);
else
sprbox.Move(-200* ElapsedTime, 0);
Everytime sprbox goes LEFT 200, in the next loop, X < 700 , so.....RIGHT again.
You can do something like:Code: [Select]
...
int dir=1;
...
while (App.IsOpened())
{
...
if(X>=700)dir=-1;
if(X<=0)dir=1;
sprbox.Move(100 * dir * ElapsedTime, 0);
...
}
while (App.IsOpened())
{
int X = sprbox.GetPosition().x;
int Y = sprbox.GetPosition().y;
if(X<=700)
sprbox.Move(100* ElapsedTime, 0);
else
sprbox.Move(-200* ElapsedTime, 0);
}
You can do something like:Code: [Select]
...
int dir=1;
...
while (App.IsOpened())
{
...
if(X>=700)dir=-1;
if(X<=0)dir=1;
sprbox.Move(100 * dir * ElapsedTime, 0);
...
}
sf::Event event
while (window.GetEvent(event))
{
if (event.Type == sf::Event::KeyReleased && event.Key.Code == sf::Key::Space)
{
bullet.moveme(App,ElapsedTime);
}
}
Quote from: "Fantasy"i looked at http://www.sfml-dev.org/wiki/en/sources/frame_anim_animated but i didn't really understand a lot from it but i did my best.Did you try the sample ? Just ask if something in particular is unclear.Quote from: "Fantasy"the end result is all i see is Sprt7 and it doesnt loop again to draw Sprt1 i dont know why is thatIn fact all sprites are drawn but the second one goes over the first, the third overs the second, ... the seventh over the sixth, in that order. Why ? Because there is no Display between the Draw.
for(int x=1; x<=7; x++)
{
Game.Draw(Sprt1);
Game.Display();
Game.Draw(Sprt2);
Game.Display();
Game.Draw(Sprt3);
Game.Display();
Game.Draw(Sprt4);
Game.Display();
Game.Draw(Sprt5);
Game.Display();
Game.Draw(Sprt6);
Game.Display();
Game.Draw(Sprt7);
}
int main()
{
Event Event;
Clock Clock;
//Create SFML windows
RenderWindow Game(VideoMode(800,600,32), "Game");
Image Img1;
Sprite Sprt1;
Image Img2;
Sprite Sprt2;
Image Img3;
Sprite Sprt3;
Image Img4;
Sprite Sprt4;
Image Img5;
Sprite Sprt5;
Image Img6;
Sprite Sprt6;
Image Img7;
Sprite Sprt7;
Img1.LoadFromFile("1.png");
Sprt1.SetImage(Img1);
Sprt1.Move(100,100);
Img2.LoadFromFile("2.png");
Sprt2.SetImage(Img2);
Sprt2.Move(100,100);
Img3.LoadFromFile("3.png");
Sprt3.SetImage(Img3);
Sprt3.Move(100,100);
Img4.LoadFromFile("4.png");
Sprt4.SetImage(Img4);
Sprt4.Move(100,100);
Img5.LoadFromFile("5.png");
Sprt5.SetImage(Img5);
Sprt5.Move(100,100);
Img6.LoadFromFile("6.png");
Sprt6.SetImage(Img6);
Sprt6.Move(100,100);
Img7.LoadFromFile("7.png");
Sprt7.SetImage(Img7);
Sprt7.Move(100,100);
Game.UseVerticalSync(true);
//Game loop
while(Game.IsOpened())
{
float Framerate = 1.f / Clock.GetElapsedTime();
Clock.Reset();
//Event handle loop
while(Game.GetEvent(Event))
{
if(Event.Type == Event::Closed)
Game.Close();
if (Game.GetInput().IsKeyDown(Key::Escape))
Game.Close();
if (Game.GetInput().IsKeyDown(Key::Num1))
StartPlaying();
if (Game.GetInput().IsKeyDown(Key::Num2))
Settings();
}
String Text("Press 1 to start playing the game\n"
"Press 2 to open Settings\n"
"Press ESC to Exit the game\n");
Text.SetColor(Color(255, 0, 0));
Text.Move(100.f, 100.f);
//Clear the game window
Game.Clear();
//Display Text
Game.Draw(Text);
//
for(int x=1; x<=7; x++)
{
Game.Draw(Sprt1);
Game.Draw(Sprt2);
Game.Draw(Sprt3);
Game.Draw(Sprt4);
Game.Draw(Sprt5);
Game.Draw(Sprt6);
Game.Draw(Sprt7);
}
//Display SFML graphics to window
Game.Display();
}
return 0;
}
http://www.sfml-dev.org/documentation/1.6/classsf_1_1Image.php#ae9a15fe9a4750295845b5ae081c2ec50
Image.CreateMaskFromColor(Color(255,0,255));
function its not working as i hoped, here is how it looksWell that's because your framerate there will be huge. You never sleep or wait anything so you will be running as fast as the CPU can.
How can you see the GPU consumption?
Can you show us your source code?
#include <SFML/Window.hpp>
int main()
{
sf::Window App(sf::VideoMode(800, 600, 32), "SFML Window");
bool Running = true;
while (Running)
{
App.Display();
}
return 0;
}
Is it really the GPU, or do you mean "the CPU"?