1 /*  RetroArch - A frontend for libretro.
2  *  Copyright (C) 2011-2017 - Daniel De Matteis
3  *  Copyright (C) 2016-2019 - Brad Parker
4  *
5  *  RetroArch is free software: you can redistribute it and/or modify it under the terms
6  *  of the GNU General Public License as published by the Free Software Found-
7  *  ation, either version 3 of the License, or (at your option) any later version.
8  *
9  *  RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10  *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11  *  PURPOSE.  See the GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License along with RetroArch.
14  *  If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #include <retro_miscellaneous.h>
18 
19 #ifdef HAVE_CONFIG_H
20 #include "../../config.h"
21 #endif
22 
23 #include "../../retroarch.h"
24 #include "../font_driver.h"
25 #include "../common/gl1_common.h"
26 
27 #include "../gfx_display.h"
28 
29 static const GLfloat gl1_menu_vertexes[] = {
30    0, 0,
31    1, 0,
32    0, 1,
33    1, 1
34 };
35 
36 static const GLfloat gl1_menu_tex_coords[] = {
37    0, 1,
38    1, 1,
39    0, 0,
40    1, 0
41 };
42 
gfx_display_gl1_get_default_vertices(void)43 static const float *gfx_display_gl1_get_default_vertices(void)
44 {
45    return &gl1_menu_vertexes[0];
46 }
47 
gfx_display_gl1_get_default_tex_coords(void)48 static const float *gfx_display_gl1_get_default_tex_coords(void)
49 {
50    return &gl1_menu_tex_coords[0];
51 }
52 
gfx_display_gl1_get_default_mvp(void * data)53 static void *gfx_display_gl1_get_default_mvp(void *data)
54 {
55    gl1_t *gl1 = (gl1_t*)data;
56 
57    if (!gl1)
58       return NULL;
59 
60    return &gl1->mvp_no_rot;
61 }
62 
gfx_display_prim_to_gl1_enum(enum gfx_display_prim_type type)63 static GLenum gfx_display_prim_to_gl1_enum(
64       enum gfx_display_prim_type type)
65 {
66    switch (type)
67    {
68       case GFX_DISPLAY_PRIM_TRIANGLESTRIP:
69          return GL_TRIANGLE_STRIP;
70       case GFX_DISPLAY_PRIM_TRIANGLES:
71          return GL_TRIANGLES;
72       case GFX_DISPLAY_PRIM_NONE:
73       default:
74          break;
75    }
76 
77    return 0;
78 }
79 
gfx_display_gl1_blend_begin(void * data)80 static void gfx_display_gl1_blend_begin(void *data)
81 {
82    glEnable(GL_BLEND);
83    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
84 }
85 
gfx_display_gl1_blend_end(void * data)86 static void gfx_display_gl1_blend_end(void *data)
87 {
88    glDisable(GL_BLEND);
89 }
90 
gfx_display_gl1_draw(gfx_display_ctx_draw_t * draw,void * data,unsigned video_width,unsigned video_height)91 static void gfx_display_gl1_draw(gfx_display_ctx_draw_t *draw,
92       void *data,
93       unsigned video_width,
94       unsigned video_height)
95 {
96    video_shader_ctx_mvp_t mvp;
97    gl1_t             *gl1          = (gl1_t*)data;
98 
99    if (!gl1 || !draw)
100       return;
101 
102    if (!draw->coords->vertex)
103       draw->coords->vertex         = &gl1_menu_vertexes[0];
104    if (!draw->coords->tex_coord)
105       draw->coords->tex_coord      = &gl1_menu_tex_coords[0];
106    if (!draw->coords->lut_tex_coord)
107       draw->coords->lut_tex_coord  = &gl1_menu_tex_coords[0];
108    if (!draw->texture)
109       return;
110 
111    glViewport(draw->x, draw->y, draw->width, draw->height);
112 
113    glEnable(GL_TEXTURE_2D);
114 
115    glBindTexture(GL_TEXTURE_2D, (GLuint)draw->texture);
116 
117    mvp.data   = gl1;
118    mvp.matrix = draw->matrix_data ? (math_matrix_4x4*)draw->matrix_data
119       : (math_matrix_4x4*)&gl1->mvp_no_rot;
120 
121    glMatrixMode(GL_PROJECTION);
122    glPushMatrix();
123    glLoadMatrixf((const GLfloat*)mvp.matrix);
124 
125    glMatrixMode(GL_MODELVIEW);
126    glPushMatrix();
127    glLoadIdentity();
128 
129    glEnableClientState(GL_COLOR_ARRAY);
130    glEnableClientState(GL_VERTEX_ARRAY);
131    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
132 
133 #ifdef VITA
134    {
135       unsigned i;
136       static float *vertices3 = NULL;
137 
138       if (vertices3)
139          free(vertices3);
140       vertices3 = (float*)malloc(sizeof(float) * 3 * draw->coords->vertices);
141       for (i = 0; i < draw->coords->vertices; i++)
142       {
143          memcpy(&vertices3[i*3], &draw->coords->vertex[i*2], sizeof(float) * 2);
144          vertices3[i*3+2]  = 0.0f;
145       }
146       glVertexPointer(3, GL_FLOAT, 0, vertices3);
147    }
148 #else
149    glVertexPointer(2, GL_FLOAT, 0, draw->coords->vertex);
150 #endif
151 
152    glColorPointer(4, GL_FLOAT, 0, draw->coords->color);
153    glTexCoordPointer(2, GL_FLOAT, 0, draw->coords->tex_coord);
154 
155    glDrawArrays(gfx_display_prim_to_gl1_enum(
156             draw->prim_type), 0, draw->coords->vertices);
157 
158    glDisableClientState(GL_COLOR_ARRAY);
159    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
160    glDisableClientState(GL_VERTEX_ARRAY);
161 
162    glMatrixMode(GL_MODELVIEW);
163    glPopMatrix();
164    glMatrixMode(GL_PROJECTION);
165    glPopMatrix();
166 
167    gl1->coords.color = gl1->white_color_ptr;
168 }
169 
gfx_display_gl1_font_init_first(void ** font_handle,void * video_data,const char * font_path,float menu_font_size,bool is_threaded)170 static bool gfx_display_gl1_font_init_first(
171       void **font_handle, void *video_data,
172       const char *font_path, float menu_font_size,
173       bool is_threaded)
174 {
175    font_data_t **handle = (font_data_t**)font_handle;
176    if (!(*handle = font_driver_init_first(video_data,
177          font_path, menu_font_size, true,
178          is_threaded,
179          FONT_DRIVER_RENDER_OPENGL1_API)))
180        return false;
181    return true;
182 }
183 
gfx_display_gl1_scissor_begin(void * data,unsigned video_width,unsigned video_height,int x,int y,unsigned width,unsigned height)184 static void gfx_display_gl1_scissor_begin(void *data,
185       unsigned video_width,
186       unsigned video_height,
187       int x, int y,
188       unsigned width, unsigned height)
189 {
190    glScissor(x, video_height - y - height, width, height);
191    glEnable(GL_SCISSOR_TEST);
192 }
193 
gfx_display_gl1_scissor_end(void * data,unsigned video_width,unsigned video_height)194 static void gfx_display_gl1_scissor_end(
195       void *data,
196       unsigned video_width,
197       unsigned video_height)
198 {
199    glScissor(0, 0, video_width, video_height);
200    glDisable(GL_SCISSOR_TEST);
201 }
202 
203 gfx_display_ctx_driver_t gfx_display_ctx_gl1 = {
204    gfx_display_gl1_draw,
205    NULL, /* draw_pipeline */
206    gfx_display_gl1_blend_begin,
207    gfx_display_gl1_blend_end,
208    gfx_display_gl1_get_default_mvp,
209    gfx_display_gl1_get_default_vertices,
210    gfx_display_gl1_get_default_tex_coords,
211    gfx_display_gl1_font_init_first,
212    GFX_VIDEO_DRIVER_OPENGL1,
213    "gl1",
214    false,
215    gfx_display_gl1_scissor_begin,
216    gfx_display_gl1_scissor_end
217 };
218