aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Graphics/Drawable.cpp
blob: 675c54d5293dd906ac193e303d75bbf8dae87f0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "../modules.h"
#if LIBJIN_MODULES_RENDER

#include "drawable.h"
#include "../math/matrix.h"
#include <stdlib.h>

namespace jin
{
namespace graphics
{
    
    Drawable::Drawable(int w, int h)
        : texture(0)
        , size(w, h)
        , anchor(0, 0)
    {
    }

    Drawable::~Drawable()
    {
        glDeleteTextures(1, &texture);
    }

    void Drawable::setAnchor(int x, int y)
    {
        anchor.x = x; 
        anchor.y = y;
    }

    void Drawable::draw(int x, int y, float sx, float sy, float r)
    {
        /* Must set textCoord and vertCoord before renderring */
        if (! textCoord||! vertCoord) return;

        static jin::math::Matrix t;
        t.setTransformation(x, y, r, sx, sy, anchor.x, anchor.y);

        glBindTexture(GL_TEXTURE_2D, texture);
        
        /* push modle matrix */
        glPushMatrix();
        glMultMatrixf((const GLfloat*)t.getElements());

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glTexCoordPointer(2, GL_FLOAT, 0, textCoord);
        glVertexPointer(2, GL_FLOAT, 0, vertCoord);
        glDrawArrays(GL_QUADS, 0, 4);
        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);

        /* pop the model matrix */
        glPopMatrix();

        /* bind texture to default screen */
        glBindTexture(GL_TEXTURE_2D, 0);
    }

} // render
} // jin

#endif // LIBJIN_MODULES_RENDER