3
« on: July 03, 2009, 08:17:39 am »
Im using a Radeon x1050 with ati's drivers, no problems in most games.
The game im writing on is a topdown shooter.
Doesnt use any graphics that SHOULD be heavy to render, only thing i can come to think about is a png image, 800x600, thats rendered every frame.. But that shouldnt be so bad?
The FPS in this game is dropping from 75(1000fps without vsync) to 37 fps :S
Here is the sourcecode to the whole game, i know its not that optimally coded, but i started writing on this just a few hours ago.
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
#include <sstream>
template <class T>
inline std::string to_string (const T& t)
{
std::stringstream ss;
ss << t;
return ss.str();
}
sf::Image img_rocket;
sf::Image img_jet;
sf::Image img_fighter;
sf::Image img_bullet;
sf::Image img_sky;
sf::Image gui_wepstats_bg;
sf::Image gui_wepstats_nomiss;
sf::Image gui_wepstats_miss;
sf::Sprite sgui_wepstats_bg;
sf::Sprite sgui_wepstats_missile1;
sf::Sprite sgui_wepstats_missile2;
sf::Sprite sgui_wepstats_missile3;
sf::Sprite sgui_wepstats_missile4;
sf::Sprite simg_sky;
sf::Font fnt_debug;
sf::String str_debug;
float ElapsedTime;
int fps = 0;
int currpyl;
int pylonlimiter;
int pylonlimit = 10;
int currcan;
int cannonlimiter;
int cannonlimit = 4;
int currbull = 0;
typedef class rocket{
public:
bool active;
int speed;
sf::Sprite sprite;
};
typedef class pylon{
public:
float x;
float y;
rocket rcket;
};
typedef class jet{
public:
sf::Sprite sprite;
};
typedef class bullet{
public:
sf::Sprite sprite;
bool active;
};
struct fighter{
public:
sf::Sprite sprite;
pylon pylons[4];
jet jets[2];
bullet bullets[128];
void load();
void update();
};
void fighter::load(){
img_fighter.LoadFromFile("data/f22.png");
img_jet.LoadFromFile("data/f22jet.png");
img_rocket.LoadFromFile("data/missile.png");
img_bullet.LoadFromFile("data/bullet.png");
for(int i = 0;i < 128;i++){
this->bullets[i].sprite.SetImage(img_bullet);
}
this->sprite.SetImage(img_fighter);
this->sprite.SetPosition( (800/2) - this->sprite.GetCenter().x, 480 );
this->jets[0].sprite.SetImage(img_jet);
this->jets[1].sprite.SetImage(img_jet);
this->pylons[0].rcket.sprite.SetImage(img_rocket);
this->pylons[1].rcket.sprite.SetImage(img_rocket);
this->pylons[2].rcket.sprite.SetImage(img_rocket);
this->pylons[3].rcket.sprite.SetImage(img_rocket);
}
void fighter::update(){
if(currbull == 129){
currbull = 0;
}
this->pylons[0].x = this->sprite.GetPosition().x + 11;
this->pylons[0].y = this->sprite.GetPosition().y + 84;
this->pylons[1].x = this->sprite.GetPosition().x + 21;
this->pylons[1].y = this->sprite.GetPosition().y + 75;
this->pylons[2].x = this->sprite.GetPosition().x + 76;
this->pylons[2].y = this->sprite.GetPosition().y + 75;
this->pylons[3].x = this->sprite.GetPosition().x + 86;
this->pylons[3].y = this->sprite.GetPosition().y + 84;
for(int i = 0;i < 4;i++){
if(this->pylons[i].rcket.active == true){
//continue path for rocket
this->pylons[i].rcket.speed += 1200*ElapsedTime;
this->pylons[i].rcket.sprite.Move(0, -this->pylons[i].rcket.speed*ElapsedTime);
if(this->pylons[i].rcket.sprite.GetPosition().y < 0){
this->pylons[i].rcket.active = false;
switch(i){
case 0:
sgui_wepstats_missile1.SetImage(gui_wepstats_miss);
break;
case 1:
sgui_wepstats_missile2.SetImage(gui_wepstats_miss);
break;
case 2:
sgui_wepstats_missile3.SetImage(gui_wepstats_miss);
break;
case 3:
sgui_wepstats_missile4.SetImage(gui_wepstats_miss);
break;
}
}
}else{
this->pylons[i].rcket.speed = 0;
this->pylons[i].rcket.sprite.SetPosition(this->pylons[i].x, this->pylons[i].y);
}
}
for(int i = 0;i < 128;i++){
if(this->bullets[i].active){
this->bullets[i].sprite.Move(0, -1000*ElapsedTime);
if(this->bullets[i].sprite.GetPosition().y < 0){
this->bullets[i].active = false;
}
}else{
if(currcan == 0){
this->bullets[i].sprite.SetPosition(this->sprite.GetPosition().x+41,this->sprite.GetPosition().y+53);
}else{
this->bullets[i].sprite.SetPosition(this->sprite.GetPosition().x + 61,this->sprite.GetPosition().y+53);
}
}
}
this->jets[0].sprite.SetPosition( (this->sprite.GetPosition().x + 43), (this->sprite.GetPosition().y + 137) );
this->jets[1].sprite.SetPosition( (this->sprite.GetPosition().x + 53), (this->sprite.GetPosition().y + 137) );
if(currpyl == 4){
currpyl = 0;
}
if(pylonlimiter < pylonlimit){
pylonlimiter+= 100*ElapsedTime;
}
if(currcan == 2){
currcan = 0;
}
if(cannonlimiter < cannonlimit){
cannonlimiter+= 100*ElapsedTime;
}
}
int main(){
sf::RenderWindow wind(sf::VideoMode(800, 600, 32), "fighterjet");
wind.UseVerticalSync(true);
wind.SetFramerateLimit(75);
fighter player1;
player1.load();
img_sky.LoadFromFile("data/clearsky.png");
simg_sky.SetImage(img_sky);
gui_wepstats_bg.LoadFromFile("data/hud_stats_bg.png");
gui_wepstats_miss.LoadFromFile("data/hud_miss.png");
gui_wepstats_nomiss.LoadFromFile("data/hud_nomiss.png");
sgui_wepstats_bg.SetPosition(8, 8);
sgui_wepstats_bg.SetImage(gui_wepstats_bg);
sgui_wepstats_missile1.SetPosition(8+15, 8+48);
sgui_wepstats_missile2.SetPosition(8+24, 8+44);
sgui_wepstats_missile3.SetPosition(8+56, 8+44);
sgui_wepstats_missile4.SetPosition(8+65, 8+48);
sgui_wepstats_missile1.SetImage(gui_wepstats_miss);
sgui_wepstats_missile2.SetImage(gui_wepstats_miss);
sgui_wepstats_missile3.SetImage(gui_wepstats_miss);
sgui_wepstats_missile4.SetImage(gui_wepstats_miss);
fnt_debug.LoadFromFile("tahoma.ttf");
str_debug.SetFont(fnt_debug);
str_debug.SetSize(24);
while (wind.IsOpened())
{
str_debug.SetPosition(800-64,8);
str_debug.SetText(to_string(fps));
fps = 1.f / wind.GetFrameTime();
ElapsedTime = wind.GetFrameTime();
sf::Event Event;
while (wind.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
{
wind.Close();
}
// F12 : Screenshot
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::F12))
{
sf::Image Screen = wind.Capture();
Screen.SaveToFile("screenshot.jpg");
}
}
if (wind.GetInput().IsKeyDown(sf::Key::Left)) player1.sprite.Move(-500 * ElapsedTime, 0);
if (wind.GetInput().IsKeyDown(sf::Key::Right)) player1.sprite.Move( 500 * ElapsedTime, 0);
if (wind.GetInput().IsKeyDown(sf::Key::Up)) player1.sprite.Move(0, -300 * ElapsedTime);
if (wind.GetInput().IsKeyDown(sf::Key::Down)) player1.sprite.Move(0, 300 * ElapsedTime);
if (wind.GetInput().IsKeyDown(sf::Key::X)){
// Cannon
if(cannonlimiter >= cannonlimit){
cannonlimiter = 0;
player1.bullets[currbull].active = true;
currcan++;
currbull++;
}
}
if (wind.GetInput().IsKeyDown(sf::Key::Z)){
// Missile
if(pylonlimiter >= pylonlimit){
pylonlimiter = 0;
player1.pylons[currpyl].rcket.active = true;
switch(currpyl){
case 0:
sgui_wepstats_missile1.SetImage(gui_wepstats_nomiss);
break;
case 1:
sgui_wepstats_missile2.SetImage(gui_wepstats_nomiss);
break;
case 2:
sgui_wepstats_missile3.SetImage(gui_wepstats_nomiss);
break;
case 3:
sgui_wepstats_missile4.SetImage(gui_wepstats_nomiss);
break;
}
currpyl++;
}
}
player1.update();
wind.Clear();
wind.Draw(simg_sky);
wind.Draw(player1.pylons[0].rcket.sprite);
wind.Draw(player1.pylons[1].rcket.sprite);
wind.Draw(player1.pylons[2].rcket.sprite);
wind.Draw(player1.pylons[3].rcket.sprite);
wind.Draw(player1.jets[0].sprite);
wind.Draw(player1.jets[1].sprite);
wind.Draw(player1.sprite);
for(int i = 0;i < 128;i++){
if(player1.bullets[i].active){
wind.Draw(player1.bullets[i].sprite);
}
}
wind.Draw(sgui_wepstats_bg);
wind.Draw(sgui_wepstats_missile1);
wind.Draw(sgui_wepstats_missile2);
wind.Draw(sgui_wepstats_missile3);
wind.Draw(sgui_wepstats_missile4);
wind.Draw(str_debug);
wind.Display();
}
return EXIT_SUCCESS;
}