SFML community forums
Help => General => Topic started by: wtf on August 03, 2011, 07:38:52 am
-
(http://i51.tinypic.com/2mwv2hh.jpg)
I believe it might be this part of the code...
int x = 761;
int y = 20;
for(int i=0; i < 2; i++)
{
Heart[i].SetPosition(x, y);
x -= 20;
}
Can anyone PLEASE help me with this!
-
You're accessing a NULL pointer. It's hard to tell you more without more code. But you'd better use the debugger to know what happens (and which variable is NULL).
-
Here is my whole source code. Hopefully you can help me with this.
Main.cpp
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Engine.h"
int main()
{
Engine Game;
Game.Run();
return EXIT_SUCCESS;
}
Engine.h
#ifndef ENGINE_H
#define ENGINE_H
class Engine
{
public:
Engine();
~Engine();
void Init();
void HandleEvents();
void Update();
void Render();
void Cleanup();
void Run();
protected:
private:
sf::RenderWindow App;
sf::Event Event;
sf::Clock Clock;
int fps;
};
#endif
Engine.cpp
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Engine.h"
#include "GUI.h"
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;
GUI gui;
Engine::Engine():
fps(0)
{
App.Create(sf::VideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP), "Engine");
sf::Sleep(1.f);
}
Engine::~Engine()
{
}
void Engine::Init()
{
}
void Engine::HandleEvents()
{
if(Event.Type == sf::Event::Closed)
App.Close();
}
void Engine::Update()
{
fps++;
if(Clock.GetElapsedTime() >= 1.f)
{
fps++;
if(Clock.GetElapsedTime() >= 1.f)
{
int Framerate = (1.f / Clock.GetElapsedTime()) * 1000;
std::cout << "FPS: " << Framerate<< std::endl;
Clock.Reset();
}
}
}
void Engine::Render()
{
App.Clear(sf::Color(0,0,0));
App.Draw(gui.Rupee_GUI);
App.Draw(gui.Rupee_Amount);
App.Draw(gui.Arrow_GUI);
App.Draw(gui.Arrow_Amount);
App.Draw(gui.Bomb_GUI);
App.Draw(gui.Bomb_Amount);
App.Draw(gui.Heart[2]);
App.Display();
}
void Engine::Cleanup()
{
}
void Engine::Run()
{
Init();
while(App.IsOpened())
{
while(App.PollEvent(Event))
{
HandleEvents();
}
Update();
Render();
}
Cleanup();
}
GUI.h
#ifndef GUI_H
#define GUI_H
class GUI
{
public:
GUI(void);
~GUI(void);
sf::Text Rupee_Amount;
sf::Image rupee_GUI;
sf::Sprite Rupee_GUI;
sf::Text Arrow_Amount;
sf::Image arrow_GUI;
sf::Sprite Arrow_GUI;
sf::Text Bomb_Amount;
sf::Image bomb_GUI;
sf::Sprite Bomb_GUI;
sf::Image heart1;
sf::Image heart2;
sf::Image heart3;
sf::Image heart4;
sf::Image heart5;
int i;
sf::Sprite Heart[2];
int Rupees;
int Bombs;
int Arrows;
private:
};
#endif
GUI.cpp
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
#include <sstream>
#include "GUI.h"
std::string IntToString (int number);
GUI::GUI(void)
{
sf::Font Font;
Font.LoadFromFile("Fonts/RetGanon.ttf");
// RUPEES**********************************************************
// IMAGE
rupee_GUI.LoadFromFile("Sprites/Rupee_Gui_Img.png");
Rupee_GUI.SetImage(rupee_GUI);
Rupee_GUI.SetPosition(30.0f, 558.0f);
// TEXT
std::string Rupees_Text = IntToString(Rupees);
Rupee_Amount.SetString(Rupees_Text);
Rupee_Amount.SetCharacterSize(16u);
Rupee_Amount.SetStyle(sf::Text::Bold);
Rupee_Amount.SetColor(sf::Color(252,253,255));
Rupee_Amount.Move(45.0f, 554.0f);
// ARROWS **********************************************************
// IMAGE
arrow_GUI.LoadFromFile("Sprites/Arrow_GUI.png");
Arrow_GUI.SetImage(arrow_GUI);
Arrow_GUI.SetPosition(355, 5);
// TEXT
std::string Arrow_Text = IntToString(Arrows);
Arrow_Amount.SetString(Arrow_Text);
Arrow_Amount.SetCharacterSize(16u);
Arrow_Amount.SetStyle(sf::Text::Bold);
Arrow_Amount.SetColor(sf::Color(252,253,255));
Arrow_Amount.Move(350,23);
// Bombs *************************************************************
// IMAGE
bomb_GUI.LoadFromFile("Sprites/Bomb_GUI.png");
Bomb_GUI.SetImage(bomb_GUI);
Bomb_GUI.SetPosition(450,7);
// TEXT
std::string Bomb_Text = IntToString(Bombs);
Bomb_Amount.SetString(Bomb_Text);
Bomb_Amount.SetCharacterSize(16u);
Bomb_Amount.SetStyle(sf::Text::Bold);
Bomb_Amount.SetColor(sf::Color(252,253,255));
Bomb_Amount.Move(445,23);
// HARTS ***************************************************************
heart1.LoadFromFile("Sprites/Hart1.png");
heart2.LoadFromFile("Sprites/Hart2.png");
heart3.LoadFromFile("Sprites/Hart3.png");
heart4.LoadFromFile("Sprites/Hart4.png");
heart5.LoadFromFile("Sprites/Hart5.png");
Heart[2].SetImage(heart1);
Heart[2].Scale(2.0f, 2.0f);
int x = 761;
int y = 20;
for(int i=0; i < 2; i++)
{
Heart[i].SetPosition(x, y);
x -= 20;
}
/*Heart1.SetPosition(761, 20);
Heart2.SetPosition(741, 20);
Heart3.SetPosition(721, 20);*/
}
GUI::~GUI(void)
{
}
std::string IntToString (int number)
{
std::ostringstream oss;
if(number <= 9)
{
oss << "00" << number;
}
else if(number <= 99 && number >= 10)
{
oss << "0" << number;
}
else if(number <= 999 && number >= 100)
{
oss << number;
}
return oss.str();
}
-
I think it comes from there :
Heart[2].SetImage(heart1);
Heart[2].Scale(2.0f, 2.0f);
Look at the declaration :
sf::Sprite Heart[2];
-
I think it comes from there :
Heart[2].SetImage(heart1);
Heart[2].Scale(2.0f, 2.0f);
Look at the declaration :
sf::Sprite Heart[2];
What exactly do you mean by this?
-
Your array contains two elements but you're accessing the third (indices start at 0).
-
Thank you for your help. i now have it working properly.
-
By the way, if you used std::array or std::vector instead of raw arrays, such errors could be found in a few seconds ;)
-
By the way, if you used std::array or std::vector instead of raw arrays, such errors could be found in a few seconds ;)
...assuming the standard library one's using actually checks the indices. Not all do, mind you.
-
...assuming the standard library one's using actually checks the indices.
Of course, that's what I assume. A standard library that doesn't provide trivially implementable debugging checks is poorly designed and should only be used if there is no other option.
Not all do, mind you.
Which one does not (of course in Debug mode)? I can't think of a standard library supporting C++0x library features while being too stupid to provide simple debug assertions.
In fact, those checks are one of the big advantages of std::array over raw arrays. Actually, there are only advantages, I see absolutely no reason to work with the error-prone C style arrays when std::array is available.
-
Which one does not (of course in Debug mode)?
I dunno, really. But I do know a standard library which has a debug mode, but that's it; most people don't know it has one, much less how to turn it on. It's libstdc++ (http://gcc.gnu.org/libstdc++/).