Compare commits

...

3 Commits

13 changed files with 87 additions and 36 deletions

View File

@ -13,10 +13,11 @@ UIDrawable* UICaption::click_at(sf::Vector2f point)
return NULL;
}
void UICaption::render(sf::Vector2f offset)
void UICaption::render(sf::Vector2f offset, sf::RenderTarget& target)
{
text.move(offset);
Resources::game->getWindow().draw(text);
//Resources::game->getWindow().draw(text);
target.draw(text);
text.move(-offset);
}

View File

@ -7,7 +7,7 @@ class UICaption: public UIDrawable
{
public:
sf::Text text;
void render(sf::Vector2f) override final;
void render(sf::Vector2f, sf::RenderTarget&) override final;
PyObjectsEnum derived_type() override final;
virtual UIDrawable* click_at(sf::Vector2f point) override final;

View File

@ -3,6 +3,7 @@
#include "UICaption.h"
#include "UISprite.h"
#include "UIGrid.h"
#include "GameEngine.h"
UIDrawable::UIDrawable() { click_callable = NULL; }
@ -13,7 +14,7 @@ void UIDrawable::click_unregister()
void UIDrawable::render()
{
render(sf::Vector2f());
render(sf::Vector2f(), Resources::game->getWindow());
}
PyObject* UIDrawable::get_click(PyObject* self, void* closure) {

View File

@ -28,7 +28,8 @@ class UIDrawable
{
public:
void render();
virtual void render(sf::Vector2f) = 0;
//virtual void render(sf::Vector2f) = 0;
virtual void render(sf::Vector2f, sf::RenderTarget&) = 0;
virtual PyObjectsEnum derived_type() = 0;
// Mouse input handling - callable object, methods to find event's destination

View File

@ -95,6 +95,10 @@ PyObject* sfVector2f_to_PyObject(sf::Vector2f vector) {
return Py_BuildValue("(ff)", vector.x, vector.y);
}
PyObject* sfVector2i_to_PyObject(sf::Vector2i vector) {
return Py_BuildValue("(ii)", vector.x, vector.y);
}
sf::Vector2f PyObject_to_sfVector2f(PyObject* obj) {
float x, y;
if (!PyArg_ParseTuple(obj, "ff", &x, &y)) {
@ -103,6 +107,14 @@ sf::Vector2f PyObject_to_sfVector2f(PyObject* obj) {
return sf::Vector2f(x, y);
}
sf::Vector2i PyObject_to_sfVector2i(PyObject* obj) {
int x, y;
if (!PyArg_ParseTuple(obj, "ii", &x, &y)) {
return sf::Vector2i(); // TODO / reconsider this default: Return default vector on parse error
}
return sf::Vector2i(x, y);
}
// TODO - deprecate / remove this helper
PyObject* UIGridPointState_to_PyObject(const UIGridPointState& state) {
return PyObject_New(PyObject, (PyTypeObject*)PyObject_GetAttrString(McRFPy_API::mcrf_module, "GridPointState"));
@ -125,11 +137,19 @@ PyObject* UIGridPointStateVector_to_PyList(const std::vector<UIGridPointState>&
}
PyObject* UIEntity::get_position(PyUIEntityObject* self, void* closure) {
if (reinterpret_cast<long>(closure) == 0) {
return sfVector2f_to_PyObject(self->data->position);
} else {
return sfVector2i_to_PyObject(self->data->collision_pos);
}
}
int UIEntity::set_position(PyUIEntityObject* self, PyObject* value, void* closure) {
if (reinterpret_cast<long>(closure) == 0) {
self->data->position = PyObject_to_sfVector2f(value);
} else {
self->data->collision_pos = PyObject_to_sfVector2i(value);
}
return 0;
}
@ -158,7 +178,8 @@ PyMethodDef UIEntity::methods[] = {
};
PyGetSetDef UIEntity::getsetters[] = {
{"position", (getter)UIEntity::get_position, (setter)UIEntity::set_position, "Entity position", NULL},
{"draw_pos", (getter)UIEntity::get_position, (setter)UIEntity::set_position, "Entity position (graphically)", (void*)0},
{"pos", (getter)UIEntity::get_position, (setter)UIEntity::set_position, "Entity position (integer grid coordinates)", (void*)1},
{"gridstate", (getter)UIEntity::get_gridstate, NULL, "Grid point states for the entity", NULL},
{"sprite_number", (getter)UIEntity::get_spritenumber, (setter)UIEntity::set_spritenumber, "Sprite number (index) on the texture on the display", NULL},
{NULL} /* Sentinel */

View File

@ -40,7 +40,8 @@ public:
std::vector<UIGridPointState> gridstate;
UISprite sprite;
sf::Vector2f position; //(x,y) in grid coordinates; float for animation
void render(sf::Vector2f); //override final;
sf::Vector2i collision_pos; //(x, y) in grid coordinates: int for collision
//void render(sf::Vector2f); //override final;
UIEntity();
UIEntity(UIGrid&);

View File

@ -44,14 +44,15 @@ PyObjectsEnum UIFrame::derived_type()
return PyObjectsEnum::UIFRAME;
}
void UIFrame::render(sf::Vector2f offset)
void UIFrame::render(sf::Vector2f offset, sf::RenderTarget& target)
{
box.move(offset);
Resources::game->getWindow().draw(box);
//Resources::game->getWindow().draw(box);
target.draw(box);
box.move(-offset);
for (auto drawable : *children) {
drawable->render(offset + box.getPosition());
drawable->render(offset + box.getPosition(), target);
}
}

View File

@ -28,7 +28,7 @@ public:
sf::RectangleShape box;
float outline;
std::shared_ptr<std::vector<std::shared_ptr<UIDrawable>>> children;
void render(sf::Vector2f) override final;
void render(sf::Vector2f, sf::RenderTarget&) override final;
void move(sf::Vector2f);
PyObjectsEnum derived_type() override final;
virtual UIDrawable* click_at(sf::Vector2f point) override final;

View File

@ -32,9 +32,9 @@ UIGrid::UIGrid(int gx, int gy, std::shared_ptr<PyTexture> _ptex, sf::Vector2f _x
void UIGrid::update() {}
void UIGrid::render(sf::Vector2f)
void UIGrid::render(sf::Vector2f offset, sf::RenderTarget& target)
{
output.setPosition(box.getPosition()); // output sprite can move; update position when drawing
output.setPosition(box.getPosition() + offset); // output sprite can move; update position when drawing
// output size can change; update size when drawing
output.setTextureRect(
sf::IntRect(0, 0,
@ -172,7 +172,8 @@ void UIGrid::render(sf::Vector2f)
// render to window
renderTexture.display();
Resources::game->getWindow().draw(output);
//Resources::game->getWindow().draw(output);
target.draw(output);
}
@ -423,22 +424,6 @@ PyObject* UIGrid::get_children(PyUIGridObject* self, void* closure)
PyObject* UIGrid::repr(PyUIGridObject* self)
{
// if (member_ptr == 0) // x
// self->data->box.setPosition(val, self->data->box.getPosition().y);
// else if (member_ptr == 1) // y
// self->data->box.setPosition(self->data->box.getPosition().x, val);
// else if (member_ptr == 2) // w
// self->data->box.setSize(sf::Vector2f(val, self->data->box.getSize().y));
// else if (member_ptr == 3) // h
// self->data->box.setSize(sf::Vector2f(self->data->box.getSize().x, val));
// else if (member_ptr == 4) // center_x
// self->data->center_x = val;
// else if (member_ptr == 5) // center_y
// self->data->center_y = val;
// else if (member_ptr == 6) // zoom
// self->data->zoom = val;
std::ostringstream ss;
if (!self->data) ss << "<Grid (invalid internal object)>";
else {

View File

@ -26,7 +26,7 @@ public:
//UIGrid(int, int, IndexTexture*, float, float, float, float);
UIGrid(int, int, std::shared_ptr<PyTexture>, sf::Vector2f, sf::Vector2f);
void update();
void render(sf::Vector2f) override final;
void render(sf::Vector2f, sf::RenderTarget&) override final;
UIGridPoint& at(int, int);
PyObjectsEnum derived_type() override final;
//void setSprite(int);

View File

@ -18,14 +18,16 @@ UISprite::UISprite(std::shared_ptr<PyTexture> _ptex, int _sprite_index, sf::Vect
sprite = ptex->sprite(sprite_index, _pos, sf::Vector2f(_scale, _scale));
}
/*
void UISprite::render(sf::Vector2f offset)
{
sprite.move(offset);
Resources::game->getWindow().draw(sprite);
sprite.move(-offset);
}
*/
void UISprite::render(sf::Vector2f offset, sf::RenderTexture& target)
void UISprite::render(sf::Vector2f offset, sf::RenderTarget& target)
{
sprite.move(offset);
target.draw(sprite);

View File

@ -25,10 +25,10 @@ public:
UISprite();
UISprite(std::shared_ptr<PyTexture>, int, sf::Vector2f, float);
void update();
void render(sf::Vector2f) override final;
void render(sf::Vector2f, sf::RenderTarget&) override final;
virtual UIDrawable* click_at(sf::Vector2f point) override final;
void render(sf::Vector2f, sf::RenderTexture&);
//void render(sf::Vector2f, sf::RenderTexture&);
void setPosition(sf::Vector2f);
sf::Vector2f getPosition();

View File

@ -40,6 +40,7 @@ print("Lol, did it segfault?")
s = mcrfpy.Sprite(25, 384+19, texture, 86, 9.0)
# pos (LinkedVector / Vector): s.pos
# texture (Texture): s.texture
s.click = lambda *args, **kwargs: print("clicky", args, kwargs)
# Grid
g = mcrfpy.Grid(10, 10, texture, 512+25, 384+19, 462, 346)
@ -54,6 +55,43 @@ g.zoom = 2.0
[ui.append(d) for d in (f, c, s, g)]
# Entity
e = mcrfpy.Entity(5, 5, mcrfpy.default_texture, 86)
e.pos = e.draw_pos # TODO - sync draw/collision positions on init
g.entities.append(e)
import random
def wander(*args, **kwargs):
p = e.pos
new_p = (p[0] + random.randint(-1, 1), p[1] + random.randint(-1, 1))
if g.grid_size[0] >= new_p[0] >= 0 and g.grid_size[1] >= new_p[1] >= 0:
e.pos = new_p
#print(e.pos)
mcrfpy.setTimer("wander", wander, 400)
last_anim = None
def anim(t, *args, **kwargs):
global last_anim
if last_anim is None:
last_anim = t
return
duration = t - last_anim
entity_speed = 1 / 250 # 250 milliseconds to move one square
if e.pos == e.draw_pos:
return
tx, ty = e.pos #"target" position - entity is already occupying that spot, animate them moving there.
dx, dy = e.draw_pos #"draw" position
newx = tx if (abs(dx - tx) < entity_speed * duration) else entity_speed * duration
if tx < dx: newx *= -1
newy = ty if (abs(dy - ty) < entity_speed * duration) else entity_speed * duration
if ty < dy: newx *= -1
print(f"({dx}, {dy}) -> ({tx}, {ty}) = ({newx}, {newy}) ; @{entity_speed} * {duration} = {entity_speed * duration}")
e.draw_pos = (newx, newy)
mcrfpy.setTimer("anim", anim, 67)
print("built!")
# tests