Missing library from test link list.
[dcpomatic.git] / hacks / gl / drawable.cc
1 #include "drawable.h"
2 #include <iostream>
3
4 #ifdef __WXMAC__
5 #include "OpenGL/gl.h"
6 #else
7 #include <GL/gl.h>
8 #endif
9
10 #include "wx/wx.h"
11
12 /*
13  * This is a simple class built on top of OpenGL that manages drawing images in a higher-level and quicker way.
14  */
15
16 DrawableThing::DrawableThing(Image* image_arg)
17 {
18
19     x=0;
20     y=0;
21     hotspotX=0;
22     hotspotY=0;
23     angle=0;
24
25     xscale=1;
26     yscale=1;
27
28     xflip=false;
29     yflip=false;
30
31     if(image_arg!=NULL) setImage(image_arg);
32     else image=NULL;
33 }
34
35 void DrawableThing::setFlip(bool x, bool y)
36 {
37     xflip=x;
38     yflip=y;
39 }
40
41 void DrawableThing::setHotspot(int x, int y)
42 {
43     hotspotX=x;
44     hotspotY=y;
45 }
46
47 void DrawableThing::move(int x, int y)
48 {
49     DrawableThing::x=x;
50     DrawableThing::y=y;
51 }
52
53 void DrawableThing::scale(float x, float y)
54 {
55     DrawableThing::xscale=x;
56     DrawableThing::yscale=y;
57 }
58
59 void DrawableThing::scale(float k)
60 {
61     DrawableThing::xscale=k;
62     DrawableThing::yscale=k;
63 }
64
65 void DrawableThing::setImage(Image* image)
66 {
67     DrawableThing::image=image;
68 }
69
70 void DrawableThing::rotate(int angle)
71 {
72     DrawableThing::angle=angle;
73 }
74
75 void DrawableThing::render()
76 {
77     assert(image!=NULL);
78
79     glLoadIdentity();
80
81     glTranslatef(x,y,0);
82
83     if(xscale!=1 || yscale!=1)
84         {
85         glScalef(xscale, yscale, 1);
86     }
87
88     if(angle!=0)
89         {
90         glRotatef(angle, 0,0,1);
91     }
92
93     glBindTexture(GL_TEXTURE_2D, image->getID()[0] );
94
95     glBegin(GL_QUADS);
96
97     printf("%f %f %d %d\n", image->tex_coord_x, image->tex_coord_y, hotspotX, hotspotY);
98
99     glTexCoord2f(xflip? image->tex_coord_x : 0, yflip? 0 : image->tex_coord_y);
100     glVertex2f( -hotspotX, -hotspotY );
101
102     glTexCoord2f(xflip? 0 : image->tex_coord_x, yflip? 0 : image->tex_coord_y);
103     glVertex2f( image->width-hotspotX, -hotspotY );
104
105     glTexCoord2f(xflip? 0 : image->tex_coord_x, yflip? image->tex_coord_y : 0);
106     glVertex2f( image->width-hotspotX, image->height-hotspotY );
107
108     glTexCoord2f(xflip? image->tex_coord_x : 0, yflip? image->tex_coord_y : 0);
109     glVertex2f( -hotspotX, image->height-hotspotY );
110
111     glEnd();
112
113 }