Skip to content

Commit 3206f3c

Browse files
Fix SDL_UpdateTexture function
1 parent 932b0a1 commit 3206f3c

File tree

8 files changed

+32
-267
lines changed

8 files changed

+32
-267
lines changed

src/event.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ PHP_MINIT_FUNCTION(sdl_event)
289289
zend_declare_property_null(php_sdl_event_ce, ZEND_STRL("key"), ZEND_ACC_PUBLIC);
290290
zend_declare_property_null(php_sdl_event_ce, ZEND_STRL("motion"), ZEND_ACC_PUBLIC);
291291
zend_declare_property_null(php_sdl_event_ce, ZEND_STRL("window"), ZEND_ACC_PUBLIC);
292+
zend_declare_property_null(php_sdl_event_ce, ZEND_STRL("button"), ZEND_ACC_PUBLIC);
292293

293294
return SUCCESS;
294295
}

src/pixels.c

Lines changed: 12 additions & 234 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ zend_bool sdl_pixels_to_zval(SDL_Pixels *pixels, zval *z_val, Uint32 flags)
214214
php_sdl_pixels *intern;
215215

216216
object_init_ex(z_val, php_sdl_pixels_ce);
217-
intern = PHP_SDL_PIXELS_P(z_val);
217+
zend_object *zo = Z_OBJ_P(z_val);
218+
intern = (php_sdl_pixels*)((char *)zo - zo->handlers->offset);
218219
intern->pixels = *pixels;
219220
intern->flags = flags;
220221

@@ -242,11 +243,12 @@ SDL_PixelFormat *zval_to_sdl_pixelformat(zval *z_val)
242243
/* {{{ zval_to_sdl_pixels */
243244
SDL_Pixels *zval_to_sdl_pixels(zval *z_val)
244245
{
246+
php_sdl_pixels *intern;
247+
245248
if (Z_TYPE_P(z_val) == IS_OBJECT && Z_OBJCE_P(z_val) == php_sdl_pixels_ce)
246249
{
247-
php_sdl_pixels *intern;
248-
249-
intern = PHP_SDL_PIXELS_P(z_val);
250+
zend_object *zo = Z_OBJ_P(z_val);
251+
intern = (php_sdl_pixels*)((char *)zo - zo->handlers->offset);
250252
return &intern->pixels;
251253
}
252254
return NULL;
@@ -1058,8 +1060,6 @@ static PHP_METHOD(SDL_Pixels, __construct)
10581060
php_error_docref(NULL, E_NOTICE, "Pitch set to %d", (int)pitch);
10591061
}
10601062
intern->pixels.pixels = ecalloc((int)pitch, (int)h);
1061-
intern->pixels.pitch = (int)pitch;
1062-
intern->pixels.h = (int)h;
10631063
}
10641064
}
10651065
/* }}} */
@@ -1070,187 +1070,18 @@ ZEND_END_ARG_INFO()
10701070
/* {{{ proto SDL_Pixels::__toString() */
10711071
static PHP_METHOD(SDL_Pixels, __toString)
10721072
{
1073-
php_sdl_pixels *intern;
10741073
char *buf;
10751074

10761075
if (zend_parse_parameters_none() == FAILURE)
10771076
{
10781077
return;
10791078
}
10801079

1081-
intern = PHP_SDL_PIXELS_P(getThis());
1082-
spprintf(&buf, 100, "SDL_Pixels(%d,%d)", intern->pixels.pitch, intern->pixels.h);
1080+
spprintf(&buf, 100, "SDL_Pixels");
10831081
RETVAL_STRING(buf);
10841082
}
10851083
/* }}} */
10861084

1087-
/* {{{ proto SDL_Pixels, count(void) */
1088-
static PHP_METHOD(SDL_Pixels, count)
1089-
{
1090-
php_sdl_pixels *intern;
1091-
1092-
intern = PHP_SDL_PIXELS_P(getThis());
1093-
if (zend_parse_parameters_none() == FAILURE)
1094-
{
1095-
return;
1096-
}
1097-
1098-
RETURN_LONG(intern->pixels.h * intern->pixels.pitch);
1099-
}
1100-
/* }}} */
1101-
1102-
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_SDL_Pixels_offsetExists, 0, 1, _IS_BOOL, 0)
1103-
ZEND_ARG_INFO(0, offset)
1104-
ZEND_END_ARG_INFO()
1105-
1106-
/* {{{ proto SDL_Pixels, offsetExists(int offset) */
1107-
PHP_METHOD(SDL_Pixels, offsetExists)
1108-
{
1109-
php_sdl_pixels *intern;
1110-
zend_long offset;
1111-
1112-
intern = PHP_SDL_PIXELS_P(getThis());
1113-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &offset) == FAILURE)
1114-
{
1115-
return;
1116-
}
1117-
if (offset < 0 || offset >= (intern->pixels.h * intern->pixels.pitch))
1118-
{
1119-
RETURN_FALSE;
1120-
}
1121-
RETURN_TRUE;
1122-
}
1123-
/* }}} */
1124-
1125-
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_SDL_Pixels_offsetGet, 0, 1, IS_MIXED, 1)
1126-
ZEND_ARG_INFO(0, offset)
1127-
ZEND_END_ARG_INFO()
1128-
1129-
/* {{{ proto SDL_Pixels, offsetGet(int offset) */
1130-
PHP_METHOD(SDL_Pixels, offsetGet)
1131-
{
1132-
php_sdl_pixels *intern;
1133-
zend_long offset;
1134-
1135-
intern = PHP_SDL_PIXELS_P(getThis());
1136-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &offset) == FAILURE)
1137-
{
1138-
return;
1139-
}
1140-
if (offset < 0 || offset >= (intern->pixels.h * intern->pixels.pitch))
1141-
{
1142-
zend_throw_exception(zend_exception_get_default(), "Invalid offset in SDL_Pixels", 0);
1143-
RETURN_FALSE;
1144-
}
1145-
RETVAL_LONG(intern->pixels.pixels[offset]);
1146-
}
1147-
/* }}} */
1148-
1149-
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_SDL_Pixels_offsetUnset, 0, 1, IS_VOID, 0)
1150-
ZEND_ARG_INFO(0, offset)
1151-
ZEND_END_ARG_INFO()
1152-
1153-
/* {{{ proto SDL_Pixels, offsetUnset(int offset) */
1154-
PHP_METHOD(SDL_Pixels, offsetUnset)
1155-
{
1156-
php_sdl_pixels *intern;
1157-
zend_long offset;
1158-
1159-
intern = PHP_SDL_PIXELS_P(getThis());
1160-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &offset) == FAILURE)
1161-
{
1162-
return;
1163-
}
1164-
if (offset < 0 || offset >= (intern->pixels.h * intern->pixels.pitch))
1165-
{
1166-
zend_throw_exception(zend_exception_get_default(), "Invalid offset in SDL_Pixels", 0);
1167-
RETURN_FALSE;
1168-
}
1169-
intern->pixels.pixels[offset] = 0;
1170-
}
1171-
/* }}} */
1172-
1173-
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_SDL_Pixels_offsetSet, 0, 2, IS_VOID, 0)
1174-
ZEND_ARG_INFO(0, offset)
1175-
ZEND_ARG_INFO(0, value)
1176-
ZEND_END_ARG_INFO()
1177-
1178-
/* {{{ proto SDL_Pixels, offsetSet(int offset, int value) */
1179-
PHP_METHOD(SDL_Pixels, offsetSet)
1180-
{
1181-
php_sdl_pixels *intern;
1182-
zend_long offset, value;
1183-
1184-
intern = PHP_SDL_PIXELS_P(getThis());
1185-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &offset, &value) == FAILURE)
1186-
{
1187-
return;
1188-
}
1189-
if (offset < 0 || offset >= (intern->pixels.h * intern->pixels.pitch))
1190-
{
1191-
zend_throw_exception(zend_exception_get_default(), "Invalid offset in SDL_Pixels", 0);
1192-
RETURN_FALSE;
1193-
}
1194-
intern->pixels.pixels[offset] = (Uint8)value;
1195-
}
1196-
/* }}} */
1197-
1198-
ZEND_BEGIN_ARG_INFO_EX(arginfo_SDL_Pixels_GetByte, 0, 0, 2)
1199-
ZEND_ARG_INFO(0, x)
1200-
ZEND_ARG_INFO(0, y)
1201-
ZEND_END_ARG_INFO()
1202-
1203-
/* {{{ proto int SDL_Pixels::GetByte(int x, int y) */
1204-
PHP_METHOD(SDL_Pixels, GetByte)
1205-
{
1206-
php_sdl_pixels *intern;
1207-
zval *z_pixels;
1208-
zend_long x, y;
1209-
1210-
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oll", &z_pixels, php_sdl_pixels_ce, &x, &y) == FAILURE)
1211-
{
1212-
return;
1213-
}
1214-
intern = PHP_SDL_PIXELS_P(z_pixels);
1215-
1216-
if (x < 0 || x >= intern->pixels.pitch || y < 0 || y >= intern->pixels.h)
1217-
{
1218-
php_error_docref(NULL, E_NOTICE, "Invalid position (%ld,%ld) in SDL_Pixels (%d,%d)", (long)x, (long)y, intern->pixels.pitch, intern->pixels.h);
1219-
RETURN_FALSE;
1220-
}
1221-
RETVAL_LONG(intern->pixels.pixels[y * intern->pixels.pitch + x]);
1222-
}
1223-
/* }}} */
1224-
1225-
ZEND_BEGIN_ARG_INFO_EX(arginfo_SDL_Pixels_SetByte, 0, 0, 3)
1226-
ZEND_ARG_INFO(0, x)
1227-
ZEND_ARG_INFO(0, y)
1228-
ZEND_ARG_INFO(0, byte)
1229-
ZEND_END_ARG_INFO()
1230-
1231-
/* {{{ proto int SDL_Pixels::SetByte(int x, int y, int byte) */
1232-
PHP_METHOD(SDL_Pixels, SetByte)
1233-
{
1234-
php_sdl_pixels *intern;
1235-
zval *z_pixels;
1236-
zend_long x, y, v;
1237-
1238-
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Olll", &z_pixels, php_sdl_pixels_ce, &x, &y, &v) == FAILURE)
1239-
{
1240-
return;
1241-
}
1242-
intern = PHP_SDL_PIXELS_P(z_pixels);
1243-
1244-
if (x < 0 || x >= intern->pixels.pitch || y < 0 || y >= intern->pixels.h)
1245-
{
1246-
php_error_docref(NULL, E_NOTICE, "Invalid position (%ld,%ld) in SDL_Pixels (%d,%d)", (long)x, (long)y, intern->pixels.pitch, intern->pixels.h);
1247-
RETURN_FALSE;
1248-
}
1249-
RETVAL_LONG(intern->pixels.pixels[y * intern->pixels.pitch + x]);
1250-
intern->pixels.pixels[y * intern->pixels.pitch + x] = (Uint8)v;
1251-
}
1252-
/* }}} */
1253-
12541085
/* {{{ php_sdl_palette_free
12551086
*/
12561087
static void php_sdl_palette_free(zend_object *object)
@@ -1584,30 +1415,18 @@ zval *sdl_pixels_read_property(zend_object *object, zend_string *member, int typ
15841415

15851416
zval *retval;
15861417

1587-
if (!intern->pixels.pixels)
1588-
{
1589-
return zend_std_read_property(object, member, type, cache_slot, rv);
1590-
}
1591-
15921418
retval = rv;
15931419

1594-
if (!strcmp(member_val, "h"))
1420+
if (!strcmp(member_val, "pixels"))
15951421
{
1596-
ZVAL_LONG(retval, intern->pixels.h);
1597-
}
1598-
else if (!strcmp(member_val, "pitch"))
1599-
{
1600-
ZVAL_LONG(retval, intern->pixels.pitch);
1601-
}
1602-
else if (!strcmp(member_val, "count"))
1603-
{
1604-
ZVAL_LONG(retval, intern->pixels.pitch * intern->pixels.h);
1422+
if (!intern->pixels.pixels)
1423+
{
1424+
return zend_std_read_property(object, member, type, cache_slot, rv);
1425+
}
16051426
}
16061427
else
16071428
{
16081429
retval = zend_std_read_property(object, member, type, cache_slot, rv);
1609-
1610-
return retval;
16111430
}
16121431

16131432
return retval;
@@ -1618,33 +1437,6 @@ zval *sdl_pixels_read_property(zend_object *object, zend_string *member, int typ
16181437
ZVAL_LONG(&zv, f); \
16191438
zend_hash_str_update(props, n, sizeof(n) - 1, &zv);
16201439

1621-
/* {{{ sdl_pixels_read_properties */
1622-
static HashTable *sdl_pixels_get_properties(zend_object *object)
1623-
{
1624-
HashTable *props;
1625-
zval zv;
1626-
php_sdl_pixels *intern = php_sdl_pixels_from_obj(object);
1627-
1628-
props = zend_std_get_properties(object);
1629-
1630-
if (intern->pixels.pixels)
1631-
{
1632-
SDL_PIXELS_ADD_PROPERTY("pitch", intern->pixels.pitch);
1633-
SDL_PIXELS_ADD_PROPERTY("h", intern->pixels.h);
1634-
SDL_PIXELS_ADD_PROPERTY("count", intern->pixels.h * intern->pixels.pitch);
1635-
}
1636-
return props;
1637-
}
1638-
/* }}} */
1639-
1640-
/* {{{ sdl_pixels_write_property */
1641-
static zval *sdl_pixels_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot)
1642-
{
1643-
php_error_docref(NULL, E_ERROR, "Not supported, SDL_Pixels is read-only");
1644-
return value;
1645-
}
1646-
/* }}} */
1647-
16481440
/* {{{ php_sdl_color_methods[] */
16491441
static const zend_function_entry php_sdl_color_methods[] = {
16501442
PHP_ME(SDL_Color, __construct, arginfo_SDL_Color__construct, ZEND_ACC_CTOR | ZEND_ACC_PUBLIC)
@@ -1690,13 +1482,6 @@ static const zend_function_entry php_sdl_pixelformat_methods[] = {
16901482
static const zend_function_entry php_sdl_pixels_methods[] = {
16911483
PHP_ME(SDL_Pixels, __construct, arginfo_SDL_Pixels__construct, ZEND_ACC_PUBLIC)
16921484
PHP_ME(SDL_Pixels, __toString, arginfo_class_SDL_Pixels___toString, ZEND_ACC_PUBLIC)
1693-
PHP_ME(SDL_Pixels, count, arginfo_format_none, ZEND_ACC_PUBLIC)
1694-
PHP_ME(SDL_Pixels, offsetExists, arginfo_SDL_Pixels_offsetExists, ZEND_ACC_PUBLIC)
1695-
PHP_ME(SDL_Pixels, offsetGet, arginfo_SDL_Pixels_offsetGet, ZEND_ACC_PUBLIC)
1696-
PHP_ME(SDL_Pixels, offsetSet, arginfo_SDL_Pixels_offsetSet, ZEND_ACC_PUBLIC)
1697-
PHP_ME(SDL_Pixels, offsetUnset, arginfo_SDL_Pixels_offsetUnset, ZEND_ACC_PUBLIC)
1698-
PHP_ME(SDL_Pixels, GetByte, arginfo_SDL_Pixels_GetByte, ZEND_ACC_PUBLIC)
1699-
PHP_ME(SDL_Pixels, SetByte, arginfo_SDL_Pixels_SetByte, ZEND_ACC_PUBLIC)
17001485
PHP_FE_END};
17011486
/* }}} */
17021487

@@ -1772,18 +1557,11 @@ PHP_MINIT_FUNCTION(sdl_pixels)
17721557
INIT_CLASS_ENTRY(ce_pixels, "SDL_Pixels", php_sdl_pixels_methods);
17731558
php_sdl_pixels_ce = zend_register_internal_class(&ce_pixels);
17741559
php_sdl_pixels_ce->create_object = php_sdl_pixels_new;
1775-
zend_class_implements(php_sdl_pixels_ce, 1, zend_ce_arrayaccess);
17761560
memcpy(&php_sdl_pixels_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
17771561
php_sdl_pixels_handlers.read_property = sdl_pixels_read_property;
1778-
php_sdl_pixels_handlers.get_properties = sdl_pixels_get_properties;
1779-
php_sdl_pixels_handlers.write_property = sdl_pixels_write_property;
17801562
php_sdl_pixels_handlers.free_obj = php_sdl_pixels_free;
17811563
php_sdl_pixels_handlers.offset = XtOffsetOf(php_sdl_pixels, zo);
17821564

1783-
REGISTER_PIXELS_PROP("pitch");
1784-
REGISTER_PIXELS_PROP("h");
1785-
REGISTER_PIXELS_PROP("count");
1786-
17871565
/* Pixel type. */
17881566
REGISTER_LONG_CONSTANT("SDL_PIXELTYPE_UNKNOWN", SDL_PIXELTYPE_UNKNOWN, CONST_CS | CONST_PERSISTENT);
17891567
REGISTER_LONG_CONSTANT("SDL_PIXELTYPE_INDEX1", SDL_PIXELTYPE_INDEX1, CONST_CS | CONST_PERSISTENT);

src/pixels.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ extern "C" {
2828
/* PHP specific struct to manage memory access */
2929
typedef struct SDL_Pixels
3030
{
31-
int h;
32-
int pitch;
3331
Uint8 *pixels;
3432
} SDL_Pixels;
3533

src/render.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,18 @@ PHP_FUNCTION(SDL_UpdateTexture)
227227
if(z_rect != NULL && Z_TYPE_P(z_rect) != IS_NULL) {
228228
rect = &def_rect;
229229
zval_to_sdl_rect(z_rect, rect);
230+
rect = NULL;
230231
}
231232

232233
if (!(pixels = zval_to_sdl_pixels(z_pixels)))
233234
{
234235
php_error_docref(NULL, E_WARNING, "Invalid source SDL_Pixels object");
235236
}
237+
236238
texture = (SDL_Texture*)zend_fetch_resource(Z_RES_P(z_texture), SDL_TEXTURE_RES_NAME, le_sdl_texture);
237239

238240
if( texture ) {
239-
RETURN_LONG(SDL_UpdateTexture(texture, rect, pixels, pitch));
241+
RETURN_LONG(SDL_UpdateTexture(texture, rect, pixels->pixels, pitch));
240242
}
241243
}
242244

src/render.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_SDL_UpdateTexture, 0, 0, 4)
8989
ZEND_ARG_OBJ_INFO(0, texture, SDL_Texture, 0)
9090
ZEND_ARG_OBJ_INFO(0, rect, SDL_Rect, 1)
9191
ZEND_ARG_OBJ_INFO(0, pixels, SDL_Pixels, 0)
92-
ZEND_ARG_INFO(0, pitch)
92+
ZEND_ARG_TYPE_INFO(0, pitch, IS_LONG, 0)
9393
ZEND_END_ARG_INFO()
9494

9595
ZEND_BEGIN_ARG_INFO_EX(arginfo_SDL_QueryTexture, 0, 0, 1)
@@ -133,9 +133,9 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_SDL_RenderCopyEx, 0, 0, 7)
133133
ZEND_END_ARG_INFO()
134134

135135
ZEND_BEGIN_ARG_INFO_EX(arginfo_SDL_RenderSetLogicalSize, 0, 0, 3)
136-
ZEND_ARG_INFO(0, renderer)
137-
ZEND_ARG_INFO(0, w)
138-
ZEND_ARG_INFO(0, h)
136+
ZEND_ARG_OBJ_INFO(0, renderer, SDL_Renderer, 0)
137+
ZEND_ARG_TYPE_INFO(0, w, IS_LONG, 0)
138+
ZEND_ARG_TYPE_INFO(0, h, IS_LONG, 0)
139139
ZEND_END_ARG_INFO()
140140

141141
ZEND_BEGIN_ARG_INFO_EX(arginfo_SDL_GetRendererOutputSize, 0, 0, 3)

0 commit comments

Comments
 (0)