Skip to content

Commit

Permalink
Merge branch 'develop' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
krakjoe committed Oct 28, 2016
2 parents feab62f + 504cbe7 commit bd9d52d
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 128 deletions.
123 changes: 123 additions & 0 deletions classes/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2016 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: krakjoe |
+----------------------------------------------------------------------+
*/
#ifndef HAVE_PHP_UI_COMMON_H
#define HAVE_PHP_UI_COMMON_H

#include <classes/point.h>
#include <classes/size.h>

static inline void php_ui_point_size_add(php_ui_point_t *result, php_ui_point_t *point, double x, double y) {
result->x = point->x + x;
result->y = point->y + y;
}

static inline void php_ui_point_size_assign_add(php_ui_point_t*result, double x, double y) {
result->x += x;
result->y += y;
}

static inline void php_ui_point_size_mul(php_ui_point_t *result, php_ui_point_t *point, double x, double y) {
result->x = point->x * x;
result->y = point->y * y;
}

static inline void php_ui_point_size_assign_mul(php_ui_point_t*result, double x, double y) {
result->x *= x;
result->y *= y;
}

static inline void php_ui_point_size_div(php_ui_point_t *result, php_ui_point_t *point, double x, double y) {
result->x = point->x / x;
result->y = point->y / y;
}

static inline void php_ui_point_size_assign_div(php_ui_point_t*result, double x, double y) {
result->x /= x;
result->y /= y;
}

static inline void php_ui_point_size_sub(php_ui_point_t *result, php_ui_point_t *point, double x, double y) {
result->x = point->x - x;
result->y = point->y - y;
}

static inline void php_ui_point_size_assign_sub(php_ui_point_t*result, double x, double y) {
result->x -= x;
result->y -= y;
}

/* {{{ */
static inline int php_ui_point_size_operation(zend_uchar opcode, zval *retval, zval *op1, zval *op2, zend_class_entry *target) {
zval *l, *r;
double x = 0, y = 0;
php_ui_point_t *point, *result;
zend_bool assign = 0;

if (retval == op1) {
assign = 1;
} else {
object_init_ex(retval, target);
}

if (Z_TYPE_P(op1) == IS_OBJECT && Z_TYPE_P(op2) == IS_OBJECT) {
if ((instanceof_function(Z_OBJCE_P(op1), uiSize_ce) || instanceof_function(Z_OBJCE_P(op1), uiPoint_ce)) &&
(instanceof_function(Z_OBJCE_P(op2), uiSize_ce) || instanceof_function(Z_OBJCE_P(op2), uiPoint_ce))) {
l = op1;
r = op2;
goto operate;
}
}

if ((Z_TYPE_P(op1) == IS_OBJECT) &&
(instanceof_function(Z_OBJCE_P(op1), uiPoint_ce) || instanceof_function(Z_OBJCE_P(op1), uiSize_ce))) {
l = op1;
r = op2;
} else if ((Z_TYPE_P(op2) == IS_OBJECT) &&
(instanceof_function(Z_OBJCE_P(op2), uiPoint_ce) || instanceof_function(Z_OBJCE_P(op2), uiSize_ce))) {
l = op2;
r = op1;
} else {
return FAILURE;
}

operate:
point = php_ui_point_fetch(l);

if (Z_TYPE_P(r) != IS_OBJECT) {
x = zval_get_double(r);
y = zval_get_double(r);
} else {
php_ui_point_t *value = php_ui_point_fetch(r);

x = value->x;
y = value->y;
}

result = php_ui_point_fetch(retval);

switch (opcode) {
case ZEND_ADD: assign ? php_ui_point_size_assign_add(result, x, y) : php_ui_point_size_add(result, point, x, y); break;
case ZEND_MUL: assign ? php_ui_point_size_assign_mul(result, x, y) : php_ui_point_size_mul(result, point, x, y); break;
case ZEND_DIV: assign ? php_ui_point_size_assign_div(result, x, y) : php_ui_point_size_div(result, point, x, y); break;
case ZEND_SUB: assign ? php_ui_point_size_assign_sub(result, x, y) : php_ui_point_size_sub(result, point, x, y); break;
}

return SUCCESS;
} /* }}} */

#endif
92 changes: 37 additions & 55 deletions classes/point.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include "php.h"

#include <classes/point.h>
#include <classes/common.h>

zend_object_handlers php_ui_point_handlers;

Expand Down Expand Up @@ -130,72 +130,54 @@ PHP_METHOD(Point, setY)
point->y = y;
} /* }}} */

/* {{{ proto Point Point::at(double value)
Point Point::at(UI\Size size) */
PHP_METHOD(Point, at)
{
php_ui_point_t *point;
zval *location = NULL;

if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "z", &location) != SUCCESS) {
return;
}

if (Z_TYPE_P(location) == IS_OBJECT && instanceof_function(Z_OBJCE_P(location), uiPoint_ce)) {
RETURN_ZVAL(location, 1, 0);
}

if (Z_TYPE_P(location) == IS_OBJECT && instanceof_function(Z_OBJCE_P(location), uiSize_ce)) {
php_ui_size_t *size = php_ui_size_fetch(location);

object_init_ex(return_value, uiPoint_ce);

point = php_ui_point_fetch(return_value);

point->x = size->width;
point->y = size->height;
return;
}

object_init_ex(return_value, uiPoint_ce);

point = php_ui_point_fetch(return_value);
point->x = zval_get_double(location);
point->y = zval_get_double(location);
} /* }}} */

/* {{{ */
const zend_function_entry php_ui_point_methods[] = {
PHP_ME(Point, __construct, php_ui_point_construct_info, ZEND_ACC_PUBLIC)
PHP_ME(Point, getX, php_ui_point_get_point_info, ZEND_ACC_PUBLIC)
PHP_ME(Point, getY, php_ui_point_get_point_info, ZEND_ACC_PUBLIC)
PHP_ME(Point, setX, php_ui_point_set_point_info, ZEND_ACC_PUBLIC)
PHP_ME(Point, setY, php_ui_point_set_point_info, ZEND_ACC_PUBLIC)
PHP_ME(Point, at, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_FE_END
}; /* }}} */

/* {{{ */
static int php_ui_point_operate(zend_uchar opcode, zval *result, zval *op1, zval *op2) {
php_ui_point_t *point = php_ui_point_fetch(op1);

#define php_ui_point_do_operation(operator) do { \
if (Z_TYPE_P(op2) == IS_OBJECT && instanceof_function(Z_OBJCE_P(op2), uiPoint_ce)) { \
php_ui_point_t *operand = php_ui_point_fetch(op2); \
php_ui_point_t *retval; \
\
if (result != op1) { \
object_init_ex(result, uiPoint_ce); \
} \
\
retval = php_ui_point_fetch(result); \
retval->x = point->x operator operand->x; \
retval->y = point->x operator operand->y; \
\
return SUCCESS; \
} \
\
if (Z_TYPE_P(op2) == IS_LONG || Z_TYPE_P(op2) == IS_DOUBLE) { \
php_ui_point_t *retval; \
\
if (result != op1) { \
object_init_ex(result, uiPoint_ce); \
} \
\
retval = php_ui_point_fetch(result); \
retval->x = point->x operator zval_get_double(op2); \
retval->y = point->y operator zval_get_double(op2); \
\
return SUCCESS; \
} \
} while(0)

switch (opcode) {
case ZEND_MUL:
php_ui_point_do_operation(*);
break;

case ZEND_DIV:
php_ui_point_do_operation(/);
break;

case ZEND_SUB:
php_ui_point_do_operation(-);
break;

case ZEND_ADD:
php_ui_point_do_operation(+);
break;
}

#undef php_ui_point_do_operation

return FAILURE;
return php_ui_point_size_operation(opcode, result, op1, op2, uiPoint_ce);
} /* }}} */

/* {{{ */
Expand Down
92 changes: 37 additions & 55 deletions classes/size.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include "php.h"

#include <classes/size.h>
#include <classes/common.h>

zend_object_handlers php_ui_size_handlers;

Expand Down Expand Up @@ -130,72 +130,54 @@ PHP_METHOD(Size, setHeight)
size->height = height;
} /* }}} */

/* {{{ proto Size Size::of(double value)
Size Size::of(UI\Point point) */
PHP_METHOD(Size, of)
{
php_ui_size_t *size;
zval *location = NULL;

if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "z", &location) != SUCCESS) {
return;
}

if (Z_TYPE_P(location) == IS_OBJECT && instanceof_function(Z_OBJCE_P(location), uiSize_ce)) {
RETURN_ZVAL(location, 1, 0);
}

if (Z_TYPE_P(location) == IS_OBJECT && instanceof_function(Z_OBJCE_P(location), uiPoint_ce)) {
php_ui_point_t *point = php_ui_point_fetch(location);

object_init_ex(return_value, uiSize_ce);

size = php_ui_size_fetch(return_value);

size->width = point->x;
size->height = point->y;
return;
}

object_init_ex(return_value, uiSize_ce);

size = php_ui_size_fetch(return_value);
size->width = zval_get_double(location);
size->height = zval_get_double(location);
} /* }}} */

/* {{{ */
const zend_function_entry php_ui_size_methods[] = {
PHP_ME(Size, __construct, php_ui_size_construct_info, ZEND_ACC_PUBLIC)
PHP_ME(Size, getWidth, php_ui_size_get_size_info, ZEND_ACC_PUBLIC)
PHP_ME(Size, getHeight, php_ui_size_get_size_info, ZEND_ACC_PUBLIC)
PHP_ME(Size, setWidth, php_ui_size_set_size_info, ZEND_ACC_PUBLIC)
PHP_ME(Size, setHeight, php_ui_size_set_size_info, ZEND_ACC_PUBLIC)
PHP_ME(Size, of, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_FE_END
}; /* }}} */

/* {{{ */
static int php_ui_size_operate(zend_uchar opcode, zval *result, zval *op1, zval *op2) {
php_ui_size_t *size = php_ui_size_fetch(op1);

#define php_ui_size_do_operation(operator) do { \
if (Z_TYPE_P(op2) == IS_OBJECT && instanceof_function(Z_OBJCE_P(op2), uiSize_ce)) { \
php_ui_size_t *operand = php_ui_size_fetch(op2); \
php_ui_size_t *retval; \
\
if (result != op1) { \
object_init_ex(result, uiSize_ce); \
} \
\
retval = php_ui_size_fetch(result); \
retval->width = size->width operator operand->width; \
retval->height = size->height operator operand->height; \
\
return SUCCESS; \
} \
\
if (Z_TYPE_P(op2) == IS_LONG || Z_TYPE_P(op2) == IS_DOUBLE) { \
php_ui_size_t *retval; \
\
if (result != op1) { \
object_init_ex(result, uiSize_ce); \
} \
\
retval = php_ui_size_fetch(result); \
retval->width = size->width operator zval_get_double(op2); \
retval->height = size->height operator zval_get_double(op2); \
\
return SUCCESS; \
} \
} while(0)

switch (opcode) {
case ZEND_MUL:
php_ui_size_do_operation(*);
break;

case ZEND_DIV:
php_ui_size_do_operation(/);
break;

case ZEND_SUB:
php_ui_size_do_operation(-);
break;

case ZEND_ADD:
php_ui_size_do_operation(+);
break;
}

#undef php_ui_size_do_operation

return FAILURE;
return php_ui_point_size_operation(opcode, result, op1, op2, uiSize_ce);
} /* }}} */

/* {{{ */
Expand Down
20 changes: 8 additions & 12 deletions examples/starfield.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@ protected function onKey(string $key, int $ext, int $flags) {
}

protected function onDraw(UI\Draw\Pen $pen, UI\Size $size, UI\Point $clip, UI\Size $clipSize) {
$hWidth = $size->width / 2;
$hHeight = $size->height /2;

$hSize = $size / 2;

$path = new Path(Path::Winding);
$path->addRectangle(
new Point(0, 0), $size);
$path->addRectangle(Point::at(0), $size);
$path->end();
$pen->fill($path, new Brush(Brush::Solid, new Color(0, 1)));

Expand All @@ -69,18 +67,16 @@ protected function onDraw(UI\Draw\Pen $pen, UI\Size $size, UI\Point $clip, UI\Si
$star[1] = $this->depth;
}

$k = 128 / $star[1];
$px = $star[0]->x * $k + $hWidth;
$py = $star[0]->y * $k + $hHeight;

if ($px >= 0 && $px <= $size->width && $py >= 0 && $py <= $size->height) {
$pos = $star[0] * (128 / $star[1]) + $hSize;

if ($pos->x >= 0 && $pos->x <= $size->width && $pos->y >= 0 && $pos->y <= $size->height) {
$starSize = (1 - $star[1] / 32) * 5;

$path = new Path(Path::Winding);
if (PHP_OS == "WINNT") {
$path->addRectangle(new Point($px, $py), new Size($starSize, $starSize));
$path->addRectangle($pos, new Size($starSize, $starSize));
} else {
$path->arcTo(new Point($px, $py), $starSize/2, 0, M_PI*2, 0);
$path->newFigureWithArc($pos, $starSize/2, 0, M_PI*2, 0);
}
$path->end();

Expand Down
Loading

0 comments on commit bd9d52d

Please sign in to comment.