renderbuffer should work without OpenGL

This commit is contained in:
Zeno Rogue 2018-02-03 13:50:47 +01:00
parent b3f047ea6a
commit dcc3e11287
3 changed files with 89 additions and 44 deletions

17
hyper.h
View File

@ -2515,20 +2515,27 @@ bool is_cell_removed(cell *c);
void set_if_removed(cell*& c, cell *val);
struct renderbuffer {
int x, y, tx, ty;
bool valid;
int x, y;
#if CAP_GL
int tx, ty;
GLuint FramebufferName;
GLuint renderedTexture;
GLuint depth_stencil_rb;
SDL_Surface *srf;
Uint32 *expanded_data;
void use_as_texture();
#endif
#if CAP_SDL
SDL_Surface *srf;
void make_surface();
SDL_Surface *render();
#endif
renderbuffer(int x, int y, bool gl);
~renderbuffer();
void enable();
void disable();
SDL_Surface *render();
void use_as_texture();
void clear(int col);
};

View File

@ -1,3 +1,4 @@
#if CAP_GL
#if !CAP_GLEW
#if ISLINUX
extern "C" {
@ -19,12 +20,21 @@ GLAPI void APIENTRY glDeleteFramebuffers (GLsizei n, const GLuint *framebuffers)
#define glFramebufferTexture glFramebufferTextureEXT
#endif
#endif
#endif
renderbuffer::renderbuffer(int x, int y, bool gl) : x(x), y(y) {
FramebufferName = renderedTexture = depth_stencil_rb = 0;
srf = NULL; expanded_data = NULL;
valid = false;
#if CAP_GL
FramebufferName = renderedTexture = depth_stencil_rb = 0; expanded_data = NULL;
#endif
#if CAP_SDL
srf = NULL;
#endif
# if CAP_GL
if(gl) {
tx = next_p2(x);
ty = next_p2(y);
@ -66,30 +76,65 @@ renderbuffer::renderbuffer(int x, int y, bool gl) : x(x), y(y) {
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
FramebufferName = renderedTexture = 0;
else
valid = true;
}
if(!FramebufferName)
#endif
#if CAP_SDL
if(!valid)
make_surface();
#endif
}
#if CAP_SDL
void renderbuffer::make_surface() {
if(!srf)
srf = SDL_CreateRGBSurface(SDL_SWSURFACE, x, y, 32,0xff0000,0xff00,0xff,0xff000000);
}
SDL_Surface *renderbuffer::render() {
make_surface();
if(FramebufferName) {
glReadPixels(0, 0, vid.xres, vid.yres, GL_BGRA, GL_UNSIGNED_BYTE, srf->pixels);
for(int y=0; y<vid.yres/2; y++)
for(int x=0; x<vid.xres; x++)
swap(qpixel(srf,x,y), qpixel(srf,x,vid.yres-1-y));
}
return srf;
}
#endif
void renderbuffer::enable() {
#if CAP_GL
if(FramebufferName) {
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
glViewport(0,0,x,y);
vid.usingGL = true;
return;
}
else {
make_surface();
s = srf;
vid.usingGL = false;
}
#endif
#if CAP_SDL
make_surface();
s = srf;
vid.usingGL = false;
#endif
}
void renderbuffer::disable() {
#if CAP_GL
if(FramebufferName) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0,0,s_screen->w,s_screen->h);
return;
}
#endif
#if CAP_SDL
s = s_screen;
#endif
}
#if CAP_GL
void renderbuffer::use_as_texture() {
if(!renderedTexture) {
glGenTextures( 1, &renderedTexture);
@ -109,44 +154,33 @@ void renderbuffer::use_as_texture() {
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, tx, ty, 0, GL_BGRA, GL_UNSIGNED_BYTE, expanded_data );
}
}
#endif
void renderbuffer::disable() {
if(FramebufferName) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0,0,s_screen->w,s_screen->h);
}
else {
s = s_screen;
}
}
renderbuffer::~renderbuffer() {
#if CAP_GL
if(renderedTexture)
glDeleteTextures(1, &renderedTexture);
if(FramebufferName) {
glDeleteRenderbuffers(1, &depth_stencil_rb);
glDeleteFramebuffers(1, &FramebufferName);
}
if(srf)
SDL_FreeSurface(srf);
if(expanded_data)
delete[] expanded_data;
}
SDL_Surface *renderbuffer::render() {
make_surface();
if(FramebufferName) {
glReadPixels(0, 0, vid.xres, vid.yres, GL_BGRA, GL_UNSIGNED_BYTE, srf->pixels);
for(int y=0; y<vid.yres/2; y++)
for(int x=0; x<vid.xres; x++)
swap(qpixel(srf,x,y), qpixel(srf,x,vid.yres-1-y));
}
return srf;
#endif
#if CAP_SDL
if(srf)
SDL_FreeSurface(srf);
#endif
}
void renderbuffer::clear(int col) {
if(FramebufferName)
#if CAP_GL
if(FramebufferName) {
setGLProjection(0);
else
SDL_FillRect(srf, NULL, col);
return;
}
#endif
#if CAP_SDL
SDL_FillRect(srf, NULL, col);
#endif
}

10
rug.cpp
View File

@ -1197,9 +1197,13 @@ transmatrix currentrot;
void init() {
if(rugged) return;
rugged = true;
if(scale < .01 || scale > 100) scale = 1;
glbuf = new renderbuffer(TEXTURESIZE, TEXTURESIZE, vid.usingGL && !rendernogl);
if(!glbuf->valid) {
addMessage(XLAT("Failed to enable"));
delete glbuf;
return;
}
rugged = true;
if(renderonce) prepareTexture();
if(!rugged) return;
@ -1591,7 +1595,7 @@ void show() {
#endif
else if(uni == 's' && !rug::rugged) {
texturesize *= 2;
if(texturesize == 8192) texturesize = 128;
if(texturesize == 8192) texturesize = 64;
}
else if(handlekeys(sym, uni)) ;
else if(doexiton(sym, uni)) popScreen();