Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: ERROR!  (Read 3612 times)

0 Members and 1 Guest are viewing this topic.

wtf

  • Newbie
  • *
  • Posts: 15
    • View Profile
ERROR!
« on: August 03, 2011, 07:38:52 am »


I believe it might be this part of the code...
Code: [Select]
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!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
ERROR!
« Reply #1 on: August 03, 2011, 07:59:49 am »
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).
Laurent Gomila - SFML developer

wtf

  • Newbie
  • *
  • Posts: 15
    • View Profile
ERROR!
« Reply #2 on: August 03, 2011, 08:08:51 am »
Here is my whole source code. Hopefully you can help me with this.

Main.cpp
Code: [Select]
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Engine.h"

int main()
{
    Engine Game;
    Game.Run();

    return EXIT_SUCCESS;
}


Engine.h
Code: [Select]
#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
Code: [Select]
#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
Code: [Select]
#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
Code: [Select]
#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();
}

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
ERROR!
« Reply #3 on: August 03, 2011, 08:12:37 am »
I think it comes from there :

Code: [Select]
Heart[2].SetImage(heart1);
Heart[2].Scale(2.0f, 2.0f);


Look at the declaration :

Code: [Select]
sf::Sprite Heart[2];

wtf

  • Newbie
  • *
  • Posts: 15
    • View Profile
ERROR!
« Reply #4 on: August 03, 2011, 08:23:47 am »
Quote from: "Lo-X"
I think it comes from there :

Code: [Select]
Heart[2].SetImage(heart1);
Heart[2].Scale(2.0f, 2.0f);


Look at the declaration :

Code: [Select]
sf::Sprite Heart[2];


What exactly do you mean by this?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
ERROR!
« Reply #5 on: August 03, 2011, 08:42:55 am »
Your array contains two elements but you're accessing the third (indices start at 0).
Laurent Gomila - SFML developer

wtf

  • Newbie
  • *
  • Posts: 15
    • View Profile
ERROR!
« Reply #6 on: August 03, 2011, 08:47:59 am »
Thank you for your help. i now have it working properly.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
ERROR!
« Reply #7 on: August 03, 2011, 10:03:55 am »
By the way, if you used std::array or std::vector instead of raw arrays, such errors could be found in a few seconds ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Silvah

  • Guest
ERROR!
« Reply #8 on: August 03, 2011, 03:47:38 pm »
Quote from: "Nexus"
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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
ERROR!
« Reply #9 on: August 03, 2011, 04:24:17 pm »
Quote from: "Silvah"
...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.

Quote from: "Silvah"
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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Silvah

  • Guest
ERROR!
« Reply #10 on: August 04, 2011, 04:18:11 pm »
Quote from: "Nexus"
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++.