forked from bastienleonard/pysfml-cython
/
hacks.cpp
122 lines (96 loc) · 2.85 KB
/
hacks.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "hacks.hpp"
#include <iostream>
#include <cassert>
// This file contains code that couldn't be written in Cython.
// C functions defined in sf.pyx
// Can't declare them in hacks.hpp because the const-qualifier will clash with
// the actual signature (Cython doesn't support const pointers)
extern "C"
{
void set_error_message(const char* message);
}
// This should be big enough to contain any message error
static const int ERROR_MESSAGE_BUFFER_SIZE = 512;
// A custom streambuf that will put error messages in a Python dict
class MyBuff : public std::streambuf
{
public:
MyBuff()
{
buffer = new char[ERROR_MESSAGE_BUFFER_SIZE];
setp(buffer, buffer + ERROR_MESSAGE_BUFFER_SIZE);
}
~MyBuff()
{
delete[] pbase();
}
private:
char* buffer;
// This code is from SFML's bufstream. In our case overflow()
// should never get called, unless I missed something. But I don't
// undestand why it would get called when there's no overflow
// either (i.e., when pptr() != epptr()), so let's be on the safe
// side.
virtual int overflow(int character)
{
if (character != EOF && pptr() != epptr())
{
// Valid character
return sputc(static_cast<char>(character));
}
else if (character != EOF)
{
// Not enough space in the buffer: synchronize output and try again
sync();
return overflow(character);
}
else
{
// Invalid character: synchronize output
return sync();
}
}
virtual int sync()
{
if (pbase() != pptr())
{
// Replace '\n' at the end of the message with '\0'
*(pptr() - 1) = '\0';
// Call the function in sf.pyx that handles new messages
set_error_message(pbase());
setp(pbase(), epptr());
}
return 0;
}
};
void replace_error_handler()
{
static MyBuff my_buff;
sf::Err().rdbuf(&my_buff);
}
CppDrawable::CppDrawable()
{
}
CppDrawable::CppDrawable(void* drawable):
sf::Drawable(),
drawable(drawable)
{
};
void CppDrawable::Render(sf::RenderTarget& target, sf::Renderer& renderer) const
{
// The string parameters to PyObject_CallMethod() are char*, so in
// theory they can be modified, and string litterals are const char*
char method_name[] = "render";
char format[] = "(O, O)";
PyObject* pyTarget = (PyObject*)(wrap_render_target_instance(&target));
PyObject* pyRenderer = (PyObject*)(wrap_renderer_instance(&renderer));
// The caller needs to use PyErr_Occurred() to know if this
// function failed
PyObject* ret = PyObject_CallMethod(
static_cast<PyObject*>(drawable), method_name, format,
pyTarget, pyRenderer);
if (ret != NULL)
{
Py_DECREF(ret);
}
}