英文:
cgo SDL function not specified
问题
我已经尝试在各个地方查找并尝试了几乎所有我能想到的方法,但我无法让SDL在Go中工作。这是我的代码,有点凌乱,但我只是想让它工作,只是为了测试它,只是作为一个起点。我之后会整理它并去掉所有不必要的#define。
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()
}
这给我报错:"command-line arguments" error: 'SDL_LoadBMP' undeclared (first use in this function),无论我如何搜索或使用技巧都无法解决。如果需要的话,我使用的是Ubuntu和SDL 1.0.2(我想是这个版本)。
我不想使用包装器,因为Go中唯一的SDL 1包装器已经两年没有更新了,而且两个好的包装器是为SDL 2准备的,我宁愿使用SDL 1。谢谢任何能够指导我的人。
另外,还有一个额外的问题,因为编译器永远不会让我测试它(显然),在BlitSurface的参数中我应该使用"nil"、"NULL"还是"C.NULL"(我知道我需要使用#define来定义)?
英文:
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?
答案1
得分: 0
你在尝试使用Go的原生SDL库之前,有没有回退到C语言?如果这样还不行,你可以直接用C语言编写,或者使用其他类型的库,比如OpenGL或Ogre3D来进行图形工作,这两个库在Go中都有支持。
**编辑:**你确定Go是你所做的游戏/图形工作的正确语言吗?在行业中,像Java或C++这样的语言更常见,并且有更好的支持。Go在游戏开发中并不常用,这是因为缺乏许多不同API的官方库。
英文:
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.
答案2
得分: 0
我很抱歉,我必须同意Prakesh的观点,尽管他的措辞有点粗鲁。
Go语言对于大规模图形应用程序来说还不够成熟,根据你使用SDL的情况,我猜你要么已经是一个独立的游戏开发者,想要尝试一下Go语言,要么是一个已经选择了不太适合游戏开发的语言的Go程序员(可以尝试C++或Haskell,学习多种语言没有坏处!;-))
当然,你也可能是对编程和游戏开发都是新手,这种情况下我建议你尝试使用Game Maker或者跟随一个C++教程。
至于你的第二个问题,你可以使用Go语言的'nil',据我所知它仍然表示ASCII字符'\0'。
英文:
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.
答案3
得分: 0
SDL_LoadBMP是一个宏,而不是一个函数。它的定义是:
#define SDL_LoadBMP(file) SDL_LoadBMP_RW(SDL_RWFromFile(file, "rb"), 1)
然而,Cgo无法使用宏,所以你有两个选择:要么使用宏的展开版本,要么编写一个C函数来使用该宏,并从Go中调用该函数。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论