1 module meatbox.imagebox;
2 
3 import meatbox.box;
4 import meatbox.colour;
5 import meatbox.image;
6 
7 //Don't need all these. Remove some?
8 import derelict.opengl3.gl;
9 import derelict.opengl3.gl3;
10 import derelict.sdl2.sdl;
11 import derelict.sdl2.image;
12 
13 static this()
14 { 
15 	DerelictSDL2Image.load();
16 	IMG_Init( IMG_INIT_PNG );
17 }
18 static ~this()
19 	{ IMG_Quit(); }
20 
21 ///
22 class Imagebox: Box
23 {
24 public:
25 	Image image;
26 	this()
27 	{ 
28 		this.colour =Colour.white;
29 	}
30 	///
31 	this( string path )
32 	{
33 		this( new Image(path) );		
34 	}
35 	///
36 	this( Image image )
37 	{
38 		this();
39 		this.image = image;
40 		this.x =0f; this.y =0f; //Floats init to NaN; Use proper constructors.
41 		this.width =1f;
42 		this.height =1f/ image.aspect;
43 	}
44 	///
45 	override void render()
46 	{
47 		//glColor3ub( colour.red, colour.green, colour.blue );
48 		glBindTexture( GL_TEXTURE_2D, image.buffer );
49 		
50 		glPushMatrix();
51 		glTranslatef( x, y, 0);
52 		glScalef( width, height, 1f );
53 		glDrawArrays( GL_QUADS, 0, 4);
54 		glPopMatrix();	
55 	}
56 	static void render( Image image, float x, float y )
57 	{
58 		//glColor3ub( colour.red, colour.green, colour.blue );
59 		glBindTexture( GL_TEXTURE_2D, image.buffer );
60 		
61 		glPushMatrix();
62 		glTranslatef( x, y, 0);
63 		glScalef( 1f, image.aspect, 1f );
64 		glDrawArrays( GL_QUADS, 0, 4);
65 		glPopMatrix();	
66 	}
67 	static this()
68 	{
69 		//Preload box vectors into GPU memory.
70 		//Stick into base Box class?
71 	}
72 	///
73 	static void startRender()
74 	{
75 		glEnableClientState( GL_VERTEX_ARRAY );
76 		glEnableClientState( GL_TEXTURE_COORD_ARRAY );
77 		glEnable( GL_TEXTURE_2D );
78 		glEnable( GL_BLEND );
79 		glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );	
80 		glColor4f( 1f, 1f, 1f, 1f );
81 		glTexCoordPointer( 2, GL_FLOAT, 0, Box.vertices.ptr );
82 		glVertexPointer( 2, GL_FLOAT, 0, Box.vertices.ptr );
83 	}
84 	///
85 	static void endRender()
86 	{
87 		glDisable( GL_BLEND);
88 		glDisable( GL_TEXTURE_2D);
89 		glDisableClientState( GL_TEXTURE_COORD_ARRAY);
90 		glDisableClientState( GL_VERTEX_ARRAY);
91 	}
92 	static immutable float[8] vertices =
93 	[ 
94 		0f, 1f,
95 		1f, 1f,
96 		1f, 0f,
97 		0f, 0f
98 	];
99 }