dopuz8728
dopuz8728
2013-08-14 18:07
浏览 39
已采纳

未指定cgo SDL函数

I've tried looking this up everywhere and tried just about everything I can, I cannot get SDL to work in Go. Here's my code, a little sloppy, but I just wanted to get it to work, just to test it, just as a starting point. I was going to clean it up and get rid of all the unnecessary #defines later.

package main

// #cgo LDFLAGS: -lSDL -lSDL_main -lSDL_image
// #include <stdio.h>
// #include <SDL/SDL.h>
// #include <SDL/SDL_main.h>
// #include <SDL/SDL_image.h>
import "C"

import "runtime"

func init() {
runtime.LockOSThread()
}

func main() {
var image* C.SDL_Surface
var screen* C.SDL_Surface
C.SDL_Init( C.SDL_INIT_EVERYTHING )
screen = C.SDL_SetVideoMode(640, 480, 32, C.SDL_SWSURFACE)
hello = C.SDL_LoadBMP( "moe.bmp" )
C.SDL_BlitSurface(hello, nil, screen, nil)
C.SDL_Flip(screen)
C.SDL_Delay(2000)
C.SDL_FreeSurface(hello)
C.SDL_Quit()
}

This gives me the "command-line arguments" error: 'SDL_LoadBMP' undeclared (first use in this function), and no amount of searching or wizardry will fix it. If it's required, I'm on Ubuntu with SDL 1.0.2 (I think)

I'd rather not use a wrapper since the only SDL 1 wrapper for Go is two years old and the two good ones are for SDL 2, and I'd rather use SDL 1. Thank you anyone that can guide me.

Also, bonus question because the compiler would never let me test it (obviously), do I use "nil", "NULL", or "C.NULL" (which I know I'd have to #define) in BlitSurface's arguments?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douchun3680
    douchun3680 2013-08-14 18:36
    已采纳

    SDL_LoadBMP is a macro, not a function. Its definition is

    #define SDL_LoadBMP(file)   SDL_LoadBMP_RW(SDL_RWFromFile(file, "rb"), 1)
    

    Cgo won't be able to use macros however, so you have two options: Either use the expanded version of the macro or, which would be preferred, write a C function that uses the macro and call that function from Go.

    点赞 评论
  • dongyi8795
    dongyi8795 2013-08-14 18:12

    Did you try using native SDL libraries in go before falling back to the c? Also if that doesn't work you can just write in c or use another type of library such as OpenGL or Ogre3D for graphics work, both of which have supported in go since a while back.

    Edit: Are you sure that Go is the right language for the type of game/graphic work you are doing? Something like java or c++ is much more common in the industry and has much better suport. Go is not used for games very often and this is because of the lack of official libraries for many different API's.

    点赞 评论
  • doukengzi3517
    doukengzi3517 2013-08-14 18:25

    I'm afraid I'm going to have to agree with Prakesh, although he phrased it a little rudely. Go is simply not mature enough for large-scale graphics applications yet, and I'm assuming by your use of SDL you're either already an independant game developer looking to try something new in the form of Go, or an existing Go programmer who's made a poor choice of language to develop a game in (try C++ or Haskell, there's no harm in studying multiple languages! ;-) )

    Of course, you could be new to both programming and games development, in which case I would suggest trying Game Maker or following a C++ tutorial.

    As for your second question, you can use Go's 'nil', as this still represents the ASCII character of '\0' as far as I am aware.

    点赞 评论

相关推荐