I have been fooling around with some simple graphics things in Python, but to improve performance, I'd like to do some stuff in C.
I know how to transfer arrays and stuff back and forth. That's no biggie. But I thought it might be beneficial if I could just create a windows with a canvas, pass a pointer to the video memory (of course not the physical video memory) and then let Python take care of just putting that memory to the screen, while the rest is done in C. Possibly asynchronously, but I don̈́'t know if that matters.
Is this possible, and does it make sense? Or am I on the completely wrong track?
My efforts so far:
# graphics.py
import ctypes
import pygame
screen = pygame.display.set_mode((300,200))
f = ctypes.CDLL('/path/to/engine.so')
f.loop(screen._pixels_address)
And
// engine.c
#include <stdint.h>
void loop(void *mem) {
while(1) {
uint8_t *p = (uint8_t*) mem;
// Was hoping this would make some pixels change
for(int i=0; i<20000; i++)
*p=127;
}
}
This didn't work and ended up with a crash. I'm not surprised, but well, it's what I've got so far.
It's not strictly necessary to use Python. But I do want to use C. And I'm also aware that in most cases, my approach is not the best one, but I do enjoy coding in C and doing stuff old school.