Search code examples
loadsdlvalagenie

How can I load a png with vala/genie and SDL?


This code gives me an error:

uses SDL
uses SDLGraphics
uses SDLImage
screen: unowned SDL.Screen
alto: int16=400
ancho:int16 =600
imagen: SDL.Surface
src: SDL.RWops 
init
    SDL.init(SDL.InitFlag.VIDEO)
    screen = SDL.Screen.set_video_mode (ancho, alto, 30, SurfaceFlag.FULLSCREEN | SurfaceFlag.DOUBLEBUF | SurfaceFlag.HWACCEL | SurfaceFlag.HWSURFACE)
    var fin = false
    var y=110
    var x=100
    var radius=10
    var color=1003232242
    var mov_x=1
    var mov_y=3
    src = new SDL.RWops.from_file ("/home/gontzal/Mahaigaina/boy.png", "rb")
    imagen =  SDLImage.load_png (src)
    while fin == false
        event: SDL.Event
        while (Event.poll (out event))== 1
            if event.type ==SDL.EventType.QUIT
                print "adios"
                fin= true
                break
            else if event.type == EventType.KEYDOWN
                fin=true
                break

        if x>ancho do mov_x=-mov_x
        if x<0 do mov_x=-mov_x
        if y>alto do mov_y=-mov_y
        if y<0 do mov_y=-mov_y
        x=x+mov_x
        y=y+mov_y


        Circle.fill_color (screen, x, y, radius, color);
        Circle.outline_color_aa (screen, x, y, radius, color);
        Rectangle.fill_color(screen,0, 0,ancho, alto,1801222)
        screen.flip()

    SDL.quit()

Error: valac --pkg sdl --pkg sdl-gfx --pkg sdl-image -X -lSDL_gfx "SDL.gs" (in directory: /home/gontzal) /tmp/ccMPg7h0.o: In function _vala_main': SDL.vala.c:(.text+0x115): undefined reference toIMG_LoadPNG_RW' collect2: ld returned 1 exit status Compilation failed: 1 error(s), 0 warning(s) error: cc exited with status 256 Compilation failed.


Solution

  • You have to manually add the relevant library and cflags. See https://live.gnome.org/Vala/SDLSample#Compile_and_Run for an example.

    The reason for this is that Vala uses pkg-config to try to figure out the correct flags to pass to the C compiler, but at the time the Vala bindings were written SDL didn't distribute pkg-config files. AFAICT some (but not all) of their modules do now, but the names do not match the Vala bindings.