src/TexChunk.cpp in gosu-1.3.0 vs src/TexChunk.cpp in gosu-1.4.0

- old
+ new

@@ -1,59 +1,68 @@ #include "TexChunk.hpp" -#include "DrawOpQueue.hpp" -#include "Texture.hpp" #include <Gosu/Bitmap.hpp> #include <Gosu/Graphics.hpp> +#include "DrawOpQueue.hpp" +#include "Texture.hpp" #include <stdexcept> -using namespace std; - void Gosu::TexChunk::set_tex_info() { - double width = texture->width(), height = texture->height(); + double width = m_texture->width(), height = m_texture->height(); - info.tex_name = texture->tex_name(); - info.left = x / width; - info.top = y / height; - info.right = (x + w) / width; - info.bottom = (y + h) / height; + m_info.tex_name = m_texture->tex_name(); + m_info.left = m_x / width; + m_info.top = m_y / height; + m_info.right = (m_x + m_w) / width; + m_info.bottom = (m_y + m_h) / height; } -Gosu::TexChunk::TexChunk(shared_ptr<Texture> texture, int x, int y, int w, int h, int padding) -: texture(move(texture)), x(x), y(y), w(w), h(h), padding(padding) +Gosu::TexChunk::TexChunk(std::shared_ptr<Texture> texture, int x, int y, int w, int h, int padding) +: m_texture{move(texture)}, + m_x{x}, + m_y{y}, + m_w{w}, + m_h{h}, + m_padding{padding} { set_tex_info(); } Gosu::TexChunk::TexChunk(const TexChunk& parent, int x, int y, int w, int h) -: texture(parent.texture), x(parent.x + x), y(parent.y + y), w(w), h(h), padding(0) +: m_texture{parent.m_texture}, + m_x{parent.m_x + x}, + m_y{parent.m_y + y}, + m_w{w}, + m_h{h}, + m_padding{0} { - if (x < 0 || y < 0 || x + w > parent.w || y + h > parent.h) { - throw invalid_argument("subimage bounds exceed those of its parent"); + if (x < 0 || y < 0 || x + w > parent.m_w || y + h > parent.m_h) { + throw std::invalid_argument{"subimage bounds exceed those of its parent"}; } if (w <= 0 || h <= 0) { - throw invalid_argument("cannot create empty image"); + throw std::invalid_argument{"cannot create empty image"}; } - + set_tex_info(); - texture->block(this->x, this->y, this->w, this->h); + m_texture->block(m_x, m_y, m_w, m_h); } Gosu::TexChunk::~TexChunk() { - texture->free(x - padding, y - padding, w + 2 * padding, h + 2 * padding); + m_texture->free(m_x - m_padding, m_y - m_padding, m_w + 2 * m_padding, m_h + 2 * m_padding); } -void Gosu::TexChunk::draw(double x1, double y1, Color c1, double x2, double y2, Color c2, - double x3, double y3, Color c3, double x4, double y4, Color c4, ZPos z, BlendMode mode) const +void Gosu::TexChunk::draw(double x1, double y1, Color c1, double x2, double y2, Color c2, // + double x3, double y3, Color c3, double x4, double y4, Color c4, // + ZPos z, BlendMode mode) const { DrawOp op; - op.render_state.texture = texture; + op.render_state.texture = m_texture; op.render_state.mode = mode; - + normalize_coordinates(x1, y1, x2, y2, x3, y3, c3, x4, y4, c4); - + op.vertices_or_block_index = 4; op.vertices[0] = DrawOp::Vertex(x1, y1, c1); op.vertices[1] = DrawOp::Vertex(x2, y2, c2); // TODO: Should be harmonized #ifdef GOSU_IS_OPENGLES @@ -61,38 +70,38 @@ op.vertices[3] = DrawOp::Vertex(x4, y4, c4); #else op.vertices[3] = DrawOp::Vertex(x3, y3, c3); op.vertices[2] = DrawOp::Vertex(x4, y4, c4); #endif - op.left = info.left; - op.top = info.top; - op.right = info.right; - op.bottom = info.bottom; - + op.left = m_info.left; + op.top = m_info.top; + op.right = m_info.right; + op.bottom = m_info.bottom; + op.z = z; Graphics::schedule_draw_op(op); } -unique_ptr<Gosu::ImageData> Gosu::TexChunk::subimage(int x, int y, int width, int height) const +std::unique_ptr<Gosu::ImageData> Gosu::TexChunk::subimage(int x, int y, int width, int height) const { - return unique_ptr<Gosu::ImageData>(new TexChunk(*this, x, y, width, height)); + return std::unique_ptr<Gosu::ImageData>(new TexChunk(*this, x, y, width, height)); } Gosu::Bitmap Gosu::TexChunk::to_bitmap() const { - return texture->to_bitmap(x, y, w, h); + return m_texture->to_bitmap(m_x, m_y, m_w, m_h); } void Gosu::TexChunk::insert(const Bitmap& original_bitmap, int x, int y) { Bitmap clipped_bitmap; const Bitmap* bitmap = &original_bitmap; - + // If inserting the bitmap at the given position exceeds the boundaries of the space allocated // for this image on the texture, we need to clip the bitmap and insert the clipped version // instead. - if (x < 0 || y < 0 || x + original_bitmap.width() > w || y + original_bitmap.height() > h) { + if (x < 0 || y < 0 || x + original_bitmap.width() > m_w || y + original_bitmap.height() > m_h) { // How many pixels to remove at the top and left sides. int clip_left = 0, clip_top = 0; // How large the clipped version needs to be. int clipped_width = original_bitmap.width(), clipped_height = original_bitmap.height(); @@ -107,24 +116,24 @@ clip_top = -y; clipped_height -= -y; y = 0; } // Clip away pixels on the right side, if necessary. - if (x + clipped_width > w) { - clipped_width = (w - x); + if (x + clipped_width > m_w) { + clipped_width = (m_w - x); } // Clip away pixels on the bottom, if necessary. - if (y + clipped_height > h) { - clipped_height = (h - y); + if (y + clipped_height > m_h) { + clipped_height = (m_h - y); } if (clipped_width <= 0 || clipped_height <= 0) return; - + clipped_bitmap.resize(clipped_width, clipped_height); clipped_bitmap.insert(-clip_left, -clip_top, original_bitmap); bitmap = &clipped_bitmap; } - + glBindTexture(GL_TEXTURE_2D, tex_name()); - glTexSubImage2D(GL_TEXTURE_2D, 0, this->x + x, this->y + y, bitmap->width(), bitmap->height(), + glTexSubImage2D(GL_TEXTURE_2D, 0, m_x + x, m_y + y, bitmap->width(), bitmap->height(), Color::GL_FORMAT, GL_UNSIGNED_BYTE, bitmap->data()); }