My renderwndow becomes blank and stops working
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Actually i was trying to make a bullet shooting code.I used vectors and added the sprite into it and the positions in seperate vectors.But when i run my program my window stops working.Here is the code.I hope its not repeated.
#include <SFML/Graphics.hpp>
#include <iostream>
#import "bulletcode.h";
#include <vector>
using namespace std;
using namespace sf;
int main()
vector<Sprite> bullets;
vector<float> xp;
vector<float> yp;
sf::RenderWindow window(sf::VideoMode(900, 600), "SFML works!");
sf::CircleShape shape(75.f);
shape.setFillColor(sf::Color::Green);
Texture bullet;
bullet.loadFromFile("bullet.png");
shape.setPosition(400,100);
while (window.isOpen())
sf::Event event;
while (window.pollEvent(event))
if (event.type == sf::Event::Closed)
window.close();
if(Keyboard::isKeyPressed(Keyboard::Space))
Sprite bulletsp;
bulletsp.setTexture(bullet);
bulletsp.setScale(0.8,0.8);
bullets.push_back(bulletsp);
xp.push_back(shape.getPosition().x);
yp.push_back(shape.getPosition().y);
for(int i=0;i<=bullets.size()-1;i=i)
yp[i]=yp[i]+0.2;
i++;
bullets[i].setPosition(xp[i],yp[i]);
window.draw(bullets[i]);
window.clear();
window.draw(shape);
window.display();
return 0;
c++ sfml
|
show 3 more comments
Actually i was trying to make a bullet shooting code.I used vectors and added the sprite into it and the positions in seperate vectors.But when i run my program my window stops working.Here is the code.I hope its not repeated.
#include <SFML/Graphics.hpp>
#include <iostream>
#import "bulletcode.h";
#include <vector>
using namespace std;
using namespace sf;
int main()
vector<Sprite> bullets;
vector<float> xp;
vector<float> yp;
sf::RenderWindow window(sf::VideoMode(900, 600), "SFML works!");
sf::CircleShape shape(75.f);
shape.setFillColor(sf::Color::Green);
Texture bullet;
bullet.loadFromFile("bullet.png");
shape.setPosition(400,100);
while (window.isOpen())
sf::Event event;
while (window.pollEvent(event))
if (event.type == sf::Event::Closed)
window.close();
if(Keyboard::isKeyPressed(Keyboard::Space))
Sprite bulletsp;
bulletsp.setTexture(bullet);
bulletsp.setScale(0.8,0.8);
bullets.push_back(bulletsp);
xp.push_back(shape.getPosition().x);
yp.push_back(shape.getPosition().y);
for(int i=0;i<=bullets.size()-1;i=i)
yp[i]=yp[i]+0.2;
i++;
bullets[i].setPosition(xp[i],yp[i]);
window.draw(bullets[i]);
window.clear();
window.draw(shape);
window.display();
return 0;
c++ sfml
#import "bulletcode.h";
? Should that read#include "bulletcode.h"
?
– user10605163
Nov 16 '18 at 14:28
for(int i=0;i<=bullets.size()-1;i=i) yp[i]=yp[i]+0.2; i++; bullets[i].setPosition(xp[i],yp[i]); window.draw(bullets[i]); I found that problem is with these line but i dont know whats the problem
– gokul adethya
Nov 16 '18 at 14:29
2
Please edit your question instead of adding comments. There's a link just below the tags.
– Some programmer dude
Nov 16 '18 at 14:30
#include "bulletcode.h"
– gokul adethya
Nov 16 '18 at 14:30
1
;i=i)
is pointless, just write;)
and make sure that you are incrementingi
at the point where you really want to (usually after the full loop body), i.e. wherei=i
is now.
– user10605163
Nov 16 '18 at 14:32
|
show 3 more comments
Actually i was trying to make a bullet shooting code.I used vectors and added the sprite into it and the positions in seperate vectors.But when i run my program my window stops working.Here is the code.I hope its not repeated.
#include <SFML/Graphics.hpp>
#include <iostream>
#import "bulletcode.h";
#include <vector>
using namespace std;
using namespace sf;
int main()
vector<Sprite> bullets;
vector<float> xp;
vector<float> yp;
sf::RenderWindow window(sf::VideoMode(900, 600), "SFML works!");
sf::CircleShape shape(75.f);
shape.setFillColor(sf::Color::Green);
Texture bullet;
bullet.loadFromFile("bullet.png");
shape.setPosition(400,100);
while (window.isOpen())
sf::Event event;
while (window.pollEvent(event))
if (event.type == sf::Event::Closed)
window.close();
if(Keyboard::isKeyPressed(Keyboard::Space))
Sprite bulletsp;
bulletsp.setTexture(bullet);
bulletsp.setScale(0.8,0.8);
bullets.push_back(bulletsp);
xp.push_back(shape.getPosition().x);
yp.push_back(shape.getPosition().y);
for(int i=0;i<=bullets.size()-1;i=i)
yp[i]=yp[i]+0.2;
i++;
bullets[i].setPosition(xp[i],yp[i]);
window.draw(bullets[i]);
window.clear();
window.draw(shape);
window.display();
return 0;
c++ sfml
Actually i was trying to make a bullet shooting code.I used vectors and added the sprite into it and the positions in seperate vectors.But when i run my program my window stops working.Here is the code.I hope its not repeated.
#include <SFML/Graphics.hpp>
#include <iostream>
#import "bulletcode.h";
#include <vector>
using namespace std;
using namespace sf;
int main()
vector<Sprite> bullets;
vector<float> xp;
vector<float> yp;
sf::RenderWindow window(sf::VideoMode(900, 600), "SFML works!");
sf::CircleShape shape(75.f);
shape.setFillColor(sf::Color::Green);
Texture bullet;
bullet.loadFromFile("bullet.png");
shape.setPosition(400,100);
while (window.isOpen())
sf::Event event;
while (window.pollEvent(event))
if (event.type == sf::Event::Closed)
window.close();
if(Keyboard::isKeyPressed(Keyboard::Space))
Sprite bulletsp;
bulletsp.setTexture(bullet);
bulletsp.setScale(0.8,0.8);
bullets.push_back(bulletsp);
xp.push_back(shape.getPosition().x);
yp.push_back(shape.getPosition().y);
for(int i=0;i<=bullets.size()-1;i=i)
yp[i]=yp[i]+0.2;
i++;
bullets[i].setPosition(xp[i],yp[i]);
window.draw(bullets[i]);
window.clear();
window.draw(shape);
window.display();
return 0;
c++ sfml
c++ sfml
asked Nov 16 '18 at 14:27
gokul adethyagokul adethya
12
12
#import "bulletcode.h";
? Should that read#include "bulletcode.h"
?
– user10605163
Nov 16 '18 at 14:28
for(int i=0;i<=bullets.size()-1;i=i) yp[i]=yp[i]+0.2; i++; bullets[i].setPosition(xp[i],yp[i]); window.draw(bullets[i]); I found that problem is with these line but i dont know whats the problem
– gokul adethya
Nov 16 '18 at 14:29
2
Please edit your question instead of adding comments. There's a link just below the tags.
– Some programmer dude
Nov 16 '18 at 14:30
#include "bulletcode.h"
– gokul adethya
Nov 16 '18 at 14:30
1
;i=i)
is pointless, just write;)
and make sure that you are incrementingi
at the point where you really want to (usually after the full loop body), i.e. wherei=i
is now.
– user10605163
Nov 16 '18 at 14:32
|
show 3 more comments
#import "bulletcode.h";
? Should that read#include "bulletcode.h"
?
– user10605163
Nov 16 '18 at 14:28
for(int i=0;i<=bullets.size()-1;i=i) yp[i]=yp[i]+0.2; i++; bullets[i].setPosition(xp[i],yp[i]); window.draw(bullets[i]); I found that problem is with these line but i dont know whats the problem
– gokul adethya
Nov 16 '18 at 14:29
2
Please edit your question instead of adding comments. There's a link just below the tags.
– Some programmer dude
Nov 16 '18 at 14:30
#include "bulletcode.h"
– gokul adethya
Nov 16 '18 at 14:30
1
;i=i)
is pointless, just write;)
and make sure that you are incrementingi
at the point where you really want to (usually after the full loop body), i.e. wherei=i
is now.
– user10605163
Nov 16 '18 at 14:32
#import "bulletcode.h";
? Should that read #include "bulletcode.h"
?– user10605163
Nov 16 '18 at 14:28
#import "bulletcode.h";
? Should that read #include "bulletcode.h"
?– user10605163
Nov 16 '18 at 14:28
for(int i=0;i<=bullets.size()-1;i=i) yp[i]=yp[i]+0.2; i++; bullets[i].setPosition(xp[i],yp[i]); window.draw(bullets[i]); I found that problem is with these line but i dont know whats the problem
– gokul adethya
Nov 16 '18 at 14:29
for(int i=0;i<=bullets.size()-1;i=i) yp[i]=yp[i]+0.2; i++; bullets[i].setPosition(xp[i],yp[i]); window.draw(bullets[i]); I found that problem is with these line but i dont know whats the problem
– gokul adethya
Nov 16 '18 at 14:29
2
2
Please edit your question instead of adding comments. There's a link just below the tags.
– Some programmer dude
Nov 16 '18 at 14:30
Please edit your question instead of adding comments. There's a link just below the tags.
– Some programmer dude
Nov 16 '18 at 14:30
#include "bulletcode.h"
– gokul adethya
Nov 16 '18 at 14:30
#include "bulletcode.h"
– gokul adethya
Nov 16 '18 at 14:30
1
1
;i=i)
is pointless, just write ;)
and make sure that you are incrementing i
at the point where you really want to (usually after the full loop body), i.e. where i=i
is now.– user10605163
Nov 16 '18 at 14:32
;i=i)
is pointless, just write ;)
and make sure that you are incrementing i
at the point where you really want to (usually after the full loop body), i.e. where i=i
is now.– user10605163
Nov 16 '18 at 14:32
|
show 3 more comments
2 Answers
2
active
oldest
votes
You're incrementing i in the loop. So on the last iteration, you'll be accessing beyond the ends of the arrays.
You probably want to replace the i=i
in the loop declaration with i++
, and not increment i in the loop body.
Its still not working
– gokul adethya
Nov 16 '18 at 14:35
Then you have other problems to debug. ;) At another quick glance, you'll probably never see your bullets because you're clearing the window after drawing them.
– James
Nov 16 '18 at 14:42
add a comment |
Your program does not work because of the for loop:
for(int i=0;i<=bullets.size()-1;i=i)
where bullets.size()
is a size_t (unsigned int)
. When your bullets vector is empty (size() = 0
), the condition i=0 <= (unsigned int)(0 - 1)
is always true. Your program enters the for body and crashed (yp, xp, bullets are empty).
Your compilers should warn you somewhere about the problem ?
You should use for(size_t i=0;i < bullets.size(); i++ )
and rework your for body codes, this keeps your code clean and avoid source of problems.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339765%2fmy-renderwndow-becomes-blank-and-stops-working%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You're incrementing i in the loop. So on the last iteration, you'll be accessing beyond the ends of the arrays.
You probably want to replace the i=i
in the loop declaration with i++
, and not increment i in the loop body.
Its still not working
– gokul adethya
Nov 16 '18 at 14:35
Then you have other problems to debug. ;) At another quick glance, you'll probably never see your bullets because you're clearing the window after drawing them.
– James
Nov 16 '18 at 14:42
add a comment |
You're incrementing i in the loop. So on the last iteration, you'll be accessing beyond the ends of the arrays.
You probably want to replace the i=i
in the loop declaration with i++
, and not increment i in the loop body.
Its still not working
– gokul adethya
Nov 16 '18 at 14:35
Then you have other problems to debug. ;) At another quick glance, you'll probably never see your bullets because you're clearing the window after drawing them.
– James
Nov 16 '18 at 14:42
add a comment |
You're incrementing i in the loop. So on the last iteration, you'll be accessing beyond the ends of the arrays.
You probably want to replace the i=i
in the loop declaration with i++
, and not increment i in the loop body.
You're incrementing i in the loop. So on the last iteration, you'll be accessing beyond the ends of the arrays.
You probably want to replace the i=i
in the loop declaration with i++
, and not increment i in the loop body.
answered Nov 16 '18 at 14:32
JamesJames
1295
1295
Its still not working
– gokul adethya
Nov 16 '18 at 14:35
Then you have other problems to debug. ;) At another quick glance, you'll probably never see your bullets because you're clearing the window after drawing them.
– James
Nov 16 '18 at 14:42
add a comment |
Its still not working
– gokul adethya
Nov 16 '18 at 14:35
Then you have other problems to debug. ;) At another quick glance, you'll probably never see your bullets because you're clearing the window after drawing them.
– James
Nov 16 '18 at 14:42
Its still not working
– gokul adethya
Nov 16 '18 at 14:35
Its still not working
– gokul adethya
Nov 16 '18 at 14:35
Then you have other problems to debug. ;) At another quick glance, you'll probably never see your bullets because you're clearing the window after drawing them.
– James
Nov 16 '18 at 14:42
Then you have other problems to debug. ;) At another quick glance, you'll probably never see your bullets because you're clearing the window after drawing them.
– James
Nov 16 '18 at 14:42
add a comment |
Your program does not work because of the for loop:
for(int i=0;i<=bullets.size()-1;i=i)
where bullets.size()
is a size_t (unsigned int)
. When your bullets vector is empty (size() = 0
), the condition i=0 <= (unsigned int)(0 - 1)
is always true. Your program enters the for body and crashed (yp, xp, bullets are empty).
Your compilers should warn you somewhere about the problem ?
You should use for(size_t i=0;i < bullets.size(); i++ )
and rework your for body codes, this keeps your code clean and avoid source of problems.
add a comment |
Your program does not work because of the for loop:
for(int i=0;i<=bullets.size()-1;i=i)
where bullets.size()
is a size_t (unsigned int)
. When your bullets vector is empty (size() = 0
), the condition i=0 <= (unsigned int)(0 - 1)
is always true. Your program enters the for body and crashed (yp, xp, bullets are empty).
Your compilers should warn you somewhere about the problem ?
You should use for(size_t i=0;i < bullets.size(); i++ )
and rework your for body codes, this keeps your code clean and avoid source of problems.
add a comment |
Your program does not work because of the for loop:
for(int i=0;i<=bullets.size()-1;i=i)
where bullets.size()
is a size_t (unsigned int)
. When your bullets vector is empty (size() = 0
), the condition i=0 <= (unsigned int)(0 - 1)
is always true. Your program enters the for body and crashed (yp, xp, bullets are empty).
Your compilers should warn you somewhere about the problem ?
You should use for(size_t i=0;i < bullets.size(); i++ )
and rework your for body codes, this keeps your code clean and avoid source of problems.
Your program does not work because of the for loop:
for(int i=0;i<=bullets.size()-1;i=i)
where bullets.size()
is a size_t (unsigned int)
. When your bullets vector is empty (size() = 0
), the condition i=0 <= (unsigned int)(0 - 1)
is always true. Your program enters the for body and crashed (yp, xp, bullets are empty).
Your compilers should warn you somewhere about the problem ?
You should use for(size_t i=0;i < bullets.size(); i++ )
and rework your for body codes, this keeps your code clean and avoid source of problems.
edited Nov 16 '18 at 15:31
answered Nov 16 '18 at 15:03
tunglttunglt
6941311
6941311
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339765%2fmy-renderwndow-becomes-blank-and-stops-working%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
#import "bulletcode.h";
? Should that read#include "bulletcode.h"
?– user10605163
Nov 16 '18 at 14:28
for(int i=0;i<=bullets.size()-1;i=i) yp[i]=yp[i]+0.2; i++; bullets[i].setPosition(xp[i],yp[i]); window.draw(bullets[i]); I found that problem is with these line but i dont know whats the problem
– gokul adethya
Nov 16 '18 at 14:29
2
Please edit your question instead of adding comments. There's a link just below the tags.
– Some programmer dude
Nov 16 '18 at 14:30
#include "bulletcode.h"
– gokul adethya
Nov 16 '18 at 14:30
1
;i=i)
is pointless, just write;)
and make sure that you are incrementingi
at the point where you really want to (usually after the full loop body), i.e. wherei=i
is now.– user10605163
Nov 16 '18 at 14:32