Smart pointer help
Sep 8, 2014 at 4:23am UTC
Ok so in my program I am using smart pointers because the GUI i'm using uses them but I don't know how to use them in my program, I'm having trouble with pointers in general, I am reading about them and trying to learn but nothing I do seems to work. I am creating new instances of SFGUI_Image which I shouldnt do, I need to use the pointer to SFGUI_Image but how? Pointer stuff is coming back to me now, reading through my book and looking at the examples, however i'm unsure about using it for an image, i'm used to working only with numbers, and on top of that it's a smart pointer so is the method of accessing the image the same as accessing a number like in my code below?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <SFGUI/SFGUI.hpp>
#include <iostream>
#include <string>
using namespace std;
class Image
{
public :
Image();
void Load();
void Show();
int GetWidth() const {return imageWidth;}
void SetWidth(int width){imageWidth = width;}
int GetHeight() const {return imageHeight;}
void SetHeight(int height){imageHeight = height;}
private :
sfg::Image::Ptr image;
int imageHeight;
int imageWidth;
};
Image::Image()
{
imageWidth = 256;
imageHeight = 512;
}
void Image::Load()
{
}
void Image::Show()
{
}
int main()
{
Image *img = new Image;
cout << "Image height is: " << img->GetHeight() << endl;
cout << "Image width is: " << img->GetWidth() << endl;
return 0;
}
Here is the code i'm trying to use with the smart pointers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
#include <SFML/Graphics.hpp>
#include <SFGUI/SFGUI.hpp>
#include <iostream>
using namespace std;
sf::RenderWindow window(sf::VideoMode(1024, 768), "Tile Editor" );
class Desktop
{
public :
Desktop();
void Run();
void CreateTileSheetWindow();
void CreateMenuBar();
void LoadImage();
sfg::Image::Ptr GetImage() const {return SFGUI_Image;}
void SetImage(sfg::Image::Ptr img){SFGUI_Image = img;}
private :
static const unsigned int screen_width;
static const unsigned int screen_height;
sfg::SFGUI SFGUI_sfgui;
sfg::Image::Ptr SFGUI_Image;
sfg::Desktop SFGUI_Desktop;
sfg::Window::Ptr SFGUI_MenuBar;
sfg::Window::Ptr SFGUI_TileSheetWindow;
sfg::ComboBox::Ptr SFGUI_File;
sfg::ComboBox::Ptr SFGUI_Edit;
sf::Image SFML_TileSheet;
};
Desktop::Desktop(): SFGUI_Desktop()
{
if (SFML_TileSheet.loadFromFile("Outside_A5.png" ))
{
SFGUI_Image->SetImage(SFML_TileSheet);
}
}
void Desktop::LoadImage()
{
}
void Desktop::CreateTileSheetWindow()
{
Desktop *D = new Desktop;
auto SFGUI_TileSheetWindow = sfg::Window::Create();
auto SFGUI_notebook = sfg::Notebook::Create();
SFGUI_notebook->AppendPage(D->GetImage(), sfg::Label::Create("TileSheet 1" ));
SFGUI_notebook->SetScrollable(true );
SFGUI_TileSheetWindow->Add(SFGUI_notebook);
SFGUI_Desktop.Add(SFGUI_TileSheetWindow);
}
void Desktop::CreateMenuBar()
{
auto SFGUI_MenuBar = sfg::Window::Create();
auto SFGUI_File = sfg::ComboBox::Create();
SFGUI_File->AppendItem("Save" );
SFGUI_File->AppendItem("Open" );
auto MenuBox = sfg::Box::Create(sfg::Box::Orientation::HORIZONTAL, 5);
MenuBox->Pack(SFGUI_File);
SFGUI_MenuBar->SetStyle(SFGUI_MenuBar->GetStyle() ^ sfg::Window::TITLEBAR);
SFGUI_MenuBar->Add(SFGUI_File);
SFGUI_Desktop.Add(SFGUI_MenuBar);
}
int main()
{
Desktop desktop;
desktop.Run();
return 0;
}
void Desktop::Run()
{
sf::Event event;
window.resetGLStates();
CreateTileSheetWindow();
while (window.isOpen())
{
while (window.pollEvent(event))
{
SFGUI_Desktop.HandleEvent(event);
switch (event.type)
{
case sf::Event::Closed:
{
window.close();
}break ;
case sf::Event::Resized:
{
sf::FloatRect viewArea(0, 0, event.size.width, event.size.height);
window.setView(sf::View(viewArea));
}break ;
}
}
SFGUI_Desktop.Update(5.f);
window.clear(sf::Color::White);
//SFGUI_MenuBar->SetRequisition(sf::FloatRect(window.getSize().x, 5.f));
SFGUI_sfgui.Display(window);
window.display();
}
}
Last edited on Sep 8, 2014 at 4:24am UTC
Sep 8, 2014 at 8:04am UTC
I am creating new instances of SFGUI_Image
You don't. You create a new instance of
Desktop
(on line 56) what you shouldn't do. You can simply remove line 56 and on line 61:
SFGUI_notebook->AppendPage(GetImage(), sfg::Label::Create("TileSheet 1" )); // No D->
On line 43 you're using
SFGUI_Image
without creating it. If you need
SFGUI_Image
no matter what, why don't you make a non pointer member?
Sep 8, 2014 at 4:15pm UTC
Ok I tried that and My program still crashes because it cant get the image. I tried making a non pointer SFGUI image and It wouldnt work, I think SFGUI has to have it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
#include <SFML/Graphics.hpp>
#include <SFGUI/SFGUI.hpp>
#include <iostream>
using namespace std;
sf::RenderWindow window(sf::VideoMode(1024, 768), "Tile Editor" );
class Desktop
{
public :
Desktop();
void Run();
void CreateTileSheetWindow();
void CreateMenuBar();
void LoadImage();
sfg::Image::Ptr GetImage(){return SFGUI_Image;}
void SetImage(sfg::Image::Ptr img){SFGUI_Image = img;}
private :
static const unsigned int screen_width;
static const unsigned int screen_height;
sfg::SFGUI SFGUI_sfgui;
sfg::Image::Ptr SFGUI_Image;
sfg::Desktop SFGUI_Desktop;
sfg::Window::Ptr SFGUI_MenuBar;
sfg::Window::Ptr SFGUI_TileSheetWindow;
sfg::ComboBox::Ptr SFGUI_File;
sfg::ComboBox::Ptr SFGUI_Edit;
sf::Image SFML_TileSheet;
};
Desktop::Desktop(): SFGUI_Desktop()
{
}
void Desktop::LoadImage()
{
auto SFGUI_Image = sfg::Image::Create();
if (SFML_TileSheet.loadFromFile("Outside_A5.png" ))
{
SFGUI_Image->SetImage(SFML_TileSheet);
}
}
void Desktop::CreateTileSheetWindow()
{
auto SFGUI_TileSheetWindow = sfg::Window::Create();
auto SFGUI_notebook = sfg::Notebook::Create();
SFGUI_notebook->AppendPage(GetImage(), sfg::Label::Create("TileSheet 1" ));
SFGUI_notebook->SetScrollable(true );
SFGUI_TileSheetWindow->Add(SFGUI_notebook);
SFGUI_Desktop.Add(SFGUI_TileSheetWindow);
}
void Desktop::CreateMenuBar()
{
auto SFGUI_MenuBar = sfg::Window::Create();
auto SFGUI_File = sfg::ComboBox::Create();
SFGUI_File->AppendItem("Save" );
SFGUI_File->AppendItem("Open" );
auto MenuBox = sfg::Box::Create(sfg::Box::Orientation::HORIZONTAL, 5);
MenuBox->Pack(SFGUI_File);
SFGUI_MenuBar->SetStyle(SFGUI_MenuBar->GetStyle() ^ sfg::Window::TITLEBAR);
SFGUI_MenuBar->Add(SFGUI_File);
SFGUI_Desktop.Add(SFGUI_MenuBar);
}
int main()
{
Desktop desktop;
desktop.Run();
return 0;
}
void Desktop::Run()
{
sf::Event event;
window.resetGLStates();
CreateTileSheetWindow();
while (window.isOpen())
{
while (window.pollEvent(event))
{
SFGUI_Desktop.HandleEvent(event);
switch (event.type)
{
case sf::Event::Closed:
{
window.close();
}break ;
case sf::Event::Resized:
{
sf::FloatRect viewArea(0, 0, event.size.width, event.size.height);
window.setView(sf::View(viewArea));
}break ;
}
}
SFGUI_Desktop.Update(5.f);
window.clear(sf::Color::White);
//SFGUI_MenuBar->SetRequisition(sf::FloatRect(window.getSize().x, 5.f));
SFGUI_sfgui.Display(window);
window.display();
}
}
Topic archived. No new replies allowed.