and now we actually can render stuff, partially
[voxelands-alt.git] / src / graphics / render2d.c
blobfce4e76e1961b0e2880a8e7fd09733617db0a5f4
1 /************************************************************************
2 * render2d.c
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 ************************************************************************/
20 #include "common.h"
21 #include "graphics.h"
22 #include "list.h"
23 #include "array.h"
25 #include <string.h>
26 #include <ctype.h>
28 typedef struct render_section_s {
29 struct render_section_s *prev;
30 struct render_section_s *next;
31 int type;
32 rectf_t box;
33 material_t *mat;
34 } render_section_t;
36 render_section_t *render2d_stack = NULL;
37 render_section_t *current_2dsection = NULL;
39 static struct {
40 GLuint vao;
41 GLuint vbo;
42 shader_t *shader;
43 } render2d_data = {
46 NULL
49 static render_section_t *render2d_section_create()
51 render_section_t *r = malloc(sizeof(render_section_t));
52 r->type = RD2_NONE;
53 r->mat = NULL;
55 return r;
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);
64 }else{
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);
73 }else{
74 current_2dsection = current_2dsection->next;
77 if (m)
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;
114 /* render text */
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 */
127 void render2d()
129 int s = 0;
130 matrix_t m;
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);
145 /* shader */
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);
159 matrix_init(&m);
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;
168 s++;
171 glDisableVertexAttribArray(0);
172 glBindVertexArray(0);
174 /* shader */
175 shader_disable(render2d_data.shader);
177 current_2dsection = NULL;