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

Author Topic: sfm and cairo  (Read 2247 times)

0 Members and 1 Guest are viewing this topic.

smisra

  • Newbie
  • *
  • Posts: 1
    • View Profile
sfm and cairo
« on: January 29, 2014, 08:22:41 pm »
Hi All,
I am novice to sfml. I am trying to use sfml with cairo. But i am not getting success. Posting my code here. Seeking your help.

           
 const auto numOfChannels = 4;
            const auto totalBytes = numOfChannels * _surfaceWidth * _surfaceHeight;
            _surfaceData.reset(new ovg::VGubyte[totalBytes]);
            _cairoSurface = cairo_image_surface_create_for_data (_surfaceData.get(),
               CAIRO_FORMAT_ARGB32,
               _surfaceWidth,
               _surfaceHeight,
               numOfChannels * _surfaceHeight);
            if (cairo_surface_status (_cairoSurface) != CAIRO_STATUS_SUCCESS)
            {
               setLastError(ovg::VG_BAD_HANDLE_ERROR);
               return;
            }

            _cr = cairo_create (_cairoSurface);
            if (cairo_status (_cr) != CAIRO_STATUS_SUCCESS)
            {
               setLastError(ovg::VG_BAD_HANDLE_ERROR);
               return;
            }

         void renderToSurface()
         {
            cairo_save (_cr);
            cairo_set_operator (_cr, CAIRO_OPERATOR_OVER);
            cairo_scale (_cr, static_cast<double>(_surfaceWidth), static_cast<double>(_surfaceHeight));
            cairo_restore (_cr);
            cairo_set_source_rgba (_cr, 0.0f, 0.0f, 0.0f, 1.0f);
            cairo_paint(_cr);

            // create temporary surface
            cairo_push_group (_cr);
            cairo_set_source_rgba (_cr, 0.0f, 0.0f, 0.0f, 0.0f);
            cairo_paint (_cr);
         }
 
and then

     
void convertPixels(blib::konark::openvg::_impl::VGContext::SurfaceDataPtrType& aData,
         sf::Image& aImg,
         int w,
         int h,
         int pitch = 4)
      {
         unsigned char *dataptr = aData.get();
         for (int y = 0 ; y < h ; ++y)
         {
            int rx = 0;
            for (int x = 0 ; x < w*4 ; x+=4)
            {
               aImg.setPixel(rx++, y, sf::Color(dataptr[x+2],dataptr[x+1],dataptr[x],dataptr[x+3]));
            }
            dataptr += pitch;
         }
      }

      void draw()
      {
         const auto ht = _context.height();
         const auto wd = _context.width();
         sf::Image img;
         img.create(wd, ht, sf::Color::Cyan);
         convertPixels(_context.draw(), img, wd, ht);

         //sf::Image img;
         //img.create(ht, wd, _context.draw().get());
         sf::Texture texture;
         texture.create(_context.height(), _context.width());
         //const sf::IntRect rect(0, 0, wd, ht);
         texture.loadFromMemory(_context.draw().get(), 4 * ht * wd);
         //texture.update(img);
         sf::Sprite sprite;
         sprite.setTexture(texture);
         _window.draw(sprite);
      }
 

This doesnt render the texture. Any one knows any other way or example?
« Last Edit: January 29, 2014, 08:27:37 pm by smisra »

Veltas

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: sfm and cairo
« Reply #1 on: January 30, 2014, 02:49:49 am »
I don't have any idea what the problem is here, but is mixing SFML and cairo really a good idea?

SFML has support for mixing with OpenGL, at least, so I'd try following them if you're not already: http://sfml-dev.org/tutorials/2.1/window-opengl.php

And then the same for cairo, if such rules exist (which they probably do, but I don't know my way around cairo's documentation).

And then hopefully it should work.

Otherwise to get people motivated to try and fix this I'd try and reduce your code down to the bare minimum that causes the same problem, hopefully you'll accidentally fix it trying to do this.
< veltas> MJBrune: Debian GNU/Linux
* MJBrune mounts veltas

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: sfm and cairo
« Reply #2 on: January 30, 2014, 09:40:11 am »
I've never worked with Cairo, however one might still be able to come up with something, if you spend a bit more time try to explain things.
Throwing code at people and say "This doesn't render the texture" will very rarely give you a good answer. Explain the process of how Cairo handles things and how you try to combine it with SFML. Explain what the outcome is (crash, black, etc). Use your debugger to track the states of various variables to see whether something went wrong in the code. And most important, remove unnecessary things and post a complete (i.e. fully compilable) and minimal example.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/