aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Graphics/je_graphic.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-10-22 21:30:21 +0800
committerchai <chaifix@163.com>2018-10-22 21:30:21 +0800
commit58d09aa3ca4409e2a15473b0ac3c0446f0acb1a2 (patch)
tree37dadab5575116c75b54064556b045ba8a245871 /src/libjin/Graphics/je_graphic.cpp
parent99bea1e47c813016d89c9e99296fa66e183d808f (diff)
*misc
Diffstat (limited to 'src/libjin/Graphics/je_graphic.cpp')
-rw-r--r--src/libjin/Graphics/je_graphic.cpp117
1 files changed, 117 insertions, 0 deletions
diff --git a/src/libjin/Graphics/je_graphic.cpp b/src/libjin/Graphics/je_graphic.cpp
new file mode 100644
index 0000000..831e409
--- /dev/null
+++ b/src/libjin/Graphics/je_graphic.cpp
@@ -0,0 +1,117 @@
+#include "../core/je_configuration.h"
+#if defined(jin_graphics)
+
+#include <stdlib.h>
+
+#include "../math/je_matrix.h"
+
+#include "shader/je_shader.h"
+#include "je_graphic.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+
+ Graphic::Graphic(int w, int h)
+ : mTexture(0)
+ , mSize(w, h)
+ {
+ mVertexCoords[0] = 0; mVertexCoords[1] = 0;
+ mVertexCoords[2] = 0; mVertexCoords[3] = h;
+ mVertexCoords[4] = w; mVertexCoords[5] = h;
+ mVertexCoords[6] = w; mVertexCoords[7] = 0;
+
+ mTextureCoords[0] = 0; mTextureCoords[1] = 0;
+ mTextureCoords[2] = 0; mTextureCoords[3] = 1;
+ mTextureCoords[4] = 1; mTextureCoords[5] = 1;
+ mTextureCoords[6] = 1; mTextureCoords[7] = 0;
+ }
+
+ Graphic::Graphic(const Bitmap* bitmap)
+ : mTexture(0)
+ {
+ uint32 w = mSize.w = bitmap->getWidth();
+ uint32 h = mSize.h = bitmap->getHeight();
+
+ mVertexCoords[0] = 0; mVertexCoords[1] = 0;
+ mVertexCoords[2] = 0; mVertexCoords[3] = h;
+ mVertexCoords[4] = w; mVertexCoords[5] = h;
+ mVertexCoords[6] = w; mVertexCoords[7] = 0;
+
+ mTextureCoords[0] = 0; mTextureCoords[1] = 0;
+ mTextureCoords[2] = 0; mTextureCoords[3] = 1;
+ mTextureCoords[4] = 1; mTextureCoords[5] = 1;
+ mTextureCoords[6] = 1; mTextureCoords[7] = 0;
+
+ const Color* pixels = bitmap->getPixels();
+
+ mTexture = gl.genTexture();
+ gl.bindTexture(mTexture);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ gl.texImage(GL_RGBA8, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
+ gl.bindTexture(0);
+ }
+
+ Graphic::~Graphic()
+ {
+ glDeleteTextures(1, &mTexture);
+ }
+
+ void Graphic::draw(int x, int y, float sx, float sy, float r, float ox, float oy)
+ {
+ gl.ModelMatrix.setTransformation(x, y, r, sx, sy, ox, oy);
+
+ Shader* shader = Shader::getCurrentShader();
+ shader->sendMatrix4(SHADER_MODEL_MATRIX, &gl.ModelMatrix);
+ shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.ProjectionMatrix);
+ shader->bindVertexPointer(2, GL_FLOAT, 0, mVertexCoords);
+ shader->bindUVPointer(2, GL_FLOAT, 0, mTextureCoords);
+
+ gl.bindTexture(mTexture);
+ gl.drawArrays(GL_QUADS, 0, 4);
+ gl.bindTexture(0);
+ }
+
+ void Graphic::draw(const Math::Quad& slice, int x, int y, float sx, float sy, float r, float ax, float ay)
+ {
+ float vertCoords[8] = {
+ 0, 0,
+ 0, slice.h,
+ slice.w, slice.h,
+ slice.w, 0
+ };
+ float slx = slice.x / mSize.w;
+ float sly = slice.y / mSize.h;
+ float slw = slice.w / mSize.w;
+ float slh = slice.h / mSize.h;
+ float texCoords[8] = {
+ slx, sly,
+ slx, sly + slh,
+ slx + slw, sly + slh,
+ slx + slw, sly
+ };
+
+ gl.ModelMatrix.setTransformation(x, y, r, sx, sy, ax, ay);
+
+ Shader* shader = Shader::getCurrentShader();
+ shader->sendMatrix4(SHADER_MODEL_MATRIX, &gl.ModelMatrix);
+ shader->sendMatrix4(SHADER_PROJECTION_MATRIX, &gl.ProjectionMatrix);
+ shader->bindVertexPointer(2, GL_FLOAT, 0, vertCoords);
+ shader->bindUVPointer(2, GL_FLOAT, 0, texCoords);
+
+ gl.bindTexture(mTexture);
+ gl.drawArrays(GL_QUADS, 0, 4);
+ gl.bindTexture(0);
+ }
+
+ //void Graphic::setFilter(GLint min, GLint max)
+ //{
+ // glTexParameteri(GL_)
+ //}
+
+ } // namespace Graphics
+} // namespace JinEngine
+
+#endif // defined(jin_graphics) \ No newline at end of file