zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
modern.h
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2  * Copyleft (C) 2013 Mitchell Keith Bloch (bazald).
3  *
4  * This source file is simply under the public domain.
5  */
6 
7 #include <zenilib.h>
8 
9 #include <memory>
10 #ifndef DISABLE_DX9
11 #include <d3dx9.h>
12 #endif
13 
14 #if defined(_DEBUG) && defined(_WINDOWS)
15 #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
16 #define new DEBUG_NEW
17 #endif
18 
19 using namespace std;
20 using namespace Zeni;
21 
22 GLfloat vertices[12] = { 0.0f, 0.0f,
23  0.0f, 300.0f,
24  400.0f, 0.0f};
25 GLuint colors[3] = {0xFF0000FF,
26  0xFF00FF00,
27  0xFFFF0000};
28 GLushort indices[3] = {0, 1, 2};
29 
30 class Modern_State : public Gamestate_Base {
31  Modern_State(const Modern_State &);
32  Modern_State operator=(const Modern_State &);
33 
34 public:
36  : m_vertex_shader(get_Video().create_Vertex_Shader("shaders/shader.vert")),
37  m_fragment_shader(get_Video().create_Fragment_Shader("shaders/shader.frag")),
38  m_program(get_Video().create_Program()),
39  a_position(0),
40  a_color(0)
41 #ifndef DISABLE_DX9
42  ,
43  vertexDecl(0),
44  vertexBuffer(0),
45  colorBuffer(0),
46  indexBuffer(0)
47 #endif
48  {
49  set_pausable(true);
50 
51  m_program->attach(*m_vertex_shader);
52  m_program->attach(*m_fragment_shader);
53  m_program->link();
54 
55  memset(buffers, 0, sizeof(buffers));
56 
57  if(Program_GL_Fixed * program = dynamic_cast<Program_GL_Fixed *>(m_program.get())) {
59 
62  a_position = glGetAttribLocationARB(program->get(), "a_position");
63 
66  a_color = glGetAttribLocationARB(program->get(), "a_color");
67 
70 
73  }
74  else if(Program_GL_Shader * program = dynamic_cast<Program_GL_Shader *>(m_program.get())) {
76 
79  a_position = glGetAttribLocation(program->get(), "a_position");
80 
83  a_color = glGetAttribLocation(program->get(), "a_color");
84 
87 
90  }
91 #ifndef DISABLE_DX9
92  else if(Program_DX9 * program = dynamic_cast<Program_DX9 *>(m_program.get())) {
93  Video_DX9 &vdx = dynamic_cast<Video_DX9 &>(get_Video());
94 
97  D3DDECL_END()};
98  vdx.get_d3d_device()->CreateVertexDeclaration(decl, &vertexDecl);
99 
100  void * data;
101  vdx.get_d3d_device()->CreateVertexBuffer(sizeof(vertices), D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &vertexBuffer, 0);
102  if(FAILED(vertexBuffer->Lock(0, 0, &data, 0)))
103  throw Error("Lock failed.");
104  memcpy(data, vertices, sizeof(vertices));
105  vertexBuffer->Unlock();
106 
107  vdx.get_d3d_device()->CreateVertexBuffer(sizeof(colors), D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &colorBuffer, 0);
108  if(FAILED(colorBuffer->Lock(0, 0, &data, 0)))
109  throw Error("Lock failed.");
110  memcpy(data, colors, sizeof(colors));
111  colorBuffer->Unlock();
112 
113  vdx.get_d3d_device()->CreateIndexBuffer(sizeof(indices), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &indexBuffer, 0);
114  if(FAILED(indexBuffer->Lock(0, 0, &data, 0)))
115  throw Error("Lock failed.");
116  memcpy(data, indices, sizeof(indices));
117  indexBuffer->Unlock();
118  }
119 #endif
120  }
121 
123 #ifndef DISABLE_DX9
124  if(dynamic_cast<Video_DX9 *>(&get_Video())) {
125  if(indexBuffer)
126  indexBuffer->Release();
127  if(colorBuffer)
128  colorBuffer->Release();
129  if(vertexBuffer)
130  vertexBuffer->Release();
131  if(vertexDecl)
132  vertexDecl->Release();
133  }
134  else
135 #endif
137  }
138 
139 private:
140  void render() {
141  Video &vr = get_Video();
142  vr.set_backface_culling(false);
143 
144  const Matrix4f modelViewMatrix = Matrix4f::Orthographic(0.0f, 800.0f, 600.0f, 0.0f, -1.0f, 1.0f);
145  const bool use_shaders = get_Game().get_key_state(SDLK_SPACE);
146 
147  if(use_shaders) {
148  vr.set_program(*m_program);
149 
150  if(Program_GL_Fixed * program = dynamic_cast<Program_GL_Fixed *>(m_program.get())) {
151  GLuint modelViewProj = glGetUniformLocationARB(program->get(), "modelViewProj");
152  glUniformMatrix4fvARB(modelViewProj, 1, GL_FALSE, reinterpret_cast<const GLfloat *>(&modelViewMatrix));
153 
155  glVertexAttribPointerARB(a_position, 2, GL_FLOAT, GL_FALSE, 0, 0);
156  glEnableVertexAttribArrayARB(a_position);
157 
161 
164 
165  glDisableVertexAttribArrayARB(a_position);
169  }
170  else if(Program_GL_Shader * program = dynamic_cast<Program_GL_Shader *>(m_program.get())) {
171  GLuint modelViewProj = glGetUniformLocation(program->get(), "modelViewProj");
172  glUniformMatrix4fv(modelViewProj, 1, GL_FALSE, reinterpret_cast<const GLfloat *>(&modelViewMatrix));
173 
175  glVertexAttribPointer(a_position, 2, GL_FLOAT, GL_FALSE, 0, 0);
176  glEnableVertexAttribArray(a_position);
177 
179  glVertexAttribPointer(a_color, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
180  glEnableVertexAttribArray(a_color);
181 
184 
185  glDisableVertexAttribArray(a_position);
189  }
190 #ifndef DISABLE_DX9
191  else if(Program_DX9 * program = dynamic_cast<Program_DX9 *>(m_program.get())) {
192  Video_DX9 &vdx = dynamic_cast<Video_DX9 &>(get_Video());
193  Shader_DX9 &vsdx = dynamic_cast<Shader_DX9 &>(*m_vertex_shader.get());
194 
195  if(FAILED(vsdx.get_constant_table()->SetMatrix(vdx.get_d3d_device(), "$_modelViewProj", reinterpret_cast<const D3DXMATRIX *>(&modelViewMatrix))))
196  throw Error("Setting modelViewProj failed.");
197  }
198 #endif
199  }
200  else {
201  vr.set_view_matrix(modelViewMatrix);
202  vr.set_projection_matrix(Matrix4f::Identity());
203  }
204 
205 #ifndef DISABLE_DX9
206  if(Program_DX9 * program = dynamic_cast<Program_DX9 *>(m_program.get())) {
207  Video_DX9 &vdx = dynamic_cast<Video_DX9 &>(get_Video());
208 
209  vdx.get_d3d_device()->SetVertexDeclaration(vertexDecl);
210  vdx.get_d3d_device()->SetStreamSource(0, vertexBuffer, 0, 2 * sizeof(float));
211  vdx.get_d3d_device()->SetStreamSource(1, colorBuffer, 0, 4 * sizeof(unsigned char));
212  vdx.get_d3d_device()->SetIndices(indexBuffer);
213 
214  vdx.get_d3d_device()->DrawIndexedPrimitive(D3DPT_TRIANGLESTRIP, 0, 0, 3, 0, 1);
215 
216  vdx.get_d3d_device()->SetIndices(0);
217  vdx.get_d3d_device()->SetStreamSource(0, 0, 0, 0);
218  vdx.get_d3d_device()->SetStreamSource(1, 0, 0, 0);
219  vdx.get_d3d_device()->SetVertexDeclaration(0);
220  }
221  else
222 #endif
223  if(!use_shaders) {
228 
230 
233  }
234 
235  if(use_shaders)
236  vr.unset_program();
237  }
238 
239  auto_ptr<Shader> m_vertex_shader;
240  auto_ptr<Shader> m_fragment_shader;
241  auto_ptr<Program> m_program;
242  GLuint a_position;
243  GLuint a_color;
244 
245  GLuint buffers[3];
246 #ifndef DISABLE_DX9
247  LPDIRECT3DVERTEXDECLARATION9 vertexDecl;
248  LPDIRECT3DVERTEXBUFFER9 vertexBuffer;
249  LPDIRECT3DVERTEXBUFFER9 colorBuffer;
250  LPDIRECT3DINDEXBUFFER9 indexBuffer;
251 #endif
252 };
#define GL_TRUE
Definition: gl2.h:51
#define glGenBuffersARB
Definition: glew.h:6914
#define GL_STATIC_DRAW_ARB
Definition: glew.h:6888
unsigned short GLushort
Definition: gl2.h:31
LPD3DXCONSTANTTABLE get_constant_table() const
Definition: Shader.hxx:53
The Direct3D9 Rendering System.
Definition: Video_DX9.h:62
#define GL_FALSE
Definition: gl2.h:50
LPDIRECT3DDEVICE9 & get_d3d_device()
See DirectX Documentation for details.
Definition: Video_DX9.hxx:48
#define GL_VERTEX_ARRAY
Definition: glew_head.h:723
~Modern_State()
Definition: modern.h:122
The Video Rendering Singleton.
Definition: Video.h:71
#define D3DUSAGE_WRITEONLY
Definition: d3d9types.h:1609
GLclampf f
Definition: glew.h:3390
virtual void set_view_matrix(const Matrix4f &view)=0
Set the view Matrix4f.
Definition: Video.cpp:226
bool get_key_state(const int &key) const
Get the state of a key.
Definition: Game.cpp:287
GL_APICALL void GL_APIENTRY glEnableVertexAttribArray(GLuint index)
#define GL_UNSIGNED_SHORT
Definition: gl2.h:238
GL_APICALL int GL_APIENTRY glGetAttribLocation(GLuint program, const GLchar *name)
static void render(const Vertex_Buffer_Macrorenderer &macrorenderer, std::vector< Vertex_Buffer::Vertex_Buffer_Range * > &descriptors)
#define memset
Definition: SDL_malloc.c:633
GL_APICALL int GL_APIENTRY glGetUniformLocation(GLuint program, const GLchar *name)
#define GL_TRIANGLE_STRIP
Definition: gl2.h:59
Game & get_Game()
Get access to the singleton.
Definition: Game.cpp:58
virtual void unset_program()=0
Disable a program.
Modern_State()
Definition: modern.h:35
#define GL_ARRAY_BUFFER
Definition: gl2.h:115
GL_APICALL void GL_APIENTRY glUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
#define GL_ELEMENT_ARRAY_BUFFER
Definition: gl2.h:116
#define GL_COLOR_ARRAY
Definition: glew_head.h:725
const GLuint * buffers
Definition: glew.h:1669
#define GL_ELEMENT_ARRAY_BUFFER_ARB
Definition: glew.h:6866
#define glGetAttribLocationARB
Definition: glew.h:7160
GL_APICALL void GL_APIENTRY glBindBuffer(GLenum target, GLuint buffer)
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl2ext.h:848
GL_APICALL void GL_APIENTRY glGenBuffers(GLsizei n, GLuint *buffers)
#define GL_STATIC_DRAW
Definition: gl2.h:121
struct IDirect3DVertexBuffer9 * LPDIRECT3DVERTEXBUFFER9
Definition: d3d9.h:1507
#define glVertexAttribPointerARB
Definition: glew.h:7135
#define glBindBufferARB
Definition: glew.h:6910
GLbitfield GLuint program
Definition: gl2ext.h:1203
khronos_float_t GLfloat
Definition: gl2.h:33
#define glUniformMatrix4fvARB
Definition: glew.h:5697
#define glDisableClientState
Definition: gl_mangle.h:390
GL_APICALL void GL_APIENTRY glVertexAttribPointer(GLuint indx, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *ptr)
#define D3DDECL_END()
Definition: d3d9types.h:774
#define glColorPointer
Definition: gl_mangle.h:236
virtual void set_projection_matrix(const Matrix4f &projection)=0
Set the projection Matrix4f.
Definition: Video.cpp:230
#define glBufferDataARB
Definition: glew.h:6911
unsigned int GLuint
Definition: gl2.h:32
#define memcpy
Definition: SDL_malloc.c:634
#define glEnableClientState
Definition: gl_mangle.h:438
The base class for all gamestates.
Definition: Gamestate.h:82
#define GL_UNSIGNED_BYTE
Definition: gl2.h:236
struct IDirect3DVertexDeclaration9 * LPDIRECT3DVERTEXDECLARATION9
Definition: d3d9.h:1001
virtual void set_backface_culling(const bool &on)=0
Set backface culling on/off.
Definition: Video.cpp:186
#define glDrawElements
Definition: gl_mangle.h:416
#define GL_FLOAT
Definition: gl2.h:241
Video & get_Video()
Get access to the singleton.
Definition: Video.cpp:149
#define glGetUniformLocationARB
Definition: glew.h:5674
A Featureful 4-Space Matrix Class.
Definition: Matrix4f.h:47
#define glVertexPointer
Definition: gl_mangle.h:2196
The Error Class.
Definition: Error.h:52
GL_APICALL void GL_APIENTRY glDisableVertexAttribArray(GLuint index)
virtual void set_program(Program &program)=0
Enable a program.
GL_APICALL void GL_APIENTRY glDeleteBuffers(GLsizei n, const GLuint *buffers)
GLfloat vertices[12]
Definition: modern.h:22
GL_APICALL void GL_APIENTRY glBufferData(GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage)
#define GL_ARRAY_BUFFER_ARB
Definition: glew.h:6865
GLuint colors[3]
Definition: modern.h:25
#define glEnableVertexAttribArrayARB
Definition: glew.h:7077
#define glDisableVertexAttribArrayARB
Definition: glew.h:7076
struct IDirect3DIndexBuffer9 * LPDIRECT3DINDEXBUFFER9
Definition: d3d9.h:1579
GLsizei GLenum const GLvoid * indices
Definition: gl2ext.h:1012