1 /************************************************************************
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 ************************************************************************/
28 typedef struct render_section_s
{
29 struct render_section_s
*prev
;
30 struct render_section_s
*next
;
36 render_section_t
*render2d_stack
= NULL
;
37 render_section_t
*current_2dsection
= NULL
;
49 static render_section_t
*render2d_section_create()
51 render_section_t
*r
= malloc(sizeof(render_section_t
));
58 static void render2d_get_section(material_t
*m
, int t
)
60 if (!current_2dsection
) {
61 if (!render2d_stack
) {
62 current_2dsection
= render2d_section_create();
63 render2d_stack
= list_push(&render2d_stack
,current_2dsection
);
65 current_2dsection
= render2d_stack
;
69 if (current_2dsection
->mat
) {
70 if (!current_2dsection
->next
) {
71 current_2dsection
= render2d_section_create();
72 render2d_stack
= list_push(&render2d_stack
,current_2dsection
);
74 current_2dsection
= current_2dsection
->next
;
78 current_2dsection
->mat
= m
;
80 current_2dsection
->type
= t
;
83 /* render a 2d line */
84 void render2d_line(material_t
*m
, float x
, float y
, float ex
, float ey
)
87 render2d_get_section(m,RD2_LINE);
89 current_2dsection->box.x = x;
90 current_2dsection->box.y = y;
91 current_2dsection->box.w = ex;
92 current_2dsection->box.h = ey;
96 /* render a 2d quad of material */
97 void render2d_quad_mat(material_t
*m
, float x
, float y
, float w
, float h
)
99 render2d_get_section(m
,RD2_QUAD
);
101 y
= wm_data
.size
.height
-y
;
103 w
/= (float)wm_data
.size
.width
;
104 h
/= (float)wm_data
.size
.height
;
105 x
= (((x
/(float)wm_data
.size
.width
)*2.0)-1.0)+w
;
106 y
= (((y
/(float)wm_data
.size
.height
)*2.0)-1.0)-h
;
108 current_2dsection
->box
.x
= x
;
109 current_2dsection
->box
.y
= y
;
110 current_2dsection
->box
.w
= w
;
111 current_2dsection
->box
.h
= h
;
115 void render2d_text(float x
, float y
, int font
, int size
, char* str
)
118 render2d_get_section(NULL,RD2_TEXT);
120 strcpy(current_2dsection->txt,str);
121 render2d_vertex_push(x,y);
122 render2d_vertex_push(font,size);
126 /* render 2d graphics to the frame */
132 current_2dsection
= render2d_stack
;
134 if (render2d_data
.vao
== 0) {
135 GLfloat vertices
[8] = {-1.0,1.0,-1.0,-1.0,1.0,1.0,1.0,-1.0};
136 glGenVertexArrays(1,&render2d_data
.vao
);
137 glBindVertexArray(render2d_data
.vao
);
138 glGenBuffers(1, &render2d_data
.vbo
);
139 glBindBuffer(GL_ARRAY_BUFFER
, render2d_data
.vbo
);
140 glBufferData(GL_ARRAY_BUFFER
, 32, vertices
, GL_STATIC_DRAW
);
141 glEnableVertexAttribArray(0);
142 glVertexAttribPointer(0,2,GL_FLOAT
,GL_FALSE
,0,0);
146 if (!render2d_data
.shader
) {
147 render2d_data
.shader
= shader_create("ui");
148 shader_attribute(render2d_data
.shader
,0,"position");
151 shader_enable(render2d_data
.shader
);
153 glBindVertexArray(render2d_data
.vao
);
154 glEnableVertexAttribArray(0);
156 while (current_2dsection
&& current_2dsection
->mat
) {
157 mat_use(current_2dsection
->mat
);
160 matrix_scale(&m
,current_2dsection
->box
.w
,current_2dsection
->box
.h
,1.0);
161 matrix_translate(&m
,current_2dsection
->box
.x
,current_2dsection
->box
.y
,1.0);
162 shader_uniform_matrix(render2d_data
.shader
,"transformationMatrix",&m
);
164 glDrawArrays(GL_TRIANGLE_STRIP
,0,4);
166 current_2dsection
->mat
= NULL
;
167 current_2dsection
= current_2dsection
->next
;
171 glDisableVertexAttribArray(0);
172 glBindVertexArray(0);
175 shader_disable(render2d_data
.shader
);
177 current_2dsection
= NULL
;