From 379803180921534a42743c1c5fb40647015c3dba Mon Sep 17 00:00:00 2001 From: majortom6 Date: Sun, 19 Feb 2017 11:13:04 -0600 Subject: -removed rectdrawer -removed unused shader -cleaned up main and window --- inc/main.hpp | 1 - inc/opengl/rectdrawer.hpp | 47 ------------- inc/opengl/shaders/basicshader.frag | 9 --- inc/opengl/shaders/basicshader.geom | 33 --------- inc/opengl/shaders/basicshader.vert | 15 ---- inc/sdl/window.hpp | 13 ++-- src/main.cpp | 97 +++++++++++--------------- src/rectdrawer.cpp | 135 ------------------------------------ src/window.cpp | 41 ++++++----- 9 files changed, 69 insertions(+), 322 deletions(-) delete mode 100644 inc/opengl/rectdrawer.hpp delete mode 100644 inc/opengl/shaders/basicshader.frag delete mode 100644 inc/opengl/shaders/basicshader.geom delete mode 100644 inc/opengl/shaders/basicshader.vert delete mode 100644 src/rectdrawer.cpp diff --git a/inc/main.hpp b/inc/main.hpp index 2a8ac44..56c8283 100644 --- a/inc/main.hpp +++ b/inc/main.hpp @@ -9,7 +9,6 @@ #include "sdl/window.hpp" #include "opengl/geoshader.hpp" -#include "opengl/rectdrawer.hpp" #include "opengl/spritebatch.hpp" #endif diff --git a/inc/opengl/rectdrawer.hpp b/inc/opengl/rectdrawer.hpp deleted file mode 100644 index f2ca1e5..0000000 --- a/inc/opengl/rectdrawer.hpp +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef rectdrawer_h -#define rectdrawer_h - - -#include -#include -#include -#include "geoshader.hpp" -#include "graphicsdata.hpp" -#include "spritebatch.hpp" - - -//merge this with spritebatch -class Rectdrawer -{ -public: - Rectdrawer(GeoShader theshader); - ~Rectdrawer(); - - - //void init(); - void begin(); - void end(); - void draw(const GraphicsData& gfxData); - void renderBatch(); - -private: - // Creates all the needed RenderBatches - void createRenderBatches(); - - - GLuint _vbo; - GLuint _vao; - - - std::vector _gfxPtr; ///< This is for sorting - std::vector _gfx; ///< These are the actual glyphs - std::vector _renderBatches; - - GeoShader shader; - - -}; - - -#endif - diff --git a/inc/opengl/shaders/basicshader.frag b/inc/opengl/shaders/basicshader.frag deleted file mode 100644 index 9aa9d0b..0000000 --- a/inc/opengl/shaders/basicshader.frag +++ /dev/null @@ -1,9 +0,0 @@ -#version 320 es -in highp vec3 fColor; -out highp vec4 outColor; - - -void main() -{ - outColor = vec4(fColor, 1.0); -} \ No newline at end of file diff --git a/inc/opengl/shaders/basicshader.geom b/inc/opengl/shaders/basicshader.geom deleted file mode 100644 index 3540d10..0000000 --- a/inc/opengl/shaders/basicshader.geom +++ /dev/null @@ -1,33 +0,0 @@ -#version 320 es -layout(points) in; -layout(line_strip, max_vertices = 64) out; - -in highp vec3 vColor[]; -in highp float vSides[]; -out highp vec3 fColor; - -const float PI = 3.1415926; - -void main() -{ - fColor = vColor[0]; - - for (int i = 0; i <= 4; i++) { // Angle between each side in radians - - vec4 offset = vec4(vSides[0] *.8,-vSides[0] *1.1, 0.0, 0.0); - if(i==1) - offset = vec4(vSides[0] *.8,vSides[0] *1.1, 0.0, 0.0); - if(i==2) - offset = vec4(-vSides[0] *.8,vSides[0] *1.1, 0.0, 0.0); - if(i==3) - offset = vec4(-vSides[0] *.8,-vSides[0] *1.1, 0.0, 0.0); - if(i==4) - offset = vec4(vSides[0] *.8,-vSides[0] *1.1, 0.0, 0.0); - - - gl_Position = gl_in[0].gl_Position - offset; - - EmitVertex(); - } - EndPrimitive(); -} diff --git a/inc/opengl/shaders/basicshader.vert b/inc/opengl/shaders/basicshader.vert deleted file mode 100644 index f17aef5..0000000 --- a/inc/opengl/shaders/basicshader.vert +++ /dev/null @@ -1,15 +0,0 @@ -#version 320 es -in highp vec2 pos; -in highp vec3 color; -in highp float sides; -uniform mat4 MVP; - -out highp vec3 fColor; -out highp float vSides; - -void main() -{ - gl_Position = MVP * vec4(pos, 0.0, 1.0); - fColor = color; - vSides = sides; -} diff --git a/inc/sdl/window.hpp b/inc/sdl/window.hpp index fc0f325..64b4e7b 100644 --- a/inc/sdl/window.hpp +++ b/inc/sdl/window.hpp @@ -12,16 +12,17 @@ class Window { public: Window(int width, int height, const std::string& title); - void Clear(float r, float g, float b, float a); - void swapBuffers(); - bool getClosed(){return closed;}; + ~Window(); + + void Clear(); + void swapBuffers(); + bool getClosed(){return closed;}; - virtual ~Window(); private: SDL_Window* main; - SDL_GLContext glContext; - bool closed; + SDL_GLContext glContext; + bool closed; }; #endif diff --git a/src/main.cpp b/src/main.cpp index 1033651..d5a192d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,78 +2,65 @@ int main() { - srand(time(NULL)); + srand(time(NULL)); - Window main(WINDOW_X, WINDOW_Y, "natures"); + Window main(WINDOW_X, WINDOW_Y, "natures"); List L; Event e; - //New opengl stuff - Transform transform; - Camera camera(glm::vec3(0,0,70), 70.0f, (float)800/(float)600, 0.01f, 1000.0f); - GeoShader shader("./inc/opengl/shaders/theshader"); - GeoShader shader2("./inc/opengl/shaders/basicshader"); - Rectdrawer rectrenderer(shader2); - SpriteBatch _spriteBatch(shader); + Transform transform; + Camera camera(glm::vec3(0,0,70), 70.0f, (float)800/(float)600, 0.01f, 1000.0f); + GeoShader shader("./inc/opengl/shaders/theshader"); + SpriteBatch _spriteBatch(shader); - _spriteBatch.init(); - shader.Bind(); + _spriteBatch.init(); + shader.Bind(); - int uniModel = glGetUniformLocation(shader.m_program, "MVP"); - glUniformMatrix4fv(uniModel, 1, false, &transform.GetMVP(camera)[0][0]); - shader2.Bind(); + int uniModel = glGetUniformLocation(shader.m_program, "MVP"); + glUniformMatrix4fv(uniModel, 1, false, &transform.GetMVP(camera)[0][0]); - int uniModel2 = glGetUniformLocation(shader2.m_program, "MPV"); - glUniformMatrix4fv(uniModel2, 1, false, &transform.GetMVP(camera)[0][0]); - int countedFrames = 0; + Timer fps; + int speed = 60; + bool pause = false; - Timer fps; - int speed = 60; - bool pause = false; - - while(e.gRun()){ - //fps.Start(); + while(e.gRun()){ + fps.Start(); while(e.Poll()){ if(e.gEventType() == SDL_QUIT) e.off(); else if(e.gEventType() == SDL_KEYDOWN) - switch(e.gEvent().key.keysym.sym){ - case SDLK_q: e.off(); break; - case SDLK_EQUALS: speed+=30; break; - case SDLK_MINUS: if(speed >30) speed-=30; break; - case SDLK_SPACE: pause=(pause)?(0):(1); break; - } + switch(e.gEvent().key.keysym.sym){ + case SDLK_q: e.off(); break; + case SDLK_EQUALS: speed+=30; break; + case SDLK_MINUS: if(speed >30) speed-=30; break; + case SDLK_SPACE: pause=(pause)?(0):(1); break; + } } - if(!pause){ - main.Clear(0.0f,0.0f,0.0f,1.0f); + if(!pause){ + main.Clear(); - L.Remove(); - L.Behavior(); - L.Place(); - - shader.Bind(); - _spriteBatch.begin(); - for(std::list::iterator it = L.creatures.begin(); it != L.creatures.end(); it++) - _spriteBatch.draw(it->getGFXD());; + L.Remove(); + L.Behavior(); + L.Place(); - for(std::list::iterator it = L.resources.begin(); it != L.resources.end(); it++) - _spriteBatch.draw(it->getGFXD());; + shader.Bind(); + _spriteBatch.begin(); + + for(std::list::iterator it = L.creatures.begin(); it != L.creatures.end(); it++) + _spriteBatch.draw(it->getGFXD()); - _spriteBatch.end(); - _spriteBatch.renderBatch(); + for(std::list::iterator it = L.resources.begin(); it != L.resources.end(); it++) + _spriteBatch.draw(it->getGFXD()); + + _spriteBatch.end(); + _spriteBatch.renderBatch(); + main.swapBuffers(); + } - main.swapBuffers(); - } - - float avgFPS = countedFrames / ( fps.getTicks() / 1000.f ); - if( avgFPS > 200000 ){ - avgFPS = 0; + if(fps.getTicks() < (1000 / speed)) + SDL_Delay((1000 / speed) - fps.getTicks()); + } - ++countedFrames; - //if(fps.getTicks() < (1000 / speed)) - // SDL_Delay((1000 / speed) - fps.getTicks()); - } - - return 0; + return 0; } diff --git a/src/rectdrawer.cpp b/src/rectdrawer.cpp deleted file mode 100644 index 8714471..0000000 --- a/src/rectdrawer.cpp +++ /dev/null @@ -1,135 +0,0 @@ -#include "opengl/rectdrawer.hpp" - -Rectdrawer::Rectdrawer(GeoShader theshader) : _vbo(0), _vao(0), shader(theshader){ - // Generate the VAO if it isn't already generated - if (_vao == 0) { - glGenVertexArrays(1, &_vao); - } - - // Bind the VAO. All subsequent opengl calls will modify it's state. - glBindVertexArray(_vao); - - //G enerate the VBO if it isn't already generated - if (_vbo == 0) { - glGenBuffers(1, &_vbo); - } - glBindBuffer(GL_ARRAY_BUFFER, _vbo); - - //Tell opengl what attribute arrays we need - GLint posAttrib = glGetAttribLocation(shader.m_program, "pos"); - glEnableVertexAttribArray(posAttrib); - - GLint colAttrib = glGetAttribLocation(shader.m_program, "color"); - glEnableVertexAttribArray(colAttrib); - - GLint sidesAttrib = glGetAttribLocation(shader.m_program, "sides"); - glEnableVertexAttribArray(sidesAttrib); - - // glEnableVertexAttribArray(2); - - //This is the position attribute pointer - glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, - 6 * sizeof(float), 0); //This is the color attribute pointer - glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, - 6 * sizeof(float), (void*) (2 * sizeof(float))); //This is the UV attribute pointer - glVertexAttribPointer(sidesAttrib, 1, GL_FLOAT, GL_FALSE, - 6 * sizeof(float), (void*) (5 * sizeof(float))); - glBindVertexArray(0); - - -} - -Rectdrawer::~Rectdrawer() -{ -} - -void Rectdrawer::draw(const GraphicsData& gfxData) { - _gfx.emplace_back(gfxData); -} - - - -void Rectdrawer::begin() { - _renderBatches.clear(); - _gfx.clear(); -} - -void Rectdrawer::end() { - // Set up all pointers for fast sorting - _gfxPtr.resize(_gfx.size()); - for (int i = 0; i < _gfx.size(); i++) { - _gfxPtr[i] = &_gfx[i]; - } - - //sortGlyphs(); - createRenderBatches(); -} - -void Rectdrawer::renderBatch() { - - // Bind our VAO. This sets up the opengl state we need, including the - // vertex attribute pointers and it binds the VBO - glBindVertexArray(_vao); - - for (int i = 0; i < _renderBatches.size(); i++) { - glDrawArrays(GL_POINTS, _renderBatches[i].offset, _renderBatches[i].numVertices); - } - glBindVertexArray(0); -} - - -// needs edit here for rectangle instead of other geometry -void Rectdrawer::createRenderBatches() { - // This will store all the vertices that we need to upload - std::vector vertices; - // Resize the buffer to the exact size we need so we can treat - // it like an array - vertices.resize(_gfxPtr.size() * 6); - - if (_gfxPtr.empty()) { - return; - } - - int offset = 0; // current offset - int cv = 0; // current vertex - - //Add the first batch - _renderBatches.emplace_back(offset, 6); - vertices[cv++] = _gfxPtr[0]->x; - vertices[cv++] = _gfxPtr[0]->y; - vertices[cv++] = _gfxPtr[0]->r; - vertices[cv++] = _gfxPtr[0]->g; - vertices[cv++] = _gfxPtr[0]->b; - vertices[cv++] = _gfxPtr[0]->sides; - - offset += 6; - - //Add all the rest of the glyphs - //std::cout << "ptr size = " << _gfxPtr.size() << std::endl; - for (int cg = 1; cg < _gfxPtr.size(); cg++) { - - _renderBatches.back().numVertices += 6; - - vertices[cv++] = _gfxPtr[cg]->x; - vertices[cv++] = _gfxPtr[cg]->y; - vertices[cv++] = _gfxPtr[cg]->r; - vertices[cv++] = _gfxPtr[cg]->g; - vertices[cv++] = _gfxPtr[cg]->b; - vertices[cv++] = _gfxPtr[cg]->sides; - - offset += 6; - } - - - - // Bind our VBO - glBindBuffer(GL_ARRAY_BUFFER, _vbo); - // Orphan the buffer (for speed) - glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), nullptr, GL_DYNAMIC_DRAW); - // Upload the data - glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(float), vertices.data()); - - // Unbind the VBO - glBindBuffer(GL_ARRAY_BUFFER, 0); - -} diff --git a/src/window.cpp b/src/window.cpp index 77d9549..9306b15 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -2,23 +2,22 @@ Window::Window(int width, int height, const std::string& title) { - SDL_Init(SDL_INIT_EVERYTHING); - - SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32); - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - - main = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height,SDL_WINDOW_OPENGL); - glContext = SDL_GL_CreateContext(main); - GLenum status = glewInit(); - closed = false; - - if(status != GLEW_OK) - std::cerr << "Failiure to init." << std::endl; - + SDL_Init(SDL_INIT_EVERYTHING); + + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + + main = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height,SDL_WINDOW_OPENGL); + glContext = SDL_GL_CreateContext(main); + GLenum status = glewInit(); + closed = false; + + if(status != GLEW_OK) + std::cerr << "Failiure to init." << std::endl; } Window::~Window() @@ -30,12 +29,12 @@ Window::~Window() void Window::swapBuffers() { - SDL_GL_SwapWindow(main); + SDL_GL_SwapWindow(main); } -void Window::Clear(float r, float g, float b, float a) +void Window::Clear() { - glClearColor(r, g, b, a); - glClear(GL_COLOR_BUFFER_BIT); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); } -- cgit v1.2.3