diff --git a/Aquaria/ScriptInterface.cpp b/Aquaria/ScriptInterface.cpp index 6e66897..0cff86a 100644 --- a/Aquaria/ScriptInterface.cpp +++ b/Aquaria/ScriptInterface.cpp @@ -9636,6 +9636,20 @@ luaFunc(quadgrid_resetUV) luaReturnNil(); } +luaFunc(quadgrid_resetPos) +{ + QuadGrid *q = getQuadGrid(L); + if(q) + { + const float w = luaL_optnumber(L, 2, 1); + const float h = luaL_optnumber(L, 3, 1); + const float xoffs = luaL_optnumber(L, 4, 0); + const float yoffs = luaL_optnumber(L, 5, 0); + q->resetPos(w, h, xoffs, yoffs); + } + luaReturnNil(); +} + // ---------- Minimap related ------------------ luaFunc(getMinimapRender) @@ -10888,6 +10902,7 @@ static const struct { luaRegister(quadgrid_setPauseLevel), luaRegister(quadgrid_getPauseLevel), luaRegister(quadgrid_resetUV), + luaRegister(quadgrid_resetPos), #undef MK_FUNC #undef MK_ALIAS diff --git a/BBGE/QuadGrid.cpp b/BBGE/QuadGrid.cpp index 593992a..ae3bf15 100644 --- a/BBGE/QuadGrid.cpp +++ b/BBGE/QuadGrid.cpp @@ -9,6 +9,9 @@ QuadGrid::QuadGrid(size_t w, size_t h) addType(SCO_QUAD_GRID); _points.resize((w+1) * (h+1)); resetUV(); + resetPos(1, 1); + this->width = 2; + this->height = 2; } QuadGrid* QuadGrid::New(size_t w, size_t h) @@ -39,6 +42,26 @@ void QuadGrid::resetUV(float xmul, float ymul) } } +void QuadGrid::resetPos(float w, float h, float xoffs, float yoffs) +{ + const float dx = 1.0f / w; + const float dy = 1.0f / h; + const size_t NX = _w + 1; + + float yy = yoffs; + // go over points, so <= to compare boundaries + for(size_t y = 0; y <= _h; ++y, yy += dy) + { + Point * const row = &_points[y * NX]; + float xx = xoffs; + for(size_t x = 0; x < NX; ++x, xx += dx) + { + row[x].x = xx; + row[y].y = yy; + } + } +} + static inline void drawOnePoint(const QuadGrid::Point& p, float ox, float oy) { glTexCoord2f(p.u + ox, p.v + oy); @@ -48,12 +71,10 @@ static inline void drawOnePoint(const QuadGrid::Point& p, float ox, float oy) void QuadGrid::onRender() { - glBindTexture(GL_TEXTURE_2D, texture->textures[0]); glColor4f(color.x, color.y, color.z, alpha.x * alphaMod); const float ox = texOffset.x; const float oy = texOffset.y; - const size_t NX = _w + 1; // go over grids, so < to compare boundaries @@ -83,3 +104,16 @@ void QuadGrid::onUpdate(float dt) RenderObject::onUpdate(dt); } +void QuadGrid::onSetTexture() // same as Quad::setTexture() +{ + if (texture) + { + width = this->texture->width; + height = this->texture->height; + } + else + { + width = 64; + height = 64; + } +} diff --git a/BBGE/QuadGrid.h b/BBGE/QuadGrid.h index 0de72ce..eaffff3 100644 --- a/BBGE/QuadGrid.h +++ b/BBGE/QuadGrid.h @@ -44,9 +44,10 @@ public: virtual void onRender(); virtual void onUpdate(float dt); + virtual void onSetTexture(); void resetUV(float xmul = 1, float ymul = 1); - void resetPos(float xmul = 1, float ymul = 1); + void resetPos(float w, float h, float xoffs = 0, float yoffs = 0); inline size_t quadsX() const { return _w; } inline size_t quadsY() const { return _h; } @@ -62,7 +63,7 @@ public: private: QuadGrid(size_t w, size_t h); - const size_t _w, _h; + const size_t _w, _h; // number of quads in each direction (2x3 quads => 3x4 grid points) std::vector _points; };