]> Chaos Git - vn/vndc.git/commitdiff
OpenGL code still not functional yet - but FBOs are *mostly* okay, code-wise, anyways.
authorchaoskagami <chaos.kagami@gmail.com>
Tue, 30 Sep 2014 08:33:52 +0000 (04:33 -0400)
committerchaoskagami <chaos.kagami@gmail.com>
Tue, 30 Sep 2014 08:33:52 +0000 (04:33 -0400)
buildscripts/build.generic
external/zero/include/ContextManager.hpp
external/zero/include/Zero.hpp
external/zero/src/ContextManager.cpp
external/zero/src/UDisplayable.cpp
vndc/include/Data.hpp
vndc/src/Data.cpp
vndc/src/Loop.cpp
vndc/src/VNDC.cpp
vndc/src/op_text.cpp

index 02859298d486391cbcdf48e596fbe5a16f7f2a0f..6ab687c1ca786f73d85943aa452d71ceb1e45e40 100755 (executable)
@@ -12,7 +12,7 @@ LIB=$ROOT/external/lib
 BIN=$ROOT/bin
 
 CXXFLAGS="-g `pkg-config sdl2 --cflags` `pkg-config SDL2_mixer --cflags` `pkg-config SDL2_image --cflags` `pkg-config SDL2_ttf --cflags`"
-LDFLAGS="`pkg-config sdl2 --libs` `pkg-config SDL2_image --libs` `pkg-config SDL2_ttf --libs` `pkg-config SDL2_mixer --libs` -lGL"
+LDFLAGS="-lSDL2 -lSDL2_mixer -lSDL2_image -lSDL2_ttf -lGL -lGLEW"
 INCLUDE="-I$ROOT/external/zero/include -I$ROOT/vndc/include"
 
 source buildscripts/mk
index ca43ccf21169a91f961212c5fe878bfdfd794a0d..1525ffd6ad5c79ad775b109c68a78d4960c7ec6f 100644 (file)
@@ -104,6 +104,8 @@ class TextManager; // Forward decl
                        bool accelerate;
                        bool opengl_mode;
 
+                       GLuint fg, bg, fg_tex, bg_tex;
+
                        bool StartQuit;
 
                        AudioManager* audioMgr;
index b99ccb29c3703506363ee23e785cb23ba7e89923..c70d64b15441affdeb57b8730be24d13409eb89c 100644 (file)
@@ -19,6 +19,7 @@
 #include <SDL_image.h>
 #include <SDL_mixer.h>
 #include <SDL_ttf.h>
+#include <GL/glew.h>
 #include <SDL_opengl.h>
 
 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
index 57046210022080e700a9e97c86f33dfabaa1760f..aec0511d0e6256d4e3cc842eb2e3273207049d54 100644 (file)
 
        // OpenGL window. Note - a LOT functions different in this mode.
        void ContextManager::_InitWindowGL(int width, int height, bool fulls) {
-
                opengl_mode = true;
 
                SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
 
                glctx = SDL_GL_CreateContext(window);
 
+               glewExperimental = GL_TRUE;
+               glewInit();
+
+               if(glGenFramebuffers == NULL || glBindFramebuffer == NULL || glFramebufferTexture == NULL || glDrawBuffers == NULL) {
+                       fprintf(stderr, "[ERR] Glew didn't load some extensions needed.\n");
+                       fprintf(stderr, "[ERR] I assume your card doesn't support them.\n");
+                       exit(-11);
+               }
+
+
                glEnable( GL_TEXTURE_2D );
 
                glViewport( 0, 0, width, height );
                glLoadIdentity();
 
                vertCnt = new VertexController(this);
+
+               // Create Renderable Textures.
+               // Unfortunately, GL is an asshole and I have no clue.
+
+               glGenFramebuffers(1, &bg);
+               glBindFramebuffer(GL_FRAMEBUFFER, bg);
+
+               glGenTextures(1, &bg_tex);
+               glBindTexture(GL_TEXTURE_2D, bg_tex);
+
+               glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+
+               glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, bg_tex, 0);
+
+               GLenum colbuf_bg[1] = {GL_COLOR_ATTACHMENT0};
+               glDrawBuffers(1, colbuf_bg);
+
+               glGenFramebuffers(1, &fg);
+               glBindFramebuffer(GL_FRAMEBUFFER, fg);
+
+               glGenTextures(1, &fg_tex);
+               glBindTexture(GL_TEXTURE_2D, fg_tex);
+
+               glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+
+               glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, fg_tex, 0);
+
+               GLenum colbuf_fg[1] = {GL_COLOR_ATTACHMENT1};
+               glDrawBuffers(1, colbuf_fg);
        }
 
        // Init window with a logical size and a real one. Determines which backend to use.
                        this->surface_o = SDL_CreateRGBSurface(0, width_win, height_win, 32, RED_MASK, GREEN_MASK, BLUE_MASK, ALPHA_MASK);
                        SDL_SetSurfaceBlendMode(this->surface_o, SDL_BLENDMODE_BLEND);
                }
+               else if (disp == OpenGL) {
+                       _InitWindowLogicalGL(width_win, height_win, width_log, height_log, fulls);
+               }
 
                txtMgr = new TextManager(this);
        }
                }
        }
 
+       // OpenGL window. Note - a LOT functions different in this mode.
+       void ContextManager::_InitWindowLogicalGL(int width_win, int height_win, int width_log, int height_log, bool fulls) {
+
+               opengl_mode = true;
+
+               SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
+
+               // Request OpenGL 3.0 context. Yes, not 3.1, 3.2, 4.0. THREE POINT O'.
+               // Why? I use the fixed function pipeline which is gonzo in >3.1
+               // and I'm not about to learn a new api that consists of VBOs, FBOs,
+               // and shader code (which btw, is retarded.)
+               SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
+               SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
+
+               if(!fulls)
+                       this->window = SDL_CreateWindow("Zero", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width_win, height_win, SDL_WINDOW_SHOWN|SDL_WINDOW_OPENGL);
+               else
+                       this->window = SDL_CreateWindow("Zero", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width_win, height_win, SDL_WINDOW_FULLSCREEN|SDL_WINDOW_OPENGL);
+
+               glctx = SDL_GL_CreateContext(window);
+
+               glewExperimental = GL_TRUE;
+               glewInit();
+
+               if(glGenFramebuffers == NULL || glBindFramebuffer == NULL || glFramebufferTexture == NULL || glDrawBuffers == NULL) {
+                       fprintf(stderr, "[ERR] Glew didn't load some extensions needed.\n");
+                       fprintf(stderr, "[ERR] I assume your card doesn't support them.\n");
+                       exit(-11);
+               }
+
+               glEnable( GL_TEXTURE_2D );
+
+               glViewport( 0, 0, width_win, height_win );
+
+               glClear( GL_COLOR_BUFFER_BIT );
+
+               glMatrixMode( GL_PROJECTION );
+               glLoadIdentity();
+
+               glOrtho(0.0f, width_win, height_win, 0.0f, -1.0f, 1.0f);
+
+               glMatrixMode( GL_MODELVIEW );
+               glLoadIdentity();
+
+               vertCnt = new VertexController(this);
+
+               fprintf(stderr, "[info] Generating FBOs...(bg) ");
+
+               glGenFramebuffers(1, &bg);
+               glBindFramebuffer(GL_FRAMEBUFFER, bg);
+
+               glGenTextures(1, &bg_tex);
+               glBindTexture(GL_TEXTURE_2D, bg_tex);
+
+               glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width_log, height_log, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+
+               glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, bg_tex, 0);
+
+               GLenum colbuf_bg[1] = {GL_COLOR_ATTACHMENT0};
+               glDrawBuffers(1, colbuf_bg);
+
+               glGenFramebuffers(1, &fg);
+               glBindFramebuffer(GL_FRAMEBUFFER, fg);
+
+               glGenTextures(1, &fg_tex);
+               glBindTexture(GL_TEXTURE_2D, fg_tex);
+
+               glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width_win, height_win, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+
+               glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, fg_tex, 0);
+
+               GLenum colbuf_fg[1] = {GL_COLOR_ATTACHMENT1};
+               glDrawBuffers(1, colbuf_fg);
+
+               fprintf(stderr, "[info] OpenGL initialized.\n");
+       }
+
        // Clears the display to black.
 
        void ContextManager::Clear() {
                        SDL_RenderClear(renderer);
                }
                else if (opengl_mode) {
+                       glBindFramebuffer(GL_FRAMEBUFFER, this->bg);
+                       glViewport(0,0,LOG_height,LOG_width);
+                       glClearColor(0,0,0,0);
+                       glClear(GL_COLOR_BUFFER_BIT);
+                       
+                       glBindFramebuffer(GL_FRAMEBUFFER, this->fg);
+                       glViewport(0,0,WIN_height,WIN_width);
+                       glClearColor(0,0,0,0);
+                       glClear(GL_COLOR_BUFFER_BIT);
+
+                       glBindFramebuffer(GL_FRAMEBUFFER, 0);
+                       glViewport(0,0,WIN_height,WIN_width);
                        glClearColor(0,0,0,0);
                        glClear(GL_COLOR_BUFFER_BIT);
                }
                        SDL_SetRenderTarget(renderer, NULL);
                }
                else if (opengl_mode) {
+                       glBindFramebuffer(GL_FRAMEBUFFER, this->fg);
+
+                       glViewport(0,0,WIN_height,WIN_width);
                        glClearColor(0,0,0,0);
                        glClear(GL_COLOR_BUFFER_BIT);
+
+                       glBindFramebuffer(GL_FRAMEBUFFER, 0);
                }
                else {
                        SDL_FillRect(this->surface_o, NULL, SDL_MapRGBA(this->surface_o->format, 0, 0, 0, 0));
                        SDL_RenderPresent(renderer);
                }
                else if (opengl_mode) {
+                       // Render prep.
+
+                       GLfloat x_tex, y_tex, x2_tex, y2_tex;
+                       x_tex  = 0.0f;
+                       y_tex  = 0.0f;
+                       x2_tex = 1.0f;
+                       y2_tex = 1.0f;
+
+
+                       GLfloat x_box, y_box, x2_box, y2_box;
+                       x_box = 0;
+                       y_box = WIN_height;
+                       x2_box = WIN_width;
+                       y2_box = 0;
+
+                       glColor4f( 1.0f, 1.0f, 1.0f, 1.0f ); //Don't use special coloring
+
+                       glBindTexture( GL_TEXTURE_2D, bg_tex );
+
+                       glBegin( GL_QUADS );
+                               //Bottom-left vertex (corner)
+                               glTexCoord2f( x_tex, y_tex );
+                               glVertex3f( x_box,  y2_box, 0.0f );
+
+                               //Bottom-right vertex (corner)
+                               glTexCoord2f( x2_tex, y_tex );
+                               glVertex3f( x2_box, y2_box, 0.0f );
+
+                               //Top-right vertex (corner)
+                               glTexCoord2f( x2_tex, y2_tex );
+                               glVertex3f( x2_box, y_box,  0.0f );
+
+                               //Top-left vertex (corner)
+                               glTexCoord2f( x_tex, y2_tex );
+                               glVertex3f( x_box,  y_box,  0.0f );
+                       glEnd();
+
+                       glBindTexture( GL_TEXTURE_2D, fg_tex );
+
+                       glBegin( GL_QUADS );
+                               //Bottom-left vertex (corner)
+                               glTexCoord2f( x_tex, y_tex );
+                               glVertex3f( x_box,  y2_box, 0.0f );
+
+                               //Bottom-right vertex (corner)
+                               glTexCoord2f( x2_tex, y_tex );
+                               glVertex3f( x2_box, y2_box, 0.0f );
+
+                               //Top-right vertex (corner)
+                               glTexCoord2f( x2_tex, y2_tex );
+                               glVertex3f( x2_box, y_box,  0.0f );
+
+                               //Top-left vertex (corner)
+                               glTexCoord2f( x_tex, y2_tex );
+                               glVertex3f( x_box,  y_box,  0.0f );
+                       glEnd();
+
+                       glBindTexture( GL_TEXTURE_2D, 0 );
+
                        SDL_GL_SwapWindow(window);
                }
                else {
                else if(opengl_mode) {
                        GLuint in = ((GLuint*)inp)[0];
 
+                       glBindFramebuffer(GL_FRAMEBUFFER, this->bg);
+                       glViewport(0,0,LOG_height,LOG_width);
+
                        // printf("[GL] (blit) in: %d\n", in);
 
                        GLfloat x_tex, y_tex, x2_tex, y2_tex;
                        glEnd();
 
                        glBindTexture( GL_TEXTURE_2D, 0 );
+
+                       glBindFramebuffer(GL_FRAMEBUFFER, 0);
+                       glViewport(0,0,WIN_height,WIN_width);
+
                }
                else {
                        SDL_BlitSurface((SDL_Surface*)inp, src, surface, dst);
                else if(opengl_mode) {
                        GLuint in = ((GLuint*)inp)[0];
 
+                       glBindFramebuffer(GL_FRAMEBUFFER, this->fg);
+                       glViewport(0,0,WIN_height,WIN_width);
+
                        GLfloat x, y, x2, y2;
                        x = 1.0f / glrect->x;
                        y = 1.0f / glrect->y;
                                glTexCoord2f( x, y2 );
                                glVertex3i( dst->x,          dst->y, 0.0f );
                        glEnd();
+
+                       glBindTexture( GL_TEXTURE_2D, 0 );
+
+                       glBindFramebuffer(GL_FRAMEBUFFER, 0);
+                       glViewport(0,0,WIN_height,WIN_width);
+
                }
                else {
                        SDL_BlitSurface((SDL_Surface*)inp, src, surface_o, dst);
index b2639f8061f5caa812494796801596fd2d7ca5a1..6c2618c2bee459ae290d83883a6c901d5597bf57 100644 (file)
                this->loc.x = (int)this->x;
                this->loc.y = (int)this->y;
 
-//             #ifdef DEBUG_OVERKILL
+               #ifdef DEBUG_OVERKILL
                printf("[UDisplayable::SetXY] x:%d y:%d\n", this->loc.x, this->loc.y);
-//             #endif
+               #endif
        }
 
        // Modifies the position on screen. Meant to avoid embedded retrievals.
 
                if (frameIndex == -1) {
                        if(over)
-                               ctx->OverlayBlit(bitmap, &src, &loc_adj, NULL);
+                               ctx->OverlayBlit(bitmap, &src, &loc_adj, &src);
                        else
-                               ctx->Blit(bitmap, &src, &loc_adj, NULL);
+                               ctx->Blit(bitmap, &src, &loc_adj, &src);
                        return;
                }
 
                frameClip.w = frameWidth;
                frameClip.h = bmp_h;
 
-               if(ctx->GLMode()) {
-                       if(over)
-                               ctx->OverlayBlit(bitmap, &frameClip, &loc_adj, &src); // GL needs data that isn't inside of bitmap.
-                       else
-                               ctx->Blit(bitmap, &frameClip, &loc_adj, &src); // GL needs data that isn't inside of bitmap.
-
-               }
-               else {
-                       if(over)
-                               ctx->OverlayBlit(bitmap, &frameClip, &loc_adj, NULL);
-                       else
-                               ctx->Blit(bitmap, &frameClip, &loc_adj, NULL);
-               }
+               if(over)
+                       ctx->OverlayBlit(bitmap, &frameClip, &loc_adj, &src);
+               else
+                       ctx->Blit(bitmap, &frameClip, &loc_adj, &src);
        }
 
        // Get SDL_Rect for collision calculation. In a base Displayable, it returns the image width.
index 41105c65f9be1eabd2c1e55755fefd9d3070eb24..e1dfd859f392d270b5f88118a5afae58510529fd 100644 (file)
@@ -37,7 +37,7 @@ class DataContainer {
                bool verbose;
                int currentLine;
                bool skip_key_on;
-               bool sw_rendering;
+               int rendering_mode;
                bool fullscreen;
                bool eof;
                char* window_name;
index 8b05151f182e0e72476517e0a1e4324207adf1df..654cb06b2143bd788e601939ab559c338b08ba9e 100644 (file)
@@ -33,10 +33,10 @@ DataContainer::DataContainer() {
        currentLine = 0;
        skip_key_on = false;
        #ifdef USE_ANDROID
-       sw_rendering = true;
+       rendering_mode = 0;
        fullscreen = true;
        #else
-       sw_rendering = false;
+       rendering_mode = 1;
        fullscreen = false;
        #endif
        eof = false;
index 2418d6efd3261d6a2bc1e891320c7bec3647bb58..18c41fe06f352ba0355a55b55b70dc35d93f5f02 100644 (file)
@@ -39,7 +39,12 @@ void Loop() {
 
 void Setup() {
        // Init window
-       GetData()->ctx->InitWindowLogical(GetData()->physical_w, GetData()->physical_h, GetData()->screen_w, GetData()->screen_h, GetData()->fullscreen, (GetData()->sw_rendering ? Software : Accel2d ));
+       if (GetData()->rendering_mode == 0)
+               GetData()->ctx->InitWindowLogical(GetData()->physical_w, GetData()->physical_h, GetData()->screen_w, GetData()->screen_h, GetData()->fullscreen, Software);
+       else if (GetData()->rendering_mode == 1)
+               GetData()->ctx->InitWindowLogical(GetData()->physical_w, GetData()->physical_h, GetData()->screen_w, GetData()->screen_h, GetData()->fullscreen, Accel2d);
+       else if (GetData()->rendering_mode == 2)        
+               GetData()->ctx->InitWindowLogical(GetData()->physical_w, GetData()->physical_h, GetData()->screen_w, GetData()->screen_h, GetData()->fullscreen, OpenGL);
 
        GetData()->window_name = (char*)calloc(sizeof(char), 400);
        sprintf(GetData()->window_name, "%s", "VNDC Interpreter ");
index 4f502a421be701f5b1c040aaafc9d2cbdd6800d0..f260b24d2ac4c3b7b9150464868bfb8c8af6de23 100644 (file)
@@ -27,26 +27,32 @@ int main(int argc, char** argv) {
        bool newgame = false;
        char c;
        #ifdef USE_ANDROID
-       bool software = true;
+       int mode = 0;
        bool fulls = true;
        #else
-       bool software = false;  
+       int mode = 1;
        bool fulls = false;
        #endif
 
-       while((c = getopt(argc, argv, "fwnbvx:y:d:m:s:ch")) != -1) {
+       while((c = getopt(argc, argv, "fr:nbvx:y:d:m:s:ch")) != -1) {
                switch(c) {
                        case 'f':
                                printf("[info] Starting in fullscreen mode.\n");
                                fulls = true;
                                break;
-                       case 'w':
-                               #ifdef USE_ANDROID
-                               printf("[info] Forcing Hardware Acceleration.\n");
-                               #else
-                               printf("[info] Utilizing Software Rendering.\n");
-                               #endif
-                               software = !software;
+                       case 'r':
+                               if(!strcmp(optarg, "sdlsw")) {
+                                       printf("[info] Using SDL software backend.\n");
+                                       mode = 0;
+                               }
+                               if(!strcmp(optarg, "sdlhw")) {
+                                       printf("[info] Using SDL hardware backend.\n");
+                                       mode = 1;
+                               }
+                               if(!strcmp(optarg, "gl")) {
+                                       printf("[info] Using OpenGL backend. (note - experimental)\n");                                 
+                                       mode = 2;
+                               }
                                break;
                        case 'n':
                                printf("[info] New game, not reloading save.\n");
@@ -85,7 +91,7 @@ int main(int argc, char** argv) {
                                break;
                        case 'h':
                                printf("-x size -y size\tStretch display window to WxH\n");
-                               printf("-w\t\tUse Software Rendering (on android: force HW render)\n");
+                               printf("-r mode\t\tUse render mode (sdlsw, sdlhw, gl)\n");
                                printf("-f\t\tFullscreen mode.\n");
                                printf("-n\t\tNew Game. Do not reload default save.\n");
                                printf("-d dir\t\tChange to directory/Run game in directory\n");
@@ -120,7 +126,7 @@ int main(int argc, char** argv) {
 
        GetData()->vndc_enabled = vndc_extensions;
        GetData()->verbose = enable_v;
-       GetData()->sw_rendering = software;
+       GetData()->rendering_mode = mode;
        GetData()->fullscreen = fulls;
 
        if(debug_enable) {
index de37750fecdc3b736e5b7605f0b84d2d4d81c463..d8892711fe3510eae10ffd0f1a67650f47b1dcb0 100644 (file)
@@ -73,7 +73,7 @@ void op_text(char* string) {
                        op_cleartext();
 
                for(int i=0; i < lines; i++) {
-                       printf("[br] %s\n", ptrs[i]);
+                       //printf("[br] %s\n", ptrs[i]);
                        txt->Render(ptrs[i], GetData()->text_x, GetData()->text_y);
                        GetData()->text_y += GetData()->text_gap;
                }