summaryrefslogtreecommitdiff
path: root/hacks/gl/drawable.cc
blob: ba790fe91c8b0fa7081c1114f7bc9fc3daba13dc (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "drawable.h"
#include <iostream>

#ifdef __WXMAC__
#include "OpenGL/gl.h"
#else
#include <GL/gl.h>
#endif

#include "wx/wx.h"

/*
 * This is a simple class built on top of OpenGL that manages drawing images in a higher-level and quicker way.
 */

DrawableThing::DrawableThing(Image* image_arg)
{

    x=0;
    y=0;
    hotspotX=0;
    hotspotY=0;
    angle=0;

    xscale=1;
    yscale=1;

    xflip=false;
    yflip=false;

    if(image_arg!=NULL) setImage(image_arg);
    else image=NULL;
}

void DrawableThing::setFlip(bool x, bool y)
{
    xflip=x;
    yflip=y;
}

void DrawableThing::setHotspot(int x, int y)
{
    hotspotX=x;
    hotspotY=y;
}

void DrawableThing::move(int x, int y)
{
    DrawableThing::x=x;
    DrawableThing::y=y;
}

void DrawableThing::scale(float x, float y)
{
    DrawableThing::xscale=x;
    DrawableThing::yscale=y;
}

void DrawableThing::scale(float k)
{
    DrawableThing::xscale=k;
    DrawableThing::yscale=k;
}

void DrawableThing::setImage(Image* image)
{
    DrawableThing::image=image;
}

void DrawableThing::rotate(int angle)
{
    DrawableThing::angle=angle;
}

void DrawableThing::render()
{
    assert(image!=NULL);

    glLoadIdentity();

    glTranslatef(x,y,0);

    if(xscale!=1 || yscale!=1)
	{
        glScalef(xscale, yscale, 1);
    }

    if(angle!=0)
	{
        glRotatef(angle, 0,0,1);
    }

    glBindTexture(GL_TEXTURE_2D, image->getID()[0] );

    glBegin(GL_QUADS);

    printf("%f %f %d %d\n", image->tex_coord_x, image->tex_coord_y, hotspotX, hotspotY);

    glTexCoord2f(xflip? image->tex_coord_x : 0, yflip? 0 : image->tex_coord_y);
    glVertex2f( -hotspotX, -hotspotY );

    glTexCoord2f(xflip? 0 : image->tex_coord_x, yflip? 0 : image->tex_coord_y);
    glVertex2f( image->width-hotspotX, -hotspotY );

    glTexCoord2f(xflip? 0 : image->tex_coord_x, yflip? image->tex_coord_y : 0);
    glVertex2f( image->width-hotspotX, image->height-hotspotY );

    glTexCoord2f(xflip? image->tex_coord_x : 0, yflip? image->tex_coord_y : 0);
    glVertex2f( -hotspotX, image->height-hotspotY );

    glEnd();

}