Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Cython中包装C结构以便在Python中使用?_Python_Struct_Sdl_Cython_Word Wrap - Fatal编程技术网

如何在Cython中包装C结构以便在Python中使用?

如何在Cython中包装C结构以便在Python中使用?,python,struct,sdl,cython,word-wrap,Python,Struct,Sdl,Cython,Word Wrap,为了获得一点学习经验,我正在尝试将SDL(1.2.14)的一些部分用Cython封装在Python3.2的扩展中 我在弄清楚如何将C结构直接包装到Python中时遇到了一个问题,因为我无法直接访问它的属性,比如: struct_name.attribute 例如,我想取struct SDL_曲面: typedef struct SDL_Rect { Uint32 flags SDL_PixelFormat * format int w, h Uint16 pit

为了获得一点学习经验,我正在尝试将SDL(1.2.14)的一些部分用Cython封装在Python3.2的扩展中

我在弄清楚如何将C结构直接包装到Python中时遇到了一个问题,因为我无法直接访问它的属性,比如:

struct_name.attribute
例如,我想取struct SDL_曲面:

typedef struct SDL_Rect {
    Uint32 flags
    SDL_PixelFormat * format
    int w, h
    Uint16 pitch
    void * pixels
    SDL_Rect clip_rect
    int refcount
} SDL_Rect;
并且能够在python中这样使用它:

import SDL
# initializing stuff

Screen = SDL.SetVideoMode( 320, 480, 32, SDL.DOUBLEBUF )

# accessing SDL_Surface.w and SDL_Surface.h
print( Screen.w, ' ', Screen.h )
现在,我将SDL_SetVideoMode和SDL_曲面像这样包装在 一个名为SDL.pyx的文件

cdef extern from 'SDL.h':
    # Other stuff

    struct SDL_Surface:
        unsigned long flags
        SDL_PixelFormat * format
        int w, h
        # like past declaration...

    SDL_Surface * SDL_SetVideoMode(int, int, int, unsigned )


cdef class Surface(object):
    # not sure how to implement       


def SetVideoMode(width, height, bpp, flags):
    cdef SDL_Surface * screen = SDL_SetVideoMode  

    if not screen:
        err = SDL_GetError()
        raise Exception(err)

    # Possible way to return?...
    return Surface(screen)

我应该如何实现SDL.Surface?

在一个简单的情况下,如果struct是不透明的,它可以简单到:

cdef extern from "foo.h":
    struct spam:
        pass
当您希望访问成员时,有几个选项,这些选项在文档中有详细介绍:


看一看。这里有一个更完整的,非常感谢。正是我需要的。我想我下次应该做一个更彻底的搜索。@l0rdx3nu回答这个问题对你来说还相关吗?