in reply to Re: SDL and Cairo segfault
in thread SDL and Cairo segfault

Thank you for reply.

The main reasons for cairo were to draw more interesting stuff than a red rectangle (which was used to make example code short) and to do it fast enough for a simple game.

Your code worked perfectly, but would you like to explain what is the reason for "no warnings", please? Everybody seems to be advocating for "use warnings".

I suddenly discovered SDL::GFX::Primitives namespace which looks suitable

Just in case, here are two pieces of code: Perl version segfaults and C one works. Either they are not equivalent or perl-cairo and perl-sdl are not compatible

use strict; no warnings; use SDL v2.3; use SDL::Video; use SDL::Surface; use Cairo; SDL::init(SDL_INIT_VIDEO); my $screen = SDL::Video::set_video_mode(400, 300, 32, SDL_SWSURFACE); SDL::Video::lock_surface($screen) if SDL::Video::MUSTLOCK($screen); my $surface = Cairo::ImageSurface->create_for_data ( $screen->get_pixels_ptr, 'argb32', $screen->w, $screen->h, $screen->pitch ); my $cr = Cairo::Context->create($surface); $cr->rectangle(10, 10, 40, 40); $cr->set_source_rgb(255, 0, 0); $cr->fill; SDL::Video::unlock_surface($screen) if SDL::Video::MUSTLOCK($screen); SDL::Video::flip($screen); SDL::delay(2000); SDL::quit();

#include <SDL.h> #include <cairo.h> int main () { SDL_Init(SDL_INIT_VIDEO); SDL_Surface *screen = SDL_SetVideoMode(400, 300, 32, SDL_SWSURFACE); if (SDL_MUSTLOCK(screen)) { SDL_LockSurface(screen); } cairo_surface_t *cairosurf = cairo_image_surface_create_for_data ( screen->pixels, CAIRO_FORMAT_ARGB32, screen->w, screen->h, screen->pitch ); cairo_t *cr = cairo_create(cairosurf); cairo_rectangle(cr, 10, 10, 40, 40); cairo_set_source_rgb(cr, 255, 0, 0); cairo_fill(cr); if (SDL_MUSTLOCK(screen)) { SDL_UnlockSurface(screen); } SDL_Flip(screen); SDL_Delay(2000); SDL_Quit(); return 0; }

Replies are listed 'Best First'.
Re^3: SDL and Cairo segfault
by Khen1950fx (Canon) on Jan 10, 2011 at 11:23 UTC
    I used "no warnings" after I finished the script. Always "use warnings" is always the right thing, but, with SDL, it likes to keep sending out little messsages that aren't useful. That's why I used "no warnings".

    I adjusted your script a little. It produces a red rectangle png. Give it a test drive.

    #!/usr/bin/perl use strict; use Cairo; use SDL; use SDL::Video; use SDL::Surface; use SDL::Rect; use constant WIDTH => 400; use constant HEIGHT => 300; SDL::init(SDL_INIT_VIDEO); my $screen = SDL::Video::set_video_mode(WIDTH, HEIGHT, 32, SDL_SWSURFACE); my $surface = Cairo::ImageSurface->create( 'rgb24', WIDTH, HEIGHT); my $cr = Cairo::Context->create($surface); $0 =~ /(.*)\.pl/; my $out = "$1.png"; $cr->rectangle (0, 0, WIDTH, HEIGHT); $cr->set_source_rgb (255, 0, 0); $cr->fill; $cr->show_page; $surface->write_to_png ($out); SDL::quit();