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.


Topics - t0rento

Pages: [1]
1
General discussions / SFML with gtkmm
« on: April 25, 2009, 07:58:04 pm »
I've been playing around with gtkmm and decided to create a SFML widget, after digging a lot through the documentation of both gtkmm and SFML and also looking at http://www.sfml-dev.org/forum/viewtopic.php?t=282 I managed to stitch something together.

However, I am fairly new to C++, gtkmm and SFML and I do not really understand the internals of SFML so I was wondering if there's any mistakes I've made. Basically I'm just looking for some constructive criticism.

Thank you.

SFMLWidget.h
Code: [Select]

#include <SFML/Graphics.hpp>
#include <gdk/gdkwin32.h>
#include <gdk/gdk.h>
#include <gdkmm/general.h>
#include <gtkmm.h>

class SFMLWidget : public Gtk::Widget, public sf::RenderWindow
{
    private:
        sf::VideoMode m_vMode;

        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_idle();
        virtual bool on_expose_event(GdkEventExpose* event);
        void DrawObjects();

        Glib::RefPtr<Gdk::Window> m_refGdkWindow;

        sf::Image m_tempImage;
        sf::Sprite m_tempSprite;
    public:
        SFMLWidget(sf::VideoMode Mode);
        virtual ~SFMLWidget();
};


SFMLWidget.cpp
Code: [Select]

#include "SFMLWidget.h"
void SFMLWidget::DrawObjects()
{
    this->Draw(m_tempSprite);
}

bool SFMLWidget::on_idle()
{
    if(m_refGdkWindow)
    {
        DrawObjects();
        this->Display();
    }

    return true;
}

SFMLWidget::SFMLWidget(sf::VideoMode Mode)
    : sf::RenderWindow(Mode, "")
{
    set_flags(Gtk::NO_WINDOW);
    m_tempImage.LoadFromFile("gtk_logo.png");
    m_tempSprite.SetImage(m_tempImage);
    m_tempSprite.SetPosition(50, 50);

    Glib::signal_idle().connect( sigc::mem_fun(*this, &SFMLWidget::on_idle) );
}

SFMLWidget::~SFMLWidget()
{
}

void SFMLWidget::on_size_request(Gtk::Requisition* requisition)
{
    *requisition = Gtk::Requisition();

    requisition->width = this->GetWidth();
    requisition->height = this->GetHeight();
}

void SFMLWidget::on_size_allocate(Gtk::Allocation& allocation)
{
    //Do something with the space that we have actually been given:
    //(We will not be given heights or widths less than we have requested, though
    //we might get more)

    this->set_allocation(allocation);

    if(m_refGdkWindow)
    {
        m_refGdkWindow->move_resize(allocation.get_x(), allocation.get_y(), allocation.get_width(), allocation.get_height() );
        this->SetSize(allocation.get_width(), allocation.get_height());
    }
}

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(!m_refGdkWindow)
    {
        //Create the GdkWindow:
        GdkWindowAttr attributes;
        memset(&attributes, 0, sizeof(attributes));

        Gtk::Allocation allocation = get_allocation();

        //Set initial position and size of the Gdk::Window:
        attributes.x = allocation.get_x();
        attributes.y = allocation.get_y();
        attributes.width = allocation.get_width();
        attributes.height = allocation.get_height();

        attributes.event_mask = get_events () | Gdk::EXPOSURE_MASK;
        attributes.window_type = GDK_WINDOW_CHILD;
        attributes.wclass = GDK_INPUT_OUTPUT;


        m_refGdkWindow = Gdk::Window::create(get_window() /* parent */, &attributes,
                GDK_WA_X | GDK_WA_Y);
        unset_flags(Gtk::NO_WINDOW);
        set_window(m_refGdkWindow);

        //set colors
        modify_bg(Gtk::STATE_NORMAL , Gdk::Color("red"));
        modify_fg(Gtk::STATE_NORMAL , Gdk::Color("blue"));

        //make the widget receive expose events
        m_refGdkWindow->set_user_data(gobj());

        ///Reference: http://www.nabble.com/Win32-HWND-td20494257.html
        ///This is platform specific, compiling on Linux/MacOS will require a different Window Handle
        this->sf::RenderWindow::Create(reinterpret_cast<HWND>(GDK_WINDOW_HWND(m_refGdkWindow->gobj())));
    }
}

void SFMLWidget::on_unrealize()
{
  m_refGdkWindow.clear();

  //Call base class:
  Gtk::Widget::on_unrealize();
}

bool SFMLWidget::on_expose_event(GdkEventExpose* event)
{
    if(m_refGdkWindow)
    {
        DrawObjects();
        this->Display();
    }
    return true;
}

2
SFML projects / SHMUPEH (Game)
« on: March 15, 2009, 01:39:47 am »
This is my Australia Year 12 project for my Software Development course, the requirements where just that it had to compile and use an array but I felt like making something remotely fun

The game is called SHMUPEH. It's a simple bullet-dodging enemy shooting game.

Most of the controls are explained when you load the game, except that F5 is used to take a screenshot.

I am using SFML primarily for rendering everything, the game as of now contains no networking, audio or use of any of the other libraries SFML provides. (I would probably be using networking and audio if I wasn't on a time limit to finish the project)

Note:
This code has gone through a lot of revisions and I can't say I'm proud of it. Like any programmer who works on a project for a long time they start to see so many flaws in it that they just want to start again. This project has been remade at least 4 times due to this and I find that if I keep remaking it it never gets done.

As such I cannot say I am happy with this code, however I hope the end result is at least a bit enjoyable.

Screenshots:





Download: http://www.electronicfiles.net/files/11823/Programs/SHMUPEH.rar
Source Code: http://www.electronicfiles.net/files/11823/Programs/SHMUPEH_SOURCE.rar
[Note] The download includes the source code.

Enjoy.

3
Graphics / Image is too large
« on: February 16, 2009, 10:32:54 am »
On some machines I get a error in the console "Error image is too large (1024x4320)" but on my own I do not.

What is the cause of this and how can I fix it?

Pages: [1]
anything