pygame C-API, , , ( , pygame pygame).
, "" API, , , / pygame, pygame API/ABI C...
, :
C (red.c):
#include <Python.h>
#include <pygame/pygame.h>
static PyObject* fill_surface(PyObject* self, PyObject* args)
{
PyObject* py_surface = NULL;
if( !PyArg_ParseTuple(args, "O", &py_surface) )
return NULL;
PySurfaceObject* c_surface = (PySurfaceObject*) py_surface;
SDL_FillRect( c_surface->surf, 0, SDL_MapRGB(c_surface->surf->format, 255, 0, 0) );
return Py_None;
}
PyMethodDef methods[] = {
{"fill_surface", fill_surface, METH_VARARGS, "Paints surface red"},
{NULL, NULL, 0, NULL},
};
void initred()
{
Py_InitModule("red", methods);
}
Makefile (Linux):
SOURCES = red.c
OBJECTS = $(SOURCES:.c=.o)
TARGET = red.so
CFLAGS += -Wall -O2 -g
CFLAGS += $(shell python-config --cflags)
CFLAGS += $(shell sdl-config --cflags)
LDFLAGS += $(shell sdl-config --libs)
all: $(TARGET)
clean:
$(RM) $(OBJECTS)
$(RM) $(TARGET)
$(TARGET): $(OBJECTS)
$(CC) -shared -o $@ $(OBJECTS) $(LDFLAGS)
$(OBJECTS): Makefile
Python:
import pygame
import red
pygame.init()
screen = pygame.display.set_mode( (640, 480) )
red.fill_surface(screen)
pygame.display.update()
pygame.time.delay(3000)
source
share