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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Avency

Pages: 1 ... 5 6 [7]
91
General / [Sovled] Intergrating SFML into gtkmm
« on: March 23, 2008, 06:57:00 pm »
Has anyone ever successfully integrated SFML into gtkmm?
I am currently trying, but I cannot find any method that gets called when repainting the widget.

See:
http://www.gtkmm.org/docs/gtkmm-2.4/docs/reference/html/classGtk_1_1Widget.html

I managed SFML to display a Sprite in the widget but only if an event occurs.
I also recreate the sf::RenderWindow each time the window is resized. There has to be a better solution.
I am definetly not an expert when it comes to creating custom gtkmm widgets.
So if there is anyone who has done that before, then help/suggestions would be appreciated.

Here is what I've got so far (this will probably not work under windows):

File: SFMLWidget.h
Code: [Select]

#ifndef SFMLWIDGET_H_
#define SFMLWIDGET_H_

#include <SFML/Graphics.hpp>
#include <gtkmm.h>
#include <gdk/gdkx.h>


class SFMLWidget : public Gtk::Widget, public sf::RenderWindow
{
public:
SFMLWidget();
virtual ~SFMLWidget();

protected:

  virtual void on_size_request(Gtk::Requisition* requisition);
  virtual void on_size_allocate(Gtk::Allocation& allocation);
  virtual void on_map();
  virtual void on_unmap();
  virtual void on_realize();
  virtual void on_unrealize();
  virtual bool on_event(GdkEvent* event);

  Glib::RefPtr<Gdk::Window> ourGdkWindow;
  sf::Image Image;
  sf::Sprite Sprite;

};

#endif /*SFMLWIDGET_H_*/


SFMLWidget.cpp
Code: [Select]

#include "SFMLWidget.h"

SFMLWidget::SFMLWidget() :
  Glib::ObjectBase("SFMLWidget"),
  Gtk::Widget()
{
  set_flags(Gtk::NO_WINDOW);
 
  Image.LoadFromFile("image.png");
  Sprite.SetImage(Image);
}

SFMLWidget::~SFMLWidget()
{
}

void SFMLWidget::on_size_request(Gtk::Requisition* requisition)
{
  *requisition = Gtk::Requisition();
 
  requisition->height = 50;
  requisition->width = 50;
}

void SFMLWidget::on_size_allocate(Gtk::Allocation& allocation)
{
set_allocation(allocation);

if (ourGdkWindow)
{
ourGdkWindow->move_resize(allocation.get_x(), allocation.get_y(),
allocation.get_width(), allocation.get_height() );
GdkWindow* Win = ourGdkWindow->gobj();
XFlush(GDK_WINDOW_XDISPLAY(Win));
sf::RenderWindow::Create(GDK_WINDOW_XWINDOW(Win));
SetBackgroundColor(sf::Color::Red);
}
}

void SFMLWidget::on_map()
{
  Gtk::Widget::on_map();
}

void SFMLWidget::on_unmap()
{
  Gtk::Widget::on_unmap();
}

void SFMLWidget::on_realize()
{
  Gtk::Widget::on_realize();

  if(!ourGdkWindow)
  {
    GdkWindowAttr Attributes;
    memset(&Attributes, 0, sizeof(Attributes));

    Gtk::Allocation Allocation = get_allocation();

    Attributes.x = Allocation.get_x();
    Attributes.y = Allocation.get_y();
    Attributes.width = Allocation.get_width();
    Attributes.height = Allocation.get_height();

    Attributes.event_mask = Gdk::ALL_EVENTS_MASK ;
    Attributes.window_type = GDK_WINDOW_CHILD;
    Attributes.wclass = GDK_INPUT_OUTPUT;

    ourGdkWindow = Gdk::Window::create(get_window(), &Attributes, GDK_WA_X | GDK_WA_Y);
    unset_flags(Gtk::NO_WINDOW);
   
    Gtk::WidgetFlags Flags = Gtk::DOUBLE_BUFFERED | Gtk::APP_PAINTABLE;
    set_flags(Flags);
   
    set_window(ourGdkWindow);

    ourGdkWindow->set_user_data(gobj());
   
    GdkWindow* Win =  ourGdkWindow->gobj();
    XFlush(GDK_WINDOW_XDISPLAY(Win));
    sf::RenderWindow::Create(GDK_WINDOW_XWINDOW(Win));
    SetBackgroundColor(sf::Color::Red);
  }
}

void SFMLWidget::on_unrealize()
{
ourGdkWindow.clear();
Gtk::Widget::on_unrealize();
}

bool SFMLWidget::on_event(GdkEvent* event)
{
if (event)
{
if (event->type == GDK_BUTTON_PRESS )
{
Sprite.SetPosition(event->button.x, event->button.y);
}
Draw(Sprite);
Display();
return true;
}

return false;
}


Edit:

Problem solved.
Next time I should look through the documentation more carefully.
There exist signals for idle and timeouts.

92
Window / Window creation (Linux)
« on: March 23, 2008, 04:18:20 pm »
I've pulled down the latest fedora snapshot -> the problem still remains.

This is especially annoying in combination with sf::Style::None. Metacity displays the window in the top left corner, Compiz on the other hand handles sf::Style::None correctly (and centers the window).
To all other combinations the problems described earlier apply.
But you certainly want to disable Compiz when running an OpenGL application in windowed mode anyway.

Is there anyone using Linux who experiences the same problems or is it just me?

93
Graphics / Tileset/Sprite - Design question
« on: March 23, 2008, 03:56:26 pm »
It depends on the type of game you are trying to create.

Quote

2. Create each frame a new sprite then use SetPosition, SetSubRect & Draw for each tile and destroy the sprite once the job is done.

I've tried this some time ago. It worked but was a bit slow (where slow can be seen relative). But for a game like SimCity it should be sufficient.
Quote

To me, the best solution would be to store one sprite per tile and just draw them each frame.

This can be quite memory consuming for big maps. But it depends on your target hardware.

My current attempt is to store an integer for each tile and then to calculate its texture coordinates in the rendering loop and blit it to the screen using OpenGL directly.
This methods kicks ass (ca. 4 times faster than the method above and can handle really big maps). :D

Another method would be to create a tileset class storing a sprite for each tile and precalculating their subrects.
In the render-loop, you would only have to decide which sprite should be drawn at which position. Or maybe creating a tile class that inherits sf::Sprite.

But as said above, it heavily depends on what you are trying to achieve.

94
Feature requests / bool sf::Sprite::CheckCollosion(Sprite param)
« on: March 22, 2008, 03:09:19 pm »
Yep, maybe a function to generate collision outlines might be expensive.
But how to check on collisions between sprites otherwise (if not using just rectangles or pixel perfect collision detection)?
I have no idea...
Collision checking between shapes would still be a nice and useful feature, even if seen independent from the sprite issue.
Though I'm not sure how to implement it.

Quote

And I think this interface would be too complicated for just simple collision tests.

Sure, the implementation is complicated, but the interface itself?
I'm just curious what you are thinking of. :D
I guess I'll have to wait until you are done.

95
Feature requests / bool sf::Sprite::CheckCollosion(Sprite param)
« on: March 22, 2008, 11:13:29 am »
What about using shapes to detect collisions?
And maybe providing a function that generates shapes from the non-transparent part of an image.
Like sf::Shape sf::Sprite::GetCollisionOutline(int Quality), where the quality parameter determines how detailed the shapes end up.
And then use bool sf::Shape::Collide(const sf::Shape& Other) and bool sf::Shape::IsPointInside(int X, int Y).
Shapes are quite flexible and can adapt almost any 2d geometry.

96
Window / Window creation (Linux)
« on: March 18, 2008, 04:21:50 pm »
Instead of starting centered, windows always appear in the upper left corner when running Metacity.
Compiz has a similar behavior, but you cannot predict in which corner windows are opened.
Is there any way to fix this?

97
Graphics / Problem changing fonts
« on: March 05, 2008, 06:19:59 pm »
If you are using VisualStudio, make sure it is in your working directory.
What happens if you try to load an image from that directory?

I figured out, that the lettering could be a problem.
If your file is named "Arial.ttf" and you try to load it as "arial.ttf", it will fail (at least under linux).

98
Feature requests / some suggestions
« on: March 04, 2008, 06:41:56 pm »
Quote from: "Lord Delvin"

An optional manager would be nice, as some of as (or at least my project) might have their own ressource management which might want to do things different.

I agree, since the implementation of a resource manager can heavily depend on what you are trying to achive.
Still, having an optional default manager could be helpful in certain situations.
But a file archives manager would be more useful and should be taken care of first (I know, it is already on the roadmap).

Quote from: "tgm"

And a last thingy: Drawables should get parents (other drawables). So every thing would be in the parents coordinate systems (Multipling the Parents matrix with the Childs Matrix at render Time) Extremly cool would be to be able to controll which porpertys depends on the parent.
(Like: My position is always relativ to my Parents one but neither my rotation nor my scale, or my sclae depends on my parent, but not my rotation and Position etc.)

I like this idea. It could help to group drawables.

Edit:: Never leave the pc when writing a post  :wink:

99
Graphics / Re: Problem changing fonts
« on: March 03, 2008, 05:43:14 pm »
Quote
Do I have to put the .tff in  the directory of my project?

Yes, otherwise the built-in font will be used.
Also, there is no need for Text.SetFont() after
sf::String Text("text", "arial.tff", 36.f);
as it already sets the "arial.ttf" as active font.
(I could be wrong since I always sync with the svn repository).

100
Graphics / how do I use sf::Image::LoadFromMemory correctly?
« on: February 28, 2008, 06:52:53 pm »
Do

Code: [Select]

    char* memory;
   
    std::ifstream myfile("image.png", std::ios::binary | std::ios::ate);
   
    std::ifstream::pos_type size;
   
    size = myfile.tellg();
   
    memory = new char[size];
   
    myfile.seekg(0, std::ios::beg);
    myfile.read(memory, size);
    myfile.close();
   
    sf::Image Image;
    Image.LoadFromMemory(memory,size);

    delete[] memory;

It should work unless your image is corrupted. But why won't you use sf::Image::LoadFromFile?

101
Window / Understanding sf::Event::KeyPressed
« on: February 14, 2008, 08:15:14 pm »
In each loop I receive a sf::Event::KeyPressed event , if a key is held down.
Is there a way to receive a single key press?
If I wanted to check if a key is beeing held down, I would use the sf::Input class instead.
sf::Event::KeyReleased events are only received if a key is released, as I would expect it.

Here is a short example of what I mean:

Code: [Select]

#include <SFML/Graphics.hpp>
#include <iostream>
#include <sstream>

int main()
{

    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Events");
   
    sf::Font Font;
    Font.LoadFromFile("ARIAL.TTF", 40);
   
    sf::String Text("", Font, 32);
   
    std::ostringstream oss;
   
    int NbKeysPressed = 0; //counts, how often a key has been pressed

    // Start game loop
    bool Running = true;
    while (Running)
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            switch(Event.Type)
            {
            case sf::Event::Closed:
            {
            Running = false;
            break;
            }
            case sf::Event::KeyPressed:
            {
            ++NbKeysPressed;
           
            oss.str("");
            oss << NbKeysPressed;
            Text.SetText(oss.str());
           
            break;
            }
            case sf::Event::KeyReleased:
            {
            --NbKeysPressed;
            oss.str("");
            oss << NbKeysPressed;
            Text.SetText(oss.str());

                  break;
            }
            default:
            {
            break;
            }      
            }
        }
       
        App.Draw(Text);

        App.Display();
    }

    return EXIT_SUCCESS;
}


Any ideas?

102
Graphics / sfml-graphics.lib
« on: December 03, 2007, 06:02:33 pm »
Recompile the sfml-binaries on your own, it should work fine then.

103
Feature requests / Possibility to load resources from a file buffer
« on: November 26, 2007, 04:05:58 pm »
I see it has been added to svn.
Thanks a lot :D. I really appreciate your work.
I am now going to replace my own code (even if it was almost the same), as I prefer to leave third-party libraries the way they are.

104
Feature requests / Possibility to load resources from a file buffer
« on: November 12, 2007, 07:00:26 pm »
I'd like to see the possibility of loading resources from a file buffer or filestream
(at least for the built-in ones like images and audio).
I was able to do image loading from a file buffer using DevIL, but since you are going
to replace it in the next version, I'd strongly apply for alternative loading methods as they
shouldn't be hard to implement.

It would be rather useful for use in combination with custom resource files,
filesystems such as PhysicsFS or compression libraries in general.

i.e.
Code: [Select]

bool sf::Image::LoadFromBuffer(void* data, std::size_t size)
bool sf::Image::ReadFromStream(std::fstream& stream, std::size_t size)
bool sf::Image::WriteToStream(std::fstream& stream, std::size_t size)

bool sf::SoundBuffer::LoadFromBuffer(void* data, std::size_t size)
bool sf::SoundBuffer::ReadFromStream(std::fstream& stream, std::size_t size)
bool sf::SoundBuffer::WriteToStream(std::fstream& stream, std::size_t size)

You get the idea...

Pages: 1 ... 5 6 [7]