Skip to content

Commit 419eb9d

Browse files
authored
add touchscreen (#77)
* add touchscreen * deprecated dynamic properties * properties dynamic * bug render target null
1 parent 04c9014 commit 419eb9d

File tree

3 files changed

+77
-6
lines changed

3 files changed

+77
-6
lines changed

src/event.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ zend_bool sdl_event_to_zval(SDL_Event *event, zval *value)
4848

4949
switch (event->type)
5050
{
51+
case SDL_FINGERDOWN:
52+
case SDL_FINGERUP:
53+
case SDL_FINGERMOTION:
54+
{
55+
zval tfinger;
56+
php_sdl_touchfingerevent_to_zval(&event->tfinger, &tfinger);
57+
add_property_zval(value, "tfinger", &tfinger);
58+
59+
}
60+
break;
5161
case SDL_MOUSEMOTION:
5262
{
5363
zval motion;
@@ -240,6 +250,11 @@ PHP_MINIT_FUNCTION(sdl_event)
240250

241251
REGISTER_LONG_CONSTANT("SDL_QUIT", SDL_QUIT, CONST_CS | CONST_PERSISTENT);
242252

253+
REGISTER_LONG_CONSTANT("SDL_FINGERDOWN", SDL_FINGERDOWN, CONST_CS | CONST_PERSISTENT);
254+
REGISTER_LONG_CONSTANT("SDL_FINGERUP", SDL_FINGERUP, CONST_CS | CONST_PERSISTENT);
255+
REGISTER_LONG_CONSTANT("SDL_FINGERMOTION", SDL_FINGERMOTION, CONST_CS | CONST_PERSISTENT);
256+
257+
243258
REGISTER_LONG_CONSTANT("SDL_APP_TERMINATING", SDL_APP_TERMINATING, CONST_CS | CONST_PERSISTENT);
244259
REGISTER_LONG_CONSTANT("SDL_APP_LOWMEMORY", SDL_APP_LOWMEMORY, CONST_CS | CONST_PERSISTENT);
245260
REGISTER_LONG_CONSTANT("SDL_APP_WILLENTERBACKGROUND", SDL_APP_WILLENTERBACKGROUND, CONST_CS | CONST_PERSISTENT);
@@ -291,6 +306,50 @@ PHP_MINIT_FUNCTION(sdl_event)
291306
zend_declare_property_null(php_sdl_event_ce, ZEND_STRL("window"), ZEND_ACC_PUBLIC);
292307
zend_declare_property_null(php_sdl_event_ce, ZEND_STRL("button"), ZEND_ACC_PUBLIC);
293308

309+
310+
// touchpad
311+
zend_declare_property_null(php_sdl_event_ce, "tfinger", sizeof("tfinger") - 1, ZEND_ACC_PUBLIC);
312+
313+
zend_class_entry ce;
314+
INIT_CLASS_ENTRY(ce, "SDL_TouchFingerEvent", NULL);
315+
sdlTouchFingerEvent_ce = zend_register_internal_class(&ce);
316+
317+
318+
// Déclaration explicite des propriétés publiques
319+
zend_declare_property_null(sdlTouchFingerEvent_ce, "type", sizeof("type") - 1, ZEND_ACC_PUBLIC);
320+
zend_declare_property_null(sdlTouchFingerEvent_ce, "timestamp", sizeof("timestamp") - 1, ZEND_ACC_PUBLIC);
321+
zend_declare_property_null(sdlTouchFingerEvent_ce, "x", sizeof("x") - 1, ZEND_ACC_PUBLIC);
322+
zend_declare_property_null(sdlTouchFingerEvent_ce, "y", sizeof("y") - 1, ZEND_ACC_PUBLIC);
323+
zend_declare_property_null(sdlTouchFingerEvent_ce, "dx", sizeof("dx") - 1, ZEND_ACC_PUBLIC);
324+
zend_declare_property_null(sdlTouchFingerEvent_ce, "dy", sizeof("dy") - 1, ZEND_ACC_PUBLIC);
325+
zend_declare_property_null(sdlTouchFingerEvent_ce, "pressure", sizeof("pressure") - 1, ZEND_ACC_PUBLIC);
326+
zend_declare_property_null(sdlTouchFingerEvent_ce, "fingerId", sizeof("fingerId") - 1, ZEND_ACC_PUBLIC);
327+
zend_declare_property_null(sdlTouchFingerEvent_ce, "touchId", sizeof("touchId") - 1, ZEND_ACC_PUBLIC);
328+
329+
294330
return SUCCESS;
295331
}
296332
/* }}} */
333+
334+
zend_class_entry *sdlTouchFingerEvent_ce;
335+
336+
zend_object* php_sdl_touchfingerevent_new(zend_class_entry *class_type) {
337+
zend_object *obj = zend_objects_new(class_type);
338+
object_properties_init(obj, class_type);
339+
obj->handlers = &std_object_handlers;
340+
return obj;
341+
}
342+
343+
void php_sdl_touchfingerevent_to_zval(SDL_TouchFingerEvent *event, zval *value) {
344+
object_init_ex(value, sdlTouchFingerEvent_ce);
345+
346+
add_property_long(value, "type", event->type);
347+
add_property_long(value, "timestamp", event->timestamp);
348+
add_property_double(value, "x", event->x);
349+
add_property_double(value, "y", event->y);
350+
add_property_double(value, "dx", event->dx);
351+
add_property_double(value, "dy", event->dy);
352+
add_property_double(value, "pressure", event->pressure);
353+
add_property_long(value, "fingerId", (zend_long)event->fingerId);
354+
add_property_long(value, "touchId", (zend_long)event->touchId);
355+
}

src/event.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ extern "C" {
2727
zend_class_entry *get_php_sdl_event_ce(void);
2828
zend_bool sdl_event_to_zval(SDL_Event *event, zval *value);
2929
zend_bool zval_to_sdl_event(zval *value, SDL_Event *event);
30-
30+
void php_sdl_touchfingerevent_to_zval(SDL_TouchFingerEvent *event, zval *value);
31+
extern zend_class_entry *sdlTouchFingerEvent_ce; // déclaration externe
3132
ZEND_BEGIN_ARG_INFO_EX(arginfo_SDL_PollEvent, 0, 0, 1)
3233
ZEND_ARG_OBJ_INFO(1, event, SDL_Event, 0)
3334
ZEND_END_ARG_INFO()
@@ -42,7 +43,7 @@ PHP_FUNCTION(SDL_WaitEvent);
4243
PHP_MINIT_FUNCTION(sdl_event);
4344

4445
#ifdef __cplusplus
45-
} // extern "C"
46+
} // extern "C"
4647
#endif
4748

4849
#endif /* PHP_SDL_EVENT_H */

src/render.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ PHP_FUNCTION(SDL_UpdateTexture)
235235
if(z_rect != NULL && Z_TYPE_P(z_rect) != IS_NULL) {
236236
rect = &def_rect;
237237
zval_to_sdl_rect(z_rect, rect);
238-
rect = NULL;
238+
rect = NULL;
239239
}
240240

241241
if (!(pixels = zval_to_sdl_pixels(z_pixels)))
@@ -261,9 +261,20 @@ PHP_FUNCTION(SDL_SetRenderTarget)
261261
}
262262

263263
renderer = (SDL_Renderer*)zend_fetch_resource(Z_RES_P(z_renderer), SDL_RENDERER_RES_NAME, le_sdl_renderer);
264-
texture = (SDL_Texture*)zend_fetch_resource(Z_RES_P(z_texture), SDL_TEXTURE_RES_NAME, le_sdl_texture);
265-
266-
if( renderer && texture ) {
264+
// texture = (SDL_Texture*)zend_fetch_resource(Z_RES_P(z_texture), SDL_TEXTURE_RES_NAME, le_sdl_texture);
265+
if (Z_TYPE_P(z_texture) == IS_NULL) {
266+
// SDL accepte NULL pour reset la target vers le default render target
267+
texture = NULL;
268+
} else {
269+
texture = (SDL_Texture*)zend_fetch_resource(Z_RES_P(z_texture), SDL_TEXTURE_RES_NAME, le_sdl_texture);
270+
if (!texture) {
271+
php_error_docref(NULL, E_WARNING, "Invalid SDL_Texture resource");
272+
RETURN_FALSE;
273+
}
274+
}
275+
276+
if (renderer)
277+
{
267278
RETURN_LONG(SDL_SetRenderTarget(renderer, texture));
268279
}
269280
}

0 commit comments

Comments
 (0)