2011-01-17 19:54:58 +00:00
|
|
|
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
|
2010-05-28 16:22:06 +00:00
|
|
|
* Copyright (C) 2010 - Hans-Kristian Arntzen
|
|
|
|
*
|
|
|
|
* Some code herein may be based on code found in BSNES.
|
|
|
|
*
|
|
|
|
* SSNES is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with SSNES.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-05-28 00:45:18 +00:00
|
|
|
#include "driver.h"
|
2011-01-06 17:34:11 +00:00
|
|
|
|
2010-05-28 00:45:18 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include "libsnes.hpp"
|
|
|
|
#include <stdio.h>
|
2010-08-16 17:49:54 +00:00
|
|
|
#include <sys/time.h>
|
2010-12-26 03:15:12 +00:00
|
|
|
#include <string.h>
|
2010-12-29 19:05:57 +00:00
|
|
|
#include "general.h"
|
2011-01-07 16:59:53 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2010-12-30 11:54:16 +00:00
|
|
|
#include "config.h"
|
2011-01-07 16:59:53 +00:00
|
|
|
#endif
|
2010-05-28 00:45:18 +00:00
|
|
|
|
2011-01-06 17:34:11 +00:00
|
|
|
#define NO_SDL_GLEXT
|
2011-01-07 17:11:06 +00:00
|
|
|
#include "SDL.h"
|
|
|
|
#include "SDL_opengl.h"
|
|
|
|
#include "input/ssnes_sdl_input.h"
|
2011-01-06 17:34:11 +00:00
|
|
|
|
|
|
|
#define GL_GLEXT_PROTOTYPES
|
|
|
|
#include <GL/glext.h>
|
2010-11-13 17:50:34 +00:00
|
|
|
|
2011-01-06 22:15:49 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <GL/glx.h>
|
|
|
|
#endif
|
|
|
|
|
2010-11-12 22:09:43 +00:00
|
|
|
#ifdef HAVE_CG
|
2011-01-05 16:22:12 +00:00
|
|
|
#include "shader_cg.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_XML
|
|
|
|
#include "shader_glsl.h"
|
2010-11-12 22:09:43 +00:00
|
|
|
#endif
|
2010-11-12 21:32:38 +00:00
|
|
|
|
2011-01-11 21:13:55 +00:00
|
|
|
#include "gl_common.h"
|
|
|
|
|
2011-01-22 23:27:20 +00:00
|
|
|
#ifdef HAVE_FREETYPE
|
|
|
|
#include "fonts.h"
|
|
|
|
#endif
|
|
|
|
|
2010-11-08 23:01:17 +00:00
|
|
|
static const GLfloat vertexes[] = {
|
|
|
|
0, 0, 0,
|
|
|
|
0, 1, 0,
|
|
|
|
1, 1, 0,
|
|
|
|
1, 0, 0
|
|
|
|
};
|
|
|
|
|
2010-11-12 21:32:38 +00:00
|
|
|
static const GLfloat tex_coords[] = {
|
|
|
|
0, 1,
|
2010-11-08 23:13:57 +00:00
|
|
|
0, 0,
|
2010-11-12 21:32:38 +00:00
|
|
|
1, 0,
|
|
|
|
1, 1
|
2010-11-08 23:13:57 +00:00
|
|
|
};
|
|
|
|
|
2010-05-28 00:45:18 +00:00
|
|
|
typedef struct gl
|
|
|
|
{
|
2010-08-16 16:40:17 +00:00
|
|
|
bool vsync;
|
2010-11-13 01:48:32 +00:00
|
|
|
GLuint texture;
|
|
|
|
GLuint tex_filter;
|
2010-12-26 03:15:12 +00:00
|
|
|
|
2011-01-06 19:01:32 +00:00
|
|
|
bool should_resize;
|
|
|
|
bool quitting;
|
2011-01-22 23:27:20 +00:00
|
|
|
bool keep_aspect;
|
2011-01-06 19:01:32 +00:00
|
|
|
|
|
|
|
unsigned win_width;
|
|
|
|
unsigned win_height;
|
|
|
|
unsigned vp_width;
|
|
|
|
unsigned vp_height;
|
2010-12-26 03:15:12 +00:00
|
|
|
unsigned last_width;
|
|
|
|
unsigned last_height;
|
|
|
|
unsigned tex_w, tex_h;
|
|
|
|
GLfloat tex_coords[8];
|
2010-05-28 00:45:18 +00:00
|
|
|
|
2011-01-22 23:27:20 +00:00
|
|
|
#ifdef HAVE_FREETYPE
|
|
|
|
font_renderer_t *font;
|
|
|
|
GLuint font_tex;
|
|
|
|
#endif
|
2010-05-28 00:45:18 +00:00
|
|
|
|
2011-01-22 23:27:20 +00:00
|
|
|
} gl_t;
|
|
|
|
|
|
|
|
////////////////// Shaders
|
2011-01-05 16:22:12 +00:00
|
|
|
static inline bool gl_shader_init(void)
|
|
|
|
{
|
2011-01-05 18:29:29 +00:00
|
|
|
if (strlen(g_settings.video.cg_shader_path) > 0 && strlen(g_settings.video.bsnes_shader_path) > 0)
|
|
|
|
SSNES_WARN("Both Cg and bSNES XML shader are defined in config file. Cg shader will be selected by default.\n");
|
|
|
|
|
2011-01-05 16:22:12 +00:00
|
|
|
#ifdef HAVE_CG
|
|
|
|
if (strlen(g_settings.video.cg_shader_path) > 0)
|
|
|
|
return gl_cg_init(g_settings.video.cg_shader_path);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_XML
|
|
|
|
if (strlen(g_settings.video.bsnes_shader_path) > 0)
|
|
|
|
return gl_glsl_init(g_settings.video.bsnes_shader_path);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-22 23:27:20 +00:00
|
|
|
static inline void gl_shader_deactivate(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_CG
|
|
|
|
gl_cg_deactivate();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_XML
|
|
|
|
gl_glsl_deactivate();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void gl_shader_activate(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_CG
|
|
|
|
gl_cg_activate();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_XML
|
|
|
|
gl_glsl_activate();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-01-05 16:22:12 +00:00
|
|
|
static inline void gl_shader_deinit(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_CG
|
|
|
|
gl_cg_deinit();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_XML
|
|
|
|
gl_glsl_deinit();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void gl_shader_set_proj_matrix(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_CG
|
|
|
|
gl_cg_set_proj_matrix();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_XML
|
|
|
|
gl_glsl_set_proj_matrix();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void gl_shader_set_params(unsigned width, unsigned height,
|
|
|
|
unsigned tex_width, unsigned tex_height,
|
|
|
|
unsigned out_width, unsigned out_height)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_CG
|
|
|
|
gl_cg_set_params(width, height, tex_width, tex_height, out_width, out_height);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_XML
|
|
|
|
gl_glsl_set_params(width, height, tex_width, tex_height, out_width, out_height);
|
|
|
|
#endif
|
|
|
|
}
|
2011-01-22 23:27:20 +00:00
|
|
|
///////////////////
|
|
|
|
|
|
|
|
//////////////// Message rendering
|
2011-01-23 01:48:06 +00:00
|
|
|
static inline void gl_init_font(gl_t *gl, const char *font_path, unsigned font_size)
|
2011-01-22 23:27:20 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_FREETYPE
|
2011-01-23 01:48:06 +00:00
|
|
|
gl->font = font_renderer_new(font_path, font_size);
|
2011-01-22 23:27:20 +00:00
|
|
|
if (gl->font)
|
|
|
|
{
|
|
|
|
glGenTextures(1, &gl->font_tex);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gl->font_tex);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gl->texture);
|
|
|
|
}
|
|
|
|
else
|
2011-01-23 13:48:07 +00:00
|
|
|
SSNES_WARN("Couldn't init font renderer with font \"%s\"...\n", font_path);
|
2011-01-22 23:27:20 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void gl_deinit_font(gl_t *gl)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_FREETYPE
|
|
|
|
if (gl->font)
|
|
|
|
{
|
|
|
|
font_renderer_free(gl->font);
|
|
|
|
glDeleteTextures(1, &gl->font_tex);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned get_alignment(unsigned pitch)
|
|
|
|
{
|
|
|
|
if (pitch & 1)
|
|
|
|
return 1;
|
|
|
|
if (pitch & 2)
|
|
|
|
return 2;
|
|
|
|
if (pitch & 4)
|
|
|
|
return 4;
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gl_render_msg(gl_t *gl, const char *msg)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_FREETYPE
|
|
|
|
if (!gl->font)
|
|
|
|
return;
|
|
|
|
|
|
|
|
GLfloat font_vertex[12];
|
|
|
|
|
|
|
|
// Deactivate custom shaders. Enable the font texture.
|
2011-01-22 23:48:47 +00:00
|
|
|
gl_shader_deactivate();
|
2011-01-22 23:27:20 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, gl->font_tex);
|
|
|
|
glVertexPointer(3, GL_FLOAT, 3 * sizeof(GLfloat), font_vertex);
|
|
|
|
glTexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), tex_coords); // Use the static one (uses whole texture).
|
|
|
|
|
|
|
|
// Need blending.
|
|
|
|
// Using fixed function pipeline here since we cannot guarantee presence of shaders (would be kinda overkill anyways).
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);
|
|
|
|
|
|
|
|
struct font_output_list out;
|
|
|
|
font_renderer_msg(gl->font, msg, &out);
|
|
|
|
struct font_output *head = out.head;
|
|
|
|
|
|
|
|
while (head != NULL)
|
|
|
|
{
|
2011-01-23 01:59:44 +00:00
|
|
|
GLfloat lx = (GLfloat)head->off_x / gl->vp_width + g_settings.video.msg_pos_x;
|
|
|
|
GLfloat hx = (GLfloat)(head->off_x + head->width) / gl->vp_width + g_settings.video.msg_pos_x;
|
|
|
|
GLfloat ly = (GLfloat)head->off_y / gl->vp_height + g_settings.video.msg_pos_y;
|
|
|
|
GLfloat hy = (GLfloat)(head->off_y + head->height) / gl->vp_height + g_settings.video.msg_pos_y;
|
2011-01-22 23:27:20 +00:00
|
|
|
|
|
|
|
font_vertex[0] = lx;
|
|
|
|
font_vertex[1] = ly;
|
|
|
|
font_vertex[3] = lx;
|
|
|
|
font_vertex[4] = hy;
|
|
|
|
font_vertex[6] = hx;
|
|
|
|
font_vertex[7] = hy;
|
|
|
|
font_vertex[9] = hx;
|
|
|
|
font_vertex[10] = ly;
|
|
|
|
|
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, get_alignment(head->pitch));
|
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, head->pitch);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D,
|
|
|
|
0, GL_RGBA, head->width, head->height, 0, GL_LUMINANCE,
|
|
|
|
GL_UNSIGNED_BYTE, head->output);
|
|
|
|
|
|
|
|
head = head->next;
|
|
|
|
glDrawArrays(GL_QUADS, 0, 4);
|
|
|
|
}
|
|
|
|
font_renderer_free_output(&out);
|
|
|
|
|
|
|
|
// Go back to old rendering path.
|
|
|
|
glTexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), gl->tex_coords);
|
|
|
|
glVertexPointer(3, GL_FLOAT, 3 * sizeof(GLfloat), vertexes);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gl->texture);
|
|
|
|
glDisable(GL_BLEND);
|
2011-01-22 23:48:47 +00:00
|
|
|
gl_shader_activate();
|
2011-01-22 23:27:20 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
//////////////
|
2011-01-05 16:22:12 +00:00
|
|
|
|
2011-01-06 19:01:32 +00:00
|
|
|
static void set_viewport(gl_t *gl)
|
2010-05-28 00:45:18 +00:00
|
|
|
{
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
2011-01-06 19:01:32 +00:00
|
|
|
GLuint out_width = gl->win_width, out_height = gl->win_height;
|
2010-05-28 00:45:18 +00:00
|
|
|
|
2011-01-22 23:27:20 +00:00
|
|
|
if (gl->keep_aspect)
|
2010-05-28 00:45:18 +00:00
|
|
|
{
|
2011-01-06 19:01:32 +00:00
|
|
|
float desired_aspect = g_settings.video.aspect_ratio;
|
|
|
|
float device_aspect = (float)gl->win_width / gl->win_height;
|
2010-05-28 00:45:18 +00:00
|
|
|
|
2010-11-08 22:38:32 +00:00
|
|
|
// If the aspect ratios of screen and desired aspect ratio are sufficiently equal (floating point stuff),
|
|
|
|
// assume they are actually equal.
|
|
|
|
if ( (int)(device_aspect*1000) > (int)(desired_aspect*1000) )
|
2010-05-28 00:45:18 +00:00
|
|
|
{
|
2010-11-08 22:38:32 +00:00
|
|
|
float delta = (desired_aspect / device_aspect - 1.0) / 2.0 + 0.5;
|
2011-01-06 19:01:32 +00:00
|
|
|
glViewport(gl->win_width * (0.5 - delta), 0, 2.0 * gl->win_width * delta, gl->win_height);
|
|
|
|
out_width = (int)(2.0 * gl->win_width * delta);
|
2010-05-28 00:45:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-08 22:38:32 +00:00
|
|
|
else if ( (int)(device_aspect*1000) < (int)(desired_aspect*1000) )
|
2010-05-28 00:45:18 +00:00
|
|
|
{
|
2010-11-08 22:38:32 +00:00
|
|
|
float delta = (device_aspect / desired_aspect - 1.0) / 2.0 + 0.5;
|
2011-01-06 19:01:32 +00:00
|
|
|
glViewport(0, gl->win_height * (0.5 - delta), gl->win_width, 2.0 * gl->win_height * delta);
|
|
|
|
out_height = (int)(2.0 * gl->win_height * delta);
|
2010-05-28 00:45:18 +00:00
|
|
|
}
|
|
|
|
else
|
2011-01-06 19:01:32 +00:00
|
|
|
glViewport(0, 0, gl->win_width, gl->win_height);
|
2010-05-28 00:45:18 +00:00
|
|
|
}
|
|
|
|
else
|
2011-01-06 19:01:32 +00:00
|
|
|
glViewport(0, 0, gl->win_width, gl->win_height);
|
2010-05-28 00:45:18 +00:00
|
|
|
|
2010-11-08 22:38:32 +00:00
|
|
|
glOrtho(0, 1, 0, 1, -1, 1);
|
2010-05-28 00:45:18 +00:00
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity();
|
2011-01-05 16:22:12 +00:00
|
|
|
|
|
|
|
gl_shader_set_proj_matrix();
|
|
|
|
|
2011-01-06 19:01:32 +00:00
|
|
|
gl->vp_width = out_width;
|
|
|
|
gl->vp_height = out_height;
|
2010-05-28 00:45:18 +00:00
|
|
|
}
|
|
|
|
|
2010-08-16 17:49:54 +00:00
|
|
|
static float tv_to_fps(const struct timeval *tv, const struct timeval *new_tv, int frames)
|
|
|
|
{
|
|
|
|
float time = new_tv->tv_sec - tv->tv_sec + (new_tv->tv_usec - tv->tv_usec)/1000000.0;
|
|
|
|
return frames/time;
|
|
|
|
}
|
|
|
|
|
2011-01-06 17:34:11 +00:00
|
|
|
static void show_fps(void)
|
2010-05-28 00:45:18 +00:00
|
|
|
{
|
2010-12-14 13:28:09 +00:00
|
|
|
// Shows FPS in taskbar.
|
2010-08-16 17:49:54 +00:00
|
|
|
static int frames = 0;
|
|
|
|
static struct timeval tv;
|
|
|
|
struct timeval new_tv;
|
2010-12-14 13:28:09 +00:00
|
|
|
|
2010-08-16 17:49:54 +00:00
|
|
|
if (frames == 0)
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
|
2010-11-08 23:17:15 +00:00
|
|
|
if ((frames % 180) == 0 && frames > 0)
|
2010-08-16 17:49:54 +00:00
|
|
|
{
|
|
|
|
gettimeofday(&new_tv, NULL);
|
2010-12-14 14:19:13 +00:00
|
|
|
struct timeval tmp_tv = tv;
|
2010-08-16 17:49:54 +00:00
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
char tmpstr[256] = {0};
|
|
|
|
|
2010-11-08 23:17:15 +00:00
|
|
|
float fps = tv_to_fps(&tmp_tv, &new_tv, 180);
|
2010-08-16 17:49:54 +00:00
|
|
|
|
2011-01-06 17:34:11 +00:00
|
|
|
snprintf(tmpstr, sizeof(tmpstr), "SSNES || FPS: %6.1f || Frames: %d", fps, frames);
|
|
|
|
SDL_WM_SetCaption(tmpstr, NULL);
|
2010-08-16 17:49:54 +00:00
|
|
|
}
|
|
|
|
frames++;
|
2010-11-08 22:38:32 +00:00
|
|
|
}
|
2010-08-16 17:49:54 +00:00
|
|
|
|
2011-01-22 23:27:20 +00:00
|
|
|
static bool gl_frame(void *data, const uint16_t* frame, unsigned width, unsigned height, unsigned pitch, const char *msg)
|
2010-11-08 22:38:32 +00:00
|
|
|
{
|
2010-11-13 01:48:32 +00:00
|
|
|
gl_t *gl = data;
|
2010-11-08 22:38:32 +00:00
|
|
|
|
2011-01-06 19:01:32 +00:00
|
|
|
if (gl->should_resize)
|
|
|
|
{
|
|
|
|
gl->should_resize = false;
|
2011-01-13 20:25:52 +00:00
|
|
|
SDL_SetVideoMode(gl->win_width, gl->win_height, 0, SDL_OPENGL | SDL_RESIZABLE | (g_settings.video.fullscreen ? SDL_FULLSCREEN : 0));
|
2011-01-06 19:01:32 +00:00
|
|
|
set_viewport(gl);
|
|
|
|
}
|
|
|
|
|
2010-11-08 22:38:32 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
2011-01-13 20:25:52 +00:00
|
|
|
gl_shader_set_params(width, height, gl->tex_w, gl->tex_h, gl->vp_width, gl->vp_height);
|
2010-11-13 01:40:38 +00:00
|
|
|
|
2010-12-26 03:15:12 +00:00
|
|
|
if (width != gl->last_width || height != gl->last_height) // res change. need to clear out texture.
|
|
|
|
{
|
|
|
|
gl->last_width = width;
|
|
|
|
gl->last_height = height;
|
2011-01-22 23:27:20 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, get_alignment(pitch));
|
2011-01-13 20:25:52 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, gl->tex_w);
|
2010-12-26 03:15:12 +00:00
|
|
|
uint8_t *tmp = calloc(1, gl->tex_w * gl->tex_h * sizeof(uint16_t));
|
|
|
|
glTexSubImage2D(GL_TEXTURE_2D,
|
|
|
|
0, 0, 0, gl->tex_w, gl->tex_h, GL_BGRA,
|
|
|
|
GL_UNSIGNED_SHORT_1_5_5_5_REV, tmp);
|
|
|
|
free(tmp);
|
|
|
|
|
|
|
|
gl->tex_coords[0] = 0;
|
|
|
|
gl->tex_coords[1] = (GLfloat)height / gl->tex_h;
|
|
|
|
gl->tex_coords[2] = 0;
|
|
|
|
gl->tex_coords[3] = 0;
|
|
|
|
gl->tex_coords[4] = (GLfloat)width / gl->tex_w;
|
|
|
|
gl->tex_coords[5] = 0;
|
|
|
|
gl->tex_coords[6] = (GLfloat)width / gl->tex_w;
|
|
|
|
gl->tex_coords[7] = (GLfloat)height / gl->tex_h;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-12 21:32:38 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch >> 1);
|
2010-12-26 03:15:12 +00:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D,
|
|
|
|
0, 0, 0, width, height, GL_BGRA,
|
2010-11-12 21:32:38 +00:00
|
|
|
GL_UNSIGNED_SHORT_1_5_5_5_REV, frame);
|
2010-11-08 22:38:32 +00:00
|
|
|
glDrawArrays(GL_QUADS, 0, 4);
|
2010-12-14 13:28:09 +00:00
|
|
|
|
2011-01-22 23:48:47 +00:00
|
|
|
if (msg)
|
|
|
|
gl_render_msg(gl, msg);
|
2011-01-22 23:27:20 +00:00
|
|
|
|
2010-11-08 22:38:32 +00:00
|
|
|
show_fps();
|
2011-01-06 19:01:32 +00:00
|
|
|
glFlush();
|
2011-01-06 17:34:11 +00:00
|
|
|
SDL_GL_SwapBuffers();
|
2010-05-28 00:45:18 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gl_free(void *data)
|
|
|
|
{
|
2010-11-13 01:48:32 +00:00
|
|
|
gl_t *gl = data;
|
2011-01-05 16:22:12 +00:00
|
|
|
|
2011-01-22 23:27:20 +00:00
|
|
|
gl_deinit_font(gl);
|
2011-01-05 16:22:12 +00:00
|
|
|
gl_shader_deinit();
|
2010-11-08 22:38:32 +00:00
|
|
|
glDisableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
2010-11-13 01:48:32 +00:00
|
|
|
glDeleteTextures(1, &gl->texture);
|
2011-01-06 17:34:11 +00:00
|
|
|
SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
2010-05-28 00:45:18 +00:00
|
|
|
}
|
|
|
|
|
2010-08-16 16:40:17 +00:00
|
|
|
static void gl_set_nonblock_state(void *data, bool state)
|
|
|
|
{
|
|
|
|
gl_t *gl = data;
|
|
|
|
if (gl->vsync)
|
|
|
|
{
|
2011-01-06 19:01:32 +00:00
|
|
|
SSNES_LOG("GL VSync => %s\n", state ? "off" : "on");
|
2011-01-06 22:15:49 +00:00
|
|
|
#ifdef _WIN32
|
2011-01-07 16:59:53 +00:00
|
|
|
static BOOL (APIENTRY *wgl_swap_interval)(int) = NULL;
|
2011-01-06 22:15:49 +00:00
|
|
|
if (!wgl_swap_interval)
|
|
|
|
SSNES_WARN("SDL VSync toggling seems to be broken, attempting to use WGL VSync call directly instead.\n");
|
|
|
|
if (!wgl_swap_interval) wgl_swap_interval = (BOOL (APIENTRY*)(int)) wglGetProcAddress("wglSwapIntervalEXT");
|
|
|
|
if (wgl_swap_interval) wgl_swap_interval(state ? 0 : 1);
|
|
|
|
#else
|
|
|
|
static int (*glx_swap_interval)(int) = NULL;
|
|
|
|
if (!glx_swap_interval)
|
|
|
|
SSNES_WARN("SDL VSync toggling seems to be broken, attempting to use GLX VSync call directly instead.\n");
|
|
|
|
if (!glx_swap_interval) glx_swap_interval = (int (*)(int))glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalSGI");
|
|
|
|
if (!glx_swap_interval) glx_swap_interval = (int (*)(int))glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalMESA");
|
|
|
|
if (glx_swap_interval) glx_swap_interval(state ? 0 : 1);
|
|
|
|
#endif
|
2010-08-16 16:40:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-06 19:01:32 +00:00
|
|
|
static void* gl_init(video_info_t *video, const input_driver_t **input, void **input_data)
|
2010-05-28 00:45:18 +00:00
|
|
|
{
|
2011-01-06 17:34:11 +00:00
|
|
|
if (SDL_Init(SDL_INIT_VIDEO) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, video->vsync ? 1 : 0);
|
2011-01-11 20:16:57 +00:00
|
|
|
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
|
2011-01-06 17:34:11 +00:00
|
|
|
|
2011-01-13 19:18:27 +00:00
|
|
|
if (!SDL_SetVideoMode(video->width, video->height, 0, SDL_OPENGL | SDL_RESIZABLE | (video->fullscreen ? SDL_FULLSCREEN : 0)))
|
2011-01-06 17:34:11 +00:00
|
|
|
return NULL;
|
2011-01-11 21:33:28 +00:00
|
|
|
|
2011-01-06 19:01:32 +00:00
|
|
|
int attr = 0;
|
|
|
|
SDL_GL_GetAttribute(SDL_GL_SWAP_CONTROL, &attr);
|
|
|
|
if (attr <= 0 && video->vsync)
|
|
|
|
SSNES_WARN("GL VSync has not been enabled!\n");
|
|
|
|
attr = 0;
|
|
|
|
SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &attr);
|
|
|
|
if (attr <= 0)
|
|
|
|
SSNES_WARN("GL double buffer has not been enabled!\n");
|
|
|
|
|
2011-01-11 21:33:28 +00:00
|
|
|
|
2011-01-11 20:16:57 +00:00
|
|
|
gl_t *gl = calloc(1, sizeof(gl_t));
|
|
|
|
if (!gl)
|
|
|
|
return NULL;
|
|
|
|
|
2011-01-13 20:25:52 +00:00
|
|
|
gl->win_width = video->width;
|
|
|
|
gl->win_height = video->height;
|
|
|
|
gl->vsync = video->vsync;
|
2011-01-22 23:27:20 +00:00
|
|
|
gl->keep_aspect = video->force_aspect;
|
2011-01-13 20:25:52 +00:00
|
|
|
set_viewport(gl);
|
2011-01-06 17:34:11 +00:00
|
|
|
|
2011-01-11 18:23:21 +00:00
|
|
|
if (!gl_shader_init())
|
|
|
|
{
|
|
|
|
SSNES_ERR("Shader init failed.\n");
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
2011-01-11 20:16:57 +00:00
|
|
|
free(gl);
|
2011-01-11 18:23:21 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-01-13 20:25:52 +00:00
|
|
|
// Remove that ugly mouse :D
|
|
|
|
SDL_ShowCursor(SDL_DISABLE);
|
2011-01-06 19:01:32 +00:00
|
|
|
|
2010-05-29 12:45:40 +00:00
|
|
|
if ( video->smooth )
|
2010-11-13 01:48:32 +00:00
|
|
|
gl->tex_filter = GL_LINEAR;
|
2010-05-29 12:45:40 +00:00
|
|
|
else
|
2010-11-13 01:48:32 +00:00
|
|
|
gl->tex_filter = GL_NEAREST;
|
2010-05-28 00:54:20 +00:00
|
|
|
|
2010-05-28 00:45:18 +00:00
|
|
|
glEnable(GL_TEXTURE_2D);
|
2010-11-08 23:13:57 +00:00
|
|
|
glDisable(GL_DITHER);
|
2010-11-08 22:38:32 +00:00
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
glColor3f(1, 1, 1);
|
|
|
|
glClearColor(0, 0, 0, 0);
|
2010-05-28 00:45:18 +00:00
|
|
|
|
2011-01-06 17:34:11 +00:00
|
|
|
SDL_WM_SetCaption("SSNES", NULL);
|
2010-05-28 00:45:18 +00:00
|
|
|
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity();
|
|
|
|
|
2010-11-13 01:48:32 +00:00
|
|
|
glGenTextures(1, &gl->texture);
|
2010-11-08 22:38:32 +00:00
|
|
|
|
2010-11-13 01:48:32 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, gl->texture);
|
2010-11-08 22:38:32 +00:00
|
|
|
|
2011-01-05 18:25:27 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
|
2010-11-13 01:48:32 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl->tex_filter);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl->tex_filter);
|
2010-11-08 22:38:32 +00:00
|
|
|
|
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
2010-11-08 23:01:17 +00:00
|
|
|
glVertexPointer(3, GL_FLOAT, 3 * sizeof(GLfloat), vertexes);
|
2010-12-26 03:15:12 +00:00
|
|
|
|
|
|
|
memcpy(gl->tex_coords, tex_coords, sizeof(tex_coords));
|
|
|
|
glTexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), gl->tex_coords);
|
|
|
|
|
|
|
|
gl->tex_w = 256 * video->input_scale;
|
|
|
|
gl->tex_h = 256 * video->input_scale;
|
|
|
|
uint8_t *tmp = calloc(1, gl->tex_w * gl->tex_h * sizeof(uint16_t));
|
|
|
|
glTexImage2D(GL_TEXTURE_2D,
|
|
|
|
0, GL_RGBA, gl->tex_w, gl->tex_h, 0, GL_BGRA,
|
|
|
|
GL_UNSIGNED_SHORT_1_5_5_5_REV, tmp);
|
|
|
|
free(tmp);
|
|
|
|
gl->last_width = gl->tex_w;
|
|
|
|
gl->last_height = gl->tex_h;
|
2010-05-28 00:45:18 +00:00
|
|
|
|
2011-01-06 19:01:32 +00:00
|
|
|
// Hook up SDL input driver to get SDL_QUIT events and RESIZE.
|
|
|
|
sdl_input_t *sdl_input = input_sdl.init();
|
|
|
|
if (sdl_input)
|
|
|
|
{
|
|
|
|
sdl_input->quitting = &gl->quitting;
|
|
|
|
sdl_input->should_resize = &gl->should_resize;
|
|
|
|
sdl_input->new_width = &gl->win_width;
|
|
|
|
sdl_input->new_height = &gl->win_height;
|
|
|
|
*input = &input_sdl;
|
|
|
|
*input_data = sdl_input;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*input = NULL;
|
2011-01-22 23:27:20 +00:00
|
|
|
|
2011-01-23 01:48:06 +00:00
|
|
|
gl_init_font(gl, g_settings.video.font_path, g_settings.video.font_size);
|
2011-01-13 20:25:52 +00:00
|
|
|
|
2011-01-11 21:33:28 +00:00
|
|
|
if (!gl_check_error())
|
|
|
|
{
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
|
|
|
free(gl);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-08-16 16:40:17 +00:00
|
|
|
return gl;
|
2010-05-28 00:45:18 +00:00
|
|
|
}
|
|
|
|
|
2011-01-06 19:01:32 +00:00
|
|
|
static bool gl_alive(void *data)
|
|
|
|
{
|
|
|
|
gl_t *gl = data;
|
|
|
|
return !gl->quitting;
|
|
|
|
}
|
|
|
|
|
2010-05-28 00:45:18 +00:00
|
|
|
const video_driver_t video_gl = {
|
|
|
|
.init = gl_init,
|
|
|
|
.frame = gl_frame,
|
2011-01-06 19:01:32 +00:00
|
|
|
.alive = gl_alive,
|
2010-08-16 16:40:17 +00:00
|
|
|
.set_nonblock_state = gl_set_nonblock_state,
|
2010-12-29 19:05:57 +00:00
|
|
|
.free = gl_free,
|
2011-01-06 17:34:11 +00:00
|
|
|
.ident = "gl"
|
2010-05-28 00:45:18 +00:00
|
|
|
};
|
2010-12-14 13:28:09 +00:00
|
|
|
|
2010-05-28 00:45:18 +00:00
|
|
|
|
|
|
|
|